Merge remote-tracking branch 'upstream/develop' into
[pub/Android/ownCloud.git] / src / com / owncloud / android / ui / dialog / CreateFolderDialogFragment.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 import com.actionbarsherlock.app.SherlockDialogFragment;
21 import com.owncloud.android.R;
22 import com.owncloud.android.datamodel.OCFile;
23 import com.owncloud.android.lib.resources.files.FileUtils;
24 import com.owncloud.android.ui.activity.ComponentsGetter;
25
26 import android.app.AlertDialog;
27 import android.app.Dialog;
28 import android.content.DialogInterface;
29 import android.os.Bundle;
30 import android.view.LayoutInflater;
31 import android.view.View;
32 import android.view.WindowManager.LayoutParams;
33 import android.widget.EditText;
34 import android.widget.TextView;
35 import android.widget.Toast;
36
37 /**
38 * Dialog to input the name for a new folder to create.
39 *
40 * Triggers the folder creation when name is confirmed.
41 *
42 * @author David A. Velasco
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 }