Search in sources :

Example 21 with RemoteOperationResult

use of com.owncloud.android.lib.common.operations.RemoteOperationResult in project android by owncloud.

the class UploadFileOperation method saveUploadedFile.

/**
     * Saves a OC File after a successful upload.
     * <p/>
     * A PROPFIND is necessary to keep the props in the local database
     * synchronized with the server, specially the modification time and Etag
     * (where available)
     * <p/>
     */
private void saveUploadedFile(OwnCloudClient client) {
    OCFile file = mFile;
    if (file.fileExists()) {
        file = getStorageManager().getFileById(file.getFileId());
    }
    long syncDate = System.currentTimeMillis();
    file.setLastSyncDateForData(syncDate);
    // new PROPFIND to keep data consistent with server
    // in theory, should return the same we already have
    // TODO from the appropriate OC server version, get data from last PUT response headers, instead
    // TODO     of a new PROPFIND; the latter may fail, specially for chunked uploads
    ReadRemoteFileOperation operation = new ReadRemoteFileOperation(getRemotePath());
    RemoteOperationResult result = operation.execute(client);
    if (result.isSuccess()) {
        updateOCFile(file, (RemoteFile) result.getData().get(0));
        file.setLastSyncDateForProperties(syncDate);
    } else {
        Log_OC.e(TAG, "Error reading properties of file after successful upload; this is gonna hurt...");
    }
    if (mWasRenamed) {
        OCFile oldFile = getStorageManager().getFileByPath(mOldFile.getRemotePath());
        if (oldFile != null) {
            oldFile.setStoragePath(null);
            getStorageManager().saveFile(oldFile);
            getStorageManager().saveConflict(oldFile, null);
        }
    // else: it was just an automatic renaming due to a name
    // coincidence; nothing else is needed, the storagePath is right
    // in the instance returned by mCurrentUpload.getFile()
    }
    file.setNeedsUpdateThumbnail(true);
    getStorageManager().saveFile(file);
    getStorageManager().saveConflict(file, null);
    FileDataStorageManager.triggerMediaScan(file.getStoragePath());
}
Also used : OCFile(com.owncloud.android.datamodel.OCFile) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) ReadRemoteFileOperation(com.owncloud.android.lib.resources.files.ReadRemoteFileOperation)

Example 22 with RemoteOperationResult

use of com.owncloud.android.lib.common.operations.RemoteOperationResult in project android by owncloud.

the class UploadFileOperation method grantFolderExistence.

/**
     * Checks the existence of the folder where the current file will be uploaded both
     * in the remote server and in the local database.
     * <p/>
     * If the upload is set to enforce the creation of the folder, the method tries to
     * create it both remote and locally.
     *
     * @param pathToGrant Full remote path whose existence will be granted.
     * @return An {@link OCFile} instance corresponding to the folder where the file
     * will be uploaded.
     */
private RemoteOperationResult grantFolderExistence(String pathToGrant, OwnCloudClient client) {
    RemoteOperation operation = new ExistenceCheckRemoteOperation(pathToGrant, mContext, false);
    RemoteOperationResult result = operation.execute(client);
    if (!result.isSuccess() && result.getCode() == ResultCode.FILE_NOT_FOUND && mRemoteFolderToBeCreated) {
        SyncOperation syncOp = new CreateFolderOperation(pathToGrant, true);
        result = syncOp.execute(client, getStorageManager());
    }
    if (result.isSuccess()) {
        OCFile parentDir = getStorageManager().getFileByPath(pathToGrant);
        if (parentDir == null) {
            parentDir = createLocalFolder(pathToGrant);
        }
        if (parentDir != null) {
            result = new RemoteOperationResult(ResultCode.OK);
        } else {
            result = new RemoteOperationResult(ResultCode.UNKNOWN_ERROR);
        }
    }
    return result;
}
Also used : OCFile(com.owncloud.android.datamodel.OCFile) ExistenceCheckRemoteOperation(com.owncloud.android.lib.resources.files.ExistenceCheckRemoteOperation) RemoteOperation(com.owncloud.android.lib.common.operations.RemoteOperation) ExistenceCheckRemoteOperation(com.owncloud.android.lib.resources.files.ExistenceCheckRemoteOperation) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) SyncOperation(com.owncloud.android.operations.common.SyncOperation)

