Search in sources :

Example 1 with RemoveFileOperation

use of com.owncloud.android.operations.RemoveFileOperation in project android by nextcloud.

the class ErrorMessageAdapterUnitTest method getErrorCauseMessageForForbiddenRemoval.

@Test
public void getErrorCauseMessageForForbiddenRemoval() {
    // Given a mocked set of resources passed to the object under test...
    when(mMockResources.getString(R.string.forbidden_permissions)).thenReturn(MOCK_FORBIDDEN_PERMISSIONS);
    when(mMockResources.getString(R.string.forbidden_permissions_delete)).thenReturn(MOCK_TO_DELETE);
    Account account = new Account("name", MainApp.getAccountType());
    // ... when method under test is called ...
    String errorMessage = ErrorMessageAdapter.getErrorCauseMessage(new RemoteOperationResult(RemoteOperationResult.ResultCode.FORBIDDEN), new RemoveFileOperation(PATH_TO_DELETE, false, account, MainApp.getAppContext()), mMockResources);
    // ... then the result should be the expected one.
    assertThat(errorMessage, is(EXPECTED_ERROR_MESSAGE));
}
Also used : Account(android.accounts.Account) RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) RemoveFileOperation(com.owncloud.android.operations.RemoveFileOperation) Test(org.junit.Test)

Example 2 with RemoveFileOperation

use of com.owncloud.android.operations.RemoveFileOperation in project android by owncloud.

the class OperationsService method newOperation.

/**
 * Creates a new operation, as described by operationIntent.
 *
 * TODO - move to ServiceHandler (probably)
 *
 * @param operationIntent Intent describing a new operation to queue and execute.
 * @return Pair with the new operation object and the information about its
 * target server.
 */
private Pair<Target, RemoteOperation> newOperation(Intent operationIntent) {
    RemoteOperation operation = null;
    Target target = null;
    try {
        if (!operationIntent.hasExtra(EXTRA_ACCOUNT) && !operationIntent.hasExtra(EXTRA_SERVER_URL)) {
            Timber.e("Not enough information provided in intent");
        } else {
            Account account = operationIntent.getParcelableExtra(EXTRA_ACCOUNT);
            String serverUrl = operationIntent.getStringExtra(EXTRA_SERVER_URL);
            String cookie = operationIntent.getStringExtra(EXTRA_COOKIE);
            target = new Target(account, (serverUrl == null) ? null : Uri.parse(serverUrl), cookie);
            String action = operationIntent.getAction();
            if (action != null) {
                switch(action) {
                    case ACTION_RENAME:
                        {
                            // Rename file or folder
                            String remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
                            String newName = operationIntent.getStringExtra(EXTRA_NEWNAME);
                            operation = new RenameFileOperation(remotePath, newName);
                            break;
                        }
                    case ACTION_REMOVE:
                        {
                            // Remove file or folder
                            String remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
                            boolean onlyLocalCopy = operationIntent.getBooleanExtra(EXTRA_REMOVE_ONLY_LOCAL, false);
                            operation = new RemoveFileOperation(remotePath, onlyLocalCopy, operationIntent.getBooleanExtra(EXTRA_IS_LAST_FILE_TO_REMOVE, false));
                            break;
                        }
                    case ACTION_CREATE_FOLDER:
                        {
                            // Create Folder
                            String remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
                            boolean createFullPath = operationIntent.getBooleanExtra(EXTRA_CREATE_FULL_PATH, true);
                            operation = new CreateFolderOperation(remotePath, createFullPath);
                            break;
                        }
                    case ACTION_SYNC_FILE:
                        {
                            // Sync file
                            String remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
                            operation = new SynchronizeFileOperation(remotePath, account, getApplicationContext());
                            break;
                        }
                    case ACTION_SYNC_FOLDER:
                        {
                            // Sync folder (all its descendant files are synced)
                            String remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
                            boolean pushOnly = operationIntent.getBooleanExtra(EXTRA_PUSH_ONLY, false);
                            boolean syncContentOfRegularFiles = operationIntent.getBooleanExtra(EXTRA_SYNC_REGULAR_FILES, false);
                            operation = new SynchronizeFolderOperation(// TODO remove this dependency from construction time
                            this, remotePath, account, // TODO remove this dependency from construction time
                            System.currentTimeMillis(), pushOnly, false, syncContentOfRegularFiles);
                            break;
                        }
                    case ACTION_MOVE_FILE:
                        {
                            // Move file/folder
                            String remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
                            String newParentPath = operationIntent.getStringExtra(EXTRA_NEW_PARENT_PATH);
                            operation = new MoveFileOperation(remotePath, newParentPath);
                            break;
                        }
                    case ACTION_COPY_FILE:
                        {
                            // Copy file/folder
                            String remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
                            String newParentPath = operationIntent.getStringExtra(EXTRA_NEW_PARENT_PATH);
                            operation = new CopyFileOperation(remotePath, newParentPath);
                            break;
                        }
                    case ACTION_CHECK_CURRENT_CREDENTIALS:
                        // Check validity of currently stored credentials for a given account
                        operation = new CheckCurrentCredentialsOperation(account);
                        break;
                }
            }
        }
    } catch (IllegalArgumentException e) {
        Timber.e(e, "Bad information provided in intent: %s", e.getMessage());
        operation = null;
    }
    if (operation != null) {
        return new Pair<>(target, operation);
    } else {
        return null;
    }
}
Also used : RemoteOperation(com.owncloud.android.lib.common.operations.RemoteOperation) Account(android.accounts.Account) OwnCloudAccount(com.owncloud.android.lib.common.OwnCloudAccount) CheckCurrentCredentialsOperation(com.owncloud.android.operations.CheckCurrentCredentialsOperation) RemoveFileOperation(com.owncloud.android.operations.RemoveFileOperation) RenameFileOperation(com.owncloud.android.operations.RenameFileOperation) CopyFileOperation(com.owncloud.android.operations.CopyFileOperation) CreateFolderOperation(com.owncloud.android.operations.CreateFolderOperation) SynchronizeFolderOperation(com.owncloud.android.operations.SynchronizeFolderOperation) MoveFileOperation(com.owncloud.android.operations.MoveFileOperation) SynchronizeFileOperation(com.owncloud.android.operations.SynchronizeFileOperation) Pair(android.util.Pair)

