annotate src/status.c @ 6088:4314e6be74c4

[gaim-migrate @ 6547] Fix removing groups containing chats, thanks to javabsp. Make sound prefs with empty file names show up as "(default)" rather than an empty input box. committer: Tailor Script <tailor@pidgin.im>
author Mark Doliner <mark@kingant.net>
date Fri, 11 Jul 2003 05:10:56 +0000
parents 3a40666c08b4
children 30d4c432101c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6065
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
1 /*
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
2 * gaim
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
3 *
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
4 * Copyright (C) 2003 Jason Priestly
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
5 * Copyright (C) 2003 Luke Perry
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
6 *
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
7 * This program is free software; you can redistribute it and/or modify
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
8 * it under the terms of the GNU General Public License as published by
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
9 * the Free Software Foundation; either version 2 of the License, or
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
10 * (at your option) any later version.
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
11 *
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
12 * This program is distributed in the hope that it will be useful,
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
15 * GNU General Public License for more details.
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
16 *
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
17 * You should have received a copy of the GNU General Public License
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
18 * along with this program; if not, write to the Free Software
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
20 */
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
21
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
22 #include "status.h"
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
23 #include "internal.h"
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
24 #include "ui.h"
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
25 #include "debug.h"
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
26 #include "util.h"
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
27
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
28 /* for people like myself who are too lazy to add an away msg :) */
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
29 /* I don't know who "myself" is in this context. The exclamation point
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
30 * makes it slightly less boring ;) */
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
31 #define BORING_DEFAULT_AWAY_MSG _("Sorry, I ran out for a bit!")
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
32
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
33 /* XML File Saving */
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
34
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
35 /* All of this code is adapted from Nathan Walp's. It's adapted all over the place
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
36 * for accounts, the buddy list, pounces, preferences, and the likes. It would be
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
37 * neat if we could somehow make this more generic. */
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
38 static gboolean status_loaded = FALSE;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
39 static guint status_save_timer = 0;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
40
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
41
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
42 typedef enum
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
43 {
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
44 TAG_NONE = 0,
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
45 TAG_STATUS,
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
46 TAG_STATE,
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
47 TAG_MESSAGE,
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
48
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
49 } StatusParserTag;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
50
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
51
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
52 typedef struct
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
53 {
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
54 StatusParserTag tag;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
55 GString *buffer;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
56 struct away_message *am;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
57
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
58 } StatusParserData;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
59
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
60 static void
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
61 free_parser_data(gpointer user_data)
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
62 {
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
63 StatusParserData *data = user_data;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
64
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
65 if (data->buffer != NULL)
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
66 g_string_free(data->buffer, TRUE);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
67
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
68 g_free(data);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
69 }
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
70
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
71 static void gaim_status_write(FILE *fp, struct away_message *am)
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
72 {
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
73 char *esc = NULL;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
74
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
75 esc = g_markup_escape_text(am->name, -1);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
76 fprintf(fp, " <status name=\"%s\">\n", esc);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
77 g_free(esc);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
78
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
79 fprintf(fp, " <state>away</state>\n");
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
80
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
81 esc = g_markup_escape_text(am->message, -1);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
82 fprintf(fp, " <message>%s</message>\n", esc);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
83 g_free(esc);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
84
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
85 fprintf(fp, " </status>\n");
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
86 }
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
87
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
88 static gboolean
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
89 status_save_cb(gpointer unused)
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
90 {
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
91 gaim_status_sync();
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
92 status_save_timer = 0;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
93
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
94 return FALSE;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
95 }
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
96
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
97 static void
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
98 schedule_status_save()
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
99 {
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
100 if (!status_save_timer)
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
101 status_save_timer = g_timeout_add(5000, status_save_cb, NULL);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
102 }
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
103
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
104 static void
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
105 start_element_handler(GMarkupParseContext *context,
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
106 const gchar *element_name,
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
107 const gchar **attribute_names,
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
108 const gchar **attribute_values,
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
109 gpointer user_data, GError **error)
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
110 {
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
111 const char *value;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
112 StatusParserData *data = user_data;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
113 GHashTable *atts;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
114 int i;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
115
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
116 atts = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
117
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
118 for (i = 0; attribute_names[i] != NULL; i++) {
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
119 g_hash_table_insert(atts, g_strdup(attribute_names[i]),
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
120 g_strdup(attribute_values[i]));
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
121 }
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
122
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
123 if (data->buffer != NULL) {
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
124 g_string_free(data->buffer, TRUE);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
125 data->buffer = NULL;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
126 }
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
127
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
128 if (!strcmp(element_name, "status")) {
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
129 data->tag = TAG_STATUS;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
130 if ((value = g_hash_table_lookup(atts, "name")) != NULL) {
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
131 data->am = g_new0(struct away_message, 1);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
132 g_snprintf(data->am->name, sizeof(data->am->name), "%s", value);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
133 away_messages = g_slist_append(away_messages, data->am);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
134 }
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
135 } else if (!strcmp(element_name, "message")) {
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
136 data->tag = TAG_MESSAGE;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
137
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
138 }
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
139 }
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
140
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
141 static void
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
142 end_element_handler(GMarkupParseContext *context, const gchar *element_name,
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
143 gpointer user_data, GError **error)
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
144 {
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
145 StatusParserData *data = user_data;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
146 gchar *buffer;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
147
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
148 if (data->buffer == NULL)
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
149 return;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
150
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
151 buffer = g_string_free(data->buffer, FALSE);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
152 data->buffer = NULL;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
153
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
154 if (data->tag == TAG_MESSAGE) {
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
155 if (*buffer != '\0')
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
156 g_snprintf(data->am->message, sizeof(data->am->message), "%s", buffer);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
157 }
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
158
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
159 data->tag = TAG_NONE;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
160
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
161 g_free(buffer);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
162 }
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
163
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
164 static void
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
165 text_handler(GMarkupParseContext *context, const gchar *text,
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
166 gsize text_len, gpointer user_data, GError **error)
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
167 {
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
168 StatusParserData *data = user_data;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
169
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
170 if (data->buffer == NULL)
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
171 data->buffer = g_string_new_len(text, text_len);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
172 else
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
173 g_string_append_len(data->buffer, text, text_len);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
174 }
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
175
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
176 static GMarkupParser status_parser =
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
177 {
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
178 start_element_handler,
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
179 end_element_handler,
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
180 text_handler,
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
181 NULL,
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
182 NULL
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
183 };
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
184
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
185 void gaim_status_sync()
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
186 {
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
187 FILE *fp;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
188 const char *user_dir = gaim_user_dir();
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
189 char *filename, *filename_real;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
190
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
191 if (!status_loaded) {
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
192 gaim_debug(GAIM_DEBUG_WARNING, "status", "Writing status to disk.\n");
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
193 schedule_status_save();
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
194 return;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
195 }
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
196
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
197 if (user_dir == NULL)
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
198 return;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
199
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
200 gaim_debug(GAIM_DEBUG_INFO, "status", "Saving statuses to disk\n");
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
201
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
202 fp = fopen(user_dir, "r");
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
203
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
204 if (fp == NULL)
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
205 mkdir(user_dir, S_IRUSR | S_IWUSR | S_IXUSR);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
206 else
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
207 fclose(fp);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
208
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
209 filename = g_build_filename(user_dir, "status.xml.save", NULL);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
210
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
211 if ((fp = fopen(filename, "w")) != NULL) {
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
212 GSList *l;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
213
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
214 fprintf(fp, "<?xml version='1.0' encoding='UTF-8' ?>\n\n");
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
215 fprintf(fp, "<statuses>\n");
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
216
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
217 for (l = away_messages; l != NULL; l = l->next)
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
218 gaim_status_write(fp, l->data);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
219
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
220 fprintf(fp, "</statuses>\n");
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
221
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
222 fclose(fp);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
223 chmod(filename, S_IRUSR | S_IWUSR);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
224 }
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
225 else {
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
226 gaim_debug(GAIM_DEBUG_ERROR, "status", "Unable to write %s\n",
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
227 filename);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
228 }
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
229
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
230 filename_real = g_build_filename(user_dir, "status.xml", NULL);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
231
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
232 if (rename(filename, filename_real) < 0) {
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
233 gaim_debug(GAIM_DEBUG_ERROR, "status", "Error renaming %s to %s\n",
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
234 filename, filename_real);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
235 }
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
236
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
237 g_free(filename);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
238 g_free(filename_real);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
239
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
240 }
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
241
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
242 void gaim_status_load()
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
243 {
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
244 gchar *filename = g_build_filename(gaim_user_dir(), "status.xml", NULL);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
245 gchar *contents = NULL;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
246 gsize length;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
247 GMarkupParseContext *context;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
248 GError *error = NULL;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
249 StatusParserData *parser_data;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
250
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
251 if (filename == NULL) {
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
252 status_loaded = TRUE;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
253 return;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
254 }
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
255
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
256 if (!g_file_get_contents(filename, &contents, &length, &error)) {
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
257 gaim_debug(GAIM_DEBUG_ERROR, "status",
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
258 "Error reading statuses: %s\n", error->message);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
259 g_error_free(error);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
260 g_free(filename);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
261 status_loaded = TRUE;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
262 if (!away_messages) {
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
263 struct away_message *a = g_new0(struct away_message, 1);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
264 g_snprintf(a->name, sizeof(a->name), _("Slightly less boring default"));
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
265 g_snprintf(a->message, sizeof(a->message), "%s", BORING_DEFAULT_AWAY_MSG);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
266 away_messages = g_slist_append(away_messages, a);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
267 }
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
268 return;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
269 }
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
270
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
271 parser_data = g_new0(StatusParserData, 1);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
272
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
273 context = g_markup_parse_context_new(&status_parser, 0,
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
274 parser_data, free_parser_data);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
275
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
276 if (!g_markup_parse_context_parse(context, contents, length, NULL)) {
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
277 g_markup_parse_context_free(context);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
278 g_free(contents);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
279 g_free(filename);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
280 status_loaded = TRUE;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
281 return;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
282 }
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
283
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
284 if (!g_markup_parse_context_end_parse(context, NULL)) {
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
285 gaim_debug(GAIM_DEBUG_ERROR, "status", "Error parsing %s\n",
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
286 filename);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
287 g_markup_parse_context_free(context);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
288 g_free(contents);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
289 g_free(filename);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
290 status_loaded = TRUE;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
291 return;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
292 }
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
293
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
294 g_markup_parse_context_free(context);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
295 g_free(contents);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
296 g_free(filename);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
297 status_loaded = TRUE;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
298 return;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
299 }