2012-11-27 [wwp] 3.9.0cvs21
[claws.git] / src / main.c
index 9a9e598f83870d8573d4f84a8aa6085098e34a80..27ff8217c1bdc22cde3fb740fc14b06be4053d6f 100644 (file)
 #include <X11/SM/SMlib.h>
 #endif
 
+#if HAVE_FLOCK
+#include <sys/file.h>
+#endif
+
 #include "wizard.h"
 #ifdef HAVE_STARTUP_NOTIFICATION
 # define SN_API_NOT_YET_FROZEN
@@ -90,6 +94,7 @@
 #include "account.h"
 #include "procmsg.h"
 #include "inc.h"
+#include "imap.h"
 #include "import.h"
 #include "manage_window.h"
 #include "alertpanel.h"
 #endif
 #include "compose.h"
 #include "folder.h"
+#include "folder_item_prefs.h"
 #include "setup.h"
 #include "utils.h"
 #include "gtkutils.h"
 #include "hooks.h"
 #include "menu.h"
 #include "quicksearch.h"
+#include "advsearch.h"
 
 #ifdef HAVE_LIBETPAN
 #include "imap-thread.h"
@@ -198,6 +205,8 @@ typedef enum
 static struct RemoteCmd {
        gboolean receive;
        gboolean receive_all;
+       gboolean cancel_receiving;
+       gboolean cancel_sending;
        gboolean compose;
        const gchar *compose_mailto;
        GList *attach_files;
@@ -377,6 +386,11 @@ static gboolean defer_check_all(void *data)
        inc_all_account_mail(static_mainwindow, autochk, 
                        prefs_common.newmail_notify_manu);
 
+       /* Accounts using a specific timer need to be checked separately
+        * at startup
+        */
+       inc_all_account_spec_timer_check_mail();
+       
        if (sc_starting) {
                sc_starting = FALSE;
                main_window_set_menu_sensitive(static_mainwindow);
@@ -608,7 +622,7 @@ static gboolean sc_process_ice_messages (GIOChannel   *source,
        if (status == IceProcessMessagesIOError) {
                IcePointer context = IceGetConnectionContext (connection);
 
-               if (context && GTK_IS_OBJECT (context)) {
+               if (context && G_IS_OBJECT(context)) {
                guint disconnect_id = g_signal_lookup ("disconnect", G_OBJECT_TYPE (context));
 
                if (disconnect_id > 0)
@@ -1087,18 +1101,31 @@ int main(int argc, char *argv[])
                return 0;
        }
 
-       main_dump_features_list(TRUE);
-
        prog_version = PROG_VERSION;
        argv0 = g_strdup(argv[0]);
 
        parse_cmd_opt(argc, argv);
 
+       sock_init();
+
+       /* check and create unix domain socket for remote operation */
+       lock_socket = prohibit_duplicate_launch();
+       if (lock_socket < 0) {
+#ifdef HAVE_STARTUP_NOTIFICATION
+               if(gtk_init_check(&argc, &argv))
+                       startup_notification_complete(TRUE);
+#endif
+               return 0;
+       }
+
+       main_dump_features_list(TRUE);
        prefs_prepare_cache();
 
 #ifdef CRASH_DIALOG
        if (cmd.crash) {
+#if !GTK_CHECK_VERSION(3, 0, 0)
                gtk_set_locale();
+#endif
                gtk_init(&argc, &argv);
                crash_main(cmd.crash_params);
 #ifdef G_OS_WIN32
@@ -1112,20 +1139,10 @@ int main(int argc, char *argv[])
 #if (defined linux && defined SIGIO)
        install_memory_sighandler();
 #endif
-       sock_init();
-
-       /* check and create unix domain socket for remote operation */
-       lock_socket = prohibit_duplicate_launch();
-       if (lock_socket < 0) {
-#ifdef HAVE_STARTUP_NOTIFICATION
-               if(gtk_init_check(&argc, &argv))
-                       startup_notification_complete(TRUE);
-#endif
-               return 0;
-       }
 
        if (cmd.status || cmd.status_full || cmd.search ||
-               cmd.statistics || cmd.reset_statistics) {
+               cmd.statistics || cmd.reset_statistics || 
+               cmd.cancel_receiving || cmd.cancel_sending) {
                puts("0 Claws Mail not running.");
                lock_socket_remove();
                return 0;
@@ -1138,7 +1155,9 @@ int main(int argc, char *argv[])
 
        reset_statistics();
        
+#if !GTK_CHECK_VERSION(3, 0, 0)
        gtk_set_locale();
+#endif
        gtk_init(&argc, &argv);
 
 #ifdef G_OS_WIN32
@@ -1193,9 +1212,11 @@ int main(int argc, char *argv[])
        hildon_program = HILDON_PROGRAM(hildon_program_get_instance());
        static_osso_context = osso_context;
 #endif 
+#if !GTK_CHECK_VERSION(3, 0, 0)
        gtk_widget_set_default_colormap(
                gdk_screen_get_system_colormap(
                        gdk_screen_get_default()));
+#endif
 
        gtkut_create_ui_manager();
 
@@ -1331,6 +1352,7 @@ int main(int argc, char *argv[])
        prefs_receive_init();
        prefs_send_init();
        tags_read_tags();
+       matcher_init();
 #ifdef USE_ENCHANT
        gtkaspell_checkers_init();
        prefs_spelling_init();
@@ -1401,13 +1423,6 @@ int main(int argc, char *argv[])
        gtk_cmclist_freeze(GTK_CMCLIST(mainwin->folderview->ctree));
        folder_item_update_freeze();
 
-       /* register the callback of unix domain socket input */
-       lock_socket_tag = claws_input_add(lock_socket,
-                                       G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_PRI,
-                                       lock_socket_input_cb,
-                                       mainwin, TRUE);
-
-
        prefs_account_init();
        account_read_config_all();
 
@@ -1686,6 +1701,13 @@ int main(int argc, char *argv[])
                main_window_set_menu_sensitive(mainwin);
                toolbar_main_set_sensitive(mainwin);
        }
+
+       /* register the callback of unix domain socket input */
+       lock_socket_tag = claws_input_add(lock_socket,
+                                       G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_PRI,
+                                       lock_socket_input_cb,
+                                       mainwin, TRUE);
+
        END_TIMING();
 
        gtk_main();
@@ -1796,6 +1818,7 @@ static void exit_claws(MainWindow *mainwin)
        
        plugin_unload_all("GTK2");
 
+       matcher_done();
        prefs_toolbar_done();
 
 #ifndef USE_NEW_ADDRBOOK
@@ -1923,6 +1946,10 @@ static void parse_cmd_opt(int argc, char *argv[])
                        cmd.receive_all = TRUE;
                } else if (!strncmp(argv[i], "--receive", 9)) {
                        cmd.receive = TRUE;
+               } else if (!strncmp(argv[i], "--cancel-receiving", 18)) {
+                       cmd.cancel_receiving = TRUE;
+               } else if (!strncmp(argv[i], "--cancel-sending", 16)) {
+                       cmd.cancel_sending = TRUE;
                } else if (!strncmp(argv[i], "--compose-from-file", 19)) {
                        const gchar *p = (i+1 < argc)?argv[i+1]:NULL;
 
@@ -2045,6 +2072,8 @@ static void parse_cmd_opt(int argc, char *argv[])
                                          "                         attached"));
                        g_print("%s\n", _("  --receive              receive new messages"));
                        g_print("%s\n", _("  --receive-all          receive new messages of all accounts"));
+                       g_print("%s\n", _("  --cancel-receiving     cancel receiving of messages"));
+                       g_print("%s\n", _("  --cancel-sending       cancel sending of messages"));
                        g_print("%s\n", _("  --search folder type request [recursive]\n"
                                          "                         searches mail\n"
                                          "                         folder ex.: \"#mh/Mailbox/inbox\" or \"Mail\"\n"
@@ -2300,14 +2329,51 @@ static gint prohibit_duplicate_launch(void)
        gchar *path;
 
        path = claws_get_socket_name();
+       /* Try to connect to the control socket */
        uxsock = fd_connect_unix(path);
        
        if (x_display == NULL)
                x_display = g_strdup(g_getenv("DISPLAY"));
 
        if (uxsock < 0) {
+#if HAVE_FLOCK
+               gchar *socket_lock;
+               gint lock_fd;
+               gint ret;
+               /* If connect failed, no other process is running.
+                * Unlink the potentially existing socket, then
+                * open it. This has to be done locking a temporary
+                * file to avoid the race condition where another
+                * process could have created the socket just in
+                * between.
+                */
+               socket_lock = g_strconcat(path, ".lock",
+                                         NULL);
+               lock_fd = g_open(socket_lock, O_RDWR|O_CREAT, 0);
+               if (lock_fd < 0) {
+                       debug_print("Couldn't open %s: %s (%d)\n", socket_lock,
+                               strerror(errno), errno);
+                       g_free(socket_lock);
+                       return -1;
+               }
+               if (flock(lock_fd, LOCK_EX) < 0) {
+                       debug_print("Couldn't lock %s: %s (%d)\n", socket_lock,
+                               strerror(errno), errno);
+                       close(lock_fd);
+                       g_free(socket_lock);
+                       return -1;
+               }
+#endif
+
                claws_unlink(path);
-               return fd_open_unix(path);
+               debug_print("Opening socket %s\n", path);
+               ret = fd_open_unix(path);
+#if HAVE_FLOCK
+               flock(lock_fd, LOCK_UN);
+               claws_unlink(socket_lock);
+               g_free(socket_lock);
+#endif
+               return ret;
        }
 #else
         HANDLE hmutex;
@@ -2336,6 +2402,10 @@ static gint prohibit_duplicate_launch(void)
                fd_write_all(uxsock, "receive_all\n", 12);
        } else if (cmd.receive) {
                fd_write_all(uxsock, "receive\n", 8);
+       } else if (cmd.cancel_receiving) {
+               fd_write_all(uxsock, "cancel_receiving\n", 17);
+       } else if (cmd.cancel_sending) {
+               fd_write_all(uxsock, "cancel_sending\n", 15);
        } else if (cmd.compose && cmd.attach_files) {
                gchar *str, *compose_str;
 
@@ -2508,8 +2578,6 @@ static void lock_socket_input_cb(gpointer data,
        MainWindow *mainwin = (MainWindow *)data;
        gint sock;
        gchar buf[BUFFSIZE];
-       /* re-use the same quicksearch (& avoid matcher_list mem.leaks) */
-       static QuickSearch *quicksearch = NULL;
 
        sock = fd_accept(source);
        fd_gets(sock, buf, sizeof(buf));
@@ -2523,6 +2591,11 @@ static void lock_socket_input_cb(gpointer data,
                                     prefs_common.newmail_notify_manu);
        } else if (!strncmp(buf, "receive", 7)) {
                inc_mail(mainwin, prefs_common.newmail_notify_manu);
+       } else if (!strncmp(buf, "cancel_receiving", 16)) {
+               inc_cancel_all();
+               imap_cancel_all();
+       } else if (!strncmp(buf, "cancel_sending", 14)) {
+               send_cancel();
        } else if (!strncmp(buf, "compose_attach", 14)) {
                GList *files = NULL, *curr;
                AttachInfo *ainfo;
@@ -2632,37 +2705,44 @@ static void lock_socket_input_cb(gpointer data,
        } else if (!strncmp(buf, "search ", 7)) {
                FolderItem* folderItem = NULL;
                GSList *messages = NULL;
-               gchar *folder_name, *request;
-               QuickSearchType searchType = QUICK_SEARCH_EXTENDED;
+               gchar *folder_name = NULL;
+               gchar *request = NULL;
+               AdvancedSearch *search;
                gboolean recursive;
-
-               if (quicksearch==NULL)
-                       quicksearch = quicksearch_new_nogui();
+               AdvancedSearchType searchType = ADVANCED_SEARCH_EXTENDED;
                
+               search = advsearch_new();
+
                folder_name = g_strdup(buf+7);
                strretchomp(folder_name);
 
-               if (fd_gets(sock, buf, sizeof(buf)) <= 0) {
-                       g_free(folder_name);
-                       folder_name=NULL;
-               }
-               searchType = quicksearch_type(buf);
-               if (fd_gets(sock, buf, sizeof(buf)) <= 0) {
-                       g_free(folder_name);
-                       folder_name=NULL;
+               if (fd_gets(sock, buf, sizeof(buf)) <= 0) 
+                       goto search_exit;
+
+               switch (toupper(buf[0])) {
+               case 'S': searchType = ADVANCED_SEARCH_SUBJECT; break;
+               case 'F': searchType = ADVANCED_SEARCH_FROM; break;
+               case 'T': searchType = ADVANCED_SEARCH_TO; break;
+               case 'M': searchType = ADVANCED_SEARCH_MIXED; break;
+               case 'G': searchType = ADVANCED_SEARCH_TAG; break;
+               case 'E': searchType = ADVANCED_SEARCH_EXTENDED; break;
                }
+
+               if (fd_gets(sock, buf, sizeof(buf)) <= 0) 
+                       goto search_exit;
+
                request = g_strdup(buf);
                strretchomp(request);
 
                recursive = TRUE;
                if (fd_gets(sock, buf, sizeof(buf)) > 0)
-                       if (buf[0]=='0')
-                               recursive = FALSE;
+                       recursive = buf[0] != '0';
 
-               debug_print("search: %s %i %s %i\n",folder_name,searchType,request,recursive);
+               debug_print("search: %s %i %s %i\n", folder_name, searchType, request, recursive);
 
                if (folder_name)
                        folderItem = folder_find_item_from_identifier(folder_name);
+
                if (folder_name && folderItem == NULL) {
                        debug_print("Unknown folder item : '%s', searching folder\n",folder_name);
                        Folder* folder = folder_find_from_path(folder_name);
@@ -2673,16 +2753,16 @@ static void lock_socket_input_cb(gpointer data,
                } else {
                        debug_print("%s %s\n",folderItem->name, folderItem->path);
                }
+
                if (folderItem != NULL) {
-                       quicksearch_set(quicksearch, searchType, request);
-                       quicksearch_set_recursive(quicksearch, recursive);
-                       search_msgs_in_folders(&messages, quicksearch, folderItem);
+                       advsearch_set(search, searchType, request);
+                       advsearch_search_msgs_in_folders(search, &messages, folderItem, recursive);
                } else {
                        g_print("Folder '%s' not found.\n'", folder_name);
                }
 
                GSList *cur;
-               for (cur=messages; cur != NULL; cur = cur->next) {
+               for (cur = messages; cur != NULL; cur = cur->next) {
                        MsgInfo* msg = (MsgInfo *)cur->data;
                        gchar *file = procmsg_get_message_file_path(msg);
                        fd_write_all(sock, file, strlen(file));
@@ -2691,10 +2771,12 @@ static void lock_socket_input_cb(gpointer data,
                }
                fd_write_all(sock, ".\n", 2);
 
-               if (messages != NULL)
-                       procmsg_msg_list_free(messages);
+search_exit:
                g_free(folder_name);
                g_free(request);
+               advsearch_free(search);
+               if (messages != NULL)
+                       procmsg_msg_list_free(messages);
        } else if (!strncmp(buf, "exit", 4)) {
                if (prefs_common.clean_on_exit && !prefs_common.ask_on_clean) {
                        procmsg_empty_all_trash();