Merge branch 'share_link_show_shared_files' into share_link__new_share
[pub/Android/ownCloud.git] / src / com / owncloud / android / operations / GetSharedFilesOperation.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.datamodel.OCShare;
25 import com.owncloud.android.lib.network.OwnCloudClient;
26 import com.owncloud.android.lib.operations.common.RemoteOperation;
27 import com.owncloud.android.lib.operations.common.RemoteOperationResult;
28 import com.owncloud.android.lib.operations.common.ShareRemoteFile;
29 import com.owncloud.android.lib.operations.common.ShareType;
30 import com.owncloud.android.lib.operations.remote.GetRemoteSharedFilesOperation;
31 import com.owncloud.android.lib.utils.FileUtils;
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 */
40
41 public class GetSharedFilesOperation extends RemoteOperation {
42
43 private static final String TAG = GetSharedFilesOperation.class.getSimpleName();
44
45 private String mUrlServer;
46 protected FileDataStorageManager mStorageManager;
47
48
49 public GetSharedFilesOperation(String urlServer, FileDataStorageManager storageManager) {
50 mUrlServer = urlServer;
51 mStorageManager = storageManager;
52 }
53
54 @Override
55 protected RemoteOperationResult run(OwnCloudClient client) {
56 GetRemoteSharedFilesOperation operation = new GetRemoteSharedFilesOperation(mUrlServer);
57 RemoteOperationResult result = operation.execute(client);
58
59 if (result.isSuccess()) {
60
61 // Clean Share data in filelist table
62 mStorageManager.cleanShareFile();
63 // Update DB with the response
64 ArrayList<ShareRemoteFile> shareRemoteFiles = operation.getSharedFiles();
65 Log_OC.d(TAG, "Share list size = " + shareRemoteFiles.size());
66 for (ShareRemoteFile remoteFile: shareRemoteFiles) {
67 OCShare shareFile = new OCShare(remoteFile);
68 saveShareFileInDB(shareFile);
69 }
70 }
71
72 return result;
73 }
74
75 private void saveShareFileInDB(OCShare shareFile) {
76 // Save share file
77 mStorageManager.saveShareFile(shareFile);
78
79 // Get the path
80 String path = shareFile.getPath();
81 if (shareFile.isDirectory()) {
82 path = path + FileUtils.PATH_SEPARATOR;
83 }
84
85 // Update OCFile with data from share: ShareByLink ¿and publicLink?
86 OCFile file = mStorageManager.getFileByPath(path);
87 if (file != null) {
88 if (shareFile.getShareType().equals(ShareType.PUBLIC_LINK)) {
89 file.setShareByLink(true);
90 mStorageManager.saveFile(file);
91 }
92 }
93 }
94
95 }