annotate src/status.c @ 6577:ea785d055825

[gaim-migrate @ 7099] value.c added committer: Tailor Script <tailor@pidgin.im>
author Herman Bloggs <hermanator12002@yahoo.com>
date Fri, 22 Aug 2003 21:09:38 +0000
parents 8f94cce8faa5
children fa6395637e2c
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 "debug.h"
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
25 #include "util.h"
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
26
6371
8f94cce8faa5 [gaim-migrate @ 6876]
Christian Hammond <chipx86@chipx86.com>
parents: 6321
diff changeset
27 /* XXX CORE/UI */
8f94cce8faa5 [gaim-migrate @ 6876]
Christian Hammond <chipx86@chipx86.com>
parents: 6321
diff changeset
28 #include "gtkinternal.h"
8f94cce8faa5 [gaim-migrate @ 6876]
Christian Hammond <chipx86@chipx86.com>
parents: 6321
diff changeset
29 #include "ui.h"
8f94cce8faa5 [gaim-migrate @ 6876]
Christian Hammond <chipx86@chipx86.com>
parents: 6321
diff changeset
30
8f94cce8faa5 [gaim-migrate @ 6876]
Christian Hammond <chipx86@chipx86.com>
parents: 6321
diff changeset
31
6065
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
32 /* 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
33 /* 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
34 * makes it slightly less boring ;) */
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
35 #define BORING_DEFAULT_AWAY_MSG _("Sorry, I ran out for a bit!")
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
36
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
37 /* XML File Saving */
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
38
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
39 /* 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
40 * 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
41 * neat if we could somehow make this more generic. */
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
42 static gboolean status_loaded = FALSE;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
43 static guint status_save_timer = 0;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
44
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
45
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
46 typedef enum
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
47 {
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
48 TAG_NONE = 0,
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
49 TAG_STATUS,
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
50 TAG_STATE,
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
51 TAG_MESSAGE,
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
52
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
53 } StatusParserTag;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
54
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
55
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
56 typedef struct
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 StatusParserTag tag;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
59 GString *buffer;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
60 struct away_message *am;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
61
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
62 } StatusParserData;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
63
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
64 static void
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
65 free_parser_data(gpointer user_data)
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
66 {
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
67 StatusParserData *data = user_data;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
68
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
69 if (data->buffer != NULL)
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
70 g_string_free(data->buffer, TRUE);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
71
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
72 g_free(data);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
73 }
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 static void gaim_status_write(FILE *fp, struct away_message *am)
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
76 {
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
77 char *esc = NULL;
6216
dc42b27101c0 [gaim-migrate @ 6702]
Nathan Walp <nwalp@pidgin.im>
parents: 6113
diff changeset
78
6065
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
79 esc = g_markup_escape_text(am->name, -1);
6216
dc42b27101c0 [gaim-migrate @ 6702]
Nathan Walp <nwalp@pidgin.im>
parents: 6113
diff changeset
80 fprintf(fp, "\t<status name=\"%s\">\n", esc);
6065
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
81 g_free(esc);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
82
6216
dc42b27101c0 [gaim-migrate @ 6702]
Nathan Walp <nwalp@pidgin.im>
parents: 6113
diff changeset
83 fprintf(fp, "\t\t<state>away</state>\n");
dc42b27101c0 [gaim-migrate @ 6702]
Nathan Walp <nwalp@pidgin.im>
parents: 6113
diff changeset
84
dc42b27101c0 [gaim-migrate @ 6702]
Nathan Walp <nwalp@pidgin.im>
parents: 6113
diff changeset
85 esc = g_markup_escape_text(am->message, -1);
dc42b27101c0 [gaim-migrate @ 6702]
Nathan Walp <nwalp@pidgin.im>
parents: 6113
diff changeset
86 fprintf(fp, "\t\t<message>%s</message>\n", esc);
dc42b27101c0 [gaim-migrate @ 6702]
Nathan Walp <nwalp@pidgin.im>
parents: 6113
diff changeset
87 g_free(esc);
dc42b27101c0 [gaim-migrate @ 6702]
Nathan Walp <nwalp@pidgin.im>
parents: 6113
diff changeset
88
dc42b27101c0 [gaim-migrate @ 6702]
Nathan Walp <nwalp@pidgin.im>
parents: 6113
diff changeset
89 fprintf(fp, "\t</status>\n");
6065
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
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
92 static gboolean
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
93 status_save_cb(gpointer unused)
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
94 {
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
95 gaim_status_sync();
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
96 status_save_timer = 0;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
97
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
98 return FALSE;
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
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
101 static void
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
102 schedule_status_save()
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 if (!status_save_timer)
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
105 status_save_timer = g_timeout_add(5000, status_save_cb, NULL);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
106 }
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
107
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
108 static void
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
109 start_element_handler(GMarkupParseContext *context,
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
110 const gchar *element_name,
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
111 const gchar **attribute_names,
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
112 const gchar **attribute_values,
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
113 gpointer user_data, GError **error)
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
114 {
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
115 const char *value;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
116 StatusParserData *data = user_data;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
117 GHashTable *atts;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
118 int i;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
119
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
120 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
121
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
122 for (i = 0; attribute_names[i] != NULL; i++) {
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
123 g_hash_table_insert(atts, g_strdup(attribute_names[i]),
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
124 g_strdup(attribute_values[i]));
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
125 }
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 if (data->buffer != NULL) {
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
128 g_string_free(data->buffer, TRUE);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
129 data->buffer = NULL;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
130 }
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
131
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
132 if (!strcmp(element_name, "status")) {
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
133 data->tag = TAG_STATUS;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
134 if ((value = g_hash_table_lookup(atts, "name")) != NULL) {
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
135 data->am = g_new0(struct away_message, 1);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
136 g_snprintf(data->am->name, sizeof(data->am->name), "%s", value);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
137 away_messages = g_slist_append(away_messages, data->am);
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 } else if (!strcmp(element_name, "message")) {
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
140 data->tag = TAG_MESSAGE;
6216
dc42b27101c0 [gaim-migrate @ 6702]
Nathan Walp <nwalp@pidgin.im>
parents: 6113
diff changeset
141
6065
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
142 }
6113
30d4c432101c [gaim-migrate @ 6575]
Mark Doliner <mark@kingant.net>
parents: 6065
diff changeset
143
30d4c432101c [gaim-migrate @ 6575]
Mark Doliner <mark@kingant.net>
parents: 6065
diff changeset
144 g_hash_table_destroy(atts);
6065
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
145 }
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
146
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
147 static void
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
148 end_element_handler(GMarkupParseContext *context, const gchar *element_name,
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
149 gpointer user_data, GError **error)
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 StatusParserData *data = user_data;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
152 gchar *buffer;
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->buffer == NULL)
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
155 return;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
156
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
157 buffer = g_string_free(data->buffer, FALSE);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
158 data->buffer = NULL;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
159
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
160 if (data->tag == TAG_MESSAGE) {
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
161 if (*buffer != '\0')
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
162 g_snprintf(data->am->message, sizeof(data->am->message), "%s", buffer);
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
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
165 data->tag = TAG_NONE;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
166
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
167 g_free(buffer);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
168 }
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 static void
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
171 text_handler(GMarkupParseContext *context, const gchar *text,
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
172 gsize text_len, gpointer user_data, GError **error)
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
173 {
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
174 StatusParserData *data = user_data;
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 if (data->buffer == NULL)
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
177 data->buffer = g_string_new_len(text, text_len);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
178 else
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
179 g_string_append_len(data->buffer, text, text_len);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
180 }
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
181
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
182 static GMarkupParser status_parser =
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 start_element_handler,
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
185 end_element_handler,
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
186 text_handler,
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
187 NULL,
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
188 NULL
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
189 };
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 void gaim_status_sync()
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
192 {
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
193 FILE *fp;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
194 const char *user_dir = gaim_user_dir();
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
195 char *filename, *filename_real;
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 (!status_loaded) {
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
198 gaim_debug(GAIM_DEBUG_WARNING, "status", "Writing status to disk.\n");
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
199 schedule_status_save();
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
200 return;
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
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
203 if (user_dir == NULL)
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
204 return;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
205
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
206 gaim_debug(GAIM_DEBUG_INFO, "status", "Saving statuses to disk\n");
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
207
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
208 fp = fopen(user_dir, "r");
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
209
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
210 if (fp == NULL)
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
211 mkdir(user_dir, S_IRUSR | S_IWUSR | S_IXUSR);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
212 else
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
213 fclose(fp);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
214
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
215 filename = g_build_filename(user_dir, "status.xml.save", NULL);
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 if ((fp = fopen(filename, "w")) != NULL) {
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
218 GSList *l;
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, "<?xml version='1.0' encoding='UTF-8' ?>\n\n");
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
221 fprintf(fp, "<statuses>\n");
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
222
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
223 for (l = away_messages; l != NULL; l = l->next)
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
224 gaim_status_write(fp, l->data);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
225
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
226 fprintf(fp, "</statuses>\n");
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
227
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
228 fclose(fp);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
229 chmod(filename, S_IRUSR | S_IWUSR);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
230 }
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
231 else {
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
232 gaim_debug(GAIM_DEBUG_ERROR, "status", "Unable to write %s\n",
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
233 filename);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
234 }
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 filename_real = g_build_filename(user_dir, "status.xml", NULL);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
237
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
238 if (rename(filename, filename_real) < 0) {
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
239 gaim_debug(GAIM_DEBUG_ERROR, "status", "Error renaming %s to %s\n",
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
240 filename, filename_real);
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
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
243 g_free(filename);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
244 g_free(filename_real);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
245
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
246 }
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
247
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
248 void gaim_status_load()
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
249 {
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
250 gchar *filename = g_build_filename(gaim_user_dir(), "status.xml", NULL);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
251 gchar *contents = NULL;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
252 gsize length;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
253 GMarkupParseContext *context;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
254 GError *error = NULL;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
255 StatusParserData *parser_data;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
256
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
257 if (filename == NULL) {
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
258 status_loaded = TRUE;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
259 return;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
260 }
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
261
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
262 if (!g_file_get_contents(filename, &contents, &length, &error)) {
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
263 gaim_debug(GAIM_DEBUG_ERROR, "status",
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
264 "Error reading statuses: %s\n", error->message);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
265 g_error_free(error);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
266 g_free(filename);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
267 status_loaded = TRUE;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
268 if (!away_messages) {
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
269 struct away_message *a = g_new0(struct away_message, 1);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
270 g_snprintf(a->name, sizeof(a->name), _("Slightly less boring default"));
6321
3613007cbb6e [gaim-migrate @ 6820]
Luke Schierer <lschiere@pidgin.im>
parents: 6216
diff changeset
271 g_snprintf(a->message, sizeof(a->message), "%s", _(BORING_DEFAULT_AWAY_MSG));
6065
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
272 away_messages = g_slist_append(away_messages, a);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
273 }
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
274 return;
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
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
277 parser_data = g_new0(StatusParserData, 1);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
278
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
279 context = g_markup_parse_context_new(&status_parser, 0,
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
280 parser_data, free_parser_data);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
281
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
282 if (!g_markup_parse_context_parse(context, contents, length, NULL)) {
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
283 g_markup_parse_context_free(context);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
284 g_free(contents);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
285 g_free(filename);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
286 status_loaded = TRUE;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
287 return;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
288 }
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
289
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
290 if (!g_markup_parse_context_end_parse(context, NULL)) {
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
291 gaim_debug(GAIM_DEBUG_ERROR, "status", "Error parsing %s\n",
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
292 filename);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
293 g_markup_parse_context_free(context);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
294 g_free(contents);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
295 g_free(filename);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
296 status_loaded = TRUE;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
297 return;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
298 }
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
299
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
300 g_markup_parse_context_free(context);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
301 g_free(contents);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
302 g_free(filename);
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
303 status_loaded = TRUE;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
304 return;
3a40666c08b4 [gaim-migrate @ 6515]
Sean Egan <seanegan@gmail.com>
parents:
diff changeset
305 }