external.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /**
  2. * Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
  3. *
  4. * This file is licensed under the Affero General Public License version 3
  5. * or later.
  6. *
  7. * See the COPYING-README file.
  8. *
  9. */
  10. (function() {
  11. OCA.Sharing = OCA.Sharing || {}
  12. /**
  13. * Shows "add external share" dialog.
  14. *
  15. * @param {Object} share the share
  16. * @param {String} share.remote remote server URL
  17. * @param {String} share.owner owner name
  18. * @param {String} share.name name of the shared folder
  19. * @param {String} share.token authentication token
  20. * @param {boolean} passwordProtected true if the share is password protected
  21. * @param {Function} callback the callback
  22. */
  23. OCA.Sharing.showAddExternalDialog = function(share, passwordProtected, callback) {
  24. var remote = share.remote;
  25. var owner = share.ownerDisplayName || share.owner;
  26. var name = share.name;
  27. var remoteClean = (remote.substr(0, 8) === 'https://') ? remote.substr(8) : remote.substr(7);
  28. if (!passwordProtected) {
  29. OC.dialogs.confirm(
  30. t(
  31. 'files_sharing',
  32. 'Do you want to add the remote share {name} from {owner}@{remote}?',
  33. { name: name, owner: owner, remote: remoteClean }
  34. ),
  35. t('files_sharing', 'Remote share'),
  36. function(result) {
  37. callback(result, share);
  38. },
  39. true
  40. ).then(this._adjustDialog);
  41. } else {
  42. OC.dialogs.prompt(
  43. t(
  44. 'files_sharing',
  45. 'Do you want to add the remote share {name} from {owner}@{remote}?',
  46. { name: name, owner: owner, remote: remoteClean }
  47. ),
  48. t('files_sharing', 'Remote share'),
  49. function(result, password) {
  50. share.password = password;
  51. callback(result, share);
  52. },
  53. true,
  54. t('files_sharing', 'Remote share password'),
  55. true
  56. ).then(this._adjustDialog);
  57. }
  58. };
  59. OCA.Sharing._adjustDialog = function() {
  60. var $dialog = $('.oc-dialog:visible');
  61. var $buttons = $dialog.find('button');
  62. // hack the buttons
  63. $dialog.find('.ui-icon').remove();
  64. $buttons.eq(1).text(t('core', 'Cancel'));
  65. $buttons.eq(2).text(t('files_sharing', 'Add remote share'));
  66. };
  67. OCA.Sharing.ExternalShareDialogPlugin = {
  68. filesApp: null,
  69. attach: function(filesApp) {
  70. var self = this;
  71. this.filesApp = filesApp;
  72. this.processIncomingShareFromUrl();
  73. if (!$('#header').find('div.notifications').length) {
  74. // No notification app, display the modal
  75. this.processSharesToConfirm();
  76. }
  77. $('body').on('OCA.Notification.Action', function(e) {
  78. if (e.notification.app === 'files_sharing' && e.notification.object_type === 'remote_share' && e.action.type === 'POST') {
  79. // User accepted a remote share reload
  80. self.filesApp.fileList.reload();
  81. }
  82. });
  83. },
  84. /**
  85. * Process incoming remote share that might have been passed
  86. * through the URL
  87. */
  88. processIncomingShareFromUrl: function() {
  89. var fileList = this.filesApp.fileList;
  90. var params = OC.Util.History.parseUrlQuery();
  91. // manually add server-to-server share
  92. if (params.remote && params.token && params.name) {
  93. var callbackAddShare = function(result, share) {
  94. var password = share.password || '';
  95. if (result) {
  96. $.post(
  97. OC.generateUrl('apps/federatedfilesharing/askForFederatedShare'),
  98. {
  99. remote: share.remote,
  100. token: share.token,
  101. owner: share.owner,
  102. ownerDisplayName: share.ownerDisplayName || share.owner,
  103. name: share.name,
  104. password: password
  105. }
  106. ).done(function(data) {
  107. if (data.hasOwnProperty('legacyMount')) {
  108. fileList.reload();
  109. } else {
  110. OC.Notification.showTemporary(data.message);
  111. }
  112. }).fail(function(data) {
  113. OC.Notification.showTemporary(JSON.parse(data.responseText).message);
  114. });
  115. }
  116. };
  117. // clear hash, it is unlikely that it contain any extra parameters
  118. location.hash = '';
  119. params.passwordProtected = parseInt(params.protected, 10) === 1;
  120. OCA.Sharing.showAddExternalDialog(
  121. params,
  122. params.passwordProtected,
  123. callbackAddShare
  124. );
  125. }
  126. },
  127. /**
  128. * Retrieve a list of remote shares that need to be approved
  129. */
  130. processSharesToConfirm: function() {
  131. var fileList = this.filesApp.fileList;
  132. // check for new server-to-server shares which need to be approved
  133. $.get(OC.generateUrl('/apps/files_sharing/api/externalShares'), {}, function(shares) {
  134. var index;
  135. for (index = 0; index < shares.length; ++index) {
  136. OCA.Sharing.showAddExternalDialog(
  137. shares[index],
  138. false,
  139. function(result, share) {
  140. if (result) {
  141. // Accept
  142. $.post(OC.generateUrl('/apps/files_sharing/api/externalShares'), {id: share.id})
  143. .then(function() {
  144. fileList.reload();
  145. });
  146. } else {
  147. // Delete
  148. $.ajax({
  149. url: OC.generateUrl('/apps/files_sharing/api/externalShares/'+share.id),
  150. type: 'DELETE'
  151. });
  152. }
  153. }
  154. );
  155. }});
  156. }
  157. };
  158. })(OC, OCA);
  159. OC.Plugins.register('OCA.Files.App', OCA.Sharing.ExternalShareDialogPlugin);