CreateShareWithShareeOperation.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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.OCFile;
  23. import com.owncloud.android.lib.common.OwnCloudClient;
  24. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  25. import com.owncloud.android.lib.resources.files.FileUtils;
  26. import com.owncloud.android.lib.resources.shares.CreateShareRemoteOperation;
  27. import com.owncloud.android.lib.resources.shares.OCShare;
  28. import com.owncloud.android.lib.resources.shares.ShareType;
  29. import com.owncloud.android.operations.common.SyncOperation;
  30. import java.util.ArrayList;
  31. import java.util.Arrays;
  32. import java.util.List;
  33. /**
  34. * Creates a new private share for a given file.
  35. */
  36. public class CreateShareWithShareeOperation extends SyncOperation {
  37. private String path;
  38. private String shareeName;
  39. private ShareType shareType;
  40. private int permissions;
  41. private static final List<ShareType> supportedShareTypes = new ArrayList<>(Arrays.asList(ShareType.USER,
  42. ShareType.GROUP,
  43. ShareType.FEDERATED,
  44. ShareType.EMAIL,
  45. ShareType.ROOM,
  46. ShareType.CIRCLE));
  47. /**
  48. * Constructor.
  49. *
  50. * @param path Full path of the file/folder being shared.
  51. * @param shareeName User or group name of the target sharee.
  52. * @param shareType Type of share determines type of sharee; {@link ShareType#USER} and {@link ShareType#GROUP}
  53. * are the only valid values for the moment.
  54. * @param permissions Share permissions key as detailed in
  55. * https://doc.owncloud.org/server/8.2/developer_manual/core/ocs-share-api.html .
  56. */
  57. public CreateShareWithShareeOperation(String path, String shareeName, ShareType shareType, int permissions) {
  58. if (!supportedShareTypes.contains(shareType)) {
  59. throw new IllegalArgumentException("Illegal share type " + shareType);
  60. }
  61. this.path = path;
  62. this.shareeName = shareeName;
  63. this.shareType = shareType;
  64. this.permissions = permissions;
  65. }
  66. @Override
  67. protected RemoteOperationResult run(OwnCloudClient client) {
  68. CreateShareRemoteOperation operation = new CreateShareRemoteOperation(
  69. path,
  70. shareType,
  71. shareeName,
  72. false,
  73. "",
  74. permissions
  75. );
  76. operation.setGetShareDetails(true);
  77. RemoteOperationResult result = operation.execute(client);
  78. if (result.isSuccess() && result.getData().size() > 0) {
  79. OCShare share = (OCShare) result.getData().get(0);
  80. updateData(share);
  81. }
  82. return result;
  83. }
  84. private void updateData(OCShare share) {
  85. // Update DB with the response
  86. share.setPath(path);
  87. share.setFolder(path.endsWith(FileUtils.PATH_SEPARATOR));
  88. getStorageManager().saveShare(share);
  89. // Update OCFile with data from share: ShareByLink and publicLink
  90. OCFile file = getStorageManager().getFileByPath(path);
  91. if (file!=null) {
  92. file.setSharedWithSharee(true); // TODO - this should be done by the FileContentProvider, as part of getStorageManager().saveShare(share)
  93. getStorageManager().saveFile(file);
  94. }
  95. }
  96. public String getPath() {
  97. return this.path;
  98. }
  99. }