OC-2585: Refresh the share items
[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.OnRemoteOperationListener;
27 import com.owncloud.android.oc_framework.operations.RemoteOperation;
28 import com.owncloud.android.oc_framework.operations.RemoteOperationResult;
29 import com.owncloud.android.oc_framework.operations.ShareRemoteFile;
30 import com.owncloud.android.oc_framework.operations.ShareType;
31 import com.owncloud.android.oc_framework.operations.remote.GetRemoteSharedFilesOperation;
32 import com.owncloud.android.oc_framework.utils.FileUtils;
33 import com.owncloud.android.utils.Log_OC;
34
35 /**
36 * Access to remote operation to get the share files/folders
37 * Save the data in Database
38 *
39 * @author masensio
40 */
41
42 public class GetSharedFilesOperation extends RemoteOperation {
43
44 private static final String TAG = GetSharedFilesOperation.class.getSimpleName();
45
46 private String mUrlServer;
47 protected FileDataStorageManager mStorageManager;
48
49
50 public GetSharedFilesOperation(String urlServer, FileDataStorageManager storageManager) {
51 mUrlServer = urlServer;
52 mStorageManager = storageManager;
53 }
54
55 @Override
56 protected RemoteOperationResult run(WebdavClient client) {
57 GetRemoteSharedFilesOperation operation = new GetRemoteSharedFilesOperation(mUrlServer);
58 RemoteOperationResult result = operation.execute(client);
59
60 if (result.isSuccess()) {
61
62 // Update DB with the response
63 ArrayList<ShareRemoteFile> shareRemoteFiles = operation.getSharedFiles();
64 Log_OC.d(TAG, "Share list size = " + shareRemoteFiles.size());
65 for (ShareRemoteFile remoteFile: shareRemoteFiles) {
66 OCShare shareFile = new OCShare(remoteFile);
67 saveShareFileInDB(shareFile);
68 }
69 }
70
71 return result;
72 }
73
74 private void saveShareFileInDB(OCShare shareFile) {
75 // Save share file
76 mStorageManager.saveShareFile(shareFile);
77
78 // Get the path
79 String path = shareFile.getPath();
80 if (shareFile.isDirectory()) {
81 path = path + FileUtils.PATH_SEPARATOR;
82 }
83
84 // Update OCFile with data from share: ShareByLink ¿and publicLink?
85 OCFile file = mStorageManager.getFileByPath(path);
86 if (file != null) {
87 if (shareFile.getShareType().equals(ShareType.PUBLIC_LINK)) {
88 file.setShareByLink(true);
89 mStorageManager.saveFile(file);
90 }
91 }
92 }
93
94 }