Changeset 2444

Show
Ignore:
Timestamp:
07/10/07 16:29:30
Author:
pvanhoof
Message:

Some major changes about the initialization and account-connecting behaviour

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/ChangeLog

    r2443 r2444  
     12007-07-10  Philip Van Hoof  <pvanhoof@gnome.org> 
     2 
     3        * Made the connecting of accounts more finegrained (once more) 
     4        * Introduced the required tny_session_camel_set_initialized API 
     5 
     6        * This was a major change in behaviour when connecting and at 
     7        initialization 
     8        * This was a major API change 
     9 
    1102007-07-10  Murray Cumming  <murrayc@murrayc.com> 
    211 
  • trunk/libtinymail-camel/tny-camel-account.c

    r2437 r2444  
    148148                        camel_exception_clear (&ex); 
    149149 
    150                 camel_service_connect (apriv->service, &ex); 
     150                camel_service_connect (apriv->service, apriv->ex); 
    151151                if (apriv->service->reconnection) 
    152152                { 
    153                         if (!camel_exception_is_set (&ex)) 
     153                        if (!camel_exception_is_set (apriv->ex)) 
    154154                                apriv->service->reconnection (apriv->service, TRUE, apriv->service->data); 
    155155                        else 
  • trunk/libtinymail-camel/tny-session-camel-priv.h

    r2423 r2444  
    1717        GMutex *conlock, *queue_lock; 
    1818        GThread *conthread; 
     19        gboolean stop_now, initialized; 
    1920        gboolean is_inuse, background_thread_running; 
    2021        GList *regged_queues; 
  • trunk/libtinymail-camel/tny-session-camel.c

    r2443 r2444  
    113113 
    114114        if (cancel || retval == NULL) { 
     115 
     116                GError *err = NULL; 
     117 
     118                _tny_camel_account_set_online (TNY_CAMEL_ACCOUNT (account), FALSE, &err); 
     119 
     120                if (err) 
     121                        g_error_free (err); 
     122 
    115123                camel_exception_set (ex, CAMEL_EXCEPTION_USER_CANCEL, 
    116124                        _("You cancelled when you had to enter a password")); 
     
    375383        priv = instance->priv; 
    376384 
     385        priv->initialized = FALSE; 
     386        priv->stop_now = FALSE; 
    377387        priv->regged_queues = NULL; 
    378388        priv->background_thread_running = FALSE; 
     
    394404} 
    395405 
    396 void  
    397 _tny_session_camel_add_account (TnySessionCamel *self, TnyCamelAccount *account) 
    398 { 
    399         TnyCamelAccountPriv *apriv = TNY_CAMEL_ACCOUNT_GET_PRIVATE (account); 
    400         TnySessionCamelPriv *priv = self->priv; 
    401  
    402         if (apriv->cache_location) 
    403                 g_free (apriv->cache_location); 
    404         apriv->cache_location = g_strdup (priv->camel_dir); 
    405  
    406         priv->current_accounts = g_list_prepend (priv->current_accounts, account); 
    407 } 
    408  
    409 void  
    410 _tny_session_camel_forget_account (TnySessionCamel *self, TnyCamelAccount *account) 
    411 { 
    412         TnyCamelAccountPriv *apriv = TNY_CAMEL_ACCOUNT_GET_PRIVATE (account); 
    413         TnySessionCamelPriv *priv = self->priv; 
    414  
    415         if (apriv->cache_location) 
    416                 g_free (apriv->cache_location); 
    417         apriv->cache_location = NULL; 
    418  
    419         priv->current_accounts = g_list_remove (priv->current_accounts, account); 
    420  
    421         return; 
    422 } 
    423  
    424406typedef struct 
    425407{ 
     
    429411        TnySessionCamelPriv *priv; 
    430412} BackgroundConnectInfo; 
     413 
     414static gboolean 
     415background_connect_idle (gpointer data) 
     416{ 
     417        BackgroundConnectInfo *info = data; 
     418        TnySessionCamel *self = info->user_data; 
     419        TnySessionCamelPriv *priv = self->priv; 
     420 
     421        if (priv->account_store) 
     422        { 
     423                g_signal_emit (priv->account_store,  
     424                        tny_account_store_signals [TNY_ACCOUNT_STORE_ACCOUNTS_RELOADED], 0); 
     425                g_signal_emit (priv->account_store, 
     426                        tny_account_store_signals [TNY_ACCOUNT_STORE_CONNECTING_FINISHED], 0); 
     427        } 
     428 
     429        return FALSE; 
     430} 
     431 
     432 
     433 
     434static void 
     435background_connect_destroy (gpointer data) 
     436{ 
     437        BackgroundConnectInfo *info = data; 
     438 
     439        g_object_unref (G_OBJECT (info->device)); 
     440        g_slice_free (BackgroundConnectInfo, data); 
     441 
     442        return; 
     443} 
     444 
     445 
    431446 
    432447static void 
     
    455470                apriv->is_connecting = TRUE; 
    456471 
    457                 _tny_camel_account_try_connect (account, info->online, &err); 
     472                _tny_camel_account_try_connect (account, FALSE /* info->online */, &err); 
    458473 
    459474                if (err == NULL) 
     
    480495} 
    481496 
    482  
    483 static void 
    484 background_connect_destroy (gpointer data) 
    485 
    486         BackgroundConnectInfo *info = data; 
    487  
    488         g_object_unref (G_OBJECT (info->device)); 
    489         g_slice_free (BackgroundConnectInfo, data); 
    490  
    491         return; 
    492 
    493  
    494 static gboolean 
    495 background_connect_idle (gpointer data) 
    496 
    497         BackgroundConnectInfo *info = data; 
    498         TnySessionCamel *self = info->user_data; 
    499         TnySessionCamelPriv *priv = self->priv; 
    500  
    501         if (priv->account_store) 
    502         { 
    503                 g_signal_emit (priv->account_store,  
    504                         tny_account_store_signals [TNY_ACCOUNT_STORE_ACCOUNTS_RELOADED], 0); 
    505                 g_signal_emit (priv->account_store, 
    506                         tny_account_store_signals [TNY_ACCOUNT_STORE_CONNECTING_FINISHED], 0); 
    507         } 
    508  
    509         return FALSE; 
    510 
     497typedef struct { 
     498        BackgroundConnectInfo *info; 
     499        TnyCamelAccount *account; 
     500} AccGoOnlineInfo; 
     501 
     502static gpointer 
     503account_go_online (gpointer data) 
     504
     505        AccGoOnlineInfo *ainfo = (AccGoOnlineInfo *) data; 
     506        TnySessionCamel *self = ainfo->info->user_data; 
     507        TnySessionCamelPriv *priv = self->priv; 
     508 
     509        priv->is_connecting = TRUE; 
     510 
     511        foreach_account_set_connectivity (ainfo->account, ainfo->info); 
     512 
     513        priv->is_connecting = FALSE; 
     514 
     515        camel_object_unref (ainfo->info->user_data); 
     516        g_object_unref (ainfo->account); 
     517 
     518        g_idle_add_full (G_PRIORITY_HIGH,  
     519                background_connect_idle,  
     520                ainfo->info, background_connect_destroy); 
     521 
     522        g_slice_free (AccGoOnlineInfo, ainfo); 
     523        /* ainfo->info is freed in the background_connect_destroy */ 
     524 
     525        g_thread_exit (NULL); 
     526        return NULL; 
     527
     528 
     529 
     530void  
     531_tny_session_camel_add_account (TnySessionCamel *self, TnyCamelAccount *account) 
     532
     533        TnyCamelAccountPriv *apriv = TNY_CAMEL_ACCOUNT_GET_PRIVATE (account); 
     534        TnySessionCamelPriv *priv = self->priv; 
     535        gboolean doit = FALSE; 
     536 
     537        if (priv->initialized) 
     538        { 
     539                g_mutex_lock (priv->conlock); 
     540                doit = (priv->conthread == NULL); 
     541                g_mutex_unlock (priv->conlock); 
     542        } 
     543 
     544        if (apriv->cache_location) 
     545                g_free (apriv->cache_location); 
     546        apriv->cache_location = g_strdup (priv->camel_dir); 
     547        priv->current_accounts = g_list_prepend (priv->current_accounts, account); 
     548 
     549        if (priv->initialized && !priv->background_thread_running && doit) 
     550        { 
     551                if (priv->device && TNY_IS_DEVICE (priv->device)) 
     552                { 
     553                        AccGoOnlineInfo *ainfo = g_slice_new (AccGoOnlineInfo); 
     554                        ainfo->info = g_slice_new (BackgroundConnectInfo); 
     555 
     556                        ainfo->info->online = tny_device_is_online (priv->device); 
     557                        ainfo->info->as_thread = TRUE; 
     558                        ainfo->info->priv = priv; 
     559 
     560                        ainfo->info->device = g_object_ref (priv->device); 
     561                        camel_object_ref (self); 
     562                        ainfo->info->user_data = self; 
     563                        ainfo->account = TNY_CAMEL_ACCOUNT (g_object_ref (account)); 
     564 
     565                        camel_session_set_online ((CamelSession *) self, TRUE);  
     566 
     567                        if (priv->account_store) 
     568                        { 
     569                                g_signal_emit (priv->account_store, 
     570                                        tny_account_store_signals [TNY_ACCOUNT_STORE_CONNECTING_STARTED], 0); 
     571                        } 
     572 
     573                        g_mutex_lock (priv->conlock); 
     574                        if (priv->conthread) 
     575                                g_thread_join (priv->conthread); 
     576                        priv->conthread = g_thread_create (account_go_online, ainfo, TRUE, NULL); 
     577                        g_mutex_unlock (priv->conlock); 
     578                } 
     579        } 
     580
     581 
     582void  
     583_tny_session_camel_forget_account (TnySessionCamel *self, TnyCamelAccount *account) 
     584
     585        TnyCamelAccountPriv *apriv = TNY_CAMEL_ACCOUNT_GET_PRIVATE (account); 
     586        TnySessionCamelPriv *priv = self->priv; 
     587 
     588        if (apriv->cache_location) 
     589                g_free (apriv->cache_location); 
     590        apriv->cache_location = NULL; 
     591 
     592        priv->current_accounts = g_list_remove (priv->current_accounts, account); 
     593 
     594        return; 
     595
     596 
     597 
     598 
    511599 
    512600static gpointer  
     
    524612        priv->background_thread_running = TRUE; 
    525613 
    526         g_mutex_lock (priv->queue_lock); 
    527         /*g_list_foreach (priv->regged_queues, (GFunc)  
    528                 tny_camel_send_queue_join_worker, NULL);*/ 
    529         g_mutex_unlock (priv->queue_lock); 
     614        if (priv->stop_now)  
     615                goto stop_now; 
    530616 
    531617        priv->is_connecting = TRUE; 
    532618 
    533         if (priv->current_accounts && priv->prev_constat != info->online && priv->account_store) { 
    534                 g_list_foreach (priv->current_accounts,  
    535                         foreach_account_set_connectivity, info); 
    536         } 
     619        if (priv->current_accounts && priv->prev_constat != info->online && priv->account_store)  
     620        { 
     621                GList *copy = priv->current_accounts; 
     622                while (copy) 
     623                { 
     624                        if (priv->stop_now)  
     625                                goto stop_now; 
     626                        foreach_account_set_connectivity (copy->data, info); 
     627                        if (priv->stop_now)  
     628                                goto stop_now; 
     629                        copy = g_list_next (copy); 
     630                } 
     631        } 
     632 
     633stop_now: 
    537634 
    538635        priv->is_connecting = FALSE; 
     
    544641        priv->prev_constat = info->online; 
    545642 
    546         g_mutex_lock (priv->conlock); 
    547643        priv->conthread = NULL; 
    548         g_mutex_unlock (priv->conlock); 
    549  
    550644        priv->background_thread_running = FALSE; 
     645 
     646        priv->stop_now = FALSE; 
    551647 
    552648        g_thread_exit (NULL); 
     
    555651 
    556652static gboolean 
    557 delayed_background_connect_thread (gpointer data) 
     653do_background_connect_thread (gpointer data) 
    558654{ 
    559655        BackgroundConnectInfo *info = data; 
     656        TnySessionCamel *self = info->user_data; 
     657        TnySessionCamelPriv *priv = self->priv; 
    560658 
    561659        g_mutex_lock (info->priv->conlock); 
     
    567665 
    568666static void 
    569 connection_changed (TnyDevice *device, gboolean online, gpointer user_data) 
     667tny_session_camel_connection_changed (TnyDevice *device, gboolean online, gpointer user_data) 
    570668{ 
    571669        TnySessionCamel *self = user_data; 
     
    593691 
    594692 
    595         if (priv->async_connect) { 
     693        if (priv->async_connect)  
     694        { 
     695                gboolean doit = FALSE; 
     696 
    596697                info->as_thread = TRUE; 
    597698 
    598699                g_mutex_lock (priv->conlock); 
    599                 if (priv->conthread) 
    600                         g_timeout_add (5000, delayed_background_connect_thread, info);  
    601                 else 
    602                         priv->conthread = g_thread_create (background_connect_thread, info, TRUE, NULL); 
     700                doit = (priv->conthread == NULL); 
    603701                g_mutex_unlock (priv->conlock); 
     702 
     703                if (doit) 
     704                        do_background_connect_thread (info); 
    604705 
    605706        } else { 
     
    628729} 
    629730 
    630 /* 
    631 static gboolean 
    632 emit_accounts_reloaded (gpointer user_data) 
    633 
    634         g_signal_emit (G_OBJECT (user_data), 
    635                 tny_account_store_signals [TNY_ACCOUNT_STORE_ACCOUNTS_RELOADED], 0); 
    636         return FALSE; 
    637 
    638 */ 
    639  
    640 static gboolean 
    641 after_one_second (gpointer data) 
    642 
    643         TnySessionCamel *self = data; 
    644         TnySessionCamelPriv *priv = self->priv; 
    645  
    646         if (priv->device && TNY_IS_DEVICE (priv->device) && tny_device_is_online (priv->device)) 
    647                 connection_changed (priv->device, TRUE, self); 
    648  
    649         camel_object_unref (self); 
    650  
    651         return FALSE; 
     731/** 
     732 * tny_session_camel_set_initialized: 
     733 * @self: the #TnySessionCamel instance 
     734 * 
     735 * This method must be called one the initial accounts are created in your 
     736 * #TnyAccountStore implementation. 
     737 **/ 
     738void  
     739tny_session_camel_set_initialized (TnySessionCamel *self) 
     740
     741        TnySessionCamelPriv *priv = self->priv; 
     742        TnyDevice *device = NULL; 
     743 
     744        if (priv->initialized) 
     745                return; 
     746 
     747        device = priv->device; 
     748 
     749        if (!device || !TNY_IS_DEVICE (device)) 
     750        { 
     751                g_critical ("Please use tny_session_camel_set_device " 
     752                        "before tny_session_camel_set_initialized"); 
     753                return; 
     754        } 
     755 
     756        priv->initialized = TRUE; 
     757 
     758        priv->connchanged_signal = g_signal_connect ( 
     759                G_OBJECT (device), "connection_changed", 
     760                G_CALLBACK (tny_session_camel_connection_changed), self); 
     761 
     762        tny_session_camel_connection_changed (device,  
     763                tny_device_is_online (device), self); 
    652764} 
    653765 
     
    673785 
    674786        priv->device = device; 
    675         priv->connchanged_signal = g_signal_connect ( 
    676                         G_OBJECT (device), "connection_changed", 
    677                         G_CALLBACK (connection_changed), self); 
    678  
    679         if (tny_device_is_online (device) == FALSE) 
    680         { 
    681                 priv->prev_constat = TRUE; 
    682                 connection_changed (device, FALSE, self); 
    683         } else { 
    684                 priv->prev_constat = FALSE; 
    685                 camel_object_ref (self); 
    686                 g_timeout_add (5000, after_one_second, self); 
    687         } 
    688787 
    689788        return; 
  • trunk/libtinymail-camel/tny-session-camel.h

    r1521 r2444  
    5555void tny_session_camel_set_async_connecting (TnySessionCamel *self, gboolean enable); 
    5656 
     57void tny_session_camel_set_initialized (TnySessionCamel *self); 
     58 
    5759G_END_DECLS 
    5860 
  • trunk/libtinymail-gnome-desktop/tny-gnome-account-store.c

    r2443 r2444  
    324324                        g_free (proto); 
    325325        } 
     326 
     327        tny_session_camel_set_initialized (priv->session); 
    326328} 
    327329 
  • trunk/libtinymail-gnome-desktop/tny-gnome-device.c

    r2384 r2444  
    167167} 
    168168 
     169/* #define IMMEDIATE_ONLINE_TEST */ 
    169170 
    170171static void 
     
    177178        priv->forced = FALSE; 
    178179 
     180#ifdef IMMEDIATE_ONLINE_TEST 
     181        priv->fset = TRUE; 
     182        priv->forced = TRUE; 
     183#endif 
     184 
    179185#ifdef GNOME 
    180186        priv->invnm = FALSE; 
    181187        priv->nm_ctx = libnm_glib_init (); 
     188#ifndef IMMEDIATE_ONLINE_TEST 
    182189        priv->callback_id = libnm_glib_register_callback  
    183190                (priv->nm_ctx, nm_callback, self, NULL); 
     191#endif 
    184192#endif 
    185193 
  • trunk/libtinymail-gpe/tny-gpe-account-store.c

    r2443 r2444  
    323323                if (mech) 
    324324                        g_free (mech); 
    325  
    326         } 
     325        } 
     326 
     327        tny_session_camel_set_initialized (priv->session); 
     328 
    327329} 
    328330 
  • trunk/libtinymail-maemo/tny-maemo-account-store.c

    r2443 r2444  
    328328                if (mech) 
    329329                        g_free (mech); 
    330  
    331         } 
     330        } 
     331 
     332        tny_session_camel_set_initialized (priv->session); 
    332333} 
    333334 
  • trunk/libtinymail-olpc/tny-olpc-account-store.c

    r2443 r2444  
    280280        } 
    281281        g_dir_close (dir); 
     282 
     283        tny_session_camel_set_initialized (priv->session); 
    282284} 
    283285 
  • trunk/libtinymail/tny-account-store.c

    r2443 r2444  
    273273 * register the created accounts with a #TnySessionCamel instance using the  
    274274 * libtinymail-camel specific tny_session_camel_set_current_accounts API. 
     275 * 
     276 * If you use the #TnySessionCamel to register accounts, you must after  
     277 * registering your last initial account call the tny_session_camel_set_initialized 
     278 * API. 
    275279 * 
    276280 * The implementation must fillup @list with available accounts. Note that if 
     
    340344 *    } 
    341345 *    g_object_unref (G_OBJECT (iter)); 
     346 *    tny_session_camel_set_initialized (session); 
    342347 * } 
    343348 * </programlisting></informalexample>