gtk-all.h, util-gtk.h: cosmetix (ignore this headers if built without GTK)
[k8lowj.git] / src / init.c
blob52432c0057f0597dbe87db16f9165f4d6e80562d
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 #ifdef HAVE_GTK
8 #include "gtk-all.h"
9 #else
10 #include "glib-all.h"
11 #endif
13 #ifdef ENABLE_NLS
14 #include <libintl.h>
15 #endif
17 #include <stdlib.h> /* for exit (and __argc and __argv on windows) */
19 #include <string.h>
20 #include <stdio.h>
21 #include <errno.h>
22 #include <signal.h>
23 #include <locale.h>
25 #include "account.h"
26 #include "conf.h"
27 #include "conf_xml.h"
28 #include "jamdoc.h"
29 #include "cmdline.h"
31 #ifdef HAVE_GTK
32 #include "login.h"
33 #include "jam.h"
34 #include "icons.h"
35 #include "remote.h"
36 #include "spawn.h"
37 #endif
39 void set_defaults() {
40 #ifdef HAVE_GTK
41 #ifndef G_OS_WIN32
42 if (conf.spawn_command == NULL)
43 conf.spawn_command = g_strdup(spawn_commands[0].command);
44 #endif
45 #endif /* HAVE_GTK */
47 conf_verify_a_host_exists();
49 if (conf.cfuserinterval == 0)
50 conf.cfuserinterval = 30;
51 cf_threshold_normalize(&conf.cfthreshold);
54 #ifdef HAVE_GTK
55 static void
56 try_remote_command(JamAccount *acc) {
57 const char *username = acc ? jam_account_get_username(acc) : NULL;
58 GError *err = NULL;
59 /* send the remote command.
60 * you'd think we'd want to conditionally do this on the configuration
61 * option, but you'd be wrong. :)
62 * if the setting in the conf is "allow multiple instances", but then
63 * the user changes the setting to "only one instance" in the ui and then
64 * runs another copy of logjam, it's counterintuitive for it to ignore that
65 * changed ui setting, even if it hadn't got propogated to the
66 * configuration file yet.
67 * so we always try to use the socket. the conf setting is only
68 * used when we consider *creating* the socket. */
70 /* a null username just brings the window to the foreground. */
71 if (remote_send_user(username, &err)) {
72 /* we popped up another window, so we're done. */
73 g_print(_("Bringing already-running LogJam instance to the "
74 "foreground...\n"));
75 exit(EXIT_SUCCESS);
76 } else if (err) {
77 g_printerr(_("Error sending remote command: %s.\n"), err->message);
78 g_error_free(err);
79 exit(EXIT_FAILURE);
82 #endif /* HAVE_GTK */
84 static void
85 init_app(int *argc, gchar *argv[]) {
86 gint i, shift = 0;
88 memset(&app, 0, sizeof(Application));
89 app.programname = argv[0]; /* not the same as PROGRAMNAME */
91 /* treatment of configuration dir; must be done before read_conf */
92 for (i = 1; i < *argc; i++) { /* look for arg */
93 if (!strcmp(argv[i], "--conf-dir")) {
94 app.conf_dir = argv[i+1];
95 shift = 2;
96 break;
98 if (g_strrstr(argv[i], "--conf-dir=")) {
99 app.conf_dir = argv[i] + strlen("--conf-dir=");
100 shift = 1;
101 break;
104 if (shift) { /* shift what comes after */
105 *argc -= shift;
106 for (; i < *argc; i++) {
107 argv[i] = argv[i + shift];
111 if (!app.conf_dir) {
112 app.conf_dir = g_strdup_printf("%s/.logjam", g_get_home_dir());
114 #ifdef HAVE_GTK
115 app.tooltips = gtk_tooltips_new();
116 gtk_tooltips_enable(app.tooltips);
117 #endif
120 #ifdef HAVE_GTK
121 static void
122 run_gtk(JamDoc *doc) {
123 gchar *accelpath;
125 app.remote = logjam_remote_new();
127 accelpath = g_build_filename(app.conf_dir, "accels", NULL);
128 gtk_accel_map_load(accelpath);
130 icons_initialize();
132 /* the 99% common case is the user has a username/password/host remembered
133 * and they just want to use those. */
134 if (!jam_doc_get_account(doc)) {
135 JamAccount *acc = NULL;
136 if (conf.lasthost && conf.lasthost->lastaccount &&
137 conf.lasthost->lastaccount->remember_password) {
138 acc = conf.lasthost->lastaccount;
139 if (JAM_ACCOUNT_IS_LJ(acc))
140 login_check_lastupdate(NULL, JAM_ACCOUNT_LJ(acc));
141 } else {
142 acc = login_dlg_run(NULL, conf.lasthost, acc);
143 if (!acc) return;
146 jam_doc_set_account(doc, acc);
149 jam_run(doc);
151 g_object_unref(G_OBJECT(app.remote));
153 gtk_accel_map_save(accelpath);
154 g_free(accelpath);
155 return;
157 #endif
159 static void
160 setup_locales() {
161 /* This call actually initializes the locale settings,
162 * and doesn't just clear LC_ALL (which is sorta what
163 * it looks like it's doing). */
164 setlocale(LC_ALL, "");
166 #ifdef ENABLE_NLS
167 bindtextdomain(GETTEXT_PACKAGE, LOCALEDIR);
168 /* GTK always wants UTF-8 strings.
169 * gettext will try to translate to the current locale's codeset
170 * unless we tell it to leave it in UTF-8. */
171 bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
172 textdomain(GETTEXT_PACKAGE);
173 #endif
176 static void
177 setup_glibgdk() {
178 #ifdef HAVE_GTK
179 /* calls to these thread inits must happen before any other
180 * g* calls. */
181 if (!g_thread_supported()) g_thread_init(NULL);
182 gdk_threads_init();
183 gdk_threads_enter();
184 #endif /* HAVE_GTK */
186 g_type_init();
188 g_set_application_name(_("LogJam"));
192 main(int argc, char* argv[]) {
193 #ifdef HAVE_GTK
194 gboolean has_gtk;
195 #endif
196 JamDoc *doc;
198 setup_locales();
200 setup_glibgdk();
202 init_app(&argc, argv); /* should be called before conf_read */
203 conf_read(&conf, app.conf_dir);
204 set_defaults();
205 jam_account_logjam_init();
207 #ifdef HAVE_GTK
208 #ifdef G_OS_WIN32
209 gtk_rc_add_default_file("gtkrc");
210 #endif
211 has_gtk = gtk_init_check(&argc, &argv);
212 #endif
214 doc = jam_doc_new();
215 cmdline_parse(doc, argc, argv); /* may terminate. */
217 #ifdef HAVE_GTK
218 if (!has_gtk) {
219 g_printerr(_("GTK init failed -- are you in X?\n"));
220 g_printerr(_("Run %s --help for command-line options.\n"), app.programname);
221 exit(EXIT_FAILURE);
223 try_remote_command(jam_doc_get_account(doc));
224 run_gtk(doc);
225 #else
226 g_printerr(_("Error: No action given.\n"));
227 g_printerr(_("Run %s --help for command-line options.\n"), app.programname);
228 exit(EXIT_FAILURE);
229 #endif
231 conf_write(&conf, app.conf_dir);
233 #ifdef G_OS_WIN32
234 gdk_threads_leave();
235 #endif
237 return 0;
240 #ifdef G_OS_WIN32
241 int _stdcall WinMain(void *hInst, void *hPrev, char* lpCmdLine, int nCmdShow) {
242 return main(__argc, __argv);
244 #endif