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
.content
.Context
;
27 import android
.util
.Log
;
29 import com
.owncloud
.android
.datamodel
.DataStorageManager
;
30 import com
.owncloud
.android
.datamodel
.OCFile
;
32 import eu
.alefzero
.webdav
.WebdavClient
;
33 import eu
.alefzero
.webdav
.WebdavEntry
;
34 import eu
.alefzero
.webdav
.WebdavUtils
;
36 public class SynchronizeFileOperation
extends RemoteOperation
{
38 private String TAG
= SynchronizeFileOperation
.class.getSimpleName();
40 private String mRemotePath
;
42 private DataStorageManager mStorageManager
;
44 private Account mAccount
;
46 public SynchronizeFileOperation(
48 DataStorageManager dataStorageManager
,
51 mRemotePath
= remotePath
;
52 mStorageManager
= dataStorageManager
;
57 protected RemoteOperationResult
run(WebdavClient client
) {
58 PropFindMethod propfind
= null
;
59 RemoteOperationResult result
= null
;
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
;
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
;
77 client
.exhaustResponse(propfind
.getResponseBodyAsStream());
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());
89 propfind
.releaseConnection();
95 * Creates and populates a new {@link OCFile} object with the data read from the server.
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.
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());