daa172b24ee968e49efa71775fc432b98b64f80f
[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.oc_framework.network.webdav.WebdavClient;
26 import com.owncloud.android.oc_framework.operations.RemoteOperation;
27 import com.owncloud.android.oc_framework.operations.RemoteOperationResult;
28 import com.owncloud.android.oc_framework.operations.ShareRemoteFile;
29 import com.owncloud.android.oc_framework.operations.ShareType;
30 import com.owncloud.android.oc_framework.operations.remote.GetRemoteSharedFilesOperation;
31 import com.owncloud.android.oc_framework.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(WebdavClient client) {
56 GetRemoteSharedFilesOperation operation = new GetRemoteSharedFilesOperation(mUrlServer);
57 RemoteOperationResult result = operation.execute(client);
58
59 if (result.isSuccess()) {
60
61 // Update DB with the response
62 ArrayList<ShareRemoteFile> shareRemoteFiles = operation.getSharedFiles();
63 Log_OC.d(TAG, "Share list size = " + shareRemoteFiles.size());
64 for (ShareRemoteFile remoteFile: shareRemoteFiles) {
65 OCShare shareFile = new OCShare(remoteFile);
66 saveShareFileInDB(shareFile);
67 }
68 }
69
70 return result;
71 }
72
73 private void saveShareFileInDB(OCShare shareFile) {
74 // Save share file
75 mStorageManager.saveShareFile(shareFile);
76
77 // Get the path
78 String path = shareFile.getPath();
79 if (shareFile.isDirectory()) {
80 path = path + FileUtils.PATH_SEPARATOR;
81 }
82
83 // Update OCFile with data from share: ShareByLink ¿and publicLink?
84 OCFile file = mStorageManager.getFileByPath(path);
85 if (file != null) {
86 if (shareFile.getShareType().equals(ShareType.PUBLIC_LINK)) {
87 file.setShareByLink(true);
88 mStorageManager.saveFile(file);
89 }
90 }
91 }
92
93
94 }