Open an explorer.exe window at the location of the file when clicking
[pidgin-git.git] / finch / finch.c
blob8ae3617569a567e4c49c580a082f4227ea75611a
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 <internal.h>
23 #include "finch.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 try to register your own at the AIM or ICQ web sites
78 * (although this functionality was removed at some point, it's
79 * possible it has been re-added). AOL's old key management
80 * page is http://developer.aim.com/manageKeys.jsp
82 g_hash_table_insert(ui_info, "prpl-aim-clientkey", "ma19sqWV9ymU6UYc");
85 * This is the client key for "Pidgin." It is owned by the AIM
86 * account "markdoliner." Please don't use this key for other
87 * applications. You can either not specify a client key, in
88 * which case the default "libpurple" key will be used, or you
89 * can try to register your own at the AIM or ICQ web sites
90 * (although this functionality was removed at some point, it's
91 * possible it has been re-added). AOL's old key management
92 * page is http://developer.aim.com/manageKeys.jsp
94 * We used to have a Finch-specific devId/clientkey
95 * (ma19sqWV9ymU6UYc), but it stopped working, so we switched
96 * to this one.
98 g_hash_table_insert(ui_info, "prpl-icq-clientkey", "ma1cSASNCKFtrdv9");
101 * This is the distid for Finch, given to us by AOL. Please
102 * don't use this for other applications. You can just not
103 * specify a distid and libpurple will use a default.
105 g_hash_table_insert(ui_info, "prpl-aim-distid", GINT_TO_POINTER(1552));
106 g_hash_table_insert(ui_info, "prpl-icq-distid", GINT_TO_POINTER(1552));
109 return ui_info;
112 static void
113 finch_quit(void)
115 gnt_ui_uninit();
116 if (ui_info)
117 g_hash_table_destroy(ui_info);
120 static PurpleCoreUiOps core_ops =
122 finch_prefs_init,
123 debug_init,
124 gnt_ui_init,
125 finch_quit,
126 finch_ui_get_info,
128 /* padding */
129 NULL,
130 NULL,
131 NULL
134 static PurpleCoreUiOps *
135 gnt_core_get_ui_ops(void)
137 return &core_ops;
140 /* Anything IO-related is directly copied from gtkpurple's source tree */
142 #define FINCH_READ_COND (G_IO_IN | G_IO_HUP | G_IO_ERR)
143 #define FINCH_WRITE_COND (G_IO_OUT | G_IO_HUP | G_IO_ERR | G_IO_NVAL)
145 typedef struct _PurpleGntIOClosure {
146 PurpleInputFunction function;
147 guint result;
148 gpointer data;
150 } PurpleGntIOClosure;
152 static void purple_gnt_io_destroy(gpointer data)
154 g_free(data);
157 static gboolean purple_gnt_io_invoke(GIOChannel *source, GIOCondition condition, gpointer data)
159 PurpleGntIOClosure *closure = data;
160 PurpleInputCondition purple_cond = 0;
162 if (condition & FINCH_READ_COND)
163 purple_cond |= PURPLE_INPUT_READ;
164 if (condition & FINCH_WRITE_COND)
165 purple_cond |= PURPLE_INPUT_WRITE;
167 #if 0
168 purple_debug(PURPLE_DEBUG_MISC, "gtk_eventloop",
169 "CLOSURE: callback for %d, fd is %d\n",
170 closure->result, g_io_channel_unix_get_fd(source));
171 #endif
173 #ifdef _WIN32
174 if(! purple_cond) {
175 #if DEBUG
176 purple_debug_misc("gnt_eventloop",
177 "CLOSURE received GIOCondition of 0x%x, which does not"
178 " match 0x%x (READ) or 0x%x (WRITE)\n",
179 condition, FINCH_READ_COND, FINCH_WRITE_COND);
180 #endif /* DEBUG */
182 return TRUE;
184 #endif /* _WIN32 */
186 closure->function(closure->data, g_io_channel_unix_get_fd(source),
187 purple_cond);
189 return TRUE;
192 static guint gnt_input_add(gint fd, PurpleInputCondition condition, PurpleInputFunction function,
193 gpointer data)
195 PurpleGntIOClosure *closure = g_new0(PurpleGntIOClosure, 1);
196 GIOChannel *channel;
197 GIOCondition cond = 0;
199 closure->function = function;
200 closure->data = data;
202 if (condition & PURPLE_INPUT_READ)
203 cond |= FINCH_READ_COND;
204 if (condition & PURPLE_INPUT_WRITE)
205 cond |= FINCH_WRITE_COND;
207 channel = g_io_channel_unix_new(fd);
208 closure->result = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT, cond,
209 purple_gnt_io_invoke, closure, purple_gnt_io_destroy);
211 g_io_channel_unref(channel);
212 return closure->result;
215 static PurpleEventLoopUiOps eventloop_ops =
217 g_timeout_add,
218 g_source_remove,
219 gnt_input_add,
220 g_source_remove,
221 NULL, /* input_get_error */
222 #if GLIB_CHECK_VERSION(2,14,0)
223 g_timeout_add_seconds,
224 #else
225 NULL,
226 #endif
228 /* padding */
229 NULL,
230 NULL,
231 NULL
234 static PurpleEventLoopUiOps *
235 gnt_eventloop_get_ui_ops(void)
237 return &eventloop_ops;
240 /* This is mostly copied from gtkpurple's source tree */
241 static void
242 show_usage(const char *name, gboolean terse)
244 char *text;
246 if (terse) {
247 text = g_strdup_printf(_("%s. Try `%s -h' for more information.\n"), DISPLAY_VERSION, name);
248 } else {
249 text = g_strdup_printf(_("%s\n"
250 "Usage: %s [OPTION]...\n\n"
251 " -c, --config=DIR use DIR for config files\n"
252 " -d, --debug print debugging messages to stderr\n"
253 " -h, --help display this help and exit\n"
254 " -n, --nologin don't automatically login\n"
255 " -v, --version display the current version and exit\n"), DISPLAY_VERSION, name);
258 purple_print_utf8_to_console(stdout, text);
259 g_free(text);
262 static int
263 init_libpurple(int argc, char **argv)
265 char *path;
266 int opt;
267 gboolean opt_help = FALSE;
268 gboolean opt_nologin = FALSE;
269 gboolean opt_version = FALSE;
270 char *opt_config_dir_arg = NULL;
271 gboolean debug_enabled = FALSE;
272 struct stat st;
274 struct option long_options[] = {
275 {"config", required_argument, NULL, 'c'},
276 {"debug", no_argument, NULL, 'd'},
277 {"help", no_argument, NULL, 'h'},
278 {"nologin", no_argument, NULL, 'n'},
279 {"version", no_argument, NULL, 'v'},
280 {0, 0, 0, 0}
283 #ifdef ENABLE_NLS
284 bindtextdomain(PACKAGE, LOCALEDIR);
285 bind_textdomain_codeset(PACKAGE, "UTF-8");
286 textdomain(PACKAGE);
287 #endif
289 #ifdef HAVE_SETLOCALE
290 setlocale(LC_ALL, "");
291 #endif
293 /* scan command-line options */
294 opterr = 1;
295 while ((opt = getopt_long(argc, argv,
296 #ifndef _WIN32
297 "c:dhn::v",
298 #else
299 "c:dhn::v",
300 #endif
301 long_options, NULL)) != -1) {
302 switch (opt) {
303 case 'c': /* config dir */
304 g_free(opt_config_dir_arg);
305 opt_config_dir_arg = g_strdup(optarg);
306 break;
307 case 'd': /* debug */
308 debug_enabled = TRUE;
309 break;
310 case 'h': /* help */
311 opt_help = TRUE;
312 break;
313 case 'n': /* no autologin */
314 opt_nologin = TRUE;
315 break;
316 case 'v': /* version */
317 opt_version = TRUE;
318 break;
319 case '?': /* show terse help */
320 default:
321 show_usage(argv[0], TRUE);
322 return 0;
323 break;
327 /* show help message */
328 if (opt_help) {
329 show_usage(argv[0], FALSE);
330 return 0;
332 /* show version message */
333 if (opt_version) {
334 /* Translators may want to transliterate the name.
335 It is not to be translated. */
336 printf("%s %s (%s)\n", _("Finch"), DISPLAY_VERSION, REVISION);
337 return 0;
340 /* set a user-specified config directory */
341 if (opt_config_dir_arg != NULL) {
342 purple_util_set_user_dir(opt_config_dir_arg);
343 g_free(opt_config_dir_arg);
347 * We're done piddling around with command line arguments.
348 * Fire up this baby.
351 /* We don't want debug-messages to show up and corrupt the display */
352 purple_debug_set_enabled(debug_enabled);
354 /* If we're using a custom configuration directory, we
355 * do NOT want to migrate, or weird things will happen. */
356 if (opt_config_dir_arg == NULL)
358 if (!purple_core_migrate())
360 char *old = g_strconcat(purple_home_dir(),
361 G_DIR_SEPARATOR_S ".gaim", NULL);
362 char *text = g_strdup_printf(_(
363 "%s encountered errors migrating your settings "
364 "from %s to %s. Please investigate and complete the "
365 "migration by hand. Please report this error at http://developer.pidgin.im"), _("Finch"),
366 old, purple_user_dir());
368 g_free(old);
370 purple_print_utf8_to_console(stderr, text);
371 g_free(text);
373 return 0;
377 purple_core_set_ui_ops(gnt_core_get_ui_ops());
378 purple_eventloop_set_ui_ops(gnt_eventloop_get_ui_ops());
379 purple_idle_set_ui_ops(finch_idle_get_ui_ops());
381 path = g_build_filename(purple_user_dir(), "plugins", NULL);
382 if (!g_stat(path, &st))
383 g_mkdir(path, S_IRUSR | S_IWUSR | S_IXUSR);
384 purple_plugins_add_search_path(path);
385 g_free(path);
387 purple_plugins_add_search_path(LIBDIR);
389 if (!purple_core_init(FINCH_UI))
391 fprintf(stderr,
392 "Initialization of the Purple core failed. Dumping core.\n"
393 "Please report this!\n");
394 abort();
397 /* TODO: Move blist loading into purple_blist_init() */
398 purple_set_blist(purple_blist_new());
399 purple_blist_load();
401 /* TODO: should this be moved into finch_prefs_init() ? */
402 finch_prefs_update_old();
404 /* load plugins we had when we quit */
405 purple_plugins_load_saved("/finch/plugins/loaded");
407 /* TODO: Move pounces loading into purple_pounces_init() */
408 purple_pounces_load();
410 if (opt_nologin)
412 /* Set all accounts to "offline" */
413 PurpleSavedStatus *saved_status;
415 /* If we've used this type+message before, lookup the transient status */
416 saved_status = purple_savedstatus_find_transient_by_type_and_message(
417 PURPLE_STATUS_OFFLINE, NULL);
419 /* If this type+message is unique then create a new transient saved status */
420 if (saved_status == NULL)
421 saved_status = purple_savedstatus_new(NULL, PURPLE_STATUS_OFFLINE);
423 /* Set the status for each account */
424 purple_savedstatus_activate(saved_status);
426 else
428 /* Everything is good to go--sign on already */
429 if (!purple_prefs_get_bool("/purple/savedstatus/startup_current_status"))
430 purple_savedstatus_activate(purple_savedstatus_get_startup());
431 purple_accounts_restore_current_statuses();
434 return 1;
437 static gboolean gnt_start(int *argc, char ***argv)
439 /* Initialize the libpurple stuff */
440 if (!init_libpurple(*argc, *argv))
441 return FALSE;
443 purple_blist_show();
444 return TRUE;
447 int main(int argc, char *argv[])
449 signal(SIGPIPE, SIG_IGN);
451 g_thread_init(NULL);
453 g_set_prgname("Finch");
454 g_set_application_name(_("Finch"));
456 if (gnt_start(&argc, &argv)) {
457 gnt_main();
459 #ifdef STANDALONE
460 purple_core_quit();
461 #endif
464 return 0;