GetSharesOperation.java 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* ownCloud Android client application
  2. * Copyright (C) 2012-2013 ownCloud Inc.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2,
  6. * as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. */
  17. package com.owncloud.android.operations;
  18. import java.util.ArrayList;
  19. import com.owncloud.android.datamodel.FileDataStorageManager;
  20. import com.owncloud.android.datamodel.OCFile;
  21. import com.owncloud.android.lib.network.OwnCloudClient;
  22. import com.owncloud.android.lib.operations.common.RemoteOperation;
  23. import com.owncloud.android.lib.operations.common.RemoteOperationResult;
  24. import com.owncloud.android.lib.operations.common.OCShare;
  25. import com.owncloud.android.lib.operations.common.ShareType;
  26. import com.owncloud.android.lib.operations.remote.GetRemoteSharesOperation;
  27. import com.owncloud.android.lib.utils.FileUtils;
  28. import com.owncloud.android.operations.common.SyncOperation;
  29. import com.owncloud.android.utils.Log_OC;
  30. /**
  31. * Access to remote operation to get the share files/folders
  32. * Save the data in Database
  33. *
  34. * @author masensio
  35. * @author David A. Velasco
  36. */
  37. public class GetSharesOperation extends SyncOperation {
  38. private static final String TAG = GetSharesOperation.class.getSimpleName();
  39. @Override
  40. protected RemoteOperationResult run(OwnCloudClient client) {
  41. GetRemoteSharesOperation operation = new GetRemoteSharesOperation();
  42. RemoteOperationResult result = operation.execute(client);
  43. if (result.isSuccess()) {
  44. // Update DB with the response
  45. Log_OC.d(TAG, "Share list size = " + result.getData().size());
  46. ArrayList<OCShare> shares = new ArrayList<OCShare>();
  47. for(Object obj: result.getData()) {
  48. shares.add((OCShare) obj);
  49. }
  50. saveSharesDB(shares);
  51. }
  52. return result;
  53. }
  54. private void saveSharesDB(ArrayList<OCShare> shares) {
  55. if (shares.size() > 0) {
  56. // Save share file
  57. getStorageManager().saveShares(shares);
  58. ArrayList<OCFile> sharedFiles = new ArrayList<OCFile>();
  59. for (OCShare share : shares) {
  60. // Get the path
  61. String path = share.getPath();
  62. if (share.isDirectory()) {
  63. path = path + FileUtils.PATH_SEPARATOR;
  64. }
  65. // Update OCFile with data from share: ShareByLink ¿and publicLink?
  66. OCFile file = getStorageManager().getFileByPath(path);
  67. if (file != null) {
  68. if (share.getShareType().equals(ShareType.PUBLIC_LINK)) {
  69. file.setShareByLink(true);
  70. sharedFiles.add(file);
  71. }
  72. }
  73. }
  74. if (sharedFiles.size() > 0) {
  75. getStorageManager().updateSharedFiles(sharedFiles);
  76. }
  77. }
  78. }
  79. }