Эх сурвалжийг харах

Refactored renaming operation (inherits RemoteOperation, so generates RemoteOperationResult)

David A. Velasco 12 жил өмнө
parent
commit
5af0fb44b4

+ 3 - 1
src/com/owncloud/android/operations/RemoteOperationResult.java

@@ -66,7 +66,9 @@ public class RemoteOperationResult implements Serializable {
         SSL_RECOVERABLE_PEER_UNVERIFIED,
         BAD_OC_VERSION,
         STORAGE_ERROR_MOVING_FROM_TMP,
-        CANCELLED
+        CANCELLED, 
+        INVALID_LOCAL_FILE_NAME, 
+        INVALID_OVERWRITE
     }
 
     private boolean mSuccess = false;

+ 3 - 1
src/com/owncloud/android/operations/RemoveFileOperation.java

@@ -60,7 +60,9 @@ public class RemoveFileOperation extends RemoteOperation {
     
     
     /**
-     * Performs the removal
+     * Performs the remove operation
+     * 
+     * @param   client      Client object to communicate with the remote ownCloud server.
      */
     @Override
     protected RemoteOperationResult run(WebdavClient client) {

+ 221 - 0
src/com/owncloud/android/operations/RenameFileOperation.java

@@ -0,0 +1,221 @@
+/* ownCloud Android client application
+ *   Copyright (C) 2012 Bartek Przybylski
+ *
+ *   This program is free software: you can redistribute it and/or modify
+ *   it under the terms of the GNU General Public License as published by
+ *   the Free Software Foundation, either version 3 of the License, or
+ *   (at your option) any later version.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ *
+ *   You should have received a copy of the GNU General Public License
+ *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+package com.owncloud.android.operations;
+
+import java.io.File;
+import java.io.IOException;
+
+import org.apache.jackrabbit.webdav.client.methods.DavMethodBase;
+//import org.apache.jackrabbit.webdav.client.methods.MoveMethod;
+
+import android.util.Log;
+
+import com.owncloud.android.datamodel.DataStorageManager;
+import com.owncloud.android.datamodel.OCFile;
+import com.owncloud.android.files.services.FileDownloader;
+import com.owncloud.android.operations.RemoteOperationResult.ResultCode;
+
+import eu.alefzero.webdav.WebdavClient;
+import eu.alefzero.webdav.WebdavUtils;
+
+/**
+ * Remote operation performing the rename of a remote file (or folder?) in the ownCloud server.
+ * 
+ * @author David A. Velasco
+ */
+public class RenameFileOperation extends RemoteOperation {
+    
+    private static final String TAG = RemoveFileOperation.class.getCanonicalName();
+
+    private static final int RENAME_READ_TIMEOUT = 10000;
+    private static final int RENAME_CONNECTION_TIMEOUT = 5000;
+    
+
+    private OCFile mFile;
+    private String mNewName;
+    private DataStorageManager mStorageManager;
+    
+    
+    /**
+     * Constructor
+     * 
+     * @param file                  OCFile instance describing the remote file or folder to rename
+     * @param newName               New name to set as the name of file.
+     * @param storageManager        Reference to the local database corresponding to the account where the file is contained. 
+     */
+    public RenameFileOperation(OCFile file, String newName, DataStorageManager storageManager) {
+        mFile = file;
+        mNewName = newName;
+        mStorageManager = storageManager;
+    }
+  
+    public OCFile getFile() {
+        return mFile;
+    }
+    
+    
+    /**
+     * Performs the rename operation.
+     * 
+     * @param   client      Client object to communicate with the remote ownCloud server.
+     */
+    @Override
+    protected RemoteOperationResult run(WebdavClient client) {
+        RemoteOperationResult result = null;
+        
+        LocalMoveMethod move = null;
+        //MoveMethod move = null;   // TODO find out why not use this
+        String newRemotePath = null;
+        try {
+            if (mNewName.equals(mFile.getFileName())) {
+                return new RemoteOperationResult(ResultCode.OK);
+            }
+        
+            newRemotePath = (new File(mFile.getRemotePath())).getParent() + mNewName;
+            
+            // check if the new name is valid in the local file system
+            if (!isValidNewName()) {
+                return new RemoteOperationResult(ResultCode.INVALID_LOCAL_FILE_NAME);
+            }
+        
+            // check if a remote file with the new name already exists
+            if (client.existsFile(newRemotePath)) {
+                return new RemoteOperationResult(ResultCode.INVALID_OVERWRITE);
+            }
+            /*move = new MoveMethod( client.getBaseUri() + WebdavUtils.encodePath(mFile.getRemotePath()), 
+                                                client.getBaseUri() + WebdavUtils.encodePath(newRemotePath),
+                                                false);*/
+            move = new LocalMoveMethod( client.getBaseUri() + WebdavUtils.encodePath(mFile.getRemotePath()),
+                                        client.getBaseUri() + WebdavUtils.encodePath(newRemotePath));
+            int status = client.executeMethod(move, RENAME_READ_TIMEOUT, RENAME_CONNECTION_TIMEOUT);
+            if (move.succeeded()) {
+
+                // create new OCFile instance for the renamed file
+                OCFile newFile = obtainUpdatedFile();
+                OCFile oldFile = mFile;
+                mFile = newFile; 
+                
+                // try to rename the local copy of the file
+                if (oldFile.isDown()) {
+                    File f = new File(oldFile.getStoragePath());
+                    String newStoragePath = f.getParent() + mNewName;
+                    if (f.renameTo(new File(newStoragePath))) {
+                        mFile.setStoragePath(newStoragePath);
+                    }
+                    // else - NOTHING: the link to the local file is kept although the local name can't be updated
+                    // TODO - study conditions when this could be a problem
+                }
+                
+                mStorageManager.removeFile(oldFile, false);
+                mStorageManager.saveFile(mFile);
+                
+            }
+            
+            move.getResponseBodyAsString(); // exhaust response, although not interesting
+            result = new RemoteOperationResult(move.succeeded(), status);
+            Log.i(TAG, "Rename " + mFile.getRemotePath() + " to " + newRemotePath + ": " + result.getLogMessage());
+            
+        } catch (Exception e) {
+            result = new RemoteOperationResult(e);
+            Log.e(TAG, "Rename " + mFile.getRemotePath() + " to " + ((newRemotePath==null) ? mNewName : newRemotePath) + ": " + result.getLogMessage(), e);
+            
+        } finally {
+            if (move != null)
+                move.releaseConnection();
+        }
+        return result;
+    }
+
+    
+    /**
+     * Checks if the new name to set is valid in the file system 
+     * 
+     * The only way to be sure is trying to create a file with that name. It's made in the temporal directory
+     * for downloads, out of any account, and then removed. 
+     * 
+     * IMPORTANT: The test must be made in the same file system where files are download. The internal storage
+     * could be formatted with a different file system.
+     * 
+     * @return      'True' if a temporal file named with the name to set could be created in the file system where 
+     *              local files are stored.
+     */
+    private boolean isValidNewName() {
+        // check tricky names
+        if (mNewName == null || mNewName.length() <= 0 || mNewName.contains(File.separator)) { 
+            return false;
+        }
+        // create a test file
+        String tmpFolder = FileDownloader.getTemporalPath("");
+        File testFile = new File(tmpFolder + mNewName);
+        try {
+            testFile.createNewFile();   // return value is ignored; it could be 'false' because the file already existed, that doesn't invalidate the name
+        } catch (IOException e) {
+            Log.i(TAG, "Test for validity of name " + mNewName + " in the file system failed");
+            return false;
+        }
+        boolean result = (testFile.exists() && testFile.isFile());
+        
+        // cleaning ; result is ignored, since there is not much we could do in case of failure, but repeat and repeat...
+        testFile.delete();
+        
+        return result;
+    }
+
+
+    /**
+     * Creates a new OCFile for the new remote name of the renamed file.
+     * 
+     * @return      OCFile object with the same information than mFile, but the renamed remoteFile and the storagePath (empty)
+     */
+    private OCFile obtainUpdatedFile() {
+        OCFile file = new OCFile(mStorageManager.getFileById(mFile.getParentId()).getRemotePath() + mNewName);
+        file.setCreationTimestamp(mFile.getCreationTimestamp());
+        file.setFileId(mFile.getFileId());
+        file.setFileLength(mFile.getFileLength());
+        file.setKeepInSync(mFile.keepInSync());
+        file.setLastSyncDate(mFile.getLastSyncDate());
+        file.setMimetype(mFile.getMimetype());
+        file.setModificationTimestamp(mFile.getModificationTimestamp());
+        file.setParentId(mFile.getParentId());
+        return file;
+    }
+
+
+    // move operation - TODO: find out why org.apache.jackrabbit.webdav.client.methods.MoveMethod is not used instead ¿?
+    private class LocalMoveMethod extends DavMethodBase {
+
+        public LocalMoveMethod(String uri, String dest) {
+            super(uri);
+            addRequestHeader(new org.apache.commons.httpclient.Header("Destination", dest));
+        }
+
+        @Override
+        public String getName() {
+            return "MOVE";
+        }
+
+        @Override
+        protected boolean isSuccess(int status) {
+            return status == 201 || status == 204;
+        }
+            
+    }
+    
+
+}

+ 106 - 0
src/com/owncloud/android/ui/dialog/EditNameDialog.java

@@ -0,0 +1,106 @@
+/* ownCloud Android client application
+ *   Copyright (C) 2011  Bartek Przybylski
+ *
+ *   This program is free software: you can redistribute it and/or modify
+ *   it under the terms of the GNU General Public License as published by
+ *   the Free Software Foundation, either version 3 of the License, or
+ *   (at your option) any later version.
+ *
+ *   This program is distributed in the hope that it will be useful,
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *   GNU General Public License for more details.
+ *
+ *   You should have received a copy of the GNU General Public License
+ *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+
+package com.owncloud.android.ui.dialog;
+
+import android.os.Bundle;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.view.View.OnClickListener;
+import android.view.WindowManager.LayoutParams;
+import android.widget.Button;
+import android.widget.TextView;
+
+import com.actionbarsherlock.app.SherlockDialogFragment;
+import com.owncloud.android.R;
+
+
+/**
+ * Dialog to request the user about a certificate that could not be validated with the certificates store in the system.
+ * 
+ * @author Bartek Przybylski
+ */
+public class EditNameDialog extends SherlockDialogFragment implements OnClickListener {
+
+    private String mNewFilename;
+    private boolean mResult;
+    private EditNameDialogListener mListener;
+    
+    static public EditNameDialog newInstance(String filename) {
+        EditNameDialog f = new EditNameDialog();
+        Bundle args = new Bundle();
+        args.putString("filename", filename);
+        f.setArguments(args);
+        return f;
+    }
+    
+    @Override
+    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
+        View v = inflater.inflate(R.layout.edit_box_dialog, container, false);
+
+        String currentName = getArguments().getString("filename");
+        if (currentName == null)
+            currentName = "";
+        
+        ((Button)v.findViewById(R.id.cancel)).setOnClickListener(this);
+        ((Button)v.findViewById(R.id.ok)).setOnClickListener(this);
+        ((TextView)v.findViewById(R.id.user_input)).setText(currentName);
+        ((TextView)v.findViewById(R.id.user_input)).requestFocus();
+        getDialog().getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
+
+        mResult = false;
+        return v;
+    }
+    
+    @Override
+    public void onClick(View view) {
+        switch (view.getId()) {
+            case R.id.ok: {
+                mNewFilename = ((TextView)getView().findViewById(R.id.user_input)).getText().toString();
+                mResult = true;
+            }
+            case R.id.cancel: { // fallthought
+                dismiss();
+                if (mListener != null)
+                    mListener.onDismiss(this);
+            }
+        }
+    }
+    
+    public void setOnDismissListener(EditNameDialogListener listener) {
+        mListener = listener;
+    }
+    
+    public String getNewFilename() {
+        return mNewFilename;
+    }
+    
+    // true if user clicked ok
+    public boolean getResult() {
+        return mResult;
+    }
+
+    
+    public interface EditNameDialogListener {
+        public void onDismiss(EditNameDialog dialog);
+    }
+    
+}
+

+ 70 - 238
src/com/owncloud/android/ui/fragment/FileDetailFragment.java

@@ -18,11 +18,9 @@
 package com.owncloud.android.ui.fragment;
 
 import java.io.File;
-import java.io.IOException;
 import java.util.ArrayList;
 import java.util.List;
 
-import org.apache.commons.httpclient.HttpException;
 import org.apache.commons.httpclient.methods.GetMethod;
 import org.apache.commons.httpclient.methods.PostMethod;
 import org.apache.commons.httpclient.methods.StringRequestEntity;
@@ -32,7 +30,6 @@ import org.apache.http.NameValuePair;
 import org.apache.http.client.utils.URLEncodedUtils;
 import org.apache.http.message.BasicNameValuePair;
 import org.apache.http.protocol.HTTP;
-import org.apache.jackrabbit.webdav.client.methods.DavMethodBase;
 import org.apache.jackrabbit.webdav.client.methods.PropFindMethod;
 import org.json.JSONObject;
 
@@ -45,7 +42,6 @@ import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
-import android.content.res.Resources.NotFoundException;
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
 import android.graphics.BitmapFactory.Options;
@@ -61,7 +57,6 @@ import android.view.LayoutInflater;
 import android.view.View;
 import android.view.View.OnClickListener;
 import android.view.ViewGroup;
-import android.view.WindowManager.LayoutParams;
 import android.webkit.MimeTypeMap;
 import android.widget.Button;
 import android.widget.CheckBox;
@@ -69,7 +64,6 @@ import android.widget.ImageView;
 import android.widget.TextView;
 import android.widget.Toast;
 
-import com.actionbarsherlock.app.SherlockDialogFragment;
 import com.actionbarsherlock.app.SherlockFragment;
 import com.owncloud.android.AccountUtils;
 import com.owncloud.android.DisplayUtils;
@@ -85,10 +79,14 @@ import com.owncloud.android.network.OwnCloudClientUtils;
 import com.owncloud.android.operations.OnRemoteOperationListener;
 import com.owncloud.android.operations.RemoteOperation;
 import com.owncloud.android.operations.RemoteOperationResult;
+import com.owncloud.android.operations.RemoteOperationResult.ResultCode;
 import com.owncloud.android.operations.RemoveFileOperation;
+import com.owncloud.android.operations.RenameFileOperation;
 import com.owncloud.android.ui.activity.FileDetailActivity;
 import com.owncloud.android.ui.activity.FileDisplayActivity;
 import com.owncloud.android.ui.activity.TransferServiceGetter;
+import com.owncloud.android.ui.dialog.EditNameDialog;
+import com.owncloud.android.ui.dialog.EditNameDialog.EditNameDialogListener;
 import com.owncloud.android.utils.OwnCloudVersion;
 
 import com.owncloud.android.R;
@@ -102,7 +100,7 @@ import eu.alefzero.webdav.WebdavUtils;
  * 
  */
 public class FileDetailFragment extends SherlockFragment implements
-        OnClickListener, ConfirmationDialogFragment.ConfirmationDialogFragmentListener, OnRemoteOperationListener {
+        OnClickListener, ConfirmationDialogFragment.ConfirmationDialogFragmentListener, OnRemoteOperationListener, EditNameDialogListener {
 
     public static final String EXTRA_FILE = "FILE";
     public static final String EXTRA_ACCOUNT = "ACCOUNT";
@@ -330,9 +328,9 @@ public class FileDetailFragment extends SherlockFragment implements
                 break;
             }
             case R.id.fdRenameBtn: {
-                EditNameFragment dialog = EditNameFragment.newInstance(mFile.getFileName());
-                dialog.show(getFragmentManager(), "nameeditdialog");
+                EditNameDialog dialog = EditNameDialog.newInstance(mFile.getFileName());
                 dialog.setOnDismissListener(this);
+                dialog.show(getFragmentManager(), "nameeditdialog");
                 break;
             }   
             case R.id.fdRemoveBtn: {
@@ -812,218 +810,20 @@ public class FileDetailFragment extends SherlockFragment implements
         }
     }
     
-    public void onDismiss(EditNameFragment dialog) {
-        if (dialog instanceof EditNameFragment) {
-            if (((EditNameFragment)dialog).getResult()) {
-                String newFilename = ((EditNameFragment)dialog).getNewFilename();
-                Log.d(TAG, "name edit dialog dismissed with new name " + newFilename);
-                if (!newFilename.equals(mFile.getFileName())) {
-                    FileDataStorageManager fdsm = new FileDataStorageManager(mAccount, getActivity().getContentResolver());
-                    if (fdsm.getFileById(mFile.getFileId()) != null) {
-                        OCFile newFile = new OCFile(fdsm.getFileById(mFile.getParentId()).getRemotePath() + newFilename);
-                        newFile.setCreationTimestamp(mFile.getCreationTimestamp());
-                        newFile.setFileId(mFile.getFileId());
-                        newFile.setFileLength(mFile.getFileLength());
-                        newFile.setKeepInSync(mFile.keepInSync());
-                        newFile.setLastSyncDate(mFile.getLastSyncDate());
-                        newFile.setMimetype(mFile.getMimetype());
-                        newFile.setModificationTimestamp(mFile.getModificationTimestamp());
-                        newFile.setParentId(mFile.getParentId());
-                        boolean localRenameFails = false;
-                        if (mFile.isDown()) {
-                            File f = new File(mFile.getStoragePath());
-                            Log.e(TAG, f.getAbsolutePath());
-                            localRenameFails = !(f.renameTo(new File(f.getParent() + File.separator + newFilename)));
-                            Log.e(TAG, f.getParent() + File.separator + newFilename);
-                            newFile.setStoragePath(f.getParent() + File.separator + newFilename);
-                        }
-                        
-                        if (localRenameFails) {
-                            Toast msg = Toast.makeText(getActivity(), R.string.rename_local_fail_msg, Toast.LENGTH_LONG); 
-                            msg.show();
-                            
-                        } else {
-                            new Thread(new RenameRunnable(mFile, newFile, mAccount, new Handler())).start();
-                            boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity;
-                            getActivity().showDialog((inDisplayActivity)? FileDisplayActivity.DIALOG_SHORT_WAIT : FileDetailActivity.DIALOG_SHORT_WAIT);
-                        }
-
-                    }
-                }
-            }
-        } else {
-            Log.e(TAG, "Unknown dialog instance passed to onDismissDalog: " + dialog.getClass().getCanonicalName());
-        }
-        
-    }
-    
-    private class RenameRunnable implements Runnable {
-        
-        Account mAccount;
-        OCFile mOld, mNew;
-        Handler mHandler;
-        
-        public RenameRunnable(OCFile oldFile, OCFile newFile, Account account, Handler handler) {
-            mOld = oldFile;
-            mNew = newFile;
-            mAccount = account;
-            mHandler = handler;
-        }
-        
-        public void run() {
+    public void onDismiss(EditNameDialog dialog) {
+        if (dialog.getResult()) {
+            String newFilename = dialog.getNewFilename();
+            Log.d(TAG, "name edit dialog dismissed with new name " + newFilename);
+            mLastRemoteOperation = new RenameFileOperation( mFile, 
+                                                            newFilename, 
+                                                            new FileDataStorageManager(mAccount, getActivity().getContentResolver()));
             WebdavClient wc = OwnCloudClientUtils.createOwnCloudClient(mAccount, getSherlockActivity().getApplicationContext());
-            AccountManager am = AccountManager.get(getSherlockActivity());
-            String baseUrl = am.getUserData(mAccount, AccountAuthenticator.KEY_OC_BASE_URL);
-            OwnCloudVersion ocv = new OwnCloudVersion(am.getUserData(mAccount, AccountAuthenticator.KEY_OC_VERSION));
-            String webdav_path = AccountUtils.getWebdavPath(ocv);
-            Log.d("ASD", ""+baseUrl + webdav_path + WebdavUtils.encodePath(mOld.getRemotePath()));
-
-            Log.e("ASD", Uri.parse(baseUrl).getPath() == null ? "" : Uri.parse(baseUrl).getPath() + webdav_path + WebdavUtils.encodePath(mNew.getRemotePath()));
-            LocalMoveMethod move = new LocalMoveMethod(baseUrl + webdav_path + WebdavUtils.encodePath(mOld.getRemotePath()),
-                                             Uri.parse(baseUrl).getPath() == null ? "" : Uri.parse(baseUrl).getPath() + webdav_path + WebdavUtils.encodePath(mNew.getRemotePath()));
-            
-            boolean success = false;
-            try {
-                int status = wc.executeMethod(move);
-                success = move.succeeded();
-                move.getResponseBodyAsString(); // exhaust response, although not interesting
-                Log.d(TAG, "Move returned status: " + status);
-                
-            } catch (HttpException e) {
-                Log.e(TAG, "HTTP Exception renaming file " + mOld.getRemotePath() + " to " + mNew.getRemotePath(), e);
-                
-            } catch (IOException e) {
-                Log.e(TAG, "I/O Exception renaming file " + mOld.getRemotePath() + " to " + mNew.getRemotePath(), e);
-                
-            } catch (Exception e) {
-                Log.e(TAG, "Unexpected exception renaming file " + mOld.getRemotePath() + " to " + mNew.getRemotePath(), e);
-                
-            } finally {
-               move.releaseConnection();
-            } 
-            
-            if (success) {
-                FileDataStorageManager fdsm = new FileDataStorageManager(mAccount, getActivity().getContentResolver());
-                fdsm.removeFile(mOld, false);
-                fdsm.saveFile(mNew);
-                mFile = mNew;
-                mHandler.post(new Runnable() {
-                    @Override
-                    public void run() { 
-                        boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity;
-                        getActivity().dismissDialog((inDisplayActivity)? FileDisplayActivity.DIALOG_SHORT_WAIT : FileDetailActivity.DIALOG_SHORT_WAIT);
-                        updateFileDetails(mFile, mAccount);
-                        mContainerActivity.onFileStateChanged();
-                    }
-                });
-                
-            } else {
-                mHandler.post(new Runnable() {
-                    @Override
-                    public void run() {
-                        // undo the local rename
-                        if (mNew.isDown()) {
-                            File f = new File(mNew.getStoragePath());
-                            if (!f.renameTo(new File(mOld.getStoragePath()))) {
-                                // the local rename undoing failed; last chance: save the new local storage path in the old file
-                                mFile.setStoragePath(mNew.getStoragePath());
-                                FileDataStorageManager fdsm = new FileDataStorageManager(mAccount, getActivity().getContentResolver());
-                                fdsm.saveFile(mFile);
-                            }
-                        }
-                        boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity;
-                        getActivity().dismissDialog((inDisplayActivity)? FileDisplayActivity.DIALOG_SHORT_WAIT : FileDetailActivity.DIALOG_SHORT_WAIT);
-                        try {
-                            Toast msg = Toast.makeText(getActivity(), R.string.rename_server_fail_msg, Toast.LENGTH_LONG); 
-                            msg.show();
-                            
-                        } catch (NotFoundException e) {
-                            e.printStackTrace();
-                        }
-                    }
-                });
-            }
-        }
-        private class LocalMoveMethod extends DavMethodBase {
-
-            public LocalMoveMethod(String uri, String dest) {
-                super(uri);
-                addRequestHeader(new org.apache.commons.httpclient.Header("Destination", dest));
-            }
-
-            @Override
-            public String getName() {
-                return "MOVE";
-            }
-
-            @Override
-            protected boolean isSuccess(int status) {
-                return status == 201 || status == 204;
-            }
-            
+            mLastRemoteOperation.execute(wc, this, mHandler);
+            boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity;
+            getActivity().showDialog((inDisplayActivity)? FileDisplayActivity.DIALOG_SHORT_WAIT : FileDetailActivity.DIALOG_SHORT_WAIT);
         }
     }
     
-    private static class EditNameFragment extends SherlockDialogFragment implements OnClickListener {
-
-        private String mNewFilename;
-        private boolean mResult;
-        private FileDetailFragment mListener;
-        
-        static public EditNameFragment newInstance(String filename) {
-            EditNameFragment f = new EditNameFragment();
-            Bundle args = new Bundle();
-            args.putString("filename", filename);
-            f.setArguments(args);
-            return f;
-        }
-        
-        @Override
-        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
-            View v = inflater.inflate(R.layout.edit_box_dialog, container, false);
-
-            String currentName = getArguments().getString("filename");
-            if (currentName == null)
-                currentName = "";
-            
-            ((Button)v.findViewById(R.id.cancel)).setOnClickListener(this);
-            ((Button)v.findViewById(R.id.ok)).setOnClickListener(this);
-            ((TextView)v.findViewById(R.id.user_input)).setText(currentName);
-            ((TextView)v.findViewById(R.id.user_input)).requestFocus();
-            getDialog().getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
-
-            mResult = false;
-            return v;
-        }
-        
-        @Override
-        public void onClick(View view) {
-            switch (view.getId()) {
-                case R.id.ok: {
-                    mNewFilename = ((TextView)getView().findViewById(R.id.user_input)).getText().toString();
-                    mResult = true;
-                }
-                case R.id.cancel: { // fallthought
-                    dismiss();
-                    mListener.onDismiss(this);
-                }
-            }
-        }
-        
-        void setOnDismissListener(FileDetailFragment listener) {
-            mListener = listener;
-        }
-        
-        public String getNewFilename() {
-            return mNewFilename;
-        }
-        
-        // true if user click ok
-        public boolean getResult() {
-            return mResult;
-        }
-        
-    }
     
     class BitmapLoader extends AsyncTask<String, Void, Bitmap> {
         @SuppressLint({ "NewApi", "NewApi", "NewApi" }) // to avoid Lint errors since Android SDK r20
@@ -1106,27 +906,59 @@ public class FileDetailFragment extends SherlockFragment implements
     public void onRemoteOperationFinish(RemoteOperation operation, RemoteOperationResult result) {
         if (operation.equals(mLastRemoteOperation)) {
             if (operation instanceof RemoveFileOperation) {
-                boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity;
-                getActivity().dismissDialog((inDisplayActivity)? FileDisplayActivity.DIALOG_SHORT_WAIT : FileDetailActivity.DIALOG_SHORT_WAIT);
-                if (result.isSuccess()) {
-                    Toast msg = Toast.makeText(getActivity().getApplicationContext(), R.string.remove_success_msg, Toast.LENGTH_LONG);
-                    msg.show();
-                    if (inDisplayActivity) {
-                        // double pane
-                        FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
-                        transaction.replace(R.id.file_details_container, new FileDetailFragment(null, null)); // empty FileDetailFragment
-                        transaction.commit();
-                        mContainerActivity.onFileStateChanged();
-                    } else {
-                        getActivity().finish();
-                    }
-                    
-                } else {
-                    Toast msg = Toast.makeText(getActivity(), R.string.remove_fail_msg, Toast.LENGTH_LONG); 
-                    msg.show();
-                    if (result.isSslRecoverableException()) {
-                        // TODO
-                    }
+                onRemoveFileOperationFinish((RemoveFileOperation)operation, result);
+                
+            } else if (operation instanceof RenameFileOperation) {
+                onRenameFileOperationFinish((RenameFileOperation)operation, result);
+            }
+        }
+    }
+    
+    
+    private void onRemoveFileOperationFinish(RemoveFileOperation operation, RemoteOperationResult result) {
+        boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity;
+        getActivity().dismissDialog((inDisplayActivity)? FileDisplayActivity.DIALOG_SHORT_WAIT : FileDetailActivity.DIALOG_SHORT_WAIT);
+        
+        if (result.isSuccess()) {
+            Toast msg = Toast.makeText(getActivity().getApplicationContext(), R.string.remove_success_msg, Toast.LENGTH_LONG);
+            msg.show();
+            if (inDisplayActivity) {
+                // double pane
+                FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
+                transaction.replace(R.id.file_details_container, new FileDetailFragment(null, null)); // empty FileDetailFragment
+                transaction.commit();
+                mContainerActivity.onFileStateChanged();
+            } else {
+                getActivity().finish();
+            }
+                
+        } else {
+            Toast msg = Toast.makeText(getActivity(), R.string.remove_fail_msg, Toast.LENGTH_LONG); 
+            msg.show();
+            if (result.isSslRecoverableException()) {
+                // TODO show the SSL warning dialog
+            }
+        }
+    }
+    
+    private void onRenameFileOperationFinish(RenameFileOperation operation, RemoteOperationResult result) {
+        boolean inDisplayActivity = getActivity() instanceof FileDisplayActivity;
+        getActivity().dismissDialog((inDisplayActivity)? FileDisplayActivity.DIALOG_SHORT_WAIT : FileDetailActivity.DIALOG_SHORT_WAIT);
+        
+        if (result.isSuccess()) {
+            updateFileDetails(((RenameFileOperation)operation).getFile(), mAccount);
+            mContainerActivity.onFileStateChanged();
+            
+        } else {
+            if (result.getCode().equals(ResultCode.INVALID_LOCAL_FILE_NAME)) {
+                Toast msg = Toast.makeText(getActivity(), R.string.rename_local_fail_msg, Toast.LENGTH_LONG); 
+                msg.show();
+                // TODO throw again the new rename dialog
+            } else {
+                Toast msg = Toast.makeText(getActivity(), R.string.rename_server_fail_msg, Toast.LENGTH_LONG); 
+                msg.show();
+                if (result.isSslRecoverableException()) {
+                    // TODO show the SSL warning dialog
                 }
             }
         }