poll.h: cosmetix
[k8lowj.git] / src / init.c
blob0646b950a46f350acf8b1efb3687f5772670b56a
1 /* logjam - a GTK client for LiveJournal.
2 * Copyright (C) 2000-2003 Evan Martin <evan@livejournal.com>
3 */
4 #ifdef HAVE_GTK
5 # include "gtk-all.h"
6 #else
7 # include "glib-all.h"
8 #endif
10 #ifdef ENABLE_NLS
11 # include <libintl.h>
12 #endif
14 #include <errno.h>
15 #include <locale.h>
16 #include <signal.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
21 #include "account.h"
22 #include "conf.h"
23 #include "conf_xml.h"
24 #include "jamdoc.h"
25 #include "cmdline.h"
27 #ifdef HAVE_GTK
28 # include "icons.h"
29 # include "jam.h"
30 # include "login.h"
31 # include "remote.h"
32 # include "spawn.h"
33 #endif
36 void set_defaults (void) {
37 #ifdef HAVE_GTK
38 if (conf.spawn_command == NULL) conf.spawn_command = g_strdup(spawn_commands[0].command);
39 #endif /* HAVE_GTK */
40 conf_verify_a_host_exists();
41 if (conf.cfuserinterval == 0) conf.cfuserinterval = 30;
42 cf_threshold_normalize(&conf.cfthreshold);
46 #ifdef HAVE_GTK
47 static void try_remote_command (JamAccount *acc) {
48 const char *username = (acc ? jam_account_get_username(acc) : NULL);
49 GError *err = NULL;
50 /* send the remote command.
51 * you'd think we'd want to conditionally do this on the configuration
52 * option, but you'd be wrong. :)
53 * if the setting in the conf is "allow multiple instances", but then
54 * the user changes the setting to "only one instance" in the ui and then
55 * runs another copy of logjam, it's counterintuitive for it to ignore that
56 * changed ui setting, even if it hadn't got propogated to the
57 * configuration file yet.
58 * so we always try to use the socket. the conf setting is only
59 * used when we consider *creating* the socket. */
60 /* a null username just brings the window to the foreground. */
61 if (remote_send_user(username, &err)) {
62 /* we popped up another window, so we're done. */
63 g_print(_("Bringing already-running LogJam instance to the " "foreground...\n"));
64 exit(EXIT_SUCCESS);
65 } else if (err) {
66 g_printerr(_("Error sending remote command: %s.\n"), err->message);
67 g_error_free(err);
68 exit(EXIT_FAILURE);
71 #endif /* HAVE_GTK */
74 static void init_app (int *argc, gchar *argv[]) {
75 gint i, shift = 0;
76 memset(&app, 0, sizeof(Application));
77 app.programname = argv[0]; /* not the same as PROGRAMNAME */
78 /* treatment of configuration dir; must be done before read_conf */
79 for (i = 1; i < *argc; i++) { /* look for arg */
80 if (!strcmp(argv[i], "--conf-dir")) {
81 app.conf_dir = argv[i + 1];
82 shift = 2;
83 break;
85 if (g_strrstr(argv[i], "--conf-dir=")) {
86 app.conf_dir = argv[i] + strlen("--conf-dir=");
87 shift = 1;
88 break;
91 if (shift) {
92 /* shift what comes after */
93 *argc -= shift;
94 for (; i < *argc; ++i) argv[i] = argv[i+shift];
96 if (!app.conf_dir) app.conf_dir = g_strdup_printf("%s/.logjam", g_get_home_dir());
97 #ifdef HAVE_GTK
98 app.tooltips = gtk_tooltips_new();
99 gtk_tooltips_enable(app.tooltips);
100 #endif
104 #ifdef HAVE_GTK
105 static void run_gtk (JamDoc *doc) {
106 gchar *accelpath;
107 app.remote = logjam_remote_new();
108 accelpath = g_build_filename(app.conf_dir, "accels", NULL);
109 gtk_accel_map_load(accelpath);
110 icons_initialize();
111 /* the 99% common case is the user has a username/password/host remembered
112 * and they just want to use those. */
113 if (!jam_doc_get_account(doc)) {
114 JamAccount *acc = NULL;
115 if (conf.lasthost && conf.lasthost->lastaccount && conf.lasthost->lastaccount->remember_password) {
116 acc = conf.lasthost->lastaccount;
117 if (JAM_ACCOUNT_IS_LJ(acc)) login_check_lastupdate(NULL, JAM_ACCOUNT_LJ(acc));
118 } else {
119 acc = login_dlg_run(NULL, conf.lasthost, acc);
120 if (!acc) return;
122 jam_doc_set_account(doc, acc);
124 jam_run(doc);
125 g_object_unref(G_OBJECT(app.remote));
126 gtk_accel_map_save(accelpath);
127 g_free(accelpath);
128 return;
130 #endif
133 static void setup_locales (void) {
134 /* This call actually initializes the locale settings,
135 * and doesn't just clear LC_ALL (which is sorta what
136 * it looks like it's doing). */
137 setlocale(LC_ALL, "");
138 #ifdef ENABLE_NLS
139 bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR);
140 /* GTK always wants UTF-8 strings.
141 * gettext will try to translate to the current locale's codeset
142 * unless we tell it to leave it in UTF-8. */
143 bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
144 textdomain(GETTEXT_PACKAGE);
145 #endif
149 static void setup_glibgdk (void) {
150 #ifdef HAVE_GTK
151 /* calls to these thread inits must happen before any other
152 * g* calls. */
153 if (!g_thread_supported()) g_thread_init(NULL);
154 gdk_threads_init();
155 gdk_threads_enter();
156 #endif /* HAVE_GTK */
157 g_type_init();
158 g_set_application_name(_("LogJam"));
162 int main (int argc, char *argv[]) {
163 #ifdef HAVE_GTK
164 gboolean has_gtk;
165 #endif
166 JamDoc *doc;
168 setup_locales();
169 setup_glibgdk();
171 init_app(&argc, argv); /* should be called before conf_read */
172 conf_read(&conf, app.conf_dir);
173 set_defaults();
174 jam_account_logjam_init();
176 #ifdef HAVE_GTK
177 has_gtk = gtk_init_check(&argc, &argv);
178 #endif
180 doc = jam_doc_new();
181 cmdline_parse(doc, argc, argv); /* may terminate. */
183 #ifdef HAVE_GTK
184 if (!has_gtk) {
185 g_printerr(_("GTK init failed -- are you in X?\n"));
186 g_printerr(_("Run %s --help for command-line options.\n"), app.programname);
187 exit(EXIT_FAILURE);
189 try_remote_command(jam_doc_get_account(doc));
190 run_gtk(doc);
191 #else
192 g_printerr(_("Error: No action given.\n"));
193 g_printerr(_("Run %s --help for command-line options.\n"), app.programname);
194 exit(EXIT_FAILURE);
195 #endif
197 conf_write(&conf, app.conf_dir);
199 return 0;