Merge branch 'develop' into rename_with_special_character
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / dialog / RenameFileDialogFragment.java
1 /* ownCloud Android client application
2 * Copyright (C) 2014 ownCloud Inc.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 */
17
18 package com.owncloud.android.ui.dialog;
19
20 /**
21 * Dialog to input a new name for an {@link OCFile} being renamed.
22 *
23 * Triggers the rename operation.
24 */
25 import android.app.AlertDialog;
26 import android.app.Dialog;
27 import android.content.DialogInterface;
28 import android.os.Bundle;
29 import android.view.LayoutInflater;
30 import android.view.View;
31 import android.view.WindowManager.LayoutParams;
32 import android.widget.EditText;
33 import android.widget.TextView;
34 import android.widget.Toast;
35
36 import com.actionbarsherlock.app.SherlockDialogFragment;
37 import com.owncloud.android.R;
38 import com.owncloud.android.datamodel.OCFile;
39 import com.owncloud.android.lib.resources.files.FileUtils;
40 import com.owncloud.android.ui.activity.ComponentsGetter;
41
42
43 /**
44 * Dialog to input a new name for a file or folder to rename.
45 *
46 * Triggers the rename operation when name is confirmed.
47 *
48 * @author David A. Velasco
49 */
50 public class RenameFileDialogFragment
51 extends SherlockDialogFragment implements DialogInterface.OnClickListener {
52
53 private static final String ARG_TARGET_FILE = "TARGET_FILE";
54
55 /**
56 * Public factory method to create new RenameFileDialogFragment instances.
57 *
58 * @param file File to rename.
59 * @return Dialog ready to show.
60 */
61 public static RenameFileDialogFragment newInstance(OCFile file) {
62 RenameFileDialogFragment frag = new RenameFileDialogFragment();
63 Bundle args = new Bundle();
64 args.putParcelable(ARG_TARGET_FILE, file);
65 frag.setArguments(args);
66 return frag;
67
68 }
69
70 private OCFile mTargetFile;
71
72 @Override
73 public Dialog onCreateDialog(Bundle savedInstanceState) {
74 mTargetFile = getArguments().getParcelable(ARG_TARGET_FILE);
75
76 // Inflate the layout for the dialog
77 LayoutInflater inflater = getSherlockActivity().getLayoutInflater();
78 View v = inflater.inflate(R.layout.edit_box_dialog, null);
79
80 // Setup layout
81 String currentName = mTargetFile.getFileName();
82 EditText inputText = ((EditText)v.findViewById(R.id.user_input));
83 inputText.setText(currentName);
84 int selectionStart = 0;
85 int extensionStart = mTargetFile.isFolder() ? -1 : currentName.lastIndexOf(".");
86 int selectionEnd = (extensionStart >= 0) ? extensionStart : currentName.length();
87 if (selectionStart >= 0 && selectionEnd >= 0) {
88 inputText.setSelection(
89 Math.min(selectionStart, selectionEnd),
90 Math.max(selectionStart, selectionEnd));
91 }
92 inputText.requestFocus();
93
94 // Build the dialog
95 AlertDialog.Builder builder = new AlertDialog.Builder(getSherlockActivity());
96 builder.setView(v)
97 .setPositiveButton(R.string.common_ok, this)
98 .setNegativeButton(R.string.common_cancel, this)
99 .setTitle(R.string.rename_dialog_title);
100 Dialog d = builder.create();
101 d.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
102 return d;
103 }
104
105
106 @Override
107 public void onClick(DialogInterface dialog, int which) {
108 if (which == AlertDialog.BUTTON_POSITIVE) {
109 String newFileName =
110 ((TextView)(getDialog().findViewById(R.id.user_input)))
111 .getText().toString().trim();
112
113 if (newFileName.length() <= 0) {
114 Toast.makeText(
115 getSherlockActivity(),
116 R.string.filename_empty,
117 Toast.LENGTH_LONG).show();
118 return;
119 }
120
121 if (!FileUtils.isValidName(newFileName)) {
122 Toast.makeText(
123 getSherlockActivity(),
124 R.string.filename_forbidden_characters,
125 Toast.LENGTH_LONG).show();
126 return;
127 }
128
129 ((ComponentsGetter)getSherlockActivity()).getFileOperationsHelper().renameFile(mTargetFile, newFileName);
130 }
131 }
132 }