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 / CreateFolderDialogFragment.java
1 /**
2 * ownCloud Android client application
3 *
4 * @author David A. Velasco
5 * Copyright (C) 2015 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 import com.actionbarsherlock.app.SherlockDialogFragment;
24 import com.owncloud.android.MainApp;
25 import com.owncloud.android.R;
26 import com.owncloud.android.datamodel.OCFile;
27 import com.owncloud.android.lib.resources.files.FileUtils;
28 import com.owncloud.android.ui.activity.ComponentsGetter;
29
30 import android.app.AlertDialog;
31 import android.app.Dialog;
32 import android.content.DialogInterface;
33 import android.os.Bundle;
34 import android.view.LayoutInflater;
35 import android.view.View;
36 import android.view.WindowManager.LayoutParams;
37 import android.widget.EditText;
38 import android.widget.TextView;
39 import android.widget.Toast;
40
41 /**
42 * Dialog to input the name for a new folder to create.
43 *
44 * Triggers the folder creation when name is confirmed.
45 */
46 public class CreateFolderDialogFragment
47 extends SherlockDialogFragment implements DialogInterface.OnClickListener {
48
49 private static final String ARG_PARENT_FOLDER = "PARENT_FOLDER";
50
51 public static final String CREATE_FOLDER_FRAGMENT = "CREATE_FOLDER_FRAGMENT";
52
53 /**
54 * Public factory method to create new CreateFolderDialogFragment instances.
55 *
56 * @param parentFolder Folder to create
57 * @return Dialog ready to show.
58 */
59 public static CreateFolderDialogFragment newInstance(OCFile parentFolder) {
60 CreateFolderDialogFragment frag = new CreateFolderDialogFragment();
61 Bundle args = new Bundle();
62 args.putParcelable(ARG_PARENT_FOLDER, parentFolder);
63 frag.setArguments(args);
64 return frag;
65
66 }
67
68 private OCFile mParentFolder;
69
70
71 @Override
72 public Dialog onCreateDialog(Bundle savedInstanceState) {
73 mParentFolder = getArguments().getParcelable(ARG_PARENT_FOLDER);
74
75 // Inflate the layout for the dialog
76 LayoutInflater inflater = getSherlockActivity().getLayoutInflater();
77 View v = inflater.inflate(R.layout.edit_box_dialog, null);
78
79 // Setup layout
80 EditText inputText = ((EditText)v.findViewById(R.id.user_input));
81 inputText.setText("");
82 inputText.requestFocus();
83
84 // Build the dialog
85 AlertDialog.Builder builder = new AlertDialog.Builder(getSherlockActivity());
86 builder.setView(v)
87 .setPositiveButton(R.string.common_ok, this)
88 .setNegativeButton(R.string.common_cancel, this)
89 .setTitle(R.string.uploader_info_dirname);
90 Dialog d = builder.create();
91 d.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
92 return d;
93 }
94
95
96 @Override
97 public void onClick(DialogInterface dialog, int which) {
98 if (which == AlertDialog.BUTTON_POSITIVE) {
99 String newFolderName =
100 ((TextView)(getDialog().findViewById(R.id.user_input)))
101 .getText().toString().trim();
102
103 if (newFolderName.length() <= 0) {
104 Toast.makeText(
105 getSherlockActivity(),
106 R.string.filename_empty,
107 Toast.LENGTH_LONG).show();
108 return;
109 }
110
111
112 if (!FileUtils.isValidName(newFolderName,
113 ((ComponentsGetter)getSherlockActivity()).
114 getFileOperationsHelper().isVersionWithForbiddenCharacters())) {
115 Toast.makeText(
116 getSherlockActivity(),
117 R.string.filename_forbidden_characters,
118 Toast.LENGTH_LONG).show();
119 return;
120 }
121
122 String path = mParentFolder.getRemotePath();
123 path += newFolderName + OCFile.PATH_SEPARATOR;
124 ((ComponentsGetter)getSherlockActivity()).
125 getFileOperationsHelper().createFolder(path, false);
126 }
127 }
128
129 }