77acd3212b199b2b645be9c230fde8bd25d7db89
[pub/Android/ownCloud.git] / src / com / owncloud / android / operations / GetSharesOperation.java
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
18 package com.owncloud.android.operations;
19
20 import java.util.ArrayList;
21
22 import com.owncloud.android.datamodel.FileDataStorageManager;
23 import com.owncloud.android.datamodel.OCFile;
24 import com.owncloud.android.lib.network.OwnCloudClient;
25 import com.owncloud.android.lib.operations.common.RemoteOperation;
26 import com.owncloud.android.lib.operations.common.RemoteOperationResult;
27 import com.owncloud.android.lib.operations.common.OCShare;
28 import com.owncloud.android.lib.operations.common.ShareType;
29 import com.owncloud.android.lib.operations.remote.GetRemoteSharesOperation;
30 import com.owncloud.android.lib.utils.FileUtils;
31 import com.owncloud.android.utils.Log_OC;
32
33 /**
34 * Access to remote operation to get the share files/folders
35 * Save the data in Database
36 *
37 * @author masensio
38 */
39
40 public class GetSharesOperation extends RemoteOperation {
41
42 private static final String TAG = GetSharesOperation.class.getSimpleName();
43
44 protected FileDataStorageManager mStorageManager;
45
46
47 public GetSharesOperation(FileDataStorageManager storageManager) {
48 mStorageManager = storageManager;
49 }
50
51 @Override
52 protected RemoteOperationResult run(OwnCloudClient client) {
53 GetRemoteSharesOperation operation = new GetRemoteSharesOperation(client.getBaseUri().toString());
54 RemoteOperationResult result = operation.execute(client);
55
56 if (result.isSuccess()) {
57
58 // Update DB with the response
59 Log_OC.d(TAG, "Share list size = " + result.getData().size());
60 ArrayList<OCShare> shares = new ArrayList<OCShare>();
61 for(Object obj: result.getData()) {
62 shares.add((OCShare) obj);
63 }
64
65 saveSharesDB(shares);
66 }
67
68 return result;
69 }
70
71 private void saveSharesDB(ArrayList<OCShare> shares) {
72
73 if (shares.size() > 0) {
74 // Save share file
75 mStorageManager.saveShares(shares);
76
77 ArrayList<OCFile> sharedFiles = new ArrayList<OCFile>();
78
79 for (OCShare share : shares) {
80 // Get the path
81 String path = share.getPath();
82 if (share.isDirectory()) {
83 path = path + FileUtils.PATH_SEPARATOR;
84 }
85
86 // Update OCFile with data from share: ShareByLink ¿and publicLink?
87 OCFile file = mStorageManager.getFileByPath(path);
88 if (file != null) {
89 if (share.getShareType().equals(ShareType.PUBLIC_LINK)) {
90 file.setShareByLink(true);
91 sharedFiles.add(file);
92 }
93 }
94 }
95
96 if (sharedFiles.size() > 0) {
97 mStorageManager.updateSharedFiles(sharedFiles);
98 }
99 }
100 }
101
102 }