gtk-all.h, util-gtk.h: cosmetix (ignore this headers if built without GTK)
[k8lowj.git] / src / host.c
blob015ae2e78460274d3959bfa8950a88a91cd148bd
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 "glib-all.h"
9 #include <string.h>
10 #include "jamdoc.h"
11 #include "account.h"
12 #include "jam_xml.h"
13 #include "conf_xml.h"
14 #include "util.h"
16 const char*
17 jam_host_get_stock_icon(JamHost *host) {
18 return JAM_HOST_GET_CLASS(host)->get_stock_icon();
21 JamAccount*
22 jam_host_get_account_by_username(JamHost *host,
23 const char *username, gboolean create) {
24 JamAccount *acc = NULL;
25 GSList *l;
26 for (l = host->accounts; l != NULL; l = l->next) {
27 acc = l->data;
28 if (strcmp(username, jam_account_get_username(acc)) == 0)
29 return acc;
31 /* this is a previously-unknown account. */
32 if (create)
33 return JAM_HOST_GET_CLASS(host)->make_account(host, username);
34 else
35 return NULL;
38 void
39 jam_host_add_account(JamHost *host, JamAccount *acc) {
40 acc->host = host;
41 host->accounts = g_slist_append(host->accounts, acc);
44 static void*
45 parseuserdir(const char *dirname, void *host) {
46 return conf_parsedirxml(dirname, (conf_parsedirxml_fn)jam_account_from_xml, host);
49 JamHost*
50 jam_host_from_xml(xmlDocPtr doc, xmlNodePtr node, void *data) {
51 JamHost *host = NULL;
52 char *username = NULL;
53 char *userspath;
54 JamHostClass *klass;
56 xmlChar *protocol = xmlGetProp(node, BAD_CAST "protocol");
57 if (!protocol || xmlStrcmp(protocol, BAD_CAST "livejournal") == 0) {
58 host = JAM_HOST(jam_host_lj_new(lj_server_new(NULL)));
59 } else {
60 g_error("unknown protocol '%s'\n", protocol);
62 if (protocol) xmlFree(protocol);
63 klass = JAM_HOST_GET_CLASS(host);
65 for (node = node->xmlChildrenNode; node != NULL; node = node->next) {
66 XML_GET_STR("name", host->name)
67 XML_GET_STR("currentuser", username)
68 XML_GET_SUB(host, klass->load_xml)
69 XML_GET_END("jam_host_from_xml")
72 userspath = g_build_filename(data, "users", NULL);
73 host->accounts = conf_parsedirlist(userspath, parseuserdir, host);
74 g_free(userspath);
76 /* after we've parsed all of the users, scan for the current one. */
77 if (username) {
78 host->lastaccount = jam_host_get_account_by_username(host, username, FALSE);
79 g_free(username);
82 return host;
85 gboolean
86 jam_host_write(JamHost *host, GError **err) {
87 xmlDocPtr doc = NULL;
88 GSList *l;
89 char *path;
90 xmlNodePtr servernode;
91 GError *terr = NULL;
92 gboolean ret = FALSE;
94 path = g_build_filename(app.conf_dir, "servers", host->name, "conf.xml", NULL);
95 if (!verify_path(path, FALSE, err))
96 goto out;
98 jam_xmlNewDoc(&doc, &servernode, "server");
100 xmlNewTextChild(servernode, NULL, BAD_CAST "name", BAD_CAST host->name);
102 for (l = host->accounts; l != NULL; l = l->next) {
103 JamAccount *acc = l->data;
104 if (!jam_account_write(acc, &terr)) {
105 g_printerr("Error writing account: %s\n", terr->message);
106 g_error_free(terr);
107 terr = NULL;
111 if (host->lastaccount)
112 xmlNewTextChild(servernode, NULL, BAD_CAST "currentuser",
113 BAD_CAST jam_account_get_username(host->lastaccount));
115 JAM_HOST_GET_CLASS(host)->save_xml(host, servernode);
117 if (xmlSaveFormatFile(path, doc, TRUE) < 0) {
118 g_set_error(err, 0, 0, "xmlSaveFormatFile error saving to %s.\n", path);
119 goto out;
121 ret = TRUE;
123 out:
124 if (path) g_free(path);
125 if (doc) xmlFreeDoc(doc);
127 return ret;
130 GType
131 jam_host_get_type(void) {
132 static GType new_type = 0;
133 if (!new_type) {
134 const GTypeInfo new_info = {
135 sizeof(JamHostClass),
136 NULL,
137 NULL,
138 NULL,
139 NULL,
140 NULL,
141 sizeof(JamHost),
143 NULL
145 new_type = g_type_register_static(G_TYPE_OBJECT,
146 "JamHost", &new_info, G_TYPE_FLAG_ABSTRACT);
148 return new_type;
152 /* protocol functions. */
153 gboolean
154 jam_host_do_post(JamHost *host, NetContext *ctx, void *doc, GError **err) {
155 if (ctx->title)
156 ctx->title(ctx, _("Submitting Entry"));
157 return JAM_HOST_GET_CLASS(host)->do_post(host, ctx, doc, err);
160 gboolean
161 jam_host_do_edit(JamHost *host, NetContext *ctx, void *doc, GError **err) {
162 if (ctx->title)
163 ctx->title(ctx, _("Saving Changes"));
164 return JAM_HOST_GET_CLASS(host)->do_edit(host, ctx, doc, err);
167 gboolean
168 jam_host_do_delete(JamHost *host, NetContext *ctx, void *doc, GError **err) {
169 if (ctx->title)
170 ctx->title(ctx, _("Deleting Entry"));
171 return JAM_HOST_GET_CLASS(host)->do_delete(host, ctx, doc, err);