[gtkhtml] Support remote locations when inserting files into editor.
- From: Matthew Barnes <mbarnes src gnome org>
- To: svn-commits-list gnome org
- Cc:
- Subject: [gtkhtml] Support remote locations when inserting files into editor.
- Date: Sat, 5 Dec 2009 04:51:56 +0000 (UTC)
commit 74ec6ec6c15e7a54d3d1040d4c9259830d1cbc3c
Author: Matthew Barnes <mbarnes redhat com>
Date: Fri Dec 4 23:51:24 2009 -0500
Support remote locations when inserting files into editor.
components/editor/gtkhtml-editor-actions.c | 165 ++++++++++++++--------------
components/editor/gtkhtml-editor-private.c | 125 +++++++++-------------
components/editor/gtkhtml-editor-private.h | 10 +-
components/editor/gtkhtml-editor.c | 4 +-
4 files changed, 139 insertions(+), 165 deletions(-)
---
diff --git a/components/editor/gtkhtml-editor-actions.c b/components/editor/gtkhtml-editor-actions.c
index 436120e..e495fca 100644
--- a/components/editor/gtkhtml-editor-actions.c
+++ b/components/editor/gtkhtml-editor-actions.c
@@ -150,107 +150,88 @@ insert_image_response_cb (GtkFileChooser *file_chooser,
}
static void
-insert_html_file_response_cb (GtkFileChooser *file_chooser,
- gint response,
- GtkhtmlEditor *editor)
+insert_html_file_ready_cb (GFile *file,
+ GAsyncResult *result,
+ GtkhtmlEditor *editor)
{
- gchar *filename;
+ GtkHTML *html;
+ GtkHTML *new_html;
+ GtkHTMLStream *stream;
+ GtkWidget *dialog;
gchar *contents;
gsize length;
GError *error = NULL;
- if (response != GTK_RESPONSE_OK)
- return;
+ g_file_load_contents_finish (
+ file, result, &contents, &length, NULL, &error);
+ if (error != NULL)
+ goto fail;
- filename = gtk_file_chooser_get_filename (file_chooser);
+ html = gtkhtml_editor_get_html (editor);
+ new_html = GTK_HTML (gtk_html_new ());
- /* Assume the file encoding is UTF-8. */
- gtkhtml_editor_get_file_contents (
- filename, NULL, &contents, &length, &error);
+ stream = gtk_html_begin (new_html);
+ gtk_html_write (html, stream, contents, length);
+ gtk_html_end (html, stream, GTK_HTML_STREAM_OK);
+ g_object_unref (stream);
- /* If we get a conversion error, look for a charset and try again. */
- if (error != NULL && g_error_matches (error, G_CONVERT_ERROR,
- G_CONVERT_ERROR_ILLEGAL_SEQUENCE)) {
+ g_free (contents);
- gchar *charset;
+ html = gtkhtml_editor_get_html (editor);
+ gtk_html_insert_gtk_html (html, new_html);
- charset = gtkhtml_editor_get_file_charset (filename);
- if (charset != NULL) {
- g_clear_error (&error);
- gtkhtml_editor_get_file_contents (
- filename, charset, &contents, &length, &error);
- g_free (charset);
- }
- }
+ g_object_unref (new_html);
- if (error == NULL) {
- GtkHTML *html;
- GtkHTMLStream *stream;
-
- html = GTK_HTML (gtk_html_new ());
- stream = gtk_html_begin_content (
- html, "text/html; charset=utf-8");
- gtk_html_write (html, stream, contents, length);
- gtk_html_end (html, stream, GTK_HTML_STREAM_OK);
- gtk_html_insert_gtk_html (
- gtkhtml_editor_get_html (editor), html);
- g_free (contents);
- } else {
- /* XXX Show an error dialog. */
- g_warning ("%s", error->message);
- g_error_free (error);
- }
+ goto exit;
+
+fail:
+ dialog = gtk_message_dialog_new (
+ GTK_WINDOW (editor), 0, GTK_MESSAGE_ERROR,
+ GTK_BUTTONS_CLOSE, _("Failed to insert HTML file."));
+ gtk_message_dialog_format_secondary_text (
+ GTK_MESSAGE_DIALOG (dialog), "%s.", error->message);
+ gtk_dialog_run (GTK_DIALOG (dialog));
+ gtk_widget_destroy (dialog);
- g_free (filename);
+ g_error_free (error);
+
+exit:
+ g_object_unref (editor);
}
static void
-insert_text_file_response_cb (GtkFileChooser *file_chooser,
- gint response,
- GtkhtmlEditor *editor)
+insert_text_file_ready_cb (GFile *file,
+ GAsyncResult *result,
+ GtkhtmlEditor *editor)
{
+ GtkHTML *html;
+ GtkWidget *dialog;
gchar *contents;
- gchar *filename;
gsize length;
GError *error = NULL;
- if (response != GTK_RESPONSE_OK)
- return;
+ g_file_load_contents_finish (
+ file, result, &contents, &length, NULL, &error);
+ if (error != NULL)
+ goto fail;
- filename = gtk_file_chooser_get_filename (file_chooser);
-
- /* Assume the file encoding is UTF-8. */
- gtkhtml_editor_get_file_contents (
- filename, NULL, &contents, &length, &error);
-
- /* If we get a conversion error, look for a charset and try again. */
- if (error != NULL && g_error_matches (error, G_CONVERT_ERROR,
- G_CONVERT_ERROR_ILLEGAL_SEQUENCE)) {
-
- gchar *charset;
-
- charset = gtkhtml_editor_get_file_charset (filename);
- if (charset != NULL) {
- g_clear_error (&error);
- gtkhtml_editor_get_file_contents (
- filename, charset, &contents, &length, &error);
- g_free (charset);
- }
- }
+ html = gtkhtml_editor_get_html (editor);
+ html_engine_paste_text (html->engine, contents, length);
+ g_free (contents);
- if (error == NULL) {
- GtkHTML *html;
+ goto exit;
- html = gtkhtml_editor_get_html (editor);
- html_engine_paste_text (html->engine, contents, length);
- g_free (contents);
- } else {
- /* XXX Show an error dialog. */
- g_warning ("%s", error->message);
- g_error_free (error);
- }
+fail:
+ dialog = gtk_message_dialog_new (
+ GTK_WINDOW (editor), 0, GTK_MESSAGE_ERROR,
+ GTK_BUTTONS_CLOSE, _("Failed to insert text file."));
+ gtk_message_dialog_format_secondary_text (
+ GTK_MESSAGE_DIALOG (dialog), "%s.", error->message);
+ gtk_dialog_run (GTK_DIALOG (dialog));
+ gtk_widget_destroy (dialog);
- g_free (filename);
+exit:
+ g_object_unref (editor);
}
static void
@@ -628,9 +609,18 @@ static void
action_insert_html_file_cb (GtkToggleAction *action,
GtkhtmlEditor *editor)
{
- gtkhtml_editor_insert_file (
- editor, _("Insert HTML File"),
- G_CALLBACK (insert_html_file_response_cb));
+ GFile *file;
+
+ file = gtkhtml_editor_run_open_dialog (
+ editor, _("Insert HTML File"), NULL, NULL);
+
+ /* XXX Need a way to cancel this. */
+ g_file_load_contents_async (
+ file, NULL, (GAsyncReadyCallback)
+ insert_html_file_ready_cb,
+ g_object_ref (editor));
+
+ g_object_unref (file);
}
static void
@@ -684,9 +674,18 @@ static void
action_insert_text_file_cb (GtkAction *action,
GtkhtmlEditor *editor)
{
- gtkhtml_editor_insert_file (
- editor, _("Insert Text File"),
- G_CALLBACK (insert_text_file_response_cb));
+ GFile *file;
+
+ file = gtkhtml_editor_run_open_dialog (
+ editor, _("Insert Text File"), NULL, NULL);
+
+ /* XXX Need a way to cancel this. */
+ g_file_load_contents_async (
+ file, NULL, (GAsyncReadyCallback)
+ insert_text_file_ready_cb,
+ g_object_ref (editor));
+
+ g_object_unref (file);
}
static void
diff --git a/components/editor/gtkhtml-editor-private.c b/components/editor/gtkhtml-editor-private.c
index 8a44207..087e383 100644
--- a/components/editor/gtkhtml-editor-private.c
+++ b/components/editor/gtkhtml-editor-private.c
@@ -633,80 +633,6 @@ gtkhtml_editor_find_data_file (const gchar *basename)
return NULL; /* never gets here */
}
-gchar *
-gtkhtml_editor_get_file_charset (const gchar *filename)
-{
- GRegex *regex;
- GMatchInfo *match_info;
- gchar *charset = NULL;
- gchar *contents;
- GError *error = NULL;
-
- g_return_val_if_fail (filename != NULL, NULL);
-
- if (!g_file_get_contents (filename, &contents, NULL, &error))
- goto exit;
-
- /* Search for "charset = token" as defined in RFC 2616. */
- regex = g_regex_new (
- "charset[ \t]*=[ \t]*(" TOKEN ")",
- G_REGEX_CASELESS, 0, &error);
- if (regex == NULL)
- goto exit;
-
- /* Extract "token", which should be the charset name. */
- g_regex_match (regex, contents, 0, &match_info);
- if (g_match_info_matches (match_info))
- charset = g_match_info_fetch (match_info, 1);
-
- g_match_info_free (match_info);
- g_regex_unref (regex);
-
-exit:
- if (error != NULL) {
- g_warning ("%s", error->message);
- g_error_free (error);
- }
-
- g_free (contents);
-
- return charset;
-}
-
-gboolean
-gtkhtml_editor_get_file_contents (const gchar *filename,
- const gchar *encoding,
- gchar **contents,
- gsize *length,
- GError **error)
-{
- GIOChannel *channel;
- GIOStatus status;
-
- g_return_val_if_fail (filename != NULL, FALSE);
- g_return_val_if_fail (contents != NULL, FALSE);
-
- channel = g_io_channel_new_file (filename, "r", error);
- if (channel == NULL)
- return FALSE;
-
- status = g_io_channel_set_encoding (channel, encoding, error);
- if (status == G_IO_STATUS_ERROR) {
- g_io_channel_unref (channel);
- return FALSE;
- }
-
- status = g_io_channel_read_to_end (channel, contents, length, error);
- if (status == G_IO_STATUS_ERROR) {
- g_io_channel_unref (channel);
- return FALSE;
- }
-
- g_io_channel_unref (channel);
-
- return TRUE;
-}
-
gint
gtkhtml_editor_insert_file (GtkhtmlEditor *editor,
const gchar *title,
@@ -783,6 +709,57 @@ gtkhtml_editor_request_finish (GtkhtmlEditor *editor,
return success;
}
+GFile *
+gtkhtml_editor_run_open_dialog (GtkhtmlEditor *editor,
+ const gchar *title,
+ GtkCallback customize_func,
+ gpointer customize_data)
+{
+ GtkFileChooser *file_chooser;
+ GFile *chosen_file = NULL;
+ GtkWidget *dialog;
+ gchar *uri;
+
+ g_return_val_if_fail (GTKHTML_IS_EDITOR (editor), NULL);
+
+ dialog = gtk_file_chooser_dialog_new (
+ title, GTK_WINDOW (editor),
+ GTK_FILE_CHOOSER_ACTION_OPEN,
+ GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
+ GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, NULL);
+
+ file_chooser = GTK_FILE_CHOOSER (dialog);
+
+ gtk_dialog_set_default_response (
+ GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
+
+ gtk_file_chooser_set_local_only (file_chooser, FALSE);
+
+ /* Restore the current folder from the previous file chooser. */
+ uri = (gchar *) gtkhtml_editor_get_current_folder (editor);
+ if (uri != NULL)
+ gtk_file_chooser_set_current_folder_uri (file_chooser, uri);
+
+ /* Allow further customizations before running the dialog. */
+ if (customize_func != NULL)
+ customize_func (dialog, customize_data);
+
+ if (gtk_dialog_run (GTK_DIALOG (dialog)) != GTK_RESPONSE_ACCEPT)
+ goto exit;
+
+ chosen_file = gtk_file_chooser_get_file (file_chooser);
+
+ /* Save the current folder for subsequent file choosers. */
+ uri = gtk_file_chooser_get_current_folder_uri (file_chooser);
+ gtkhtml_editor_set_current_folder (editor, uri);
+ g_free (uri);
+
+exit:
+ gtk_widget_destroy (dialog);
+
+ return chosen_file;
+}
+
void
gtkhtml_editor_show_uri (GtkWindow *parent,
const gchar *uri)
diff --git a/components/editor/gtkhtml-editor-private.h b/components/editor/gtkhtml-editor-private.h
index a9df794..7e07851 100644
--- a/components/editor/gtkhtml-editor-private.h
+++ b/components/editor/gtkhtml-editor-private.h
@@ -200,12 +200,6 @@ void gtkhtml_editor_private_finalize (GtkhtmlEditor *editor);
void gtkhtml_editor_actions_init (GtkhtmlEditor *editor);
gchar * gtkhtml_editor_find_data_file (const gchar *basename);
-gchar * gtkhtml_editor_get_file_charset (const gchar *filename);
-gboolean gtkhtml_editor_get_file_contents (const gchar *filename,
- const gchar *encoding,
- gchar **contents,
- gsize *length,
- GError **error);
gint gtkhtml_editor_insert_file (GtkhtmlEditor *editor,
const gchar *title,
GCallback response_cb);
@@ -218,6 +212,10 @@ void gtkhtml_editor_request_async (GtkhtmlEditor *editor,
gboolean gtkhtml_editor_request_finish (GtkhtmlEditor *editor,
GAsyncResult *result,
GError **error);
+GFile * gtkhtml_editor_run_open_dialog (GtkhtmlEditor *editor,
+ const gchar *title,
+ GtkCallback customize_func,
+ gpointer customize_data);
void gtkhtml_editor_show_uri (GtkWindow *parent,
const gchar *uri);
void gtkhtml_editor_spell_check (GtkhtmlEditor *editor,
diff --git a/components/editor/gtkhtml-editor.c b/components/editor/gtkhtml-editor.c
index 737af53..c7fdbf5 100644
--- a/components/editor/gtkhtml-editor.c
+++ b/components/editor/gtkhtml-editor.c
@@ -1351,7 +1351,7 @@ gtkhtml_editor_file_chooser_dialog_run (GtkhtmlEditor *editor,
g_return_val_if_fail (GTKHTML_IS_EDITOR (editor), response);
g_return_val_if_fail (GTK_IS_FILE_CHOOSER_DIALOG (dialog), response);
- gtk_file_chooser_set_current_folder (
+ gtk_file_chooser_set_current_folder_uri (
GTK_FILE_CHOOSER (dialog),
gtkhtml_editor_get_current_folder (editor));
@@ -1366,7 +1366,7 @@ gtkhtml_editor_file_chooser_dialog_run (GtkhtmlEditor *editor,
if (save_folder) {
gchar *folder;
- folder = gtk_file_chooser_get_current_folder (
+ folder = gtk_file_chooser_get_current_folder_uri (
GTK_FILE_CHOOSER (dialog));
gtkhtml_editor_set_current_folder (editor, folder);
g_free (folder);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]