2  *   ownCloud Android client application 
   4  *   @author David A. Velasco 
   5  *   Copyright (C) 2014 ownCloud Inc. 
   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. 
  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. 
  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/>. 
  21 package com
.owncloud
.android
.ui
.dialog
; 
  24  *  Dialog to input a new name for an {@link OCFile} being renamed.   
  26  *  Triggers the rename operation.  
  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
; 
  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
; 
  47  *  Dialog to input a new name for a file or folder to rename.   
  49  *  Triggers the rename operation when name is confirmed. 
  51 public class RenameFileDialogFragment
 
  52         extends DialogFragment 
implements DialogInterface
.OnClickListener 
{ 
  54     private static final String ARG_TARGET_FILE 
= "TARGET_FILE"; 
  57      * Public factory method to create new RenameFileDialogFragment instances. 
  59      * @param file            File to rename. 
  60      * @return                Dialog ready to show. 
  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
); 
  71     private OCFile mTargetFile
; 
  74     public Dialog 
onCreateDialog(Bundle savedInstanceState
) { 
  75         mTargetFile 
= getArguments().getParcelable(ARG_TARGET_FILE
); 
  77         // Inflate the layout for the dialog 
  78         LayoutInflater inflater 
= getActivity().getLayoutInflater(); 
  79         View v 
= inflater
.inflate(R
.layout
.edit_box_dialog
, null
); 
  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
)); 
  93         inputText
.requestFocus(); 
  96         AlertDialog
.Builder builder 
= new AlertDialog
.Builder(getActivity()); 
  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
); 
 108     public void onClick(DialogInterface dialog
, int which
) { 
 109         if (which 
== AlertDialog
.BUTTON_POSITIVE
) { 
 111                 ((TextView
)(getDialog().findViewById(R
.id
.user_input
))) 
 112                     .getText().toString().trim(); 
 114             if (newFileName
.length() <= 0) { 
 117                         R
.string
.filename_empty
,  
 118                         Toast
.LENGTH_LONG
).show(); 
 122             boolean serverWithForbiddenChars 
= ((ComponentsGetter
)getActivity()). 
 123                     getFileOperationsHelper().isVersionWithForbiddenCharacters(); 
 125             if (!FileUtils
.isValidName(newFileName
, serverWithForbiddenChars
)) { 
 127                 if (serverWithForbiddenChars
) { 
 128                     messageId 
= R
.string
.filename_forbidden_charaters_from_server
; 
 130                     messageId 
= R
.string
.filename_forbidden_characters
; 
 132                 Toast
.makeText(getActivity(), messageId
, Toast
.LENGTH_LONG
).show(); 
 136             ((ComponentsGetter
)getActivity()).getFileOperationsHelper(). 
 137                     renameFile(mTargetFile
, newFileName
);