]> git.0d.be Git - empathy.git/blobdiff - libempathy/empathy-status-presets.c
don't pass a GError when first trying to start gnome-contacts
[empathy.git] / libempathy / empathy-status-presets.c
index 9f9a2030bccd4fa6c69e0d30c6fef036105a65dc..aacee83d82fa112e0af8daa05bcd2dc86fd76062 100644 (file)
  */
 
 #include "config.h"
+#include "empathy-status-presets.h"
 
-#include <sys/types.h>
 #include <sys/stat.h>
-#include <string.h>
-
-#include <glib.h>
-#include <glib/gi18n-lib.h>
-
-#include <libxml/parser.h>
-#include <libxml/tree.h>
-
-#include <telepathy-glib/util.h>
+#include <tp-account-widgets/tpaw-utils.h>
 
 #include "empathy-utils.h"
-#include "empathy-status-presets.h"
 
 #define DEBUG_FLAG EMPATHY_DEBUG_OTHER
 #include "empathy-debug.h"
 
 #define STATUS_PRESETS_XML_FILENAME "status-presets.xml"
-#define STATUS_PRESETS_DTD_FILENAME "empathy-status-presets.dtd"
+#define STATUS_PRESETS_DTD_RESOURCENAME "/org/gnome/Empathy/empathy-status-presets.dtd"
 #define STATUS_PRESETS_MAX_EACH     15
 
 typedef struct {
@@ -102,7 +93,7 @@ status_presets_file_parse (const gchar *filename)
                return;
        }
 
