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