fix indent (broken in last commit)
[claws.git] / src / common / session.c
index 21213f7e9ac965929ddc7585428e3845d7031f30..959c7a28e6785e6956dd765c893df4476fe371ad 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2009 Hiroyuki Yamamoto and the Claws Mail team
+ * Copyright (C) 1999-2012 Hiroyuki Yamamoto and the Claws Mail team
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -19,6 +19,7 @@
 
 #ifdef HAVE_CONFIG_H
 #  include "config.h"
+#include "claws-features.h"
 #endif
 
 #include "defs.h"
@@ -99,6 +100,8 @@ void session_init(Session *session, const void *prefs_account, gboolean is_smtp)
        session->data = NULL;
        session->account = prefs_account;
        session->is_smtp = is_smtp;
+
+       session->ping_tag = -1;
 }
 
 /*!
@@ -162,7 +165,11 @@ static gint session_connect_cb(SockInfo *sock, gpointer data)
        session->sock = sock;
        sock->account = session->account;
        sock->is_smtp = session->is_smtp;
+       sock->ssl_cert_auto_accept = session->ssl_cert_auto_accept;
+
 #ifdef USE_GNUTLS
+       sock->gnutls_priority = session->gnutls_priority;
+
        if (session->ssl_type == SSL_TUNNEL) {
                sock_set_nonblocking_mode(sock, FALSE);
                if (!ssl_init_socket(sock)) {
@@ -214,6 +221,8 @@ void session_destroy(Session *session)
        cm_return_if_fail(session != NULL);
        cm_return_if_fail(session->destroy != NULL);
 
+       session_register_ping(session, NULL);
+
        session_close(session);
        session->destroy(session);
        g_free(session->server);
@@ -221,19 +230,28 @@ void session_destroy(Session *session)
        g_byte_array_free(session->read_data_buf, TRUE);
        g_free(session->read_data_terminator);
        g_free(session->write_buf);
+#ifdef USE_GNUTLS
+       g_free(session->gnutls_priority);
+#endif
 
        debug_print("session (%p): destroyed\n", session);
 
        g_free(session);
 }
 
-gboolean session_is_connected(Session *session)
+gboolean session_is_running(Session *session)
 {
        return (session->state == SESSION_READY ||
                session->state == SESSION_SEND ||
                session->state == SESSION_RECV);
 }
 
+gboolean session_is_connected(Session *session)
+{
+       return (session->state == SESSION_SEND ||
+               session->state == SESSION_RECV);
+}
+
 void session_set_access_time(Session *session)
 {
        session->last_access_time = time(NULL);
@@ -246,14 +264,12 @@ void session_set_timeout(Session *session, guint interval)
 
        session->timeout_interval = interval;
        if (interval > 0) {
-#if GLIB_CHECK_VERSION(2,14,0)
                if (interval % 1000 == 0)
                        session->timeout_tag =
                                g_timeout_add_seconds(interval/1000, session_timeout_cb, session);
                else
-#endif
-               session->timeout_tag =
-                       g_timeout_add(interval, session_timeout_cb, session);
+                       session->timeout_tag =
+                               g_timeout_add(interval, session_timeout_cb, session);
        } else
                session->timeout_tag = 0;
 }
@@ -357,6 +373,8 @@ gint session_start_tls(Session *session)
 
        nb_mode = sock_is_nonblocking_mode(session->sock);
 
+       session->sock->ssl_cert_auto_accept = session->ssl_cert_auto_accept;
+
        if (nb_mode)
                sock_set_nonblocking_mode(session->sock, FALSE);
 
@@ -512,9 +530,10 @@ static gboolean session_read_msg_cb(SockInfo *source, GIOCondition condition,
        session_set_timeout(session, session->timeout_interval);
 
        if (session->read_buf_len == 0) {
-               gint read_len;
+               gint read_len = -1;
 
-               read_len = sock_read(session->sock, session->read_buf,
+               if (session->sock)
+                       read_len = sock_read(session->sock, session->read_buf,
                                     SESSION_BUFFSIZE - 1);
 
                if (read_len == -1 && session->state == SESSION_DISCONNECTED) {
@@ -860,3 +879,16 @@ static gboolean session_write_data_cb(SockInfo *source,
 
        return FALSE;
 }
+
+void session_register_ping(Session *session, gboolean (*ping_cb)(gpointer data))
+{
+       if (!session)
+               return;
+       if (session->ping_tag > -1)
+               g_source_remove(session->ping_tag);
+
+       session->ping_tag = -1;
+
+       if (ping_cb != NULL)
+               session->ping_tag = g_timeout_add_seconds(60, ping_cb, session);
+}