CreateShareWithShareeOperation.java 4.0 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. /**
  23. * Creates a new private share for a given file
  24. */
  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.common.operations.RemoteOperationResult;
  29. import com.owncloud.android.lib.resources.files.FileUtils;
  30. import com.owncloud.android.lib.resources.shares.CreateRemoteShareOperation;
  31. import com.owncloud.android.lib.resources.shares.OCShare;
  32. import com.owncloud.android.lib.resources.shares.ShareType;
  33. import com.owncloud.android.operations.common.SyncOperation;
  34. public class CreateShareWithShareeOperation extends SyncOperation {
  35. protected FileDataStorageManager mStorageManager;
  36. private String mPath;
  37. private String mShareeName;
  38. private ShareType mShareType;
  39. /**
  40. * Constructor.
  41. *
  42. * @param path Full path of the file/folder being shared.
  43. * @param shareeName User or group name of the target sharee.
  44. * @param shareType Type of share determines type of sharee; {@link ShareType#USER} and {@link ShareType#GROUP}
  45. * are the only valid values for the moment.
  46. */
  47. public CreateShareWithShareeOperation(String path, String shareeName, ShareType shareType) {
  48. if (!ShareType.USER.equals(shareType) && !ShareType.GROUP.equals(shareType)) {
  49. throw new IllegalArgumentException("Illegal share type " + shareType);
  50. }
  51. mPath = path;
  52. mShareeName = shareeName;
  53. mShareType = shareType;
  54. }
  55. @Override
  56. protected RemoteOperationResult run(OwnCloudClient client) {
  57. // Check if the share link already exists
  58. // TODO or not
  59. /*
  60. RemoteOperation operation = new GetRemoteSharesForFileOperation(mPath, false, false);
  61. RemoteOperationResult result = operation.execute(client);
  62. if (!result.isSuccess() || result.getData().size() <= 0) {
  63. */
  64. CreateRemoteShareOperation operation = new CreateRemoteShareOperation(
  65. mPath,
  66. mShareType,
  67. mShareeName,
  68. false,
  69. "",
  70. OCShare.DEFAULT_PERMISSION
  71. );
  72. operation.setGetShareDetails(true);
  73. RemoteOperationResult result = operation.execute(client);
  74. if (result.isSuccess()) {
  75. if (result.getData().size() > 0) {
  76. OCShare share = (OCShare) result.getData().get(0);
  77. updateData(share);
  78. }
  79. }
  80. return result;
  81. }
  82. public String getPath() {
  83. return mPath;
  84. }
  85. private void updateData(OCShare share) {
  86. // Update DB with the response
  87. share.setPath(mPath);
  88. share.setIsFolder(mPath.endsWith(FileUtils.PATH_SEPARATOR));
  89. getStorageManager().saveShare(share);
  90. // Update OCFile with data from share: ShareByLink and publicLink
  91. OCFile file = getStorageManager().getFileByPath(mPath);
  92. if (file!=null) {
  93. file.setShareWithSharee(true); // TODO - this should be done by the FileContentProvider, as part of getStorageManager().saveShare(share)
  94. getStorageManager().saveFile(file);
  95. }
  96. }
  97. }