root / src / main.c @ 92
History | View | Annotate | Download (20.2 kB)
| 1 | /*
|
|---|---|
| 2 | * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client |
| 3 | * Copyright (C) 1999-2005 Hiroyuki Yamamoto |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 18 | */ |
| 19 | |
| 20 | #ifdef HAVE_CONFIG_H
|
| 21 | # include "config.h" |
| 22 | #endif
|
| 23 | |
| 24 | #include "defs.h" |
| 25 | |
| 26 | #include <glib.h> |
| 27 | #include <glib/gi18n.h> |
| 28 | #include <gtk/gtkmain.h> |
| 29 | #include <gtk/gtkrc.h> |
| 30 | |
| 31 | #include <stdio.h> |
| 32 | #include <stdlib.h> |
| 33 | #include <string.h> |
| 34 | #include <ctype.h> |
| 35 | #include <unistd.h> |
| 36 | #include <time.h> |
| 37 | #include <sys/stat.h> |
| 38 | #include <sys/types.h> |
| 39 | #include <signal.h> |
| 40 | #include <dirent.h> |
| 41 | |
| 42 | #if HAVE_LOCALE_H
|
| 43 | # include <locale.h> |
| 44 | #endif
|
| 45 | |
| 46 | #if USE_GPGME
|
| 47 | # include <gpgme.h> |
| 48 | #endif
|
| 49 | |
| 50 | #include "main.h" |
| 51 | #include "mainwindow.h" |
| 52 | #include "folderview.h" |
| 53 | #include "summaryview.h" |
| 54 | #include "prefs_common.h" |
| 55 | #include "prefs_account.h" |
| 56 | #include "prefs_actions.h" |
| 57 | #include "prefs_display_header.h" |
| 58 | #include "account.h" |
| 59 | #include "procmsg.h" |
| 60 | #include "filter.h" |
| 61 | #include "inc.h" |
| 62 | #include "import.h" |
| 63 | #include "manage_window.h" |
| 64 | #include "alertpanel.h" |
| 65 | #include "statusbar.h" |
| 66 | #include "addressbook.h" |
| 67 | #include "addrindex.h" |
| 68 | #include "compose.h" |
| 69 | #include "folder.h" |
| 70 | #include "setup.h" |
| 71 | #include "utils.h" |
| 72 | #include "gtkutils.h" |
| 73 | #include "socket.h" |
| 74 | |
| 75 | #if USE_GPGME
|
| 76 | # include "rfc2015.h" |
| 77 | #endif
|
| 78 | #if USE_SSL
|
| 79 | # include "ssl.h" |
| 80 | #endif
|
| 81 | |
| 82 | #include "version.h" |
| 83 | |
| 84 | gchar *prog_version; |
| 85 | gchar *startup_dir; |
| 86 | gboolean debug_mode = FALSE; |
| 87 | |
| 88 | static gint lock_socket = -1; |
| 89 | static gint lock_socket_tag = 0; |
| 90 | |
| 91 | static struct RemoteCmd { |
| 92 | gboolean receive; |
| 93 | gboolean receive_all; |
| 94 | gboolean compose; |
| 95 | const gchar *compose_mailto;
|
| 96 | GPtrArray *attach_files; |
| 97 | gboolean status; |
| 98 | gboolean status_full; |
| 99 | GPtrArray *status_folders; |
| 100 | GPtrArray *status_full_folders; |
| 101 | gboolean send; |
| 102 | } cmd; |
| 103 | |
| 104 | static void parse_cmd_opt(int argc, char *argv[]); |
| 105 | |
| 106 | #if 0
|
| 107 | #if USE_GPGME |
| 108 | static void idle_function_for_gpgme(void); |
| 109 | #endif /* USE_GPGME */ |
| 110 | #endif /* 0 */ |
| 111 | |
| 112 | static gint prohibit_duplicate_launch (void); |
| 113 | static gint lock_socket_remove (void); |
| 114 | static void lock_socket_input_cb (gpointer data, |
| 115 | gint source, |
| 116 | GdkInputCondition condition); |
| 117 | static gchar *get_socket_name (void); |
| 118 | |
| 119 | static void migrate_old_config (void); |
| 120 | |
| 121 | static void open_compose_new (const gchar *address, |
| 122 | GPtrArray *attach_files); |
| 123 | |
| 124 | static void send_queue (void); |
| 125 | |
| 126 | #if 0
|
| 127 | /* for gettext */ |
| 128 | _("File `%s' already exists.\n"
|
| 129 | "Can't create folder.") |
| 130 | #endif |
| 131 | |
| 132 | #define MAKE_DIR_IF_NOT_EXIST(dir) \
|
| 133 | { \
|
| 134 | if (!is_dir_exist(dir)) { \
|
| 135 | if (is_file_exist(dir)) { \
|
| 136 | alertpanel_warning \ |
| 137 | (_("File `%s' already exists.\n" \
|
| 138 | "Can't create folder."), \
|
| 139 | dir); \ |
| 140 | return 1; \ |
| 141 | } \ |
| 142 | if (make_dir(dir) < 0) \ |
| 143 | return 1; \ |
| 144 | } \ |
| 145 | } |
| 146 | |
| 147 | int main(int argc, char *argv[]) |
| 148 | {
|
| 149 | gchar *userrc; |
| 150 | MainWindow *mainwin; |
| 151 | FolderView *folderview; |
| 152 | |
| 153 | setlocale(LC_ALL, "");
|
| 154 | bindtextdomain(PACKAGE, LOCALEDIR); |
| 155 | bind_textdomain_codeset(PACKAGE, CS_UTF_8); |
| 156 | textdomain(PACKAGE); |
| 157 | |
| 158 | prog_version = PROG_VERSION; |
| 159 | startup_dir = g_get_current_dir(); |
| 160 | |
| 161 | parse_cmd_opt(argc, argv); |
| 162 | |
| 163 | /* check and create unix domain socket for remote operation */
|
| 164 | lock_socket = prohibit_duplicate_launch(); |
| 165 | if (lock_socket < 0) return 0; |
| 166 | |
| 167 | if (cmd.status || cmd.status_full) {
|
| 168 | puts("0 Sylpheed not running.");
|
| 169 | lock_socket_remove(); |
| 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | gtk_set_locale(); |
| 174 | gtk_init(&argc, &argv); |
| 175 | |
| 176 | gdk_rgb_init(); |
| 177 | gtk_widget_set_default_colormap(gdk_rgb_get_cmap()); |
| 178 | gtk_widget_set_default_visual(gdk_rgb_get_visual()); |
| 179 | |
| 180 | #if USE_THREADS || USE_LDAP
|
| 181 | g_thread_init(NULL);
|
| 182 | if (!g_thread_supported())
|
| 183 | g_error(_("g_thread is not supported by glib.\n"));
|
| 184 | #endif
|
| 185 | |
| 186 | #if USE_SSL
|
| 187 | ssl_init(); |
| 188 | #endif
|
| 189 | |
| 190 | srandom((gint)time(NULL));
|
| 191 | |
| 192 | /* parse gtkrc files */
|
| 193 | userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtkrc-2.0",
|
| 194 | NULL);
|
| 195 | gtk_rc_parse(userrc); |
| 196 | g_free(userrc); |
| 197 | userrc = g_strconcat(get_home_dir(), G_DIR_SEPARATOR_S, ".gtk",
|
| 198 | G_DIR_SEPARATOR_S, "gtkrc-2.0", NULL); |
| 199 | gtk_rc_parse(userrc); |
| 200 | g_free(userrc); |
| 201 | userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, "gtkrc", NULL); |
| 202 | gtk_rc_parse(userrc); |
| 203 | g_free(userrc); |
| 204 | |
| 205 | userrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
|
| 206 | gtk_accel_map_load(userrc); |
| 207 | g_free(userrc); |
| 208 | |
| 209 | CHDIR_RETURN_VAL_IF_FAIL(get_home_dir(), 1);
|
| 210 | |
| 211 | /* backup if old rc file exists */
|
| 212 | if (is_file_exist(RC_DIR)) {
|
| 213 | if (rename(RC_DIR, RC_DIR ".bak") < 0) |
| 214 | FILE_OP_ERROR(RC_DIR, "rename");
|
| 215 | } |
| 216 | |
| 217 | /* migration from ~/.sylpheed to ~/.sylpheed-2.0 */
|
| 218 | if (!is_dir_exist(RC_DIR)) {
|
| 219 | if (make_dir(RC_DIR) < 0) |
| 220 | return 1; |
| 221 | if (is_dir_exist(OLD_RC_DIR))
|
| 222 | migrate_old_config(); |
| 223 | } |
| 224 | |
| 225 | MAKE_DIR_IF_NOT_EXIST(get_imap_cache_dir()); |
| 226 | MAKE_DIR_IF_NOT_EXIST(get_news_cache_dir()); |
| 227 | MAKE_DIR_IF_NOT_EXIST(get_mime_tmp_dir()); |
| 228 | MAKE_DIR_IF_NOT_EXIST(get_tmp_dir()); |
| 229 | MAKE_DIR_IF_NOT_EXIST(RC_DIR G_DIR_SEPARATOR_S UIDL_DIR); |
| 230 | |
| 231 | if (is_file_exist(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log")) { |
| 232 | if (rename(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log", |
| 233 | RC_DIR G_DIR_SEPARATOR_S "sylpheed.log.bak") < 0) |
| 234 | FILE_OP_ERROR("sylpheed.log", "rename"); |
| 235 | } |
| 236 | set_log_file(RC_DIR G_DIR_SEPARATOR_S "sylpheed.log");
|
| 237 | |
| 238 | prefs_common_read_config(); |
| 239 | |
| 240 | #if USE_GPGME
|
| 241 | if (gpgme_check_version("0.4.5")) { /* Also does some gpgme init */ |
| 242 | gpgme_engine_info_t engineInfo; |
| 243 | rfc2015_disable_all(); |
| 244 | |
| 245 | gpgme_set_locale(NULL, LC_CTYPE, setlocale(LC_CTYPE, NULL)); |
| 246 | gpgme_set_locale(NULL, LC_MESSAGES, setlocale(LC_MESSAGES, NULL)); |
| 247 | |
| 248 | if (!gpgme_get_engine_info(&engineInfo)) {
|
| 249 | while (engineInfo) {
|
| 250 | debug_print("GpgME Protocol: %s\n Version: %s\n",
|
| 251 | gpgme_get_protocol_name(engineInfo->protocol), |
| 252 | engineInfo->version); |
| 253 | engineInfo = engineInfo->next; |
| 254 | } |
| 255 | } |
| 256 | } else {
|
| 257 | if (prefs_common.gpg_warning) {
|
| 258 | AlertValue val; |
| 259 | |
| 260 | val = alertpanel_message_with_disable |
| 261 | (_("Warning"),
|
| 262 | _("GnuPG is not installed properly, or its version is too old.\n"
|
| 263 | "OpenPGP support disabled."),
|
| 264 | ALERT_WARNING); |
| 265 | if (val & G_ALERTDISABLE)
|
| 266 | prefs_common.gpg_warning = FALSE; |
| 267 | } |
| 268 | } |
| 269 | /* FIXME: This function went away. We can either block until gpgme
|
| 270 | * operations finish (currently implemented) or register callbacks |
| 271 | * with the gtk main loop via the gpgme io callback interface instead. |
| 272 | * |
| 273 | * gpgme_register_idle(idle_function_for_gpgme); |
| 274 | */ |
| 275 | #endif
|
| 276 | |
| 277 | sock_set_io_timeout(prefs_common.io_timeout_secs); |
| 278 | |
| 279 | prefs_filter_read_config(); |
| 280 | prefs_actions_read_config(); |
| 281 | prefs_display_header_read_config(); |
| 282 | |
| 283 | gtkut_widget_init(); |
| 284 | |
| 285 | mainwin = main_window_create |
| 286 | (prefs_common.sep_folder | prefs_common.sep_msg << 1);
|
| 287 | folderview = mainwin->folderview; |
| 288 | |
| 289 | /* register the callback of unix domain socket input */
|
| 290 | lock_socket_tag = gdk_input_add(lock_socket, |
| 291 | GDK_INPUT_READ | GDK_INPUT_EXCEPTION, |
| 292 | lock_socket_input_cb, |
| 293 | mainwin); |
| 294 | |
| 295 | account_read_config_all(); |
| 296 | |
| 297 | if (folder_read_list() < 0) { |
| 298 | setup(mainwin); |
| 299 | folder_write_list(); |
| 300 | } |
| 301 | if (!account_get_list()) {
|
| 302 | account_edit_open(); |
| 303 | account_add(); |
| 304 | } |
| 305 | |
| 306 | account_set_missing_folder(); |
| 307 | folder_set_missing_folders(); |
| 308 | folderview_set(folderview); |
| 309 | |
| 310 | addressbook_read_file(); |
| 311 | |
| 312 | inc_autocheck_timer_init(mainwin); |
| 313 | |
| 314 | /* ignore SIGPIPE signal for preventing sudden death of program */
|
| 315 | signal(SIGPIPE, SIG_IGN); |
| 316 | |
| 317 | if (cmd.receive_all)
|
| 318 | inc_all_account_mail(mainwin, FALSE); |
| 319 | else if (prefs_common.chk_on_startup) |
| 320 | inc_all_account_mail(mainwin, TRUE); |
| 321 | else if (cmd.receive) |
| 322 | inc_mail(mainwin); |
| 323 | else
|
| 324 | gtk_widget_grab_focus(folderview->ctree); |
| 325 | |
| 326 | if (cmd.compose)
|
| 327 | open_compose_new(cmd.compose_mailto, cmd.attach_files); |
| 328 | if (cmd.attach_files) {
|
| 329 | ptr_array_free_strings(cmd.attach_files); |
| 330 | g_ptr_array_free(cmd.attach_files, TRUE); |
| 331 | cmd.attach_files = NULL;
|
| 332 | } |
| 333 | if (cmd.send)
|
| 334 | send_queue(); |
| 335 | if (cmd.status_folders) {
|
| 336 | g_ptr_array_free(cmd.status_folders, TRUE); |
| 337 | cmd.status_folders = NULL;
|
| 338 | } |
| 339 | if (cmd.status_full_folders) {
|
| 340 | g_ptr_array_free(cmd.status_full_folders, TRUE); |
| 341 | cmd.status_full_folders = NULL;
|
| 342 | } |
| 343 | |
| 344 | gtk_main(); |
| 345 | |
| 346 | return 0; |
| 347 | } |
| 348 | |
| 349 | static void parse_cmd_opt(int argc, char *argv[]) |
| 350 | {
|
| 351 | gint i; |
| 352 | |
| 353 | for (i = 1; i < argc; i++) { |
| 354 | if (!strncmp(argv[i], "--debug", 7)) |
| 355 | debug_mode = TRUE; |
| 356 | else if (!strncmp(argv[i], "--receive-all", 13)) |
| 357 | cmd.receive_all = TRUE; |
| 358 | else if (!strncmp(argv[i], "--receive", 9)) |
| 359 | cmd.receive = TRUE; |
| 360 | else if (!strncmp(argv[i], "--compose", 9)) { |
| 361 | const gchar *p = argv[i + 1]; |
| 362 | |
| 363 | cmd.compose = TRUE; |
| 364 | cmd.compose_mailto = NULL;
|
| 365 | if (p && *p != '\0' && *p != '-') { |
| 366 | if (!strncmp(p, "mailto:", 7)) |
| 367 | cmd.compose_mailto = p + 7;
|
| 368 | else
|
| 369 | cmd.compose_mailto = p; |
| 370 | i++; |
| 371 | } |
| 372 | } else if (!strncmp(argv[i], "--attach", 8)) { |
| 373 | const gchar *p = argv[i + 1]; |
| 374 | gchar *file; |
| 375 | |
| 376 | while (p && *p != '\0' && *p != '-') { |
| 377 | if (!cmd.attach_files)
|
| 378 | cmd.attach_files = g_ptr_array_new(); |
| 379 | if (*p != G_DIR_SEPARATOR)
|
| 380 | file = g_strconcat(startup_dir, |
| 381 | G_DIR_SEPARATOR_S, |
| 382 | p, NULL);
|
| 383 | else
|
| 384 | file = g_strdup(p); |
| 385 | g_ptr_array_add(cmd.attach_files, file); |
| 386 | i++; |
| 387 | p = argv[i + 1];
|
| 388 | } |
| 389 | } else if (!strncmp(argv[i], "--send", 6)) { |
| 390 | cmd.send = TRUE; |
| 391 | } else if (!strncmp(argv[i], "--version", 9)) { |
| 392 | puts("Sylpheed version " VERSION);
|
| 393 | exit(0);
|
| 394 | } else if (!strncmp(argv[i], "--status-full", 13)) { |
| 395 | const gchar *p = argv[i + 1]; |
| 396 | |
| 397 | cmd.status_full = TRUE; |
| 398 | while (p && *p != '\0' && *p != '-') { |
| 399 | if (!cmd.status_full_folders)
|
| 400 | cmd.status_full_folders = |
| 401 | g_ptr_array_new(); |
| 402 | g_ptr_array_add(cmd.status_full_folders, |
| 403 | g_strdup(p)); |
| 404 | i++; |
| 405 | p = argv[i + 1];
|
| 406 | } |
| 407 | } else if (!strncmp(argv[i], "--status", 8)) { |
| 408 | const gchar *p = argv[i + 1]; |
| 409 | |
| 410 | cmd.status = TRUE; |
| 411 | while (p && *p != '\0' && *p != '-') { |
| 412 | if (!cmd.status_folders)
|
| 413 | cmd.status_folders = g_ptr_array_new(); |
| 414 | g_ptr_array_add(cmd.status_folders, |
| 415 | g_strdup(p)); |
| 416 | i++; |
| 417 | p = argv[i + 1];
|
| 418 | } |
| 419 | } else if (!strncmp(argv[i], "--help", 6)) { |
| 420 | g_print(_("Usage: %s [OPTION]...\n"),
|
| 421 | g_basename(argv[0]));
|
| 422 | |
| 423 | g_print("%s\n", _(" --compose [address] open composition window")); |
| 424 | g_print("%s\n", _(" --attach file1 [file2]...\n" |
| 425 | " open composition window with specified files\n"
|
| 426 | " attached"));
|
| 427 | g_print("%s\n", _(" --receive receive new messages")); |
| 428 | g_print("%s\n", _(" --receive-all receive new messages of all accounts")); |
| 429 | g_print("%s\n", _(" --send send all queued messages")); |
| 430 | g_print("%s\n", _(" --status [folder]... show the total number of messages")); |
| 431 | g_print("%s\n", _(" --status-full [folder]...\n" |
| 432 | " show the status of each folder"));
|
| 433 | g_print("%s\n", _(" --debug debug mode")); |
| 434 | g_print("%s\n", _(" --help display this help and exit")); |
| 435 | g_print("%s\n", _(" --version output version information and exit")); |
| 436 | |
| 437 | exit(1);
|
| 438 | } |
| 439 | } |
| 440 | |
| 441 | if (cmd.attach_files && cmd.compose == FALSE) {
|
| 442 | cmd.compose = TRUE; |
| 443 | cmd.compose_mailto = NULL;
|
| 444 | } |
| 445 | } |
| 446 | |
| 447 | static gint get_queued_message_num(void) |
| 448 | {
|
| 449 | FolderItem *queue; |
| 450 | |
| 451 | queue = folder_get_default_queue(); |
| 452 | if (!queue) return -1; |
| 453 | |
| 454 | folder_item_scan(queue); |
| 455 | return queue->total;
|
| 456 | } |
| 457 | |
| 458 | void app_will_exit(GtkWidget *widget, gpointer data)
|
| 459 | {
|
| 460 | MainWindow *mainwin = data; |
| 461 | gchar *filename; |
| 462 | |
| 463 | if (compose_get_compose_list()) {
|
| 464 | if (alertpanel(_("Notice"), |
| 465 | _("Composing message exists. Really quit?"),
|
| 466 | GTK_STOCK_OK, GTK_STOCK_CANCEL, NULL)
|
| 467 | != G_ALERTDEFAULT) |
| 468 | return;
|
| 469 | manage_window_focus_in(mainwin->window, NULL, NULL); |
| 470 | } |
| 471 | |
| 472 | if (prefs_common.warn_queued_on_exit && get_queued_message_num() > 0) { |
| 473 | if (alertpanel(_("Queued messages"), |
| 474 | _("Some unsent messages are queued. Exit now?"),
|
| 475 | GTK_STOCK_OK, GTK_STOCK_CANCEL, NULL)
|
| 476 | != G_ALERTDEFAULT) |
| 477 | return;
|
| 478 | manage_window_focus_in(mainwin->window, NULL, NULL); |
| 479 | } |
| 480 | |
| 481 | inc_autocheck_timer_remove(); |
| 482 | |
| 483 | if (prefs_common.clean_on_exit)
|
| 484 | main_window_empty_trash(mainwin, prefs_common.ask_on_clean); |
| 485 | |
| 486 | /* save all state before exiting */
|
| 487 | folder_write_list(); |
| 488 | summary_write_cache(mainwin->summaryview); |
| 489 | |
| 490 | main_window_get_size(mainwin); |
| 491 | main_window_get_position(mainwin); |
| 492 | prefs_common_write_config(); |
| 493 | prefs_filter_write_config(); |
| 494 | account_write_config_all(); |
| 495 | addressbook_export_to_file(); |
| 496 | |
| 497 | filename = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MENU_RC, NULL);
|
| 498 | gtk_accel_map_save(filename); |
| 499 | g_free(filename); |
| 500 | |
| 501 | /* delete temporary files */
|
| 502 | remove_all_files(get_mime_tmp_dir()); |
| 503 | |
| 504 | close_log_file(); |
| 505 | lock_socket_remove(); |
| 506 | |
| 507 | #if USE_SSL
|
| 508 | ssl_done(); |
| 509 | #endif
|
| 510 | |
| 511 | gtk_main_quit(); |
| 512 | } |
| 513 | |
| 514 | #if 0
|
| 515 | #if USE_GPGME |
| 516 | static void idle_function_for_gpgme(void) |
| 517 | {
|
| 518 | while (gtk_events_pending()) |
| 519 | gtk_main_iteration(); |
| 520 | } |
| 521 | #endif /* USE_GPGME */ |
| 522 | #endif /* 0 */ |
| 523 | |
| 524 | static gchar *get_socket_name(void) |
| 525 | {
|
| 526 | static gchar *filename = NULL; |
| 527 | |
| 528 | if (filename == NULL) { |
| 529 | filename = g_strdup_printf("%s%csylpheed-%d",
|
| 530 | g_get_tmp_dir(), G_DIR_SEPARATOR, |
| 531 | getuid()); |
| 532 | } |
| 533 | |
| 534 | return filename;
|
| 535 | } |
| 536 | |
| 537 | static gint prohibit_duplicate_launch(void) |
| 538 | {
|
| 539 | gint uxsock; |
| 540 | gchar *path; |
| 541 | |
| 542 | path = get_socket_name(); |
| 543 | uxsock = fd_connect_unix(path); |
| 544 | if (uxsock < 0) { |
| 545 | unlink(path); |
| 546 | return fd_open_unix(path);
|
| 547 | } |
| 548 | |
| 549 | /* remote command mode */
|
| 550 | |
| 551 | debug_print(_("another Sylpheed is already running.\n"));
|
| 552 | |
| 553 | if (cmd.receive_all)
|
| 554 | fd_write_all(uxsock, "receive_all\n", 12); |
| 555 | else if (cmd.receive) |
| 556 | fd_write_all(uxsock, "receive\n", 8); |
| 557 | else if (cmd.compose && cmd.attach_files) { |
| 558 | gchar *str, *compose_str; |
| 559 | gint i; |
| 560 | |
| 561 | if (cmd.compose_mailto)
|
| 562 | compose_str = g_strdup_printf("compose_attach %s\n",
|
| 563 | cmd.compose_mailto); |
| 564 | else
|
| 565 | compose_str = g_strdup("compose_attach\n");
|
| 566 | |
| 567 | fd_write_all(uxsock, compose_str, strlen(compose_str)); |
| 568 | g_free(compose_str); |
| 569 | |
| 570 | for (i = 0; i < cmd.attach_files->len; i++) { |
| 571 | str = g_ptr_array_index(cmd.attach_files, i); |
| 572 | fd_write_all(uxsock, str, strlen(str)); |
| 573 | fd_write_all(uxsock, "\n", 1); |
| 574 | } |
| 575 | |
| 576 | fd_write_all(uxsock, ".\n", 2); |
| 577 | } else if (cmd.compose) { |
| 578 | gchar *compose_str; |
| 579 | |
| 580 | if (cmd.compose_mailto)
|
| 581 | compose_str = g_strdup_printf |
| 582 | ("compose %s\n", cmd.compose_mailto);
|
| 583 | else
|
| 584 | compose_str = g_strdup("compose\n");
|
| 585 | |
| 586 | fd_write_all(uxsock, compose_str, strlen(compose_str)); |
| 587 | g_free(compose_str); |
| 588 | } else if (cmd.send) { |
| 589 | fd_write_all(uxsock, "send\n", 5); |
| 590 | } else if (cmd.status || cmd.status_full) { |
| 591 | gchar buf[BUFFSIZE]; |
| 592 | gint i; |
| 593 | const gchar *command;
|
| 594 | GPtrArray *folders; |
| 595 | gchar *folder; |
| 596 | |
| 597 | command = cmd.status_full ? "status-full\n" : "status\n"; |
| 598 | folders = cmd.status_full ? cmd.status_full_folders : |
| 599 | cmd.status_folders; |
| 600 | |
| 601 | fd_write_all(uxsock, command, strlen(command)); |
| 602 | for (i = 0; folders && i < folders->len; ++i) { |
| 603 | folder = g_ptr_array_index(folders, i); |
| 604 | fd_write_all(uxsock, folder, strlen(folder)); |
| 605 | fd_write_all(uxsock, "\n", 1); |
| 606 | } |
| 607 | fd_write_all(uxsock, ".\n", 2); |
| 608 | for (;;) {
|
| 609 | fd_gets(uxsock, buf, sizeof(buf));
|
| 610 | if (!strncmp(buf, ".\n", 2)) break; |
| 611 | fputs(buf, stdout); |
| 612 | } |
| 613 | } else
|
| 614 | fd_write_all(uxsock, "popup\n", 6); |
| 615 | |
| 616 | fd_close(uxsock); |
| 617 | return -1; |
| 618 | } |
| 619 | |
| 620 | static gint lock_socket_remove(void) |
| 621 | {
|
| 622 | gchar *filename; |
| 623 | |
| 624 | if (lock_socket < 0) return -1; |
| 625 | |
| 626 | if (lock_socket_tag > 0) |
| 627 | gdk_input_remove(lock_socket_tag); |
| 628 | fd_close(lock_socket); |
| 629 | filename = get_socket_name(); |
| 630 | unlink(filename); |
| 631 | |
| 632 | return 0; |
| 633 | } |
| 634 | |
| 635 | static GPtrArray *get_folder_item_list(gint sock)
|
| 636 | {
|
| 637 | gchar buf[BUFFSIZE]; |
| 638 | FolderItem *item; |
| 639 | GPtrArray *folders = NULL;
|
| 640 | |
| 641 | for (;;) {
|
| 642 | fd_gets(sock, buf, sizeof(buf));
|
| 643 | if (!strncmp(buf, ".\n", 2)) break; |
| 644 | strretchomp(buf); |
| 645 | if (!folders) folders = g_ptr_array_new();
|
| 646 | item = folder_find_item_from_identifier(buf); |
| 647 | if (item)
|
| 648 | g_ptr_array_add(folders, item); |
| 649 | else
|
| 650 | g_warning("no such folder: %s\n", buf);
|
| 651 | } |
| 652 | |
| 653 | return folders;
|
| 654 | } |
| 655 | |
| 656 | static void lock_socket_input_cb(gpointer data, |
| 657 | gint source, |
| 658 | GdkInputCondition condition) |
| 659 | {
|
| 660 | MainWindow *mainwin = (MainWindow *)data; |
| 661 | gint sock; |
| 662 | gchar buf[BUFFSIZE]; |
| 663 | |
| 664 | sock = fd_accept(source); |
| 665 | fd_gets(sock, buf, sizeof(buf));
|
| 666 | |
| 667 | if (!strncmp(buf, "popup", 5)) { |
| 668 | main_window_popup(mainwin); |
| 669 | } else if (!strncmp(buf, "receive_all", 11)) { |
| 670 | main_window_popup(mainwin); |
| 671 | inc_all_account_mail(mainwin, FALSE); |
| 672 | } else if (!strncmp(buf, "receive", 7)) { |
| 673 | main_window_popup(mainwin); |
| 674 | inc_mail(mainwin); |
| 675 | } else if (!strncmp(buf, "compose_attach", 14)) { |
| 676 | GPtrArray *files; |
| 677 | gchar *mailto; |
| 678 | |
| 679 | mailto = g_strdup(buf + strlen("compose_attach") + 1); |
| 680 | files = g_ptr_array_new(); |
| 681 | while (fd_gets(sock, buf, sizeof(buf)) > 0) { |
| 682 | if (buf[0] == '.' && buf[1] == '\n') break; |
| 683 | strretchomp(buf); |
| 684 | g_ptr_array_add(files, g_strdup(buf)); |
| 685 | } |
| 686 | open_compose_new(mailto, files); |
| 687 | ptr_array_free_strings(files); |
| 688 | g_ptr_array_free(files, TRUE); |
| 689 | g_free(mailto); |
| 690 | } else if (!strncmp(buf, "compose", 7)) { |
| 691 | open_compose_new(buf + strlen("compose") + 1, NULL); |
| 692 | } else if (!strncmp(buf, "send", 4)) { |
| 693 | send_queue(); |
| 694 | } else if (!strncmp(buf, "status-full", 11) || |
| 695 | !strncmp(buf, "status", 6)) { |
| 696 | gchar *status; |
| 697 | GPtrArray *folders; |
| 698 | |
| 699 | folders = get_folder_item_list(sock); |
| 700 | status = folder_get_status |
| 701 | (folders, !strncmp(buf, "status-full", 11)); |
| 702 | fd_write_all(sock, status, strlen(status)); |
| 703 | fd_write_all(sock, ".\n", 2); |
| 704 | g_free(status); |
| 705 | if (folders) g_ptr_array_free(folders, TRUE);
|
| 706 | } |
| 707 | |
| 708 | fd_close(sock); |
| 709 | } |
| 710 | |
| 711 | static void migrate_old_config(void) |
| 712 | {
|
| 713 | DIR *dp; |
| 714 | struct dirent *d;
|
| 715 | GPatternSpec *pspec; |
| 716 | |
| 717 | if (alertpanel(_("Migration of configuration"), |
| 718 | _("The previous version of configuration found.\n"
|
| 719 | "Do you want to migrate it?"),
|
| 720 | GTK_STOCK_YES, GTK_STOCK_NO, NULL) != G_ALERTDEFAULT)
|
| 721 | return;
|
| 722 | |
| 723 | debug_print("Migrating old configuration...\n");
|
| 724 | |
| 725 | #define COPY_FILE(rcfile) \
|
| 726 | if (is_file_exist(OLD_RC_DIR G_DIR_SEPARATOR_S rcfile)) { \
|
| 727 | conv_copy_file(OLD_RC_DIR G_DIR_SEPARATOR_S rcfile, \ |
| 728 | RC_DIR G_DIR_SEPARATOR_S rcfile, \ |
| 729 | conv_get_locale_charset_str()); \ |
| 730 | } |
| 731 | |
| 732 | COPY_FILE(ACCOUNT_RC); |
| 733 | COPY_FILE(ACTIONS_RC); |
| 734 | COPY_FILE(COMMON_RC); |
| 735 | COPY_FILE(CUSTOM_HEADER_RC); |
| 736 | COPY_FILE(DISPLAY_HEADER_RC); |
| 737 | COPY_FILE(FILTER_HEADER_RC); |
| 738 | COPY_FILE(COMMAND_HISTORY); |
| 739 | |
| 740 | #undef COPY_FILE
|
| 741 | |
| 742 | if (is_file_exist(OLD_RC_DIR G_DIR_SEPARATOR_S FILTER_LIST))
|
| 743 | copy_file(OLD_RC_DIR G_DIR_SEPARATOR_S FILTER_LIST, |
| 744 | RC_DIR G_DIR_SEPARATOR_S FILTER_LIST, FALSE); |
| 745 | if (is_file_exist(OLD_RC_DIR G_DIR_SEPARATOR_S FOLDER_LIST))
|
| 746 | copy_file(OLD_RC_DIR G_DIR_SEPARATOR_S FOLDER_LIST, |
| 747 | RC_DIR G_DIR_SEPARATOR_S FOLDER_LIST, FALSE); |
| 748 | |
| 749 | if (is_dir_exist(OLD_RC_DIR G_DIR_SEPARATOR_S TEMPLATE_DIR))
|
| 750 | conv_copy_dir(OLD_RC_DIR G_DIR_SEPARATOR_S TEMPLATE_DIR, |
| 751 | RC_DIR G_DIR_SEPARATOR_S TEMPLATE_DIR, |
| 752 | conv_get_locale_charset_str()); |
| 753 | if (is_dir_exist(OLD_RC_DIR G_DIR_SEPARATOR_S UIDL_DIR))
|
| 754 | copy_dir(OLD_RC_DIR G_DIR_SEPARATOR_S UIDL_DIR, |
| 755 | RC_DIR G_DIR_SEPARATOR_S UIDL_DIR); |
| 756 | |
| 757 | if (!is_file_exist(OLD_RC_DIR G_DIR_SEPARATOR_S ADDRESSBOOK_INDEX_FILE))
|
| 758 | return;
|
| 759 | |
| 760 | if ((dp = opendir(OLD_RC_DIR)) == NULL) { |
| 761 | FILE_OP_ERROR(OLD_RC_DIR, "opendir");
|
| 762 | return;
|
| 763 | } |
| 764 | |
| 765 | pspec = g_pattern_spec_new("addrbook-*.xml");
|
| 766 | |
| 767 | while ((d = readdir(dp)) != NULL) { |
| 768 | if (g_pattern_match_string(pspec, d->d_name)) {
|
| 769 | gchar *old_file; |
| 770 | gchar *new_file; |
| 771 | |
| 772 | old_file = g_strconcat(OLD_RC_DIR G_DIR_SEPARATOR_S, |
| 773 | d->d_name, NULL);
|
| 774 | new_file = g_strconcat(RC_DIR G_DIR_SEPARATOR_S, |
| 775 | d->d_name, NULL);
|
| 776 | copy_file(old_file, new_file, FALSE); |
| 777 | g_free(new_file); |
| 778 | g_free(old_file); |
| 779 | } |
| 780 | } |
| 781 | |
| 782 | g_pattern_spec_free(pspec); |
| 783 | |
| 784 | closedir(dp); |
| 785 | } |
| 786 | |
| 787 | static void open_compose_new(const gchar *address, GPtrArray *attach_files) |
| 788 | {
|
| 789 | gchar *addr = NULL;
|
| 790 | |
| 791 | if (address) {
|
| 792 | Xstrdup_a(addr, address, return);
|
| 793 | g_strstrip(addr); |
| 794 | } |
| 795 | |
| 796 | compose_new(NULL, NULL, addr, attach_files); |
| 797 | } |
| 798 | |
| 799 | static void send_queue(void) |
| 800 | {
|
| 801 | GList *list; |
| 802 | |
| 803 | if (!main_window_toggle_online_if_offline(main_window_get()))
|
| 804 | return;
|
| 805 | |
| 806 | for (list = folder_get_list(); list != NULL; list = list->next) { |
| 807 | Folder *folder = list->data; |
| 808 | |
| 809 | if (folder->queue) {
|
| 810 | gint ret; |
| 811 | |
| 812 | ret = procmsg_send_queue(folder->queue, |
| 813 | prefs_common.savemsg); |
| 814 | statusbar_pop_all(); |
| 815 | if (ret > 0) |
| 816 | folder_item_scan(folder->queue); |
| 817 | } |
| 818 | } |
| 819 | |
| 820 | folderview_update_all_updated(TRUE); |
| 821 | } |