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
.datamodel
;
22 import java
.util
.ArrayList
;
23 import java
.util
.Collection
;
24 import java
.util
.Collections
;
25 import java
.util
.Iterator
;
26 import java
.util
.Vector
;
28 import com
.owncloud
.android
.MainApp
;
29 import com
.owncloud
.android
.db
.ProviderMeta
.ProviderTableMeta
;
30 import com
.owncloud
.android
.lib
.operations
.common
.OCShare
;
31 import com
.owncloud
.android
.lib
.operations
.common
.ShareType
;
32 import com
.owncloud
.android
.utils
.FileStorageUtils
;
33 import com
.owncloud
.android
.utils
.Log_OC
;
36 import android
.accounts
.Account
;
37 import android
.content
.ContentProviderClient
;
38 import android
.content
.ContentProviderOperation
;
39 import android
.content
.ContentProviderResult
;
40 import android
.content
.ContentResolver
;
41 import android
.content
.ContentUris
;
42 import android
.content
.ContentValues
;
43 import android
.content
.OperationApplicationException
;
44 import android
.database
.Cursor
;
45 import android
.net
.Uri
;
46 import android
.os
.RemoteException
;
48 public class FileDataStorageManager
{
50 public static final int ROOT_PARENT_ID
= 0;
52 private ContentResolver mContentResolver
;
53 private ContentProviderClient mContentProviderClient
;
54 private Account mAccount
;
56 private static String TAG
= FileDataStorageManager
.class.getSimpleName();
59 public FileDataStorageManager(Account account
, ContentResolver cr
) {
60 mContentProviderClient
= null
;
61 mContentResolver
= cr
;
65 public FileDataStorageManager(Account account
, ContentProviderClient cp
) {
66 mContentProviderClient
= cp
;
67 mContentResolver
= null
;
72 public void setAccount(Account account
) {
76 public Account
getAccount() {
80 public void setContentResolver(ContentResolver cr
) {
81 mContentResolver
= cr
;
84 public ContentResolver
getContentResolver() {
85 return mContentResolver
;
88 public void setContentProviderClient(ContentProviderClient cp
) {
89 mContentProviderClient
= cp
;
92 public ContentProviderClient
getContentProviderClient() {
93 return mContentProviderClient
;
97 public OCFile
getFileByPath(String path
) {
98 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_PATH
, path
);
100 if (c
.moveToFirst()) {
101 file
= createFileInstance(c
);
104 if (file
== null
&& OCFile
.ROOT_PATH
.equals(path
)) {
105 return createRootDir(); // root should always exist
111 public OCFile
getFileById(long id
) {
112 Cursor c
= getCursorForValue(ProviderTableMeta
._ID
, String
.valueOf(id
));
114 if (c
.moveToFirst()) {
115 file
= createFileInstance(c
);
121 public OCFile
getFileByLocalPath(String path
) {
122 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_STORAGE_PATH
, path
);
124 if (c
.moveToFirst()) {
125 file
= createFileInstance(c
);
131 public boolean fileExists(long id
) {
132 return fileExists(ProviderTableMeta
._ID
, String
.valueOf(id
));
135 public boolean fileExists(String path
) {
136 return fileExists(ProviderTableMeta
.FILE_PATH
, path
);
140 public Vector
<OCFile
> getFolderContent(OCFile f
) {
141 if (f
!= null
&& f
.isFolder() && f
.getFileId() != -1) {
142 return getFolderContent(f
.getFileId());
145 return new Vector
<OCFile
>();
150 public Vector
<OCFile
> getFolderImages(OCFile folder
) {
151 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
152 if (folder
!= null
) {
153 // TODO better implementation, filtering in the access to database (if possible) instead of here
154 Vector
<OCFile
> tmp
= getFolderContent(folder
);
155 OCFile current
= null
;
156 for (int i
=0; i
<tmp
.size(); i
++) {
157 current
= tmp
.get(i
);
158 if (current
.isImage()) {
167 public boolean saveFile(OCFile file
) {
168 boolean overriden
= false
;
169 ContentValues cv
= new ContentValues();
170 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, file
.getModificationTimestamp());
171 cv
.put(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
, file
.getModificationTimestampAtLastSyncForData());
172 cv
.put(ProviderTableMeta
.FILE_CREATION
, file
.getCreationTimestamp());
173 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, file
.getFileLength());
174 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, file
.getMimetype());
175 cv
.put(ProviderTableMeta
.FILE_NAME
, file
.getFileName());
176 //if (file.getParentId() != DataStorageManager.ROOT_PARENT_ID)
177 cv
.put(ProviderTableMeta
.FILE_PARENT
, file
.getParentId());
178 cv
.put(ProviderTableMeta
.FILE_PATH
, file
.getRemotePath());
179 if (!file
.isFolder())
180 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
181 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
182 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, file
.getLastSyncDateForProperties());
183 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
, file
.getLastSyncDateForData());
184 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, file
.keepInSync() ?
1 : 0);
185 cv
.put(ProviderTableMeta
.FILE_ETAG
, file
.getEtag());
186 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, file
.isShareByLink() ?
1 : 0);
187 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, file
.getPublicLink());
189 boolean sameRemotePath
= fileExists(file
.getRemotePath());
190 if (sameRemotePath
||
191 fileExists(file
.getFileId()) ) { // for renamed files; no more delete and create
193 OCFile oldFile
= null
;
194 if (sameRemotePath
) {
195 oldFile
= getFileByPath(file
.getRemotePath());
196 file
.setFileId(oldFile
.getFileId());
198 oldFile
= getFileById(file
.getFileId());
202 if (getContentResolver() != null
) {
203 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
, cv
,
204 ProviderTableMeta
._ID
+ "=?",
205 new String
[] { String
.valueOf(file
.getFileId()) });
208 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI
,
209 cv
, ProviderTableMeta
._ID
+ "=?",
210 new String
[] { String
.valueOf(file
.getFileId()) });
211 } catch (RemoteException e
) {
213 "Fail to insert insert file to database "
218 Uri result_uri
= null
;
219 if (getContentResolver() != null
) {
220 result_uri
= getContentResolver().insert(
221 ProviderTableMeta
.CONTENT_URI_FILE
, cv
);
224 result_uri
= getContentProviderClient().insert(
225 ProviderTableMeta
.CONTENT_URI_FILE
, cv
);
226 } catch (RemoteException e
) {
228 "Fail to insert insert file to database "
232 if (result_uri
!= null
) {
233 long new_id
= Long
.parseLong(result_uri
.getPathSegments()
235 file
.setFileId(new_id
);
239 if (file
.isFolder()) {
240 updateFolderSize(file
.getFileId());
242 updateFolderSize(file
.getParentId());
250 * Inserts or updates the list of files contained in a given folder.
252 * CALLER IS THE RESPONSIBLE FOR GRANTING RIGHT UPDATE OF INFORMATION, NOT THIS METHOD.
253 * HERE ONLY DATA CONSISTENCY SHOULD BE GRANTED
257 * @param removeNotUpdated
259 public void saveFolder(OCFile folder
, Collection
<OCFile
> updatedFiles
, Collection
<OCFile
> filesToRemove
) {
261 Log_OC
.d(TAG
, "Saving folder " + folder
.getRemotePath() + " with " + updatedFiles
.size() + " children and " + filesToRemove
.size() + " files to remove");
263 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>(updatedFiles
.size());
265 // prepare operations to insert or update files to save in the given folder
266 for (OCFile file
: updatedFiles
) {
267 ContentValues cv
= new ContentValues();
268 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, file
.getModificationTimestamp());
269 cv
.put(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
, file
.getModificationTimestampAtLastSyncForData());
270 cv
.put(ProviderTableMeta
.FILE_CREATION
, file
.getCreationTimestamp());
271 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, file
.getFileLength());
272 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, file
.getMimetype());
273 cv
.put(ProviderTableMeta
.FILE_NAME
, file
.getFileName());
274 //cv.put(ProviderTableMeta.FILE_PARENT, file.getParentId());
275 cv
.put(ProviderTableMeta
.FILE_PARENT
, folder
.getFileId());
276 cv
.put(ProviderTableMeta
.FILE_PATH
, file
.getRemotePath());
277 if (!file
.isFolder()) {
278 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, file
.getStoragePath());
280 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
281 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, file
.getLastSyncDateForProperties());
282 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
, file
.getLastSyncDateForData());
283 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, file
.keepInSync() ?
1 : 0);
284 cv
.put(ProviderTableMeta
.FILE_ETAG
, file
.getEtag());
285 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, file
.isShareByLink() ?
1 : 0);
286 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, file
.getPublicLink());
288 boolean existsByPath
= fileExists(file
.getRemotePath());
289 if (existsByPath
|| fileExists(file
.getFileId())) {
290 // updating an existing file
291 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
293 withSelection( ProviderTableMeta
._ID
+ "=?",
294 new String
[] { String
.valueOf(file
.getFileId()) })
299 operations
.add(ContentProviderOperation
.newInsert(ProviderTableMeta
.CONTENT_URI
).withValues(cv
).build());
303 // prepare operations to remove files in the given folder
304 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?" + " AND " + ProviderTableMeta
.FILE_PATH
+ "=?";
305 String
[] whereArgs
= null
;
306 for (OCFile file
: filesToRemove
) {
307 if (file
.getParentId() == folder
.getFileId()) {
308 whereArgs
= new String
[]{mAccount
.name
, file
.getRemotePath()};
309 //Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_FILE, "" + file.getFileId());
310 if (file
.isFolder()) {
311 operations
.add(ContentProviderOperation
312 .newDelete(ContentUris
.withAppendedId(ProviderTableMeta
.CONTENT_URI_DIR
, file
.getFileId())).withSelection(where
, whereArgs
)
314 // TODO remove local folder
316 operations
.add(ContentProviderOperation
317 .newDelete(ContentUris
.withAppendedId(ProviderTableMeta
.CONTENT_URI_FILE
, file
.getFileId())).withSelection(where
, whereArgs
)
320 new File(file
.getStoragePath()).delete();
321 // TODO move the deletion of local contents after success of deletions
327 // update metadata of folder
328 ContentValues cv
= new ContentValues();
329 cv
.put(ProviderTableMeta
.FILE_MODIFIED
, folder
.getModificationTimestamp());
330 cv
.put(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
, folder
.getModificationTimestampAtLastSyncForData());
331 cv
.put(ProviderTableMeta
.FILE_CREATION
, folder
.getCreationTimestamp());
332 cv
.put(ProviderTableMeta
.FILE_CONTENT_LENGTH
, 0); // FileContentProvider calculates the right size
333 cv
.put(ProviderTableMeta
.FILE_CONTENT_TYPE
, folder
.getMimetype());
334 cv
.put(ProviderTableMeta
.FILE_NAME
, folder
.getFileName());
335 cv
.put(ProviderTableMeta
.FILE_PARENT
, folder
.getParentId());
336 cv
.put(ProviderTableMeta
.FILE_PATH
, folder
.getRemotePath());
337 cv
.put(ProviderTableMeta
.FILE_ACCOUNT_OWNER
, mAccount
.name
);
338 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE
, folder
.getLastSyncDateForProperties());
339 cv
.put(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
, folder
.getLastSyncDateForData());
340 cv
.put(ProviderTableMeta
.FILE_KEEP_IN_SYNC
, folder
.keepInSync() ?
1 : 0);
341 cv
.put(ProviderTableMeta
.FILE_ETAG
, folder
.getEtag());
342 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, folder
.isShareByLink() ?
1 : 0);
343 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, folder
.getPublicLink());
345 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
347 withSelection( ProviderTableMeta
._ID
+ "=?",
348 new String
[] { String
.valueOf(folder
.getFileId()) })
351 // apply operations in batch
352 ContentProviderResult
[] results
= null
;
353 Log_OC
.d(TAG
, "Sending " + operations
.size() + " operations to FileContentProvider");
355 if (getContentResolver() != null
) {
356 results
= getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
359 results
= getContentProviderClient().applyBatch(operations
);
362 } catch (OperationApplicationException e
) {
363 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
365 } catch (RemoteException e
) {
366 Log_OC
.e(TAG
, "Exception in batch of operations " + e
.getMessage());
369 // update new id in file objects for insertions
370 if (results
!= null
) {
372 Iterator
<OCFile
> filesIt
= updatedFiles
.iterator();
374 for (int i
=0; i
<results
.length
; i
++) {
375 if (filesIt
.hasNext()) {
376 file
= filesIt
.next();
380 if (results
[i
].uri
!= null
) {
381 newId
= Long
.parseLong(results
[i
].uri
.getPathSegments().get(1));
382 //updatedFiles.get(i).setFileId(newId);
384 file
.setFileId(newId
);
390 updateFolderSize(folder
.getFileId());
399 private void updateFolderSize(long id
) {
400 if (id
> FileDataStorageManager
.ROOT_PARENT_ID
) {
401 Log_OC
.d(TAG
, "Updating size of " + id
);
402 if (getContentResolver() != null
) {
403 getContentResolver().update(ProviderTableMeta
.CONTENT_URI_DIR
,
404 new ContentValues(), // won't be used, but cannot be null; crashes in KLP
405 ProviderTableMeta
._ID
+ "=?",
406 new String
[] { String
.valueOf(id
) });
409 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI_DIR
,
410 new ContentValues(), // won't be used, but cannot be null; crashes in KLP
411 ProviderTableMeta
._ID
+ "=?",
412 new String
[] { String
.valueOf(id
) });
414 } catch (RemoteException e
) {
415 Log_OC
.e(TAG
, "Exception in update of folder size through compatibility patch " + e
.getMessage());
419 Log_OC
.e(TAG
, "not updating size for folder " + id
);
424 public void removeFile(OCFile file
, boolean removeDBData
, boolean removeLocalCopy
) {
426 if (file
.isFolder()) {
427 removeFolder(file
, removeDBData
, removeLocalCopy
);
431 //Uri file_uri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_FILE, ""+file.getFileId());
432 Uri file_uri
= ContentUris
.withAppendedId(ProviderTableMeta
.CONTENT_URI_FILE
, file
.getFileId());
433 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?" + " AND " + ProviderTableMeta
.FILE_PATH
+ "=?";
434 String
[] whereArgs
= new String
[]{mAccount
.name
, file
.getRemotePath()};
435 if (getContentProviderClient() != null
) {
437 getContentProviderClient().delete(file_uri
, where
, whereArgs
);
438 } catch (RemoteException e
) {
442 getContentResolver().delete(file_uri
, where
, whereArgs
);
444 updateFolderSize(file
.getParentId());
446 if (removeLocalCopy
&& file
.isDown() && file
.getStoragePath() != null
) {
447 boolean success
= new File(file
.getStoragePath()).delete();
448 if (!removeDBData
&& success
) {
449 // maybe unnecessary, but should be checked TODO remove if unnecessary
450 file
.setStoragePath(null
);
459 public void removeFolder(OCFile folder
, boolean removeDBData
, boolean removeLocalContent
) {
460 if (folder
!= null
&& folder
.isFolder()) {
461 if (removeDBData
&& folder
.getFileId() != -1) {
462 removeFolderInDb(folder
);
464 if (removeLocalContent
) {
465 File localFolder
= new File(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, folder
));
466 removeLocalFolder(localFolder
);
471 private void removeFolderInDb(OCFile folder
) {
472 Uri folder_uri
= Uri
.withAppendedPath(ProviderTableMeta
.CONTENT_URI_DIR
, ""+ folder
.getFileId()); // URI for recursive deletion
473 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?" + " AND " + ProviderTableMeta
.FILE_PATH
+ "=?";
474 String
[] whereArgs
= new String
[]{mAccount
.name
, folder
.getRemotePath()};
475 if (getContentProviderClient() != null
) {
477 getContentProviderClient().delete(folder_uri
, where
, whereArgs
);
478 } catch (RemoteException e
) {
482 getContentResolver().delete(folder_uri
, where
, whereArgs
);
484 updateFolderSize(folder
.getParentId());
487 private void removeLocalFolder(File folder
) {
488 if (folder
.exists()) {
489 File
[] files
= folder
.listFiles();
491 for (File file
: files
) {
492 if (file
.isDirectory()) {
493 removeLocalFolder(file
);
504 * Updates database for a folder that was moved to a different location.
506 * TODO explore better (faster) implementations
507 * TODO throw exceptions up !
509 public void moveFolder(OCFile folder
, String newPath
) {
510 // TODO check newPath
512 if (folder
!= null
&& folder
.isFolder() && folder
.fileExists() && !OCFile
.ROOT_PATH
.equals(folder
.getFileName())) {
513 /// 1. get all the descendants of 'dir' in a single QUERY (including 'dir')
515 if (getContentProviderClient() != null
) {
517 c
= getContentProviderClient().query(ProviderTableMeta
.CONTENT_URI
,
519 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " + ProviderTableMeta
.FILE_PATH
+ " LIKE ? ",
520 new String
[] { mAccount
.name
, folder
.getRemotePath() + "%" }, ProviderTableMeta
.FILE_PATH
+ " ASC ");
521 } catch (RemoteException e
) {
522 Log_OC
.e(TAG
, e
.getMessage());
525 c
= getContentResolver().query(ProviderTableMeta
.CONTENT_URI
,
527 ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=? AND " + ProviderTableMeta
.FILE_PATH
+ " LIKE ? ",
528 new String
[] { mAccount
.name
, folder
.getRemotePath() + "%" }, ProviderTableMeta
.FILE_PATH
+ " ASC ");
531 /// 2. prepare a batch of update operations to change all the descendants
532 ArrayList
<ContentProviderOperation
> operations
= new ArrayList
<ContentProviderOperation
>(c
.getCount());
533 int lengthOfOldPath
= folder
.getRemotePath().length();
534 String defaultSavePath
= FileStorageUtils
.getSavePath(mAccount
.name
);
535 int lengthOfOldStoragePath
= defaultSavePath
.length() + lengthOfOldPath
;
536 if (c
.moveToFirst()) {
538 ContentValues cv
= new ContentValues(); // don't take the constructor out of the loop and clear the object
539 OCFile child
= createFileInstance(c
);
540 cv
.put(ProviderTableMeta
.FILE_PATH
, newPath
+ child
.getRemotePath().substring(lengthOfOldPath
));
541 if (child
.getStoragePath() != null
&& child
.getStoragePath().startsWith(defaultSavePath
)) {
542 cv
.put(ProviderTableMeta
.FILE_STORAGE_PATH
, defaultSavePath
+ newPath
+ child
.getStoragePath().substring(lengthOfOldStoragePath
));
544 operations
.add(ContentProviderOperation
.newUpdate(ProviderTableMeta
.CONTENT_URI
).
546 withSelection( ProviderTableMeta
._ID
+ "=?",
547 new String
[] { String
.valueOf(child
.getFileId()) })
549 } while (c
.moveToNext());
553 /// 3. apply updates in batch
555 if (getContentResolver() != null
) {
556 getContentResolver().applyBatch(MainApp
.getAuthority(), operations
);
559 getContentProviderClient().applyBatch(operations
);
562 } catch (OperationApplicationException e
) {
563 Log_OC
.e(TAG
, "Fail to update descendants of " + folder
.getFileId() + " in database", e
);
565 } catch (RemoteException e
) {
566 Log_OC
.e(TAG
, "Fail to update desendants of " + folder
.getFileId() + " in database", e
);
573 private Vector
<OCFile
> getFolderContent(long parentId
) {
575 Vector
<OCFile
> ret
= new Vector
<OCFile
>();
577 Uri req_uri
= Uri
.withAppendedPath(
578 ProviderTableMeta
.CONTENT_URI_DIR
,
579 String
.valueOf(parentId
));
582 if (getContentProviderClient() != null
) {
584 c
= getContentProviderClient().query(req_uri
, null
,
585 ProviderTableMeta
.FILE_PARENT
+ "=?" ,
586 new String
[] { String
.valueOf(parentId
)}, null
);
587 } catch (RemoteException e
) {
588 Log_OC
.e(TAG
, e
.getMessage());
592 c
= getContentResolver().query(req_uri
, null
,
593 ProviderTableMeta
.FILE_PARENT
+ "=?" ,
594 new String
[] { String
.valueOf(parentId
)}, null
);
597 if (c
.moveToFirst()) {
599 OCFile child
= createFileInstance(c
);
601 } while (c
.moveToNext());
606 Collections
.sort(ret
);
612 private OCFile
createRootDir() {
613 OCFile file
= new OCFile(OCFile
.ROOT_PATH
);
614 file
.setMimetype("DIR");
615 file
.setParentId(FileDataStorageManager
.ROOT_PARENT_ID
);
620 private boolean fileExists(String cmp_key
, String value
) {
622 if (getContentResolver() != null
) {
623 c
= getContentResolver()
624 .query(ProviderTableMeta
.CONTENT_URI
,
627 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
629 new String
[] { value
, mAccount
.name
}, null
);
632 c
= getContentProviderClient().query(
633 ProviderTableMeta
.CONTENT_URI
,
636 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?",
637 new String
[] { value
, mAccount
.name
}, null
);
638 } catch (RemoteException e
) {
640 "Couldn't determine file existance, assuming non existance: "
645 boolean retval
= c
.moveToFirst();
650 private Cursor
getCursorForValue(String key
, String value
) {
652 if (getContentResolver() != null
) {
653 c
= getContentResolver()
654 .query(ProviderTableMeta
.CONTENT_URI
,
657 + ProviderTableMeta
.FILE_ACCOUNT_OWNER
659 new String
[] { value
, mAccount
.name
}, null
);
662 c
= getContentProviderClient().query(
663 ProviderTableMeta
.CONTENT_URI
,
665 key
+ "=? AND " + ProviderTableMeta
.FILE_ACCOUNT_OWNER
666 + "=?", new String
[] { value
, mAccount
.name
},
668 } catch (RemoteException e
) {
669 Log_OC
.e(TAG
, "Could not get file details: " + e
.getMessage());
676 private Cursor
getShareCursorForValue(String key
, String value
) {
678 if (getContentResolver() != null
) {
679 c
= getContentResolver()
680 .query(ProviderTableMeta
.CONTENT_URI_SHARE
,
683 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
685 new String
[] { value
, mAccount
.name
}, null
);
688 c
= getContentProviderClient().query(
689 ProviderTableMeta
.CONTENT_URI_SHARE
,
691 key
+ "=? AND " + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
692 + "=?", new String
[] { value
, mAccount
.name
},
694 } catch (RemoteException e
) {
695 Log_OC
.e(TAG
, "Could not get file details: " + e
.getMessage());
702 private OCFile
createFileInstance(Cursor c
) {
705 file
= new OCFile(c
.getString(c
706 .getColumnIndex(ProviderTableMeta
.FILE_PATH
)));
707 file
.setFileId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
._ID
)));
708 file
.setParentId(c
.getLong(c
709 .getColumnIndex(ProviderTableMeta
.FILE_PARENT
)));
710 file
.setMimetype(c
.getString(c
711 .getColumnIndex(ProviderTableMeta
.FILE_CONTENT_TYPE
)));
712 if (!file
.isFolder()) {
713 file
.setStoragePath(c
.getString(c
714 .getColumnIndex(ProviderTableMeta
.FILE_STORAGE_PATH
)));
715 if (file
.getStoragePath() == null
) {
716 // try to find existing file and bind it with current account; - with the current update of SynchronizeFolderOperation, this won't be necessary anymore after a full synchronization of the account
717 File f
= new File(FileStorageUtils
.getDefaultSavePathFor(mAccount
.name
, file
));
719 file
.setStoragePath(f
.getAbsolutePath());
720 file
.setLastSyncDateForData(f
.lastModified());
724 file
.setFileLength(c
.getLong(c
725 .getColumnIndex(ProviderTableMeta
.FILE_CONTENT_LENGTH
)));
726 file
.setCreationTimestamp(c
.getLong(c
727 .getColumnIndex(ProviderTableMeta
.FILE_CREATION
)));
728 file
.setModificationTimestamp(c
.getLong(c
729 .getColumnIndex(ProviderTableMeta
.FILE_MODIFIED
)));
730 file
.setModificationTimestampAtLastSyncForData(c
.getLong(c
731 .getColumnIndex(ProviderTableMeta
.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA
)));
732 file
.setLastSyncDateForProperties(c
.getLong(c
733 .getColumnIndex(ProviderTableMeta
.FILE_LAST_SYNC_DATE
)));
734 file
.setLastSyncDateForData(c
.getLong(c
.
735 getColumnIndex(ProviderTableMeta
.FILE_LAST_SYNC_DATE_FOR_DATA
)));
736 file
.setKeepInSync(c
.getInt(
737 c
.getColumnIndex(ProviderTableMeta
.FILE_KEEP_IN_SYNC
)) == 1 ? true
: false
);
738 file
.setEtag(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_ETAG
)));
739 file
.setShareByLink(c
.getInt(
740 c
.getColumnIndex(ProviderTableMeta
.FILE_SHARE_BY_LINK
)) == 1 ? true
: false
);
741 file
.setPublicLink(c
.getString(c
.getColumnIndex(ProviderTableMeta
.FILE_PUBLIC_LINK
)));
748 * Returns if the file/folder is shared by link or not
749 * @param path Path of the file/folder
752 public boolean isShareByLink(String path
) {
753 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_STORAGE_PATH
, path
);
755 if (c
.moveToFirst()) {
756 file
= createFileInstance(c
);
759 return file
.isShareByLink();
763 * Returns the public link of the file/folder
764 * @param path Path of the file/folder
767 public String
getPublicLink(String path
) {
768 Cursor c
= getCursorForValue(ProviderTableMeta
.FILE_STORAGE_PATH
, path
);
770 if (c
.moveToFirst()) {
771 file
= createFileInstance(c
);
774 return file
.getPublicLink();
778 // Methods for Shares
779 public boolean saveShare(OCShare share
) {
780 boolean overriden
= false
;
781 ContentValues cv
= new ContentValues();
782 cv
.put(ProviderTableMeta
.OCSHARES_FILE_SOURCE
, share
.getFileSource());
783 cv
.put(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
, share
.getItemSource());
784 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_TYPE
, share
.getShareType().getValue());
785 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH
, share
.getShareWith());
786 cv
.put(ProviderTableMeta
.OCSHARES_PATH
, share
.getPath());
787 cv
.put(ProviderTableMeta
.OCSHARES_PERMISSIONS
, share
.getPermissions());
788 cv
.put(ProviderTableMeta
.OCSHARES_SHARED_DATE
, share
.getSharedDate());
789 cv
.put(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
, share
.getExpirationDate());
790 cv
.put(ProviderTableMeta
.OCSHARES_TOKEN
, share
.getToken());
791 cv
.put(ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
, share
.getSharedWithDisplayName());
792 cv
.put(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
, share
.isDirectory() ?
1 : 0);
793 cv
.put(ProviderTableMeta
.OCSHARES_USER_ID
, share
.getUserId());
794 cv
.put(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
, share
.getIdRemoteShared());
795 cv
.put(ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
, mAccount
.name
);
797 boolean samePath
= shareExists(share
.getPath());
799 shareExists(share
.getId())) { // for renamed files; no more delete and create
801 OCShare oldFile
= null
;
803 oldFile
= getShareByPath(share
.getPath());
804 share
.setId(oldFile
.getId());
806 oldFile
= getShareById(share
.getId());
810 if (getContentResolver() != null
) {
811 getContentResolver().update(ProviderTableMeta
.CONTENT_URI_SHARE
, cv
,
812 ProviderTableMeta
._ID
+ "=?",
813 new String
[] { String
.valueOf(share
.getId()) });
816 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI_SHARE
,
817 cv
, ProviderTableMeta
._ID
+ "=?",
818 new String
[] { String
.valueOf(share
.getId()) });
819 } catch (RemoteException e
) {
821 "Fail to insert insert file to database "
826 Uri result_uri
= null
;
827 if (getContentResolver() != null
) {
828 result_uri
= getContentResolver().insert(
829 ProviderTableMeta
.CONTENT_URI_SHARE
, cv
);
832 result_uri
= getContentProviderClient().insert(
833 ProviderTableMeta
.CONTENT_URI_SHARE
, cv
);
834 } catch (RemoteException e
) {
836 "Fail to insert insert file to database "
840 if (result_uri
!= null
) {
841 long new_id
= Long
.parseLong(result_uri
.getPathSegments()
850 private OCShare
getShareById(long id
) {
851 Cursor c
= getShareCursorForValue(ProviderTableMeta
._ID
, String
.valueOf(id
));
852 OCShare share
= null
;
853 if (c
.moveToFirst()) {
854 share
= createShareInstance(c
);
860 public OCShare
getShareByPath(String path
) {
861 Cursor c
= getShareCursorForValue(ProviderTableMeta
.OCSHARES_PATH
, path
);
862 OCShare share
= null
;
863 if (c
.moveToFirst()) {
864 share
= createShareInstance(c
);
870 private OCShare
createShareInstance(Cursor c
) {
871 OCShare share
= null
;
873 share
= new OCShare(c
.getString(c
874 .getColumnIndex(ProviderTableMeta
.OCSHARES_PATH
)));
875 share
.setId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
._ID
)));
876 share
.setFileSource(c
.getLong(c
877 .getColumnIndex(ProviderTableMeta
.OCSHARES_ITEM_SOURCE
)));
878 share
.setShareType(ShareType
.fromValue(c
.getInt(c
879 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARE_TYPE
))));
880 share
.setPermissions(c
.getInt(c
881 .getColumnIndex(ProviderTableMeta
.OCSHARES_PERMISSIONS
)));
882 share
.setSharedDate(c
.getLong(c
883 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARED_DATE
)));
884 share
.setExpirationDate(c
.getLong(c
885 .getColumnIndex(ProviderTableMeta
.OCSHARES_EXPIRATION_DATE
)));
886 share
.setToken(c
.getString(c
887 .getColumnIndex(ProviderTableMeta
.OCSHARES_TOKEN
)));
888 share
.setSharedWithDisplayName(c
.getString(c
889 .getColumnIndex(ProviderTableMeta
.OCSHARES_SHARE_WITH_DISPLAY_NAME
)));
890 share
.setIsDirectory(c
.getInt(
891 c
.getColumnIndex(ProviderTableMeta
.OCSHARES_IS_DIRECTORY
)) == 1 ? true
: false
);
892 share
.setUserId(c
.getLong(c
.getColumnIndex(ProviderTableMeta
.OCSHARES_USER_ID
)));
893 share
.setIdRemoteShared(c
.getLong(c
.getColumnIndex(ProviderTableMeta
.OCSHARES_ID_REMOTE_SHARED
)));
899 private boolean shareExists(String cmp_key
, String value
) {
901 if (getContentResolver() != null
) {
902 c
= getContentResolver()
903 .query(ProviderTableMeta
.CONTENT_URI_SHARE
,
906 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
908 new String
[] { value
, mAccount
.name
}, null
);
911 c
= getContentProviderClient().query(
912 ProviderTableMeta
.CONTENT_URI_SHARE
,
915 + ProviderTableMeta
.OCSHARES_ACCOUNT_OWNER
+ "=?",
916 new String
[] { value
, mAccount
.name
}, null
);
917 } catch (RemoteException e
) {
919 "Couldn't determine file existance, assuming non existance: "
924 boolean retval
= c
.moveToFirst();
929 public boolean shareExists(long id
) {
930 return shareExists(ProviderTableMeta
._ID
, String
.valueOf(id
));
933 public boolean shareExists(String path
) {
934 return shareExists(ProviderTableMeta
.OCSHARES_PATH
, path
);
937 public void cleanShare() {
938 ContentValues cv
= new ContentValues();
939 cv
.put(ProviderTableMeta
.FILE_SHARE_BY_LINK
, false
);
940 cv
.put(ProviderTableMeta
.FILE_PUBLIC_LINK
, "");
941 String where
= ProviderTableMeta
.FILE_ACCOUNT_OWNER
+ "=?";
942 String
[] whereArgs
= new String
[]{mAccount
.name
};
944 if (getContentResolver() != null
) {
945 getContentResolver().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
);
949 getContentProviderClient().update(ProviderTableMeta
.CONTENT_URI
, cv
, where
, whereArgs
);
951 } catch (RemoteException e
) {
952 Log_OC
.e(TAG
, "Exception in cleanShareFile" + e
.getMessage());