Commit f8b1898a authored by Paolo Bacchilega's avatar Paolo Bacchilega
Browse files

do not allow the user to create catalogs with a / in the name

[bug #652482]
parent 3f81d8a6
......@@ -66,27 +66,44 @@ gth_browser_activate_action_edit_remove_from_catalog (GtkAction *action,
}
void
gth_browser_activate_action_catalog_new (GtkAction *action,
GthBrowser *browser)
static void
catalog_new_dialog_response_cb (GtkWidget *dialog,
int response_id,
gpointer user_data)
{
GthBrowser *browser = user_data;
char *name;
GthFileData *selected_parent;
GFile *parent;
GthFileSource *file_source;
GFile *gio_parent;
char *display_name;
GError *error = NULL;
GFile *gio_file;
name = _gtk_request_dialog_run (GTK_WINDOW (browser),
GTK_DIALOG_MODAL,
_("Enter the catalog name: "),
"",
1024,
GTK_STOCK_CANCEL,
_("C_reate"));
if (name == NULL)
if (response_id != GTK_RESPONSE_OK) {
gtk_widget_destroy (dialog);
return;
}
name = gth_request_dialog_get_normalized_text (GTH_REQUEST_DIALOG (dialog));
if (_g_utf8_all_spaces (name)) {
g_free (name);
gth_request_dialog_set_info_text (GTH_REQUEST_DIALOG (dialog), GTK_MESSAGE_ERROR, _("No name specified"));
return;
}
if (g_regex_match_simple ("/", name, 0, 0)) {
char *message;
message = g_strdup_printf (_("Invalid name. The following characters are not allowed: %s"), "/");
gth_request_dialog_set_info_text (GTH_REQUEST_DIALOG (dialog), GTK_MESSAGE_ERROR, message);
g_free (message);
g_free (name);
return;
}
selected_parent = gth_browser_get_folder_popup_file_data (browser);
if (selected_parent != NULL) {
......@@ -108,34 +125,75 @@ gth_browser_activate_action_catalog_new (GtkAction *action,
file_source = gth_main_get_file_source (parent);
gio_parent = gth_file_source_to_gio_file (file_source, parent);
gio_file = _g_file_create_unique (gio_parent, name, ".catalog", &error);
display_name = g_strconcat (name, ".catalog", NULL);
gio_file = g_file_get_child_for_display_name (gio_parent, display_name, &error);
if (gio_file != NULL) {
GFile *file;
GList *list;
file = gth_catalog_file_from_gio_file (gio_file, NULL);
list = g_list_prepend (NULL, file);
gth_monitor_folder_changed (gth_main_get_default_monitor (),
parent,
list,
GTH_MONITOR_EVENT_CREATED);
GFileOutputStream *stream;
stream = g_file_create (gio_file, G_FILE_CREATE_NONE, NULL, &error);
if (stream != NULL) {
GFile *file;
GList *list;
file = gth_catalog_file_from_gio_file (gio_file, NULL);
list = g_list_prepend (NULL, file);
gth_monitor_folder_changed (gth_main_get_default_monitor (),
parent,
list,
GTH_MONITOR_EVENT_CREATED);
g_list_free (list);
g_object_unref (file);
g_object_unref (stream);
}
g_object_unref (gio_file);
}
g_list_free (list);
g_object_unref (file);
if (error != NULL) {
if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_EXISTS))
gth_request_dialog_set_info_text (GTH_REQUEST_DIALOG (dialog), GTK_MESSAGE_ERROR, _("Name already used"));
else
gth_request_dialog_set_info_text (GTH_REQUEST_DIALOG (dialog), GTK_MESSAGE_ERROR, error->message);
g_clear_error (&error);
}
else
_gtk_error_dialog_from_gerror_show (GTK_WINDOW (browser), _("Could not create the catalog"), &error);
gtk_widget_destroy (dialog);
g_object_unref (gio_file);
g_free (display_name);
g_object_unref (gio_parent);
g_object_unref (file_source);
g_free (name);
}
void
gth_browser_activate_action_catalog_new_library (GtkAction *action,
GthBrowser *browser)
gth_browser_activate_action_catalog_new (GtkAction *action,
GthBrowser *browser)
{
GtkWidget *dialog;
dialog = gth_request_dialog_new (GTK_WINDOW (browser),
GTK_DIALOG_MODAL,
_("New catalog"),
_("Enter the catalog name:"),
GTK_STOCK_CANCEL,
_("C_reate"));
g_signal_connect (dialog,
"response",
G_CALLBACK (catalog_new_dialog_response_cb),
browser);
gtk_widget_show (dialog);
}
static void
new_library_dialog_response_cb (GtkWidget *dialog,
int response_id,
gpointer user_data)
{
GthBrowser *browser = user_data;
char *name;
GthFileData *selected_parent;
GFile *parent;
......@@ -144,15 +202,29 @@ gth_browser_activate_action_catalog_new_library (GtkAction *action,
GError *error = NULL;
GFile *gio_file;
name = _gtk_request_dialog_run (GTK_WINDOW (browser),
GTK_DIALOG_MODAL,
_("Enter the library name: "),
"",
1024,
GTK_STOCK_CANCEL,
_("C_reate"));
if (name == NULL)
if (response_id != GTK_RESPONSE_OK) {
gtk_widget_destroy (dialog);
return;
}
name = gth_request_dialog_get_normalized_text (GTH_REQUEST_DIALOG (dialog));
if (_g_utf8_all_spaces (name)) {
g_free (name);
gth_request_dialog_set_info_text (GTH_REQUEST_DIALOG (dialog), GTK_MESSAGE_ERROR, _("No name specified"));
return;
}
if (g_regex_match_simple ("/", name, 0, 0)) {
char *message;
message = g_strdup_printf (_("Invalid name. The following characters are not allowed: %s"), "/");
gth_request_dialog_set_info_text (GTH_REQUEST_DIALOG (dialog), GTK_MESSAGE_ERROR, message);
g_free (message);
g_free (name);
return;
}
selected_parent = gth_browser_get_folder_popup_file_data (browser);
if (selected_parent != NULL) {
......@@ -174,8 +246,8 @@ gth_browser_activate_action_catalog_new_library (GtkAction *action,
file_source = gth_main_get_file_source (parent);
gio_parent = gth_file_source_to_gio_file (file_source, parent);
gio_file = _g_directory_create_unique (gio_parent, name, "", &error);
if (gio_file != NULL) {
gio_file = g_file_get_child_for_display_name (gio_parent, name, &error);
if (g_file_make_directory (gio_file, NULL, &error)) {
GFile *file;
GList *list;
......@@ -189,12 +261,42 @@ gth_browser_activate_action_catalog_new_library (GtkAction *action,
g_list_free (list);
g_object_unref (file);
}
if (error != NULL) {
if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_EXISTS))
gth_request_dialog_set_info_text (GTH_REQUEST_DIALOG (dialog), GTK_MESSAGE_ERROR, _("Name already used"));
else
gth_request_dialog_set_info_text (GTH_REQUEST_DIALOG (dialog), GTK_MESSAGE_ERROR, error->message);
g_clear_error (&error);
}
else
_gtk_error_dialog_from_gerror_show (GTK_WINDOW (browser), _("Could not create the library"), &error);
gtk_widget_destroy (dialog);
g_object_unref (gio_file);
g_object_unref (gio_parent);
g_object_unref (file_source);
g_free (name);
}
void
gth_browser_activate_action_catalog_new_library (GtkAction *action,
GthBrowser *browser)
{
GtkWidget *dialog;
dialog = gth_request_dialog_new (GTK_WINDOW (browser),
GTK_DIALOG_MODAL,
_("New library"),
_("Enter the library name:"),
GTK_STOCK_CANCEL,
_("C_reate"));
g_signal_connect (dialog,
"response",
G_CALLBACK (new_library_dialog_response_cb),
browser);
gtk_widget_show (dialog);
}
......
......@@ -250,25 +250,42 @@ new_catalog_metadata_ready_cb (GObject *object,
static void
new_catalog_button_clicked_cb (GtkWidget *widget,
DialogData *data)
new_catalog_dialog_response_cb (GtkWidget *dialog,
int response_id,
gpointer user_data)
{
DialogData *data = user_data;
char *name;
GthFileData *selected_parent;
GFile *parent;
GFile *gio_parent;
char *display_name;
GError *error = NULL;
GFile *gio_file;
name = _gtk_request_dialog_run (GTK_WINDOW (data->dialog),
GTK_DIALOG_MODAL,
_("Enter the catalog name: "),
"",
1024,
GTK_STOCK_CANCEL,
_("C_reate"));
if (name == NULL)
if (response_id != GTK_RESPONSE_OK) {
gtk_widget_destroy (dialog);
return;
}
name = gth_request_dialog_get_normalized_text (GTH_REQUEST_DIALOG (dialog));
if (_g_utf8_all_spaces (name)) {
g_free (name);
gth_request_dialog_set_info_text (GTH_REQUEST_DIALOG (dialog), GTK_MESSAGE_ERROR, _("No name specified"));
return;
}
if (g_regex_match_simple ("/", name, 0, 0)) {
char *message;
message = g_strdup_printf (_("Invalid name. The following characters are not allowed: %s"), "/");
gth_request_dialog_set_info_text (GTH_REQUEST_DIALOG (dialog), GTK_MESSAGE_ERROR, message);
g_free (message);
g_free (name);
return;
}
selected_parent = gth_folder_tree_get_selected_or_parent (GTH_FOLDER_TREE (data->source_tree));
if (selected_parent != NULL) {
......@@ -291,33 +308,73 @@ new_catalog_button_clicked_cb (GtkWidget *widget,
_g_object_unref (data->catalog_source);
data->catalog_source = gth_main_get_file_source (parent);
gio_parent = gth_file_source_to_gio_file (data->catalog_source, parent);
gio_file = _g_file_create_unique (gio_parent, name, ".catalog", &error);
display_name = g_strconcat (name, ".catalog", NULL);
gio_file = g_file_get_child_for_display_name (gio_parent, display_name, &error);
if (gio_file != NULL) {
GFile *file;
GFileOutputStream *stream;
_g_object_unref (data->new_catalog);
file = gth_catalog_file_from_gio_file (gio_file, NULL);
data->new_catalog = gth_file_data_new (file, NULL);
gth_file_source_read_metadata (data->catalog_source,
data->new_catalog,
"*",
new_catalog_metadata_ready_cb,
data);
stream = g_file_create (gio_file, G_FILE_CREATE_NONE, NULL, &error);
if (stream != NULL) {
GFile *file;
g_object_unref (file);
_g_object_unref (data->new_catalog);
file = gth_catalog_file_from_gio_file (gio_file, NULL);
data->new_catalog = gth_file_data_new (file, NULL);
gth_file_source_read_metadata (data->catalog_source,
data->new_catalog,
"*",
new_catalog_metadata_ready_cb,
data);
g_object_unref (file);
g_object_unref (stream);
}
g_object_unref (gio_file);
}
if (error != NULL) {
if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_EXISTS))
gth_request_dialog_set_info_text (GTH_REQUEST_DIALOG (dialog), GTK_MESSAGE_ERROR, _("Name already used"));
else
gth_request_dialog_set_info_text (GTH_REQUEST_DIALOG (dialog), GTK_MESSAGE_ERROR, error->message);
g_clear_error (&error);
}
else
_gtk_error_dialog_from_gerror_show (GTK_WINDOW (data->dialog), _("Could not create the catalog"), &error);
gtk_widget_destroy (dialog);
g_object_unref (gio_file);
g_free (display_name);
g_object_unref (gio_parent);
}
static void
new_library_button_clicked_cb (GtkWidget *widget,
new_catalog_button_clicked_cb (GtkWidget *widget,
DialogData *data)
{
GtkWidget *dialog;
dialog = gth_request_dialog_new (GTK_WINDOW (data->dialog),
GTK_DIALOG_MODAL,
_("New catalog"),
_("Enter the catalog name:"),
GTK_STOCK_CANCEL,
_("C_reate"));
g_signal_connect (dialog,
"response",
G_CALLBACK (new_catalog_dialog_response_cb),
data);
gtk_widget_show (dialog);
}
static void
new_library_dialog_response_cb (GtkWidget *dialog,
int response_id,
gpointer user_data)
{
DialogData *data = user_data;
char *name;
GthFileData *selected_parent;
GFile *parent;
......@@ -326,15 +383,29 @@ new_library_button_clicked_cb (GtkWidget *widget,
GError *error = NULL;
GFile *gio_file;
name = _gtk_request_dialog_run (GTK_WINDOW (data->dialog),
GTK_DIALOG_MODAL,
_("Enter the library name: "),
"",
1024,
GTK_STOCK_CANCEL,
_("C_reate"));
if (name == NULL)
if (response_id != GTK_RESPONSE_OK) {
gtk_widget_destroy (dialog);
return;
}
name = gth_request_dialog_get_normalized_text (GTH_REQUEST_DIALOG (dialog));
if (_g_utf8_all_spaces (name)) {
g_free (name);
gth_request_dialog_set_info_text (GTH_REQUEST_DIALOG (dialog), GTK_MESSAGE_ERROR, _("No name specified"));
return;
}
if (g_regex_match_simple ("/", name, 0, 0)) {
char *message;
message = g_strdup_printf (_("Invalid name. The following characters are not allowed: %s"), "/");
gth_request_dialog_set_info_text (GTH_REQUEST_DIALOG (dialog), GTK_MESSAGE_ERROR, message);
g_free (message);
g_free (name);
return;
}
selected_parent = gth_folder_tree_get_selected_or_parent (GTH_FOLDER_TREE (data->source_tree));
if (selected_parent != NULL) {
......@@ -357,7 +428,7 @@ new_library_button_clicked_cb (GtkWidget *widget,
file_source = gth_main_get_file_source (parent);
gio_parent = gth_file_source_to_gio_file (file_source, parent);
gio_file = _g_directory_create_unique (gio_parent, name, "", &error);
if (gio_file != NULL) {
if (g_file_make_directory (gio_file, NULL, &error)) {
GFile *file;
GList *list;
......@@ -371,44 +442,42 @@ new_library_button_clicked_cb (GtkWidget *widget,
g_list_free (list);
g_object_unref (file);
}
if (error != NULL) {
if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_EXISTS))
gth_request_dialog_set_info_text (GTH_REQUEST_DIALOG (dialog), GTK_MESSAGE_ERROR, _("Name already used"));
else
gth_request_dialog_set_info_text (GTH_REQUEST_DIALOG (dialog), GTK_MESSAGE_ERROR, error->message);
g_clear_error (&error);
}
else
_gtk_error_dialog_from_gerror_show (GTK_WINDOW (data->dialog), _("Could not create the library"), &error);
gtk_widget_destroy (dialog);
g_object_unref (gio_file);
g_object_unref (gio_parent);
g_object_unref (file_source);
g_free (name);
}
/*
selected_catalog = gth_folder_tree_get_selected (GTH_FOLDER_TREE (data->source_tree));
parent = get_catalog_parent (selected_catalog->file);
new_library = g_file_get_child_for_display_name (parent, name, &error);
if ((new_library != NULL) && (strchr (display_name, '/') != NULL)) {
error = g_error_new (G_IO_ERROR, G_IO_ERROR_INVALID_FILENAME, _("The name \"%s\" is not valid because it contains the character \"/\". " "Please use a different name."), display_name);
g_object_unref (new_library);
new_library = NULL;
}
if (error == NULL) {
GFile *gio_file;
gio_file = gth_file_source_to_gio_file (data->file_source, new_library);
g_file_make_directory (new_library, NULL, &error);
g_object_unref (gio_file);
}
if (error != NULL)
_gtk_error_dialog_from_gerror_show (GTK_WINDOW (data->dialog), _("Could not create a new library"), &error);
if (new_library != NULL)
g_object_unref (new_library);
g_object_unref (parent);
g_object_unref (selected_catalog);
g_free (name);
static void
new_library_button_clicked_cb (GtkWidget *widget,
DialogData *data)
{
GtkWidget *dialog;
dialog = gth_request_dialog_new (GTK_WINDOW (data->dialog),
GTK_DIALOG_MODAL,
_("New library"),
_("Enter the library name:"),
GTK_STOCK_CANCEL,
_("C_reate"));
g_signal_connect (dialog,
"response",
G_CALLBACK (new_library_dialog_response_cb),
data);
*/
gtk_widget_show (dialog);
}
......
......@@ -90,6 +90,7 @@ PUBLIC_HEADER_FILES = \
gth-pixbuf-saver.h \
gth-preferences.h \
gth-progress-dialog.h \
gth-request-dialog.h \
gth-screensaver.h \
gth-sidebar.h \
gth-statusbar.h \
......@@ -216,6 +217,7 @@ gthumb_SOURCES = \
gth-pixbuf-saver.c \
gth-preferences.c \
gth-progress-dialog.c \
gth-request-dialog.c \
gth-screensaver.c \
gth-sidebar.c \
gth-source-tree.c \
......
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
* GThumb
*
* Copyright (C) 2011 Free Software Foundation, Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <glib/gi18n.h>
#include "gth-request-dialog.h"
#include "gtk-utils.h"
#define REQUEST_ENTRY_WIDTH_IN_CHARS 40
static gpointer gth_request_dialog_parent_class = NULL;
struct _GthRequestDialogPrivate {
GtkWidget *entry;
GtkWidget *infobar;
GtkWidget *info_label;
};
static void
gth_request_dialog_finalize (GObject *base)
{
/* GthRequestDialog *self = (GthRequestDialog *) base; */
G_OBJECT_CLASS (gth_request_dialog_parent_class)->finalize (base);
}
static void
gth_request_dialog_class_init (GthRequestDialogClass *klass)
{
gth_request_dialog_parent_class = g_type_class_peek_parent (klass);
g_type_class_add_private (klass, sizeof (GthRequestDialogPrivate));
G_OBJECT_CLASS (klass)->finalize = gth_request_dialog_finalize;
}
static void
gth_request_dialog_init (GthRequestDialog *self)
{
self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, GTH_TYPE_REQUEST_DIALOG, GthRequestDialogPrivate);
gtk_window_set_title (GTK_WINDOW (self), "");
gtk_window_set_resizable (GTK_WINDOW (self), TRUE);
}
GType
gth_request_dialog_get_type (void)
{
static GType type = 0;
if (type == 0) {
static const GTypeInfo g_define_type_info = {
sizeof (GthRequestDialogClass),
(GBaseInitFunc) NULL,
(GBaseFinalizeFunc) NULL,
(GClassInitFunc) gth_request_dialog_class_init,
(GClassFinalizeFunc) NULL,
NULL,
sizeof (GthRequestDialog),
0,
(GInstanceInitFunc) gth_request_dialog_init,
NULL
};
type = g_type_register_static (GTK_TYPE_DIALOG,
"GthRequestDialog",
&g_define_type_info,
0);
}
return type;
}
static void
_gth_request_dialog_construct (GthRequestDialog *self,
GtkWindow *parent,
GtkDialogFlags flags,
const char *title,