Merge remote-tracking branch 'remotes/upstream/master' into beta
[pub/Android/ownCloud.git] / src / com / owncloud / android / operations / GetSharesForFileOperation.java
1 /**
2 * ownCloud Android client application
3 *
4 * @author masensio
5 * Copyright (C) 2015 ownCloud Inc.
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2,
9 * as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
21
22 package com.owncloud.android.operations;
23
24 import com.owncloud.android.lib.common.OwnCloudClient;
25 import com.owncloud.android.lib.common.operations.RemoteOperationResult;
26 import com.owncloud.android.lib.common.utils.Log_OC;
27 import com.owncloud.android.lib.resources.shares.GetRemoteSharesForFileOperation;
28 import com.owncloud.android.lib.resources.shares.OCShare;
29 import com.owncloud.android.operations.common.SyncOperation;
30
31 import java.util.ArrayList;
32
33 /**
34 * Provide a list shares for a specific file.
35 */
36 public class GetSharesForFileOperation extends SyncOperation {
37
38 private static final String TAG = GetSharesForFileOperation.class.getSimpleName();
39
40 private String mPath;
41 private boolean mReshares;
42 private boolean mSubfiles;
43
44 /**
45 * Constructor
46 *
47 * @param path Path to file or folder
48 * @param reshares If set to false (default), only shares from the current user are returned
49 * If set to true, all shares from the given file are returned
50 * @param subfiles If set to false (default), lists only the folder being shared
51 * If set to true, all shared files within the folder are returned.
52 */
53 public GetSharesForFileOperation(String path, boolean reshares, boolean subfiles) {
54 mPath = path;
55 mReshares = reshares;
56 mSubfiles = subfiles;
57 }
58
59 @Override
60 protected RemoteOperationResult run(OwnCloudClient client) {
61 GetRemoteSharesForFileOperation operation = new GetRemoteSharesForFileOperation(mPath,
62 mReshares, mSubfiles);
63 RemoteOperationResult result = operation.execute(client);
64
65 if (result.isSuccess()) {
66
67 // Update DB with the response
68 Log_OC.d(TAG, "File = " + mPath + " Share list size " + result.getData().size());
69 ArrayList<OCShare> shares = new ArrayList<OCShare>();
70 for(Object obj: result.getData()) {
71 shares.add((OCShare) obj);
72 }
73
74 getStorageManager().saveSharesDB(shares);
75
76 } else if (result.getCode() == RemoteOperationResult.ResultCode.SHARE_NOT_FOUND) {
77 // no share on the file - remove local shares
78 getStorageManager().removeSharesForFile(mPath);
79
80 }
81
82 return result;
83 }
84
85 }