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 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
.FileDataStorageManager
;
22 import com
.owncloud
.android
.datamodel
.OCFile
;
23 import com
.owncloud
.android
.files
.services
.FileDownloader
;
24 import com
.owncloud
.android
.files
.services
.FileUploader
;
25 import com
.owncloud
.android
.lib
.network
.OwnCloudClient
;
26 import com
.owncloud
.android
.lib
.operations
.common
.RemoteFile
;
27 import com
.owncloud
.android
.lib
.operations
.common
.RemoteOperation
;
28 import com
.owncloud
.android
.lib
.operations
.common
.RemoteOperationResult
;
29 import com
.owncloud
.android
.lib
.operations
.common
.RemoteOperationResult
.ResultCode
;
30 import com
.owncloud
.android
.lib
.operations
.remote
.ReadRemoteFileOperation
;
31 import com
.owncloud
.android
.utils
.FileStorageUtils
;
32 import com
.owncloud
.android
.utils
.Log_OC
;
34 import android
.accounts
.Account
;
35 import android
.content
.Context
;
36 import android
.content
.Intent
;
39 * Remote operation performing the read of remote file in the ownCloud server.
41 * @author David A. Velasco
45 public class SynchronizeFileOperation
extends RemoteOperation
{
47 private String TAG
= SynchronizeFileOperation
.class.getSimpleName();
49 private OCFile mLocalFile
;
50 private OCFile mServerFile
;
51 private FileDataStorageManager mStorageManager
;
52 private Account mAccount
;
53 private boolean mSyncFileContents
;
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 FileDataStorageManager storageManager
,
63 boolean syncFileContents
,
66 mLocalFile
= localFile
;
67 mServerFile
= serverFile
;
68 mStorageManager
= storageManager
;
70 mSyncFileContents
= syncFileContents
;
76 protected RemoteOperationResult
run(OwnCloudClient client
) {
78 RemoteOperationResult result
= null
;
79 mTransferWasRequested
= false
;
80 if (!mLocalFile
.isDown()) {
82 requestForDownload(mLocalFile
);
83 result
= new RemoteOperationResult(ResultCode
.OK
);
86 /// local copy in the device -> need to think a bit more before do anything
88 if (mServerFile
== null
) {
89 String remotePath
= mLocalFile
.getRemotePath();
90 ReadRemoteFileOperation operation
= new ReadRemoteFileOperation(remotePath
);
91 result
= operation
.execute(client
);
92 if (result
.isSuccess()){
93 mServerFile
= FileStorageUtils
.fillOCFile((RemoteFile
)result
.getData().get(0));
94 mServerFile
.setLastSyncDateForProperties(System
.currentTimeMillis());
98 if (mServerFile
!= null
) {
100 /// check changes in server and local file
101 boolean serverChanged
= false
;
102 /* time for eTag is coming, but not yet
103 if (mServerFile.getEtag() != null) {
104 serverChanged = (!mServerFile.getEtag().equals(mLocalFile.getEtag())); // TODO could this be dangerous when the user upgrades the server from non-tagged to tagged?
106 // server without etags
107 serverChanged
= (mServerFile
.getModificationTimestamp() != mLocalFile
.getModificationTimestampAtLastSyncForData());
109 boolean localChanged
= (mLocalFile
.getLocalModificationTimestamp() > mLocalFile
.getLastSyncDateForData());
110 // TODO this will be always true after the app is upgraded to database version 2; will result in unnecessary uploads
112 /// decide action to perform depending upon changes
113 //if (!mLocalFile.getEtag().isEmpty() && localChanged && serverChanged) {
114 if (localChanged
&& serverChanged
) {
115 result
= new RemoteOperationResult(ResultCode
.SYNC_CONFLICT
);
117 } else if (localChanged
) {
118 if (mSyncFileContents
) {
119 requestForUpload(mLocalFile
);
120 // the local update of file properties will be done by the FileUploader service when the upload finishes
122 // NOTHING TO DO HERE: updating the properties of the file in the server without uploading the contents would be stupid;
123 // So, an instance of SynchronizeFileOperation created with syncFileContents == false is completely useless when we suspect
124 // that an upload is necessary (for instance, in FileObserverService).
126 result
= new RemoteOperationResult(ResultCode
.OK
);
128 } else if (serverChanged
) {
129 if (mSyncFileContents
) {
130 requestForDownload(mLocalFile
); // local, not server; we won't to keep the value of keepInSync!
131 // the update of local data will be done later by the FileUploader service when the upload finishes
133 // TODO CHECK: is this really useful in some point in the code?
134 mServerFile
.setKeepInSync(mLocalFile
.keepInSync());
135 mServerFile
.setLastSyncDateForData(mLocalFile
.getLastSyncDateForData());
136 mServerFile
.setStoragePath(mLocalFile
.getStoragePath());
137 mServerFile
.setParentId(mLocalFile
.getParentId());
138 mStorageManager
.saveFile(mServerFile
);
141 result
= new RemoteOperationResult(ResultCode
.OK
);
144 // nothing changed, nothing to do
145 result
= new RemoteOperationResult(ResultCode
.OK
);
152 Log_OC
.i(TAG
, "Synchronizing " + mAccount
.name
+ ", file " + mLocalFile
.getRemotePath() + ": " + result
.getLogMessage());
159 * Requests for an upload to the FileUploader service
161 * @param file OCFile object representing the file to upload
163 private void requestForUpload(OCFile file
) {
164 Intent i
= new Intent(mContext
, FileUploader
.class);
165 i
.putExtra(FileUploader
.KEY_ACCOUNT
, mAccount
);
166 i
.putExtra(FileUploader
.KEY_FILE
, file
);
167 /*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!
168 i.putExtra(FileUploader.KEY_LOCAL_FILE, localFile.getStoragePath());*/
169 i
.putExtra(FileUploader
.KEY_UPLOAD_TYPE
, FileUploader
.UPLOAD_SINGLE_FILE
);
170 i
.putExtra(FileUploader
.KEY_FORCE_OVERWRITE
, true
);
171 mContext
.startService(i
);
172 mTransferWasRequested
= true
;
177 * Requests for a download to the FileDownloader service
179 * @param file OCFile object representing the file to download
181 private void requestForDownload(OCFile file
) {
182 Intent i
= new Intent(mContext
, FileDownloader
.class);
183 i
.putExtra(FileDownloader
.EXTRA_ACCOUNT
, mAccount
);
184 i
.putExtra(FileDownloader
.EXTRA_FILE
, file
);
185 mContext
.startService(i
);
186 mTransferWasRequested
= true
;
190 public boolean transferWasRequested() {
191 return mTransferWasRequested
;
195 public OCFile
getLocalFile() {