Example 23 with RemoteOperationResult

use of com.owncloud.android.lib.common.operations.RemoteOperationResult in project android by owncloud.

the class SyncFolderHandler method doOperation.

/**
     * Performs the next operation in the queue
     */
private void doOperation(Account account, String remotePath) {
    mCurrentSyncOperation = mPendingOperations.get(account.name, remotePath);
    if (mCurrentSyncOperation != null) {
        RemoteOperationResult result = null;
        try {
            if (mCurrentAccount == null || !mCurrentAccount.equals(account)) {
                mCurrentAccount = account;
                mStorageManager = new FileDataStorageManager(account, mService.getContentResolver());
            }
            // else, reuse storage manager from previous operation
            // always get client from client manager, to get fresh credentials in case of update
            OwnCloudAccount ocAccount = new OwnCloudAccount(account, mService);
            mOwnCloudClient = OwnCloudClientManagerFactory.getDefaultSingleton().getClientFor(ocAccount, mService);
            result = mCurrentSyncOperation.execute(mOwnCloudClient, mStorageManager);
        } catch (AccountsException e) {
            Log_OC.e(TAG, "Error while trying to get authorization", e);
        } catch (IOException e) {
            Log_OC.e(TAG, "Error while trying to get authorization", e);
        } finally {
            mPendingOperations.removePayload(account.name, remotePath);
            mService.dispatchResultToOperationListeners(mCurrentSyncOperation, result);
            sendBroadcastFinishedSyncFolder(account, remotePath, result.isSuccess());
        }
    }
}
Also used : RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) FileDataStorageManager(com.owncloud.android.datamodel.FileDataStorageManager) OwnCloudAccount(com.owncloud.android.lib.common.OwnCloudAccount) IOException(java.io.IOException) AccountsException(android.accounts.AccountsException)

Example 24 with RemoteOperationResult

use of com.owncloud.android.lib.common.operations.RemoteOperationResult in project android by owncloud.

the class AvailableOfflineObserver method startSyncOperation.

/**
     * Triggers an operation to synchronize the contents of a file inside the observed folder with
     * its remote counterpart in the associated ownCloud account.
     *    
     * @param fileName          Name of a file inside the watched folder.
     */
private void startSyncOperation(String fileName) {
    FileDataStorageManager storageManager = new FileDataStorageManager(mAccount, mContext.getContentResolver());
    // a fresh object is needed; many things could have occurred to the file
    // since it was registered to observe again, assuming that local files
    // are linked to a remote file AT MOST, SOMETHING TO BE DONE;
    OCFile file = storageManager.getFileByLocalPath(mPath + File.separator + fileName);
    if (file == null) {
        Log_OC.w(TAG, "Could not find OC file for observed " + mPath + File.separator + fileName);
    } else {
        SynchronizeFileOperation sfo = new SynchronizeFileOperation(file, null, mAccount, false, mContext);
        RemoteOperationResult result = sfo.execute(storageManager, mContext);
        if (result.getCode() == ResultCode.SYNC_CONFLICT) {
            // ISSUE 5: if the user is not running the app (this is a service!),
            // this can be very intrusive; a notification should be preferred
            Intent i = new Intent(mContext, ConflictsResolveActivity.class);
            i.setFlags(i.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK);
            i.putExtra(ConflictsResolveActivity.EXTRA_FILE, file);
            i.putExtra(ConflictsResolveActivity.EXTRA_ACCOUNT, mAccount);
            mContext.startActivity(i);
        }
    }
}
Also used : OCFile(com.owncloud.android.datamodel.OCFile) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) FileDataStorageManager(com.owncloud.android.datamodel.FileDataStorageManager) Intent(android.content.Intent) SynchronizeFileOperation(com.owncloud.android.operations.SynchronizeFileOperation)

Example 25 with RemoteOperationResult

use of com.owncloud.android.lib.common.operations.RemoteOperationResult in project android by owncloud.

the class FileSyncAdapter method synchronizeFolder.

/**
     *  Synchronizes the list of files contained in a folder identified with its remote path.
     *  
     *  Fetches the list and properties of the files contained in the given folder, including their 
     *  properties, and updates the local database with them.
     *  
     *  Enters in the child folders to synchronize their contents also, following a recursive
     *  depth first strategy. 
     * 
     *  @param folder                   Folder to synchronize.
     *  @param pushOnly                 When 'true', it's assumed that the folder did not change in the
     *                                  server, so data will not be fetched. Only local changes of
     *                                  available offline files will be pushed.
     */