Example 3 with RemoveFileOperation

use of com.owncloud.android.operations.RemoveFileOperation in project android by owncloud.

the class ErrorMessageAdapterUnitTest method getErrorCauseMessageForForbiddenRemoval.

@Test
public void getErrorCauseMessageForForbiddenRemoval() {
    // Given a mocked set of resources passed to the object under test...
    when(mMockResources.getString(R.string.forbidden_permissions)).thenReturn(MOCK_FORBIDDEN_PERMISSIONS);
    when(mMockResources.getString(R.string.forbidden_permissions_delete)).thenReturn(MOCK_TO_DELETE);
    // ... when method under test is called ...
    String errorMessage = ErrorMessageAdapter.getErrorCauseMessage(new RemoteOperationResult(RemoteOperationResult.ResultCode.FORBIDDEN), new RemoveFileOperation(PATH_TO_DELETE, false), mMockResources);
    // ... then the result should be the expected one.
    assertThat(errorMessage, is(EXPECTED_ERROR_MESSAGE));
}
Also used : RemoteOperationResult(com.owncloud.android.lib.common.operations.RemoteOperationResult) RemoveFileOperation(com.owncloud.android.operations.RemoveFileOperation) Test(org.junit.Test)

Example 4 with RemoveFileOperation

use of com.owncloud.android.operations.RemoveFileOperation in project android by nextcloud.

the class OperationsService method newOperation.

/**
 * Creates a new operation, as described by operationIntent.
 *
 * TODO - move to ServiceHandler (probably)
 *
 * @param operationIntent       Intent describing a new operation to queue and execute.
 * @return                      Pair with the new operation object and the information about its
 *                              target server.
 */
