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