Mercurial > pidgin
changeset 5866:d6b5cab288bb
[gaim-migrate @ 6297]
Pounces are now imported and saved to disk. However, they are not yet
loaded. I'll get to that tomorrow (later today? awwww!)
committer: Tailor Script <tailor@pidgin.im>
author | Christian Hammond <chipx86@chipx86.com> |
---|---|
date | Sat, 14 Jun 2003 11:55:15 +0000 |
parents | 412c5a0f9ef1 |
children | db4df0be06fd |
files | src/gtkpounce.c src/main.c src/pounce.c src/pounce.h |
diffstat | 4 files changed, 217 insertions(+), 7 deletions(-) [+] |
line wrap: on
line diff
--- a/src/gtkpounce.c Sat Jun 14 11:18:08 2003 +0000 +++ b/src/gtkpounce.c Sat Jun 14 11:55:15 2003 +0000 @@ -831,7 +831,7 @@ const char *buddy; GList *bp; - for (bp = gaim_get_pounces(); bp != NULL; bp = bp->next) { + for (bp = gaim_pounces_get_all(); bp != NULL; bp = bp->next) { pounce = (GaimPounce *)bp->data; buddy = gaim_pounce_get_pouncee(pounce);
--- a/src/main.c Sat Jun 14 11:18:08 2003 +0000 +++ b/src/main.c Sat Jun 14 11:55:15 2003 +0000 @@ -839,6 +839,7 @@ gaim_plugins_load_saved(); gaim_accounts_load(); + gaim_pounces_load(); #ifdef _WIN32 /* Various win32 initializations */
--- a/src/pounce.c Sat Jun 14 11:18:08 2003 +0000 +++ b/src/pounce.c Sat Jun 14 11:55:15 2003 +0000 @@ -20,8 +20,16 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ +#include <stdio.h> +#include <stdlib.h> #include <string.h> -#include "gaim.h" +#include <sys/stat.h> +#include <sys/types.h> +#include <glib.h> + +#include "debug.h" +#include "pounce.h" +#include "util.h" typedef struct { @@ -34,7 +42,9 @@ } GaimPounceActionData; -static GList *pounces = NULL; +static GList *pounces = NULL; +static guint pounces_save_timer = 0; +static gboolean pounces_loaded = FALSE; static GaimPounceActionData * find_action_data(const GaimPounce *pounce, const char *name) @@ -61,6 +71,22 @@ g_free(action_data); } +static gboolean +pounces_save_cb(gpointer unused) +{ + gaim_pounces_sync(); + pounces_save_timer = 0; + + return FALSE; +} + +static void +schedule_pounces_save(void) +{ + if (!pounces_save_timer) + pounces_save_timer = g_timeout_add(5000, pounces_save_cb, NULL); +} + GaimPounce * gaim_pounce_new(const char *ui_type, GaimAccount *pouncer, const char *pouncee, GaimPounceEvent event, @@ -108,6 +134,8 @@ pounce->free(pounce->data); g_free(pounce); + + schedule_pounces_save(); } void @@ -117,6 +145,8 @@ g_return_if_fail(pounce != GAIM_POUNCE_NONE); pounce->events = events; + + schedule_pounces_save(); } void @@ -126,6 +156,8 @@ g_return_if_fail(pouncer != NULL); pounce->pouncer = pouncer; + + schedule_pounces_save(); } void @@ -138,6 +170,8 @@ g_free(pounce->pouncee); pounce->pouncee = (pouncee == NULL ? NULL : g_strdup(pouncee)); + + schedule_pounces_save(); } void @@ -146,6 +180,8 @@ g_return_if_fail(pounce != NULL); pounce->save = save; + + schedule_pounces_save(); } void @@ -164,6 +200,8 @@ g_free, g_free); g_hash_table_insert(pounce->actions, g_strdup(name), action_data); + + schedule_pounces_save(); } void @@ -180,6 +218,8 @@ g_return_if_fail(action_data != NULL); action_data->enabled = enabled; + + schedule_pounces_save(); } void @@ -201,6 +241,8 @@ else g_hash_table_insert(action_data->atts, g_strdup(attr), g_strdup(value)); + + schedule_pounces_save(); } void @@ -294,7 +336,7 @@ g_return_if_fail(pouncee != NULL); g_return_if_fail(events != GAIM_POUNCE_NONE); - for (l = gaim_get_pounces(); l != NULL; l = l_next) { + for (l = gaim_pounces_get_all(); l != NULL; l = l_next) { pounce = (GaimPounce *)l->data; l_next = l->next; @@ -323,7 +365,7 @@ g_return_val_if_fail(pouncee != NULL, NULL); g_return_val_if_fail(events != GAIM_POUNCE_NONE, NULL); - for (l = gaim_get_pounces(); l != NULL; l = l->next) { + for (l = gaim_pounces_get_all(); l != NULL; l = l->next) { pounce = (GaimPounce *)l->data; if ((gaim_pounce_get_events(pounce) & events) && @@ -337,8 +379,162 @@ return NULL; } +gboolean +gaim_pounces_load(void) +{ + pounces_loaded = TRUE; + + return TRUE; +} + +static void +write_action_parameter(gpointer key, gpointer value, gpointer user_data) +{ + const char *name, *param_value; + FILE *fp; + + param_value = (const char *)value; + name = (const char *)key; + fp = (FILE *)user_data; + + fprintf(fp, " <param name='%s'>%s</param>\n", name, param_value); +} + +static void +write_action_parameter_list(gpointer key, gpointer value, gpointer user_data) +{ + GaimPounceActionData *action_data; + const char *action; + FILE *fp; + + action_data = (GaimPounceActionData *)value; + action = (const char *)key; + fp = (FILE *)user_data; + + if (!action_data->enabled) + return; + + fprintf(fp, " <action type='%s'", action); + + if (g_hash_table_size(action_data->atts) == 0) { + fprintf(fp, "/>\n"); + } + else { + fprintf(fp, ">\n"); + + g_hash_table_foreach(action_data->atts, write_action_parameter, fp); + + fprintf(fp, " </action>\n"); + } +} + +static void +gaim_pounces_write(FILE *fp, GaimPounce *pounce) +{ + GaimAccount *pouncer; + GaimPounceEvent events; + char *pouncer_name; + + pouncer = gaim_pounce_get_pouncer(pounce); + events = gaim_pounce_get_events(pounce); + + pouncer_name = g_markup_escape_text(gaim_account_get_username(pouncer), -1); + + fprintf(fp, " <pounce ui='%s'>\n", pounce->ui_type); + fprintf(fp, " <account protocol='%s'>%s</account>\n", + pouncer->protocol_id, pouncer_name); + fprintf(fp, " <pouncee>%s</pouncee>\n", gaim_pounce_get_pouncee(pounce)); + fprintf(fp, " <events>\n"); + + if (events & GAIM_POUNCE_SIGNON) + fprintf(fp, " <event type='sign-on'>\n"); + if (events & GAIM_POUNCE_SIGNOFF) + fprintf(fp, " <event type='sign-off'>\n"); + if (events & GAIM_POUNCE_AWAY) + fprintf(fp, " <event type='away'>\n"); + if (events & GAIM_POUNCE_AWAY_RETURN) + fprintf(fp, " <event type='return-from-away'>\n"); + if (events & GAIM_POUNCE_IDLE) + fprintf(fp, " <event type='idle'>\n"); + if (events & GAIM_POUNCE_IDLE_RETURN) + fprintf(fp, " <event type='return-from-idle'>\n"); + if (events & GAIM_POUNCE_TYPING) + fprintf(fp, " <event type='start-typing'>\n"); + if (events & GAIM_POUNCE_TYPING_STOPPED) + fprintf(fp, " <event type='stop-typing'>\n"); + + fprintf(fp, " </events>\n"); + fprintf(fp, " <actions>\n"); + + g_hash_table_foreach(pounce->actions, write_action_parameter_list, fp); + + fprintf(fp, " </actions>\n"); + fprintf(fp, " </pounce>\n"); + + g_free(pouncer_name); +} + +void +gaim_pounces_sync(void) +{ + FILE *fp; + const char *user_dir = gaim_user_dir(); + char *filename; + char *filename_real; + + if (!pounces_loaded) { + gaim_debug(GAIM_DEBUG_WARNING, "pounces", + "Writing pounces to disk.\n"); + schedule_pounces_save(); + return; + } + + if (user_dir == NULL) + return; + + gaim_debug(GAIM_DEBUG_INFO, "pounces", "Writing pounces to disk.\n"); + + fp = fopen(user_dir, "r"); + + if (fp == NULL) + mkdir(user_dir, S_IRUSR | S_IWUSR | S_IXUSR); + else + fclose(fp); + + filename = g_build_filename(user_dir, "pounces.xml.save", NULL); + + if ((fp = fopen(filename, "w")) != NULL) { + GList *l; + + fprintf(fp, "<?xml version='1.0' encoding='UTF-8' ?>\n\n"); + fprintf(fp, "<pounces>\n"); + + for (l = gaim_pounces_get_all(); l != NULL; l = l->next) + gaim_pounces_write(fp, l->data); + + fprintf(fp, "</pounces>\n"); + + fclose(fp); + chmod(filename, S_IRUSR | S_IWUSR); + } + else { + gaim_debug(GAIM_DEBUG_ERROR, "pounces", "Unable to write %s\n", + filename); + } + + filename_real = g_build_filename(user_dir, "pounces.xml", NULL); + + if (rename(filename, filename_real) < 0) { + gaim_debug(GAIM_DEBUG_ERROR, "pounces", "Error renaming %s to %s\n", + filename, filename_real); + } + + g_free(filename); + g_free(filename_real); +} + GList * -gaim_get_pounces(void) +gaim_pounces_get_all(void) { return pounces; }
--- a/src/pounce.h Sat Jun 14 11:18:08 2003 +0000 +++ b/src/pounce.h Sat Jun 14 11:55:15 2003 +0000 @@ -25,6 +25,9 @@ typedef struct _GaimPounce GaimPounce; +#include <glib.h> +#include "account.h" + /** * Events that trigger buddy pounces. */ @@ -261,10 +264,20 @@ const char *pouncee, GaimPounceEvent events); /** + * Loads the pounces. + */ +gboolean gaim_pounces_load(void); + +/** + * Force an immediate write of pounces. + */ +void gaim_pounces_sync(void); + +/** * Returns a list of all registered buddy pounces. * * @return The list of buddy pounces. */ -GList *gaim_get_pounces(void); +GList *gaim_pounces_get_all(void); #endif /* _GAIM_POUNCE_H_ */