Open, download and cancel operations linked to contextual menu for files
[pub/Android/ownCloud.git] / src / com / owncloud / android / operations / SynchronizeFileOperation.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
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 as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 *
17 */
18
19 package com.owncloud.android.operations;
20
21 import org.apache.http.HttpStatus;
22 import org.apache.jackrabbit.webdav.MultiStatus;
23 import org.apache.jackrabbit.webdav.client.methods.PropFindMethod;
24
25 import android.accounts.Account;
26 import android.content.Context;
27 import android.util.Log;
28
29 import com.owncloud.android.datamodel.DataStorageManager;
30 import com.owncloud.android.datamodel.OCFile;
31
32 import eu.alefzero.webdav.WebdavClient;
33 import eu.alefzero.webdav.WebdavEntry;
34 import eu.alefzero.webdav.WebdavUtils;
35
36 public class SynchronizeFileOperation extends RemoteOperation {
37
38 private String TAG = SynchronizeFileOperation.class.getSimpleName();
39
40 private String mRemotePath;
41
42 private DataStorageManager mStorageManager;
43
44 private Account mAccount;
45
46 public SynchronizeFileOperation(
47 String remotePath,
48 DataStorageManager dataStorageManager,
49 Account account,
50 Context context ) {
51 mRemotePath = remotePath;
52 mStorageManager = dataStorageManager;
53 mAccount = account;
54 }
55
56 @Override
57 protected RemoteOperationResult run(WebdavClient client) {
58 PropFindMethod propfind = null;
59 RemoteOperationResult result = null;
60 try {
61 propfind = new PropFindMethod(client.getBaseUri() + WebdavUtils.encodePath(mRemotePath));
62 int status = client.executeMethod(propfind);
63 boolean isMultiStatus = status == HttpStatus.SC_MULTI_STATUS;
64 Boolean isConflict = Boolean.FALSE;
65 if (isMultiStatus) {
66 MultiStatus resp = propfind.getResponseBodyAsMultiStatus();
67 WebdavEntry we = new WebdavEntry(resp.getResponses()[0],
68 client.getBaseUri().getPath());
69 OCFile file = fillOCFile(we);
70 OCFile oldFile = mStorageManager.getFileByPath(file.getRemotePath());
71 if (oldFile.getFileLength() != file.getFileLength() ||
72 oldFile.getModificationTimestamp() != file.getModificationTimestamp()) {
73 isConflict = Boolean.TRUE;
74 }
75
76 } else {
77 client.exhaustResponse(propfind.getResponseBodyAsStream());
78 }
79
80 result = new RemoteOperationResult(isMultiStatus, status);
81 result.setExtraData(isConflict);
82 Log.i(TAG, "Synchronizing " + mAccount.name + ", file " + mRemotePath + ": " + result.getLogMessage());
83 } catch (Exception e) {
84 result = new RemoteOperationResult(e);
85 Log.e(TAG, "Synchronizing " + mAccount.name + ", file " + mRemotePath + ": " + result.getLogMessage(), result.getException());
86
87 } finally {
88 if (propfind != null)
89 propfind.releaseConnection();
90 }
91 return result;
92 }
93
94 /**
95 * Creates and populates a new {@link OCFile} object with the data read from the server.
96 *
97 * @param we WebDAV entry read from the server for a WebDAV resource (remote file or folder).
98 * @return New OCFile instance representing the remote resource described by we.
99 */
100 private OCFile fillOCFile(WebdavEntry we) {
101 OCFile file = new OCFile(we.decodedPath());
102 file.setCreationTimestamp(we.createTimestamp());
103 file.setFileLength(we.contentLength());
104 file.setMimetype(we.contentType());
105 file.setModificationTimestamp(we.modifiedTimesamp());
106 file.setLastSyncDate(System.currentTimeMillis());
107 return file;
108 }
109
110 }