]> git.0d.be Git - empathy.git/commitdiff
Move empathy-status-presets to libempathy, nothing to do with GTK
authorXavier Claessens <xclaesse@src.gnome.org>
Mon, 21 Jan 2008 14:12:26 +0000 (14:12 +0000)
committerXavier Claessens <xclaesse@src.gnome.org>
Mon, 21 Jan 2008 14:12:26 +0000 (14:12 +0000)
svn path=/trunk/; revision=591

libempathy-gtk/Makefile.am
libempathy-gtk/empathy-main-window.c
libempathy-gtk/empathy-presence-chooser.c
libempathy-gtk/empathy-status-presets.c [deleted file]
libempathy-gtk/empathy-status-presets.dtd [deleted file]
libempathy-gtk/empathy-status-presets.h [deleted file]
libempathy/Makefile.am
libempathy/empathy-status-presets.c [new file with mode: 0644]
libempathy/empathy-status-presets.dtd [new file with mode: 0644]
libempathy/empathy-status-presets.h [new file with mode: 0644]

index 07336f55cb8997a30b666897ad802fe582a0a2a6..002762138d24185d141c8b72c16c9feb5e9152c2 100644 (file)
@@ -43,7 +43,6 @@ libempathy_gtk_la_SOURCES =                   \
        empathy-private-chat.c                  \
        empathy-group-chat.c                    \
        empathy-geometry.c                      \
-       empathy-status-presets.c                \
        empathy-presence-chooser.c              \
        empathy-about-dialog.c                  \
        empathy-account-chooser.c               \
@@ -99,7 +98,6 @@ libempathy_gtk_headers =                      \
        empathy-private-chat.h                  \
        empathy-group-chat.h                    \
        empathy-geometry.h                      \
-       empathy-status-presets.h                \
        empathy-presence-chooser.h              \
        empathy-about-dialog.h                  \
        empathy-account-chooser.h               \
@@ -178,7 +176,6 @@ empathy-gtk-enum-types.c: Makefile $(libempathy_gtk_headers)
 
 dtddir = $(datadir)/empathy
 dtd_DATA =                                     \
-       empathy-status-presets.dtd              \
        empathy-contact-groups.dtd
 
 pkgconfigdir = $(libdir)/pkgconfig
index d0353419a6dd27beeef379feac9891648c857068..7970cb1f950c131a8399d35a926dc02f781084a8 100644 (file)
@@ -37,6 +37,7 @@
 #include <libempathy/empathy-contact-list.h>
 #include <libempathy/empathy-contact-manager.h>
 #include <libempathy/empathy-contact-factory.h>
+#include <libempathy/empathy-status-presets.h>
 
 #include "empathy-main-window.h"
 #include "empathy-contact-dialogs.h"
@@ -45,7 +46,6 @@
 #include "empathy-contact-list-view.h"
 #include "empathy-presence-chooser.h"
 #include "empathy-ui-utils.h"
-#include "empathy-status-presets.h"
 #include "empathy-geometry.h"
 #include "empathy-preferences.h"
 #include "empathy-accounts-dialog.h"
index 17a627389469736e6b07b0172d281505d1ba0490..c64962afa64996aef21805cfec7b95538e8ded47 100644 (file)
 #include <libempathy/empathy-utils.h>
 #include <libempathy/empathy-debug.h>
 #include <libempathy/empathy-marshal.h>
+#include <libempathy/empathy-status-presets.h>
 
 #include "empathy-ui-utils.h"
 #include "empathy-images.h"
 #include "empathy-presence-chooser.h"
-#include "empathy-status-presets.h"
 
 #define GET_PRIV(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), EMPATHY_TYPE_PRESENCE_CHOOSER, EmpathyPresenceChooserPriv))
 
