2005-06-08 [colin] 1.9.11cvs53
authorColin Leroy <colin@colino.net>
Wed, 8 Jun 2005 20:17:21 +0000 (20:17 +0000)
committerColin Leroy <colin@colino.net>
Wed, 8 Jun 2005 20:17:21 +0000 (20:17 +0000)
* src/imap.c
Thread-ize imap_cmd_fetch

ChangeLog-gtk2.claws
PATCHSETS
configure.ac
src/imap.c

index 742ee02a7d29e4caf0751eba456a0c47a9fa17bc..59e7cb98fe17679686498abdc96c8f09d37680e7 100644 (file)
@@ -1,3 +1,8 @@
+2005-06-08 [colin]     1.9.11cvs53
+
+       * src/imap.c
+               Thread-ize imap_cmd_fetch
+
 2005-06-08 [colin]     1.9.11cvs52
 
        * src/main.c
index 35931512ac957813624246680ed8891a97e40caf..46c14a7758acbb94bc2e4d33f87bcabc1e7c0c0a 100644 (file)
--- a/PATCHSETS
+++ b/PATCHSETS
 ( cvs diff -u -r 1.179.2.19 -r 1.179.2.20 src/imap.c;  ) > 1.9.11cvs50.patchset
 ( cvs diff -u -r 1.115.2.38 -r 1.115.2.39 src/main.c;  ) > 1.9.11cvs51.patchset
 ( cvs diff -u -r 1.115.2.39 -r 1.115.2.40 src/main.c;  ) > 1.9.11cvs52.patchset
+( cvs diff -u -r 1.179.2.20 -r 1.179.2.21 src/imap.c;  ) > 1.9.11cvs53.patchset
index 80f445775766f9212add536bfa900e9e0f4fffd7..86bbb82058f92de491105f03b94c510e02713d52 100644 (file)
@@ -11,7 +11,7 @@ MINOR_VERSION=9
 MICRO_VERSION=11
 INTERFACE_AGE=0
 BINARY_AGE=0
-EXTRA_VERSION=52
+EXTRA_VERSION=53
 EXTRA_RELEASE=
 EXTRA_GTK2_VERSION=
 
index b60314c9c3496664c9163aff19a6a08759cc6d31..b44cb70278fc31325572f092497201346401a895 100644 (file)
@@ -410,10 +410,18 @@ static gint imap_cmd_close      (IMAPSession    *session);
 
 static gint imap_cmd_ok                (IMAPSession    *session,
                                 GPtrArray      *argbuf);
+static gint imap_cmd_ok_block  (IMAPSession    *session,
+                                GPtrArray      *argbuf);
 static void imap_gen_send      (IMAPSession    *session,
                                 const gchar    *format, ...);
 static gint imap_gen_recv      (IMAPSession    *session,
                                 gchar         **ret);
