Merge branch 'develop' into refresh_folder_contents_when_browsed_into
[pub/Android/ownCloud.git] / src / com / owncloud / android / datamodel / FileDataStorageManager.java
1 /* ownCloud Android client application
2 * Copyright (C) 2012 Bartek Przybylski
3 * Copyright (C) 2012-2013 ownCloud Inc.
4 *
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.
8 *
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.
13 *
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/>.
16 *
17 */
18
19 package com.owncloud.android.datamodel;
20
21 import java.io.File;
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;
27
28 import com.owncloud.android.Log_OC;
29 import com.owncloud.android.db.ProviderMeta;
30 import com.owncloud.android.db.ProviderMeta.ProviderTableMeta;
31 import com.owncloud.android.utils.FileStorageUtils;
32
33 import android.accounts.Account;
34 import android.content.ContentProviderClient;
35 import android.content.ContentProviderOperation;
36 import android.content.ContentProviderResult;
37 import android.content.ContentResolver;
38 import android.content.ContentUris;
39 import android.content.ContentValues;
40 import android.content.OperationApplicationException;
41 import android.database.Cursor;
42 import android.net.Uri;
43 import android.os.RemoteException;
44
45 public class FileDataStorageManager {
46
47 public static final int ROOT_PARENT_ID = 0;
48
49 private ContentResolver mContentResolver;
50 private ContentProviderClient mContentProviderClient;
51 private Account mAccount;
52
53 private static String TAG = FileDataStorageManager.class.getSimpleName();
54
55
56 public FileDataStorageManager(Account account, ContentResolver cr) {
57 mContentProviderClient = null;
58 mContentResolver = cr;
59 mAccount = account;
60 }
61
62 public FileDataStorageManager(Account account, ContentProviderClient cp) {
63 mContentProviderClient = cp;
64 mContentResolver = null;
65 mAccount = account;
66 }
67
68
69 public void setAccount(Account account) {
70 mAccount = account;
71 }
72
73 public Account getAccount() {
74 return mAccount;
75 }
76
77 public void setContentResolver(ContentResolver cr) {
78 mContentResolver = cr;
79 }
80
81 public ContentResolver getContentResolver() {
82 return mContentResolver;
83 }
84
85 public void setContentProviderClient(ContentProviderClient cp) {
86 mContentProviderClient = cp;
87 }
88
89 public ContentProviderClient getContentProviderClient() {
90 return mContentProviderClient;
91 }
92
93
94 public OCFile getFileByPath(String path) {
95 Cursor c = getCursorForValue(ProviderTableMeta.FILE_PATH, path);
96 OCFile file = null;
97 if (c.moveToFirst()) {
98 file = createFileInstance(c);
99 }
100 c.close();
101 if (file == null && OCFile.ROOT_PATH.equals(path)) {
102 return createRootDir(); // root should always exist
103 }
104 return file;
105 }
106
107
108 public OCFile getFileById(long id) {
109 Cursor c = getCursorForValue(ProviderTableMeta._ID, String.valueOf(id));
110 OCFile file = null;
111 if (c.moveToFirst()) {
112 file = createFileInstance(c);
113 }
114 c.close();
115 return file;
116 }
117
118 public OCFile getFileByLocalPath(String path) {
119 Cursor c = getCursorForValue(ProviderTableMeta.FILE_STORAGE_PATH, path);
120 OCFile file = null;
121 if (c.moveToFirst()) {
122 file = createFileInstance(c);
123 }
124 c.close();
125 return file;
126 }
127
128 public boolean fileExists(long id) {
129 return fileExists(ProviderTableMeta._ID, String.valueOf(id));
130 }
131
132 public boolean fileExists(String path) {
133 return fileExists(ProviderTableMeta.FILE_PATH, path);
134 }
135
136
137 public Vector<OCFile> getFolderContent(OCFile f) {
138 if (f != null && f.isFolder() && f.getFileId() != -1) {
139 return getFolderContent(f.getFileId());
140
141 } else {
142 return new Vector<OCFile>();
143 }
144 }
145
146
147 public Vector<OCFile> getFolderImages(OCFile folder) {
148 Vector<OCFile> ret = new Vector<OCFile>();
149 if (folder != null) {
150 // TODO better implementation, filtering in the access to database (if possible) instead of here
151 Vector<OCFile> tmp = getFolderContent(folder);
152 OCFile current = null;
153 for (int i=0; i<tmp.size(); i++) {
154 current = tmp.get(i);
155 if (current.isImage()) {
156 ret.add(current);
157 }
158 }
159 }
160 return ret;
161 }
162
163
164 public boolean saveFile(OCFile file) {
165 boolean overriden = false;
166 ContentValues cv = new ContentValues();
167 cv.put(ProviderTableMeta.FILE_MODIFIED, file.getModificationTimestamp());
168 cv.put(ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA, file.getModificationTimestampAtLastSyncForData());
169 cv.put(ProviderTableMeta.FILE_CREATION, file.getCreationTimestamp());
170 cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, file.getFileLength());
171 cv.put(ProviderTableMeta.FILE_CONTENT_TYPE, file.getMimetype());
172 cv.put(ProviderTableMeta.FILE_NAME, file.getFileName());
173 //if (file.getParentId() != DataStorageManager.ROOT_PARENT_ID)
174 cv.put(ProviderTableMeta.FILE_PARENT, file.getParentId());
175 cv.put(ProviderTableMeta.FILE_PATH, file.getRemotePath());
176 if (!file.isFolder())
177 cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getStoragePath());
178 cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, mAccount.name);
179 cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE, file.getLastSyncDateForProperties());
180 cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA, file.getLastSyncDateForData());
181 cv.put(ProviderTableMeta.FILE_KEEP_IN_SYNC, file.keepInSync() ? 1 : 0);
182 cv.put(ProviderTableMeta.FILE_ETAG, file.getEtag());
183
184 boolean sameRemotePath = fileExists(file.getRemotePath());
185 if (sameRemotePath ||
186 fileExists(file.getFileId()) ) { // for renamed files; no more delete and create
187
188 OCFile oldFile = null;
189 if (sameRemotePath) {
190 oldFile = getFileByPath(file.getRemotePath());
191 file.setFileId(oldFile.getFileId());
192 } else {
193 oldFile = getFileById(file.getFileId());
194 }
195
196 overriden = true;
197 if (getContentResolver() != null) {
198 getContentResolver().update(ProviderTableMeta.CONTENT_URI, cv,
199 ProviderTableMeta._ID + "=?",
200 new String[] { String.valueOf(file.getFileId()) });
201 } else {
202 try {
203 getContentProviderClient().update(ProviderTableMeta.CONTENT_URI,
204 cv, ProviderTableMeta._ID + "=?",
205 new String[] { String.valueOf(file.getFileId()) });
206 } catch (RemoteException e) {
207 Log_OC.e(TAG,
208 "Fail to insert insert file to database "
209 + e.getMessage());
210 }
211 }
212 } else {
213 Uri result_uri = null;
214 if (getContentResolver() != null) {
215 result_uri = getContentResolver().insert(
216 ProviderTableMeta.CONTENT_URI_FILE, cv);
217 } else {
218 try {
219 result_uri = getContentProviderClient().insert(
220 ProviderTableMeta.CONTENT_URI_FILE, cv);
221 } catch (RemoteException e) {
222 Log_OC.e(TAG,
223 "Fail to insert insert file to database "
224 + e.getMessage());
225 }
226 }
227 if (result_uri != null) {
228 long new_id = Long.parseLong(result_uri.getPathSegments()
229 .get(1));
230 file.setFileId(new_id);
231 }
232 }
233
234 if (file.isFolder()) {
235 updateFolderSize(file.getFileId());
236 if (file.needsUpdatingWhileSaving()) {
237 for (OCFile f : getFolderContent(file))
238 saveFile(f);
239 }
240 }
241
242 return overriden;
243 }
244
245
246 /**
247 * Inserts or updates the list of files contained in a given folder.
248 *
249 * @param folder
250 * @param files
251 * @param removeNotUpdated
252 */
253 public void saveFolder(OCFile folder, Collection<OCFile> updatedFiles, Collection<OCFile> filesToRemove) {
254
255 Log_OC.d(TAG, "Saving folder " + folder.getRemotePath() + " with " + updatedFiles.size() + " children and " + filesToRemove.size() + " files to remove");
256
257 ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>(updatedFiles.size());
258 long folderSize = 0;
259
260 // prepare operations to insert or update files to save in the given folder
261 for (OCFile file : updatedFiles) {
262 ContentValues cv = new ContentValues();
263 cv.put(ProviderTableMeta.FILE_MODIFIED, file.getModificationTimestamp());
264 cv.put(ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA, file.getModificationTimestampAtLastSyncForData());
265 cv.put(ProviderTableMeta.FILE_CREATION, file.getCreationTimestamp());
266 cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, file.getFileLength());
267 folderSize += file.getFileLength();
268 cv.put(ProviderTableMeta.FILE_CONTENT_TYPE, file.getMimetype());
269 cv.put(ProviderTableMeta.FILE_NAME, file.getFileName());
270 //cv.put(ProviderTableMeta.FILE_PARENT, file.getParentId());
271 cv.put(ProviderTableMeta.FILE_PARENT, folder.getFileId());
272 cv.put(ProviderTableMeta.FILE_PATH, file.getRemotePath());
273 if (!file.isFolder()) {
274 cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getStoragePath());
275 }
276 cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, mAccount.name);
277 cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE, file.getLastSyncDateForProperties());
278 cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA, file.getLastSyncDateForData());
279 cv.put(ProviderTableMeta.FILE_KEEP_IN_SYNC, file.keepInSync() ? 1 : 0);
280 cv.put(ProviderTableMeta.FILE_ETAG, file.getEtag());
281
282 boolean existsByPath = fileExists(file.getRemotePath());
283 if (existsByPath || fileExists(file.getFileId())) {
284 // updating an existing file
285
286 /* CALLER IS THE RESPONSIBLE FOR GRANTING RIGHT UPDATE OF INFORMATION, NOT THIS METHOD.
287 *
288 * HERE ONLY DATA CONSISTENCY SHOULD BE GRANTED
289 */
290 /*
291 OCFile oldFile = null;
292 if (existsByPath) {
293 // grant same id
294 oldFile = getFileByPath(file.getRemotePath());
295 file.setFileId(oldFile.getFileId());
296 } else {
297 oldFile = getFileById(file.getFileId());
298 }
299
300 if (file.isFolder()) {
301 // folders keep size information, since it's calculated
302 file.setFileLength(oldFile.getFileLength());
303 cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, oldFile.getFileLength());
304
305 } else if (file.getStoragePath() == null && oldFile.getStoragePath() != null) {
306 // regular files keep access to local contents, although it's lost in the new OCFile
307 file.setStoragePath(oldFile.getStoragePath());
308 cv.put(ProviderTableMeta.FILE_STORAGE_PATH, oldFile.getStoragePath());
309 }
310 */
311
312 operations.add(ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI).
313 withValues(cv).
314 withSelection( ProviderTableMeta._ID + "=?",
315 new String[] { String.valueOf(file.getFileId()) })
316 .build());
317
318 } else {
319 // adding a new file
320 operations.add(ContentProviderOperation.newInsert(ProviderTableMeta.CONTENT_URI).withValues(cv).build());
321 }
322 }
323
324 // prepare operations to remove files in the given folder
325 String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?" + " AND " + ProviderTableMeta.FILE_PATH + "=?";
326 String [] whereArgs = null;
327 for (OCFile file : filesToRemove) {
328 if (file.getParentId() == folder.getFileId()) {
329 whereArgs = new String[]{mAccount.name, file.getRemotePath()};
330 //Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_FILE, "" + file.getFileId());
331 if (file.isFolder()) {
332 operations.add(ContentProviderOperation
333 .newDelete(ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_DIR, file.getFileId())).withSelection(where, whereArgs)
334 .build());
335 // TODO remove local folder
336 } else {
337 operations.add(ContentProviderOperation
338 .newDelete(ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_FILE, file.getFileId())).withSelection(where, whereArgs)
339 .build());
340 if (file.isDown()) {
341 new File(file.getStoragePath()).delete();
342 // TODO move the deletion of local contents after success of deletions
343 }
344 }
345 }
346 }
347
348 // update metadata of folder
349 ContentValues cv = new ContentValues();
350 cv.put(ProviderTableMeta.FILE_MODIFIED, folder.getModificationTimestamp());
351 cv.put(ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA, folder.getModificationTimestampAtLastSyncForData());
352 cv.put(ProviderTableMeta.FILE_CREATION, folder.getCreationTimestamp());
353 cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, folderSize);
354 cv.put(ProviderTableMeta.FILE_CONTENT_TYPE, folder.getMimetype());
355 cv.put(ProviderTableMeta.FILE_NAME, folder.getFileName());
356 cv.put(ProviderTableMeta.FILE_PARENT, folder.getParentId());
357 cv.put(ProviderTableMeta.FILE_PATH, folder.getRemotePath());
358 cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, mAccount.name);
359 cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE, folder.getLastSyncDateForProperties());
360 cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA, folder.getLastSyncDateForData());
361 cv.put(ProviderTableMeta.FILE_KEEP_IN_SYNC, folder.keepInSync() ? 1 : 0);
362 cv.put(ProviderTableMeta.FILE_ETAG, folder.getEtag());
363 operations.add(ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI).
364 withValues(cv).
365 withSelection( ProviderTableMeta._ID + "=?",
366 new String[] { String.valueOf(folder.getFileId()) })
367 .build());
368
369 // apply operations in batch
370 ContentProviderResult[] results = null;
371 Log_OC.d(TAG, "Sending " + operations.size() + " operations to FileContentProvider");
372 try {
373 if (getContentResolver() != null) {
374 results = getContentResolver().applyBatch(ProviderMeta.AUTHORITY_FILES, operations);
375
376 } else {
377 results = getContentProviderClient().applyBatch(operations);
378 }
379
380 } catch (OperationApplicationException e) {
381 Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage());
382
383 } catch (RemoteException e) {
384 Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage());
385 }
386
387 // update new id in file objects for insertions
388 if (results != null) {
389 long newId;
390 Iterator<OCFile> filesIt = updatedFiles.iterator();
391 OCFile file = null;
392 for (int i=0; i<results.length; i++) {
393 if (filesIt.hasNext()) {
394 file = filesIt.next();
395 } else {
396 file = null;
397 }
398 if (results[i].uri != null) {
399 newId = Long.parseLong(results[i].uri.getPathSegments().get(1));
400 //updatedFiles.get(i).setFileId(newId);
401 if (file != null) {
402 file.setFileId(newId);
403 }
404 }
405 }
406 }
407
408 }
409
410
411 /**
412 *
413 * @param id
414 */
415 public void updateFolderSize(long id) {
416 if (getContentResolver() != null) {
417 getContentResolver().update(ProviderTableMeta.CONTENT_URI_DIR, null,
418 ProviderTableMeta._ID + "=?",
419 new String[] { String.valueOf(id) });
420 } else {
421 try {
422 getContentProviderClient().update(ProviderTableMeta.CONTENT_URI_DIR, null,
423 ProviderTableMeta._ID + "=?",
424 new String[] { String.valueOf(id) });
425
426 } catch (RemoteException e) {
427 Log_OC.e(TAG, "Exception in update of folder size through compatibility patch " + e.getMessage());
428 }
429 }
430 }
431
432
433 public void removeFile(OCFile file, boolean removeDBData, boolean removeLocalCopy) {
434 if (file != null) {
435 if (file.isFolder()) {
436 removeFolder(file, removeDBData, removeLocalCopy);
437
438 } else {
439 if (removeDBData) {
440 //Uri file_uri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_FILE, ""+file.getFileId());
441 Uri file_uri = ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_FILE, file.getFileId());
442 String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?" + " AND " + ProviderTableMeta.FILE_PATH + "=?";
443 String [] whereArgs = new String[]{mAccount.name, file.getRemotePath()};
444 if (getContentProviderClient() != null) {
445 try {
446 getContentProviderClient().delete(file_uri, where, whereArgs);
447 } catch (RemoteException e) {
448 e.printStackTrace();
449 }
450 } else {
451 getContentResolver().delete(file_uri, where, whereArgs);
452 }
453 }
454 if (removeLocalCopy && file.isDown()) {
455 boolean success = new File(file.getStoragePath()).delete();
456 if (!removeDBData && success) {
457 // maybe unnecessary, but should be checked TODO remove if unnecessary
458 file.setStoragePath(null);
459 saveFile(file);
460 }
461 }
462 }
463 }
464 }
465
466
467 public void removeFolder(OCFile folder, boolean removeDBData, boolean removeLocalContent) {
468 if (folder != null && folder.isFolder()) {
469 if (removeDBData && folder.getFileId() != -1) {
470 removeFolderInDb(folder);
471 }
472 if (removeLocalContent) {
473 File localFolder = new File(FileStorageUtils.getDefaultSavePathFor(mAccount.name, folder));
474 removeLocalFolder(localFolder);
475 }
476 }
477 }
478
479 private void removeFolderInDb(OCFile folder) {
480 Uri folder_uri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_DIR, ""+ folder.getFileId()); // URI for recursive deletion
481 String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?" + " AND " + ProviderTableMeta.FILE_PATH + "=?";
482 String [] whereArgs = new String[]{mAccount.name, folder.getRemotePath()};
483 if (getContentProviderClient() != null) {
484 try {
485 getContentProviderClient().delete(folder_uri, where, whereArgs);
486 } catch (RemoteException e) {
487 e.printStackTrace();
488 }
489 } else {
490 getContentResolver().delete(folder_uri, where, whereArgs);
491 }
492 }
493
494 private void removeLocalFolder(File folder) {
495 if (folder.exists()) {
496 File[] files = folder.listFiles();
497 if (files != null) {
498 for (File file : files) {
499 if (file.isDirectory()) {
500 removeLocalFolder(file);
501 } else {
502 file.delete();
503 }
504 }
505 }
506 folder.delete();
507 }
508 }
509
510 /**
511 * Updates database for a folder that was moved to a different location.
512 *
513 * TODO explore better (faster) implementations
514 * TODO throw exceptions up !
515 */
516 public void moveFolder(OCFile folder, String newPath) {
517 // TODO check newPath
518
519 if (folder != null && folder.isFolder() && folder.fileExists() && !OCFile.ROOT_PATH.equals(folder.getFileName())) {
520 /// 1. get all the descendants of 'dir' in a single QUERY (including 'dir')
521 Cursor c = null;
522 if (getContentProviderClient() != null) {
523 try {
524 c = getContentProviderClient().query(ProviderTableMeta.CONTENT_URI,
525 null,
526 ProviderTableMeta.FILE_ACCOUNT_OWNER + "=? AND " + ProviderTableMeta.FILE_PATH + " LIKE ? ",
527 new String[] { mAccount.name, folder.getRemotePath() + "%" }, ProviderTableMeta.FILE_PATH + " ASC ");
528 } catch (RemoteException e) {
529 Log_OC.e(TAG, e.getMessage());
530 }
531 } else {
532 c = getContentResolver().query(ProviderTableMeta.CONTENT_URI,
533 null,
534 ProviderTableMeta.FILE_ACCOUNT_OWNER + "=? AND " + ProviderTableMeta.FILE_PATH + " LIKE ? ",
535 new String[] { mAccount.name, folder.getRemotePath() + "%" }, ProviderTableMeta.FILE_PATH + " ASC ");
536 }
537
538 /// 2. prepare a batch of update operations to change all the descendants
539 ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>(c.getCount());
540 int lengthOfOldPath = folder.getRemotePath().length();
541 String defaultSavePath = FileStorageUtils.getSavePath(mAccount.name);
542 int lengthOfOldStoragePath = defaultSavePath.length() + lengthOfOldPath;
543 if (c.moveToFirst()) {
544 do {
545 ContentValues cv = new ContentValues(); // don't take the constructor out of the loop and clear the object
546 OCFile child = createFileInstance(c);
547 cv.put(ProviderTableMeta.FILE_PATH, newPath + child.getRemotePath().substring(lengthOfOldPath));
548 if (child.getStoragePath() != null && child.getStoragePath().startsWith(defaultSavePath)) {
549 cv.put(ProviderTableMeta.FILE_STORAGE_PATH, defaultSavePath + newPath + child.getStoragePath().substring(lengthOfOldStoragePath));
550 }
551 operations.add(ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI).
552 withValues(cv).
553 withSelection( ProviderTableMeta._ID + "=?",
554 new String[] { String.valueOf(child.getFileId()) })
555 .build());
556 } while (c.moveToNext());
557 }
558 c.close();
559
560 /// 3. apply updates in batch
561 try {
562 if (getContentResolver() != null) {
563 getContentResolver().applyBatch(ProviderMeta.AUTHORITY_FILES, operations);
564
565 } else {
566 getContentProviderClient().applyBatch(operations);
567 }
568
569 } catch (OperationApplicationException e) {
570 Log_OC.e(TAG, "Fail to update descendants of " + folder.getFileId() + " in database", e);
571
572 } catch (RemoteException e) {
573 Log_OC.e(TAG, "Fail to update desendants of " + folder.getFileId() + " in database", e);
574 }
575
576 }
577 }
578
579
580 private Vector<OCFile> getFolderContent(long parentId) {
581
582 Vector<OCFile> ret = new Vector<OCFile>();
583
584 Uri req_uri = Uri.withAppendedPath(
585 ProviderTableMeta.CONTENT_URI_DIR,
586 String.valueOf(parentId));
587 Cursor c = null;
588
589 if (getContentProviderClient() != null) {
590 try {
591 c = getContentProviderClient().query(req_uri, null,
592 ProviderTableMeta.FILE_PARENT + "=?" ,
593 new String[] { String.valueOf(parentId)}, null);
594 } catch (RemoteException e) {
595 Log_OC.e(TAG, e.getMessage());
596 return ret;
597 }
598 } else {
599 c = getContentResolver().query(req_uri, null,
600 ProviderTableMeta.FILE_PARENT + "=?" ,
601 new String[] { String.valueOf(parentId)}, null);
602 }
603
604 if (c.moveToFirst()) {
605 do {
606 OCFile child = createFileInstance(c);
607 ret.add(child);
608 } while (c.moveToNext());
609 }
610
611 c.close();
612
613 Collections.sort(ret);
614
615 return ret;
616 }
617
618
619 private OCFile createRootDir() {
620 OCFile file = new OCFile(OCFile.ROOT_PATH);
621 file.setMimetype("DIR");
622 file.setParentId(FileDataStorageManager.ROOT_PARENT_ID);
623 saveFile(file);
624 return file;
625 }
626
627 private boolean fileExists(String cmp_key, String value) {
628 Cursor c;
629 if (getContentResolver() != null) {
630 c = getContentResolver()
631 .query(ProviderTableMeta.CONTENT_URI,
632 null,
633 cmp_key + "=? AND "
634 + ProviderTableMeta.FILE_ACCOUNT_OWNER
635 + "=?",
636 new String[] { value, mAccount.name }, null);
637 } else {
638 try {
639 c = getContentProviderClient().query(
640 ProviderTableMeta.CONTENT_URI,
641 null,
642 cmp_key + "=? AND "
643 + ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?",
644 new String[] { value, mAccount.name }, null);
645 } catch (RemoteException e) {
646 Log_OC.e(TAG,
647 "Couldn't determine file existance, assuming non existance: "
648 + e.getMessage());
649 return false;
650 }
651 }
652 boolean retval = c.moveToFirst();
653 c.close();
654 return retval;
655 }
656
657 private Cursor getCursorForValue(String key, String value) {
658 Cursor c = null;
659 if (getContentResolver() != null) {
660 c = getContentResolver()
661 .query(ProviderTableMeta.CONTENT_URI,
662 null,
663 key + "=? AND "
664 + ProviderTableMeta.FILE_ACCOUNT_OWNER
665 + "=?",
666 new String[] { value, mAccount.name }, null);
667 } else {
668 try {
669 c = getContentProviderClient().query(
670 ProviderTableMeta.CONTENT_URI,
671 null,
672 key + "=? AND " + ProviderTableMeta.FILE_ACCOUNT_OWNER
673 + "=?", new String[] { value, mAccount.name },
674 null);
675 } catch (RemoteException e) {
676 Log_OC.e(TAG, "Could not get file details: " + e.getMessage());
677 c = null;
678 }
679 }
680 return c;
681 }
682
683 private OCFile createFileInstance(Cursor c) {
684 OCFile file = null;
685 if (c != null) {
686 file = new OCFile(c.getString(c
687 .getColumnIndex(ProviderTableMeta.FILE_PATH)));
688 file.setFileId(c.getLong(c.getColumnIndex(ProviderTableMeta._ID)));
689 file.setParentId(c.getLong(c
690 .getColumnIndex(ProviderTableMeta.FILE_PARENT)));
691 file.setMimetype(c.getString(c
692 .getColumnIndex(ProviderTableMeta.FILE_CONTENT_TYPE)));
693 if (!file.isFolder()) {
694 file.setStoragePath(c.getString(c
695 .getColumnIndex(ProviderTableMeta.FILE_STORAGE_PATH)));
696 if (file.getStoragePath() == null) {
697 // 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
698 File f = new File(FileStorageUtils.getDefaultSavePathFor(mAccount.name, file));
699 if (f.exists()) {
700 file.setStoragePath(f.getAbsolutePath());
701 file.setLastSyncDateForData(f.lastModified());
702 }
703 }
704 }
705 file.setFileLength(c.getLong(c
706 .getColumnIndex(ProviderTableMeta.FILE_CONTENT_LENGTH)));
707 file.setCreationTimestamp(c.getLong(c
708 .getColumnIndex(ProviderTableMeta.FILE_CREATION)));
709 file.setModificationTimestamp(c.getLong(c
710 .getColumnIndex(ProviderTableMeta.FILE_MODIFIED)));
711 file.setModificationTimestampAtLastSyncForData(c.getLong(c
712 .getColumnIndex(ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA)));
713 file.setLastSyncDateForProperties(c.getLong(c
714 .getColumnIndex(ProviderTableMeta.FILE_LAST_SYNC_DATE)));
715 file.setLastSyncDateForData(c.getLong(c.
716 getColumnIndex(ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA)));
717 file.setKeepInSync(c.getInt(
718 c.getColumnIndex(ProviderTableMeta.FILE_KEEP_IN_SYNC)) == 1 ? true : false);
719 file.setEtag(c.getString(c.getColumnIndex(ProviderTableMeta.FILE_ETAG)));
720
721 }
722 return file;
723 }
724
725 /*
726 * Update the size value of an OCFile in DB
727 *
728 private int updateSize(long id, long size) {
729 ContentValues cv = new ContentValues();
730 cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, size);
731 int result = -1;
732 if (getContentResolver() != null) {
733 result = getContentResolver().update(ProviderTableMeta.CONTENT_URI, cv, ProviderTableMeta._ID + "=?",
734 new String[] { String.valueOf(id) });
735 } else {
736 try {
737 result = getContentProviderClient().update(ProviderTableMeta.CONTENT_URI, cv, ProviderTableMeta._ID + "=?",
738 new String[] { String.valueOf(id) });
739 } catch (RemoteException e) {
740 Log_OC.e(TAG,"Fail to update size column into database " + e.getMessage());
741 }
742 }
743 return result;
744 }
745 */
746
747 /*
748 * Update the size of a subtree of folder from a file to the root
749 * @param parentId: parent of the file
750 *
751 private void updateSizesToTheRoot(long parentId) {
752
753 OCFile file;
754
755 while (parentId != FileDataStorageManager.ROOT_PARENT_ID) {
756
757 // Update the size of the parent
758 updateFolderSize(parentId);
759
760 // search the next parent
761 file = getFileById(parentId);
762 parentId = file.getParentId();
763
764 }
765 }
766 */
767
768 }