1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
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.
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.
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/>.
19 package com
.owncloud
.android
.operations
;
21 import org
.apache
.http
.HttpStatus
;
22 import org
.apache
.jackrabbit
.webdav
.MultiStatus
;
23 import org
.apache
.jackrabbit
.webdav
.client
.methods
.PropFindMethod
;
25 import android
.accounts
.Account
;
26 import android
.util
.Log
;
28 import com
.owncloud
.android
.datamodel
.DataStorageManager
;
29 import com
.owncloud
.android
.datamodel
.OCFile
;
31 import eu
.alefzero
.webdav
.WebdavClient
;
32 import eu
.alefzero
.webdav
.WebdavEntry
;
33 import eu
.alefzero
.webdav
.WebdavUtils
;
35 public class SynchronizeFileOperation
extends RemoteOperation
{
37 private String TAG
= SynchronizeFileOperation
.class.getSimpleName();
39 private String mRemotePath
;
41 private DataStorageManager mStorageManager
;
43 private Account mAccount
;
45 public SynchronizeFileOperation(
47 DataStorageManager dataStorageManager
,
49 mRemotePath
= remotePath
;
50 mStorageManager
= dataStorageManager
;
55 protected RemoteOperationResult
run(WebdavClient client
) {
56 PropFindMethod propfind
= null
;
57 RemoteOperationResult result
= null
;
59 propfind
= new PropFindMethod(client
.getBaseUri() + WebdavUtils
.encodePath(mRemotePath
));
60 int status
= client
.executeMethod(propfind
);
61 boolean isMultiStatus
= status
== HttpStatus
.SC_MULTI_STATUS
;
62 Boolean isConflict
= Boolean
.FALSE
;
64 MultiStatus resp
= propfind
.getResponseBodyAsMultiStatus();
65 WebdavEntry we
= new WebdavEntry(resp
.getResponses()[0],
66 client
.getBaseUri().getPath());
67 OCFile file
= fillOCFile(we
);
68 OCFile oldFile
= mStorageManager
.getFileByPath(file
.getRemotePath());
69 if (oldFile
.getFileLength() != file
.getFileLength() ||
70 oldFile
.getModificationTimestamp() != file
.getModificationTimestamp()) {
71 isConflict
= Boolean
.TRUE
;
75 client
.exhaustResponse(propfind
.getResponseBodyAsStream());
78 result
= new RemoteOperationResult(isMultiStatus
, status
);
79 result
.setExtraData(isConflict
);
80 Log
.i(TAG
, "Synchronizing " + mAccount
.name
+ ", file " + mRemotePath
+ ": " + result
.getLogMessage());
81 } catch (Exception e
) {
82 result
= new RemoteOperationResult(e
);
83 Log
.e(TAG
, "Synchronizing " + mAccount
.name
+ ", file " + mRemotePath
+ ": " + result
.getLogMessage(), result
.getException());
87 propfind
.releaseConnection();
93 * Creates and populates a new {@link OCFile} object with the data read from the server.
95 * @param we WebDAV entry read from the server for a WebDAV resource (remote file or folder).
96 * @return New OCFile instance representing the remote resource described by we.
98 private OCFile
fillOCFile(WebdavEntry we
) {
99 OCFile file
= new OCFile(we
.decodedPath());
100 file
.setCreationTimestamp(we
.createTimestamp());
101 file
.setFileLength(we
.contentLength());
102 file
.setMimetype(we
.contentType());
103 file
.setModificationTimestamp(we
.modifiedTimesamp());
104 file
.setLastSyncDate(System
.currentTimeMillis());