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