Yasumichi Akahoshi
yasum****@users*****
2005年 8月 31日 (水) 23:15:22 JST
Index: libcxp/src/Makefile.am diff -u libcxp/src/Makefile.am:1.13 libcxp/src/Makefile.am:1.14 --- libcxp/src/Makefile.am:1.13 Tue Aug 23 21:22:40 2005 +++ libcxp/src/Makefile.am Wed Aug 31 23:15:22 2005 @@ -8,7 +8,7 @@ libcxp_la_LDFLAGS = libcxp_la_SOURCES = cxp-handler.c \ cxp-utils.c \ - cxp-mime-utils.c \ + cxp-mime-manager.c \ cxp-about-dialog.c \ cxp-entry-dialog.c \ cxp-gconf-pair-editor.c \ Index: libcxp/src/Makefile.in diff -u libcxp/src/Makefile.in:1.14 libcxp/src/Makefile.in:1.15 --- libcxp/src/Makefile.in:1.14 Tue Aug 23 21:22:40 2005 +++ libcxp/src/Makefile.in Wed Aug 31 23:15:22 2005 @@ -51,7 +51,7 @@ LTLIBRARIES = $(lib_LTLIBRARIES) am__DEPENDENCIES_1 = libcxp_la_DEPENDENCIES = $(am__DEPENDENCIES_1) $(am__DEPENDENCIES_1) -am_libcxp_la_OBJECTS = cxp-handler.lo cxp-utils.lo cxp-mime-utils.lo \ +am_libcxp_la_OBJECTS = cxp-handler.lo cxp-utils.lo cxp-mime-manager.lo \ cxp-about-dialog.lo cxp-entry-dialog.lo \ cxp-gconf-pair-editor.lo xdgmime.lo xdgmimealias.lo \ xdgmimecache.lo xdgmimeglob.lo xdgmimeint.lo xdgmimemagic.lo \ @@ -64,7 +64,7 @@ @AMDEP_TRUE@ ./$(DEPDIR)/cxp-entry-dialog.Plo \ @AMDEP_TRUE@ ./$(DEPDIR)/cxp-gconf-pair-editor.Plo \ @AMDEP_TRUE@ ./$(DEPDIR)/cxp-handler.Plo \ - @ AMDEP_TRUE@ ./$(DEPDIR)/cxp-mime-utils.Plo \ + @ AMDEP_TRUE@ ./$(DEPDIR)/cxp-mime-manager.Plo \ @AMDEP_TRUE@ ./$(DEPDIR)/cxp-utils.Plo ./$(DEPDIR)/xdgmime.Plo \ @AMDEP_TRUE@ ./$(DEPDIR)/xdgmimealias.Plo \ @AMDEP_TRUE@ ./$(DEPDIR)/xdgmimecache.Plo \ @@ -234,7 +234,7 @@ libcxp_la_LDFLAGS = libcxp_la_SOURCES = cxp-handler.c \ cxp-utils.c \ - cxp-mime-utils.c \ + cxp-mime-manager.c \ cxp-about-dialog.c \ cxp-entry-dialog.c \ cxp-gconf-pair-editor.c \ @@ -326,7 +326,7 @@ @AMDEP_TRUE@@am__include@ @am__quote @ ./$(DEPDIR)/cxp-entry-dialog.Plo @ am__quote@ @AMDEP_TRUE@@am__include@ @am__quote @ ./$(DEPDIR)/cxp-gconf-pair-editor.Plo @ am__quote@ @AMDEP_TRUE@@am__include@ @am__quote @ ./$(DEPDIR)/cxp-handler.Plo @ am__quote@ - @ AMDEP_TRUE@@am__include@ @am__quote @ ./$(DEPDIR)/cxp-mime-utils.Plo @ am__quote@ + @ AMDEP_TRUE@@am__include@ @am__quote @ ./$(DEPDIR)/cxp-mime-manager.Plo @ am__quote@ @AMDEP_TRUE@@am__include@ @am__quote @ ./$(DEPDIR)/cxp-utils.Plo @ am__quote@ @AMDEP_TRUE@@am__include@ @am__quote @ ./$(DEPDIR)/xdgmime.Plo @ am__quote@ @AMDEP_TRUE@@am__include@ @am__quote @ ./$(DEPDIR)/xdgmimealias.Plo @ am__quote@ Index: libcxp/src/cxp-mime-manager.c diff -u /dev/null libcxp/src/cxp-mime-manager.c:1.1 --- /dev/null Wed Aug 31 23:15:22 2005 +++ libcxp/src/cxp-mime-manager.c Wed Aug 31 23:15:22 2005 @@ -0,0 +1,257 @@ +/*************************************************************************** + * cxp-mime-manager.c + * + * Wed 8 31 20:59:53 2005 + * Copyright 2005 Yasumichi Akahoshi + * yasum****@users***** + ***************************************************************************/ + +/* + * 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 Library General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#include "cxp-mime-manager.h" + +static void cxp_mime_manager_class_init(CxpMimeManagerClass *klass); +static void cxp_mime_manager_init(CxpMimeManager *sp); +static void cxp_mime_manager_finalize(GObject *object); + +struct CxpMimeManagerPrivate { + /* Place Private Members Here */ + gchar *lang; + gchar *country; + gchar *last_matched; + gchar *common; + gint parse_flag; + GHashTable *comments; +}; + +typedef enum { + /* Place Signal Types Here */ + SIGNAL_TYPE_EXAMPLE, + LAST_SIGNAL +} CxpMimeManagerSignalType; + +typedef struct { + CxpMimeManager *object; +} CxpMimeManagerSignal; + +enum { + NO_NEED_PARSE, + NEED_PARSE, + NEED_GET_COMMON +}; + +static guint cxp_mime_manager_signals[LAST_SIGNAL] = { 0 }; +static GObjectClass *parent_class = NULL; + +GType +cxp_mime_manager_get_type() +{ + static GType type = 0; + + if(type == 0) { + static const GTypeInfo our_info = { + sizeof (CxpMimeManagerClass), + NULL, + NULL, + (GClassInitFunc)cxp_mime_manager_class_init, + NULL, + NULL, + sizeof (CxpMimeManager), + 0, + (GInstanceInitFunc)cxp_mime_manager_init, + }; + + type = g_type_register_static(G_TYPE_OBJECT, + "CxpMimeManager", &our_info, 0); + } + + return type; +} + +static void +cxp_mime_manager_class_init(CxpMimeManagerClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS(klass); + + parent_class = g_type_class_peek_parent(klass); + object_class->finalize = cxp_mime_manager_finalize; + + /* Create signals here: + cxp_mime_manager_signals[SIGNAL_TYPE_EXAMPLE] = g_signal_new(...) + */ +} + +static void +cxp_mime_manager_init(CxpMimeManager *obj) +{ + gchar **elements; + + obj->priv = g_new0(CxpMimeManagerPrivate, 1); + /* Initialize private members, etc. */ + obj->priv->lang = g_strdup(g_getenv("LANG")); + elements = g_strsplit (obj->priv->lang, "_", 2); + obj->priv->country = g_strdup(elements[0]); + g_strfreev (elements); + obj->priv->last_matched = NULL; + obj->priv->common = NULL; + obj->priv->parse_flag = NO_NEED_PARSE; + obj->priv->comments = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); +} + +static void +cxp_mime_manager_finalize(GObject *object) +{ + CxpMimeManager *cobj; + cobj = CXP_MIME_MANAGER(object); + + /* Free private members, etc. */ + g_free(cobj->priv->lang); + g_free(cobj->priv->country); + g_hash_table_destroy (cobj->priv->comments); + + g_free(cobj->priv); + G_OBJECT_CLASS(parent_class)->finalize(object); +} + +CxpMimeManager * +cxp_mime_manager_new() +{ + CxpMimeManager *obj; + + obj = CXP_MIME_MANAGER(g_object_new(CXP_TYPE_MIME_MANAGER, NULL)); + + return obj; +} + +/* ã¿ã°ãéãã¦ãããå¼ã³åºãããã³ã¼ã«ããã¯ã§ã; ãã¨ãã° <foo bar="baz"> */ +static void cxp_mime_manager_on_start_element (GMarkupParseContext * context, + const gchar * element_name, + const gchar ** attribute_names, + const gchar ** attribute_values, + gpointer user_data, GError ** error) +{ + CxpMimeManager *manager = CXP_MIME_MANAGER(user_data); + gint idx; + + if(strcmp(element_name, "comment") == 0) + { + if(attribute_names[0] == NULL) + { + manager->priv->parse_flag = NEED_GET_COMMON; + } + else + { + for(idx=0; attribute_names[idx] != NULL; idx++) + { + if(strcmp(attribute_names[idx], "xml:lang") == 0) + { + if(strcmp(attribute_values[idx], manager->priv->lang) == 0) + { + manager->priv->parse_flag = NEED_PARSE; + } + else if(strcmp(attribute_values[idx], manager->priv->country) == 0) + { + manager->priv->parse_flag = NEED_PARSE; + } + else + { + manager->priv->parse_flag = NO_NEED_PARSE; + } + } + } + } + } +} + +/* ã¿ã°ãéãã¦ãããå¼ã³åºãããã³ã¼ã«ããã¯ã§ã; ãã¨ãã° </foo> */ +static void cxp_mime_manager_on_end_element (GMarkupParseContext * context, + const gchar * element_name, + gpointer user_data, GError ** error) +{ + CxpMimeManager *manager = CXP_MIME_MANAGER(user_data); + + manager->priv->parse_flag = NO_NEED_PARSE; +} + +/* æåãã¼ã¿ã ã£ããå¼ã³åºãããã³ã¼ã«ããã¯ã§ãã*/ +/* 'text' ã®çµç«¯ã¯ãã«æå '\0' ã«ãã¦ä¸ããã*/ +static void cxp_mime_manager_on_text (GMarkupParseContext * context, + const gchar * text, + gsize text_len, gpointer user_data, GError ** error) +{ + CxpMimeManager *manager = CXP_MIME_MANAGER(user_data); + + switch(manager->priv->parse_flag) + { + case NO_NEED_PARSE: + break; + case NEED_PARSE: + manager->priv->last_matched = g_strdup(text); + break; + case NEED_GET_COMMON: + manager->priv->common = g_strdup(text); + break; + } +} + +gchar *cxp_mime_manager_get_comment_from_mime (CxpMimeManager *manager, const gchar *mimetype) +{ + gchar *result; + gchar *filename; + gchar *content; + gssize len; + GMarkupParseContext *context; + GMarkupParser parser = { + cxp_mime_manager_on_start_element, + cxp_mime_manager_on_end_element, + cxp_mime_manager_on_text, + NULL, + NULL + }; + + if((result = g_strdup(g_hash_table_lookup(manager->priv->comments, mimetype))) == NULL) + { + manager->priv->parse_flag = NO_NEED_PARSE; + + filename = g_strdup_printf("/usr/share/mime/%s.xml", mimetype); + if (g_file_get_contents (filename, &content, &len, NULL)) + { + context = g_markup_parse_context_new (&parser, 0, manager, NULL); + g_markup_parse_context_parse (context, content, len, NULL); + g_markup_parse_context_free (context); + if(manager->priv->last_matched != NULL) + { + result = g_strdup (manager->priv->last_matched); + g_hash_table_replace (manager->priv->comments, g_strdup(mimetype), manager->priv->last_matched); + g_free (manager->priv->common); + } + else + { + result = g_strdup (manager->priv->common); + g_hash_table_replace (manager->priv->comments, g_strdup(mimetype), manager->priv->common); + } + } + else + { + result = g_strdup ("unknown file"); + g_hash_table_replace (manager->priv->comments, g_strdup(mimetype), g_strdup("unknown file")); + } + g_free (filename); + } + + return result; +}