-       if (!empathy_xml_validate (doc, STATUS_PRESETS_DTD_FILENAME)) {
+       if (!tpaw_xml_validate_from_resource (doc, STATUS_PRESETS_DTD_RESOURCENAME)) {
                g_warning ("Failed to validate file:'%s'", filename);
                xmlFreeDoc (doc);
                xmlFreeParserCtxt (ctxt);
@@ -127,20 +118,21 @@ status_presets_file_parse (const gchar *filename)
                        }
 
                        status = (gchar *) xmlNodeGetContent (node);
-                       state_str = (gchar *) xmlGetProp (node, "presence");
+                       state_str = (gchar *) xmlGetProp (node, (const xmlChar *) "presence");
 
                        if (state_str) {
                                state = empathy_presence_from_str (state_str);
-
-                               if (is_default) {
-                                       DEBUG ("Default status preset state is:"
-                                               " '%s', status:'%s'", state_str,
-                                               status);
-
-                                       status_presets_set_default (state, status);
-                               } else {
-                                       preset = status_preset_new (state, status);
-                                       presets = g_list_append (presets, preset);
+                               if (empathy_status_presets_is_valid (state)) {
+                                       if (is_default) {
+                                               DEBUG ("Default status preset state is:"
+                                                       " '%s', status:'%s'", state_str,
+                                                       status);
+
+                                               status_presets_set_default (state, status);
+                                       } else {
+                                               preset = status_preset_new (state, status);
+                                               presets = g_list_append (presets, preset);
+                                       }
                                }
                        }
 
@@ -175,7 +167,7 @@ empathy_status_presets_get_all (void)
                presets = NULL;
        }
 
-       dir = g_build_filename (g_get_home_dir (), ".gnome2", PACKAGE_NAME, NULL);
+       dir = g_build_filename (g_get_user_config_dir (), PACKAGE_NAME, NULL);
        g_mkdir_with_parents (dir, S_IRUSR | S_IWUSR | S_IXUSR);
        file_with_path = g_build_filename (dir, STATUS_PRESETS_XML_FILENAME, NULL);
        g_free (dir);
@@ -195,31 +187,31 @@ status_presets_file_save (void)
        GList      *l;
        gchar      *dir;
        gchar      *file;
-       gint        count[NUM_TP_CONNECTION_PRESENCE_TYPES];
+       gint        count[TP_NUM_CONNECTION_PRESENCE_TYPES];
        gint        i;
 
-       for (i = 0; i < NUM_TP_CONNECTION_PRESENCE_TYPES; i++) {
+       for (i = 0; i < TP_NUM_CONNECTION_PRESENCE_TYPES; i++) {
                count[i] = 0;
        }
 
-       dir = g_build_filename (g_get_home_dir (), ".gnome2", PACKAGE_NAME, NULL);
+       dir = g_build_filename (g_get_user_config_dir (), PACKAGE_NAME, NULL);
        g_mkdir_with_parents (dir, S_IRUSR | S_IWUSR | S_IXUSR);
        file = g_build_filename (dir, STATUS_PRESETS_XML_FILENAME, NULL);
        g_free (dir);
 
-       doc = xmlNewDoc ("1.0");
-       root = xmlNewNode (NULL, "presets");
+       doc = xmlNewDoc ((const xmlChar *) "1.0");
+       root = xmlNewNode (NULL, (const xmlChar *) "presets");
        xmlDocSetRootElement (doc, root);
 
        if (default_preset) {
                xmlNodePtr  subnode;
                xmlChar    *state;
 
-               state = (gchar *) empathy_presence_to_str (default_preset->state);
+               state = (xmlChar *) empathy_presence_to_str (default_preset->state);
 
-               subnode = xmlNewTextChild (root, NULL, "default",
-                                          default_preset->status);
-               xmlNewProp (subnode, "presence", state);
+               subnode = xmlNewTextChild (root, NULL, (const xmlChar *) "default",
+                                         (const xmlChar *) default_preset->status);
+               xmlNewProp (subnode, (const xmlChar *) "presence", state);
        }
 
        for (l = presets; l; l = l->next) {
@@ -228,7 +220,7 @@ status_presets_file_save (void)
                xmlChar      *state;
 
                sp = l->data;
-               state = (gchar *) empathy_presence_to_str (sp->state);
+               state = (xmlChar *) empathy_presence_to_str (sp->state);
 
                count[sp->state]++;
                if (count[sp->state] > STATUS_PRESETS_MAX_EACH) {
@@ -236,8 +228,8 @@ status_presets_file_save (void)
                }
 
                subnode = xmlNewTextChild (root, NULL,
-                                          "status", sp->status);
-               xmlNewProp (subnode, "presence", state);
+                                          (const xmlChar *) "status", (const xmlChar *) sp->status);
+               xmlNewProp (subnode, (const xmlChar *) "presence", state);
        }
 
        /* Make sure the XML is indented properly */
@@ -405,3 +397,33 @@ empathy_status_presets_clear_default (void)
 
        status_presets_file_save ();
 }
+
+/**
+ * empathy_status_presets_is_valid:
+ * @state: a #TpConnectionPresenceType
+ *
+ * Check if a presence type can be used as a preset.
+ *
+ * Returns: %TRUE if the presence type can be used as a preset.
+ */
+gboolean
+empathy_status_presets_is_valid (TpConnectionPresenceType state)
+{
+       switch (state) {
+               case TP_CONNECTION_PRESENCE_TYPE_UNSET:
+               case TP_CONNECTION_PRESENCE_TYPE_OFFLINE:
+               case TP_CONNECTION_PRESENCE_TYPE_UNKNOWN:
+               case TP_CONNECTION_PRESENCE_TYPE_ERROR:
+                       return FALSE;
+
+               case TP_CONNECTION_PRESENCE_TYPE_AVAILABLE:
+               case TP_CONNECTION_PRESENCE_TYPE_AWAY:
+               case TP_CONNECTION_PRESENCE_TYPE_EXTENDED_AWAY:
+               case TP_CONNECTION_PRESENCE_TYPE_HIDDEN:
+               case TP_CONNECTION_PRESENCE_TYPE_BUSY:
+                       return TRUE;
+
+               default:
+                       return FALSE;
+       }
+}