progress.*: cosmetix
[k8lowj.git] / src / conf.c
blob71115c9f538f3726a6dfcb788760f5425df8e25f
1 /* logjam - a GTK client for LiveJournal.
2 * Copyright (C) 2000-2003 Evan Martin <evan@livejournal.com>
3 */
4 #include "glib-all.h"
6 #include <errno.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
11 #include "liblj/livejournal.h"
13 #include "account.h"
14 #include "conf.h"
15 #include "conf_xml.h"
16 #include "util.h"
19 #define PATH_BUF_SIZE (1024)
22 Configuration conf;
23 Application app;
26 JamHost *conf_host_by_name (Configuration *c, const char *hostname) {
27 GSList *l;
28 for (l = c->hosts; l != NULL; l = l->next) {
29 if (strcmp(hostname, ((JamHost *)l->data)->name) == 0) {
30 return l->data;
33 return NULL;
37 gboolean conf_verify_dir (void) {
38 return verify_dir(app.conf_dir, NULL);
42 void conf_verify_a_host_exists (void) {
43 if (conf.hosts == NULL) {
44 /* make a default host. */
45 LJServer *s = lj_server_new("http://lj.rossia.org");
46 JamHost *host = (JamHost *) jam_host_lj_new(s);
47 host->name = g_strdup("LJ.Rossia.Org");
48 conf.hosts = g_slist_append(conf.hosts, host);
53 void conf_make_path (const char *file, char *buf, size_t bufsz) {
54 char *path = g_build_filename(app.conf_dir, file, NULL);
55 snprintf(buf, bufsz, "%s", path);
56 g_free(path);
60 char *conf_make_account_path (JamAccount *acc, const char *path) {
61 //k8: "" was NULL
62 return g_build_filename(app.conf_dir, "servers", jam_account_get_host(acc)->name, "users", jam_account_get_username(acc), (path ? path : ""), NULL);
66 gboolean conf_rename_host (JamHost *host, const char *newname, GError **err) {
67 char *oldpath, *newpath;
68 /* disallow:
69 * [empty string]
70 * .
71 * ..
72 * ./../foo
73 * /foo
74 * allow:
75 * .lAmE.sErVeR.
77 if (newname[0] == 0 || (newname[0] == '.' && (newname[1] == '.' || newname[1] == '/' || newname[1] == 0)) || newname[0] == '/') {
78 g_set_error(err, 0, 0, "%s", _("new host name is invalid"));
79 return FALSE;
81 oldpath = g_build_filename(app.conf_dir, "servers", host->name, NULL);
82 if (!g_file_test(oldpath, G_FILE_TEST_EXISTS)) {
83 string_replace(&host->name, g_strdup(newname));
84 g_free(oldpath);
85 return TRUE;
87 newpath = g_build_filename(app.conf_dir, "servers", newname, NULL);
88 if (rename(oldpath, newpath) < 0) {
89 g_set_error(err, G_FILE_ERROR, g_file_error_from_errno(errno), _("renaming '%s' to '%s': %s"), oldpath, newpath, g_strerror(errno));
90 g_free(oldpath);
91 g_free(newpath);
92 return FALSE;
94 string_replace(&host->name, g_strdup(newname));
95 g_free(oldpath);
96 g_free(newpath);
97 return TRUE;