CreateShareWithShareeOperation.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /**
  2. * ownCloud Android client application
  3. *
  4. * @author masensio
  5. * @author David A. Velasco
  6. * Copyright (C) 2015 ownCloud Inc.
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. package com.owncloud.android.operations;
  22. import com.owncloud.android.datamodel.FileDataStorageManager;
  23. import com.owncloud.android.datamodel.OCFile;
  24. import com.owncloud.android.lib.common.OwnCloudClient;
  25. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  26. import com.owncloud.android.lib.resources.files.FileUtils;
  27. import com.owncloud.android.lib.resources.shares.CreateRemoteShareOperation;
  28. import com.owncloud.android.lib.resources.shares.OCShare;
  29. import com.owncloud.android.lib.resources.shares.ShareType;
  30. import com.owncloud.android.operations.common.SyncOperation;
  31. /**
  32. * Creates a new private share for a given file.
  33. */
  34. public class CreateShareWithShareeOperation extends SyncOperation {
  35. protected FileDataStorageManager mStorageManager;
  36. private String mPath;
  37. private String mShareeName;
  38. private ShareType mShareType;
  39. private int mPermissions;
  40. /**
  41. * Constructor.
  42. *
  43. * @param path Full path of the file/folder being shared.
  44. * @param shareeName User or group name of the target sharee.
  45. * @param shareType Type of share determines type of sharee; {@link ShareType#USER} and {@link ShareType#GROUP}
  46. * are the only valid values for the moment.
  47. * @param permissions Share permissions key as detailed in
  48. * https://doc.owncloud.org/server/8.2/developer_manual/core/ocs-share-api.html .
  49. */
  50. public CreateShareWithShareeOperation(String path, String shareeName, ShareType shareType, int permissions) {
  51. if (!ShareType.USER.equals(shareType) && !ShareType.GROUP.equals(shareType)
  52. && !ShareType.FEDERATED.equals(shareType) && !ShareType.EMAIL.equals(shareType)
  53. && !ShareType.ROOM.equals(shareType)) {
  54. throw new IllegalArgumentException("Illegal share type " + shareType);
  55. }
  56. mPath = path;
  57. mShareeName = shareeName;
  58. mShareType = shareType;
  59. mPermissions = permissions;
  60. }
  61. @Override
  62. protected RemoteOperationResult run(OwnCloudClient client) {
  63. CreateRemoteShareOperation operation = new CreateRemoteShareOperation(
  64. mPath,
  65. mShareType,
  66. mShareeName,
  67. false,
  68. "",
  69. mPermissions
  70. );
  71. operation.setGetShareDetails(true);
  72. RemoteOperationResult result = operation.execute(client);
  73. if (result.isSuccess() && result.getData().size() > 0) {
  74. OCShare share = (OCShare) result.getData().get(0);
  75. updateData(share);
  76. }
  77. return result;
  78. }
  79. public String getPath() {
  80. return mPath;
  81. }
  82. private void updateData(OCShare share) {
  83. // Update DB with the response
  84. share.setPath(mPath);
  85. share.setIsFolder(mPath.endsWith(FileUtils.PATH_SEPARATOR));
  86. getStorageManager().saveShare(share);
  87. // Update OCFile with data from share: ShareByLink and publicLink
  88. OCFile file = getStorageManager().getFileByPath(mPath);
  89. if (file!=null) {
  90. file.setShareWithSharee(true); // TODO - this should be done by the FileContentProvider, as part of getStorageManager().saveShare(share)
  91. getStorageManager().saveFile(file);
  92. }
  93. }
  94. }