gtk-all.h, util-gtk.h: cosmetix (ignore this headers if built without GTK)
[k8lowj.git] / src / conf.c
blob9fb03a3982d412ba1edfdac919197f754bf7cbda
1 /* logjam - a GTK client for LiveJournal.
2 * Copyright (C) 2000-2003 Evan Martin <evan@livejournal.com>
4 * vim: tabstop=4 shiftwidth=4 noexpandtab :
5 */
7 #include "glib-all.h"
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <errno.h>
14 #include "liblj/livejournal.h"
16 #include "conf_xml.h"
17 #include "conf.h"
18 #include "account.h"
19 #include "util.h"
21 Configuration conf;
22 Application app;
24 #define PATH_BUF_SIZE 1024
26 JamHost*
27 conf_host_by_name(Configuration *c, const char *hostname) {
28 GSList *l;
29 for (l = c->hosts; l != NULL; l = l->next) {
30 if (strcmp(hostname, ((JamHost*)l->data)->name) == 0) {
31 return l->data;
34 return NULL;
37 gboolean
38 conf_verify_dir(void) {
39 return verify_dir(app.conf_dir, NULL);
42 void
43 conf_verify_a_host_exists() {
44 if (conf.hosts == NULL) {
45 /* make a default host. */
46 LJServer *s = lj_server_new("http://www.livejournal.com");
47 JamHost *host = (JamHost*)jam_host_lj_new(s);
48 host->name = g_strdup("LiveJournal.com");
49 conf.hosts = g_slist_append(conf.hosts, host);
53 void
54 conf_make_path(char *file, char *buf, int len) {
55 char *path;
56 path = g_build_filename(app.conf_dir, file, NULL);
57 strncpy(buf, path, len);
58 g_free(path);
61 char*
62 conf_make_account_path(JamAccount *acc, const char *path) {
63 return g_build_filename(app.conf_dir,
64 "servers", jam_account_get_host(acc)->name,
65 "users", jam_account_get_username(acc),
66 path ? path : NULL,
67 NULL);
70 gboolean
71 conf_rename_host(JamHost *host, const char *newname, GError **err) {
72 char *oldpath, *newpath;
74 /* disallow:
75 * [empty string]
76 * .
77 * ..
78 * ./../foo
79 * /foo
80 * allow:
81 * .lAmE.sErVeR.
83 if ((newname[0] == 0) ||
84 (newname[0] == '.' &&
85 (newname[1] == '.' || newname[1] == '/' || newname[1] == 0)) ||
86 (newname[0] == '/')) {
87 g_set_error(err, 0, 0, "%s", _("new host name is invalid"));
88 return FALSE;
90 oldpath = g_build_filename(app.conf_dir, "servers", host->name, NULL);
91 if (!g_file_test(oldpath, G_FILE_TEST_EXISTS)) {
92 string_replace(&host->name, g_strdup(newname));
93 g_free(oldpath);
94 return TRUE;
96 newpath = g_build_filename(app.conf_dir, "servers", newname, NULL);
97 if (rename(oldpath, newpath) < 0) {
98 g_set_error(err, G_FILE_ERROR, g_file_error_from_errno(errno),
99 _("renaming '%s' to '%s': %s"), oldpath, newpath,
100 g_strerror(errno));
101 g_free(oldpath);
102 g_free(newpath);
103 return FALSE;
105 string_replace(&host->name, g_strdup(newname));
106 g_free(oldpath);
107 g_free(newpath);
108 return TRUE;