view: clean up

parent d5179bc1
...@@ -34,8 +34,8 @@ ...@@ -34,8 +34,8 @@
<object class="GtkEntry" id="name_entry"> <object class="GtkEntry" id="name_entry">
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">True</property> <property name="can_focus">True</property>
<signal name="notify::text" handler="validate_cb" swapped="no" /> <signal name="notify::text" handler="nautilus_view_new_folder_dialog_validate_name" swapped="no" />
<signal name="activate" handler="activated_cb" swapped="no" /> <signal name="activate" handler="nautilus_view_new_folder_dialog_entry_activate" swapped="no" />
</object> </object>
<packing> <packing>
<property name="expand">False</property> <property name="expand">False</property>
......
...@@ -116,7 +116,7 @@ ...@@ -116,7 +116,7 @@
#define TEMPLATE_LIMIT 30 #define TEMPLATE_LIMIT 30
/* Time to show the duplicated folder label */ /* Time to show the duplicated folder label */
#define NEW_FOLDER_DIALOG_ERROR_LABEL_TIMEOUT 500 #define DIALOG_DUPLICATED_NAME_ERROR_LABEL_TIMEOUT 500
enum { enum {
ADD_FILE, ADD_FILE,
...@@ -157,7 +157,7 @@ struct NautilusViewDetails ...@@ -157,7 +157,7 @@ struct NautilusViewDetails
GdkEventButton *pathbar_popup_event; GdkEventButton *pathbar_popup_event;
guint dir_merge_id; guint dir_merge_id;
gint new_folder_name_timeout_id; gint dialog_duplicated_name_label_timeout_id;
gboolean supports_zooming; gboolean supports_zooming;
...@@ -1628,112 +1628,98 @@ context_menu_to_file_operation_position (NautilusView *view) ...@@ -1628,112 +1628,98 @@ context_menu_to_file_operation_position (NautilusView *view)
typedef struct { typedef struct {
NautilusView *view; NautilusView *view;
GtkWidget *dialog; GtkWidget *dialog;
GtkWidget *label; GtkWidget *error_label;
} NewFolderDialogData; GtkWidget *name_entry;
} FileNameDialogData;
static gboolean static gboolean
show_has_folder_label (NewFolderDialogData *data) duplicated_file_label_show (FileNameDialogData *data)
{ {
gtk_label_set_label (GTK_LABEL (data->label), _("A file or folder with that name already exists.")); gtk_label_set_label (GTK_LABEL (data->error_label), _("A file or folder with that name already exists."));
data->view->details->new_folder_name_timeout_id = 0; data->view->details->dialog_duplicated_name_label_timeout_id = 0;
return FALSE; return FALSE;
} }
static void static void
nautilus_view_add_file_dialog_validate_name (GObject *object, nautilus_view_validate_file_name (FileNameDialogData *data)
GParamSpec *params,
gpointer user_data)
{ {
NewFolderDialogData *data = user_data; gboolean duplicated_name;
NautilusFile *file;
NautilusView *view;
GtkWidget *dialog;
gboolean contains_slash; gboolean contains_slash;
gboolean is_empty; gboolean is_empty;
gboolean has_folder; const gchar *name;
GList *file_list, *node; GList *files;
const gchar *text; GList *node;
NautilusFile *file;
g_assert (GTK_IS_ENTRY (object)); g_assert (data != NULL);
g_assert (user_data); g_assert (GTK_IS_ENTRY (data->name_entry));
g_assert (NAUTILUS_IS_VIEW (data->view)); g_assert (GTK_IS_LABEL (data->error_label));
g_assert (GTK_IS_DIALOG (data->dialog)); g_assert (GTK_IS_DIALOG (data->dialog));
g_assert (GTK_IS_LABEL (data->label)); g_assert (NAUTILUS_IS_VIEW (data->view));
text = gtk_entry_get_text (GTK_ENTRY (object)); name = gtk_entry_get_text (GTK_ENTRY (data->name_entry));
dialog = gtk_widget_get_toplevel (GTK_WIDGET (object)); is_empty = strlen (name) == 0;
is_empty = gtk_entry_get_text_length (GTK_ENTRY (object)) == 0; contains_slash = strstr (name, "/") != NULL;
contains_slash = strstr (text, "/") != NULL; duplicated_name = FALSE;
files = nautilus_directory_get_file_list (data->view->details->model);
/* Check whether current location already has for (node = files; node != NULL; node = node->next) {
* a folder with the proposed name.
*/
view = data->view;
has_folder = FALSE;
file_list = nautilus_directory_get_file_list (view->details->model);
for (node = file_list; node != NULL; node = node->next) {
file = node->data; file = node->data;
if (nautilus_file_compare_display_name (file, text) == 0) { if (nautilus_file_compare_display_name (file, name) == 0) {
has_folder = TRUE; duplicated_name = TRUE;
break; break;
} }
} }
nautilus_file_list_free (file_list); nautilus_file_list_free (files);
/* Remove any sources left behind by /* Remove any sources left behind by
* previous calls of this function. * previous calls of this function.
*/ */
if (view->details->new_folder_name_timeout_id > 0) { if (data->view->details->dialog_duplicated_name_label_timeout_id > 0) {
g_source_remove (view->details->new_folder_name_timeout_id); g_source_remove (data->view->details->dialog_duplicated_name_label_timeout_id);
view->details->new_folder_name_timeout_id = 0; data->view->details->dialog_duplicated_name_label_timeout_id = 0;
} }
if (has_folder && !contains_slash && !is_empty) { if (duplicated_name && !contains_slash && !is_empty) {
/* Before showing the dup folder label, clear out the data->view->details->dialog_duplicated_name_label_timeout_id =
* previous message to stop showing any previous errors, g_timeout_add (DIALOG_DUPLICATED_NAME_ERROR_LABEL_TIMEOUT,
* considering that there are other possible error (GSourceFunc)duplicated_file_label_show,
* labels. data);
*/
gtk_label_set_label (GTK_LABEL (data->label), NULL);
view->details->new_folder_name_timeout_id = g_timeout_add (NEW_FOLDER_DIALOG_ERROR_LABEL_TIMEOUT,
(GSourceFunc)show_has_folder_label,
user_data);
} else if (contains_slash) { } else if (contains_slash) {
/* If the user types forbidden characters, gtk_label_set_label (GTK_LABEL (data->error_label), _("Folder names cannot contain \"/\"."));
* immediately shows the error label.
*/
gtk_label_set_label (GTK_LABEL (data->label), _("Folder names cannot contain \"/\"."));
} else { } else {
/* No errors detected, empty the label */ /* No errors detected, empty the label */
gtk_label_set_label (GTK_LABEL (data->label), NULL); gtk_label_set_label (GTK_LABEL (data->error_label), NULL);
} }
gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog), gtk_dialog_set_response_sensitive (GTK_DIALOG (data->dialog),
GTK_RESPONSE_OK, GTK_RESPONSE_OK,
!is_empty && !contains_slash && !has_folder); !is_empty && !contains_slash && !duplicated_name);
} }
static void static void
nautilus_view_add_file_dialog_entry_activate (GtkWidget *entry, nautilus_view_new_folder_dialog_validate_name (GObject *object,
gpointer user_data) GParamSpec *params,
gpointer user_data)
{ {
NewFolderDialogData *data = user_data; nautilus_view_validate_file_name (user_data);
GtkWidget *create_button;
g_assert (GTK_IS_ENTRY (entry)); }
g_assert (user_data);
g_assert (NAUTILUS_IS_VIEW (data->view)); static void
g_assert (GTK_IS_DIALOG (data->dialog)); nautilus_view_new_folder_dialog_entry_activate (GtkWidget *entry,
g_assert (GTK_IS_LABEL (data->label)); gpointer user_data)
{
FileNameDialogData *data;
GtkWidget *create_button;
data = (FileNameDialogData *) user_data;
create_button = gtk_dialog_get_widget_for_response (GTK_DIALOG (data->dialog), create_button = gtk_dialog_get_widget_for_response (GTK_DIALOG (data->dialog),
GTK_RESPONSE_OK); GTK_RESPONSE_OK);
/* nautilus_view_add_file_dialog_validate_content performs /* nautilus_view_new_folder_dialog_validate_name performs
* all the necessary validation, and it's not needed to check * all the necessary validation, and it's not needed to check
* it all again. Checking if the "Create" button is sensitive * it all again. Checking if the "Create" button is sensitive
* is enough. * is enough.
...@@ -1747,9 +1733,9 @@ nautilus_view_add_file_dialog_entry_activate (GtkWidget *entry, ...@@ -1747,9 +1733,9 @@ nautilus_view_add_file_dialog_entry_activate (GtkWidget *entry,
/* Since typos are immediately shown, only /* Since typos are immediately shown, only
* handle name collisions here. * handle name collisions here.
*/ */
if (view->details->new_folder_name_timeout_id > 0) { if (view->details->dialog_duplicated_name_label_timeout_id > 0) {
g_source_remove (view->details->new_folder_name_timeout_id); g_source_remove (view->details->dialog_duplicated_name_label_timeout_id);
show_has_folder_label (data); duplicated_file_label_show (data);
} }
} }
} }
...@@ -1758,7 +1744,7 @@ static void ...@@ -1758,7 +1744,7 @@ static void
nautilus_view_new_folder (NautilusView *directory_view, nautilus_view_new_folder (NautilusView *directory_view,
gboolean with_selection) gboolean with_selection)
{ {
NewFolderDialogData *dialog_data; FileNameDialogData *dialog_data;
GtkBuilder *builder; GtkBuilder *builder;
GtkWindow *dialog; GtkWindow *dialog;
GtkEntry *entry; GtkEntry *entry;
...@@ -1769,20 +1755,21 @@ nautilus_view_new_folder (NautilusView *directory_view, ...@@ -1769,20 +1755,21 @@ nautilus_view_new_folder (NautilusView *directory_view,
entry = GTK_ENTRY (gtk_builder_get_object (builder, "name_entry")); entry = GTK_ENTRY (gtk_builder_get_object (builder, "name_entry"));
/* build up dialog fields */ /* build up dialog fields */
dialog_data = g_new0 (NewFolderDialogData, 1); dialog_data = g_new0 (FileNameDialogData, 1);
dialog_data->view = directory_view; dialog_data->view = directory_view;
dialog_data->dialog = GTK_WIDGET (dialog); dialog_data->dialog = GTK_WIDGET (dialog);
dialog_data->label = GTK_WIDGET (gtk_builder_get_object (builder, "error_label")); dialog_data->error_label = GTK_WIDGET (gtk_builder_get_object (builder, "error_label"));
dialog_data->name_entry = GTK_WIDGET (gtk_builder_get_object (builder, "name_entry"));
gtk_window_set_transient_for (dialog, gtk_window_set_transient_for (dialog,
GTK_WINDOW (nautilus_view_get_window (directory_view))); GTK_WINDOW (nautilus_view_get_window (directory_view)));
/* Connect signals */ /* Connect signals */
gtk_builder_add_callback_symbols (builder, gtk_builder_add_callback_symbols (builder,
"validate_cb", "nautilus_view_new_folder_dialog_validate_name",
G_CALLBACK (nautilus_view_add_file_dialog_validate_name), G_CALLBACK (nautilus_view_new_folder_dialog_validate_name),
"activated_cb", "nautilus_view_new_folder_dialog_entry_activate",
G_CALLBACK (nautilus_view_add_file_dialog_entry_activate), G_CALLBACK (nautilus_view_new_folder_dialog_entry_activate),
NULL); NULL);
gtk_builder_connect_signals (builder, dialog_data); gtk_builder_connect_signals (builder, dialog_data);
...@@ -1821,9 +1808,9 @@ nautilus_view_new_folder (NautilusView *directory_view, ...@@ -1821,9 +1808,9 @@ nautilus_view_new_folder (NautilusView *directory_view,
* message, it should be removed before it gets * message, it should be removed before it gets
* triggered. * triggered.
*/ */
if (directory_view->details->new_folder_name_timeout_id > 0) { if (directory_view->details->dialog_duplicated_name_label_timeout_id > 0) {
g_source_remove (directory_view->details->new_folder_name_timeout_id); g_source_remove (directory_view->details->dialog_duplicated_name_label_timeout_id);
directory_view->details->new_folder_name_timeout_id = 0; directory_view->details->dialog_duplicated_name_label_timeout_id = 0;
} }
g_free (dialog_data); g_free (dialog_data);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment