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