merge of 'c965c78fc4196475e8fd004312be08c074958764'
[pidgin-git.git] / finch / finch.c
blob49a88ecef6d80fef05f977b1e9e598b04b54cc50
1 /**
2 * finch
4 * Finch is the legal property of its developers, whose names are too numerous
5 * to list here. Please refer to the COPYRIGHT file distributed with this
6 * source distribution.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
22 #include "finch.h"
23 #include <internal.h>
25 #include "account.h"
26 #include "conversation.h"
27 #include "core.h"
28 #include "debug.h"
29 #include "eventloop.h"
30 #include "ft.h"
31 #include "log.h"
32 #include "notify.h"
33 #include "prefs.h"
34 #include "prpl.h"
35 #include "pounce.h"
36 #include "savedstatuses.h"
37 #include "sound.h"
38 #include "status.h"
39 #include "util.h"
40 #include "whiteboard.h"
42 #include "gntdebug.h"
43 #include "gntprefs.h"
44 #include "gntui.h"
45 #include "gntidle.h"
47 #define _GNU_SOURCE
48 #include <getopt.h>
50 #include "config.h"
51 #include "package_revision.h"
53 static void
54 debug_init(void)
56 finch_debug_init();
57 purple_debug_set_ui_ops(finch_debug_get_ui_ops());
60 static GHashTable *ui_info = NULL;
61 static GHashTable *finch_ui_get_info(void)
63 if (ui_info == NULL) {
64 ui_info = g_hash_table_new(g_str_hash, g_str_equal);
66 g_hash_table_insert(ui_info, "name", (char*)_("Finch"));
67 g_hash_table_insert(ui_info, "version", VERSION);
68 g_hash_table_insert(ui_info, "website", "http://pidgin.im");
69 g_hash_table_insert(ui_info, "dev_website", "http://developer.pidgin.im");
70 g_hash_table_insert(ui_info, "client_type", "console");
73 * This is the client key for "Finch." It is owned by the AIM
74 * account "markdoliner." Please don't use this key for other
75 * applications. You can either not specify a client key, in
76 * which case the default "libpurple" key will be used, or you
77 * can register for your own client key at
78 * http://developer.aim.com/manageKeys.jsp
80 g_hash_table_insert(ui_info, "prpl-aim-clientkey", "ma19sqWV9ymU6UYc");
81 g_hash_table_insert(ui_info, "prpl-icq-clientkey", "ma19sqWV9ymU6UYc");
84 * This is the distid for Finch, given to us by AOL. Please
85 * don't use this for other applications. You can just not
86 * specify a distid and libpurple will use a default.
88 g_hash_table_insert(ui_info, "prpl-aim-distid", GINT_TO_POINTER(1552));
89 g_hash_table_insert(ui_info, "prpl-icq-distid", GINT_TO_POINTER(1552));
92 return ui_info;
95 static void
96 finch_quit(void)
98 gnt_ui_uninit();
99 if (ui_info)
100 g_hash_table_destroy(ui_info);
103 static PurpleCoreUiOps core_ops =
105 finch_prefs_init,
106 debug_init,
107 gnt_ui_init,
108 finch_quit,
109 finch_ui_get_info,
111 /* padding */
112 NULL,
113 NULL,
114 NULL
117 static PurpleCoreUiOps *
118 gnt_core_get_ui_ops(void)
120 return &core_ops;
123 /* Anything IO-related is directly copied from gtkpurple's source tree */
125 #define FINCH_READ_COND (G_IO_IN | G_IO_HUP | G_IO_ERR)
126 #define FINCH_WRITE_COND (G_IO_OUT | G_IO_HUP | G_IO_ERR | G_IO_NVAL)
128 typedef struct _PurpleGntIOClosure {
129 PurpleInputFunction function;
130 guint result;
131 gpointer data;
133 } PurpleGntIOClosure;
135 static void purple_gnt_io_destroy(gpointer data)
137 g_free(data);
140 static gboolean purple_gnt_io_invoke(GIOChannel *source, GIOCondition condition, gpointer data)
142 PurpleGntIOClosure *closure = data;
143 PurpleInputCondition purple_cond = 0;
145 if (condition & FINCH_READ_COND)
146 purple_cond |= PURPLE_INPUT_READ;
147 if (condition & FINCH_WRITE_COND)
148 purple_cond |= PURPLE_INPUT_WRITE;
150 #if 0
151 purple_debug(PURPLE_DEBUG_MISC, "gtk_eventloop",
152 "CLOSURE: callback for %d, fd is %d\n",
153 closure->result, g_io_channel_unix_get_fd(source));
154 #endif
156 #ifdef _WIN32
157 if(! purple_cond) {
158 #if DEBUG
159 purple_debug_misc("gnt_eventloop",
160 "CLOSURE received GIOCondition of 0x%x, which does not"
161 " match 0x%x (READ) or 0x%x (WRITE)\n",
162 condition, FINCH_READ_COND, FINCH_WRITE_COND);
163 #endif /* DEBUG */
165 return TRUE;
167 #endif /* _WIN32 */
169 closure->function(closure->data, g_io_channel_unix_get_fd(source),
170 purple_cond);
172 return TRUE;
175 static guint gnt_input_add(gint fd, PurpleInputCondition condition, PurpleInputFunction function,
176 gpointer data)
178 PurpleGntIOClosure *closure = g_new0(PurpleGntIOClosure, 1);
179 GIOChannel *channel;
180 GIOCondition cond = 0;
182 closure->function = function;
183 closure->data = data;
185 if (condition & PURPLE_INPUT_READ)
186 cond |= FINCH_READ_COND;
187 if (condition & PURPLE_INPUT_WRITE)
188 cond |= FINCH_WRITE_COND;
190 channel = g_io_channel_unix_new(fd);
191 closure->result = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, cond,
192 purple_gnt_io_invoke, closure, purple_gnt_io_destroy);
194 g_io_channel_unref(channel);
195 return closure->result;
198 static PurpleEventLoopUiOps eventloop_ops =
200 g_timeout_add,
201 g_source_remove,
202 gnt_input_add,
203 g_source_remove,
204 NULL, /* input_get_error */
205 #if GLIB_CHECK_VERSION(2,14,0)
206 g_timeout_add_seconds,
207 #else
208 NULL,
209 #endif
211 /* padding */
212 NULL,
213 NULL,
214 NULL
217 static PurpleEventLoopUiOps *
218 gnt_eventloop_get_ui_ops(void)
220 return &eventloop_ops;
223 /* This is mostly copied from gtkpurple's source tree */
224 static void
225 show_usage(const char *name, gboolean terse)
227 char *text;
229 if (terse) {
230 text = g_strdup_printf(_("%s. Try `%s -h' for more information.\n"), DISPLAY_VERSION, name);
231 } else {
232 text = g_strdup_printf(_("%s\n"
233 "Usage: %s [OPTION]...\n\n"
234 " -c, --config=DIR use DIR for config files\n"
235 " -d, --debug print debugging messages to stderr\n"
236 " -h, --help display this help and exit\n"
237 " -n, --nologin don't automatically login\n"
238 " -v, --version display the current version and exit\n"), DISPLAY_VERSION, name);
241 purple_print_utf8_to_console(stdout, text);
242 g_free(text);
245 static int
246 init_libpurple(int argc, char **argv)
248 char *path;
249 int opt;
250 gboolean opt_help = FALSE;
251 gboolean opt_nologin = FALSE;
252 gboolean opt_version = FALSE;
253 char *opt_config_dir_arg = NULL;
254 gboolean debug_enabled = FALSE;
256 struct option long_options[] = {
257 {"config", required_argument, NULL, 'c'},
258 {"debug", no_argument, NULL, 'd'},
259 {"help", no_argument, NULL, 'h'},
260 {"nologin", no_argument, NULL, 'n'},
261 {"version", no_argument, NULL, 'v'},
262 {0, 0, 0, 0}
265 #ifdef ENABLE_NLS
266 bindtextdomain(PACKAGE, LOCALEDIR);
267 bind_textdomain_codeset(PACKAGE, "UTF-8");
268 textdomain(PACKAGE);
269 #endif
271 #ifdef HAVE_SETLOCALE
272 setlocale(LC_ALL, "");
273 #endif
275 /* scan command-line options */
276 opterr = 1;
277 while ((opt = getopt_long(argc, argv,
278 #ifndef _WIN32
279 "c:dhn::v",
280 #else
281 "c:dhn::v",
282 #endif
283 long_options, NULL)) != -1) {
284 switch (opt) {
285 case 'c': /* config dir */
286 g_free(opt_config_dir_arg);
287 opt_config_dir_arg = g_strdup(optarg);
288 break;
289 case 'd': /* debug */
290 debug_enabled = TRUE;
291 break;
292 case 'h': /* help */
293 opt_help = TRUE;
294 break;
295 case 'n': /* no autologin */
296 opt_nologin = TRUE;
297 break;
298 case 'v': /* version */
299 opt_version = TRUE;
300 break;
301 case '?': /* show terse help */
302 default:
303 show_usage(argv[0], TRUE);
304 return 0;
305 break;
309 /* show help message */
310 if (opt_help) {
311 show_usage(argv[0], FALSE);
312 return 0;
314 /* show version message */
315 if (opt_version) {
316 /* Translators may want to transliterate the name.
317 It is not to be translated. */
318 printf("%s %s (%s)\n", _("Finch"), DISPLAY_VERSION, REVISION);
319 return 0;
322 /* set a user-specified config directory */
323 if (opt_config_dir_arg != NULL) {
324 purple_util_set_user_dir(opt_config_dir_arg);
325 g_free(opt_config_dir_arg);
329 * We're done piddling around with command line arguments.
330 * Fire up this baby.
333 /* We don't want debug-messages to show up and corrupt the display */
334 purple_debug_set_enabled(debug_enabled);
336 /* If we're using a custom configuration directory, we
337 * do NOT want to migrate, or weird things will happen. */
338 if (opt_config_dir_arg == NULL)
340 if (!purple_core_migrate())
342 char *old = g_strconcat(purple_home_dir(),
343 G_DIR_SEPARATOR_S ".gaim", NULL);
344 char *text = g_strdup_printf(_(
345 "%s encountered errors migrating your settings "
346 "from %s to %s. Please investigate and complete the "
347 "migration by hand. Please report this error at http://developer.pidgin.im"), _("Finch"),
348 old, purple_user_dir());
350 g_free(old);
352 purple_print_utf8_to_console(stderr, text);
353 g_free(text);
355 return 0;
359 purple_core_set_ui_ops(gnt_core_get_ui_ops());
360 purple_eventloop_set_ui_ops(gnt_eventloop_get_ui_ops());
361 purple_idle_set_ui_ops(finch_idle_get_ui_ops());
363 path = g_build_filename(purple_user_dir(), "plugins", NULL);
364 purple_plugins_add_search_path(path);
365 g_free(path);
367 purple_plugins_add_search_path(LIBDIR);
369 if (!purple_core_init(FINCH_UI))
371 fprintf(stderr,
372 "Initialization of the Purple core failed. Dumping core.\n"
373 "Please report this!\n");
374 abort();
377 /* TODO: Move blist loading into purple_blist_init() */
378 purple_set_blist(purple_blist_new());
379 purple_blist_load();
381 /* TODO: should this be moved into finch_prefs_init() ? */
382 finch_prefs_update_old();
384 /* load plugins we had when we quit */
385 purple_plugins_load_saved("/finch/plugins/loaded");
387 /* TODO: Move pounces loading into purple_pounces_init() */
388 purple_pounces_load();
390 if (opt_nologin)
392 /* Set all accounts to "offline" */
393 PurpleSavedStatus *saved_status;
395 /* If we've used this type+message before, lookup the transient status */
396 saved_status = purple_savedstatus_find_transient_by_type_and_message(
397 PURPLE_STATUS_OFFLINE, NULL);
399 /* If this type+message is unique then create a new transient saved status */
400 if (saved_status == NULL)
401 saved_status = purple_savedstatus_new(NULL, PURPLE_STATUS_OFFLINE);
403 /* Set the status for each account */
404 purple_savedstatus_activate(saved_status);
406 else
408 /* Everything is good to go--sign on already */
409 if (!purple_prefs_get_bool("/purple/savedstatus/startup_current_status"))
410 purple_savedstatus_activate(purple_savedstatus_get_startup());
411 purple_accounts_restore_current_statuses();
414 return 1;
417 static gboolean gnt_start(int *argc, char ***argv)
419 /* Initialize the libpurple stuff */
420 if (!init_libpurple(*argc, *argv))
421 return FALSE;
423 purple_blist_show();
424 return TRUE;
427 int main(int argc, char *argv[])
429 signal(SIGPIPE, SIG_IGN);
431 g_thread_init(NULL);
433 g_set_prgname("Finch");
434 #if GLIB_CHECK_VERSION(2,2,0)
435 g_set_application_name(_("Finch"));
436 #endif
438 if (gnt_start(&argc, &argv)) {
439 gnt_main();
441 #ifdef STANDALONE
442 purple_core_quit();
443 #endif
446 return 0;