OC-2746: Changes from comments: new method to construct base url server
[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 com.owncloud.android.datamodel.FileDataStorageManager;
21 import com.owncloud.android.datamodel.OCFile;
22 import com.owncloud.android.lib.network.OwnCloudClient;
23 import com.owncloud.android.lib.operations.common.RemoteOperation;
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.utils.Log_OC;
30
31 /**
32 * Access to remote operation to get the share files/folders
33 * Save the data in Database
34 *
35 * @author masensio
36 */
37
38 public class GetSharesOperation extends RemoteOperation {
39
40 private static final String TAG = GetSharesOperation.class.getSimpleName();
41
42 protected FileDataStorageManager mStorageManager;
43
44
45 public GetSharesOperation(FileDataStorageManager storageManager) {
46 mStorageManager = storageManager;
47 }
48
49 @Override
50 protected RemoteOperationResult run(OwnCloudClient client) {
51 GetRemoteSharesOperation operation = new GetRemoteSharesOperation(client.getBaseUri().toString());
52 RemoteOperationResult result = operation.execute(client);
53
54 if (result.isSuccess()) {
55
56 // Clean Share data in filelist table
57 mStorageManager.cleanShares();
58
59 // Update DB with the response
60 Log_OC.d(TAG, "Share list size = " + result.getData().size());
61 for(Object obj: result.getData()) {
62 saveShareDB((OCShare) obj);
63 }
64 }
65
66 return result;
67 }
68
69 private void saveShareDB(OCShare shareFile) {
70 // Save share file
71 mStorageManager.saveShare(shareFile);
72
73 // Get the path
74 String path = shareFile.getPath();
75 if (shareFile.isDirectory()) {
76 path = path + FileUtils.PATH_SEPARATOR;
77 }
78
79 // Update OCFile with data from share: ShareByLink ¿and publicLink?
80 OCFile file = mStorageManager.getFileByPath(path);
81 if (file != null) {
82 if (shareFile.getShareType().equals(ShareType.PUBLIC_LINK)) {
83 file.setShareByLink(true);
84 mStorageManager.saveFile(file);
85 }
86 }
87 }
88
89 }