external.js 4.8 KB

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