UpdateSharePermissionsOperation.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /**
  2. * ownCloud Android client application
  3. *
  4. * @author David A. Velasco
  5. * Copyright (C) 2015 ownCloud Inc.
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2,
  9. * as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. package com.owncloud.android.operations;
  21. import com.owncloud.android.lib.common.OwnCloudClient;
  22. import com.owncloud.android.lib.common.operations.RemoteOperation;
  23. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  24. import com.owncloud.android.lib.resources.files.FileUtils;
  25. import com.owncloud.android.lib.resources.shares.GetRemoteShareOperation;
  26. import com.owncloud.android.lib.resources.shares.OCShare;
  27. import com.owncloud.android.lib.resources.shares.UpdateRemoteShareOperation;
  28. import com.owncloud.android.operations.common.SyncOperation;
  29. /**
  30. * Updates an existing private share for a given file
  31. */
  32. public class UpdateSharePermissionsOperation extends SyncOperation {
  33. private long mShareId;
  34. private int mPermissions;
  35. private String mPath;
  36. /**
  37. * Constructor
  38. *
  39. * @param shareId Private {@link OCShare} to update. Mandatory argument
  40. */
  41. public UpdateSharePermissionsOperation(long shareId) {
  42. mShareId = shareId;
  43. mPermissions = -1;
  44. }
  45. /**
  46. * Set permissions to update in private share.
  47. *
  48. * @param permissions Permissions to set to the private share.
  49. * Values <= 0 result in no update applied to the permissions.
  50. */
  51. public void setPermissions(int permissions) {
  52. mPermissions = permissions;
  53. }
  54. @Override
  55. protected RemoteOperationResult run(OwnCloudClient client) {
  56. OCShare share = getStorageManager().getShareById(mShareId); // ShareType.USER | ShareType.GROUP
  57. if (share == null) {
  58. // TODO try to get remote share before failing?
  59. return new RemoteOperationResult(
  60. RemoteOperationResult.ResultCode.SHARE_NOT_FOUND
  61. );
  62. }
  63. mPath = share.getPath();
  64. // Update remote share with password
  65. UpdateRemoteShareOperation updateOp = new UpdateRemoteShareOperation(
  66. share.getRemoteId()
  67. );
  68. updateOp.setPermissions(mPermissions);
  69. RemoteOperationResult result = updateOp.execute(client);
  70. if (result.isSuccess()) {
  71. RemoteOperation getShareOp = new GetRemoteShareOperation(share.getRemoteId());
  72. result = getShareOp.execute(client);
  73. if (result.isSuccess()) {
  74. share = (OCShare) result.getData().get(0);
  75. // TODO check permissions are being saved
  76. updateData(share);
  77. }
  78. }
  79. return result;
  80. }
  81. public String getPath() {
  82. return mPath;
  83. }
  84. private void updateData(OCShare share) {
  85. // Update DB with the response
  86. share.setPath(mPath); // TODO - check if may be moved to UpdateRemoteShareOperation
  87. if (mPath.endsWith(FileUtils.PATH_SEPARATOR)) {
  88. share.setIsFolder(true);
  89. } else {
  90. share.setIsFolder(false);
  91. }
  92. getStorageManager().saveShare(share);
  93. }
  94. }