1 /* logjam - a GTK client for LiveJournal.
2 * Copyright (C) 2000-2003 Gaal Yahas <gaal@forum2.org>
13 static GObjectClass
*jam_account_parent_class
= NULL
;
14 static GHashTable
*jam_account_cache
= NULL
;
17 void jam_account_logjam_init (void) {
18 /* can't be in jam_account_class_init because we can call
19 * jam_account_lookup before the jam_account type exists! */
20 jam_account_cache
= g_hash_table_new(g_str_hash
, g_str_equal
);
24 static void jam_account_finalize (GObject
*object
) {
25 JamAccount
*acc
= JAM_ACCOUNT(object
);
27 g_hash_table_remove(jam_account_cache
, acc
);
29 (*jam_account_parent_class
->finalize
)(object
);
33 static void jam_account_class_init (GObjectClass
*class) {
34 jam_account_parent_class
= g_type_class_peek_parent(class);
35 class->finalize
= jam_account_finalize
;
39 const gchar
*jam_account_get_username (JamAccount
*acc
) {
40 return JAM_ACCOUNT_GET_CLASS(acc
)->get_username(acc
);
44 const gchar
*jam_account_get_password (JamAccount
*acc
) {
45 return JAM_ACCOUNT_GET_CLASS(acc
)->get_password(acc
);
49 void jam_account_set_username (JamAccount
*acc
, const char *username
) {
50 JAM_ACCOUNT_GET_CLASS(acc
)->set_username(acc
, username
);
54 void jam_account_set_password (JamAccount
*acc
, const char *password
) {
55 JAM_ACCOUNT_GET_CLASS(acc
)->set_password(acc
, password
);
59 JamHost
*jam_account_get_host (JamAccount
*acc
) {
64 void jam_account_set_remember (JamAccount
*acc
, gboolean u
, gboolean p
) {
65 acc
->remember_user
= u
;
66 acc
->remember_password
= p
;
70 void jam_account_get_remember (JamAccount
*acc
, gboolean
*u
, gboolean
*p
) {
71 if (u
) *u
= acc
->remember_user
;
72 if (p
) *p
= acc
->remember_password
;
76 gboolean
jam_account_get_remember_password (JamAccount
*acc
) {
77 return acc
->remember_password
;
81 gchar
*jam_account_id_strdup_from_names (const gchar
*username
, const gchar
*hostname
) {
82 return g_strdup_printf("%s@%s", username
, hostname
);
86 gchar
*jam_account_id_strdup (JamAccount
*acc
) {
87 return jam_account_id_strdup_from_names(jam_account_get_username(acc
), acc
->host
->name
);
91 JamAccount
*jam_account_lookup(gchar
*id
) {
92 return (JamAccount
*)g_hash_table_lookup(jam_account_cache
, id
);
96 void jam_account_rename (JamAccount
*acc
, const gchar
*username
, const gchar
*hostname
) {
97 gchar
*newid
= jam_account_id_strdup_from_names(username
, hostname
);
98 gchar
*oldid
= jam_account_id_strdup(acc
);
99 if (!g_hash_table_steal(jam_account_cache
, oldid
)) g_error("%s", "can't rename account: old account with this name not found");
100 jam_account_set_username(acc
, username
);
101 string_replace(&(acc
->host
->name
), (gchar
*)hostname
);
103 g_hash_table_insert(jam_account_cache
, newid
, acc
);
107 GType
jam_account_get_type (void) {
108 static GType new_type
= 0;
110 const GTypeInfo new_info
= {
111 sizeof(JamAccountClass
),
114 (GClassInitFunc
) jam_account_class_init
,
121 new_type
= g_type_register_static(G_TYPE_OBJECT
, "JamAccount", &new_info
, G_TYPE_FLAG_ABSTRACT
);
127 JamAccount
*jam_account_from_xml (xmlDocPtr doc
, xmlNodePtr node
, JamHost
*host
) {
129 xmlChar
*protocol
= xmlGetProp(node
, BAD_CAST
"protocol");
130 if (!protocol
|| xmlStrcmp(protocol
, BAD_CAST
"livejournal") == 0) {
131 acc
= jam_account_lj_from_xml(doc
, node
, JAM_HOST_LJ(host
));
133 g_error("unknown protocol '%s'\n", protocol
);
136 if (protocol
) xmlFree(protocol
);
137 acc
->remember_user
= TRUE
;
138 if (jam_account_get_password(acc
)) acc
->remember_password
= TRUE
;
144 gboolean
jam_account_write (JamAccount
*account
, GError
**err
) {
145 xmlDocPtr doc
= NULL
;
148 gboolean ret
= FALSE
;
150 if (!account
->remember_user
) return TRUE
;
151 path
= g_build_filename(app
.conf_dir
, "servers", account
->host
->name
, "users", jam_account_get_username(account
), "conf.xml", NULL
);
152 if (!verify_path(path
, FALSE
, err
)) goto out
;
153 jam_xmlNewDoc(&doc
, &node
, "user");
154 xmlNewTextChild(node
, NULL
, BAD_CAST
"username", BAD_CAST
jam_account_get_username(account
));
155 if (account
->remember_password
) xmlNewTextChild(node
, NULL
, BAD_CAST
"password", BAD_CAST
jam_account_get_password(account
));
156 if (JAM_ACCOUNT_IS_LJ(account
)) {
157 jam_account_lj_write(JAM_ACCOUNT_LJ(account
), node
);
158 xmlSetProp(node
, BAD_CAST
"protocol", BAD_CAST
"livejournal");
160 if (xmlSaveFormatFile(path
, doc
, TRUE
) < 0) {
161 g_set_error(err
, 0, 0, "xmlSaveFormatFile error saving to %s.\n", path
);
167 if (path
) g_free(path
);
168 if (doc
) xmlFreeDoc(doc
);