[gedit] Use the gio metadata system.
- From: Ignacio Casal Quinteiro <icq src gnome org>
- To: svn-commits-list gnome org
- Cc:
- Subject: [gedit] Use the gio metadata system.
- Date: Sat, 28 Nov 2009 18:45:32 +0000 (UTC)
commit 75c8fc566f0b7f7cd2af490261c596aa6bb20b94
Author: Ignacio Casal Quinteiro <icq gnome org>
Date: Sat Nov 28 18:00:59 2009 +0100
Use the gio metadata system.
Now we use the gio metadata system and the old metadata manager is used
only in win32.
gedit/gedit-document-loader.c | 32 ++++-
gedit/gedit-document.c | 237 ++++++++++++++++++++++++++++++++-----
gedit/gedit-document.h | 10 ++
gedit/gedit-gio-document-loader.c | 4 +-
gedit/gedit.c | 5 +-
5 files changed, 251 insertions(+), 37 deletions(-)
---
diff --git a/gedit/gedit-document-loader.c b/gedit/gedit-document-loader.c
index 09ab744..923eb6a 100644
--- a/gedit/gedit-document-loader.c
+++ b/gedit/gedit-document-loader.c
@@ -229,10 +229,15 @@ insert_text_in_document (GeditDocumentLoader *loader,
}
static const GeditEncoding *
-get_metadata_encoding (const gchar *uri)
+get_metadata_encoding (GeditDocumentLoader *loader)
{
+ const GeditEncoding *enc = NULL;
+
+#ifdef G_OS_WIN32
gchar *charset;
- const GeditEncoding *enc;
+ const gchar *uri;
+
+ uri = gedit_document_loader_get_uri (loader);
charset = gedit_metadata_manager_get (uri, "encoding");
@@ -242,6 +247,25 @@ get_metadata_encoding (const gchar *uri)
enc = gedit_encoding_get_from_charset (charset);
g_free (charset);
+#else
+ GFileInfo *info;
+
+ info = gedit_document_loader_get_info (loader);
+
+ /* check if the encoding was set in the metadata */
+ if (g_file_info_has_attribute (info, GEDIT_METADATA_ATTRIBUTE_ENCODING))
+ {
+ const gchar *charset;
+
+ charset = g_file_info_get_attribute_string (info,
+ GEDIT_METADATA_ATTRIBUTE_ENCODING);
+
+ if (charset == NULL)
+ return NULL;
+
+ enc = gedit_encoding_get_from_charset (charset);
+ }
+#endif
return enc;
}
@@ -294,12 +318,10 @@ gedit_document_loader_update_document_contents (GeditDocumentLoader *loader,
/* Autodetecting the encoding */
if (loader->encoding == NULL)
{
- const gchar *uri;
const GeditEncoding *metadata_encoding;
/* first try with the encoding stored in the metadata, if any */
- uri = gedit_document_loader_get_uri (loader);
- metadata_encoding = get_metadata_encoding (uri);
+ metadata_encoding = get_metadata_encoding (loader);
if (metadata_encoding != NULL)
{
diff --git a/gedit/gedit-document.c b/gedit/gedit-document.c
index c770517..b4e672f 100644
--- a/gedit/gedit-document.c
+++ b/gedit/gedit-document.c
@@ -44,7 +44,6 @@
#include "gedit-document.h"
#include "gedit-debug.h"
#include "gedit-utils.h"
-#include "gedit-metadata-manager.h"
#include "gedit-language-manager.h"
#include "gedit-style-scheme-manager.h"
#include "gedit-document-loader.h"
@@ -53,6 +52,14 @@
#include "gedit-enum-types.h"
#include "gedittextregion.h"
+#ifdef G_OS_WIN32
+#include "gedit-metadata-manager.h"
+#else
+#define METADATA_QUERY GEDIT_METADATA_ATTRIBUTE_POSITION "," \
+ GEDIT_METADATA_ATTRIBUTE_ENCODING "," \
+ GEDIT_METADATA_ATTRIBUTE_LANGUAGE
+#endif
+
#undef ENABLE_PROFILE
#ifdef ENABLE_PROFILE
@@ -101,6 +108,8 @@ struct _GeditDocumentPrivate
gchar *uri;
gint untitled_number;
+ GFileInfo *metadata_info;
+
const GeditEncoding *encoding;
gchar *content_type;
@@ -212,6 +221,122 @@ release_untitled_number (gint n)
g_hash_table_remove (allocated_untitled_numbers, GINT_TO_POINTER (n));
}
+#ifdef G_OS_WIN32
+static gchar *
+get_metadata (GeditDocument *doc,
+ const gchar *key)
+{
+ gchar *value = NULL;
+
+ if (doc->priv->uri != NULL)
+ value = gedit_metadata_manager_get (doc->priv->uri, key);
+
+ return value;
+}
+
+static void
+set_metadata (GeditDocument *doc,
+ const gchar *first_key,
+ ...)
+{
+ const gchar *key;
+ const gchar *value;
+ va_list var_args;
+
+ va_start (var_args, first_key);
+
+ for (key = first_key; key; key = va_arg (var_args, const gchar *))
+ {
+ value = va_arg (var_args, const gchar *);
+
+ gedit_metadata_manager_set (doc->priv->uri,
+ key,
+ value);
+ }
+
+ va_end (var_args);
+}
+
+#else
+
+static gchar *
+get_metadata (GeditDocument *doc,
+ const gchar *key)
+{
+ gchar *value = NULL;
+
+ if (doc->priv->metadata_info && g_file_info_has_attribute (doc->priv->metadata_info,
+ key))
+ {
+ /* FIXME: Remove g_strdup when supported on win32 */
+ value = g_strdup (g_file_info_get_attribute_string (doc->priv->metadata_info,
+ key));
+ }
+
+ return value;
+}
+
+static void
+set_attributes_cb (GObject *source,
+ GAsyncResult *res,
+ gpointer useless)
+{
+ g_file_set_attributes_finish (G_FILE (source),
+ res,
+ NULL,
+ NULL);
+}
+
+/* pairs of key/value */
+static void
+set_metadata (GeditDocument *doc,
+ const gchar *first_key,
+ ...)
+{
+ const gchar *key;
+ const gchar *value;
+ va_list var_args;
+ GFile *location;
+
+ if (doc->priv->metadata_info == NULL)
+ return;
+
+ va_start (var_args, first_key);
+
+ for (key = first_key; key; key = va_arg (var_args, const gchar *))
+ {
+ value = va_arg (var_args, const gchar *);
+
+ if (value != NULL)
+ {
+ g_file_info_set_attribute_string (doc->priv->metadata_info,
+ key, value);
+ }
+ else
+ {
+ /* Unset the key */
+ g_file_info_set_attribute (doc->priv->metadata_info, key,
+ G_FILE_ATTRIBUTE_TYPE_INVALID,
+ NULL);
+ }
+ }
+
+ va_end (var_args);
+
+ location = gedit_document_get_location (doc);
+
+ g_file_set_attributes_async (location,
+ doc->priv->metadata_info,
+ G_FILE_QUERY_INFO_NONE,
+ G_PRIORITY_DEFAULT,
+ NULL,
+ set_attributes_cb,
+ NULL);
+
+ g_object_unref (location);
+}
+#endif
+
static void
gedit_document_dispose (GObject *object)
{
@@ -227,30 +352,36 @@ gedit_document_dispose (GObject *object)
{
GtkTextIter iter;
gchar *position;
+ const gchar *language = NULL;
+
+ if (doc->priv->language_set_by_user)
+ {
+ GtkSourceLanguage *lang;
+
+ lang = gedit_document_get_language (doc);
+
+ if (lang == NULL)
+ language = "_NORMAL_";
+ else
+ language = gtk_source_language_get_id (lang);
+ }
gtk_text_buffer_get_iter_at_mark (
- GTK_TEXT_BUFFER (doc),
+ GTK_TEXT_BUFFER (doc),
&iter,
gtk_text_buffer_get_insert (GTK_TEXT_BUFFER (doc)));
position = g_strdup_printf ("%d",
gtk_text_iter_get_offset (&iter));
- gedit_metadata_manager_set (doc->priv->uri,
- "position",
- position);
+ if (language == NULL)
+ set_metadata (doc, GEDIT_METADATA_ATTRIBUTE_POSITION,
+ position, NULL);
+ else
+ set_metadata (doc, GEDIT_METADATA_ATTRIBUTE_POSITION,
+ position, GEDIT_METADATA_ATTRIBUTE_LANGUAGE,
+ language, NULL);
g_free (position);
-
- if (doc->priv->language_set_by_user)
- {
- GtkSourceLanguage *lang;
-
- lang = gedit_document_get_language (doc);
-
- gedit_metadata_manager_set (doc->priv->uri,
- "language",
- (lang == NULL) ? "_NORMAL_" : gtk_source_language_get_id (lang));
- }
}
if (doc->priv->loader)
@@ -259,6 +390,12 @@ gedit_document_dispose (GObject *object)
doc->priv->loader = NULL;
}
+ if (doc->priv->metadata_info != NULL)
+ {
+ g_object_unref (doc->priv->metadata_info);
+ doc->priv->metadata_info = NULL;
+ }
+
doc->priv->dispose_has_run = TRUE;
G_OBJECT_CLASS (gedit_document_parent_class)->dispose (object);
@@ -624,9 +761,9 @@ set_language (GeditDocument *doc,
if (set_by_user && (doc->priv->uri != NULL))
{
- gedit_metadata_manager_set (doc->priv->uri,
- "language",
- (lang == NULL) ? "_NORMAL_" : gtk_source_language_get_id (lang));
+ set_metadata (doc, GEDIT_METADATA_ATTRIBUTE_LANGUAGE,
+ (lang == NULL) ? "_NORMAL_" : gtk_source_language_get_id (lang),
+ NULL);
}
doc->priv->language_set_by_user = set_by_user;
@@ -652,9 +789,7 @@ set_encoding (GeditDocument *doc,
charset = gedit_encoding_get_charset (encoding);
- gedit_metadata_manager_set (doc->priv->uri,
- "encoding",
- charset);
+ set_metadata (doc, GEDIT_METADATA_ATTRIBUTE_ENCODING, charset, NULL);
}
g_object_notify (G_OBJECT (doc), "encoding");
@@ -690,14 +825,12 @@ get_default_style_scheme (void)
static GtkSourceLanguage *
guess_language (GeditDocument *doc,
- const gchar *content_type)
-
+ const gchar *content_type)
{
- gchar *data = NULL;
+ gchar *data;
GtkSourceLanguage *language = NULL;
- if (doc->priv->uri != NULL)
- data = gedit_metadata_manager_get (doc->priv->uri, "language");
+ data = get_metadata (doc, GEDIT_METADATA_ATTRIBUTE_LANGUAGE);
if (data != NULL)
{
@@ -768,6 +901,8 @@ gedit_document_init (GeditDocument *doc)
doc->priv->uri = NULL;
doc->priv->untitled_number = get_untitled_number ();
+ doc->priv->metadata_info = NULL;
+
doc->priv->content_type = g_content_type_from_mime_type ("text/plain");
doc->priv->readonly = FALSE;
@@ -866,6 +1001,32 @@ set_content_type (GeditDocument *doc,
g_object_notify (G_OBJECT (doc), "content-type");
}
+#ifndef G_OS_WIN32
+static void
+query_info_cb (GFile *source,
+ GAsyncResult *res,
+ GeditDocument *doc)
+{
+ GError *error = NULL;
+
+ doc->priv->metadata_info = g_file_query_info_finish (source,
+ res,
+ &error);
+
+ if (error != NULL)
+ {
+ if (error->code != G_FILE_ERROR_ISDIR)
+ g_warning ("%s", error->message);
+ g_error_free (error);
+
+ return;
+ }
+
+
+ on_content_type_changed (doc, NULL, NULL);
+}
+#endif
+
static void
set_uri (GeditDocument *doc,
const gchar *uri)
@@ -889,6 +1050,25 @@ set_uri (GeditDocument *doc,
}
}
+#ifndef G_OS_WIN32
+ GFile *location;
+
+ /* Get the GFileInfo async so we can set the language from the metadata */
+ location = gedit_document_get_location (doc);
+
+ if (location != NULL)
+ {
+ g_file_query_info_async (location,
+ METADATA_QUERY,
+ G_FILE_QUERY_INFO_NONE,
+ G_PRIORITY_DEFAULT,
+ NULL,
+ (GAsyncReadyCallback) query_info_cb,
+ doc);
+ g_object_unref (location);
+ }
+#endif
+
g_object_notify (G_OBJECT (doc), "uri");
g_object_notify (G_OBJECT (doc), "shortname");
}
@@ -1106,8 +1286,7 @@ document_loader_loaded (GeditDocumentLoader *loader,
gchar *pos;
gint offset;
- pos = gedit_metadata_manager_get (doc->priv->uri,
- "position");
+ pos = get_metadata (doc, GEDIT_METADATA_ATTRIBUTE_POSITION);
offset = pos ? atoi (pos) : 0;
g_free (pos);
diff --git a/gedit/gedit-document.h b/gedit/gedit-document.h
index 7928601..f04ad95 100644
--- a/gedit/gedit-document.h
+++ b/gedit/gedit-document.h
@@ -51,6 +51,16 @@ G_BEGIN_DECLS
#define GEDIT_IS_DOCUMENT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GEDIT_TYPE_DOCUMENT))
#define GEDIT_DOCUMENT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), GEDIT_TYPE_DOCUMENT, GeditDocumentClass))
+#ifdef G_OS_WIN32
+#define GEDIT_METADATA_ATTRIBUTE_POSITION "position"
+#define GEDIT_METADATA_ATTRIBUTE_ENCODING "encoding"
+#define GEDIT_METADATA_ATTRIBUTE_LANGUAGE "language"
+#else
+#define GEDIT_METADATA_ATTRIBUTE_POSITION "metadata::gedit-position"
+#define GEDIT_METADATA_ATTRIBUTE_ENCODING "metadata::gedit-encoding"
+#define GEDIT_METADATA_ATTRIBUTE_LANGUAGE "metadata::gedit-language"
+#endif
+
typedef enum
{
GEDIT_SEARCH_DONT_SET_FLAGS = 1 << 0,
diff --git a/gedit/gedit-gio-document-loader.c b/gedit/gedit-gio-document-loader.c
index d829f5c..79b100b 100644
--- a/gedit/gedit-gio-document-loader.c
+++ b/gedit/gedit-gio-document-loader.c
@@ -40,7 +40,6 @@
#include "gedit-gio-document-loader.h"
#include "gedit-debug.h"
-#include "gedit-metadata-manager.h"
#include "gedit-utils.h"
typedef struct
@@ -55,7 +54,8 @@ typedef struct
G_FILE_ATTRIBUTE_STANDARD_TYPE "," \
G_FILE_ATTRIBUTE_TIME_MODIFIED "," \
G_FILE_ATTRIBUTE_STANDARD_SIZE "," \
- G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE
+ G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE "," \
+ GEDIT_METADATA_ATTRIBUTE_ENCODING
#define GEDIT_GIO_DOCUMENT_LOADER_GET_PRIVATE(object) \
(G_TYPE_INSTANCE_GET_PRIVATE ((object), \
diff --git a/gedit/gedit.c b/gedit/gedit.c
index f07d45d..7d8b503 100644
--- a/gedit/gedit.c
+++ b/gedit/gedit.c
@@ -50,7 +50,6 @@
#include "gedit-debug.h"
#include "gedit-dirs.h"
#include "gedit-encodings.h"
-#include "gedit-metadata-manager.h"
#include "gedit-plugins-engine.h"
#include "gedit-prefs-manager-app.h"
#include "gedit-session.h"
@@ -67,6 +66,7 @@
#include <conio.h>
#define _WIN32_WINNT 0x0500
#include <windows.h>
+#include "gedit-metadata-manager.h"
#define DATADIR SAVE_DATADIR
#undef SAVE_DATADIR
#endif
@@ -749,7 +749,10 @@ main (int argc, char *argv[])
*/
g_object_unref (engine);
gedit_prefs_manager_app_shutdown ();
+
+#ifdef G_OS_WIN32
gedit_metadata_manager_shutdown ();
+#endif
return 0;
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]