[gedit/dbus: 2/5] Revived loading a new document from STDIN
- From: Jesse van den Kieboom <jessevdk src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gedit/dbus: 2/5] Revived loading a new document from STDIN
- Date: Wed, 5 May 2010 21:24:03 +0000 (UTC)
commit a3bff05e60ec63a7d7376bbbf633364e9c1f6b91
Author: Jesse van den Kieboom <jesse vandenkieboom epfl ch>
Date: Tue May 4 19:00:05 2010 +0200
Revived loading a new document from STDIN
gedit/gedit-document-loader.c | 116 +++++++++++++++++++++++++++---------
gedit/gedit-document-loader.h | 8 ++-
gedit/gedit-document.c | 32 ++++++++++
gedit/gedit-document.h | 6 ++
gedit/gedit-tab.c | 99 +++++++++++++++++++++++++++----
gedit/gedit-tab.h | 13 ++++
gedit/gedit-window.c | 68 +++++++++++++++------
gedit/gedit-window.h | 9 +++-
gedit/gedit.c | 133 +++++++++++++++++++++++++++++++----------
9 files changed, 389 insertions(+), 95 deletions(-)
---
diff --git a/gedit/gedit-document-loader.c b/gedit/gedit-document-loader.c
index 5e17507..a1dd214 100644
--- a/gedit/gedit-document-loader.c
+++ b/gedit/gedit-document-loader.c
@@ -78,7 +78,8 @@ enum
PROP_DOCUMENT,
PROP_LOCATION,
PROP_ENCODING,
- PROP_NEWLINE_TYPE
+ PROP_NEWLINE_TYPE,
+ PROP_STREAM
};
#define READ_CHUNK_SIZE 8192
@@ -147,6 +148,9 @@ gedit_document_loader_set_property (GObject *object,
case PROP_NEWLINE_TYPE:
loader->priv->auto_detected_newline_type = g_value_get_enum (value);
break;
+ case PROP_STREAM:
+ loader->priv->stream = g_value_dup_object (value);
+ break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
@@ -175,6 +179,9 @@ gedit_document_loader_get_property (GObject *object,
case PROP_NEWLINE_TYPE:
g_value_set_enum (value, loader->priv->auto_detected_newline_type);
break;
+ case PROP_STREAM:
+ g_value_set_object (value, loader->priv->stream);
+ break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
@@ -282,6 +289,15 @@ gedit_document_loader_class_init (GeditDocumentLoaderClass *klass)
G_PARAM_STATIC_NAME |
G_PARAM_STATIC_BLURB));
+ g_object_class_install_property (object_class,
+ PROP_STREAM,
+ g_param_spec_object ("stream",
+ "STREAM",
+ "The STREAM this GeditDocumentLoader loads the document from",
+ G_TYPE_INPUT_STREAM,
+ G_PARAM_READWRITE |
+ G_PARAM_CONSTRUCT_ONLY));
+
signals[LOADING] =
g_signal_new ("loading",
G_OBJECT_CLASS_TYPE (object_class),
@@ -309,6 +325,21 @@ gedit_document_loader_init (GeditDocumentLoader *loader)
}
GeditDocumentLoader *
+gedit_document_loader_new_from_stream (GeditDocument *doc,
+ GInputStream *stream,
+ const GeditEncoding *encoding)
+{
+ g_return_val_if_fail (GEDIT_IS_DOCUMENT (doc), NULL);
+ g_return_val_if_fail (G_IS_INPUT_STREAM (stream), NULL);
+
+ return GEDIT_DOCUMENT_LOADER (g_object_new (GEDIT_TYPE_DOCUMENT_LOADER,
+ "document", doc,
+ "stream", stream,
+ "encoding", encoding,
+ NULL));
+}
+
+GeditDocumentLoader *
gedit_document_loader_new (GeditDocument *doc,
GFile *location,
const GeditEncoding *encoding)
@@ -347,14 +378,18 @@ get_metadata_encoding (GeditDocumentLoader *loader)
{
const GeditEncoding *enc = NULL;
+ if (loader->priv->location == NULL)
+ {
+ /* If we are reading from a stream directly, then there is
+ nothing to do */
+ return NULL;
+ }
+
#ifndef ENABLE_GVFS_METADATA
gchar *charset;
- GFile *location;
gchar *uri;
- location = gedit_document_loader_get_location (loader);
- uri = g_file_get_uri (location);
- g_object_unref (location);
+ uri = g_file_get_uri (loader->priv->location);
charset = gedit_metadata_manager_get (uri, "encoding");
g_free (uri);
@@ -599,29 +634,13 @@ get_candidate_encodings (GeditDocumentLoader *loader)
}
static void
-finish_query_info (AsyncData *async)
+start_stream_read (AsyncData *async)
{
+ GSList *candidate_encodings;
GeditDocumentLoader *loader;
GInputStream *conv_stream;
- GFileInfo *info;
- GSList *candidate_encodings;
-
- loader = async->loader;
- info = loader->priv->info;
-
- /* if it's not a regular file, error out... */
- if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_TYPE) &&
- g_file_info_get_file_type (info) != G_FILE_TYPE_REGULAR)
- {
- g_set_error (&loader->priv->error,
- G_IO_ERROR,
- G_IO_ERROR_NOT_REGULAR_FILE,
- "Not a regular file");
-
- remote_load_completed_or_failed (loader, async);
- return;
- }
+ loader = async->loader;
/* Get the candidate encodings */
if (loader->priv->encoding == NULL)
@@ -630,7 +649,7 @@ finish_query_info (AsyncData *async)
}
else
{
- candidate_encodings = g_slist_prepend (NULL, (gpointer) loader->priv->encoding);
+ candidate_encodings = g_slist_prepend (NULL, (gpointer)loader->priv->encoding);
}
loader->priv->converter = gedit_smart_charset_converter_new (candidate_encodings);
@@ -650,6 +669,32 @@ finish_query_info (AsyncData *async)
}
static void
+finish_query_info (AsyncData *async)
+{
+ GeditDocumentLoader *loader;
+ GFileInfo *info;
+
+ loader = async->loader;
+ info = loader->priv->info;
+
+ /* if it's not a regular file, error out... */
+ if (g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_STANDARD_TYPE) &&
+ g_file_info_get_file_type (info) != G_FILE_TYPE_REGULAR)
+ {
+ g_set_error (&loader->priv->error,
+ G_IO_ERROR,
+ G_IO_ERROR_NOT_REGULAR_FILE,
+ "Not a regular file");
+
+ remote_load_completed_or_failed (loader, async);
+
+ return;
+ }
+
+ start_stream_read (async);
+}
+
+static void
query_info_cb (GFile *source,
GAsyncResult *res,
AsyncData *async)
@@ -850,8 +895,15 @@ gedit_document_loader_load (GeditDocumentLoader *loader)
loader->priv->cancellable = g_cancellable_new ();
async = async_data_new (loader);
-
- open_async_read (async);
+
+ if (loader->priv->stream)
+ {
+ start_stream_read (async);
+ }
+ else
+ {
+ open_async_read (async);
+ }
}
gboolean
@@ -884,13 +936,19 @@ gedit_document_loader_get_document (GeditDocumentLoader *loader)
return loader->priv->document;
}
-/* Returns STDIN_URI if loading from stdin */
GFile *
gedit_document_loader_get_location (GeditDocumentLoader *loader)
{
g_return_val_if_fail (GEDIT_IS_DOCUMENT_LOADER (loader), NULL);
- return g_file_dup (loader->priv->location);
+ if (loader->priv->location)
+ {
+ return g_file_dup (loader->priv->location);
+ }
+ else
+ {
+ return NULL;
+ }
}
goffset
diff --git a/gedit/gedit-document-loader.h b/gedit/gedit-document-loader.h
index 644af20..33bcaf0 100644
--- a/gedit/gedit-document-loader.h
+++ b/gedit/gedit-document-loader.h
@@ -93,9 +93,11 @@ void gedit_document_loader_loading (GeditDocumentLoader *loader,
GError *error);
void gedit_document_loader_load (GeditDocumentLoader *loader);
-#if 0
-gboolean gedit_document_loader_load_from_stdin (GeditDocumentLoader *loader);
-#endif
+
+GeditDocumentLoader *gedit_document_loader_new_from_stream (GeditDocument *doc,
+ GInputStream *stream,
+ const GeditEncoding *encoding);
+
gboolean gedit_document_loader_cancel (GeditDocumentLoader *loader);
GeditDocument *gedit_document_loader_get_document (GeditDocumentLoader *loader);
diff --git a/gedit/gedit-document.c b/gedit/gedit-document.c
index 98b979b..3859d2e 100644
--- a/gedit/gedit-document.c
+++ b/gedit/gedit-document.c
@@ -1428,6 +1428,38 @@ gedit_document_load_real (GeditDocument *doc,
gedit_document_loader_load (doc->priv->loader);
}
+void
+gedit_document_load_stream (GeditDocument *doc,
+ GInputStream *stream,
+ const GeditEncoding *encoding,
+ gint line_pos,
+ gint column_pos)
+{
+ g_return_if_fail (GEDIT_IS_DOCUMENT (doc));
+ g_return_if_fail (G_IS_INPUT_STREAM (stream));
+ g_return_if_fail (doc->priv->loader == NULL);
+
+ gedit_debug_message (DEBUG_DOCUMENT, "load stream");
+
+ /* create a loader. It will be destroyed when loading is completed */
+ doc->priv->loader = gedit_document_loader_new_from_stream (doc, stream, encoding);
+
+ g_signal_connect (doc->priv->loader,
+ "loading",
+ G_CALLBACK (document_loader_loading),
+ doc);
+
+ doc->priv->create = FALSE;
+ doc->priv->requested_encoding = encoding;
+ doc->priv->requested_line_pos = line_pos;
+ doc->priv->requested_column_pos = column_pos;
+
+ set_location (doc, NULL);
+ set_content_type (doc, NULL);
+
+ gedit_document_loader_load (doc->priv->loader);
+}
+
/**
* gedit_document_load:
* @doc: the #GeditDocument.
diff --git a/gedit/gedit-document.h b/gedit/gedit-document.h
index fe6b623..b5a80ed 100644
--- a/gedit/gedit-document.h
+++ b/gedit/gedit-document.h
@@ -209,6 +209,12 @@ void gedit_document_load (GeditDocument *doc,
gint column_pos,
gboolean create);
+void gedit_document_load_stream (GeditDocument *doc,
+ GInputStream *stream,
+ const GeditEncoding *encoding,
+ gint line_pos,
+ gint column_pos);
+
gboolean gedit_document_load_cancel (GeditDocument *doc);
void gedit_document_save (GeditDocument *doc,
diff --git a/gedit/gedit-tab.c b/gedit/gedit-tab.c
index 5fa908c..ce387f5 100644
--- a/gedit/gedit-tab.c
+++ b/gedit/gedit-tab.c
@@ -932,7 +932,10 @@ document_loaded (GeditDocument *document,
}
else
{
- _gedit_recent_remove (GEDIT_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (tab))), location);
+ if (location)
+ {
+ _gedit_recent_remove (GEDIT_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (tab))), location);
+ }
if (tab->priv->state == GEDIT_TAB_STATE_LOADING_ERROR)
{
@@ -970,23 +973,28 @@ document_loaded (GeditDocument *document,
gtk_widget_show (emsg);
- g_object_unref (location);
+ if (location)
+ {
+ g_object_unref (location);
+ }
return;
}
else
{
- gchar *mime;
GList *all_documents;
GList *l;
- g_return_if_fail (location != NULL);
+ if (location != NULL)
+ {
+ gchar *mime;
+ mime = gedit_document_get_mime_type (document);
- mime = gedit_document_get_mime_type (document);
- _gedit_recent_add (GEDIT_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (tab))),
- location,
- mime);
- g_free (mime);
+ _gedit_recent_add (GEDIT_WINDOW (gtk_widget_get_toplevel (GTK_WIDGET (tab))),
+ location,
+ mime);
+ g_free (mime);
+ }
if (error &&
error->domain == GEDIT_DOCUMENT_ERROR &&
@@ -1033,7 +1041,7 @@ document_loaded (GeditDocument *document,
loc = gedit_document_get_location (d);
- if ((loc != NULL) &&
+ if (loc != NULL && location != NULL &&
g_file_equal (location, loc))
{
GtkWidget *w;
@@ -1074,14 +1082,24 @@ document_loaded (GeditDocument *document,
g_list_free (all_documents);
gedit_tab_set_state (tab, GEDIT_TAB_STATE_NORMAL);
-
+
+ if (location == NULL)
+ {
+ /* FIXME: hackish */
+ gtk_text_buffer_set_modified (GTK_TEXT_BUFFER (gedit_tab_get_document (tab)),
+ TRUE);
+ }
+
install_auto_save_timeout_if_needed (tab);
tab->priv->ask_if_externally_modified = TRUE;
}
end:
- g_object_unref (location);
+ if (location)
+ {
+ g_object_unref (location);
+ }
tab->priv->tmp_line_pos = 0;
tab->priv->tmp_encoding = NULL;
@@ -1631,6 +1649,28 @@ _gedit_tab_new_from_location (GFile *location,
return GTK_WIDGET (tab);
}
+GtkWidget *
+_gedit_tab_new_from_stream (GInputStream *stream,
+ const GeditEncoding *encoding,
+ gint line_pos,
+ gint column_pos)
+{
+ GeditTab *tab;
+
+ g_return_val_if_fail (G_IS_INPUT_STREAM (stream), NULL);
+
+ tab = GEDIT_TAB (_gedit_tab_new ());
+
+ _gedit_tab_load_stream (tab,
+ stream,
+ encoding,
+ line_pos,
+ column_pos);
+
+ return GTK_WIDGET (tab);
+
+}
+
/**
* gedit_tab_get_view:
* @tab: a #GeditTab
@@ -2028,6 +2068,41 @@ _gedit_tab_load (GeditTab *tab,
}
void
+_gedit_tab_load_stream (GeditTab *tab,
+ GInputStream *stream,
+ const GeditEncoding *encoding,
+ gint line_pos,
+ gint column_pos)
+{
+ GeditDocument *doc;
+
+ g_return_if_fail (GEDIT_IS_TAB (tab));
+ g_return_if_fail (G_IS_INPUT_STREAM (stream));
+ g_return_if_fail (tab->priv->state == GEDIT_TAB_STATE_NORMAL);
+
+ doc = gedit_tab_get_document (tab);
+ g_return_if_fail (GEDIT_IS_DOCUMENT (doc));
+
+ gedit_tab_set_state (tab, GEDIT_TAB_STATE_LOADING);
+
+ tab->priv->tmp_line_pos = line_pos;
+ tab->priv->tmp_column_pos = column_pos;
+ tab->priv->tmp_encoding = encoding;
+
+ if (tab->priv->auto_save_timeout > 0)
+ {
+ remove_auto_save_timeout (tab);
+ }
+
+ gedit_document_load_stream (doc,
+ stream,
+ encoding,
+ line_pos,
+ column_pos);
+
+}
+
+void
_gedit_tab_revert (GeditTab *tab)
{
GeditDocument *doc;
diff --git a/gedit/gedit-tab.h b/gedit/gedit-tab.h
index acb632f..f607a96 100644
--- a/gedit/gedit-tab.h
+++ b/gedit/gedit-tab.h
@@ -135,6 +135,12 @@ GtkWidget *_gedit_tab_new_from_location (GFile *location,
gint line_pos,
gint column_pos,
gboolean create);
+
+GtkWidget *_gedit_tab_new_from_stream (GInputStream *stream,
+ const GeditEncoding *encoding,
+ gint line_pos,
+ gint column_pos);
+
gchar *_gedit_tab_get_name (GeditTab *tab);
gchar *_gedit_tab_get_tooltips (GeditTab *tab);
GdkPixbuf *_gedit_tab_get_icon (GeditTab *tab);
@@ -144,6 +150,13 @@ void _gedit_tab_load (GeditTab *tab,
gint line_pos,
gint column_pos,
gboolean create);
+
+void _gedit_tab_load_stream (GeditTab *tab,
+ GInputStream *location,
+ const GeditEncoding *encoding,
+ gint line_pos,
+ gint column_pos);
+
void _gedit_tab_revert (GeditTab *tab);
void _gedit_tab_save (GeditTab *tab);
void _gedit_tab_save_as (GeditTab *tab,
diff --git a/gedit/gedit-window.c b/gedit/gedit-window.c
index 106e156..f4f2b5d 100644
--- a/gedit/gedit-window.c
+++ b/gedit/gedit-window.c
@@ -4182,6 +4182,31 @@ gedit_window_create_tab (GeditWindow *window,
return tab;
}
+static GeditTab *
+process_create_tab (GeditWindow *window,
+ GeditTab *tab,
+ gboolean jump_to)
+{
+ if (tab == NULL)
+ {
+ return NULL;
+ }
+
+ gtk_widget_show (GTK_WIDGET (tab));
+
+ gedit_notebook_add_tab (GEDIT_NOTEBOOK (window->priv->notebook),
+ tab,
+ -1,
+ jump_to);
+
+ if (!gtk_widget_get_visible (GTK_WIDGET (window)))
+ {
+ gtk_window_present (GTK_WINDOW (window));
+ }
+
+ return tab;
+}
+
/**
* gedit_window_create_tab_from_location:
* @window: a #GeditWindow
@@ -4213,28 +4238,35 @@ gedit_window_create_tab_from_location (GeditWindow *window,
g_return_val_if_fail (G_IS_FILE (location), NULL);
tab = _gedit_tab_new_from_location (location,
- encoding,
- line_pos,
- column_pos,
- create);
- if (tab == NULL)
- return NULL;
+ encoding,
+ line_pos,
+ column_pos,
+ create);
- gtk_widget_show (tab);
-
- gedit_notebook_add_tab (GEDIT_NOTEBOOK (window->priv->notebook),
- GEDIT_TAB (tab),
- -1,
- jump_to);
+ return process_create_tab (window, GEDIT_TAB (tab), jump_to);
+}
+GeditTab *
+gedit_window_create_tab_from_stream (GeditWindow *window,
+ GInputStream *stream,
+ const GeditEncoding *encoding,
+ gint line_pos,
+ gint column_pos,
+ gboolean jump_to)
+{
+ GtkWidget *tab;
- if (!gtk_widget_get_visible (GTK_WIDGET (window)))
- {
- gtk_window_present (GTK_WINDOW (window));
- }
+ g_return_val_if_fail (GEDIT_IS_WINDOW (window), NULL);
+ g_return_val_if_fail (G_IS_INPUT_STREAM (stream), NULL);
+
+ tab = _gedit_tab_new_from_stream (stream,
+ encoding,
+ line_pos,
+ column_pos);
- return GEDIT_TAB (tab);
-}
+ return process_create_tab (window, GEDIT_TAB (tab), jump_to);
+
+}
/**
* gedit_window_get_active_tab:
diff --git a/gedit/gedit-window.h b/gedit/gedit-window.h
index c1fc4d5..f82ad52 100644
--- a/gedit/gedit-window.h
+++ b/gedit/gedit-window.h
@@ -112,7 +112,14 @@ GeditTab *gedit_window_create_tab_from_location (GeditWindow *window,
gint column_pos,
gboolean create,
gboolean jump_to);
-
+
+GeditTab *gedit_window_create_tab_from_stream (GeditWindow *window,
+ GInputStream *stream,
+ const GeditEncoding *encoding,
+ gint line_pos,
+ gint column_pos,
+ gboolean jump_to);
+
void gedit_window_close_tab (GeditWindow *window,
GeditTab *tab);
diff --git a/gedit/gedit.c b/gedit/gedit.c
index da6ff43..cf55c2f 100644
--- a/gedit/gedit.c
+++ b/gedit/gedit.c
@@ -58,16 +58,114 @@
#include "gedit-metadata-manager.h"
#endif
+#ifdef G_OS_UNIX
+#include <unistd.h>
+#include <gio/gunixinputstream.h>
+#endif
+
+#ifdef G_OS_UNIX
+static gboolean
+gedit_main_load_from_stdin (GeditWindow *window,
+ gboolean jump_to)
+{
+ GInputStream *stream;
+ const GeditEncoding *encoding;
+ gint line_position;
+ gint column_position;
+ GeditCommandLine *command_line;
+
+ /* Only if it's not a tty */
+ if (isatty (STDIN_FILENO))
+ {
+ return FALSE;
+ }
+
+ command_line = gedit_command_line_get_default ();
+
+ encoding = gedit_command_line_get_encoding (command_line);
+ line_position = gedit_command_line_get_line_position (command_line);
+ column_position = gedit_command_line_get_column_position (command_line);
+
+ /* Construct a stream for stdin */
+ stream = g_unix_input_stream_new (STDIN_FILENO, TRUE);
+
+ gedit_window_create_tab_from_stream (window,
+ stream,
+ encoding,
+ line_position,
+ column_position,
+ jump_to);
+ g_object_unref (stream);
+
+ return TRUE;
+}
+#endif
+
+static void
+gedit_main_window (void)
+{
+ GSList *file_list;
+ GeditWindow *window;
+ GeditCommandLine *command_line;
+ GeditApp *app;
+ gboolean doc_created = FALSE;
+
+ app = gedit_app_get_default ();
+
+ gedit_debug_message (DEBUG_APP, "Create main window");
+ window = gedit_app_create_window (app, NULL);
+
+ command_line = gedit_command_line_get_default ();
+ file_list = gedit_command_line_get_file_list (command_line);
+
+ if (file_list != NULL)
+ {
+ GSList *loaded;
+ const GeditEncoding *encoding;
+ gint line_position;
+ gint column_position;
+
+ encoding = gedit_command_line_get_encoding (command_line);
+ line_position = gedit_command_line_get_line_position (command_line);
+ column_position = gedit_command_line_get_column_position (command_line);
+
+ gedit_debug_message (DEBUG_APP, "Load files");
+ loaded = _gedit_cmd_load_files_from_prompt (window,
+ file_list,
+ encoding,
+ line_position,
+ column_position);
+
+ doc_created = loaded != NULL;
+ g_slist_free (loaded);
+ }
+
+#ifdef G_OS_UNIX
+ /* We can only do this on unix systems */
+ if (gedit_main_load_from_stdin (window, !doc_created))
+ {
+ doc_created = TRUE;
+ }
+#endif
+
+ if (!doc_created || gedit_command_line_get_new_document (command_line))
+ {
+ gedit_debug_message (DEBUG_APP, "Create tab");
+ gedit_window_create_tab (window, TRUE);
+ }
+
+ gedit_debug_message (DEBUG_APP, "Show window");
+ gtk_widget_show (GTK_WIDGET (window));
+}
+
static void
gedit_main (gboolean service)
{
GeditPluginsEngine *engine;
- GeditWindow *window;
GeditApp *app;
gboolean restored = FALSE;
gchar *dir;
gchar *icon_dir;
- GeditCommandLine *command_line;
gedit_debug_message (DEBUG_APP, "Set icon");
@@ -95,38 +193,9 @@ gedit_main (gboolean service)
restored = gedit_session_load ();
}
- command_line = gedit_command_line_get_default ();
-
if (!service && !restored)
{
- GSList *file_list;
-
- gedit_debug_message (DEBUG_APP, "Create main window");
- window = gedit_app_create_window (app, NULL);
-
- file_list = gedit_command_line_get_file_list (command_line);
-
- if (file_list != NULL)
- {
- GSList *loaded;
-
- gedit_debug_message (DEBUG_APP, "Load files");
- loaded = _gedit_cmd_load_files_from_prompt (window,
- file_list,
- gedit_command_line_get_encoding (command_line),
- gedit_command_line_get_line_position (command_line),
- gedit_command_line_get_column_position (command_line));
-
- g_slist_free (loaded);
- }
- else
- {
- gedit_debug_message (DEBUG_APP, "Create tab");
- gedit_window_create_tab (window, TRUE);
- }
-
- gedit_debug_message (DEBUG_APP, "Show window");
- gtk_widget_show (GTK_WIDGET (window));
+ gedit_main_window ();
}
gedit_debug_message (DEBUG_APP, "Start gtk-main");
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]