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