CreateShareWithShareeOperation.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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)) {
  53. throw new IllegalArgumentException("Illegal share type " + shareType);
  54. }
  55. mPath = path;
  56. mShareeName = shareeName;
  57. mShareType = shareType;
  58. mPermissions = permissions;
  59. }
  60. @Override
  61. protected RemoteOperationResult run(OwnCloudClient client) {
  62. CreateRemoteShareOperation operation = new CreateRemoteShareOperation(
  63. mPath,
  64. mShareType,
  65. mShareeName,
  66. false,
  67. "",
  68. mPermissions
  69. );
  70. operation.setGetShareDetails(true);
  71. RemoteOperationResult result = operation.execute(client);
  72. if (result.isSuccess() && result.getData().size() > 0) {
  73. OCShare share = (OCShare) result.getData().get(0);
  74. updateData(share);
  75. }
  76. return result;
  77. }
  78. public String getPath() {
  79. return mPath;
  80. }
  81. private void updateData(OCShare share) {
  82. // Update DB with the response
  83. share.setPath(mPath);
  84. share.setIsFolder(mPath.endsWith(FileUtils.PATH_SEPARATOR));
  85. getStorageManager().saveShare(share);
  86. // Update OCFile with data from share: ShareByLink and publicLink
  87. OCFile file = getStorageManager().getFileByPath(mPath);
  88. if (file!=null) {
  89. file.setShareWithSharee(true); // TODO - this should be done by the FileContentProvider, as part of getStorageManager().saveShare(share)
  90. getStorageManager().saveFile(file);
  91. }
  92. }
  93. }