'smart qotes' now will replace '<', '>' and '&' inside 'pre' and 'code' tags
[k8lowj.git] / src / account.c
blobaddc96f274b08f991876d49387512bcadab1fedd
1 /* logjam - a GTK client for LiveJournal.
2 * Copyright (C) 2000-2003 Gaal Yahas <gaal@forum2.org>
4 * vim: tabstop=4 shiftwidth=4 noexpandtab :
5 */
7 #include <string.h>
8 #include "conf.h"
9 #include "conf_xml.h"
10 #include "jam_xml.h"
11 #include "account.h"
12 #include "util.h"
14 static GObjectClass *jam_account_parent_class = NULL;
16 static GHashTable *jam_account_cache = NULL;
18 void
19 jam_account_logjam_init(void) {
20 /* can't be in jam_account_class_init because we can call
21 * jam_account_lookup before the jam_account type exists! */
22 jam_account_cache = g_hash_table_new(g_str_hash, g_str_equal);
25 static void
26 jam_account_finalize(GObject *object) {
27 JamAccount *acc = JAM_ACCOUNT(object);
29 /* unregister it */
30 g_hash_table_remove(jam_account_cache, acc);
32 /* must chain up */
33 (* jam_account_parent_class->finalize) (object);
36 static void
37 jam_account_class_init(GObjectClass *class) {
38 jam_account_parent_class = g_type_class_peek_parent (class);
39 class->finalize = jam_account_finalize;
42 const gchar*
43 jam_account_get_username(JamAccount *acc) {
44 return JAM_ACCOUNT_GET_CLASS(acc)->get_username(acc);
47 const gchar*
48 jam_account_get_password(JamAccount *acc) {
49 return JAM_ACCOUNT_GET_CLASS(acc)->get_password(acc);
52 void
53 jam_account_set_username(JamAccount *acc, const char *username) {
54 JAM_ACCOUNT_GET_CLASS(acc)->set_username(acc, username);
57 void
58 jam_account_set_password(JamAccount *acc, const char *password) {
59 JAM_ACCOUNT_GET_CLASS(acc)->set_password(acc, password);
62 JamHost*
63 jam_account_get_host(JamAccount *acc) {
64 return acc->host;
67 void
68 jam_account_set_remember(JamAccount *acc, gboolean u, gboolean p) {
69 acc->remember_user = u;
70 acc->remember_password = p;
73 void
74 jam_account_get_remember(JamAccount *acc, gboolean *u, gboolean *p) {
75 if (u) *u = acc->remember_user;
76 if (p) *p = acc->remember_password;
79 gboolean
80 jam_account_get_remember_password(JamAccount *acc) {
81 return acc->remember_password;
84 gchar*
85 jam_account_id_strdup_from_names(const gchar *username, const gchar *hostname) {
86 return g_strdup_printf("%s@%s", username, hostname);
89 gchar*
90 jam_account_id_strdup(JamAccount *acc) {
91 return jam_account_id_strdup_from_names(
92 jam_account_get_username(acc),
93 acc->host->name);
96 JamAccount*
97 jam_account_lookup(gchar *id) {
98 return (JamAccount*) g_hash_table_lookup(jam_account_cache, id);
101 #if 0
102 static void
103 jam_account_add_to_cache(JamAccount *acc) {
104 char *id;
105 id = jam_account_id_strdup_from_names(user->username, host->name);
106 g_hash_table_insert(jam_account_cache, id, acc);
107 g_free(id);
109 #endif
111 /*JamAccount*
112 jam_account_new(LJUser *u) {
113 gchar *id = jam_account_id_strdup_from_names(u->username, u->server->name);
114 JamAccount *acc = jam_account_lookup(id);
115 if (acc) {
116 g_free(id);
117 return acc;
119 acc = JAM_ACCOUNT(g_object_new(jam_account_get_type(), NULL));
120 acc->user = u;
121 g_hash_table_insert(cache, id, acc);
122 return acc;
125 #if 0
126 JamAccount*
127 jam_account_get_from_names(const gchar *username, const gchar *hostname) {
128 JamHost *host;
129 JamAccount *acc;
130 gchar *id;
131 LJUser *u;
133 id = jam_account_id_strdup_from_names(username, hostname);
134 acc = jam_account_lookup(id);
136 if (acc) { /* have it already */
137 g_free(id);
138 return acc;
141 // FIXME: create a new server / user if they're missing
142 if (!(host = conf_host_by_name(&conf, hostname)))
143 g_error(_("host %s not found in conf"), hostname);
144 if (!(acc = jam_host_get_account_by_username(host, username)))
145 g_error(_("user %s not found in server %s"), username, hostname);
147 #if 0
148 u = lj_user_new(
149 acc =
150 acc = JAM_ACCOUNT(g_object_new(jam_account_get_type(), NULL));
151 acc->user = u;
152 acc->host = h;
153 acc->user->server = s;
154 #endif
155 g_hash_table_insert(jam_account_cache, id, acc);
156 return acc;
158 #endif
160 /*JamAccount*
161 jam_account_lookup_by_user(LJUser *u) {
162 return jam_account_new(u);
165 void
166 jam_account_rename(JamAccount *acc,
167 const gchar *username, const gchar *hostname) {
168 gchar *newid = jam_account_id_strdup_from_names(username, hostname);
169 gchar *oldid = jam_account_id_strdup(acc);
170 if (!g_hash_table_steal(jam_account_cache, oldid))
171 g_error("can't rename account: old account with this name not found");
172 jam_account_set_username(acc, username);
173 string_replace(&(acc->host->name), (gchar*)hostname);
174 g_free(oldid);
175 g_hash_table_insert(jam_account_cache, newid, acc);
178 GType
179 jam_account_get_type(void) {
180 static GType new_type = 0;
181 if (!new_type) {
182 const GTypeInfo new_info = {
183 sizeof(JamAccountClass),
184 NULL,
185 NULL,
186 (GClassInitFunc) jam_account_class_init,
187 NULL,
188 NULL,
189 sizeof(JamAccount),
191 NULL
193 new_type = g_type_register_static(G_TYPE_OBJECT,
194 "JamAccount", &new_info, G_TYPE_FLAG_ABSTRACT);
196 return new_type;
199 JamAccount*
200 jam_account_from_xml(xmlDocPtr doc, xmlNodePtr node, JamHost *host) {
201 JamAccount *acc;
202 xmlChar *protocol = xmlGetProp(node, BAD_CAST "protocol");
203 if (!protocol || xmlStrcmp(protocol, BAD_CAST "livejournal") == 0) {
204 acc = jam_account_lj_from_xml(doc, node, JAM_HOST_LJ(host));
205 #ifdef blogger_punted_for_this_release
206 } else if (xmlStrcmp(protocol, BAD_CAST "blogger") == 0) {
207 acc = jam_account_blogger_from_xml(doc, node, JAM_HOST_BLOGGER(host));
208 #endif /* blogger_punted_for_this_release */
209 } else {
210 g_error("unknown protocol '%s'\n", protocol);
211 return NULL;
213 if (protocol) xmlFree(protocol);
215 acc->remember_user = TRUE;
216 if (jam_account_get_password(acc))
217 acc->remember_password = TRUE;
218 acc->host = host;
220 return acc;
223 gboolean
224 jam_account_write(JamAccount *account, GError **err) {
225 xmlDocPtr doc = NULL;
226 char *path;
227 xmlNodePtr node;
228 gboolean ret = FALSE;
230 if (!account->remember_user)
231 return TRUE;
233 path = g_build_filename(app.conf_dir, "servers", account->host->name,
234 "users", jam_account_get_username(account), "conf.xml", NULL);
235 if (!verify_path(path, FALSE, err))
236 goto out;
238 jam_xmlNewDoc(&doc, &node, "user");
240 xmlNewTextChild(node, NULL, BAD_CAST "username",
241 BAD_CAST jam_account_get_username(account));
242 if (account->remember_password)
243 xmlNewTextChild(node, NULL, BAD_CAST "password",
244 BAD_CAST jam_account_get_password(account));
246 if (JAM_ACCOUNT_IS_LJ(account)) { // XXX blogger
247 jam_account_lj_write(JAM_ACCOUNT_LJ(account), node);
248 xmlSetProp(node, BAD_CAST "protocol", BAD_CAST "livejournal");
250 #ifdef blogger_punted_for_this_release
251 else {
252 xmlSetProp(node, BAD_CAST "protocol", BAD_CAST "blogger");
254 #endif
256 if (xmlSaveFormatFile(path, doc, TRUE) < 0) {
257 g_set_error(err, 0, 0, "xmlSaveFormatFile error saving to %s.\n", path);
258 goto out;
260 ret = TRUE;
262 out:
263 if (path) g_free(path);
264 if (doc) xmlFreeDoc(doc);
266 return ret;
269 #ifdef blogger_punted_for_this_release
270 JamAccount*
271 jam_account_blogger_from_xml(xmlDocPtr doc, xmlNodePtr node, JamHostBlogger *host) {
272 JamAccount *account;
273 JamAccountBlogger *accountb;
275 account = jam_account_blogger_new(NULL);
276 accountb = JAM_ACCOUNT_BLOGGER(account);
277 for (node = node->xmlChildrenNode; node != NULL; node = node->next) {
278 XML_GET_STR("username", accountb->username)
279 XML_GET_STR("password", accountb->password)
280 //XML_GET_FUNC("usejournals", accountb->usejournals, parseusejournals)
281 XML_GET_END("jam_account_blogger_from_xml")
284 return account;
287 const gchar*
288 jam_account_blogger_get_username(JamAccount *acc) {
289 return JAM_ACCOUNT_BLOGGER(acc)->username;
292 const gchar*
293 jam_account_blogger_get_password(JamAccount *acc) {
294 return JAM_ACCOUNT_BLOGGER(acc)->password;
297 static void
298 jam_account_blogger_set_password(JamAccount *acc, const char *password) {
299 string_replace(&JAM_ACCOUNT_BLOGGER(acc)->password, g_strdup(password));
302 static void
303 jam_account_blogger_class_init(JamAccountClass *klass) {
304 klass->get_username = jam_account_blogger_get_username;
305 klass->set_password = jam_account_blogger_set_password;
306 klass->get_password = jam_account_blogger_get_password;
309 GType
310 jam_account_blogger_get_type(void) {
311 static GType new_type = 0;
312 if (!new_type) {
313 const GTypeInfo new_info = {
314 sizeof(JamAccountClass),
315 NULL,
316 NULL,
317 (GClassInitFunc) jam_account_blogger_class_init,
318 NULL,
319 NULL,
320 sizeof(JamAccountBlogger),
322 NULL
324 new_type = g_type_register_static(JAM_TYPE_ACCOUNT,
325 "JamAccountBlogger", &new_info, 0);
327 return new_type;
330 JamAccount*
331 jam_account_blogger_new(const char *username) {
332 JamAccountBlogger *acc;
334 acc = JAM_ACCOUNT_BLOGGER(g_object_new(jam_account_blogger_get_type(), NULL));
335 if (username)
336 acc->username = g_strdup(username);
338 return JAM_ACCOUNT(acc);
340 #endif /* blogger_punted_for_this_release */