+static gint imap_gen_recv_block        (IMAPSession    *session,
+                                gchar         **ret);
+static gint imap_gen_recv_with_block   
+                               (IMAPSession    *session,
+                                gchar         **ret,
+                                gboolean        block);
 
 /* misc utility functions */
 static gchar *strchr_cpy                       (const gchar    *src,
@@ -3064,36 +3072,53 @@ static gint imap_cmd_search(IMAPSession *session, const gchar *criteria,
        return IMAP_SUCCESS;
 }
 
-static gint imap_cmd_fetch(IMAPSession *session, guint32 uid, 
-                          const gchar *filename)
+typedef struct _fetch_data {
+       IMAPSession *session;
+       guint32 uid;
+       const gchar *filename;
+       gboolean done;
+} fetch_data;
+
+static void *imap_cmd_fetch_thread(void *data)
 {
+       fetch_data *stuff = (fetch_data *)data;
+       IMAPSession *session = stuff->session;
+       guint32 uid = stuff->uid;
+       const gchar *filename = stuff->filename;
+       
        gint ok;
        gchar *buf = NULL;
        gchar *cur_pos;
        gchar size_str[32];
        glong size_num;
 
-       g_return_val_if_fail(filename != NULL, IMAP_ERROR);
+       if (filename == NULL) {
+               stuff->done = TRUE;
+               return GINT_TO_POINTER(IMAP_ERROR);
+       }
 
        imap_gen_send(session, "UID FETCH %d BODY.PEEK[]", uid);
 
-       while ((ok = imap_gen_recv(session, &buf)) == IMAP_SUCCESS) {
+       while ((ok = imap_gen_recv_block(session, &buf)) == IMAP_SUCCESS) {
                if (buf[0] != '*' || buf[1] != ' ') {
                        g_free(buf);
-                       return IMAP_ERROR;
+                       stuff->done = TRUE;
+                       return GINT_TO_POINTER(IMAP_ERROR);
                }
                if (strstr(buf, "FETCH") != NULL) break;
                g_free(buf);
        }
        if (ok != IMAP_SUCCESS) {
                g_free(buf);
-               return ok;
+               stuff->done = TRUE;
+               return GINT_TO_POINTER(ok);
        }
 
 #define RETURN_ERROR_IF_FAIL(cond)     \
        if (!(cond)) {                  \
                g_free(buf);            \
-               return IMAP_ERROR;      \
+               stuff->done = TRUE;     \
+               return GINT_TO_POINTER(IMAP_ERROR);     \
        }
 
        cur_pos = strchr(buf, '{');
@@ -3110,23 +3135,64 @@ static gint imap_cmd_fetch(IMAPSession *session, guint32 uid,
        g_free(buf);
 
        if (recv_bytes_write_to_file(SESSION(session)->sock,
-                                    size_num, filename) != 0)
-               return IMAP_ERROR;
-
-       if (imap_gen_recv(session, &buf) != IMAP_SUCCESS) {
+                                    size_num, filename) != 0) {
+               stuff->done = TRUE;
+               return GINT_TO_POINTER(IMAP_ERROR);
+       }
+       if (imap_gen_recv_block(session, &buf) != IMAP_SUCCESS) {
                g_free(buf);
-               return IMAP_ERROR;
+               stuff->done = TRUE;
+               return GINT_TO_POINTER(IMAP_ERROR);
        }
 
        if (buf[0] == '\0' || buf[strlen(buf) - 1] != ')') {
                g_free(buf);
-               return IMAP_ERROR;
+               stuff->done = TRUE;
+               return GINT_TO_POINTER(IMAP_ERROR);
        }
        g_free(buf);
 
-       ok = imap_cmd_ok(session, NULL);
+       ok = imap_cmd_ok_block(session, NULL);
 
-       return ok;
+       stuff->done = TRUE;
+       return GINT_TO_POINTER(ok);
+}
+
+static gint imap_cmd_fetch(IMAPSession *session, guint32 uid,
+                               const gchar *filename)
+{
+       fetch_data *data = g_new0(fetch_data, 1);
+       int result = 0;
+       void *tmp;
+#ifdef USE_PTHREAD
+       pthread_t pt;
+#endif
+       data->done = FALSE;
+       data->session = session;
+       data->uid = uid;
+       data->filename = filename;
+#if (defined USE_PTHREAD && defined __GLIBC__ && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3)))
+       if (pthread_create(&pt, PTHREAD_CREATE_JOINABLE,
+                       imap_cmd_fetch_thread, data) != 0) {
+               result = GPOINTER_TO_INT(imap_cmd_fetch_thread(data));
+               g_free(data);
+               return result;
+       }
+       debug_print("waiting for imap_cmd_fetch_thread...\n");
+       while(!data->done) {
+               /* don't let the interface freeze while waiting */
+               sylpheed_do_idle();
+       }
+       debug_print("imap_cmd_fetch_thread done\n");
+
+       /* get the thread's return value and clean its resources */
+       pthread_join(pt, &tmp);
+       result = GPOINTER_TO_INT(tmp);
+#else
+       result = GPOINTER_TO_INT(imap_cmd_fetch_thread(data));
+#endif
+       g_free(data);
+       return result;
 }
 
 static gint imap_cmd_append(IMAPSession *session, const gchar *destfolder,
@@ -3374,14 +3440,14 @@ static gint imap_cmd_close(IMAPSession *session)
        return ok;
 }
 
-static gint imap_cmd_ok(IMAPSession *session, GPtrArray *argbuf)
+static gint imap_cmd_ok_with_block(IMAPSession *session, GPtrArray *argbuf, gboolean block)
 {
        gint ok = IMAP_SUCCESS;
        gchar *buf;
        gint cmd_num;
        gchar *data;
 
-       while ((ok = imap_gen_recv(session, &buf))
+       while ((ok = imap_gen_recv_with_block(session, &buf, block))
               == IMAP_SUCCESS) {
                /* make sure data is long enough for any substring of buf */
                data = alloca(strlen(buf) + 1);
@@ -3421,7 +3487,14 @@ static gint imap_cmd_ok(IMAPSession *session, GPtrArray *argbuf)
 
        return ok;
 }
-
+static gint imap_cmd_ok(IMAPSession *session, GPtrArray *argbuf)
+{
+       return imap_cmd_ok_with_block(session, argbuf, FALSE);
+}
+static gint imap_cmd_ok_block(IMAPSession *session, GPtrArray *argbuf)
+{
+       return imap_cmd_ok_with_block(session, argbuf, TRUE);
+}
 static void imap_gen_send(IMAPSession *session, const gchar *format, ...)
 {
        gchar *buf;
@@ -3447,11 +3520,15 @@ static void imap_gen_send(IMAPSession *session, const gchar *format, ...)
        g_free(buf);
 }
 
-static gint imap_gen_recv(IMAPSession *session, gchar **ret)
+static gint imap_gen_recv_with_block(IMAPSession *session, gchar **ret, gboolean block)
 {
-       if ((*ret = imap_getline(SESSION(session)->sock)) == NULL)
-               return IMAP_SOCKET;
-
+       if (!block) {
+               if ((*ret = imap_getline(SESSION(session)->sock)) == NULL)
+                       return IMAP_SOCKET;
+       } else {
+               if ((*ret = sock_getline(SESSION(session)->sock)) == NULL)
+                       return IMAP_SOCKET;
+       }
        strretchomp(*ret);
 
        log_print("IMAP4< %s\n", *ret);
@@ -3461,7 +3538,15 @@ static gint imap_gen_recv(IMAPSession *session, gchar **ret)
        return IMAP_SUCCESS;
 }
 
+static gint imap_gen_recv_block(IMAPSession *session, gchar **ret)
+{
+       return imap_gen_recv_with_block(session, ret, TRUE);
+}
 
+static gint imap_gen_recv(IMAPSession *session, gchar **ret)
+{
+       return imap_gen_recv_with_block(session, ret, FALSE);
+}
 /* misc utility functions */
 
 static gchar *strchr_cpy(const gchar *src, gchar ch, gchar *dest, gint len)