medit/moo/mooutils/moofiledialog.c

888 lines
26 KiB
C
Raw Normal View History

2006-04-13 05:02:33 -05:00
/*
2007-06-24 12:56:20 -05:00
* moofiledialog.c
2006-04-13 05:02:33 -05:00
*
2010-12-21 20:15:45 -08:00
* Copyright (C) 2004-2010 by Yevgen Muntyan <emuntyan@users.sourceforge.net>
2006-04-13 05:02:33 -05:00
*
* This file is part of medit. medit is free software; you can
* redistribute it and/or modify it under the terms of the
* GNU Lesser General Public License as published by the
* Free Software Foundation; either version 2.1 of the License,
* or (at your option) any later version.
2006-04-13 05:02:33 -05:00
*
* You should have received a copy of the GNU Lesser General Public
* License along with medit. If not, see <http://www.gnu.org/licenses/>.
2006-04-13 05:02:33 -05:00
*/
2010-12-10 01:06:58 -08:00
/**
2010-12-10 03:11:38 -08:00
* class:MooFileDialog: (parent GObject) (constructable)
2010-12-10 01:06:58 -08:00
**/
#include "config.h"
2006-04-13 05:02:33 -05:00
#include "mooutils/moodialogs.h"
#include "mooutils/mooprefs.h"
#include "mooutils/mooutils-misc.h"
2006-12-17 03:23:19 -06:00
#include "mooutils/mooencodings.h"
2007-09-07 23:59:37 -05:00
#include "mooutils/moohelp.h"
#include "mooutils/moofiltermgr.h"
#include "mooutils/mooutils-enums.h"
2008-10-03 01:12:34 -05:00
#include "mooutils/mooi18n.h"
2008-05-22 22:40:19 -05:00
#include "marshals.h"
#include <gtk/gtk.h>
#include <string.h>
2006-04-13 05:02:33 -05:00
2010-12-08 01:25:34 -08:00
struct _MooFileDialogPrivate {
2006-04-13 05:02:33 -05:00
gboolean multiple;
char *title;
2008-05-21 15:37:57 -05:00
char *current_dir;
2006-04-13 05:02:33 -05:00
char *name;
MooFileDialogType type;
GtkWidget *parent;
char *filter_mgr_id;
2006-12-17 03:23:19 -06:00
gboolean enable_encodings;
2006-04-13 05:02:33 -05:00
2008-05-21 15:37:57 -05:00
char *size_prefs_key;
2007-09-07 23:59:37 -05:00
char *help_id;
2008-05-21 15:37:57 -05:00
GSList *uris;
char *uri;
2006-12-17 03:23:19 -06:00
char *encoding;
2008-05-21 15:37:57 -05:00
GtkWidget *extra_widget;
MooFileDialogCheckNameFunc check_name_func;
gpointer check_name_func_data;
GDestroyNotify check_name_func_data_notify;
2006-04-13 05:02:33 -05:00
};
G_DEFINE_TYPE (MooFileDialog, moo_file_dialog, G_TYPE_OBJECT)
enum {
PROP_0,
PROP_MULTIPLE,
PROP_TITLE,
2008-05-21 15:37:57 -05:00
PROP_CURRENT_FOLDER_URI,
2006-04-13 05:02:33 -05:00
PROP_NAME,
PROP_TYPE,
PROP_PARENT,
2006-12-17 03:23:19 -06:00
PROP_FILTER_MGR_ID,
PROP_ENABLE_ENCODINGS
2006-04-13 05:02:33 -05:00
};
static char *
get_string_maybe_stock (const char *string)
{
GtkStockItem item;
char *underscore;
char *copy;
if (!gtk_stock_lookup (string, &item))
return g_strdup (string);
if (!(underscore = strchr (item.label, '_')))
return g_strdup (item.label);
copy = g_strdup (item.label);
memmove (copy + (underscore - item.label),
copy + (underscore - item.label) + 1,
strlen (copy + (underscore - item.label) + 1) + 1);
return copy;
}
2006-04-13 05:02:33 -05:00
static void
moo_file_dialog_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
char *tmp;
2006-04-13 05:02:33 -05:00
MooFileDialog *dialog = MOO_FILE_DIALOG (object);
switch (prop_id)
{
case PROP_MULTIPLE:
dialog->priv->multiple = g_value_get_boolean (value) != 0;
g_object_notify (object, "multiple");
break;
case PROP_TITLE:
tmp = dialog->priv->title;
dialog->priv->title = get_string_maybe_stock (g_value_get_string (value));
g_free (tmp);
2006-04-13 05:02:33 -05:00
g_object_notify (object, "title");
break;
2008-05-21 15:37:57 -05:00
case PROP_CURRENT_FOLDER_URI:
MOO_ASSIGN_STRING (dialog->priv->current_dir, g_value_get_string (value));
g_object_notify (object, "current-folder-uri");
2006-04-13 05:02:33 -05:00
break;
case PROP_NAME:
2007-12-19 14:02:42 -06:00
MOO_ASSIGN_STRING (dialog->priv->name, g_value_get_string (value));
2006-04-13 05:02:33 -05:00
g_object_notify (object, "name");
break;
case PROP_TYPE:
2010-08-12 18:17:00 -07:00
dialog->priv->type = (MooFileDialogType) g_value_get_enum (value);
2006-04-13 05:02:33 -05:00
g_object_notify (object, "type");
break;
case PROP_PARENT:
2010-08-12 18:17:00 -07:00
dialog->priv->parent = GTK_WIDGET (g_value_get_object (value));
2006-04-13 05:02:33 -05:00
g_object_notify (object, "parent");
break;
case PROP_FILTER_MGR_ID:
g_free (dialog->priv->filter_mgr_id);
dialog->priv->filter_mgr_id = g_value_dup_string (value);
g_object_notify (object, "filter-mgr-id");
break;
2006-12-17 03:23:19 -06:00
case PROP_ENABLE_ENCODINGS:
dialog->priv->enable_encodings = g_value_get_boolean (value);
g_object_notify (object, "enable-encodings");
break;
2006-04-13 05:02:33 -05:00
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
moo_file_dialog_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
MooFileDialog *dialog = MOO_FILE_DIALOG (object);
switch (prop_id)
{
case PROP_MULTIPLE:
g_value_set_boolean (value, dialog->priv->multiple != 0);
break;
case PROP_TITLE:
g_value_set_string (value, dialog->priv->title);
break;
2008-05-21 15:37:57 -05:00
case PROP_CURRENT_FOLDER_URI:
g_value_set_string (value, dialog->priv->current_dir);
2006-04-13 05:02:33 -05:00
break;
case PROP_NAME:
g_value_set_string (value, dialog->priv->name);
break;
case PROP_TYPE:
g_value_set_enum (value, dialog->priv->type);
break;
case PROP_PARENT:
g_value_set_object (value, dialog->priv->parent);
break;
case PROP_FILTER_MGR_ID:
g_value_set_string (value, dialog->priv->filter_mgr_id);
break;
2006-12-17 03:23:19 -06:00
case PROP_ENABLE_ENCODINGS:
g_value_set_boolean (value, dialog->priv->enable_encodings);
break;
2006-04-13 05:02:33 -05:00
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
string_slist_free (GSList *list)
{
g_slist_foreach (list, (GFunc) g_free, NULL);
g_slist_free (list);
}
static void
moo_file_dialog_finalize (GObject *object)
{
MooFileDialog *dialog = MOO_FILE_DIALOG (object);
2008-05-21 15:37:57 -05:00
g_free (dialog->priv->current_dir);
2006-04-13 05:02:33 -05:00
g_free (dialog->priv->title);
g_free (dialog->priv->name);
g_free (dialog->priv->filter_mgr_id);
2008-05-21 15:37:57 -05:00
g_free (dialog->priv->uri);
2006-12-17 03:23:19 -06:00
g_free (dialog->priv->encoding);
2007-09-07 23:59:37 -05:00
g_free (dialog->priv->help_id);
2008-05-21 15:37:57 -05:00
g_free (dialog->priv->size_prefs_key);
string_slist_free (dialog->priv->uris);
2006-04-13 05:02:33 -05:00
2008-05-21 15:37:57 -05:00
if (dialog->priv->check_name_func_data_notify)
dialog->priv->check_name_func_data_notify (dialog->priv->check_name_func_data);
if (dialog->priv->extra_widget)
g_object_unref (dialog->priv->extra_widget);
2006-04-13 05:02:33 -05:00
g_free (dialog->priv);
G_OBJECT_CLASS(moo_file_dialog_parent_class)->finalize (object);
}
static void
moo_file_dialog_init (MooFileDialog *dialog)
{
dialog->priv = g_new0 (MooFileDialogPrivate, 1);
2006-06-08 10:50:18 -05:00
dialog->priv->type = MOO_FILE_DIALOG_OPEN;
2006-04-13 05:02:33 -05:00
}
static void
moo_file_dialog_class_init (MooFileDialogClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
gobject_class->finalize = moo_file_dialog_finalize;
gobject_class->set_property = moo_file_dialog_set_property;
gobject_class->get_property = moo_file_dialog_get_property;
2008-05-21 15:37:57 -05:00
g_object_class_install_property (gobject_class, PROP_MULTIPLE,
g_param_spec_boolean ("multiple", "multiple", "multiple",
2010-08-12 18:17:00 -07:00
FALSE, (GParamFlags) G_PARAM_READWRITE));
2008-05-21 15:37:57 -05:00
g_object_class_install_property (gobject_class, PROP_ENABLE_ENCODINGS,
g_param_spec_boolean ("enable-encodings", "enable-encodings", "enable-encodings",
2010-08-12 18:17:00 -07:00
FALSE, (GParamFlags) G_PARAM_READWRITE));
2008-05-21 15:37:57 -05:00
g_object_class_install_property (gobject_class, PROP_TITLE,
g_param_spec_string ("title", "title", "title",
2010-08-12 18:17:00 -07:00
NULL, (GParamFlags) G_PARAM_READWRITE));
2008-05-21 15:37:57 -05:00
g_object_class_install_property (gobject_class, PROP_CURRENT_FOLDER_URI,
g_param_spec_string ("current-folder-uri", "current-folder-uri",
2010-08-12 18:17:00 -07:00
"current-folder-uri", NULL,
(GParamFlags) G_PARAM_READWRITE));
2008-05-21 15:37:57 -05:00
g_object_class_install_property (gobject_class, PROP_NAME,
2010-08-12 18:17:00 -07:00
g_param_spec_string ("name", "name", "name", NULL,
(GParamFlags) G_PARAM_READWRITE));
2008-05-21 15:37:57 -05:00
g_object_class_install_property (gobject_class, PROP_PARENT,
g_param_spec_object ("parent", "parent", "parent",
2010-08-12 18:17:00 -07:00
GTK_TYPE_WIDGET,
(GParamFlags) G_PARAM_READWRITE));
2008-05-21 15:37:57 -05:00
g_object_class_install_property (gobject_class, PROP_TYPE,
g_param_spec_enum ("type", "type", "type",
MOO_TYPE_FILE_DIALOG_TYPE, MOO_FILE_DIALOG_OPEN,
2010-08-12 18:17:00 -07:00
(GParamFlags) G_PARAM_READWRITE));
2008-05-21 15:37:57 -05:00
g_object_class_install_property (gobject_class, PROP_FILTER_MGR_ID,
g_param_spec_string ("filter-mgr-id", "filter-mgr-id", "filter-mgr-id",
2010-08-12 18:17:00 -07:00
NULL, (GParamFlags) G_PARAM_READWRITE));
2006-04-13 05:02:33 -05:00
}
inline static
GtkWidget *file_chooser_dialog_new (const char *title,
GtkFileChooserAction action,
const char *okbtn,
2008-05-21 15:37:57 -05:00
const char *start_dir_uri,
2007-09-07 23:59:37 -05:00
const char *help_id)
2006-04-13 05:02:33 -05:00
{
GtkWidget *dialog =
gtk_file_chooser_dialog_new (title, NULL, action,
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
okbtn, GTK_RESPONSE_OK,
NULL);
gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
GTK_RESPONSE_OK,
GTK_RESPONSE_CANCEL,
-1);
2008-05-21 15:37:57 -05:00
if (start_dir_uri)
gtk_file_chooser_set_current_folder_uri (GTK_FILE_CHOOSER(dialog),
start_dir_uri);
2007-09-07 23:59:37 -05:00
if (help_id)
{
moo_help_set_id (dialog, help_id);
moo_help_connect_keys (dialog);
gtk_dialog_add_button (GTK_DIALOG (dialog), GTK_STOCK_HELP, GTK_RESPONSE_HELP);
}
2006-04-13 05:02:33 -05:00
return dialog;
}
#define file_chooser_set_select_multiple(dialog,multiple) \
gtk_file_chooser_set_select_multiple (GTK_FILE_CHOOSER (dialog), multiple)
#define file_chooser_set_name(dialog, name) \
gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (dialog), name)
const char *
moo_file_dialog (GtkWidget *parent,
MooFileDialogType type,
const char *start_name,
2006-04-13 05:02:33 -05:00
const char *title,
const char *start_dir)
{
static char *filename;
MooFileDialog *dialog;
2010-11-23 21:54:39 -08:00
GFile *start;
GFile *file;
2006-04-13 05:02:33 -05:00
2010-11-23 21:54:39 -08:00
start = start_dir ? g_file_new_for_path (start_dir) : NULL;
dialog = moo_file_dialog_new (type, parent, FALSE, title, start, start_name);
2006-04-13 05:02:33 -05:00
g_return_val_if_fail (dialog != NULL, NULL);
moo_file_dialog_run (dialog);
2010-11-23 21:54:39 -08:00
file = moo_file_dialog_get_file (dialog);
2006-04-13 05:02:33 -05:00
g_free (filename);
2010-11-23 21:54:39 -08:00
filename = file ? g_file_get_path (file) : NULL;
2006-04-13 05:02:33 -05:00
2010-11-23 21:54:39 -08:00
g_object_unref (file);
g_object_unref (start);
2006-04-13 05:02:33 -05:00
g_object_unref (dialog);
return filename;
}
static GtkWidget *
moo_file_dialog_create_widget (MooFileDialog *dialog)
{
GtkFileChooserAction chooser_action;
GtkWidget *widget = NULL;
2006-12-17 03:23:19 -06:00
GtkWidget *extra_box = NULL;
2006-04-13 05:02:33 -05:00
switch (dialog->priv->type)
{
2006-06-08 10:50:18 -05:00
case MOO_FILE_DIALOG_OPEN:
case MOO_FILE_DIALOG_OPEN_ANY:
case MOO_FILE_DIALOG_OPEN_DIR:
if (dialog->priv->type == MOO_FILE_DIALOG_OPEN_DIR)
2006-04-13 05:02:33 -05:00
chooser_action = GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER;
else
chooser_action = GTK_FILE_CHOOSER_ACTION_OPEN;
widget = file_chooser_dialog_new (dialog->priv->title,
chooser_action,
GTK_STOCK_OPEN,
2008-05-21 15:37:57 -05:00
dialog->priv->current_dir,
2007-09-07 23:59:37 -05:00
dialog->priv->help_id);
2006-04-13 05:02:33 -05:00
file_chooser_set_select_multiple (widget, dialog->priv->multiple);
break;
2006-06-08 10:50:18 -05:00
case MOO_FILE_DIALOG_SAVE:
2006-04-13 05:02:33 -05:00
chooser_action = GTK_FILE_CHOOSER_ACTION_SAVE;
widget = file_chooser_dialog_new (dialog->priv->title,
chooser_action,
GTK_STOCK_SAVE,
2008-05-21 15:37:57 -05:00
dialog->priv->current_dir,
2007-09-07 23:59:37 -05:00
dialog->priv->help_id);
2006-04-13 05:02:33 -05:00
if (dialog->priv->name)
file_chooser_set_name (widget, dialog->priv->name);
break;
default:
g_critical ("incorrect dialog type specified");
2006-04-13 05:02:33 -05:00
return NULL;
}
gtk_dialog_set_default_response (GTK_DIALOG (widget), GTK_RESPONSE_OK);
if (dialog->priv->filter_mgr_id || dialog->priv->enable_encodings)
2006-12-17 03:23:19 -06:00
{
extra_box = gtk_hbox_new (FALSE, 0);
gtk_file_chooser_set_extra_widget (GTK_FILE_CHOOSER (widget), extra_box);
gtk_widget_show (extra_box);
}
if (dialog->priv->filter_mgr_id)
moo_filter_mgr_attach (moo_filter_mgr_default (),
2006-12-17 03:23:19 -06:00
GTK_FILE_CHOOSER (widget), extra_box,
2006-04-13 05:02:33 -05:00
dialog->priv->filter_mgr_id);
2006-12-17 03:23:19 -06:00
if (dialog->priv->enable_encodings)
_moo_encodings_attach_combo (widget, extra_box,
dialog->priv->type == MOO_FILE_DIALOG_SAVE,
dialog->priv->encoding);
2008-05-21 15:37:57 -05:00
if (dialog->priv->extra_widget)
gtk_box_pack_start (GTK_BOX (extra_box), dialog->priv->extra_widget,
FALSE, FALSE, 0);
if (dialog->priv->size_prefs_key)
_moo_window_set_remember_size (GTK_WINDOW (widget),
dialog->priv->size_prefs_key, -1, -1, TRUE);
2006-11-19 12:36:22 -06:00
if (dialog->priv->parent)
moo_window_set_parent (widget, dialog->priv->parent);
2006-04-13 05:02:33 -05:00
return widget;
}
2008-05-21 15:37:57 -05:00
static char **
string_slist_to_strv (GSList *list)
2006-04-13 05:02:33 -05:00
{
2008-05-21 15:37:57 -05:00
guint len, i;
char **uris;
if (!list)
return NULL;
len = g_slist_length (list);
uris = g_new (char*, len + 1);
2006-04-13 05:02:33 -05:00
2008-05-21 15:37:57 -05:00
for (i = 0; i < len; i++, list = list->next)
2010-08-12 18:17:00 -07:00
uris[i] = g_strdup ((const char*) list->data);
2006-04-13 05:02:33 -05:00
2008-05-21 15:37:57 -05:00
uris[i] = NULL;
return uris;
}
2010-11-23 21:54:39 -08:00
static MooFileArray *
uri_list_to_files (GSList *list)
2008-05-21 15:37:57 -05:00
{
2010-11-23 21:54:39 -08:00
MooFileArray *flocs;
2008-05-21 15:37:57 -05:00
if (!list)
return NULL;
2010-11-23 21:54:39 -08:00
flocs = moo_file_array_new ();
2008-05-21 15:37:57 -05:00
2010-11-23 21:54:39 -08:00
while (list)
2010-11-25 02:12:08 -08:00
{
2010-11-23 21:54:39 -08:00
moo_file_array_take (flocs, g_file_new_for_uri (list->data));
2010-11-25 02:12:08 -08:00
list = list->next;
}
2008-05-21 15:37:57 -05:00
2010-11-23 21:54:39 -08:00
return flocs;
2006-04-13 05:02:33 -05:00
}
static void
2008-05-21 15:37:57 -05:00
set_uri (MooFileDialog *dialog,
char *uri)
2006-04-13 05:02:33 -05:00
{
2008-05-21 15:37:57 -05:00
g_free (dialog->priv->uri);
dialog->priv->uri = uri;
2006-04-13 05:02:33 -05:00
}
static void
2008-05-21 15:37:57 -05:00
set_uris (MooFileDialog *dialog,
GSList *uris)
2006-04-13 05:02:33 -05:00
{
2008-05-21 15:37:57 -05:00
string_slist_free (dialog->priv->uris);
dialog->priv->uris = uris;
2006-04-13 05:02:33 -05:00
}
2006-12-17 03:23:19 -06:00
static void
set_encoding (MooFileDialog *dialog,
const char *encoding)
{
2007-12-19 14:02:42 -06:00
MOO_ASSIGN_STRING (dialog->priv->encoding, encoding);
2006-12-17 03:23:19 -06:00
}
void
moo_file_dialog_set_encoding (MooFileDialog *dialog,
const char *encoding)
{
g_return_if_fail (MOO_IS_FILE_DIALOG (dialog));
set_encoding (dialog, encoding);
}
static gboolean
2008-05-21 15:37:57 -05:00
uri_is_valid (G_GNUC_UNUSED const char *uri,
G_GNUC_UNUSED char **msg)
{
#ifndef __WIN32__
return TRUE;
#else
struct name {
const char *name;
guint len;
} names[] = {
{ "con", 3 }, { "aux", 3 }, { "prn", 3 }, { "nul", 3 },
{ "com1", 4 }, { "com2", 4 }, { "com3", 4 }, { "com4", 4 },
{ "lpt1", 4 }, { "lpt2", 4 }, { "lpt3", 4 }
};
guint i;
2008-05-21 15:37:57 -05:00
char *filename;
const char *basename;
gboolean invalid = FALSE;
2008-05-21 15:37:57 -05:00
if (!(filename = g_filename_from_uri (uri, NULL, NULL)))
return TRUE;
basename = strrchr (filename, '\\');
if (!basename)
basename = strrchr (filename, '/');
if (!basename)
basename = filename;
else
basename += 1;
for (i = 0; i < G_N_ELEMENTS (names); ++i)
{
if (!g_ascii_strncasecmp (basename, names[i].name, names[i].len))
{
if (!basename[names[i].len] || basename[names[i].len] == '.')
invalid = TRUE;
break;
}
}
if (invalid)
{
char *base = g_path_get_basename (filename);
*msg = g_strdup_printf ("Filename '%s' is a reserved device name.\n"
"Please choose another name", base);
g_free (base);
}
2008-05-21 15:37:57 -05:00
g_free (filename);
return !invalid;
#endif
}
2006-04-13 05:02:33 -05:00
2008-05-21 15:37:57 -05:00
static char *
get_uri_for_saving (MooFileDialog *dialog,
GtkWidget *filechooser)
{
char *uri;
char *real;
if (!(uri = gtk_file_chooser_get_uri (GTK_FILE_CHOOSER (filechooser))) ||
!dialog->priv->check_name_func)
return uri;
real = dialog->priv->check_name_func (dialog, uri,
dialog->priv->check_name_func_data);
g_free (uri);
return real;
}
2006-04-13 05:02:33 -05:00
gboolean
moo_file_dialog_run (MooFileDialog *dialog)
{
GtkWidget *filechooser;
gboolean result = FALSE;
2007-09-07 23:59:37 -05:00
int response;
2006-04-13 05:02:33 -05:00
g_return_val_if_fail (MOO_IS_FILE_DIALOG (dialog), FALSE);
2008-05-21 15:37:57 -05:00
set_uri (dialog, NULL);
set_uris (dialog, NULL);
2006-04-13 05:02:33 -05:00
filechooser = moo_file_dialog_create_widget (dialog);
switch (dialog->priv->type)
{
2006-06-08 10:50:18 -05:00
case MOO_FILE_DIALOG_OPEN:
case MOO_FILE_DIALOG_OPEN_ANY:
case MOO_FILE_DIALOG_OPEN_DIR:
2007-09-07 23:59:37 -05:00
while ((response = gtk_dialog_run (GTK_DIALOG (filechooser))) == GTK_RESPONSE_HELP)
moo_help_open (filechooser);
if (response == GTK_RESPONSE_OK)
2006-04-13 05:02:33 -05:00
{
2008-05-21 15:37:57 -05:00
set_uri (dialog, gtk_file_chooser_get_uri (GTK_FILE_CHOOSER (filechooser)));
2006-04-13 05:02:33 -05:00
if (dialog->priv->multiple)
2008-05-21 15:37:57 -05:00
set_uris (dialog, gtk_file_chooser_get_uris (GTK_FILE_CHOOSER (filechooser)));
2006-04-13 05:02:33 -05:00
result = TRUE;
}
goto out;
2006-06-08 10:50:18 -05:00
case MOO_FILE_DIALOG_SAVE:
2006-04-13 05:02:33 -05:00
while (TRUE)
{
2008-05-21 15:37:57 -05:00
char *msg;
char *filename;
char *uri;
2007-09-07 23:59:37 -05:00
response = gtk_dialog_run (GTK_DIALOG (filechooser));
if (response == GTK_RESPONSE_HELP)
{
moo_help_open (filechooser);
continue;
}
2008-05-21 15:37:57 -05:00
if (response != GTK_RESPONSE_OK)
goto out;
2008-05-21 15:37:57 -05:00
uri = get_uri_for_saving (dialog, filechooser);
2006-04-13 05:02:33 -05:00
2008-05-21 15:37:57 -05:00
if (!uri_is_valid (uri, &msg))
{
2011-01-01 17:41:01 -08:00
moo_error_dialog (msg, NULL, filechooser);
2008-05-21 15:37:57 -05:00
g_free (uri);
g_free (msg);
continue;
}
if ((filename = g_filename_from_uri (uri, NULL, NULL)))
{
if (g_file_test (filename, G_FILE_TEST_EXISTS) &&
!g_file_test (filename, G_FILE_TEST_IS_REGULAR))
2006-04-13 05:02:33 -05:00
{
2011-01-01 17:41:01 -08:00
moo_error_dialog (_("Selected file is not a regular file"),
NULL,
filechooser);
2006-04-13 05:02:33 -05:00
g_free (filename);
2008-05-21 15:37:57 -05:00
g_free (uri);
continue;
2006-04-13 05:02:33 -05:00
}
2008-05-21 15:37:57 -05:00
if (g_file_test (filename, G_FILE_TEST_EXISTS) &&
g_file_test (filename, G_FILE_TEST_IS_REGULAR))
2006-04-13 05:02:33 -05:00
{
char *basename = g_path_get_basename (filename);
char *dirname = g_path_get_dirname (filename);
char *display_name = g_filename_display_name (basename);
char *display_dirname = g_filename_display_name (dirname);
gboolean overwrite;
2011-01-01 17:41:01 -08:00
overwrite = moo_overwrite_file_dialog (display_name, display_dirname, filechooser);
2006-04-13 05:02:33 -05:00
g_free (basename);
g_free (dirname);
g_free (display_name);
g_free (display_dirname);
2008-05-21 15:37:57 -05:00
if (!overwrite)
2006-04-13 05:02:33 -05:00
{
2008-05-21 15:37:57 -05:00
g_free (filename);
g_free (uri);
continue;
2006-04-13 05:02:33 -05:00
}
}
}
2008-05-21 15:37:57 -05:00
set_uri (dialog, uri);
g_free (filename);
result = TRUE;
goto out;
2006-04-13 05:02:33 -05:00
}
default:
g_critical ("incorrect dialog type specified");
2006-04-13 05:02:33 -05:00
}
out:
2006-12-17 03:23:19 -06:00
if (result && dialog->priv->enable_encodings)
set_encoding (dialog, _moo_encodings_combo_get (filechooser,
dialog->priv->type == MOO_FILE_DIALOG_SAVE));
2008-05-21 15:37:57 -05:00
if (result)
{
g_free (dialog->priv->current_dir);
dialog->priv->current_dir =
gtk_file_chooser_get_current_folder_uri (GTK_FILE_CHOOSER (filechooser));
}
2006-04-13 05:02:33 -05:00
gtk_widget_destroy (filechooser);
return result;
}
2008-05-21 15:37:57 -05:00
void
moo_file_dialog_set_check_name_func (MooFileDialog *dialog,
MooFileDialogCheckNameFunc func,
gpointer data,
GDestroyNotify notify)
{
g_return_if_fail (MOO_IS_FILE_DIALOG (dialog));
if (dialog->priv->check_name_func_data_notify)
dialog->priv->check_name_func_data_notify (dialog->priv->check_name_func_data);
dialog->priv->check_name_func = func;
dialog->priv->check_name_func_data = data;
dialog->priv->check_name_func_data_notify = notify;
}
void
moo_file_dialog_set_extra_widget (MooFileDialog *dialog,
GtkWidget *widget)
{
g_return_if_fail (MOO_IS_FILE_DIALOG (dialog));
g_return_if_fail (!widget || GTK_IS_WIDGET (widget));
if (widget)
2010-11-07 03:32:15 -08:00
g_object_ref_sink (widget);
2008-05-21 15:37:57 -05:00
if (dialog->priv->extra_widget)
g_object_unref (dialog->priv->extra_widget);
dialog->priv->extra_widget = widget;
}
2010-11-23 21:54:39 -08:00
GFile *
moo_file_dialog_get_file (MooFileDialog *dialog)
2006-04-13 05:02:33 -05:00
{
g_return_val_if_fail (MOO_IS_FILE_DIALOG (dialog), NULL);
2010-11-23 21:54:39 -08:00
return dialog->priv->uri ? g_file_new_for_uri (dialog->priv->uri) : NULL;
2006-04-13 05:02:33 -05:00
}
char *
2008-05-21 15:37:57 -05:00
moo_file_dialog_get_uri (MooFileDialog *dialog)
{
g_return_val_if_fail (MOO_IS_FILE_DIALOG (dialog), NULL);
return g_strdup (dialog->priv->uri);
2008-05-21 15:37:57 -05:00
}
2006-04-13 05:02:33 -05:00
2010-11-23 21:54:39 -08:00
MooFileArray *
moo_file_dialog_get_files (MooFileDialog *dialog)
2006-04-13 05:02:33 -05:00
{
g_return_val_if_fail (MOO_IS_FILE_DIALOG (dialog), NULL);
2010-11-23 21:54:39 -08:00
return uri_list_to_files (dialog->priv->uris);
2008-05-21 15:37:57 -05:00
}
char **
moo_file_dialog_get_uris (MooFileDialog *dialog)
{
return string_slist_to_strv (dialog->priv->uris);
2006-04-13 05:02:33 -05:00
}
2006-12-17 03:23:19 -06:00
const char *
moo_file_dialog_get_encoding (MooFileDialog *dialog)
{
g_return_val_if_fail (MOO_IS_FILE_DIALOG (dialog), NULL);
return dialog->priv->encoding ? dialog->priv->encoding : MOO_ENCODING_UTF8;
}
2006-04-13 05:02:33 -05:00
const char *
moo_file_dialogp (GtkWidget *parent,
MooFileDialogType type,
const char *start_name,
2006-04-13 05:02:33 -05:00
const char *title,
const char *prefs_key,
const char *alternate_prefs_key)
{
const char *start = NULL;
const char *filename = NULL;
if (!title)
title = "Choose File";
2006-04-13 05:02:33 -05:00
if (prefs_key)
{
2006-11-19 12:36:22 -06:00
moo_prefs_create_key (prefs_key, MOO_PREFS_STATE, G_TYPE_STRING, NULL);
2006-04-13 05:02:33 -05:00
start = moo_prefs_get_string (prefs_key);
}
2006-04-13 05:02:33 -05:00
if (!start && alternate_prefs_key)
{
2006-11-19 12:36:22 -06:00
moo_prefs_create_key (alternate_prefs_key, MOO_PREFS_STATE, G_TYPE_STRING, NULL);
2006-04-13 05:02:33 -05:00
start = moo_prefs_get_string (alternate_prefs_key);
}
2006-04-13 05:02:33 -05:00
filename = moo_file_dialog (parent, type, start_name, title, start);
2006-04-13 05:02:33 -05:00
if (filename && prefs_key)
{
char *new_start = g_path_get_dirname (filename);
2006-11-19 12:36:22 -06:00
moo_prefs_create_key (prefs_key, MOO_PREFS_STATE, G_TYPE_STRING, NULL);
2006-04-13 05:02:33 -05:00
moo_prefs_set_filename (prefs_key, new_start);
g_free (new_start);
}
return filename;
}
MooFileDialog *
moo_file_dialog_new (MooFileDialogType type,
GtkWidget *parent,
gboolean multiple,
const char *title,
2010-11-23 21:54:39 -08:00
GFile *start_loc,
2006-04-13 05:02:33 -05:00
const char *start_name)
{
2008-05-21 15:37:57 -05:00
char *start_dir_uri = NULL;
MooFileDialog *dialog;
2010-11-23 21:54:39 -08:00
if (start_loc)
start_dir_uri = g_file_get_uri (start_loc);
2008-05-21 15:37:57 -05:00
2010-08-12 18:17:00 -07:00
dialog = MOO_FILE_DIALOG (
g_object_new (MOO_TYPE_FILE_DIALOG,
2008-05-21 15:37:57 -05:00
"parent", parent,
"type", type,
"multiple", multiple,
"title", title,
"current-folder-uri", start_dir_uri,
"name", start_name,
2010-08-12 18:17:00 -07:00
(const char*) NULL));
2008-05-21 15:37:57 -05:00
g_free (start_dir_uri);
return dialog;
2006-04-13 05:02:33 -05:00
}
void
moo_file_dialog_set_filter_mgr_id (MooFileDialog *dialog,
const char *id)
2006-04-13 05:02:33 -05:00
{
g_return_if_fail (MOO_IS_FILE_DIALOG (dialog));
g_object_set (dialog, "filter-mgr-id", id, NULL);
2006-04-13 05:02:33 -05:00
}
2007-09-07 23:59:37 -05:00
void
moo_file_dialog_set_help_id (MooFileDialog *dialog,
const char *id)
{
g_return_if_fail (MOO_IS_FILE_DIALOG (dialog));
2008-05-21 15:37:57 -05:00
MOO_ASSIGN_STRING (dialog->priv->help_id, id);
}
void
moo_file_dialog_set_remember_size (MooFileDialog *dialog,
const char *prefs_key)
{
g_return_if_fail (MOO_IS_FILE_DIALOG (dialog));
g_return_if_fail (prefs_key != NULL);
MOO_ASSIGN_STRING (dialog->priv->size_prefs_key, prefs_key);
}
void
moo_file_dialog_set_current_folder_uri (MooFileDialog *dialog,
const char *uri)
{
g_return_if_fail (MOO_IS_FILE_DIALOG (dialog));
MOO_ASSIGN_STRING (dialog->priv->current_dir, uri);
}
const char *
moo_file_dialog_get_current_folder_uri (MooFileDialog *dialog)
{
g_return_val_if_fail (MOO_IS_FILE_DIALOG (dialog), NULL);
return dialog->priv->current_dir;
2007-09-07 23:59:37 -05:00
}