external.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. var addExternalShare = function (remote, token, owner, name, password) {
  12. return $.post(OC.generateUrl('apps/files_sharing/external'), {
  13. remote: remote,
  14. token: token,
  15. owner: owner,
  16. name: name,
  17. password: password
  18. });
  19. };
  20. /**
  21. * Shows "add external share" dialog.
  22. *
  23. * @param {String} remote remote server URL
  24. * @param {String} owner owner name
  25. * @param {String} name name of the shared folder
  26. * @param {String} token authentication token
  27. * @param {bool} passwordProtected true if the share is password protected
  28. */
  29. OCA.Sharing.showAddExternalDialog = function (remote, token, owner, name, passwordProtected) {
  30. var remoteClean = (remote.substr(0, 8) === 'https://') ? remote.substr(8) : remote.substr(7);
  31. var callback = function (add, password) {
  32. password = password || '';
  33. if (add) {
  34. addExternalShare(remote, token, owner, name, password).then(function (result) {
  35. if (result.status === 'error') {
  36. OC.Notification.show(result.data.message);
  37. } else {
  38. FileList.reload();
  39. }
  40. });
  41. }
  42. };
  43. if (!passwordProtected) {
  44. OC.dialogs.confirm(
  45. t(
  46. 'files_sharing',
  47. 'Do you want to add the remote share {name} from {owner}@{remote}?',
  48. {name: name, owner: owner, remote: remoteClean}
  49. ),
  50. t('files_sharing','Remote share'),
  51. callback,
  52. true
  53. ).then(this._adjustDialog);
  54. } else {
  55. OC.dialogs.prompt(
  56. t(
  57. 'files_sharing',
  58. 'Do you want to add the remote share {name} from {owner}@{remote}?',
  59. {name: name, owner: owner, remote: remoteClean}
  60. ),
  61. t('files_sharing','Remote share'),
  62. callback,
  63. true,
  64. t('files_sharing','Remote share password'),
  65. true
  66. ).then(this._adjustDialog);
  67. }
  68. };
  69. OCA.Sharing._adjustDialog = function() {
  70. var $dialog = $('.oc-dialog:visible');
  71. var $buttons = $dialog.find('button');
  72. // hack the buttons
  73. $dialog.find('.ui-icon').remove();
  74. $buttons.eq(0).text(t('core', 'Cancel'));
  75. $buttons.eq(1).text(t('core', 'Add remote share'));
  76. };
  77. })();
  78. $(document).ready(function () {
  79. // FIXME: HACK: do not init when running unit tests, need a better way
  80. if (!window.TESTING && OCA.Files) {// only run in the files app
  81. var params = OC.Util.History.parseUrlQuery();
  82. if (params.remote && params.token && params.owner && params.name) {
  83. // clear hash, it is unlikely that it contain any extra parameters
  84. location.hash = '';
  85. params.passwordProtected = parseInt(params.protected, 10) === 1;
  86. OCA.Sharing.showAddExternalDialog(
  87. params.remote,
  88. params.token,
  89. params.owner,
  90. params.name,
  91. params.passwordProtected
  92. );
  93. }
  94. }
  95. });