- public void moveFolder(OCFile folder, String newPath) {
- // TODO check newPath
-
- if ( folder != null && folder.isFolder() &&
- folder.fileExists() && !OCFile.ROOT_PATH.equals(folder.getFileName())
- ) {
- /// 1. get all the descendants of 'dir' in a single QUERY (including 'dir')
- Cursor c = null;
- if (getContentProviderClient() != null) {
- try {
- c = getContentProviderClient().query (
- ProviderTableMeta.CONTENT_URI,
- null,
- ProviderTableMeta.FILE_ACCOUNT_OWNER + "=? AND " +
- ProviderTableMeta.FILE_PATH + " LIKE ? ",
- new String[] { mAccount.name, folder.getRemotePath() + "%" },
- ProviderTableMeta.FILE_PATH + " ASC "
- );
- } catch (RemoteException e) {
- Log_OC.e(TAG, e.getMessage());
- }
- } else {
- c = getContentResolver().query (
- ProviderTableMeta.CONTENT_URI,
- null,
- ProviderTableMeta.FILE_ACCOUNT_OWNER + "=? AND " +
- ProviderTableMeta.FILE_PATH + " LIKE ? ",
- new String[] { mAccount.name, folder.getRemotePath() + "%" },
- ProviderTableMeta.FILE_PATH + " ASC "
- );
- }
-
- /// 2. prepare a batch of update operations to change all the descendants
- ArrayList<ContentProviderOperation> operations =
- new ArrayList<ContentProviderOperation>(c.getCount());
- int lengthOfOldPath = folder.getRemotePath().length();
- String defaultSavePath = FileStorageUtils.getSavePath(mAccount.name);
- int lengthOfOldStoragePath = defaultSavePath.length() + lengthOfOldPath;
- if (c.moveToFirst()) {
- do {
- ContentValues cv = new ContentValues(); // keep the constructor in the loop
- OCFile child = createFileInstance(c);
- cv.put(
- ProviderTableMeta.FILE_PATH,
- newPath + child.getRemotePath().substring(lengthOfOldPath)
- );
- if ( child.getStoragePath() != null &&
- child.getStoragePath().startsWith(defaultSavePath) ) {
- cv.put(
- ProviderTableMeta.FILE_STORAGE_PATH,
- defaultSavePath + newPath +
- child.getStoragePath().substring(lengthOfOldStoragePath)
- );
- }
- operations.add(
- ContentProviderOperation.
- newUpdate(ProviderTableMeta.CONTENT_URI).
- withValues(cv).
- withSelection(
- ProviderTableMeta._ID + "=?",
- new String[] { String.valueOf(child.getFileId()) }
- ).
- build()
- );
- } while (c.moveToNext());
- }
- c.close();
-
- /// 3. apply updates in batch
- try {
- if (getContentResolver() != null) {
- getContentResolver().applyBatch(MainApp.getAuthority(), operations);
-
- } else {
- getContentProviderClient().applyBatch(operations);
- }
-
- } catch (OperationApplicationException e) {
- Log_OC.e(TAG, "Fail to update descendants of " +
- folder.getFileId() + " in database", e);
-
- } catch (RemoteException e) {
- Log_OC.e(TAG, "Fail to update desendants of " +
- folder.getFileId() + " in database", e);
- }
-
- }
- }
-
-