1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 package com
.owncloud
.android
.operations
;
22 import org
.apache
.http
.HttpStatus
;
23 import org
.apache
.jackrabbit
.webdav
.MultiStatus
;
24 import org
.apache
.jackrabbit
.webdav
.client
.methods
.PropFindMethod
;
26 import android
.accounts
.Account
;
27 import android
.content
.Context
;
28 import android
.content
.Intent
;
29 import android
.util
.Log
;
31 import com
.owncloud
.android
.Log_OC
;
32 import com
.owncloud
.android
.datamodel
.DataStorageManager
;
33 import com
.owncloud
.android
.datamodel
.OCFile
;
34 import com
.owncloud
.android
.files
.services
.FileDownloader
;
35 import com
.owncloud
.android
.files
.services
.FileUploader
;
36 import com
.owncloud
.android
.operations
.RemoteOperationResult
.ResultCode
;
38 import eu
.alefzero
.webdav
.WebdavClient
;
39 import eu
.alefzero
.webdav
.WebdavEntry
;
40 import eu
.alefzero
.webdav
.WebdavUtils
;
42 public class SynchronizeFileOperation
extends RemoteOperation
{
44 private String TAG
= SynchronizeFileOperation
.class.getSimpleName();
45 private static final int SYNC_READ_TIMEOUT
= 10000;
46 private static final int SYNC_CONNECTION_TIMEOUT
= 5000;
48 private OCFile mLocalFile
;
49 private OCFile mServerFile
;
50 private DataStorageManager mStorageManager
;
51 private Account mAccount
;
52 private boolean mSyncFileContents
;
53 private boolean mLocalChangeAlreadyKnown
;
54 private Context mContext
;
56 private boolean mTransferWasRequested
= false
;
58 public SynchronizeFileOperation(
60 OCFile serverFile
, // make this null to let the operation checks the server; added to reuse info from SynchronizeFolderOperation
61 DataStorageManager storageManager
,
63 boolean syncFileContents
,
64 boolean localChangeAlreadyKnown
,
67 mLocalFile
= localFile
;
68 mServerFile
= serverFile
;
69 mStorageManager
= storageManager
;
71 mSyncFileContents
= syncFileContents
;
72 mLocalChangeAlreadyKnown
= localChangeAlreadyKnown
;
78 protected RemoteOperationResult
run(WebdavClient client
) {
80 PropFindMethod propfind
= null
;
81 RemoteOperationResult result
= null
;
82 mTransferWasRequested
= false
;
84 if (!mLocalFile
.isDown()) {
86 requestForDownload(mLocalFile
);
87 result
= new RemoteOperationResult(ResultCode
.OK
);
90 /// local copy in the device -> need to think a bit more before do anything
92 if (mServerFile
== null
) {
93 /// take the duty of check the server for the current state of the file there
94 propfind
= new PropFindMethod(client
.getBaseUri() + WebdavUtils
.encodePath(mLocalFile
.getRemotePath()));
95 int status
= client
.executeMethod(propfind
, SYNC_READ_TIMEOUT
, SYNC_CONNECTION_TIMEOUT
);
96 boolean isMultiStatus
= status
== HttpStatus
.SC_MULTI_STATUS
;
98 MultiStatus resp
= propfind
.getResponseBodyAsMultiStatus();
99 WebdavEntry we
= new WebdavEntry(resp
.getResponses()[0],
100 client
.getBaseUri().getPath());
101 mServerFile
= fillOCFile(we
);
102 mServerFile
.setLastSyncDateForProperties(System
.currentTimeMillis());
105 client
.exhaustResponse(propfind
.getResponseBodyAsStream());
106 result
= new RemoteOperationResult(false
, status
);
110 if (result
== null
) { // true if the server was not checked, or nothing was wrong with the remote request
112 /// check changes in server and local file
113 boolean serverChanged
= false
;
114 if (mServerFile
.getEtag() != null
) {
115 serverChanged
= (!mServerFile
.getEtag().equals(mLocalFile
.getEtag())); // TODO could this be dangerous when the user upgrades the server from non-tagged to tagged?
117 // server without etags
118 serverChanged
= (mServerFile
.getModificationTimestamp() > mLocalFile
.getModificationTimestampAtLastSyncForData());
120 boolean localChanged
= (mLocalChangeAlreadyKnown
|| mLocalFile
.getLocalModificationTimestamp() > mLocalFile
.getLastSyncDateForData());
121 // TODO this will be always true after the app is upgraded to database version 2; will result in unnecessary uploads
123 /// decide action to perform depending upon changes
124 if (localChanged
&& serverChanged
) {
125 result
= new RemoteOperationResult(ResultCode
.SYNC_CONFLICT
);
127 } else if (localChanged
) {
128 if (mSyncFileContents
) {
129 requestForUpload(mLocalFile
);
130 // the local update of file properties will be done by the FileUploader service when the upload finishes
132 // NOTHING TO DO HERE: updating the properties of the file in the server without uploading the contents would be stupid;
133 // So, an instance of SynchronizeFileOperation created with syncFileContents == false is completely useless when we suspect
134 // that an upload is necessary (for instance, in FileObserverService).
136 result
= new RemoteOperationResult(ResultCode
.OK
);
138 } else if (serverChanged
) {
139 if (mSyncFileContents
) {
140 requestForDownload(mLocalFile
); // local, not server; we won't to keep the value of keepInSync!
141 // the update of local data will be done later by the FileUploader service when the upload finishes
143 // TODO CHECK: is this really useful in some point in the code?
144 mServerFile
.setKeepInSync(mLocalFile
.keepInSync());
145 mServerFile
.setLastSyncDateForData(mLocalFile
.getLastSyncDateForData());
146 mServerFile
.setStoragePath(mLocalFile
.getStoragePath());
147 mServerFile
.setParentId(mLocalFile
.getParentId());
148 mStorageManager
.saveFile(mServerFile
);
151 result
= new RemoteOperationResult(ResultCode
.OK
);
154 // nothing changed, nothing to do
155 result
= new RemoteOperationResult(ResultCode
.OK
);
162 Log_OC
.i(TAG
, "Synchronizing " + mAccount
.name
+ ", file " + mLocalFile
.getRemotePath() + ": " + result
.getLogMessage());
164 } catch (Exception e
) {
165 result
= new RemoteOperationResult(e
);
166 Log_OC
.e(TAG
, "Synchronizing " + mAccount
.name
+ ", file " + (mLocalFile
!= null ? mLocalFile
.getRemotePath() : "NULL") + ": " + result
.getLogMessage(), result
.getException());
169 if (propfind
!= null
)
170 propfind
.releaseConnection();
177 * Requests for an upload to the FileUploader service
179 * @param file OCFile object representing the file to upload
181 private void requestForUpload(OCFile file
) {
182 Intent i
= new Intent(mContext
, FileUploader
.class);
183 i
.putExtra(FileUploader
.KEY_ACCOUNT
, mAccount
);
184 i
.putExtra(FileUploader
.KEY_FILE
, file
);
185 /*i.putExtra(FileUploader.KEY_REMOTE_FILE, mRemotePath); // doing this we would lose the value of keepInSync in the road, and maybe it's not updated in the database when the FileUploader service gets it!
186 i.putExtra(FileUploader.KEY_LOCAL_FILE, localFile.getStoragePath());*/
187 i
.putExtra(FileUploader
.KEY_UPLOAD_TYPE
, FileUploader
.UPLOAD_SINGLE_FILE
);
188 i
.putExtra(FileUploader
.KEY_FORCE_OVERWRITE
, true
);
189 mContext
.startService(i
);
190 mTransferWasRequested
= true
;
195 * Requests for a download to the FileDownloader service
197 * @param file OCFile object representing the file to download
199 private void requestForDownload(OCFile file
) {
200 Intent i
= new Intent(mContext
, FileDownloader
.class);
201 i
.putExtra(FileDownloader
.EXTRA_ACCOUNT
, mAccount
);
202 i
.putExtra(FileDownloader
.EXTRA_FILE
, file
);
203 mContext
.startService(i
);
204 mTransferWasRequested
= true
;
209 * Creates and populates a new {@link OCFile} object with the data read from the server.
211 * @param we WebDAV entry read from the server for a WebDAV resource (remote file or folder).
212 * @return New OCFile instance representing the remote resource described by we.
214 private OCFile
fillOCFile(WebdavEntry we
) {
215 OCFile file
= new OCFile(we
.decodedPath());
216 file
.setCreationTimestamp(we
.createTimestamp());
217 file
.setFileLength(we
.contentLength());
218 file
.setMimetype(we
.contentType());
219 file
.setModificationTimestamp(we
.modifiedTimestamp());
224 public boolean transferWasRequested() {
225 return mTransferWasRequested
;
229 public OCFile
getLocalFile() {