CreateShareOperation.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /* ownCloud Android client application
  2. * Copyright (C) 2014 ownCloud Inc.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2,
  6. * as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. */
  17. package com.owncloud.android.operations;
  18. /**
  19. * Creates a new share from a given file
  20. *
  21. * @author masensio
  22. *
  23. */
  24. import android.content.Intent;
  25. import com.owncloud.android.datamodel.FileDataStorageManager;
  26. import com.owncloud.android.datamodel.OCFile;
  27. import com.owncloud.android.lib.common.OwnCloudClient;
  28. import com.owncloud.android.lib.resources.shares.OCShare;
  29. import com.owncloud.android.lib.common.operations.RemoteOperation;
  30. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  31. import com.owncloud.android.lib.resources.shares.GetRemoteSharesForFileOperation;
  32. import com.owncloud.android.lib.resources.shares.ShareType;
  33. import com.owncloud.android.lib.resources.shares.CreateRemoteShareOperation;
  34. import com.owncloud.android.lib.resources.files.FileUtils;
  35. import com.owncloud.android.operations.common.SyncOperation;
  36. import com.owncloud.android.utils.Log_OC;
  37. public class CreateShareOperation extends SyncOperation {
  38. private static final String TAG = CreateShareOperation.class.getSimpleName();
  39. protected FileDataStorageManager mStorageManager;
  40. private String mPath;
  41. private ShareType mShareType;
  42. private String mShareWith;
  43. private boolean mPublicUpload;
  44. private String mPassword;
  45. private int mPermissions;
  46. private Intent mSendIntent;
  47. /**
  48. * Constructor
  49. * @param path Full path of the file/folder being shared. Mandatory argument
  50. * @param shareType ‘0’ = user, ‘1’ = group, ‘3’ = Public link. Mandatory argument
  51. * @param shareWith User/group ID with who the file should be shared. This is mandatory for shareType of 0 or 1
  52. * @param publicUpload If ‘false’ (default) public cannot upload to a public shared folder.
  53. * If ‘true’ public can upload to a shared folder. Only available for public link shares
  54. * @param password Password to protect a public link share. Only available for public link shares
  55. * @param permissions 1 - Read only – Default for “public” shares
  56. * 2 - Update
  57. * 4 - Create
  58. * 8 - Delete
  59. * 16- Re-share
  60. * 31- All above – Default for “private” shares
  61. * For user or group shares.
  62. * To obtain combinations, add the desired values together.
  63. * For instance, for “Re-Share”, “delete”, “read”, “update”, add 16+8+2+1 = 27.
  64. */
  65. public CreateShareOperation(String path, ShareType shareType, String shareWith, boolean publicUpload,
  66. String password, int permissions, Intent sendIntent) {
  67. mPath = path;
  68. mShareType = shareType;
  69. mShareWith = shareWith;
  70. mPublicUpload = publicUpload;
  71. mPassword = password;
  72. mPermissions = permissions;
  73. mSendIntent = sendIntent;
  74. }
  75. @Override
  76. protected RemoteOperationResult run(OwnCloudClient client) {
  77. RemoteOperation operation = null;
  78. // Check if the share link already exists
  79. operation = new GetRemoteSharesForFileOperation(mPath, false, false);
  80. RemoteOperationResult result = ((GetRemoteSharesForFileOperation)operation).execute(client);
  81. if (!result.isSuccess() || result.getData().size() <= 0) {
  82. operation = new CreateRemoteShareOperation(mPath, mShareType, mShareWith, mPublicUpload, mPassword, mPermissions);
  83. result = ((CreateRemoteShareOperation)operation).execute(client);
  84. }
  85. if (result.isSuccess()) {
  86. if (result.getData().size() > 0) {
  87. OCShare share = (OCShare) result.getData().get(0);
  88. updateData(share);
  89. }
  90. }
  91. return result;
  92. }
  93. public Intent getSendIntent() {
  94. return mSendIntent;
  95. }
  96. private void updateData(OCShare share) {
  97. // Update DB with the response
  98. share.setPath(mPath);
  99. if (mPath.endsWith(FileUtils.PATH_SEPARATOR)) {
  100. share.setIsFolder(true);
  101. } else {
  102. share.setIsFolder(false);
  103. }
  104. share.setPermissions(mPermissions);
  105. getStorageManager().saveShare(share);
  106. // Update OCFile with data from share: ShareByLink and publicLink
  107. OCFile file = getStorageManager().getFileByPath(mPath);
  108. if (file!=null) {
  109. mSendIntent.putExtra(Intent.EXTRA_TEXT, share.getShareLink());
  110. file.setPublicLink(share.getShareLink());
  111. file.setShareByLink(true);
  112. getStorageManager().saveFile(file);
  113. Log_OC.d(TAG, "Public Link = " + file.getPublicLink());
  114. }
  115. }
  116. }