diff --git a/libempathy-gtk/empathy-status-presets.c b/libempathy-gtk/empathy-status-presets.c
deleted file mode 100644 (file)
index 13b71bc..0000000
+++ /dev/null
@@ -1,408 +0,0 @@
-/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
-/*
- * Copyright (C) 2005-2007 Imendio AB
- *
- * 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
- * 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.
- *
- * Author: Martyn Russell <martyn@imendio.com>
- */
-
-#include "config.h"
-
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <string.h>
-
-#include <glib.h>
-#include <glib/gi18n.h>
-
-#include <libxml/parser.h>
-#include <libxml/tree.h>
-
-#include <telepathy-glib/util.h>
-
-#include <libempathy/empathy-debug.h>
-#include <libempathy/empathy-utils.h>
-
-#include "empathy-status-presets.h"
-
-#define DEBUG_DOMAIN "StatusPresets"
-
-#define STATUS_PRESETS_XML_FILENAME "status-presets.xml"
-#define STATUS_PRESETS_DTD_FILENAME "empathy-status-presets.dtd"
-#define STATUS_PRESETS_MAX_EACH     15
-
-typedef struct {
-       gchar      *status;
-       McPresence  state;
-} StatusPreset;
-
-static StatusPreset *status_preset_new          (McPresence    state,
-                                                const gchar  *status);
-static void     status_preset_free              (StatusPreset *status);
-static void     status_presets_file_parse       (const gchar  *filename);
-const gchar *   status_presets_get_state_as_str (McPresence    state);
-static gboolean status_presets_file_save        (void);
-static void     status_presets_set_default      (McPresence    state,
-                                                const gchar  *status);
-
-static GList        *presets = NULL;
-static StatusPreset *default_preset = NULL;
-
-static StatusPreset *
-status_preset_new (McPresence   state,
-                  const gchar *status)
-{
-       StatusPreset *preset;
-
-       preset = g_new0 (StatusPreset, 1);
-
-       preset->status = g_strdup (status);
-       preset->state = state;
-
-       return preset;
-}
-
-static void
-status_preset_free (StatusPreset *preset)
-{
-       g_free (preset->status);
-       g_free (preset);
-}
-
-static void
-status_presets_file_parse (const gchar *filename)
-{
-       xmlParserCtxtPtr ctxt;
-       xmlDocPtr        doc;
-       xmlNodePtr       presets_node;
-       xmlNodePtr       node;
-
-       empathy_debug (DEBUG_DOMAIN, "Attempting to parse file:'%s'...", filename);
-
-       ctxt = xmlNewParserCtxt ();
-
-       /* Parse and validate the file. */
-       doc = xmlCtxtReadFile (ctxt, filename, NULL, 0);
-       if (!doc) {
-               g_warning ("Failed to parse file:'%s'", filename);
-               xmlFreeParserCtxt (ctxt);
-               return;
-       }
-
-       if (!empathy_xml_validate (doc, STATUS_PRESETS_DTD_FILENAME)) {
-               g_warning ("Failed to validate file:'%s'", filename);
-               xmlFreeDoc(doc);
-               xmlFreeParserCtxt (ctxt);
-               return;
-       }
-
-       /* The root node, presets. */
-       presets_node = xmlDocGetRootElement (doc);
-
-       node = presets_node->children;
-       while (node) {
-               if (strcmp ((gchar *) node->name, "status") == 0 ||
-                   strcmp ((gchar *) node->name, "default") == 0) {
-                       McPresence    state;
-                       gchar        *status;
-                       gchar        *state_str;
-                       StatusPreset *preset;
-                       gboolean      is_default = FALSE;
-
-                       if (strcmp ((gchar *) node->name, "default") == 0) {
-                               is_default = TRUE;
-                       }
-
-                       status = (gchar *) xmlNodeGetContent (node);
-                       state_str = (gchar *) xmlGetProp (node, "presence");
-
-                       if (state_str) {
-                               state = empathy_presence_state_from_str (state_str);
-
-                               if (is_default) {
-                                       empathy_debug (DEBUG_DOMAIN,
-                                                     "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);
-                               }
-                       }
-
-                       xmlFree (status);
-                       xmlFree (state_str);
-               }
-
-               node = node->next;
-       }
-
-       /* Use the default if not set */
-       if (!default_preset) {
-               status_presets_set_default (MC_PRESENCE_OFFLINE, NULL);
-       }
-
-       empathy_debug (DEBUG_DOMAIN, "Parsed %d status presets", g_list_length (presets));
-
-       xmlFreeDoc (doc);
-       xmlFreeParserCtxt (ctxt);
-}
-
-void
-empathy_status_presets_get_all (void)
-{
-       gchar *dir;
-       gchar *file_with_path;
-
-       /* If already set up clean up first. */
-       if (presets) {
-               g_list_foreach (presets, (GFunc) status_preset_free, NULL);
-               g_list_free (presets);
-               presets = NULL;
-       }
-
-       dir = g_build_filename (g_get_home_dir (), ".gnome2", 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);
-
-       if (g_file_test (file_with_path, G_FILE_TEST_EXISTS)) {
-               status_presets_file_parse (file_with_path);
-       }
-
-       g_free (file_with_path);
-}
-
-static gboolean
-status_presets_file_save (void)
-{
-       xmlDocPtr   doc;
-       xmlNodePtr  root;
-       GList      *l;
-       gchar      *dir;
-       gchar      *file;
-       gint        count[LAST_MC_PRESENCE];
-       gint        i;
-
-       for (i = 0; i < LAST_MC_PRESENCE; i++) {
-               count[i] = 0;
-       }
-
-       dir = g_build_filename (g_get_home_dir (), ".gnome2", 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");
-       xmlDocSetRootElement (doc, root);
-
-       if (default_preset) {
-               xmlNodePtr  subnode;
-               xmlChar    *state;
-
-               state = (gchar*) empathy_presence_state_to_str (default_preset->state);
-
-               subnode = xmlNewTextChild (root, NULL, "default",
-                                          default_preset->status);
-               xmlNewProp (subnode, "presence", state);
-       }
-
-       for (l = presets; l; l = l->next) {
-               StatusPreset *sp;
-               xmlNodePtr    subnode;
-               xmlChar      *state;
-
-               sp = l->data;
-               state = (gchar*) empathy_presence_state_to_str (sp->state);
-
-               count[sp->state]++;
-               if (count[sp->state] > STATUS_PRESETS_MAX_EACH) {
-                       continue;
-               }
-
-               subnode = xmlNewTextChild (root, NULL,
-                                          "status", sp->status);
-               xmlNewProp (subnode, "presence", state);
-       }
-
-       /* Make sure the XML is indented properly */
-       xmlIndentTreeOutput = 1;
-
-       empathy_debug (DEBUG_DOMAIN, "Saving file:'%s'", file);
-       xmlSaveFormatFileEnc (file, doc, "utf-8", 1);
-       xmlFreeDoc (doc);
-
-       g_free (file);
-
-       return TRUE;
-}
-
-GList *
-empathy_status_presets_get (McPresence state,
-                          gint       max_number)
-{
-       GList *list = NULL;
-       GList *l;
-       gint   i;
-
-       i = 0;
-       for (l = presets; l; l = l->next) {
-               StatusPreset *sp;
-
-               sp = l->data;
-
-               if (sp->state != state) {
-                       continue;
-               }
-
-               list = g_list_append (list, sp->status);
-               i++;
-
-               if (max_number != -1 && i >= max_number) {
-                       break;
-               }
-       }
-
-       return list;
-}
-
-void
-empathy_status_presets_set_last (McPresence   state,
-                               const gchar *status)
-{
-       GList        *l;
-       StatusPreset *preset;
-       gint          num;
-
-       /* Check if duplicate */
-       for (l = presets; l; l = l->next) {
-               preset = l->data;
-
-               if (state == preset->state &&
-                   !tp_strdiff (status, preset->status)) {
-                       return;
-               }
-       }
-
-       preset = status_preset_new (state, status);
-       presets = g_list_prepend (presets, preset);
-
-       num = 0;
-       for (l = presets; l; l = l->next) {
-               preset = l->data;
-
-               if (state != preset->state) {
-                       continue;
-               }
-
-               num++;
-
-               if (num > STATUS_PRESETS_MAX_EACH) {
-                       status_preset_free (preset);
-                       presets = g_list_delete_link (presets, l);
-                       break;
-               }
-       }
-
-       status_presets_file_save ();
-}
-
-void
-empathy_status_presets_remove (McPresence   state,
-                              const gchar *status)
-{
-       StatusPreset *preset;
-       GList        *l;
-
-       for (l = presets; l; l = l->next) {
-               preset = l->data;
-
-               if (state == preset->state &&
-                   !tp_strdiff (status, preset->status)) {
-                       status_preset_free (preset);
-                       presets = g_list_delete_link (presets, l);
-                       status_presets_file_save ();
-                       break;
-               }
-       }
-}
-
-void
-empathy_status_presets_reset (void)
-{
-       g_list_foreach (presets, (GFunc) status_preset_free, NULL);
-       g_list_free (presets);
-
-       presets = NULL;
-
-       status_presets_set_default (MC_PRESENCE_AVAILABLE, NULL);
-
-       status_presets_file_save ();
-}
-
-McPresence
-empathy_status_presets_get_default_state (void)
-{
-       if (!default_preset) {
-               return MC_PRESENCE_OFFLINE;
-       }
-
-       return default_preset->state;
-}
-
-const gchar *
-empathy_status_presets_get_default_status (void)
-{
-       if (!default_preset ||
-           !default_preset->status) {
-               return NULL;
-       }
-
-       return default_preset->status;
-}
-
-static void
-status_presets_set_default (McPresence   state,
-                           const gchar *status)
-{
-       if (default_preset) {
-               status_preset_free (default_preset);
-       }
-
-       default_preset = status_preset_new (state, status);
-}
-
-void
-empathy_status_presets_set_default (McPresence   state,
-                                  const gchar *status)
-{
-       status_presets_set_default (state, status);
-       status_presets_file_save ();
-}
-
-void
-empathy_status_presets_clear_default (void)
-{
-       if (default_preset) {
-               status_preset_free (default_preset);
-               default_preset = NULL;
-       }
-
-       status_presets_file_save ();
-}
diff --git a/libempathy-gtk/empathy-status-presets.dtd b/libempathy-gtk/empathy-status-presets.dtd
deleted file mode 100644 (file)
index 872be6b..0000000
+++ /dev/null
@@ -1,14 +0,0 @@
-<!--
-  DTD for Empathys status presets.
-  by Martyn Russell <martyn@imendio.com>
--->
-
-<!-- Root element. -->
-<!ELEMENT presets ((default?),status*)>
-
-<!ELEMENT default (#PCDATA)>
-<!ATTLIST default presence CDATA #REQUIRED>
-
-<!ELEMENT status (#PCDATA)>
-<!ATTLIST status presence CDATA #REQUIRED>
-
diff --git a/libempathy-gtk/empathy-status-presets.h b/libempathy-gtk/empathy-status-presets.h
deleted file mode 100644 (file)
index 8a4564f..0000000
+++ /dev/null
@@ -1,46 +0,0 @@
-/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
-/*
- * Copyright (C) 2005-2007 Imendio AB
- *
- * 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
- * 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.
- *
- * Author: Martyn Russell <martyn@imendio.com>
- */
-
-#ifndef __EMPATHY_STATUS_PRESETS_H__
-#define __EMPATHY_STATUS_PRESETS_H__
-
-#include <libempathy/empathy-presence.h>
-
-G_BEGIN_DECLS
-
-void          empathy_status_presets_get_all            (void);
-GList *       empathy_status_presets_get                (McPresence   state,
-                                                        gint         max_number);
-void          empathy_status_presets_set_last           (McPresence   state,
-                                                        const gchar *status);
-void          empathy_status_presets_remove             (McPresence   state,
-                                                        const gchar *status);
-void          empathy_status_presets_reset              (void);
-McPresence    empathy_status_presets_get_default_state  (void);
-const gchar * empathy_status_presets_get_default_status (void);
-void          empathy_status_presets_set_default        (McPresence   state,
-                                                        const gchar *status);
-void          empathy_status_presets_clear_default      (void);
-
-G_END_DECLS
-
-#endif /* __EMPATHY_STATUS_PRESETS_H__ */
index 733289f519d401eaea17bbe695bd7dd3918f54cb..c6c7faa8e4e2dff73dcc3094596ecab3077948bd 100644 (file)
@@ -20,6 +20,7 @@ libempathy_la_SOURCES =                                       \
        empathy-avatar.c                                \
        empathy-time.c                                  \
        empathy-presence.c                              \
+       empathy-status-presets.c                \
        empathy-debug.c                                 \
        empathy-utils.c                                 \
        empathy-message.c                               \
@@ -56,6 +57,7 @@ libempathy_headers =                          \
        empathy-avatar.h                        \
        empathy-time.h                          \
        empathy-presence.h                      \
+       empathy-status-presets.h                \
        empathy-debug.h                         \
        empathy-utils.h                         \
        empathy-message.h                       \
@@ -139,6 +141,7 @@ empathy-enum-types.c: $(libempathy_headers) Makefile
 
 dtddir = $(datadir)/empathy
 dtd_DATA =                                     \
+       empathy-status-presets.dtd              \
        empathy-chatroom-manager.dtd
 
 stylesheetdir = $(datadir)/empathy
diff --git a/libempathy/empathy-status-presets.c b/libempathy/empathy-status-presets.c
new file mode 100644 (file)
index 0000000..54f7b62
--- /dev/null
@@ -0,0 +1,407 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+/*
+ * Copyright (C) 2005-2007 Imendio AB
+ *
+ * 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
+ * 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.
+ *
+ * Author: Martyn Russell <martyn@imendio.com>
+ */
+
+#include "config.h"
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <string.h>
+
+#include <glib.h>
+#include <glib/gi18n.h>
+
+#include <libxml/parser.h>
+#include <libxml/tree.h>
+
+#include <telepathy-glib/util.h>
+
+#include "empathy-debug.h"
+#include "empathy-utils.h"
+#include "empathy-status-presets.h"
+
+#define DEBUG_DOMAIN "StatusPresets"
+
+#define STATUS_PRESETS_XML_FILENAME "status-presets.xml"
+#define STATUS_PRESETS_DTD_FILENAME "empathy-status-presets.dtd"
+#define STATUS_PRESETS_MAX_EACH     15
+
+typedef struct {
+       gchar      *status;
+       McPresence  state;
+} StatusPreset;
+
+static StatusPreset *status_preset_new          (McPresence    state,
+                                                const gchar  *status);
+static void     status_preset_free              (StatusPreset *status);
+static void     status_presets_file_parse       (const gchar  *filename);
+const gchar *   status_presets_get_state_as_str (McPresence    state);
+static gboolean status_presets_file_save        (void);
+static void     status_presets_set_default      (McPresence    state,
+                                                const gchar  *status);
+
+static GList        *presets = NULL;
+static StatusPreset *default_preset = NULL;
+
+static StatusPreset *
+status_preset_new (McPresence   state,
+                  const gchar *status)
+{
+       StatusPreset *preset;
+
+       preset = g_new0 (StatusPreset, 1);
+
+       preset->status = g_strdup (status);
+       preset->state = state;
+
+       return preset;
+}
+
+static void
+status_preset_free (StatusPreset *preset)
+{
+       g_free (preset->status);
+       g_free (preset);
+}
+
+static void
+status_presets_file_parse (const gchar *filename)
+{
+       xmlParserCtxtPtr ctxt;
+       xmlDocPtr        doc;
+       xmlNodePtr       presets_node;
+       xmlNodePtr       node;
+
+       empathy_debug (DEBUG_DOMAIN, "Attempting to parse file:'%s'...", filename);
+
+       ctxt = xmlNewParserCtxt ();
+
+       /* Parse and validate the file. */
+       doc = xmlCtxtReadFile (ctxt, filename, NULL, 0);
+       if (!doc) {
+               g_warning ("Failed to parse file:'%s'", filename);
+               xmlFreeParserCtxt (ctxt);
+               return;
+       }
+
+       if (!empathy_xml_validate (doc, STATUS_PRESETS_DTD_FILENAME)) {
+               g_warning ("Failed to validate file:'%s'", filename);
+               xmlFreeDoc(doc);
+               xmlFreeParserCtxt (ctxt);
+               return;
+       }
+
+       /* The root node, presets. */
+       presets_node = xmlDocGetRootElement (doc);
+
+       node = presets_node->children;
+       while (node) {
+               if (strcmp ((gchar *) node->name, "status") == 0 ||
+                   strcmp ((gchar *) node->name, "default") == 0) {
+                       McPresence    state;
+                       gchar        *status;
+                       gchar        *state_str;
+                       StatusPreset *preset;
+                       gboolean      is_default = FALSE;
+
+                       if (strcmp ((gchar *) node->name, "default") == 0) {
+                               is_default = TRUE;
+                       }
+
+                       status = (gchar *) xmlNodeGetContent (node);
+                       state_str = (gchar *) xmlGetProp (node, "presence");
+
+                       if (state_str) {
+                               state = empathy_presence_state_from_str (state_str);
+
+                               if (is_default) {
+                                       empathy_debug (DEBUG_DOMAIN,
+                                                     "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);
+                               }
+                       }
+
+                       xmlFree (status);
+                       xmlFree (state_str);
+               }
+
+               node = node->next;
+       }
+
+       /* Use the default if not set */
+       if (!default_preset) {
+               status_presets_set_default (MC_PRESENCE_OFFLINE, NULL);
+       }
+
+       empathy_debug (DEBUG_DOMAIN, "Parsed %d status presets", g_list_length (presets));
+
+       xmlFreeDoc (doc);
+       xmlFreeParserCtxt (ctxt);
+}
+
+void
+empathy_status_presets_get_all (void)
+{
+       gchar *dir;
+       gchar *file_with_path;
+
+       /* If already set up clean up first. */
+       if (presets) {
+               g_list_foreach (presets, (GFunc) status_preset_free, NULL);
+               g_list_free (presets);
+               presets = NULL;
+       }
+
+       dir = g_build_filename (g_get_home_dir (), ".gnome2", 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);
+
+       if (g_file_test (file_with_path, G_FILE_TEST_EXISTS)) {
+               status_presets_file_parse (file_with_path);
+       }
+
+       g_free (file_with_path);
+}
+
+static gboolean
+status_presets_file_save (void)
+{
+       xmlDocPtr   doc;
+       xmlNodePtr  root;
+       GList      *l;
+       gchar      *dir;
+       gchar      *file;
+       gint        count[LAST_MC_PRESENCE];
+       gint        i;
+
+       for (i = 0; i < LAST_MC_PRESENCE; i++) {
+               count[i] = 0;
+       }
+
+       dir = g_build_filename (g_get_home_dir (), ".gnome2", 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");
+       xmlDocSetRootElement (doc, root);
+
+       if (default_preset) {
+               xmlNodePtr  subnode;
+               xmlChar    *state;
+
+               state = (gchar*) empathy_presence_state_to_str (default_preset->state);
+
+               subnode = xmlNewTextChild (root, NULL, "default",
+                                          default_preset->status);
+               xmlNewProp (subnode, "presence", state);
+       }
+
+       for (l = presets; l; l = l->next) {
+               StatusPreset *sp;
+               xmlNodePtr    subnode;
+               xmlChar      *state;
+
+               sp = l->data;
+               state = (gchar*) empathy_presence_state_to_str (sp->state);
+
+               count[sp->state]++;
+               if (count[sp->state] > STATUS_PRESETS_MAX_EACH) {
+                       continue;
+               }
+
+               subnode = xmlNewTextChild (root, NULL,
+                                          "status", sp->status);
+               xmlNewProp (subnode, "presence", state);
+       }
+
+       /* Make sure the XML is indented properly */
+       xmlIndentTreeOutput = 1;
+
+       empathy_debug (DEBUG_DOMAIN, "Saving file:'%s'", file);
+       xmlSaveFormatFileEnc (file, doc, "utf-8", 1);
+       xmlFreeDoc (doc);
+
+       g_free (file);
+
+       return TRUE;
+}
+
+GList *
+empathy_status_presets_get (McPresence state,
+                          gint       max_number)
+{
+       GList *list = NULL;
+       GList *l;
+       gint   i;
+
+       i = 0;
+       for (l = presets; l; l = l->next) {
+               StatusPreset *sp;
+
+               sp = l->data;
+
+               if (sp->state != state) {
+                       continue;
+               }
+
+               list = g_list_append (list, sp->status);
+               i++;
+
+               if (max_number != -1 && i >= max_number) {
+                       break;
+               }
+       }
+
+       return list;
+}
+
+void
+empathy_status_presets_set_last (McPresence   state,
+                               const gchar *status)
+{
+       GList        *l;
+       StatusPreset *preset;
+       gint          num;
+
+       /* Check if duplicate */
+       for (l = presets; l; l = l->next) {
+               preset = l->data;
+
+               if (state == preset->state &&
+                   !tp_strdiff (status, preset->status)) {
+                       return;
+               }
+       }
+
+       preset = status_preset_new (state, status);
+       presets = g_list_prepend (presets, preset);
+
+       num = 0;
+       for (l = presets; l; l = l->next) {
+               preset = l->data;
+
+               if (state != preset->state) {
+                       continue;
+               }
+
+               num++;
+
+               if (num > STATUS_PRESETS_MAX_EACH) {
+                       status_preset_free (preset);
+                       presets = g_list_delete_link (presets, l);
+                       break;
+               }
+       }
+
+       status_presets_file_save ();
+}
+
+void
+empathy_status_presets_remove (McPresence   state,
+                              const gchar *status)
+{
+       StatusPreset *preset;
+       GList        *l;
+
+       for (l = presets; l; l = l->next) {
+               preset = l->data;
+
+               if (state == preset->state &&
+                   !tp_strdiff (status, preset->status)) {
+                       status_preset_free (preset);
+                       presets = g_list_delete_link (presets, l);
+                       status_presets_file_save ();
+                       break;
+               }
+       }
+}
+
+void
+empathy_status_presets_reset (void)
+{
+       g_list_foreach (presets, (GFunc) status_preset_free, NULL);
+       g_list_free (presets);
+
+       presets = NULL;
+
+       status_presets_set_default (MC_PRESENCE_AVAILABLE, NULL);
+
+       status_presets_file_save ();
+}
+
+McPresence
+empathy_status_presets_get_default_state (void)
+{
+       if (!default_preset) {
+               return MC_PRESENCE_OFFLINE;
+       }
+
+       return default_preset->state;
+}
+
+const gchar *
+empathy_status_presets_get_default_status (void)
+{
+       if (!default_preset ||
+           !default_preset->status) {
+               return NULL;
+       }
+
+       return default_preset->status;
+}
+
+static void
+status_presets_set_default (McPresence   state,
+                           const gchar *status)
+{
+       if (default_preset) {
+               status_preset_free (default_preset);
+       }
+
+       default_preset = status_preset_new (state, status);
+}
+
+void
+empathy_status_presets_set_default (McPresence   state,
+                                  const gchar *status)
+{
+       status_presets_set_default (state, status);
+       status_presets_file_save ();
+}
+
+void
+empathy_status_presets_clear_default (void)
+{
+       if (default_preset) {
+               status_preset_free (default_preset);
+               default_preset = NULL;
+       }
+
+       status_presets_file_save ();
+}
diff --git a/libempathy/empathy-status-presets.dtd b/libempathy/empathy-status-presets.dtd
new file mode 100644 (file)
index 0000000..872be6b
--- /dev/null
@@ -0,0 +1,14 @@
+<!--
+  DTD for Empathys status presets.
+  by Martyn Russell <martyn@imendio.com>
+-->
+
+<!-- Root element. -->
+<!ELEMENT presets ((default?),status*)>
+
+<!ELEMENT default (#PCDATA)>
+<!ATTLIST default presence CDATA #REQUIRED>
+
+<!ELEMENT status (#PCDATA)>
+<!ATTLIST status presence CDATA #REQUIRED>
+
diff --git a/libempathy/empathy-status-presets.h b/libempathy/empathy-status-presets.h
new file mode 100644 (file)
index 0000000..68f3cea
--- /dev/null
@@ -0,0 +1,46 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
+/*
+ * Copyright (C) 2005-2007 Imendio AB
+ *
+ * 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
+ * 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.
+ *
+ * Author: Martyn Russell <martyn@imendio.com>
+ */
+
+#ifndef __EMPATHY_STATUS_PRESETS_H__
+#define __EMPATHY_STATUS_PRESETS_H__
+
+#include "empathy-presence.h"
+
+G_BEGIN_DECLS
+
+void          empathy_status_presets_get_all            (void);
+GList *       empathy_status_presets_get                (McPresence   state,
+                                                        gint         max_number);
+void          empathy_status_presets_set_last           (McPresence   state,
+                                                        const gchar *status);
+void          empathy_status_presets_remove             (McPresence   state,
+                                                        const gchar *status);
+void          empathy_status_presets_reset              (void);
+McPresence    empathy_status_presets_get_default_state  (void);
+const gchar * empathy_status_presets_get_default_status (void);
+void          empathy_status_presets_set_default        (McPresence   state,
+                                                        const gchar *status);
+void          empathy_status_presets_clear_default      (void);
+
+G_END_DECLS
+
+#endif /* __EMPATHY_STATUS_PRESETS_H__ */