private Pair<Target, RemoteOperation> newOperation(Intent operationIntent) {
    RemoteOperation operation = null;
    Target target = null;
    try {
        if (!operationIntent.hasExtra(EXTRA_ACCOUNT) && !operationIntent.hasExtra(EXTRA_SERVER_URL)) {
            Log_OC.e(TAG, "Not enough information provided in intent");
        } else {
            Account account = operationIntent.getParcelableExtra(EXTRA_ACCOUNT);
            String serverUrl = operationIntent.getStringExtra(EXTRA_SERVER_URL);
            String cookie = operationIntent.getStringExtra(EXTRA_COOKIE);
            target = new Target(account, (serverUrl == null) ? null : Uri.parse(serverUrl), cookie);
            String action = operationIntent.getAction();
            if (action.equals(ACTION_CREATE_SHARE_VIA_LINK)) {
                // Create public share via link
                String remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
                String password = operationIntent.getStringExtra(EXTRA_SHARE_PASSWORD);
                if (remotePath.length() > 0) {
                    operation = new CreateShareViaLinkOperation(remotePath, password);
                }
            } else if (ACTION_UPDATE_SHARE.equals(action)) {
                String remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
                long shareId = operationIntent.getLongExtra(EXTRA_SHARE_ID, -1);
                if (remotePath != null && remotePath.length() > 0) {
                    operation = new UpdateShareViaLinkOperation(remotePath);
                    String password = operationIntent.getStringExtra(EXTRA_SHARE_PASSWORD);
                    ((UpdateShareViaLinkOperation) operation).setPassword(password);
                    long expirationDate = operationIntent.getLongExtra(EXTRA_SHARE_EXPIRATION_DATE_IN_MILLIS, 0);
                    ((UpdateShareViaLinkOperation) operation).setExpirationDate(expirationDate);
                    if (operationIntent.hasExtra(EXTRA_SHARE_PUBLIC_UPLOAD)) {
                        ((UpdateShareViaLinkOperation) operation).setPublicUpload(operationIntent.getBooleanExtra(EXTRA_SHARE_PUBLIC_UPLOAD, false));
                    }
                } else if (shareId > 0) {
                    operation = new UpdateSharePermissionsOperation(shareId);
                    int permissions = operationIntent.getIntExtra(EXTRA_SHARE_PERMISSIONS, 1);
                    ((UpdateSharePermissionsOperation) operation).setPermissions(permissions);
                }
            } else if (action.equals(ACTION_CREATE_SHARE_WITH_SHAREE)) {
                // Create private share with user or group
                String remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
                String shareeName = operationIntent.getStringExtra(EXTRA_SHARE_WITH);
                ShareType shareType = (ShareType) operationIntent.getSerializableExtra(EXTRA_SHARE_TYPE);
                int permissions = operationIntent.getIntExtra(EXTRA_SHARE_PERMISSIONS, -1);
                if (remotePath.length() > 0) {
                    operation = new CreateShareWithShareeOperation(remotePath, shareeName, shareType, permissions);
                }
            } else if (action.equals(ACTION_UNSHARE)) {
                // Unshare file
                String remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
                ShareType shareType = (ShareType) operationIntent.getSerializableExtra(EXTRA_SHARE_TYPE);
                String shareWith = operationIntent.getStringExtra(EXTRA_SHARE_WITH);
                if (remotePath.length() > 0) {
                    operation = new UnshareOperation(remotePath, shareType, shareWith, OperationsService.this);
                }
            } else if (action.equals(ACTION_GET_SERVER_INFO)) {
                // check OC server and get basic information from it
                operation = new GetServerInfoOperation(serverUrl, OperationsService.this);
            } else if (action.equals(ACTION_OAUTH2_GET_ACCESS_TOKEN)) {
                // / GET ACCESS TOKEN to the OAuth server
                String oauth2QueryParameters = operationIntent.getStringExtra(EXTRA_OAUTH2_QUERY_PARAMETERS);
                operation = new OAuth2GetAccessToken(getString(R.string.oauth2_client_id), getString(R.string.oauth2_redirect_uri), getString(R.string.oauth2_grant_type), oauth2QueryParameters);
            } else if (action.equals(ACTION_GET_USER_NAME)) {
                // Get User Name
                operation = new GetRemoteUserInfoOperation();
            } else if (action.equals(ACTION_RENAME)) {
                // Rename file or folder
                String remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
                String newName = operationIntent.getStringExtra(EXTRA_NEWNAME);
                operation = new RenameFileOperation(remotePath, newName);
            } else if (action.equals(ACTION_REMOVE)) {
                // Remove file or folder
                String remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
                boolean onlyLocalCopy = operationIntent.getBooleanExtra(EXTRA_REMOVE_ONLY_LOCAL, false);
                operation = new RemoveFileOperation(remotePath, onlyLocalCopy, account, getApplicationContext());
            } else if (action.equals(ACTION_CREATE_FOLDER)) {
                // Create Folder
                String remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
                boolean createFullPath = operationIntent.getBooleanExtra(EXTRA_CREATE_FULL_PATH, true);
                operation = new CreateFolderOperation(remotePath, createFullPath);
            } else if (action.equals(ACTION_SYNC_FILE)) {
                // Sync file
                String remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
                boolean syncFileContents = operationIntent.getBooleanExtra(EXTRA_SYNC_FILE_CONTENTS, true);
                operation = new SynchronizeFileOperation(remotePath, account, syncFileContents, getApplicationContext());
            } else if (action.equals(ACTION_SYNC_FOLDER)) {
                // Sync folder (all its descendant files are sync'ed)
                String remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
                operation = new SynchronizeFolderOperation(// TODO remove this dependency from construction time
                this, remotePath, account, // TODO remove this dependency from construction time
                System.currentTimeMillis());
            } else if (action.equals(ACTION_MOVE_FILE)) {
                // Move file/folder
                String remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
                String newParentPath = operationIntent.getStringExtra(EXTRA_NEW_PARENT_PATH);
                operation = new MoveFileOperation(remotePath, newParentPath);
            } else if (action.equals(ACTION_COPY_FILE)) {
                // Copy file/folder
                String remotePath = operationIntent.getStringExtra(EXTRA_REMOTE_PATH);
                String newParentPath = operationIntent.getStringExtra(EXTRA_NEW_PARENT_PATH);
                operation = new CopyFileOperation(remotePath, newParentPath);
            } else if (action.equals(ACTION_CHECK_CURRENT_CREDENTIALS)) {
                // Check validity of currently stored credentials for a given account
                operation = new CheckCurrentCredentialsOperation(account);
            }
        }
    } catch (IllegalArgumentException e) {
        Log_OC.e(TAG, "Bad information provided in intent: " + e.getMessage());
        operation = null;
    }
    if (operation != null) {
        return new Pair<Target, RemoteOperation>(target, operation);
    } else {
        return null;
    }
}
Also used : Account(android.accounts.Account) OwnCloudAccount(com.owncloud.android.lib.common.OwnCloudAccount) UnshareOperation(com.owncloud.android.operations.UnshareOperation) RemoveFileOperation(com.owncloud.android.operations.RemoveFileOperation) RenameFileOperation(com.owncloud.android.operations.RenameFileOperation) CopyFileOperation(com.owncloud.android.operations.CopyFileOperation) SynchronizeFolderOperation(com.owncloud.android.operations.SynchronizeFolderOperation) UpdateShareViaLinkOperation(com.owncloud.android.operations.UpdateShareViaLinkOperation) Pair(android.util.Pair) RemoteOperation(com.owncloud.android.lib.common.operations.RemoteOperation) GetRemoteUserInfoOperation(com.owncloud.android.lib.resources.users.GetRemoteUserInfoOperation) CreateShareWithShareeOperation(com.owncloud.android.operations.CreateShareWithShareeOperation) CheckCurrentCredentialsOperation(com.owncloud.android.operations.CheckCurrentCredentialsOperation) CreateShareViaLinkOperation(com.owncloud.android.operations.CreateShareViaLinkOperation) CreateFolderOperation(com.owncloud.android.operations.CreateFolderOperation) OAuth2GetAccessToken(com.owncloud.android.operations.OAuth2GetAccessToken) MoveFileOperation(com.owncloud.android.operations.MoveFileOperation) GetServerInfoOperation(com.owncloud.android.operations.GetServerInfoOperation) UpdateSharePermissionsOperation(com.owncloud.android.operations.UpdateSharePermissionsOperation) ShareType(com.owncloud.android.lib.resources.shares.ShareType) SynchronizeFileOperation(com.owncloud.android.operations.SynchronizeFileOperation)

