Fixed typos from 'shareWhatever' to 'sharedWhatever', and updated reference to library
[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-2014 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.MainApp;
29 import com.owncloud.android.db.ProviderMeta.ProviderTableMeta;
30 import com.owncloud.android.lib.resources.shares.OCShare;
31 import com.owncloud.android.lib.resources.shares.ShareType;
32 import com.owncloud.android.lib.resources.files.FileUtils;
33 import com.owncloud.android.utils.FileStorageUtils;
34 import com.owncloud.android.utils.Log_OC;
35
36
37 import android.accounts.Account;
38 import android.content.ContentProviderClient;
39 import android.content.ContentProviderOperation;
40 import android.content.ContentProviderResult;
41 import android.content.ContentResolver;
42 import android.content.ContentUris;
43 import android.content.ContentValues;
44 import android.content.OperationApplicationException;
45 import android.database.Cursor;
46 import android.net.Uri;
47 import android.os.RemoteException;
48
49 public class FileDataStorageManager {
50
51 public static final int ROOT_PARENT_ID = 0;
52
53 private ContentResolver mContentResolver;
54 private ContentProviderClient mContentProviderClient;
55 private Account mAccount;
56
57 private static String TAG = FileDataStorageManager.class.getSimpleName();
58
59
60 public FileDataStorageManager(Account account, ContentResolver cr) {
61 mContentProviderClient = null;
62 mContentResolver = cr;
63 mAccount = account;
64 }
65
66 public FileDataStorageManager(Account account, ContentProviderClient cp) {
67 mContentProviderClient = cp;
68 mContentResolver = null;
69 mAccount = account;
70 }
71
72
73 public void setAccount(Account account) {
74 mAccount = account;
75 }
76
77 public Account getAccount() {
78 return mAccount;
79 }
80
81 public void setContentResolver(ContentResolver cr) {
82 mContentResolver = cr;
83 }
84
85 public ContentResolver getContentResolver() {
86 return mContentResolver;
87 }
88
89 public void setContentProviderClient(ContentProviderClient cp) {
90 mContentProviderClient = cp;
91 }
92
93 public ContentProviderClient getContentProviderClient() {
94 return mContentProviderClient;
95 }
96
97
98 public OCFile getFileByPath(String path) {
99 Cursor c = getCursorForValue(ProviderTableMeta.FILE_PATH, path);
100 OCFile file = null;
101 if (c.moveToFirst()) {
102 file = createFileInstance(c);
103 }
104 c.close();
105 if (file == null && OCFile.ROOT_PATH.equals(path)) {
106 return createRootDir(); // root should always exist
107 }
108 return file;
109 }
110
111
112 public OCFile getFileById(long id) {
113 Cursor c = getCursorForValue(ProviderTableMeta._ID, String.valueOf(id));
114 OCFile file = null;
115 if (c.moveToFirst()) {
116 file = createFileInstance(c);
117 }
118 c.close();
119 return file;
120 }
121
122 public OCFile getFileByLocalPath(String path) {
123 Cursor c = getCursorForValue(ProviderTableMeta.FILE_STORAGE_PATH, path);
124 OCFile file = null;
125 if (c.moveToFirst()) {
126 file = createFileInstance(c);
127 }
128 c.close();
129 return file;
130 }
131
132 public boolean fileExists(long id) {
133 return fileExists(ProviderTableMeta._ID, String.valueOf(id));
134 }
135
136 public boolean fileExists(String path) {
137 return fileExists(ProviderTableMeta.FILE_PATH, path);
138 }
139
140
141 public Vector<OCFile> getFolderContent(OCFile f) {
142 if (f != null && f.isFolder() && f.getFileId() != -1) {
143 return getFolderContent(f.getFileId());
144
145 } else {
146 return new Vector<OCFile>();
147 }
148 }
149
150
151 public Vector<OCFile> getFolderImages(OCFile folder) {
152 Vector<OCFile> ret = new Vector<OCFile>();
153 if (folder != null) {
154 // TODO better implementation, filtering in the access to database (if possible) instead of here
155 Vector<OCFile> tmp = getFolderContent(folder);
156 OCFile current = null;
157 for (int i=0; i<tmp.size(); i++) {
158 current = tmp.get(i);
159 if (current.isImage()) {
160 ret.add(current);
161 }
162 }
163 }
164 return ret;
165 }
166
167
168 public boolean saveFile(OCFile file) {
169 boolean overriden = false;
170 ContentValues cv = new ContentValues();
171 cv.put(ProviderTableMeta.FILE_MODIFIED, file.getModificationTimestamp());
172 cv.put(ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA, file.getModificationTimestampAtLastSyncForData());
173 cv.put(ProviderTableMeta.FILE_CREATION, file.getCreationTimestamp());
174 cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, file.getFileLength());
175 cv.put(ProviderTableMeta.FILE_CONTENT_TYPE, file.getMimetype());
176 cv.put(ProviderTableMeta.FILE_NAME, file.getFileName());
177 //if (file.getParentId() != DataStorageManager.ROOT_PARENT_ID)
178 cv.put(ProviderTableMeta.FILE_PARENT, file.getParentId());
179 cv.put(ProviderTableMeta.FILE_PATH, file.getRemotePath());
180 if (!file.isFolder())
181 cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getStoragePath());
182 cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, mAccount.name);
183 cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE, file.getLastSyncDateForProperties());
184 cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA, file.getLastSyncDateForData());
185 cv.put(ProviderTableMeta.FILE_KEEP_IN_SYNC, file.keepInSync() ? 1 : 0);
186 cv.put(ProviderTableMeta.FILE_ETAG, file.getEtag());
187 cv.put(ProviderTableMeta.FILE_SHARE_BY_LINK, file.isShareByLink() ? 1 : 0);
188 cv.put(ProviderTableMeta.FILE_PUBLIC_LINK, file.getPublicLink());
189 cv.put(ProviderTableMeta.FILE_PERMISSIONS, file.getPermissions());
190 cv.put(ProviderTableMeta.FILE_REMOTE_ID, file.getRemoteId());
191
192 boolean sameRemotePath = fileExists(file.getRemotePath());
193 if (sameRemotePath ||
194 fileExists(file.getFileId()) ) { // for renamed files; no more delete and create
195
196 OCFile oldFile = null;
197 if (sameRemotePath) {
198 oldFile = getFileByPath(file.getRemotePath());
199 file.setFileId(oldFile.getFileId());
200 } else {
201 oldFile = getFileById(file.getFileId());
202 }
203
204 overriden = true;
205 if (getContentResolver() != null) {
206 getContentResolver().update(ProviderTableMeta.CONTENT_URI, cv,
207 ProviderTableMeta._ID + "=?",
208 new String[] { String.valueOf(file.getFileId()) });
209 } else {
210 try {
211 getContentProviderClient().update(ProviderTableMeta.CONTENT_URI,
212 cv, ProviderTableMeta._ID + "=?",
213 new String[] { String.valueOf(file.getFileId()) });
214 } catch (RemoteException e) {
215 Log_OC.e(TAG,
216 "Fail to insert insert file to database "
217 + e.getMessage());
218 }
219 }
220 } else {
221 Uri result_uri = null;
222 if (getContentResolver() != null) {
223 result_uri = getContentResolver().insert(
224 ProviderTableMeta.CONTENT_URI_FILE, cv);
225 } else {
226 try {
227 result_uri = getContentProviderClient().insert(
228 ProviderTableMeta.CONTENT_URI_FILE, cv);
229 } catch (RemoteException e) {
230 Log_OC.e(TAG,
231 "Fail to insert insert file to database "
232 + e.getMessage());
233 }
234 }
235 if (result_uri != null) {
236 long new_id = Long.parseLong(result_uri.getPathSegments()
237 .get(1));
238 file.setFileId(new_id);
239 }
240 }
241
242 // if (file.isFolder()) {
243 // updateFolderSize(file.getFileId());
244 // } else {
245 // updateFolderSize(file.getParentId());
246 // }
247
248 return overriden;
249 }
250
251
252 /**
253 * Inserts or updates the list of files contained in a given folder.
254 *
255 * CALLER IS THE RESPONSIBLE FOR GRANTING RIGHT UPDATE OF INFORMATION, NOT THIS METHOD.
256 * HERE ONLY DATA CONSISTENCY SHOULD BE GRANTED
257 *
258 * @param folder
259 * @param files
260 * @param removeNotUpdated
261 */
262 public void saveFolder(OCFile folder, Collection<OCFile> updatedFiles, Collection<OCFile> filesToRemove) {
263
264 Log_OC.d(TAG, "Saving folder " + folder.getRemotePath() + " with " + updatedFiles.size() + " children and " + filesToRemove.size() + " files to remove");
265
266 ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>(updatedFiles.size());
267
268 // prepare operations to insert or update files to save in the given folder
269 for (OCFile file : updatedFiles) {
270 ContentValues cv = new ContentValues();
271 cv.put(ProviderTableMeta.FILE_MODIFIED, file.getModificationTimestamp());
272 cv.put(ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA, file.getModificationTimestampAtLastSyncForData());
273 cv.put(ProviderTableMeta.FILE_CREATION, file.getCreationTimestamp());
274 cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, file.getFileLength());
275 cv.put(ProviderTableMeta.FILE_CONTENT_TYPE, file.getMimetype());
276 cv.put(ProviderTableMeta.FILE_NAME, file.getFileName());
277 //cv.put(ProviderTableMeta.FILE_PARENT, file.getParentId());
278 cv.put(ProviderTableMeta.FILE_PARENT, folder.getFileId());
279 cv.put(ProviderTableMeta.FILE_PATH, file.getRemotePath());
280 if (!file.isFolder()) {
281 cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getStoragePath());
282 }
283 cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, mAccount.name);
284 cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE, file.getLastSyncDateForProperties());
285 cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA, file.getLastSyncDateForData());
286 cv.put(ProviderTableMeta.FILE_KEEP_IN_SYNC, file.keepInSync() ? 1 : 0);
287 cv.put(ProviderTableMeta.FILE_ETAG, file.getEtag());
288 cv.put(ProviderTableMeta.FILE_SHARE_BY_LINK, file.isShareByLink() ? 1 : 0);
289 cv.put(ProviderTableMeta.FILE_PUBLIC_LINK, file.getPublicLink());
290 cv.put(ProviderTableMeta.FILE_PERMISSIONS, file.getPermissions());
291 cv.put(ProviderTableMeta.FILE_REMOTE_ID, file.getRemoteId());
292
293 boolean existsByPath = fileExists(file.getRemotePath());
294 if (existsByPath || fileExists(file.getFileId())) {
295 // updating an existing file
296 operations.add(ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI).
297 withValues(cv).
298 withSelection( ProviderTableMeta._ID + "=?",
299 new String[] { String.valueOf(file.getFileId()) })
300 .build());
301
302 } else {
303 // adding a new file
304 operations.add(ContentProviderOperation.newInsert(ProviderTableMeta.CONTENT_URI).withValues(cv).build());
305 }
306 }
307
308 // prepare operations to remove files in the given folder
309 String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?" + " AND " + ProviderTableMeta.FILE_PATH + "=?";
310 String [] whereArgs = null;
311 for (OCFile file : filesToRemove) {
312 if (file.getParentId() == folder.getFileId()) {
313 whereArgs = new String[]{mAccount.name, file.getRemotePath()};
314 //Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_FILE, "" + file.getFileId());
315 if (file.isFolder()) {
316 operations.add(ContentProviderOperation
317 .newDelete(ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_DIR, file.getFileId())).withSelection(where, whereArgs)
318 .build());
319 // TODO remove local folder
320 } else {
321 operations.add(ContentProviderOperation
322 .newDelete(ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_FILE, file.getFileId())).withSelection(where, whereArgs)
323 .build());
324 if (file.isDown()) {
325 new File(file.getStoragePath()).delete();
326 // TODO move the deletion of local contents after success of deletions
327 }
328 }
329 }
330 }
331
332 // update metadata of folder
333 ContentValues cv = new ContentValues();
334 cv.put(ProviderTableMeta.FILE_MODIFIED, folder.getModificationTimestamp());
335 cv.put(ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA, folder.getModificationTimestampAtLastSyncForData());
336 cv.put(ProviderTableMeta.FILE_CREATION, folder.getCreationTimestamp());
337 cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, 0); // FileContentProvider calculates the right size
338 cv.put(ProviderTableMeta.FILE_CONTENT_TYPE, folder.getMimetype());
339 cv.put(ProviderTableMeta.FILE_NAME, folder.getFileName());
340 cv.put(ProviderTableMeta.FILE_PARENT, folder.getParentId());
341 cv.put(ProviderTableMeta.FILE_PATH, folder.getRemotePath());
342 cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, mAccount.name);
343 cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE, folder.getLastSyncDateForProperties());
344 cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA, folder.getLastSyncDateForData());
345 cv.put(ProviderTableMeta.FILE_KEEP_IN_SYNC, folder.keepInSync() ? 1 : 0);
346 cv.put(ProviderTableMeta.FILE_ETAG, folder.getEtag());
347 cv.put(ProviderTableMeta.FILE_SHARE_BY_LINK, folder.isShareByLink() ? 1 : 0);
348 cv.put(ProviderTableMeta.FILE_PUBLIC_LINK, folder.getPublicLink());
349 cv.put(ProviderTableMeta.FILE_PERMISSIONS, folder.getPermissions());
350 cv.put(ProviderTableMeta.FILE_REMOTE_ID, folder.getRemoteId());
351
352 operations.add(ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI).
353 withValues(cv).
354 withSelection( ProviderTableMeta._ID + "=?",
355 new String[] { String.valueOf(folder.getFileId()) })
356 .build());
357
358 // apply operations in batch
359 ContentProviderResult[] results = null;
360 Log_OC.d(TAG, "Sending " + operations.size() + " operations to FileContentProvider");
361 try {
362 if (getContentResolver() != null) {
363 results = getContentResolver().applyBatch(MainApp.getAuthority(), operations);
364
365 } else {
366 results = getContentProviderClient().applyBatch(operations);
367 }
368
369 } catch (OperationApplicationException e) {
370 Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage());
371
372 } catch (RemoteException e) {
373 Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage());
374 }
375
376 // update new id in file objects for insertions
377 if (results != null) {
378 long newId;
379 Iterator<OCFile> filesIt = updatedFiles.iterator();
380 OCFile file = null;
381 for (int i=0; i<results.length; i++) {
382 if (filesIt.hasNext()) {
383 file = filesIt.next();
384 } else {
385 file = null;
386 }
387 if (results[i].uri != null) {
388 newId = Long.parseLong(results[i].uri.getPathSegments().get(1));
389 //updatedFiles.get(i).setFileId(newId);
390 if (file != null) {
391 file.setFileId(newId);
392 }
393 }
394 }
395 }
396
397 //updateFolderSize(folder.getFileId());
398
399 }
400
401
402 // /**
403 // *
404 // * @param id
405 // */
406 // private void updateFolderSize(long id) {
407 // if (id > FileDataStorageManager.ROOT_PARENT_ID) {
408 // Log_OC.d(TAG, "Updating size of " + id);
409 // if (getContentResolver() != null) {
410 // getContentResolver().update(ProviderTableMeta.CONTENT_URI_DIR,
411 // new ContentValues(), // won't be used, but cannot be null; crashes in KLP
412 // ProviderTableMeta._ID + "=?",
413 // new String[] { String.valueOf(id) });
414 // } else {
415 // try {
416 // getContentProviderClient().update(ProviderTableMeta.CONTENT_URI_DIR,
417 // new ContentValues(), // won't be used, but cannot be null; crashes in KLP
418 // ProviderTableMeta._ID + "=?",
419 // new String[] { String.valueOf(id) });
420 //
421 // } catch (RemoteException e) {
422 // Log_OC.e(TAG, "Exception in update of folder size through compatibility patch " + e.getMessage());
423 // }
424 // }
425 // } else {
426 // Log_OC.e(TAG, "not updating size for folder " + id);
427 // }
428 // }
429
430
431 public boolean removeFile(OCFile file, boolean removeDBData, boolean removeLocalCopy) {
432 boolean success = true;
433 if (file != null) {
434 if (file.isFolder()) {
435 success = removeFolder(file, removeDBData, removeLocalCopy);
436
437 } else {
438 if (removeDBData) {
439 //Uri file_uri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_FILE, ""+file.getFileId());
440 Uri file_uri = ContentUris.withAppendedId(ProviderTableMeta.CONTENT_URI_FILE, file.getFileId());
441 String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?" + " AND " + ProviderTableMeta.FILE_PATH + "=?";
442 String [] whereArgs = new String[]{mAccount.name, file.getRemotePath()};
443 int deleted = 0;
444 if (getContentProviderClient() != null) {
445 try {
446 deleted = getContentProviderClient().delete(file_uri, where, whereArgs);
447 } catch (RemoteException e) {
448 e.printStackTrace();
449 }
450 } else {
451 deleted = getContentResolver().delete(file_uri, where, whereArgs);
452 }
453 success &= (deleted > 0);
454 }
455 if (removeLocalCopy && file.isDown() && file.getStoragePath() != null && success) {
456 success = new File(file.getStoragePath()).delete();
457 if (!removeDBData && success) {
458 // maybe unnecessary, but should be checked TODO remove if unnecessary
459 file.setStoragePath(null);
460 saveFile(file);
461 }
462 }
463 }
464 }
465 return success;
466 }
467
468
469 public boolean removeFolder(OCFile folder, boolean removeDBData, boolean removeLocalContent) {
470 boolean success = true;
471 if (folder != null && folder.isFolder()) {
472 if (removeDBData && folder.getFileId() != -1) {
473 success = removeFolderInDb(folder);
474 }
475 if (removeLocalContent && success) {
476 success = removeLocalFolder(folder);
477 }
478 }
479 return success;
480 }
481
482 private boolean removeFolderInDb(OCFile folder) {
483 Uri folder_uri = Uri.withAppendedPath(ProviderTableMeta.CONTENT_URI_DIR, ""+ folder.getFileId()); // URI for recursive deletion
484 String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?" + " AND " + ProviderTableMeta.FILE_PATH + "=?";
485 String [] whereArgs = new String[]{mAccount.name, folder.getRemotePath()};
486 int deleted = 0;
487 if (getContentProviderClient() != null) {
488 try {
489 deleted = getContentProviderClient().delete(folder_uri, where, whereArgs);
490 } catch (RemoteException e) {
491 e.printStackTrace();
492 }
493 } else {
494 deleted = getContentResolver().delete(folder_uri, where, whereArgs);
495 }
496 return deleted > 0;
497 }
498
499 private boolean removeLocalFolder(OCFile folder) {
500 boolean success = true;
501 File localFolder = new File(FileStorageUtils.getDefaultSavePathFor(mAccount.name, folder));
502 if (localFolder.exists()) {
503 // stage 1: remove the local files already registered in the files database
504 Vector<OCFile> files = getFolderContent(folder.getFileId());
505 if (files != null) {
506 for (OCFile file : files) {
507 if (file.isFolder()) {
508 success &= removeLocalFolder(file);
509 } else {
510 if (file.isDown()) {
511 File localFile = new File(file.getStoragePath());
512 success &= localFile.delete();
513 if (success) {
514 file.setStoragePath(null);
515 saveFile(file);
516 }
517 }
518 }
519 }
520 }
521
522 // stage 2: remove the folder itself and any local file inside out of sync;
523 // for instance, after clearing the app cache or reinstalling
524 success &= removeLocalFolder(localFolder);
525 }
526 return success;
527 }
528
529 private boolean removeLocalFolder(File localFolder) {
530 boolean success = true;
531 File[] localFiles = localFolder.listFiles();
532 if (localFiles != null) {
533 for (File localFile : localFiles) {
534 if (localFile.isDirectory()) {
535 success &= removeLocalFolder(localFile);
536 } else {
537 success &= localFile.delete();
538 }
539 }
540 }
541 success &= localFolder.delete();
542 return success;
543 }
544
545 /**
546 * Updates database for a folder that was moved to a different location.
547 *
548 * TODO explore better (faster) implementations
549 * TODO throw exceptions up !
550 */
551 public void moveFolder(OCFile folder, String newPath) {
552 // TODO check newPath
553
554 if (folder != null && folder.isFolder() && folder.fileExists() && !OCFile.ROOT_PATH.equals(folder.getFileName())) {
555 /// 1. get all the descendants of 'dir' in a single QUERY (including 'dir')
556 Cursor c = null;
557 if (getContentProviderClient() != null) {
558 try {
559 c = getContentProviderClient().query(ProviderTableMeta.CONTENT_URI,
560 null,
561 ProviderTableMeta.FILE_ACCOUNT_OWNER + "=? AND " + ProviderTableMeta.FILE_PATH + " LIKE ? ",
562 new String[] { mAccount.name, folder.getRemotePath() + "%" }, ProviderTableMeta.FILE_PATH + " ASC ");
563 } catch (RemoteException e) {
564 Log_OC.e(TAG, e.getMessage());
565 }
566 } else {
567 c = getContentResolver().query(ProviderTableMeta.CONTENT_URI,
568 null,
569 ProviderTableMeta.FILE_ACCOUNT_OWNER + "=? AND " + ProviderTableMeta.FILE_PATH + " LIKE ? ",
570 new String[] { mAccount.name, folder.getRemotePath() + "%" }, ProviderTableMeta.FILE_PATH + " ASC ");
571 }
572
573 /// 2. prepare a batch of update operations to change all the descendants
574 ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>(c.getCount());
575 int lengthOfOldPath = folder.getRemotePath().length();
576 String defaultSavePath = FileStorageUtils.getSavePath(mAccount.name);
577 int lengthOfOldStoragePath = defaultSavePath.length() + lengthOfOldPath;
578 if (c.moveToFirst()) {
579 do {
580 ContentValues cv = new ContentValues(); // don't take the constructor out of the loop and clear the object
581 OCFile child = createFileInstance(c);
582 cv.put(ProviderTableMeta.FILE_PATH, newPath + child.getRemotePath().substring(lengthOfOldPath));
583 if (child.getStoragePath() != null && child.getStoragePath().startsWith(defaultSavePath)) {
584 cv.put(ProviderTableMeta.FILE_STORAGE_PATH, defaultSavePath + newPath + child.getStoragePath().substring(lengthOfOldStoragePath));
585 }
586 operations.add(ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI).
587 withValues(cv).
588 withSelection( ProviderTableMeta._ID + "=?",
589 new String[] { String.valueOf(child.getFileId()) })
590 .build());
591 } while (c.moveToNext());
592 }
593 c.close();
594
595 /// 3. apply updates in batch
596 try {
597 if (getContentResolver() != null) {
598 getContentResolver().applyBatch(MainApp.getAuthority(), operations);
599
600 } else {
601 getContentProviderClient().applyBatch(operations);
602 }
603
604 } catch (OperationApplicationException e) {
605 Log_OC.e(TAG, "Fail to update descendants of " + folder.getFileId() + " in database", e);
606
607 } catch (RemoteException e) {
608 Log_OC.e(TAG, "Fail to update desendants of " + folder.getFileId() + " in database", e);
609 }
610
611 }
612 }
613
614
615 private Vector<OCFile> getFolderContent(long parentId) {
616
617 Vector<OCFile> ret = new Vector<OCFile>();
618
619 Uri req_uri = Uri.withAppendedPath(
620 ProviderTableMeta.CONTENT_URI_DIR,
621 String.valueOf(parentId));
622 Cursor c = null;
623
624 if (getContentProviderClient() != null) {
625 try {
626 c = getContentProviderClient().query(req_uri, null,
627 ProviderTableMeta.FILE_PARENT + "=?" ,
628 new String[] { String.valueOf(parentId)}, null);
629 } catch (RemoteException e) {
630 Log_OC.e(TAG, e.getMessage());
631 return ret;
632 }
633 } else {
634 c = getContentResolver().query(req_uri, null,
635 ProviderTableMeta.FILE_PARENT + "=?" ,
636 new String[] { String.valueOf(parentId)}, null);
637 }
638
639 if (c.moveToFirst()) {
640 do {
641 OCFile child = createFileInstance(c);
642 ret.add(child);
643 } while (c.moveToNext());
644 }
645
646 c.close();
647
648 Collections.sort(ret);
649
650 return ret;
651 }
652
653
654 private OCFile createRootDir() {
655 OCFile file = new OCFile(OCFile.ROOT_PATH);
656 file.setMimetype("DIR");
657 file.setParentId(FileDataStorageManager.ROOT_PARENT_ID);
658 saveFile(file);
659 return file;
660 }
661
662 private boolean fileExists(String cmp_key, String value) {
663 Cursor c;
664 if (getContentResolver() != null) {
665 c = getContentResolver()
666 .query(ProviderTableMeta.CONTENT_URI,
667 null,
668 cmp_key + "=? AND "
669 + ProviderTableMeta.FILE_ACCOUNT_OWNER
670 + "=?",
671 new String[] { value, mAccount.name }, null);
672 } else {
673 try {
674 c = getContentProviderClient().query(
675 ProviderTableMeta.CONTENT_URI,
676 null,
677 cmp_key + "=? AND "
678 + ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?",
679 new String[] { value, mAccount.name }, null);
680 } catch (RemoteException e) {
681 Log_OC.e(TAG,
682 "Couldn't determine file existance, assuming non existance: "
683 + e.getMessage());
684 return false;
685 }
686 }
687 boolean retval = c.moveToFirst();
688 c.close();
689 return retval;
690 }
691
692 private Cursor getCursorForValue(String key, String value) {
693 Cursor c = null;
694 if (getContentResolver() != null) {
695 c = getContentResolver()
696 .query(ProviderTableMeta.CONTENT_URI,
697 null,
698 key + "=? AND "
699 + ProviderTableMeta.FILE_ACCOUNT_OWNER
700 + "=?",
701 new String[] { value, mAccount.name }, null);
702 } else {
703 try {
704 c = getContentProviderClient().query(
705 ProviderTableMeta.CONTENT_URI,
706 null,
707 key + "=? AND " + ProviderTableMeta.FILE_ACCOUNT_OWNER
708 + "=?", new String[] { value, mAccount.name },
709 null);
710 } catch (RemoteException e) {
711 Log_OC.e(TAG, "Could not get file details: " + e.getMessage());
712 c = null;
713 }
714 }
715 return c;
716 }
717
718
719 private OCFile createFileInstance(Cursor c) {
720 OCFile file = null;
721 if (c != null) {
722 file = new OCFile(c.getString(c
723 .getColumnIndex(ProviderTableMeta.FILE_PATH)));
724 file.setFileId(c.getLong(c.getColumnIndex(ProviderTableMeta._ID)));
725 file.setParentId(c.getLong(c
726 .getColumnIndex(ProviderTableMeta.FILE_PARENT)));
727 file.setMimetype(c.getString(c
728 .getColumnIndex(ProviderTableMeta.FILE_CONTENT_TYPE)));
729 if (!file.isFolder()) {
730 file.setStoragePath(c.getString(c
731 .getColumnIndex(ProviderTableMeta.FILE_STORAGE_PATH)));
732 if (file.getStoragePath() == null) {
733 // 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
734 File f = new File(FileStorageUtils.getDefaultSavePathFor(mAccount.name, file));
735 if (f.exists()) {
736 file.setStoragePath(f.getAbsolutePath());
737 file.setLastSyncDateForData(f.lastModified());
738 }
739 }
740 }
741 file.setFileLength(c.getLong(c
742 .getColumnIndex(ProviderTableMeta.FILE_CONTENT_LENGTH)));
743 file.setCreationTimestamp(c.getLong(c
744 .getColumnIndex(ProviderTableMeta.FILE_CREATION)));
745 file.setModificationTimestamp(c.getLong(c
746 .getColumnIndex(ProviderTableMeta.FILE_MODIFIED)));
747 file.setModificationTimestampAtLastSyncForData(c.getLong(c
748 .getColumnIndex(ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA)));
749 file.setLastSyncDateForProperties(c.getLong(c
750 .getColumnIndex(ProviderTableMeta.FILE_LAST_SYNC_DATE)));
751 file.setLastSyncDateForData(c.getLong(c.
752 getColumnIndex(ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA)));
753 file.setKeepInSync(c.getInt(
754 c.getColumnIndex(ProviderTableMeta.FILE_KEEP_IN_SYNC)) == 1 ? true : false);
755 file.setEtag(c.getString(c.getColumnIndex(ProviderTableMeta.FILE_ETAG)));
756 file.setShareByLink(c.getInt(
757 c.getColumnIndex(ProviderTableMeta.FILE_SHARE_BY_LINK)) == 1 ? true : false);
758 file.setPublicLink(c.getString(c.getColumnIndex(ProviderTableMeta.FILE_PUBLIC_LINK)));
759 file.setPermissions(c.getString(c.getColumnIndex(ProviderTableMeta.FILE_PERMISSIONS)));
760 file.setRemoteId(c.getString(c.getColumnIndex(ProviderTableMeta.FILE_REMOTE_ID)));
761
762 }
763 return file;
764 }
765
766 /**
767 * Returns if the file/folder is shared by link or not
768 * @param path Path of the file/folder
769 * @return
770 */
771 public boolean isShareByLink(String path) {
772 Cursor c = getCursorForValue(ProviderTableMeta.FILE_STORAGE_PATH, path);
773 OCFile file = null;
774 if (c.moveToFirst()) {
775 file = createFileInstance(c);
776 }
777 c.close();
778 return file.isShareByLink();
779 }
780
781 /**
782 * Returns the public link of the file/folder
783 * @param path Path of the file/folder
784 * @return
785 */
786 public String getPublicLink(String path) {
787 Cursor c = getCursorForValue(ProviderTableMeta.FILE_STORAGE_PATH, path);
788 OCFile file = null;
789 if (c.moveToFirst()) {
790 file = createFileInstance(c);
791 }
792 c.close();
793 return file.getPublicLink();
794 }
795
796
797 // Methods for Shares
798 public boolean saveShare(OCShare share) {
799 boolean overriden = false;
800 ContentValues cv = new ContentValues();
801 cv.put(ProviderTableMeta.OCSHARES_FILE_SOURCE, share.getFileSource());
802 cv.put(ProviderTableMeta.OCSHARES_ITEM_SOURCE, share.getItemSource());
803 cv.put(ProviderTableMeta.OCSHARES_SHARE_TYPE, share.getShareType().getValue());
804 cv.put(ProviderTableMeta.OCSHARES_SHARE_WITH, share.getShareWith());
805 cv.put(ProviderTableMeta.OCSHARES_PATH, share.getPath());
806 cv.put(ProviderTableMeta.OCSHARES_PERMISSIONS, share.getPermissions());
807 cv.put(ProviderTableMeta.OCSHARES_SHARED_DATE, share.getSharedDate());
808 cv.put(ProviderTableMeta.OCSHARES_EXPIRATION_DATE, share.getExpirationDate());
809 cv.put(ProviderTableMeta.OCSHARES_TOKEN, share.getToken());
810 cv.put(ProviderTableMeta.OCSHARES_SHARE_WITH_DISPLAY_NAME, share.getSharedWithDisplayName());
811 cv.put(ProviderTableMeta.OCSHARES_IS_DIRECTORY, share.isFolder() ? 1 : 0);
812 cv.put(ProviderTableMeta.OCSHARES_USER_ID, share.getUserId());
813 cv.put(ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED, share.getIdRemoteShared());
814 cv.put(ProviderTableMeta.OCSHARES_ACCOUNT_OWNER, mAccount.name);
815
816 if (shareExists(share.getIdRemoteShared())) { // for renamed files; no more delete and create
817
818 overriden = true;
819 if (getContentResolver() != null) {
820 getContentResolver().update(ProviderTableMeta.CONTENT_URI_SHARE, cv,
821 ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED + "=?",
822 new String[] { String.valueOf(share.getIdRemoteShared()) });
823 } else {
824 try {
825 getContentProviderClient().update(ProviderTableMeta.CONTENT_URI_SHARE,
826 cv, ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED + "=?",
827 new String[] { String.valueOf(share.getIdRemoteShared()) });
828 } catch (RemoteException e) {
829 Log_OC.e(TAG,
830 "Fail to insert insert file to database "
831 + e.getMessage());
832 }
833 }
834 } else {
835 Uri result_uri = null;
836 if (getContentResolver() != null) {
837 result_uri = getContentResolver().insert(
838 ProviderTableMeta.CONTENT_URI_SHARE, cv);
839 } else {
840 try {
841 result_uri = getContentProviderClient().insert(
842 ProviderTableMeta.CONTENT_URI_SHARE, cv);
843 } catch (RemoteException e) {
844 Log_OC.e(TAG,
845 "Fail to insert insert file to database "
846 + e.getMessage());
847 }
848 }
849 if (result_uri != null) {
850 long new_id = Long.parseLong(result_uri.getPathSegments()
851 .get(1));
852 share.setId(new_id);
853 }
854 }
855
856 return overriden;
857 }
858
859
860 public OCShare getFirstShareByPathAndType(String path, ShareType type) {
861 Cursor c = null;
862 if (getContentResolver() != null) {
863 c = getContentResolver().query(
864 ProviderTableMeta.CONTENT_URI_SHARE,
865 null,
866 ProviderTableMeta.OCSHARES_PATH + "=? AND "
867 + ProviderTableMeta.OCSHARES_SHARE_TYPE + "=? AND "
868 + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + "=?",
869 new String[] { path, Integer.toString(type.getValue()), mAccount.name },
870 null);
871 } else {
872 try {
873 c = getContentProviderClient().query(
874 ProviderTableMeta.CONTENT_URI_SHARE,
875 null,
876 ProviderTableMeta.OCSHARES_PATH + "=? AND "
877 + ProviderTableMeta.OCSHARES_SHARE_TYPE + "=? AND "
878 + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + "=?",
879 new String[] { path, Integer.toString(type.getValue()), mAccount.name },
880 null);
881
882 } catch (RemoteException e) {
883 Log_OC.e(TAG, "Could not get file details: " + e.getMessage());
884 c = null;
885 }
886 }
887 OCShare share = null;
888 if (c.moveToFirst()) {
889 share = createShareInstance(c);
890 }
891 c.close();
892 return share;
893 }
894
895 private OCShare createShareInstance(Cursor c) {
896 OCShare share = null;
897 if (c != null) {
898 share = new OCShare(c.getString(c
899 .getColumnIndex(ProviderTableMeta.OCSHARES_PATH)));
900 share.setId(c.getLong(c.getColumnIndex(ProviderTableMeta._ID)));
901 share.setFileSource(c.getLong(c
902 .getColumnIndex(ProviderTableMeta.OCSHARES_ITEM_SOURCE)));
903 share.setShareType(ShareType.fromValue(c.getInt(c
904 .getColumnIndex(ProviderTableMeta.OCSHARES_SHARE_TYPE))));
905 share.setPermissions(c.getInt(c
906 .getColumnIndex(ProviderTableMeta.OCSHARES_PERMISSIONS)));
907 share.setSharedDate(c.getLong(c
908 .getColumnIndex(ProviderTableMeta.OCSHARES_SHARED_DATE)));
909 share.setExpirationDate(c.getLong(c
910 .getColumnIndex(ProviderTableMeta.OCSHARES_EXPIRATION_DATE)));
911 share.setToken(c.getString(c
912 .getColumnIndex(ProviderTableMeta.OCSHARES_TOKEN)));
913 share.setSharedWithDisplayName(c.getString(c
914 .getColumnIndex(ProviderTableMeta.OCSHARES_SHARE_WITH_DISPLAY_NAME)));
915 share.setIsFolder(c.getInt(
916 c.getColumnIndex(ProviderTableMeta.OCSHARES_IS_DIRECTORY)) == 1 ? true : false);
917 share.setUserId(c.getLong(c.getColumnIndex(ProviderTableMeta.OCSHARES_USER_ID)));
918 share.setIdRemoteShared(c.getLong(c.getColumnIndex(ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED)));
919
920 }
921 return share;
922 }
923
924 private boolean shareExists(String cmp_key, String value) {
925 Cursor c;
926 if (getContentResolver() != null) {
927 c = getContentResolver()
928 .query(ProviderTableMeta.CONTENT_URI_SHARE,
929 null,
930 cmp_key + "=? AND "
931 + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER
932 + "=?",
933 new String[] { value, mAccount.name }, null);
934 } else {
935 try {
936 c = getContentProviderClient().query(
937 ProviderTableMeta.CONTENT_URI_SHARE,
938 null,
939 cmp_key + "=? AND "
940 + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + "=?",
941 new String[] { value, mAccount.name }, null);
942 } catch (RemoteException e) {
943 Log_OC.e(TAG,
944 "Couldn't determine file existance, assuming non existance: "
945 + e.getMessage());
946 return false;
947 }
948 }
949 boolean retval = c.moveToFirst();
950 c.close();
951 return retval;
952 }
953
954 private boolean shareExists(long remoteId) {
955 return shareExists(ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED, String.valueOf(remoteId));
956 }
957
958 private void cleanSharedFiles() {
959 ContentValues cv = new ContentValues();
960 cv.put(ProviderTableMeta.FILE_SHARE_BY_LINK, false);
961 cv.put(ProviderTableMeta.FILE_PUBLIC_LINK, "");
962 String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + "=?";
963 String [] whereArgs = new String[]{mAccount.name};
964
965 if (getContentResolver() != null) {
966 getContentResolver().update(ProviderTableMeta.CONTENT_URI, cv, where, whereArgs);
967
968 } else {
969 try {
970 getContentProviderClient().update(ProviderTableMeta.CONTENT_URI, cv, where, whereArgs);
971
972 } catch (RemoteException e) {
973 Log_OC.e(TAG, "Exception in cleanSharedFiles" + e.getMessage());
974 }
975 }
976 }
977
978 private void cleanSharedFilesInFolder(OCFile folder) {
979 ContentValues cv = new ContentValues();
980 cv.put(ProviderTableMeta.FILE_SHARE_BY_LINK, false);
981 cv.put(ProviderTableMeta.FILE_PUBLIC_LINK, "");
982 String where = ProviderTableMeta.FILE_ACCOUNT_OWNER + "=? AND " + ProviderTableMeta.FILE_PARENT + "=?";
983 String [] whereArgs = new String[] { mAccount.name , String.valueOf(folder.getFileId()) };
984
985 if (getContentResolver() != null) {
986 getContentResolver().update(ProviderTableMeta.CONTENT_URI, cv, where, whereArgs);
987
988 } else {
989 try {
990 getContentProviderClient().update(ProviderTableMeta.CONTENT_URI, cv, where, whereArgs);
991
992 } catch (RemoteException e) {
993 Log_OC.e(TAG, "Exception in cleanSharedFilesInFolder " + e.getMessage());
994 }
995 }
996 }
997
998 private void cleanShares() {
999 String where = ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + "=?";
1000 String [] whereArgs = new String[]{mAccount.name};
1001
1002 if (getContentResolver() != null) {
1003 getContentResolver().delete(ProviderTableMeta.CONTENT_URI_SHARE, where, whereArgs);
1004
1005 } else {
1006 try {
1007 getContentProviderClient().delete(ProviderTableMeta.CONTENT_URI_SHARE, where, whereArgs);
1008
1009 } catch (RemoteException e) {
1010 Log_OC.e(TAG, "Exception in cleanShares" + e.getMessage());
1011 }
1012 }
1013 }
1014
1015 public void saveShares(Collection<OCShare> shares) {
1016 cleanShares();
1017 if (shares != null) {
1018 ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>(shares.size());
1019
1020 // prepare operations to insert or update files to save in the given folder
1021 for (OCShare share : shares) {
1022 ContentValues cv = new ContentValues();
1023 cv.put(ProviderTableMeta.OCSHARES_FILE_SOURCE, share.getFileSource());
1024 cv.put(ProviderTableMeta.OCSHARES_ITEM_SOURCE, share.getItemSource());
1025 cv.put(ProviderTableMeta.OCSHARES_SHARE_TYPE, share.getShareType().getValue());
1026 cv.put(ProviderTableMeta.OCSHARES_SHARE_WITH, share.getShareWith());
1027 cv.put(ProviderTableMeta.OCSHARES_PATH, share.getPath());
1028 cv.put(ProviderTableMeta.OCSHARES_PERMISSIONS, share.getPermissions());
1029 cv.put(ProviderTableMeta.OCSHARES_SHARED_DATE, share.getSharedDate());
1030 cv.put(ProviderTableMeta.OCSHARES_EXPIRATION_DATE, share.getExpirationDate());
1031 cv.put(ProviderTableMeta.OCSHARES_TOKEN, share.getToken());
1032 cv.put(ProviderTableMeta.OCSHARES_SHARE_WITH_DISPLAY_NAME, share.getSharedWithDisplayName());
1033 cv.put(ProviderTableMeta.OCSHARES_IS_DIRECTORY, share.isFolder() ? 1 : 0);
1034 cv.put(ProviderTableMeta.OCSHARES_USER_ID, share.getUserId());
1035 cv.put(ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED, share.getIdRemoteShared());
1036 cv.put(ProviderTableMeta.OCSHARES_ACCOUNT_OWNER, mAccount.name);
1037
1038 if (shareExists(share.getIdRemoteShared())) {
1039 // updating an existing file
1040 operations.add(ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI_SHARE).
1041 withValues(cv).
1042 withSelection( ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED + "=?",
1043 new String[] { String.valueOf(share.getIdRemoteShared()) })
1044 .build());
1045
1046 } else {
1047 // adding a new file
1048 operations.add(ContentProviderOperation.newInsert(ProviderTableMeta.CONTENT_URI_SHARE).withValues(cv).build());
1049 }
1050 }
1051
1052 // apply operations in batch
1053 if (operations.size() > 0) {
1054 @SuppressWarnings("unused")
1055 ContentProviderResult[] results = null;
1056 Log_OC.d(TAG, "Sending " + operations.size() + " operations to FileContentProvider");
1057 try {
1058 if (getContentResolver() != null) {
1059 results = getContentResolver().applyBatch(MainApp.getAuthority(), operations);
1060
1061 } else {
1062 results = getContentProviderClient().applyBatch(operations);
1063 }
1064
1065 } catch (OperationApplicationException e) {
1066 Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage());
1067
1068 } catch (RemoteException e) {
1069 Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage());
1070 }
1071 }
1072 }
1073
1074 }
1075
1076 public void updateSharedFiles(Collection<OCFile> sharedFiles) {
1077 cleanSharedFiles();
1078
1079 if (sharedFiles != null) {
1080 ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>(sharedFiles.size());
1081
1082 // prepare operations to insert or update files to save in the given folder
1083 for (OCFile file : sharedFiles) {
1084 ContentValues cv = new ContentValues();
1085 cv.put(ProviderTableMeta.FILE_MODIFIED, file.getModificationTimestamp());
1086 cv.put(ProviderTableMeta.FILE_MODIFIED_AT_LAST_SYNC_FOR_DATA, file.getModificationTimestampAtLastSyncForData());
1087 cv.put(ProviderTableMeta.FILE_CREATION, file.getCreationTimestamp());
1088 cv.put(ProviderTableMeta.FILE_CONTENT_LENGTH, file.getFileLength());
1089 cv.put(ProviderTableMeta.FILE_CONTENT_TYPE, file.getMimetype());
1090 cv.put(ProviderTableMeta.FILE_NAME, file.getFileName());
1091 cv.put(ProviderTableMeta.FILE_PARENT, file.getParentId());
1092 cv.put(ProviderTableMeta.FILE_PATH, file.getRemotePath());
1093 if (!file.isFolder()) {
1094 cv.put(ProviderTableMeta.FILE_STORAGE_PATH, file.getStoragePath());
1095 }
1096 cv.put(ProviderTableMeta.FILE_ACCOUNT_OWNER, mAccount.name);
1097 cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE, file.getLastSyncDateForProperties());
1098 cv.put(ProviderTableMeta.FILE_LAST_SYNC_DATE_FOR_DATA, file.getLastSyncDateForData());
1099 cv.put(ProviderTableMeta.FILE_KEEP_IN_SYNC, file.keepInSync() ? 1 : 0);
1100 cv.put(ProviderTableMeta.FILE_ETAG, file.getEtag());
1101 cv.put(ProviderTableMeta.FILE_SHARE_BY_LINK, file.isShareByLink() ? 1 : 0);
1102 cv.put(ProviderTableMeta.FILE_PUBLIC_LINK, file.getPublicLink());
1103 cv.put(ProviderTableMeta.FILE_PERMISSIONS, file.getPermissions());
1104 cv.put(ProviderTableMeta.FILE_REMOTE_ID, file.getRemoteId());
1105
1106 boolean existsByPath = fileExists(file.getRemotePath());
1107 if (existsByPath || fileExists(file.getFileId())) {
1108 // updating an existing file
1109 operations.add(ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI).
1110 withValues(cv).
1111 withSelection( ProviderTableMeta._ID + "=?",
1112 new String[] { String.valueOf(file.getFileId()) })
1113 .build());
1114
1115 } else {
1116 // adding a new file
1117 operations.add(ContentProviderOperation.newInsert(ProviderTableMeta.CONTENT_URI).withValues(cv).build());
1118 }
1119 }
1120
1121 // apply operations in batch
1122 if (operations.size() > 0) {
1123 @SuppressWarnings("unused")
1124 ContentProviderResult[] results = null;
1125 Log_OC.d(TAG, "Sending " + operations.size() + " operations to FileContentProvider");
1126 try {
1127 if (getContentResolver() != null) {
1128 results = getContentResolver().applyBatch(MainApp.getAuthority(), operations);
1129
1130 } else {
1131 results = getContentProviderClient().applyBatch(operations);
1132 }
1133
1134 } catch (OperationApplicationException e) {
1135 Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage());
1136
1137 } catch (RemoteException e) {
1138 Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage());
1139 }
1140 }
1141 }
1142
1143 }
1144
1145 public void removeShare(OCShare share){
1146 Uri share_uri = ProviderTableMeta.CONTENT_URI_SHARE;
1147 String where = ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + "=?" + " AND " + ProviderTableMeta.FILE_PATH + "=?";
1148 String [] whereArgs = new String[]{mAccount.name, share.getPath()};
1149 if (getContentProviderClient() != null) {
1150 try {
1151 getContentProviderClient().delete(share_uri, where, whereArgs);
1152 } catch (RemoteException e) {
1153 e.printStackTrace();
1154 }
1155 } else {
1156 getContentResolver().delete(share_uri, where, whereArgs);
1157 }
1158 }
1159
1160 public void saveSharesDB(ArrayList<OCShare> shares) {
1161 saveShares(shares);
1162
1163 ArrayList<OCFile> sharedFiles = new ArrayList<OCFile>();
1164
1165 for (OCShare share : shares) {
1166 // Get the path
1167 String path = share.getPath();
1168 if (share.isFolder()) {
1169 path = path + FileUtils.PATH_SEPARATOR;
1170 }
1171
1172 // Update OCFile with data from share: ShareByLink ¿and publicLink?
1173 OCFile file = getFileByPath(path);
1174 if (file != null) {
1175 if (share.getShareType().equals(ShareType.PUBLIC_LINK)) {
1176 file.setShareByLink(true);
1177 sharedFiles.add(file);
1178 }
1179 }
1180 }
1181
1182 updateSharedFiles(sharedFiles);
1183 }
1184
1185
1186 public void saveSharesInFolder(ArrayList<OCShare> shares, OCFile folder) {
1187 cleanSharedFilesInFolder(folder);
1188 ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>();
1189 operations = prepareRemoveSharesInFolder(folder, operations);
1190
1191 if (shares != null) {
1192 // prepare operations to insert or update files to save in the given folder
1193 for (OCShare share : shares) {
1194 ContentValues cv = new ContentValues();
1195 cv.put(ProviderTableMeta.OCSHARES_FILE_SOURCE, share.getFileSource());
1196 cv.put(ProviderTableMeta.OCSHARES_ITEM_SOURCE, share.getItemSource());
1197 cv.put(ProviderTableMeta.OCSHARES_SHARE_TYPE, share.getShareType().getValue());
1198 cv.put(ProviderTableMeta.OCSHARES_SHARE_WITH, share.getShareWith());
1199 cv.put(ProviderTableMeta.OCSHARES_PATH, share.getPath());
1200 cv.put(ProviderTableMeta.OCSHARES_PERMISSIONS, share.getPermissions());
1201 cv.put(ProviderTableMeta.OCSHARES_SHARED_DATE, share.getSharedDate());
1202 cv.put(ProviderTableMeta.OCSHARES_EXPIRATION_DATE, share.getExpirationDate());
1203 cv.put(ProviderTableMeta.OCSHARES_TOKEN, share.getToken());
1204 cv.put(ProviderTableMeta.OCSHARES_SHARE_WITH_DISPLAY_NAME, share.getSharedWithDisplayName());
1205 cv.put(ProviderTableMeta.OCSHARES_IS_DIRECTORY, share.isFolder() ? 1 : 0);
1206 cv.put(ProviderTableMeta.OCSHARES_USER_ID, share.getUserId());
1207 cv.put(ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED, share.getIdRemoteShared());
1208 cv.put(ProviderTableMeta.OCSHARES_ACCOUNT_OWNER, mAccount.name);
1209
1210 /*
1211 if (shareExists(share.getIdRemoteShared())) {
1212 // updating an existing share resource
1213 operations.add(ContentProviderOperation.newUpdate(ProviderTableMeta.CONTENT_URI_SHARE).
1214 withValues(cv).
1215 withSelection( ProviderTableMeta.OCSHARES_ID_REMOTE_SHARED + "=?",
1216 new String[] { String.valueOf(share.getIdRemoteShared()) })
1217 .build());
1218
1219 } else {
1220 */
1221 // adding a new share resource
1222 operations.add(ContentProviderOperation.newInsert(ProviderTableMeta.CONTENT_URI_SHARE).withValues(cv).build());
1223 //}
1224 }
1225 }
1226
1227 // apply operations in batch
1228 if (operations.size() > 0) {
1229 @SuppressWarnings("unused")
1230 ContentProviderResult[] results = null;
1231 Log_OC.d(TAG, "Sending " + operations.size() + " operations to FileContentProvider");
1232 try {
1233 if (getContentResolver() != null) {
1234 results = getContentResolver().applyBatch(MainApp.getAuthority(), operations);
1235
1236 } else {
1237 results = getContentProviderClient().applyBatch(operations);
1238 }
1239
1240 } catch (OperationApplicationException e) {
1241 Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage());
1242
1243 } catch (RemoteException e) {
1244 Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage());
1245 }
1246 }
1247 //}
1248
1249 }
1250
1251 private ArrayList<ContentProviderOperation> prepareRemoveSharesInFolder(OCFile folder, ArrayList<ContentProviderOperation> preparedOperations) {
1252 if (folder != null) {
1253 String where = ProviderTableMeta.OCSHARES_PATH + "=?" + " AND " + ProviderTableMeta.OCSHARES_ACCOUNT_OWNER + "=?";
1254 String [] whereArgs = new String[]{ "", mAccount.name };
1255
1256 Vector<OCFile> files = getFolderContent(folder);
1257
1258 for (OCFile file : files) {
1259 whereArgs[0] = file.getRemotePath();
1260 preparedOperations.add(ContentProviderOperation.newDelete(ProviderTableMeta.CONTENT_URI_SHARE)
1261 .withSelection(where, whereArgs)
1262 .build());
1263 }
1264 }
1265 return preparedOperations;
1266
1267 /*
1268 if (operations.size() > 0) {
1269 try {
1270 if (getContentResolver() != null) {
1271 getContentResolver().applyBatch(MainApp.getAuthority(), operations);
1272
1273 } else {
1274 getContentProviderClient().applyBatch(operations);
1275 }
1276
1277 } catch (OperationApplicationException e) {
1278 Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage());
1279
1280 } catch (RemoteException e) {
1281 Log_OC.e(TAG, "Exception in batch of operations " + e.getMessage());
1282 }
1283 }
1284 */
1285
1286 /*
1287 if (getContentResolver() != null) {
1288
1289 getContentResolver().delete(ProviderTableMeta.CONTENT_URI_SHARE,
1290 where,
1291 whereArgs);
1292 } else {
1293 try {
1294 getContentProviderClient().delete( ProviderTableMeta.CONTENT_URI_SHARE,
1295 where,
1296 whereArgs);
1297
1298 } catch (RemoteException e) {
1299 Log_OC.e(TAG, "Exception deleting shares in a folder " + e.getMessage());
1300 }
1301 }
1302 */
1303 //}
1304 }
1305 }