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