Aggregations

RemoveFileOperation (com.owncloud.android.operations.RemoveFileOperation)4 Account (android.accounts.Account)3 Pair (android.util.Pair)2 OwnCloudAccount (com.owncloud.android.lib.common.OwnCloudAccount)2 RemoteOperation (com.owncloud.android.lib.common.operations.RemoteOperation)2 RemoteOperationResult (com.owncloud.android.lib.common.operations.RemoteOperationResult)2 CheckCurrentCredentialsOperation (com.owncloud.android.operations.CheckCurrentCredentialsOperation)2 CopyFileOperation (com.owncloud.android.operations.CopyFileOperation)2 CreateFolderOperation (com.owncloud.android.operations.CreateFolderOperation)2 MoveFileOperation (com.owncloud.android.operations.MoveFileOperation)2 RenameFileOperation (com.owncloud.android.operations.RenameFileOperation)2 SynchronizeFileOperation (com.owncloud.android.operations.SynchronizeFileOperation)2 SynchronizeFolderOperation (com.owncloud.android.operations.SynchronizeFolderOperation)2 Test (org.junit.Test)2 ShareType (com.owncloud.android.lib.resources.shares.ShareType)1 GetRemoteUserInfoOperation (com.owncloud.android.lib.resources.users.GetRemoteUserInfoOperation)1 CreateShareViaLinkOperation (com.owncloud.android.operations.CreateShareViaLinkOperation)1 CreateShareWithShareeOperation (com.owncloud.android.operations.CreateShareWithShareeOperation)1 GetServerInfoOperation (com.owncloud.android.operations.GetServerInfoOperation)1 OAuth2GetAccessToken (com.owncloud.android.operations.OAuth2GetAccessToken)1