Mercurial > pidgin
annotate src/main.c @ 4515:9b9737a00a96
[gaim-migrate @ 4793]
The "Hide Icon" on a buddy icon right-click shouldn't crash gaim anymore.
committer: Tailor Script <tailor@pidgin.im>
| author | Christian Hammond <chipx86@chipx86.com> |
|---|---|
| date | Tue, 04 Feb 2003 07:49:25 +0000 |
| parents | 7521e29658bc |
| children | 9df99116840a |
| rev | line source |
|---|---|
| 4489 | 1 /* |
| 2 * gaim | |
| 3 * | |
| 4 * Copyright (C) 1998-1999, Mark Spencer <markster@marko.net> | |
| 5 * | |
| 6 * This program is free software; you can redistribute it and/or modify | |
| 7 * it under the terms of the GNU General Public License as published by | |
| 8 * the Free Software Foundation; either version 2 of the License, or | |
| 9 * (at your option) any later version. | |
| 10 * | |
| 11 * This program is distributed in the hope that it will be useful, | |
| 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 14 * GNU General Public License for more details. | |
| 15 * | |
| 16 * You should have received a copy of the GNU General Public License | |
| 17 * along with this program; if not, write to the Free Software | |
| 18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
| 19 * | |
| 20 */ | |
| 21 | |
| 22 #ifdef HAVE_CONFIG_H | |
| 23 #include <config.h> | |
| 24 #endif | |
| 25 #ifdef GAIM_PLUGINS | |
| 26 #ifndef _WIN32 | |
| 27 #include <dlfcn.h> | |
| 28 #endif | |
| 29 #endif /* GAIM_PLUGINS */ | |
| 30 #include <gtk/gtk.h> | |
| 31 #ifndef _WIN32 | |
| 32 #include <gdk/gdkx.h> | |
| 33 #include <unistd.h> | |
| 34 #include <sys/socket.h> | |
| 35 #include <netinet/in.h> | |
| 36 #include <arpa/inet.h> | |
| 37 #include <sys/un.h> | |
| 38 #include <sys/wait.h> | |
| 39 #endif /* !_WIN32 */ | |
| 40 #include <gdk/gdk.h> | |
| 41 #include <sys/types.h> | |
| 42 #include <sys/stat.h> | |
| 43 #include <errno.h> | |
| 44 #include <stdio.h> | |
| 45 #include <string.h> | |
| 46 #include <stdarg.h> | |
| 47 #include <stdlib.h> | |
| 48 #include <ctype.h> | |
| 49 #include "prpl.h" | |
| 50 #include "gaim.h" | |
| 51 #include "gaim-socket.h" | |
| 52 #if HAVE_SIGNAL_H | |
| 53 #include <signal.h> | |
| 54 #endif | |
| 55 #include "locale.h" | |
| 56 #include <getopt.h> | |
| 57 | |
| 58 #ifdef _WIN32 | |
| 59 #include "win32dep.h" | |
| 60 #endif | |
| 61 | |
| 62 static GtkWidget *name; | |
| 63 static GtkWidget *pass; | |
| 64 | |
| 65 GList *log_conversations = NULL; | |
| 66 GList *buddy_pounces = NULL; | |
| 67 GSList *away_messages = NULL; | |
| 68 GSList *message_queue = NULL; | |
| 69 GSList *unread_message_queue = NULL; | |
| 70 GSList *away_time_queue = NULL; | |
| 71 | |
| 72 GtkWidget *mainwindow = NULL; | |
| 73 | |
| 74 int opt_away = 0; | |
| 75 char *opt_away_arg = NULL; | |
| 76 char *opt_rcfile_arg = NULL; | |
| 77 int opt_debug = 0; | |
| 78 #ifdef _WIN32 | |
| 79 int opt_gdebug = 0; | |
| 80 #endif | |
| 81 | |
| 82 #if HAVE_SIGNAL_H | |
| 83 /* | |
| 84 * Lists of signals we wish to catch and those we wish to ignore. | |
| 85 * Each list terminated with -1 | |
| 86 */ | |
| 87 static int catch_sig_list[] = { | |
| 88 SIGSEGV, | |
| 89 SIGHUP, | |
| 90 SIGINT, | |
| 91 SIGTERM, | |
| 92 SIGQUIT, | |
| 93 SIGCHLD, | |
| 94 -1 | |
| 95 }; | |
| 96 | |
| 97 static int ignore_sig_list[] = { | |
| 98 SIGPIPE, | |
| 99 -1 | |
| 100 }; | |
| 101 #endif | |
| 102 | |
| 103 void do_quit() | |
| 104 { | |
| 105 /* captain's log, stardate... */ | |
| 106 system_log(log_quit, NULL, NULL, OPT_LOG_BUDDY_SIGNON | OPT_LOG_MY_SIGNON); | |
| 107 | |
| 108 /* the self destruct sequence has been initiated */ | |
| 109 plugin_event(event_quit); | |
| 110 | |
| 111 /* transmission ends */ | |
| 112 signoff_all(); | |
| 113 | |
| 114 /* record what we have before we blow it away... */ | |
| 115 save_prefs(); | |
| 116 | |
| 117 #ifdef GAIM_PLUGINS | |
| 118 /* jettison cargo */ | |
| 119 remove_all_plugins(); | |
| 120 #endif | |
| 121 | |
| 122 #ifdef USE_PERL | |
| 123 /* yup, perl too */ | |
| 124 perl_end(); | |
| 125 #endif | |
| 126 | |
| 127 #ifdef USE_SM | |
| 128 /* unplug */ | |
| 129 session_end(); | |
| 130 #endif | |
| 131 | |
| 132 /* and end it all... */ | |
| 133 gtk_main_quit(); | |
| 134 } | |
| 135 | |
| 136 static int snd_tmout; | |
| 137 int logins_not_muted = 1; | |
| 138 static gboolean sound_timeout(gpointer data) | |
| 139 { | |
| 140 logins_not_muted = 1; | |
| 141 g_source_remove(snd_tmout); | |
| 142 return FALSE; | |
| 143 } | |
| 144 | |
| 145 /* we need to do this for Oscar because serv_login only starts the login | |
| 146 * process, it doesn't end there. gaim_setup will be called later from | |
| 147 * oscar.c, after the buddy list is made and serv_finish_login is called */ | |
| 148 void gaim_setup(struct gaim_connection *gc) | |
| 149 { | |
| 150 if ((sound_options & OPT_SOUND_LOGIN) && (sound_options & OPT_SOUND_SILENT_SIGNON)) { | |
| 151 logins_not_muted = 0; | |
| 152 snd_tmout = g_timeout_add(10000, sound_timeout, NULL); | |
| 153 } | |
| 154 } | |
| 155 | |
| 156 static gboolean domiddleclick(GtkWidget *w, GdkEventButton *event, gpointer null) | |
| 157 { | |
| 158 if (event->button != 2) | |
| 159 return FALSE; | |
| 160 | |
| 161 auto_login(); | |
| 162 return TRUE; | |
| 163 } | |
| 164 | |
| 165 static void dologin(GtkWidget *widget, GtkWidget *w) | |
| 166 { | |
| 4491 | 167 struct gaim_account *account; |
| 4489 | 168 const char *username = gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(name)->entry)); |
| 169 const char *password = gtk_entry_get_text(GTK_ENTRY(pass)); | |
| 170 | |
| 171 if (!strlen(username)) { | |
| 172 do_error_dialog(_("Please enter your login."), NULL, GAIM_ERROR); | |
| 173 return; | |
| 174 } | |
| 175 | |
| 176 /* if there is more than one user of the same name, then fuck | |
| 177 * them, they just have to use the account editor to sign in | |
| 178 * the second one */ | |
| 179 | |
| 4491 | 180 account = gaim_account_find(username, -1); |
| 181 if (!account) | |
| 182 account = gaim_account_new(username, DEFAULT_PROTO, OPT_ACCT_REM_PASS); | |
| 183 g_snprintf(account->password, sizeof account->password, "%s", password); | |
| 4489 | 184 save_prefs(); |
| 4491 | 185 serv_login(account); |
| 4489 | 186 } |
| 187 | |
| 188 /* <name> is a comma-separated list of names, or NULL | |
| 189 if NULL and there is at least one user defined in .gaimrc, try to login. | |
| 190 if not NULL, parse <name> into separate strings, look up each one in | |
| 191 .gaimrc and, if it's there, try to login. | |
| 192 returns: 0 if successful | |
| 193 -1 if no user was found that had a saved password | |
| 194 */ | |
| 195 static int dologin_named(char *name) | |
| 196 { | |
| 4491 | 197 struct gaim_account *account; |
| 4489 | 198 char **names, **n; |
| 199 int retval = -1; | |
| 200 | |
| 201 if (name !=NULL) { /* list of names given */ | |
| 202 names = g_strsplit(name, ",", 32); | |
| 203 for (n = names; *n != NULL; n++) { | |
| 4491 | 204 account = gaim_account_find(*n, -1); |
| 205 if (account) { /* found a user */ | |
| 206 if (account->options & OPT_ACCT_REM_PASS) { | |
| 4489 | 207 retval = 0; |
| 4491 | 208 serv_login(account); |
| 4489 | 209 } |
| 210 } | |
| 211 } | |
| 212 g_strfreev(names); | |
| 213 } else { /* no name given, use default */ | |
| 4491 | 214 account = (struct gaim_account *)gaim_accounts->data; |
| 215 if (account->options & OPT_ACCT_REM_PASS) { | |
| 4489 | 216 retval = 0; |
| 4491 | 217 serv_login(account); |
| 4489 | 218 } |
| 219 } | |
| 220 | |
| 221 return retval; | |
| 222 } | |
| 223 | |
| 224 | |
| 225 static void doenter(GtkWidget *widget, GtkWidget *w) | |
| 226 { | |
| 227 if (widget == name) { | |
| 228 gtk_entry_set_text(GTK_ENTRY(pass), ""); | |
| 229 gtk_entry_select_region(GTK_ENTRY(GTK_COMBO(name)->entry), 0, 0); | |
| 230 gtk_widget_grab_focus(pass); | |
| 231 } else if (widget == pass) { | |
| 232 dologin(widget, w); | |
| 233 } | |
| 234 } | |
| 235 | |
| 236 | |
| 237 static void combo_changed(GtkWidget *w, GtkWidget *combo) | |
| 238 { | |
| 239 const char *txt = gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(combo)->entry)); | |
| 4491 | 240 struct gaim_account *account; |
| 4489 | 241 |
| 4491 | 242 account = gaim_account_find(txt, -1); |
| 4489 | 243 |
| 4491 | 244 if (account && account->options & OPT_ACCT_REM_PASS) { |
| 245 gtk_entry_set_text(GTK_ENTRY(pass), account->password); | |
| 4489 | 246 } else { |
| 247 gtk_entry_set_text(GTK_ENTRY(pass), ""); | |
| 248 } | |
| 249 } | |
| 250 | |
| 251 | |
| 252 static GList *combo_user_names() | |
| 253 { | |
| 4491 | 254 GSList *accts = gaim_accounts; |
| 4489 | 255 GList *tmp = NULL; |
| 4491 | 256 struct gaim_account *account; |
| 4489 | 257 |
| 4491 | 258 if (!accts) |
| 4489 | 259 return g_list_append(NULL, _("<New User>")); |
| 260 | |
| 4491 | 261 while (accts) { |
| 262 account = (struct gaim_account *)accts->data; | |
| 263 tmp = g_list_append(tmp, account->username); | |
| 264 accts = accts->next; | |
| 4489 | 265 } |
| 266 | |
| 267 return tmp; | |
| 268 } | |
| 269 | |
| 270 static void login_window_closed(GtkWidget *w, GdkEvent *ev, gpointer d) | |
| 271 { | |
| 272 if(docklet_count) { | |
| 273 #if _WIN32 | |
| 274 wgaim_systray_minimize(mainwindow); | |
| 275 #endif | |
| 276 gtk_widget_hide(mainwindow); | |
| 277 } else | |
| 278 do_quit(); | |
| 279 } | |
| 280 | |
| 281 void show_login() | |
| 282 { | |
| 283 GdkPixbuf *icon; | |
| 284 GtkWidget *image; | |
| 285 GtkWidget *vbox; | |
| 286 GtkWidget *button; | |
| 287 GtkWidget *hbox; | |
| 288 GtkWidget *label; | |
| 289 GtkWidget *vbox2; | |
| 290 GList *tmp; | |
| 291 | |
| 292 /* Do we already have a main window opened? If so, bring it back, baby... ribs... yeah */ | |
| 293 if (mainwindow) { | |
| 294 gtk_window_present(GTK_WINDOW(mainwindow)); | |
| 295 return; | |
| 296 } | |
| 297 | |
| 298 mainwindow = gtk_window_new(GTK_WINDOW_TOPLEVEL); | |
| 299 | |
| 300 gtk_window_set_role(GTK_WINDOW(mainwindow), "login"); | |
| 301 gtk_window_set_policy(GTK_WINDOW(mainwindow), FALSE, FALSE, TRUE); | |
| 302 gtk_window_set_title(GTK_WINDOW(mainwindow), _("Gaim - Login")); | |
| 303 gtk_widget_realize(mainwindow); | |
| 304 gdk_window_set_group(mainwindow->window, mainwindow->window); | |
| 305 gtk_container_set_border_width(GTK_CONTAINER(mainwindow), 5); | |
| 306 g_signal_connect(G_OBJECT(mainwindow), "delete_event", | |
| 307 G_CALLBACK(login_window_closed), mainwindow); | |
| 308 | |
| 309 | |
| 310 icon = gaim_pixbuf(NULL, "gaim.png"); | |
| 311 if (icon) { | |
| 312 gtk_window_set_icon(GTK_WINDOW(mainwindow), icon); | |
| 313 gdk_pixbuf_unref(icon); | |
| 314 } | |
| 315 | |
| 316 vbox = gtk_vbox_new(FALSE, 0); | |
| 317 gtk_container_add(GTK_CONTAINER(mainwindow), vbox); | |
| 318 | |
| 319 image = gaim_pixmap(NULL, "logo.png"); | |
| 320 gtk_box_pack_start(GTK_BOX(vbox), image, FALSE, FALSE, 0); | |
| 321 | |
| 322 vbox2 = gtk_vbox_new(FALSE, 0); | |
| 323 gtk_container_set_border_width(GTK_CONTAINER(vbox2), 5); | |
| 324 | |
| 325 label = gtk_label_new(_("Screen Name:")); | |
| 326 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5); | |
| 327 gtk_box_pack_start(GTK_BOX(vbox2), label, FALSE, FALSE, 0); | |
| 328 | |
| 329 name = gtk_combo_new(); | |
| 330 tmp = combo_user_names(); | |
| 331 gtk_combo_set_popdown_strings(GTK_COMBO(name), tmp); | |
| 332 g_list_free(tmp); | |
| 333 g_signal_connect(G_OBJECT(GTK_COMBO(name)->entry), "activate", | |
| 334 G_CALLBACK(doenter), mainwindow); | |
| 335 g_signal_connect(G_OBJECT(GTK_COMBO(name)->entry), "changed", | |
| 336 G_CALLBACK(combo_changed), name); | |
| 337 gtk_box_pack_start(GTK_BOX(vbox2), name, FALSE, TRUE, 0); | |
| 338 gtk_box_pack_start(GTK_BOX(vbox), vbox2, FALSE, TRUE, 0); | |
| 339 | |
| 340 vbox2 = gtk_vbox_new(FALSE, 0); | |
| 341 gtk_container_set_border_width(GTK_CONTAINER(vbox2), 5); | |
| 342 | |
| 343 label = gtk_label_new(_("Password:")); | |
| 344 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5); | |
| 345 gtk_box_pack_start(GTK_BOX(vbox2), label, FALSE, FALSE, 0); | |
| 346 | |
| 347 pass = gtk_entry_new(); | |
| 348 gtk_entry_set_visibility(GTK_ENTRY(pass), FALSE); | |
| 349 g_signal_connect(G_OBJECT(pass), "activate", | |
| 350 G_CALLBACK(doenter), mainwindow); | |
| 351 gtk_box_pack_start(GTK_BOX(vbox2), pass, FALSE, TRUE, 0); | |
| 352 gtk_box_pack_start(GTK_BOX(vbox), vbox2, FALSE, TRUE, 0); | |
| 353 | |
| 354 /* Now for the button box */ | |
| 355 hbox = gtk_hbox_new(TRUE, 0); | |
| 356 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, TRUE, 5); | |
| 357 | |
| 358 /* And now for the buttons */ | |
| 359 button = gaim_pixbuf_button("Accounts", "accounts.png", GAIM_BUTTON_VERTICAL); | |
| 360 gtk_button_set_relief(GTK_BUTTON(button), GTK_RELIEF_NONE); | |
| 361 g_signal_connect(G_OBJECT(button), "clicked", | |
| 362 G_CALLBACK(account_editor), mainwindow); | |
| 363 gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0); | |
| 364 | |
| 365 #ifdef NO_MULTI | |
| 366 gtk_widget_set_sensitive(GTK_WIDGET(button), FALSE); | |
| 367 #endif | |
| 368 | |
| 369 button = gaim_pixbuf_button("Settings", "preferences.png", GAIM_BUTTON_VERTICAL); | |
| 370 gtk_button_set_relief(GTK_BUTTON(button), GTK_RELIEF_NONE); | |
| 371 g_signal_connect(G_OBJECT(button), "clicked", | |
| 372 G_CALLBACK(show_prefs), mainwindow); | |
| 373 gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0); | |
| 374 | |
| 375 button = gaim_pixbuf_button("Sign On", "signon.png", GAIM_BUTTON_VERTICAL); | |
| 376 gtk_button_set_relief(GTK_BUTTON(button), GTK_RELIEF_NONE); | |
| 377 g_signal_connect(G_OBJECT(button), "clicked", | |
| 378 G_CALLBACK(dologin), mainwindow); | |
| 379 g_signal_connect(G_OBJECT(button), "button-press-event", G_CALLBACK(domiddleclick), NULL); | |
| 380 gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0); | |
| 381 | |
| 382 #ifdef _WIN32 | |
| 383 /* Register newly created window with systray module */ | |
| 384 wgaim_created_loginwin(GTK_WIDGET(mainwindow)); | |
| 385 #endif | |
| 386 | |
| 387 /* Now grab the focus that we need */ | |
| 4491 | 388 if (gaim_accounts) { |
| 389 struct gaim_account *account = gaim_accounts->data; | |
| 390 if (account->options & OPT_ACCT_REM_PASS) { | |
| 4489 | 391 combo_changed(NULL, name); |
| 392 gtk_widget_grab_focus(button); | |
| 393 } else { | |
| 394 gtk_widget_grab_focus(pass); | |
| 395 } | |
| 396 } else { | |
| 397 gtk_widget_grab_focus(name); | |
| 398 } | |
| 399 | |
| 400 /* And raise the curtain! */ | |
| 401 gtk_widget_show_all(mainwindow); | |
| 402 | |
| 403 } | |
| 404 | |
| 405 #if HAVE_SIGNAL_H | |
| 406 void sighandler(int sig) | |
| 407 { | |
| 408 switch (sig) { | |
| 409 case SIGHUP: | |
| 410 debug_printf("caught signal %d\n", sig); | |
| 411 signoff_all(NULL, NULL); | |
| 412 break; | |
| 413 case SIGSEGV: | |
| 414 core_quit(); | |
| 415 #ifndef DEBUG | |
| 416 fprintf(stderr, "Gaim has segfaulted and attempted to dump a core file.\n" | |
| 417 "This is a bug in the software and has happened through\n" | |
| 418 "no fault of your own.\n\n" | |
| 419 "It is possible that this bug is already fixed in CVS.\n" | |
| 420 "You can get a tarball of CVS from the Gaim website, at\n" | |
| 421 WEBSITE "gaim-CVS.tar.gz.\n\n" | |
| 422 "If you are already using CVS, or can reproduce the crash\n" | |
| 423 "using the CVS version, please notify the gaim maintainers\n" | |
| 424 "by reporting a bug at\n" | |
| 425 WEBSITE "bug.php\n\n" | |
| 426 "Please make sure to specify what you were doing at the time,\n" | |
| 427 "and post the backtrace from the core file. If you do not know\n" | |
| 428 "how to get the backtrace, please get instructions at\n" | |
| 429 WEBSITE "gdb.php. If you need further\n" | |
| 430 "assistance, please IM either RobFlynn or SeanEgn and\n" | |
| 431 "they can help you.\n"); | |
| 432 #else | |
| 433 fprintf(stderr, "Oh no! Segmentation fault!\n"); | |
| 434 g_on_error_query (g_get_prgname()); | |
| 435 exit(1); | |
| 436 #endif | |
| 437 abort(); | |
| 438 break; | |
| 439 case SIGCHLD: | |
| 440 clean_pid(); | |
| 441 #if HAVE_SIGNAL_H | |
| 442 signal(SIGCHLD, sighandler); /* restore signal catching on this one! */ | |
| 443 #endif | |
| 444 break; | |
| 445 default: | |
| 446 debug_printf("caught signal %d\n", sig); | |
| 447 signoff_all(NULL, NULL); | |
| 448 #ifdef GAIM_PLUGINS | |
| 449 remove_all_plugins(); | |
| 450 #endif | |
| 451 if (gtk_main_level()) | |
| 452 gtk_main_quit(); | |
| 453 core_quit(); | |
| 454 exit(0); | |
| 455 } | |
| 456 } | |
| 457 #endif | |
| 458 | |
| 459 #ifndef _WIN32 | |
| 460 static gboolean socket_readable(GIOChannel *source, GIOCondition cond, gpointer ud) | |
| 461 { | |
| 462 guchar type; | |
| 463 guchar subtype; | |
| 464 guint32 len; | |
| 465 guchar *data; | |
| 466 guint32 x; | |
| 467 | |
| 468 debug_printf("Core says: "); | |
| 469 g_io_channel_read(source, &type, sizeof(type), &x); | |
| 470 if (x == 0) { | |
| 471 debug_printf("CORE IS GONE!\n"); | |
| 472 g_io_channel_close(source); | |
| 473 return FALSE; | |
| 474 } | |
| 475 debug_printf("%d ", type); | |
| 476 g_io_channel_read(source, &subtype, sizeof(subtype), &x); | |
| 477 if (x == 0) { | |
| 478 debug_printf("CORE IS GONE!\n"); | |
| 479 g_io_channel_close(source); | |
| 480 return FALSE; | |
| 481 } | |
| 482 debug_printf("%d ", subtype); | |
| 483 g_io_channel_read(source, (guchar *)&len, sizeof(len), &x); | |
| 484 if (x == 0) { | |
| 485 debug_printf("CORE IS GONE!\n"); | |
| 486 g_io_channel_close(source); | |
| 487 return FALSE; | |
| 488 } | |
| 489 debug_printf("(%d bytes)\n", len); | |
| 490 | |
| 491 data = g_malloc(len); | |
| 492 g_io_channel_read(source, data, len, &x); | |
| 493 if (x != len) { | |
| 494 debug_printf("CORE IS GONE! (read %d/%d bytes)\n", x, len); | |
| 495 g_free(data); | |
| 496 g_io_channel_close(source); | |
| 497 return FALSE; | |
| 498 } | |
| 499 | |
| 500 g_free(data); | |
| 501 return TRUE; | |
| 502 } | |
| 503 #endif /* _WIN32 */ | |
| 504 | |
| 505 static int ui_main() | |
| 506 { | |
| 507 #ifndef _WIN32 | |
| 508 GIOChannel *channel; | |
| 509 int UI_fd; | |
| 510 char name[256]; | |
| 511 GList *icons = NULL; | |
| 512 GdkPixbuf *icon = NULL; | |
| 513 char *icon_path; | |
| 514 #endif | |
| 515 if (current_smiley_theme == NULL) { | |
| 516 smiley_theme_probe(); | |
| 517 if (smiley_themes) { | |
| 518 struct smiley_theme *smile = smiley_themes->data; | |
| 519 load_smiley_theme(smile->path, TRUE); | |
| 520 } | |
| 521 } | |
| 522 | |
|
4514
7521e29658bc
[gaim-migrate @ 4792]
Christian Hammond <chipx86@chipx86.com>
parents:
4491
diff
changeset
|
523 /* Set the UI operation structures. */ |
| 4489 | 524 gaim_set_win_ui_ops(gaim_get_gtk_window_ui_ops()); |
|
4514
7521e29658bc
[gaim-migrate @ 4792]
Christian Hammond <chipx86@chipx86.com>
parents:
4491
diff
changeset
|
525 gaim_set_xfer_ui_ops(gaim_get_gtk_xfer_ui_ops()); |
| 4489 | 526 |
| 527 setup_stock(); | |
| 528 | |
| 529 #ifndef _WIN32 | |
| 530 /* use the nice PNG icon for all the windows */ | |
| 531 icon_path = g_build_filename(DATADIR, "pixmaps", "gaim.png", NULL); | |
| 532 icon = gdk_pixbuf_new_from_file(icon_path, NULL); | |
| 533 g_free(icon_path); | |
| 534 if (icon) { | |
| 535 icons = g_list_append(icons,icon); | |
| 536 gtk_window_set_default_icon_list(icons); | |
| 537 g_object_unref(G_OBJECT(icon)); | |
| 538 } else { | |
| 539 debug_printf("Failed to load icon from %s" G_DIR_SEPARATOR_S "pixmaps" G_DIR_SEPARATOR_S "gaim.png\n",DATADIR); | |
| 540 } | |
| 541 | |
| 542 g_snprintf(name, sizeof(name), "%s" G_DIR_SEPARATOR_S "gaim_%s.%d", g_get_tmp_dir(), g_get_user_name(), gaim_session); | |
| 543 UI_fd = gaim_connect_to_session(0); | |
| 544 if (UI_fd < 0) | |
| 545 return 1; | |
| 546 | |
| 547 channel = g_io_channel_unix_new(UI_fd); | |
| 548 g_io_add_watch(channel, G_IO_IN | G_IO_HUP | G_IO_ERR, socket_readable, NULL); | |
| 549 #endif | |
| 550 return 0; | |
| 551 } | |
| 552 | |
| 553 static void set_first_user(char *name) | |
| 554 { | |
| 4491 | 555 struct gaim_account *account; |
| 4489 | 556 |
| 4491 | 557 account = gaim_account_find(name, -1); |
| 4489 | 558 |
| 4491 | 559 if (!account) { /* new user */ |
| 560 account = g_new0(struct gaim_account, 1); | |
| 561 g_snprintf(account->username, sizeof(account->username), "%s", name); | |
| 562 account->protocol = DEFAULT_PROTO; | |
| 563 gaim_accounts = g_slist_prepend(gaim_accounts, account); | |
| 4489 | 564 } else { /* user already exists */ |
| 4491 | 565 gaim_accounts = g_slist_remove(gaim_accounts, account); |
| 566 gaim_accounts = g_slist_prepend(gaim_accounts, account); | |
| 4489 | 567 } |
| 568 save_prefs(); | |
| 569 } | |
| 570 | |
| 571 #ifdef _WIN32 | |
| 572 /* WIN32 print and log handlers */ | |
| 573 | |
| 574 static void gaim_dummy_print( const gchar* string ) { | |
| 575 return; | |
| 576 } | |
| 577 | |
| 578 static void gaim_dummy_log_handler (const gchar *domain, | |
| 579 GLogLevelFlags flags, | |
| 580 const gchar *msg, | |
| 581 gpointer user_data) { | |
| 582 return; | |
| 583 } | |
| 584 | |
| 585 static void gaim_log_handler (const gchar *domain, | |
| 586 GLogLevelFlags flags, | |
| 587 const gchar *msg, | |
| 588 gpointer user_data) { | |
| 589 debug_printf("%s - %s\n", domain, msg); | |
| 590 g_log_default_handler(domain, flags, msg, user_data); | |
| 591 } | |
| 592 #endif /* _WIN32 */ | |
| 593 | |
| 594 /* FUCKING GET ME A TOWEL! */ | |
| 595 #ifdef _WIN32 | |
| 596 int gaim_main(int argc, char *argv[]) | |
| 597 #else | |
| 598 int main(int argc, char *argv[]) | |
| 599 #endif | |
| 600 { | |
| 601 int opt_acct = 0, opt_help = 0, opt_version = 0, opt_login = 0, opt_nologin = 0, dologin_ret = -1; | |
| 602 char *opt_user_arg = NULL, *opt_login_arg = NULL; | |
| 603 char *opt_session_arg = NULL; | |
| 604 #if HAVE_SIGNAL_H | |
| 605 int sig_indx; /* for setting up signal catching */ | |
| 606 sigset_t sigset; | |
| 607 void (*prev_sig_disp)(); | |
| 608 #endif | |
| 609 int opt, opt_user = 0; | |
| 610 int i; | |
| 611 | |
| 612 struct option long_options[] = { | |
| 613 {"acct", no_argument, NULL, 'a'}, | |
| 614 /*{"away", optional_argument, NULL, 'w'}, */ | |
| 615 {"help", no_argument, NULL, 'h'}, | |
| 616 /*{"login", optional_argument, NULL, 'l'}, */ | |
| 617 {"loginwin", no_argument, NULL, 'n'}, | |
| 618 {"user", required_argument, NULL, 'u'}, | |
| 619 {"file", required_argument, NULL, 'f'}, | |
| 620 {"debug", no_argument, NULL, 'd'}, | |
| 621 {"version", no_argument, NULL, 'v'}, | |
| 622 {"session", required_argument, NULL, 's'}, | |
| 623 {0, 0, 0, 0} | |
| 624 }; | |
| 625 | |
| 626 #ifdef DEBUG | |
| 627 opt_debug = 1; | |
| 628 #endif | |
| 629 | |
| 630 #ifdef ENABLE_NLS | |
| 631 bindtextdomain(PACKAGE, LOCALEDIR); | |
| 632 bind_textdomain_codeset(PACKAGE, "UTF-8"); | |
| 633 textdomain(PACKAGE); | |
| 634 #endif | |
| 635 | |
| 636 #if HAVE_SIGNAL_H | |
| 637 /* Let's not violate any PLA's!!!! */ | |
| 638 /* jseymour: whatever the fsck that means */ | |
| 639 /* Robot101: for some reason things like gdm like to block * | |
| 640 * useful signals like SIGCHLD, so we unblock all the ones we * | |
| 641 * declare a handler for. thanks JSeymour and Vann. */ | |
| 642 if (sigemptyset(&sigset)) { | |
| 643 char errmsg[BUFSIZ]; | |
| 644 sprintf(errmsg, "Warning: couldn't initialise empty signal set"); | |
| 645 perror(errmsg); | |
| 646 } | |
| 647 for(sig_indx = 0; catch_sig_list[sig_indx] != -1; ++sig_indx) { | |
| 648 if((prev_sig_disp = signal(catch_sig_list[sig_indx], sighandler)) == SIG_ERR) { | |
| 649 char errmsg[BUFSIZ]; | |
| 650 sprintf(errmsg, "Warning: couldn't set signal %d for catching", | |
| 651 catch_sig_list[sig_indx]); | |
| 652 perror(errmsg); | |
| 653 } | |
| 654 if(sigaddset(&sigset, catch_sig_list[sig_indx])) { | |
| 655 char errmsg[BUFSIZ]; | |
| 656 sprintf(errmsg, "Warning: couldn't include signal %d for unblocking", | |
| 657 catch_sig_list[sig_indx]); | |
| 658 perror(errmsg); | |
| 659 } | |
| 660 } | |
| 661 for(sig_indx = 0; ignore_sig_list[sig_indx] != -1; ++sig_indx) { | |
| 662 if((prev_sig_disp = signal(ignore_sig_list[sig_indx], SIG_IGN)) == SIG_ERR) { | |
| 663 char errmsg[BUFSIZ]; | |
| 664 sprintf(errmsg, "Warning: couldn't set signal %d to ignore", | |
| 665 ignore_sig_list[sig_indx]); | |
| 666 perror(errmsg); | |
| 667 } | |
| 668 } | |
| 669 | |
| 670 if (sigprocmask(SIG_UNBLOCK, &sigset, NULL)) { | |
| 671 char errmsg[BUFSIZ]; | |
| 672 sprintf(errmsg, "Warning: couldn't unblock signals"); | |
| 673 perror(errmsg); | |
| 674 } | |
| 675 #endif | |
| 676 | |
| 677 for (i = 0; i < argc; i++) { | |
| 678 /* --login option */ | |
| 679 if (strstr(argv[i], "--l") == argv[i]) { | |
| 680 char *equals; | |
| 681 opt_login = 1; | |
| 682 if ((equals = strchr(argv[i], '=')) != NULL) { | |
| 683 /* --login=NAME */ | |
| 684 opt_login_arg = g_strdup(equals + 1); | |
| 685 if (strlen(opt_login_arg) == 0) { | |
| 686 g_free(opt_login_arg); | |
| 687 opt_login_arg = NULL; | |
| 688 } | |
| 689 } else if (i + 1 < argc && argv[i + 1][0] != '-') { | |
| 690 /* --login NAME */ | |
| 691 opt_login_arg = g_strdup(argv[i + 1]); | |
| 692 strcpy(argv[i + 1], " "); | |
| 693 } | |
| 694 strcpy(argv[i], " "); | |
| 695 } | |
| 696 /* -l option */ | |
| 697 else if (strstr(argv[i], "-l") == argv[i]) { | |
| 698 opt_login = 1; | |
| 699 if (strlen(argv[i]) > 2) { | |
| 700 /* -lNAME */ | |
| 701 opt_login_arg = g_strdup(argv[i] + 2); | |
| 702 } else if (i + 1 < argc && argv[i + 1][0] != '-') { | |
| 703 /* -l NAME */ | |
| 704 opt_login_arg = g_strdup(argv[i + 1]); | |
| 705 strcpy(argv[i + 1], " "); | |
| 706 } | |
| 707 strcpy(argv[i], " "); | |
| 708 } | |
| 709 /* --away option */ | |
| 710 else if (strstr(argv[i], "--aw") == argv[i]) { | |
| 711 char *equals; | |
| 712 opt_away = 1; | |
| 713 if ((equals = strchr(argv[i], '=')) != NULL) { | |
| 714 /* --away=MESG */ | |
| 715 opt_away_arg = g_strdup(equals + 1); | |
| 716 if (strlen(opt_away_arg) == 0) { | |
| 717 g_free(opt_away_arg); | |
| 718 opt_away_arg = NULL; | |
| 719 } | |
| 720 } else if (i + 1 < argc && argv[i + 1][0] != '-') { | |
| 721 /* --away MESG */ | |
| 722 opt_away_arg = g_strdup(argv[i + 1]); | |
| 723 strcpy(argv[i + 1], " "); | |
| 724 } | |
| 725 strcpy(argv[i], " "); | |
| 726 } | |
| 727 /* -w option */ | |
| 728 else if (strstr(argv[i], "-w") == argv[i]) { | |
| 729 opt_away = 1; | |
| 730 if (strlen(argv[i]) > 2) { | |
| 731 /* -wMESG */ | |
| 732 opt_away_arg = g_strdup(argv[i] + 2); | |
| 733 } else if (i + 1 < argc && argv[i + 1][0] != '-') { | |
| 734 /* -w MESG */ | |
| 735 opt_away_arg = g_strdup(argv[i + 1]); | |
| 736 strcpy(argv[i + 1], " "); | |
| 737 } | |
| 738 strcpy(argv[i], " "); | |
| 739 } | |
| 740 } | |
| 741 /* | |
| 742 if (opt_login) { | |
| 743 printf ("--login given with arg %s\n", | |
| 744 opt_login_arg ? opt_login_arg : "NULL"); | |
| 745 exit(0); | |
| 746 } | |
| 747 */ | |
| 748 | |
| 749 gtk_set_locale(); | |
| 750 gtk_init(&argc, &argv); | |
| 751 | |
| 752 /* scan command-line options */ | |
| 753 opterr = 1; | |
| 754 while ((opt = getopt_long(argc, argv, | |
| 755 #ifndef _WIN32 | |
| 756 "adhu:f:vns:", | |
| 757 #else | |
| 758 "adghu:f:vn", | |
| 759 #endif | |
| 760 long_options, NULL)) != -1) { | |
| 761 switch (opt) { | |
| 762 case 'u': /* set user */ | |
| 763 opt_user = 1; | |
| 764 opt_user_arg = g_strdup(optarg); | |
| 765 break; | |
| 766 case 'a': /* account editor */ | |
| 767 opt_acct = 1; | |
| 768 break; | |
| 769 case 'd': /* debug */ | |
| 770 opt_debug = 1; | |
| 771 break; | |
| 772 case 'f': | |
| 773 opt_rcfile_arg = g_strdup(optarg); | |
| 774 break; | |
| 775 case 's': /* use existing session ID */ | |
| 776 opt_session_arg = g_strdup(optarg); | |
| 777 break; | |
| 778 case 'v': /* version */ | |
| 779 opt_version = 1; | |
| 780 break; | |
| 781 case 'h': /* help */ | |
| 782 opt_help = 1; | |
| 783 break; | |
| 784 case 'n': /* don't autologin */ | |
| 785 opt_nologin = 1; | |
| 786 break; | |
| 787 #ifdef _WIN32 | |
| 788 case 'g': /* debug GTK and GLIB */ | |
| 789 opt_gdebug = 1; | |
| 790 break; | |
| 791 #endif | |
| 792 case '?': | |
| 793 default: | |
| 794 show_usage(1, argv[0]); | |
| 795 return 0; | |
| 796 break; | |
| 797 } | |
| 798 } | |
| 799 | |
| 800 #ifdef _WIN32 | |
| 801 /* We don't want a console window.. */ | |
| 802 /* | |
| 803 * Any calls to the glib logging functions, result in a call to AllocConsole(). | |
| 804 * ME and 98 will in such cases produce a console window (2000 not), despite | |
| 805 * being built as a windows app rather than a console app. So we should either | |
| 806 * ignore messages by setting dummy log handlers, or redirect messages. | |
| 807 * This requires setting handlers for all domains (any lib which uses g_logging). | |
| 808 */ | |
| 809 | |
| 810 g_log_set_handler (NULL, G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION, | |
| 811 (opt_gdebug ? gaim_log_handler : gaim_dummy_log_handler), | |
| 812 NULL); | |
| 813 g_log_set_handler ("Gdk", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION, | |
| 814 (opt_gdebug ? gaim_log_handler : gaim_dummy_log_handler), | |
| 815 NULL); | |
| 816 g_log_set_handler ("Gtk", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION, | |
| 817 (opt_gdebug ? gaim_log_handler : gaim_dummy_log_handler), | |
| 818 NULL); | |
| 819 g_log_set_handler ("GLib", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION, | |
| 820 (opt_gdebug ? gaim_log_handler : gaim_dummy_log_handler), | |
| 821 NULL); | |
| 822 g_log_set_handler ("GModule", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION, | |
| 823 (opt_gdebug ? gaim_log_handler : gaim_dummy_log_handler), | |
| 824 NULL); | |
| 825 g_log_set_handler ("GLib-GObject", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION, | |
| 826 (opt_gdebug ? gaim_log_handler : gaim_dummy_log_handler), | |
| 827 NULL); | |
| 828 g_log_set_handler ("GThread", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION, | |
| 829 (opt_gdebug ? gaim_log_handler : gaim_dummy_log_handler), | |
| 830 NULL); | |
| 831 | |
| 832 /* g_print also makes a call to AllocConsole(), therefore a handler needs to be | |
| 833 set here aswell */ | |
| 834 if(!opt_debug) | |
| 835 g_set_print_handler( gaim_dummy_print ); | |
| 836 | |
| 837 #endif | |
| 838 | |
| 839 /* show help message */ | |
| 840 if (opt_help) { | |
| 841 show_usage(0, argv[0]); | |
| 842 return 0; | |
| 843 } | |
| 844 /* show version message */ | |
| 845 if (opt_version) { | |
| 846 printf("Gaim %s\n",VERSION); | |
| 847 return 0; | |
| 848 } | |
| 849 | |
| 850 #if GAIM_PLUGINS || USE_PERL | |
| 851 gaim_probe_plugins(); | |
| 852 #endif | |
| 853 | |
| 854 #ifdef _WIN32 | |
| 855 /* Various win32 initializations */ | |
| 856 wgaim_init(); | |
| 857 #endif | |
| 858 | |
| 859 load_prefs(); | |
| 860 core_main(); | |
| 861 ui_main(); | |
| 862 | |
| 863 #ifdef USE_SM | |
| 864 session_init(argv[0], opt_session_arg); | |
| 865 #endif | |
| 866 if (opt_session_arg != NULL) { | |
| 867 g_free(opt_session_arg); | |
| 868 opt_session_arg = NULL; | |
| 869 }; | |
| 870 | |
| 871 /* set the default username */ | |
| 872 if (opt_user_arg != NULL) { | |
| 873 set_first_user(opt_user_arg); | |
| 874 g_free(opt_user_arg); | |
| 875 opt_user_arg = NULL; | |
| 876 } | |
| 877 | |
| 878 if (misc_options & OPT_MISC_DEBUG) | |
| 879 show_debug(); | |
| 880 | |
| 881 static_proto_init(); | |
| 882 | |
| 883 /* deal with --login */ | |
| 884 if (opt_login) { | |
| 885 dologin_ret = dologin_named(opt_login_arg); | |
| 886 if (opt_login_arg != NULL) { | |
| 887 g_free(opt_login_arg); | |
| 888 opt_login_arg = NULL; | |
| 889 } | |
| 890 } | |
| 891 | |
| 892 if (!opt_acct && !opt_nologin && gaim_session == 0) | |
| 893 auto_login(); | |
| 894 | |
| 895 if (opt_acct) { | |
| 896 account_editor(NULL, NULL); | |
| 897 } else if ((dologin_ret == -1) && !connections) | |
| 898 show_login(); | |
| 899 | |
| 900 gtk_main(); | |
| 901 core_quit(); | |
| 902 /* don't need ui_quit here because ui doesn't create anything */ | |
| 903 #ifdef _WIN32 | |
| 904 wgaim_cleanup(); | |
| 905 #endif | |
| 906 return 0; | |
| 907 | |
| 908 } |
