removed alot of warnings
[k8lowj.git] / src / host.c
blob1ae00c57314e4c4c8b69ad7530c7dd75929eb09e
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 #ifdef blogger_punted_for_this_release
60 } else if (xmlStrcmp(protocol, BAD_CAST "blogger") == 0) {
61 host = JAM_HOST(jam_host_blogger_new());
62 #endif
63 } else {
64 g_error("unknown protocol '%s'\n", protocol);
66 if (protocol) xmlFree(protocol);
67 klass = JAM_HOST_GET_CLASS(host);
69 for (node = node->xmlChildrenNode; node != NULL; node = node->next) {
70 XML_GET_STR("name", host->name)
71 XML_GET_STR("currentuser", username)
72 XML_GET_SUB(host, klass->load_xml)
73 XML_GET_END("jam_host_from_xml")
76 userspath = g_build_filename(data, "users", NULL);
77 host->accounts = conf_parsedirlist(userspath, parseuserdir, host);
78 g_free(userspath);
80 /* after we've parsed all of the users, scan for the current one. */
81 if (username) {
82 host->lastaccount = jam_host_get_account_by_username(host, username, FALSE);
83 g_free(username);
86 return host;
89 gboolean
90 jam_host_write(JamHost *host, GError **err) {
91 xmlDocPtr doc = NULL;
92 GSList *l;
93 char *path;
94 xmlNodePtr servernode;
95 GError *terr = NULL;
96 gboolean ret = FALSE;
98 path = g_build_filename(app.conf_dir, "servers", host->name, "conf.xml", NULL);
99 if (!verify_path(path, FALSE, err))
100 goto out;
102 jam_xmlNewDoc(&doc, &servernode, "server");
104 xmlNewTextChild(servernode, NULL, BAD_CAST "name", BAD_CAST host->name);
106 for (l = host->accounts; l != NULL; l = l->next) {
107 JamAccount *acc = l->data;
108 if (!jam_account_write(acc, &terr)) {
109 g_printerr("Error writing account: %s\n", terr->message);
110 g_error_free(terr);
111 terr = NULL;
115 if (host->lastaccount)
116 xmlNewTextChild(servernode, NULL, BAD_CAST "currentuser",
117 BAD_CAST jam_account_get_username(host->lastaccount));
119 JAM_HOST_GET_CLASS(host)->save_xml(host, servernode);
121 if (xmlSaveFormatFile(path, doc, TRUE) < 0) {
122 g_set_error(err, 0, 0, "xmlSaveFormatFile error saving to %s.\n", path);
123 goto out;
125 ret = TRUE;
127 out:
128 if (path) g_free(path);
129 if (doc) xmlFreeDoc(doc);
131 return ret;
134 GType
135 jam_host_get_type(void) {
136 static GType new_type = 0;
137 if (!new_type) {
138 const GTypeInfo new_info = {
139 sizeof(JamHostClass),
140 NULL,
141 NULL,
142 NULL,
143 NULL,
144 NULL,
145 sizeof(JamHost),
147 NULL
149 new_type = g_type_register_static(G_TYPE_OBJECT,
150 "JamHost", &new_info, G_TYPE_FLAG_ABSTRACT);
152 return new_type;
156 /* protocol functions. */
157 gboolean
158 jam_host_do_post(JamHost *host, NetContext *ctx, void *doc, GError **err) {
159 if (ctx->title)
160 ctx->title(ctx, _("Submitting Entry"));
161 return JAM_HOST_GET_CLASS(host)->do_post(host, ctx, doc, err);
164 gboolean
165 jam_host_do_edit(JamHost *host, NetContext *ctx, void *doc, GError **err) {
166 if (ctx->title)
167 ctx->title(ctx, _("Saving Changes"));
168 return JAM_HOST_GET_CLASS(host)->do_edit(host, ctx, doc, err);
171 gboolean
172 jam_host_do_delete(JamHost *host, NetContext *ctx, void *doc, GError **err) {
173 if (ctx->title)
174 ctx->title(ctx, _("Deleting Entry"));
175 return JAM_HOST_GET_CLASS(host)->do_delete(host, ctx, doc, err);