CreateShareOperation.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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.network.OwnCloudClient;
  28. import com.owncloud.android.lib.operations.common.OCShare;
  29. import com.owncloud.android.lib.operations.common.RemoteOperationResult;
  30. import com.owncloud.android.lib.operations.common.ShareType;
  31. import com.owncloud.android.lib.operations.remote.CreateShareRemoteOperation;
  32. import com.owncloud.android.lib.utils.FileUtils;
  33. import com.owncloud.android.operations.common.SyncOperation;
  34. import com.owncloud.android.utils.Log_OC;
  35. public class CreateShareOperation extends SyncOperation {
  36. private static final String TAG = CreateShareOperation.class.getSimpleName();
  37. protected FileDataStorageManager mStorageManager;
  38. private String mPath;
  39. private ShareType mShareType;
  40. private String mShareWith;
  41. private boolean mPublicUpload;
  42. private String mPassword;
  43. private int mPermissions;
  44. private Intent mSendIntent;
  45. /**
  46. * Constructor
  47. * @param path Full path of the file/folder being shared. Mandatory argument
  48. * @param shareType ‘0’ = user, ‘1’ = group, ‘3’ = Public link. Mandatory argument
  49. * @param shareWith User/group ID with who the file should be shared. This is mandatory for shareType of 0 or 1
  50. * @param publicUpload If ‘false’ (default) public cannot upload to a public shared folder.
  51. * If ‘true’ public can upload to a shared folder. Only available for public link shares
  52. * @param password Password to protect a public link share. Only available for public link shares
  53. * @param permissions 1 - Read only – Default for “public” shares
  54. * 2 - Update
  55. * 4 - Create
  56. * 8 - Delete
  57. * 16- Re-share
  58. * 31- All above – Default for “private” shares
  59. * For user or group shares.
  60. * To obtain combinations, add the desired values together.
  61. * For instance, for “Re-Share”, “delete”, “read”, “update”, add 16+8+2+1 = 27.
  62. */
  63. public CreateShareOperation(String path, ShareType shareType, String shareWith, boolean publicUpload,
  64. String password, int permissions, Intent sendIntent) {
  65. mPath = path;
  66. mShareType = shareType;
  67. mShareWith = shareWith;
  68. mPublicUpload = publicUpload;
  69. mPassword = password;
  70. mPermissions = permissions;
  71. mSendIntent = sendIntent;
  72. }
  73. @Override
  74. protected RemoteOperationResult run(OwnCloudClient client) {
  75. CreateShareRemoteOperation operation = new CreateShareRemoteOperation(mPath, mShareType, mShareWith, mPublicUpload, mPassword, mPermissions);
  76. RemoteOperationResult result = operation.execute(client);
  77. if (result.isSuccess()) {
  78. if (result.getData().size() > 0) {
  79. OCShare share = (OCShare) result.getData().get(0);
  80. // Update DB with the response
  81. share.setPath(mPath);
  82. if (mPath.endsWith(FileUtils.PATH_SEPARATOR)) {
  83. share.setIsFolder(true);
  84. } else {
  85. share.setIsFolder(false);
  86. }
  87. share.setPermissions(mPermissions);
  88. getStorageManager().saveShare(share);
  89. // Update OCFile with data from share: ShareByLink and publicLink
  90. OCFile file = getStorageManager().getFileByPath(mPath);
  91. if (file!=null) {
  92. mSendIntent.putExtra(Intent.EXTRA_TEXT, share.getShareLink());
  93. file.setPublicLink(share.getShareLink());
  94. file.setShareByLink(true);
  95. getStorageManager().saveFile(file);
  96. Log_OC.d(TAG, "Public Link = " + file.getPublicLink());
  97. }
  98. }
  99. }
  100. return result;
  101. }
  102. public Intent getSendIntent() {
  103. return mSendIntent;
  104. }
  105. }