private void synchronizeFolder(OCFile folder, boolean pushOnly) {
    if (mFailedResultsCounter > MAX_FAILED_RESULTS || isFinisher(mLastFailedResult))
        return;
    // folder synchronization
    SynchronizeFolderOperation synchFolderOp = new SynchronizeFolderOperation(getContext(), folder.getRemotePath(), getAccount(), mCurrentSyncTime, pushOnly, // sync full account
    true, // only sync contents of available offline files
    false);
    RemoteOperationResult result = synchFolderOp.execute(getClient(), getStorageManager());
    // synchronized folder -> notice to UI - ALWAYS, although !result.isSuccess
    sendLocalBroadcast(EVENT_FULL_SYNC_FOLDER_CONTENTS_SYNCED, folder.getRemotePath(), result);
    // check the result of synchronizing the folder
    if (result.isSuccess() || result.getCode() == ResultCode.SYNC_CONFLICT) {
        if (result.getCode() == ResultCode.SYNC_CONFLICT) {
            mConflictsFound += synchFolderOp.getConflictsFound();
            mFailsInFavouritesFound += synchFolderOp.getFailsInFileSyncsFound();
        }
        if (synchFolderOp.getForgottenLocalFiles().size() > 0) {
            mForgottenLocalFiles.putAll(synchFolderOp.getForgottenLocalFiles());
        }
        if (result.isSuccess()) {
            // synchronize children folders 
            List<Pair<OCFile, Boolean>> children = synchFolderOp.getFoldersToVisit();
            // beware of the 'hidden' recursion here!
            syncSubfolders(children);
        }
    } else if (result.getCode() != ResultCode.FILE_NOT_FOUND) {
        // in failures, the statistics for the global result are updated
        if (RemoteOperationResult.ResultCode.UNAUTHORIZED.equals(result.getCode())) {
            mSyncResult.stats.numAuthExceptions++;
        } else if (result.getException() instanceof DavException) {
            mSyncResult.stats.numParseExceptions++;
        } else if (result.getException() instanceof IOException) {
            mSyncResult.stats.numIoExceptions++;
        }
        mFailedResultsCounter++;
        mLastFailedResult = result;
    }
// else, ResultCode.FILE_NOT_FOUND is ignored, remote folder was
// removed from other thread or other client during the synchronization,
// before this thread fetched its contents
}
Also used : SynchronizeFolderOperation(com.owncloud.android.operations.SynchronizeFolderOperation) DavException(org.apache.jackrabbit.webdav.DavException) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) IOException(java.io.IOException) Pair(android.support.v4.util.Pair)

Aggregations

RemoteOperationResult (com.owncloud.android.lib.common.operations.RemoteOperationResult)46 OCFile (com.owncloud.android.datamodel.OCFile)11 ArrayList (java.util.ArrayList)9 RemoteOperation (com.owncloud.android.lib.common.operations.RemoteOperation)7 OCShare (com.owncloud.android.lib.resources.shares.OCShare)7 FileDataStorageManager (com.owncloud.android.datamodel.FileDataStorageManager)6 ExistenceCheckRemoteOperation (com.owncloud.android.lib.resources.files.ExistenceCheckRemoteOperation)6 OwnCloudAccount (com.owncloud.android.lib.common.OwnCloudAccount)5 OperationCancelledException (com.owncloud.android.lib.common.operations.OperationCancelledException)5 File (java.io.File)5 IOException (java.io.IOException)5 Account (android.accounts.Account)4 Uri (android.net.Uri)4 Intent (android.content.Intent)3 RemoteFile (com.owncloud.android.lib.resources.files.RemoteFile)3 GetRemoteSharesForFileOperation (com.owncloud.android.lib.resources.shares.GetRemoteSharesForFileOperation)3 UserInfo (com.owncloud.android.lib.resources.users.GetRemoteUserInfoOperation.UserInfo)3 AccountManager (android.accounts.AccountManager)2 Pair (android.util.Pair)2 OwnCloudClient (com.owncloud.android.lib.common.OwnCloudClient)2