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