1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 * Copyright (C) 2012-2014 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 version 2,
7 * as published by the Free Software Foundation.
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 com
.owncloud
.android
.datamodel
.OCFile
;
22 import com
.owncloud
.android
.files
.services
.FileDownloader
;
23 import com
.owncloud
.android
.files
.services
.FileUploader
;
24 import com
.owncloud
.android
.lib
.common
.OwnCloudClient
;
25 import com
.owncloud
.android
.lib
.resources
.files
.RemoteFile
;
26 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
;
27 import com
.owncloud
.android
.lib
.common
.operations
.RemoteOperationResult
.ResultCode
;
28 import com
.owncloud
.android
.lib
.resources
.files
.ReadRemoteFileOperation
;
29 import com
.owncloud
.android
.operations
.common
.SyncOperation
;
30 import com
.owncloud
.android
.utils
.FileStorageUtils
;
31 import com
.owncloud
.android
.utils
.Log_OC
;
33 import android
.accounts
.Account
;
34 import android
.content
.Context
;
35 import android
.content
.Intent
;
38 * Remote operation performing the read of remote file in the ownCloud server.
40 * @author David A. Velasco
44 public class SynchronizeFileOperation
extends SyncOperation
{
46 private String TAG
= SynchronizeFileOperation
.class.getSimpleName();
48 private OCFile mLocalFile
;
49 private String mRemotePath
;
50 private OCFile mServerFile
;
51 private Account mAccount
;
52 private boolean mSyncFileContents
;
53 private Context mContext
;
55 private boolean mTransferWasRequested
= false
;
57 public SynchronizeFileOperation(
59 OCFile serverFile
, // make this null to let the operation checks the server; added to reuse info from SynchronizeFolderOperation
61 boolean syncFileContents
,
64 mLocalFile
= localFile
;
65 mServerFile
= serverFile
;
67 mSyncFileContents
= syncFileContents
;
71 public SynchronizeFileOperation(
74 boolean syncFileContents
,
77 mRemotePath
= remotePath
;
80 mSyncFileContents
= syncFileContents
;
85 protected RemoteOperationResult
run(OwnCloudClient client
) {
87 RemoteOperationResult result
= null
;
88 mTransferWasRequested
= false
;
90 // Get local file from the DB
91 mLocalFile
= getStorageManager().getFileByPath(mRemotePath
);
93 if (!mLocalFile
.isDown()) {
95 requestForDownload(mLocalFile
);
96 result
= new RemoteOperationResult(ResultCode
.OK
);
99 /// local copy in the device -> need to think a bit more before do anything
101 if (mServerFile
== null
) {
102 String remotePath
= mLocalFile
.getRemotePath();
103 ReadRemoteFileOperation operation
= new ReadRemoteFileOperation(remotePath
);
104 result
= operation
.execute(client
);
105 if (result
.isSuccess()){
106 mServerFile
= FileStorageUtils
.fillOCFile((RemoteFile
) result
.getData().get(0));
107 mServerFile
.setLastSyncDateForProperties(System
.currentTimeMillis());
111 if (mServerFile
!= null
) {
113 /// check changes in server and local file
114 boolean serverChanged
= false
;
115 /* time for eTag is coming, but not yet
116 if (mServerFile.getEtag() != null) {
117 serverChanged = (!mServerFile.getEtag().equals(mLocalFile.getEtag())); // TODO could this be dangerous when the user upgrades the server from non-tagged to tagged?
119 // server without etags
120 serverChanged
= (mServerFile
.getModificationTimestamp() != mLocalFile
.getModificationTimestampAtLastSyncForData());
122 boolean localChanged
= (mLocalFile
.getLocalModificationTimestamp() > mLocalFile
.getLastSyncDateForData());
123 // TODO this will be always true after the app is upgraded to database version 2; will result in unnecessary uploads
125 /// decide action to perform depending upon changes
126 //if (!mLocalFile.getEtag().isEmpty() && localChanged && serverChanged) {
127 if (localChanged
&& serverChanged
) {
128 result
= new RemoteOperationResult(ResultCode
.SYNC_CONFLICT
);
130 } else if (localChanged
) {
131 if (mSyncFileContents
) {
132 requestForUpload(mLocalFile
);
133 // the local update of file properties will be done by the FileUploader service when the upload finishes
135 // NOTHING TO DO HERE: updating the properties of the file in the server without uploading the contents would be stupid;
136 // So, an instance of SynchronizeFileOperation created with syncFileContents == false is completely useless when we suspect
137 // that an upload is necessary (for instance, in FileObserverService).
139 result
= new RemoteOperationResult(ResultCode
.OK
);
141 } else if (serverChanged
) {
142 if (mSyncFileContents
) {
143 requestForDownload(mLocalFile
); // local, not server; we won't to keep the value of keepInSync!
144 // the update of local data will be done later by the FileUploader service when the upload finishes
146 // TODO CHECK: is this really useful in some point in the code?
147 mServerFile
.setKeepInSync(mLocalFile
.keepInSync());
148 mServerFile
.setLastSyncDateForData(mLocalFile
.getLastSyncDateForData());
149 mServerFile
.setStoragePath(mLocalFile
.getStoragePath());
150 mServerFile
.setParentId(mLocalFile
.getParentId());
151 getStorageManager().saveFile(mServerFile
);
154 result
= new RemoteOperationResult(ResultCode
.OK
);
157 // nothing changed, nothing to do
158 result
= new RemoteOperationResult(ResultCode
.OK
);
165 Log_OC
.i(TAG
, "Synchronizing " + mAccount
.name
+ ", file " + mLocalFile
.getRemotePath() + ": " + result
.getLogMessage());
172 * Requests for an upload to the FileUploader service
174 * @param file OCFile object representing the file to upload
176 private void requestForUpload(OCFile file
) {
177 Intent i
= new Intent(mContext
, FileUploader
.class);
178 i
.putExtra(FileUploader
.KEY_ACCOUNT
, mAccount
);
179 i
.putExtra(FileUploader
.KEY_FILE
, file
);
180 /*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!
181 i.putExtra(FileUploader.KEY_LOCAL_FILE, localFile.getStoragePath());*/
182 i
.putExtra(FileUploader
.KEY_UPLOAD_TYPE
, FileUploader
.UPLOAD_SINGLE_FILE
);
183 i
.putExtra(FileUploader
.KEY_FORCE_OVERWRITE
, true
);
184 mContext
.startService(i
);
185 mTransferWasRequested
= true
;
190 * Requests for a download to the FileDownloader service
192 * @param file OCFile object representing the file to download
194 private void requestForDownload(OCFile file
) {
195 Intent i
= new Intent(mContext
, FileDownloader
.class);
196 i
.putExtra(FileDownloader
.EXTRA_ACCOUNT
, mAccount
);
197 i
.putExtra(FileDownloader
.EXTRA_FILE
, file
);
198 mContext
.startService(i
);
199 mTransferWasRequested
= true
;
203 public boolean transferWasRequested() {
204 return mTransferWasRequested
;
208 public OCFile
getLocalFile() {