1 /* logjam - a GTK client for LiveJournal.
2 * Copyright (C) 2000-2003 Gaal Yahas <gaal@forum2.org>
4 * vim: tabstop=4 shiftwidth=4 noexpandtab :
14 static GObjectClass
*jam_account_parent_class
= NULL
;
16 static GHashTable
*jam_account_cache
= NULL
;
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
);
26 jam_account_finalize(GObject
*object
) {
27 JamAccount
*acc
= JAM_ACCOUNT(object
);
30 g_hash_table_remove(jam_account_cache
, acc
);
33 (* jam_account_parent_class
->finalize
) (object
);
37 jam_account_class_init(GObjectClass
*class) {
38 jam_account_parent_class
= g_type_class_peek_parent (class);
39 class->finalize
= jam_account_finalize
;
43 jam_account_get_username(JamAccount
*acc
) {
44 return JAM_ACCOUNT_GET_CLASS(acc
)->get_username(acc
);
48 jam_account_get_password(JamAccount
*acc
) {
49 return JAM_ACCOUNT_GET_CLASS(acc
)->get_password(acc
);
53 jam_account_set_username(JamAccount
*acc
, const char *username
) {
54 JAM_ACCOUNT_GET_CLASS(acc
)->set_username(acc
, username
);
58 jam_account_set_password(JamAccount
*acc
, const char *password
) {
59 JAM_ACCOUNT_GET_CLASS(acc
)->set_password(acc
, password
);
63 jam_account_get_host(JamAccount
*acc
) {
68 jam_account_set_remember(JamAccount
*acc
, gboolean u
, gboolean p
) {
69 acc
->remember_user
= u
;
70 acc
->remember_password
= p
;
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
;
80 jam_account_get_remember_password(JamAccount
*acc
) {
81 return acc
->remember_password
;
85 jam_account_id_strdup_from_names(const gchar
*username
, const gchar
*hostname
) {
86 return g_strdup_printf("%s@%s", username
, hostname
);
90 jam_account_id_strdup(JamAccount
*acc
) {
91 return jam_account_id_strdup_from_names(
92 jam_account_get_username(acc
),
97 jam_account_lookup(gchar
*id
) {
98 return (JamAccount
*) g_hash_table_lookup(jam_account_cache
, id
);
103 jam_account_add_to_cache(JamAccount
*acc
) {
105 id
= jam_account_id_strdup_from_names(user
->username
, host
->name
);
106 g_hash_table_insert(jam_account_cache
, id
, acc
);
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);
119 acc = JAM_ACCOUNT(g_object_new(jam_account_get_type(), NULL));
121 g_hash_table_insert(cache, id, acc);
127 jam_account_get_from_names(const gchar
*username
, const gchar
*hostname
) {
133 id
= jam_account_id_strdup_from_names(username
, hostname
);
134 acc
= jam_account_lookup(id
);
136 if (acc
) { /* have it already */
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
);
150 acc
= JAM_ACCOUNT(g_object_new(jam_account_get_type(), NULL
));
153 acc
->user
->server
= s
;
155 g_hash_table_insert(jam_account_cache
, id
, acc
);
161 jam_account_lookup_by_user(LJUser *u) {
162 return jam_account_new(u);
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
);
175 g_hash_table_insert(jam_account_cache
, newid
, acc
);
179 jam_account_get_type(void) {
180 static GType new_type
= 0;
182 const GTypeInfo new_info
= {
183 sizeof(JamAccountClass
),
186 (GClassInitFunc
) jam_account_class_init
,
193 new_type
= g_type_register_static(G_TYPE_OBJECT
,
194 "JamAccount", &new_info
, G_TYPE_FLAG_ABSTRACT
);
200 jam_account_from_xml(xmlDocPtr doc
, xmlNodePtr node
, JamHost
*host
) {
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 */
210 g_error("unknown protocol '%s'\n", protocol
);
213 if (protocol
) xmlFree(protocol
);
215 acc
->remember_user
= TRUE
;
216 if (jam_account_get_password(acc
))
217 acc
->remember_password
= TRUE
;
224 jam_account_write(JamAccount
*account
, GError
**err
) {
225 xmlDocPtr doc
= NULL
;
228 gboolean ret
= FALSE
;
230 if (!account
->remember_user
)
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
))
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
252 xmlSetProp(node
, BAD_CAST
"protocol", BAD_CAST
"blogger");
256 if (xmlSaveFormatFile(path
, doc
, TRUE
) < 0) {
257 g_set_error(err
, 0, 0, "xmlSaveFormatFile error saving to %s.\n", path
);
263 if (path
) g_free(path
);
264 if (doc
) xmlFreeDoc(doc
);
269 #ifdef blogger_punted_for_this_release
271 jam_account_blogger_from_xml(xmlDocPtr doc
, xmlNodePtr node
, JamHostBlogger
*host
) {
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")
288 jam_account_blogger_get_username(JamAccount
*acc
) {
289 return JAM_ACCOUNT_BLOGGER(acc
)->username
;
293 jam_account_blogger_get_password(JamAccount
*acc
) {
294 return JAM_ACCOUNT_BLOGGER(acc
)->password
;
298 jam_account_blogger_set_password(JamAccount
*acc
, const char *password
) {
299 string_replace(&JAM_ACCOUNT_BLOGGER(acc
)->password
, g_strdup(password
));
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
;
310 jam_account_blogger_get_type(void) {
311 static GType new_type
= 0;
313 const GTypeInfo new_info
= {
314 sizeof(JamAccountClass
),
317 (GClassInitFunc
) jam_account_blogger_class_init
,
320 sizeof(JamAccountBlogger
),
324 new_type
= g_type_register_static(JAM_TYPE_ACCOUNT
,
325 "JamAccountBlogger", &new_info
, 0);
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
));
336 acc
->username
= g_strdup(username
);
338 return JAM_ACCOUNT(acc
);
340 #endif /* blogger_punted_for_this_release */