1 /* logjam - a GTK client for LiveJournal.
2 * Copyright (C) 2003 Evan Martin <evan@livejournal.com>
4 * vim: tabstop=4 shiftwidth=4 noexpandtab :
9 #include "liblj/editpostevent.h"
23 jam_account_lj_new(LJServer
*server
, const char *username
) {
26 acc
= JAM_ACCOUNT_LJ(g_object_new(jam_account_lj_get_type(), NULL
));
27 acc
->user
= lj_user_new(server
);
29 acc
->user
->username
= g_strdup(username
);
31 return JAM_ACCOUNT(acc
);
35 parsepickws(xmlDocPtr doc
, xmlNodePtr node
) {
36 GSList
*pickws
= NULL
;
37 for (node
= node
->xmlChildrenNode
; node
!= NULL
; node
= node
->next
) {
38 XML_GET_LIST("pickw", pickws
, jam_xmlGetString
)
39 XML_GET_END("parsepickws")
45 parseusejournals(xmlDocPtr doc
, xmlNodePtr node
) {
48 for (node
= node
->xmlChildrenNode
; node
!= NULL
; node
= node
->next
) {
49 if (xmlStrcmp(node
->name
, BAD_CAST
"usejournal") == 0) {
50 journal
= jam_xmlGetString(doc
, node
);
51 l
= g_slist_append(l
, journal
);
53 XML_GET_END("usejournals")
59 parsefriendgroups(xmlDocPtr doc
, xmlNodePtr node
) {
63 for (node
= node
->xmlChildrenNode
; node
!= NULL
; node
= node
->next
) {
64 if (xmlStrcmp(node
->name
, BAD_CAST
"friendgroup") == 0) {
65 if (jam_xmlGetIntProp(node
, "id", &id
)) {
66 fg
= lj_friendgroup_new();
69 fg
->name
= jam_xmlGetString(doc
, node
);
70 jam_xmlGetIntProp(node
, "ispublic", &fg
->ispublic
);
71 fgs
= g_slist_append(fgs
, fg
);
74 XML_GET_END("parsefriendgroups")
80 parsewebmenu(xmlDocPtr doc
, xmlNodePtr node
) {
81 GSList
*webmenu
= NULL
;
85 for (node
= node
->xmlChildrenNode
; node
!= NULL
; node
= node
->next
) {
86 if (xmlStrcmp(node
->name
, BAD_CAST
"menuitem") == 0) {
87 wmi
= lj_webmenuitem_new();
88 if ((text
= xmlGetProp(node
, BAD_CAST
"text"))) {
89 wmi
->text
= g_strdup((char*)text
);
90 if ((url
= xmlGetProp(node
, BAD_CAST
"url"))) {
91 wmi
->url
= g_strdup((char*)url
);
96 if (node
->xmlChildrenNode
)
97 wmi
->subitems
= parsewebmenu(doc
, node
);
98 webmenu
= g_slist_append(webmenu
, wmi
);
100 XML_GET_END("parsewebmenu")
106 writewebmenu(xmlNodePtr parent
, GSList
*webmenu
) {
111 for (l
= webmenu
; l
!= NULL
; l
= l
->next
) {
112 wmi
= (LJWebMenuItem
*)l
->data
;
113 node
= xmlNewChild(parent
, NULL
, BAD_CAST
"menuitem", NULL
);
115 xmlSetProp(node
, BAD_CAST
"text", BAD_CAST wmi
->text
);
117 xmlSetProp(node
, BAD_CAST
"url", BAD_CAST wmi
->url
);
119 writewebmenu(node
, wmi
->subitems
);
124 jam_account_lj_write(JamAccountLJ
*account
, xmlNodePtr node
) {
125 LJUser
*user
= account
->user
;
126 xmlNodePtr xmlList
, child
;
132 xmlNewTextChild(node
, NULL
,
133 BAD_CAST
"fullname", BAD_CAST user
->fullname
);
135 if (account
->lastupdate
)
136 jam_xmlAddInt(node
, "lastupdate", (int)account
->lastupdate
);
137 if (user
->checkfriends
)
138 jam_xmlAddInt(node
, "checkfriends", (int)TRUE
);
139 if (account
->cfmask
) {
140 g_snprintf(buf
, 10, "%d", account
->cfmask
);
141 xmlNewTextChild(node
, NULL
, BAD_CAST
"cfmask", BAD_CAST buf
);
145 xmlList
= xmlNewChild(node
, NULL
, BAD_CAST
"pickws", NULL
);
146 for (l
= user
->pickws
; l
!= NULL
; l
= l
->next
) {
147 xmlNewTextChild(xmlList
, NULL
, BAD_CAST
"pickw", BAD_CAST l
->data
);
151 if (user
->usejournals
) {
152 xmlList
= xmlNewChild(node
, NULL
, BAD_CAST
"usejournals", NULL
);
153 for (l
= user
->usejournals
; l
!= NULL
; l
= l
->next
) {
154 xmlNewTextChild(xmlList
, NULL
, BAD_CAST
"usejournal", BAD_CAST l
->data
);
158 if (user
->friendgroups
) {
159 xmlList
= xmlNewChild(node
, NULL
, BAD_CAST
"friendgroups", NULL
);
160 for (l
= user
->friendgroups
; l
!= NULL
; l
= l
->next
) {
161 fg
= (LJFriendGroup
*)l
->data
;
162 child
= xmlNewTextChild(xmlList
, NULL
,
163 BAD_CAST
"friendgroup", BAD_CAST fg
->name
);
164 g_snprintf(buf
, 10, "%d", fg
->id
);
165 xmlSetProp(child
, BAD_CAST
"id", BAD_CAST buf
);
166 g_snprintf(buf
, 10, "%d", fg
->ispublic
);
167 xmlSetProp(child
, BAD_CAST
"ispublic", BAD_CAST buf
);
172 xmlList
= xmlNewChild(node
, NULL
, BAD_CAST
"webmenu", NULL
);
173 writewebmenu(xmlList
, user
->webmenu
);
179 jam_account_lj_from_xml(xmlDocPtr doc
, xmlNodePtr node
, JamHostLJ
*host
) {
181 JamAccountLJ
*accountlj
;
183 guint32 lastupdate
= 0;
185 account
= jam_account_lj_new(jam_host_lj_get_server(host
), NULL
);
186 accountlj
= JAM_ACCOUNT_LJ(account
);
187 user
= accountlj
->user
;
188 for (node
= node
->xmlChildrenNode
; node
!= NULL
; node
= node
->next
) {
189 XML_GET_STR("username", user
->username
)
190 XML_GET_STR("fullname", user
->fullname
)
191 XML_GET_STR("password", user
->password
)
192 XML_GET_BOOL("checkfriends", user
->checkfriends
)
193 XML_GET_UINT32("lastupdate", lastupdate
)
194 XML_GET_FUNC("pickws", user
->pickws
, parsepickws
)
195 XML_GET_FUNC("usejournals", user
->usejournals
, parseusejournals
)
196 XML_GET_FUNC("friendgroups", user
->friendgroups
, parsefriendgroups
)
197 XML_GET_FUNC("webmenu", user
->webmenu
, parsewebmenu
)
198 XML_GET_UINT32("cfmask", accountlj
->cfmask
)
199 XML_GET_END("jam_account_lj_from_xml")
202 accountlj
->lastupdate
= (time_t)lastupdate
;
208 jam_account_lj_get_username(JamAccount
*acc
) {
209 return JAM_ACCOUNT_LJ(acc
)->user
->username
;
213 jam_account_lj_get_password(JamAccount
*acc
) {
214 return JAM_ACCOUNT_LJ(acc
)->user
->password
;
218 jam_account_lj_get_checkfriends(JamAccount
*acc
) {
219 return JAM_ACCOUNT_LJ(acc
)->user
->checkfriends
;
223 jam_account_lj_set_password(JamAccount
*acc
, const char *password
) {
224 string_replace(&JAM_ACCOUNT_LJ(acc
)->user
->password
, g_strdup(password
));
228 jam_account_lj_get_user(JamAccountLJ
*acc
) {
233 jam_account_lj_get_server(JamAccountLJ
*acc
) {
234 return acc
->user
->server
;
238 jam_account_lj_get_cfmask(JamAccountLJ
*acc
) {
243 jam_account_lj_set_cfmask(JamAccountLJ
*acc
, guint32 mask
) {
248 jam_account_lj_class_init(JamAccountClass
*klass
) {
249 klass
->get_username
= jam_account_lj_get_username
;
250 klass
->set_password
= jam_account_lj_set_password
;
251 klass
->get_password
= jam_account_lj_get_password
;
255 jam_account_lj_get_type(void) {
256 static GType new_type
= 0;
258 const GTypeInfo new_info
= {
259 sizeof(JamAccountClass
),
262 (GClassInitFunc
) jam_account_lj_class_init
,
265 sizeof(JamAccountLJ
),
269 new_type
= g_type_register_static(JAM_TYPE_ACCOUNT
,
270 "JamAccountLJ", &new_info
, 0);
276 jam_host_lj_get_server(JamHostLJ
*host
) {
281 jam_host_lj_get_stock_icon(void) {
282 return "logjam-server";
286 parsemoods(xmlDocPtr doc
, xmlNodePtr node
) {
287 GSList
*moods
= NULL
;
290 for (node
= node
->xmlChildrenNode
; node
!= NULL
; node
= node
->next
) {
291 if (xmlStrcmp(node
->name
, BAD_CAST
"mood") == 0) {
294 idstr
= xmlGetProp(node
, BAD_CAST
"id");
295 name
= jam_xmlGetString(doc
, node
);
297 /* an older bug causes some bad moods to be stored.
298 * filter them out. */
299 if (idstr
== NULL
|| name
== NULL
) {
300 if (idstr
) xmlFree(idstr
);
301 if (name
) xmlFree(name
);
304 mood
= g_new0(LJMood
, 1);
305 mood
->id
= atoi((char*)idstr
);
308 idstr
= xmlGetProp(node
, BAD_CAST
"parentid");
310 mood
->parentid
= atoi((char*)idstr
);
314 moods
= g_slist_append(moods
, mood
);
315 } else XML_GET_END("parsemoods")
321 jam_host_lj_load_xml(JamHost
*host
, xmlDocPtr doc
, xmlNodePtr node
) {
322 LJServer
*server
= JAM_HOST_LJ(host
)->server
;
324 XML_GET_STR("url", server
->url
)
325 XML_GET_INT("protocolversion", server
->protocolversion
)
326 XML_GET_FUNC("moods", server
->moods
, parsemoods
)
327 /* else: */ return FALSE
;
333 jam_host_lj_save_xml(JamHost
*host
, xmlNodePtr servernode
) {
334 JamHostLJ
*hostlj
= JAM_HOST_LJ(host
);
337 LJServer
*server
= hostlj
->server
;
339 xmlSetProp(servernode
, BAD_CAST
"protocol", BAD_CAST
"livejournal");
341 xmlNewTextChild(servernode
, NULL
, BAD_CAST
"url", BAD_CAST server
->url
);
343 jam_xmlAddInt(servernode
, "protocolversion", server
->protocolversion
);
345 node
= xmlNewChild(servernode
, NULL
, BAD_CAST
"moods", NULL
);
346 for (l
= server
->moods
; l
!= NULL
; l
= l
->next
) {
347 LJMood
*mood
= (LJMood
*)l
->data
;
349 xmlNodePtr moodnode
= xmlNewTextChild(node
, NULL
, BAD_CAST
"mood",
350 BAD_CAST mood
->name
);
352 g_snprintf(buf
, 10, "%d", mood
->id
);
353 xmlSetProp(moodnode
, BAD_CAST
"id", BAD_CAST buf
);
354 if (mood
->parentid
) {
355 g_snprintf(buf
, 10, "%d", mood
->parentid
);
356 xmlSetProp(moodnode
, BAD_CAST
"parentid", BAD_CAST buf
);
362 jam_host_lj_make_account(JamHost
*host
, const char *username
) {
364 acc
= jam_account_lj_new(JAM_HOST_LJ(host
)->server
, username
);
365 jam_host_add_account(host
, acc
);
370 jam_host_lj_do_action(JamHost
*host
, NetContext
*ctx
, JamDoc
*doc
, gboolean edit
, gboolean
delete, GError
**err
) {
371 LJEditPostEvent
*editpostevent
;
372 JamAccount
*account
= jam_doc_get_account(doc
);
373 JamAccountLJ
*acc
= JAM_ACCOUNT_LJ(account
);
375 gboolean ret
= FALSE
;
377 entry
= jam_doc_get_entry(doc
);
381 g_free(entry
->event
);
387 if (jam_host_lj_get_server(JAM_HOST_LJ(host
))->protocolversion
< 1) {
388 /* scan the entry and make sure they're sending ASCII. */
390 gboolean invalid
= FALSE
;
392 for (p
= entry
->event
; *p
; p
++) {
399 jam_warning(parent
, _("Error: Your post contains Unicode "
400 "characters, but you're trying to submit them to a server "
401 "that doesn't support Unicode."));
407 editpostevent
= lj_editpostevent_new(jam_account_lj_get_user(acc
),
408 jam_doc_get_usejournal(doc
), edit
, entry
);
410 if (net_run_verb_ctx((LJVerb
*)editpostevent
, ctx
, err
))
413 /*fprintf(stderr, "url: [%s]\n", lj_result_get(((LJVerb *)editpostevent)->result, "url"));*/
414 jam_doc_set_url(doc
, lj_result_get(((LJVerb
*)editpostevent
)->result
, "url"));
416 lj_editpostevent_free(editpostevent
);
417 lj_entry_free(entry
);
423 jam_host_lj_do_post(JamHost
*host
, NetContext
*ctx
, void *doc
, GError
**err
) {
424 return jam_host_lj_do_action(host
, ctx
, doc
, FALSE
, FALSE
, err
);
428 jam_host_lj_do_edit(JamHost
*host
, NetContext
*ctx
, void *doc
, GError
**err
) {
429 return jam_host_lj_do_action(host
, ctx
, doc
, TRUE
, FALSE
, err
);
433 jam_host_lj_do_delete(JamHost
*host
, NetContext
*ctx
, void *doc
, GError
**err
) {
434 return jam_host_lj_do_action(host
, ctx
, doc
, TRUE
, TRUE
, err
);
438 jam_host_lj_class_init(JamHostClass
*klass
) {
439 klass
->get_stock_icon
= jam_host_lj_get_stock_icon
;
440 klass
->save_xml
= jam_host_lj_save_xml
;
441 klass
->load_xml
= jam_host_lj_load_xml
;
442 klass
->make_account
= jam_host_lj_make_account
;
443 klass
->do_post
= jam_host_lj_do_post
;
444 klass
->do_edit
= jam_host_lj_do_edit
;
445 klass
->do_delete
= jam_host_lj_do_delete
;
449 jam_host_lj_get_type(void) {
450 static GType new_type
= 0;
452 const GTypeInfo new_info
= {
453 sizeof(JamHostClass
),
456 (GClassInitFunc
) jam_host_lj_class_init
,
463 new_type
= g_type_register_static(JAM_TYPE_HOST
,
464 "JamHostLJ", &new_info
, 0);
470 jam_host_lj_new(LJServer
*s
) {
471 JamHostLJ
*h
= JAM_HOST_LJ(g_object_new(jam_host_lj_get_type(), NULL
));