Changeset 2643

Show
Ignore:
Timestamp:
08/15/07 14:38:47
Author:
pvanhoof
Message:

2007-08-15 Philip Van Hoof <pvanhoof@gnome.org>

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/ChangeLog

    r2642 r2643  
     12007-08-15  Philip Van Hoof  <pvanhoof@gnome.org> 
     2 
     3        * Improved the TnyMaemoConicDevice a bit 
     4 
    152007-08-15  Murray Cumming  <murrayc@murrayc.com> 
    26 
  • trunk/libtinymail-maemo/tny-maemo-conic-device.c

    r2642 r2643  
    3434 
    3535#ifdef MAEMO_CONIC_DUMMY 
     36 
    3637/* #include "coniciap-private.h" 
    37  * This is not installed, so we predeclare  
    38  * the struct instead. 
    39  * Of course, this is a hack and could break if the private API  
    40  * changes. 
    41  * It would be better for libconic to support scratchbox. 
    42  */ 
     38 * This is not installed, so we predeclare the struct instead. Of course, this  
     39 * is a hack and could break if the private API changes. It would be better for  
     40 * libconic to support scratchbox. */ 
     41 
    4342struct _ConIcIap  
    4443{ 
     
    6160typedef struct { 
    6261        ConIcConnection *cnx; 
    63         gboolean        is_online; 
    64         gchar     *iap; 
    65         gboolean        forced; /* Whether the is_online value is forced rather than real. */ 
    66          
     62        gboolean is_online; 
     63        gchar *iap; 
     64        gboolean forced; /* Whether the is_online value is forced rather than real. */ 
    6765        /* When non-NULL, we are waiting for the success or failure signal. */ 
    6866        GMainLoop *loop; 
    69          
    70 #ifdef MAEMO_CONIC_DUMMY         
     67#ifdef MAEMO_CONIC_DUMMY 
    7168        gint dummy_env_check_timeout; 
    7269#endif /* MAEMO_CONIC_DUMMY */ 
     
    7572#define TNY_MAEMO_CONIC_DEVICE_GET_PRIVATE(o)   \ 
    7673        (G_TYPE_INSTANCE_GET_PRIVATE ((o), TNY_TYPE_MAEMO_CONIC_DEVICE, TnyMaemoConicDevicePriv)) 
    77  
    7874 
    7975typedef struct { 
     
    8985        /* We lock the gdk thread because tinymail wants implementations to do this  
    9086         * before emitting _any_ signals. 
    91          * See http://www.tinymail.org/trac/tinymail/wiki/HowTnyLockable 
    92          */ 
     87         * See http://www.tinymail.org/trac/tinymail/wiki/HowTnyLockable */ 
     88 
    9389        gdk_threads_enter (); 
    9490        g_signal_emit (info->self, tny_device_signals [TNY_DEVICE_CONNECTION_CHANGED], 
     
    117113         * about this - it would be nice if libconic documented that). 
    118114         * But if there is no mainloop, then just emit it, as tinymail  
    119          * requires when there is no mainloop: 
    120          */ 
     115         * requires when there is no mainloop: */ 
     116 
    121117         /* TODO: We have no way to check for this now, and  
    122118          * at this time it's not even clear whether tinymail should/can really  
    123           * demand this. murrayc. 15th Aug. 2007. 
    124           */ 
     119          * demand this. murrayc. 15th Aug. 2007. */ 
     120 
    125121        if (TRUE) /* TODO: But NULL is not allowed here: g_main_loop_is_running (NULL)) */ 
    126122        { 
    127123                /* Emit it in an idle handler: */ 
    128124                EmitStatusInfo *info = g_slice_new (EmitStatusInfo); 
    129          
    130125                info->self = g_object_ref (self); 
    131126                info->status = status; 
    132          
    133127                g_idle_add_full (G_PRIORITY_DEFAULT, conic_emit_status_idle, 
    134128                        info, conic_emit_status_destroy); 
     129 
    135130        } else { 
    136131                /* Emit it directly: */ 
    137                  
    138                 /* We lock the gdk thread because tinymail wants implementations to do this  
    139                  * before emitting _any_ signals. 
    140                  * See http://www.tinymail.org/trac/tinymail/wiki/HowTnyLockable 
    141                  */ 
     132 
     133                /* We lock the gdk thread because tinymail wants implementations 
     134                 * to do this before emitting _any_ signals. 
     135                 * See http://www.tinymail.org/trac/tinymail/wiki/HowTnyLockable */ 
     136 
    142137                gdk_threads_enter (); 
    143138                g_signal_emit (self, tny_device_signals [TNY_DEVICE_CONNECTION_CHANGED], 
     
    155150        TnyMaemoConicDevicePriv *priv; 
    156151        gboolean status_before = FALSE; 
    157          
     152 
    158153        g_return_if_fail (TNY_IS_DEVICE(device)); 
    159154        self = TNY_MAEMO_CONIC_DEVICE (device); 
     
    161156 
    162157        status_before = tny_maemo_conic_device_is_online (device); 
    163  
    164158        priv->forced = FALSE; 
    165159 
    166160        if (status_before != tny_maemo_conic_device_is_online (device)) 
    167161                conic_emit_status (device, !status_before); 
     162 
     163        return; 
    168164} 
    169165 
     
    171167 
    172168static void  
    173 stop_loop(TnyMaemoConicDevice *self) 
    174 
    175         TnyMaemoConicDevicePriv *priv 
    176                 = TNY_MAEMO_CONIC_DEVICE_GET_PRIVATE (self); 
    177  
    178         if (priv->loop) { 
     169stop_loop (TnyMaemoConicDevice *self) 
     170
     171        TnyMaemoConicDevicePriv *priv = TNY_MAEMO_CONIC_DEVICE_GET_PRIVATE (self); 
     172        if (priv->loop) 
    179173                g_main_loop_quit (priv->loop); 
    180         } 
     174        return; 
    181175} 
    182176 
     
    184178on_connection_event (ConIcConnection *cnx, ConIcConnectionEvent *event, gpointer user_data) 
    185179{ 
    186         TnyMaemoConicDevice *device;  
    187         TnyMaemoConicDevicePriv *priv
     180        TnyMaemoConicDevice *device = TNY_MAEMO_CONIC_DEVICE (user_data); 
     181        TnyMaemoConicDevicePriv *priv = TNY_MAEMO_CONIC_DEVICE_GET_PRIVATE (device)
    188182        gboolean is_online = FALSE; 
     183        gboolean emit = FALSE; 
     184 
     185        /* Don't emit nor make any changes in case of forced state */ 
     186        if (priv->forced) 
     187                return; 
    189188 
    190189        g_return_if_fail (CON_IC_IS_CONNECTION(cnx)); 
    191         g_return_if_fail (user_data); 
    192  
    193         device = TNY_MAEMO_CONIC_DEVICE(user_data); 
    194         priv   = TNY_MAEMO_CONIC_DEVICE_GET_PRIVATE (device); 
    195  
     190 
     191#ifdef DEBUG 
    196192        g_message (__FUNCTION__); 
    197  
    198         switch (con_ic_connection_event_get_error(event)) { 
    199         case CON_IC_CONNECTION_ERROR_NONE: 
    200                 break; 
    201         case CON_IC_CONNECTION_ERROR_INVALID_IAP: 
    202                 g_warning ("conic: IAP is invalid"); 
    203                 break; 
    204         case CON_IC_CONNECTION_ERROR_CONNECTION_FAILED: 
    205                 g_warning ("conic: connection failed"); 
    206                 break; 
    207         case CON_IC_CONNECTION_ERROR_USER_CANCELED: 
    208                 g_warning ("conic: user cancelled"); 
    209                 break; 
    210         default: 
    211                 g_return_if_reached (); 
    212         } 
    213          
    214         switch (con_ic_connection_event_get_status(event)) { 
    215         case CON_IC_STATUS_CONNECTED: 
    216                 priv->iap = g_strdup (con_ic_event_get_iap_id ((ConIcEvent*)(event))); 
    217                 is_online = TRUE; 
    218                  
    219                 /* Stop blocking  
    220                  * tny_maemo_conic_device_connect(), if we are: */ 
    221                 stop_loop (device); 
    222                          
    223                 /* g_message ("new status: CONNECTED (%s)", priv->iap); */ 
    224                 break; 
    225         case CON_IC_STATUS_DISCONNECTED: 
    226                 priv->iap = NULL; 
    227                 is_online = FALSE; 
    228                  
    229                 /* Stop blocking  
    230                  * tny_maemo_conic_device_connect(), if we are: */ 
    231                 stop_loop (device); 
    232                          
    233                 /* g_message ("new status: DISCONNECTED"); */ 
    234                 break; 
    235         case CON_IC_STATUS_DISCONNECTING: 
    236                 is_online = FALSE; 
    237                 /* g_message ("new status: DISCONNECTING"); */ 
    238                 break; 
    239         default: 
    240                 g_return_if_reached ();  
     193#endif 
     194 
     195        switch (con_ic_connection_event_get_error(event))  
     196        { 
     197                case CON_IC_CONNECTION_ERROR_NONE: 
     198                        break; 
     199                case CON_IC_CONNECTION_ERROR_INVALID_IAP: 
     200                        g_warning ("conic: IAP is invalid"); 
     201                        break; 
     202                case CON_IC_CONNECTION_ERROR_CONNECTION_FAILED: 
     203                        g_warning ("conic: connection failed"); 
     204                        break; 
     205                case CON_IC_CONNECTION_ERROR_USER_CANCELED: 
     206                        g_warning ("conic: user cancelled"); 
     207                        break; 
     208                default: 
     209                        g_return_if_reached (); 
     210        } 
     211 
     212        switch (con_ic_connection_event_get_status(event))  
     213        { 
     214                case CON_IC_STATUS_CONNECTED: 
     215                        if (priv->iap) 
     216                                g_free (priv->iap); 
     217                        priv->iap = g_strdup (con_ic_event_get_iap_id ((ConIcEvent*)(event))); 
     218                        if (!priv->is_online) 
     219                                emit = TRUE; 
     220                        is_online = TRUE; 
     221                        /* Stop blocking tny_maemo_conic_device_connect(), if we are: */ 
     222                        stop_loop (device); 
     223                        break; 
     224 
     225                case CON_IC_STATUS_DISCONNECTED: 
     226                        priv->iap = NULL; 
     227                        if (priv->is_online) 
     228                                emit = TRUE; 
     229                        is_online = FALSE; 
     230                        /* Stop blocking tny_maemo_conic_device_connect(), if we are: */ 
     231                        stop_loop (device); 
     232                        break; 
     233 
     234                case CON_IC_STATUS_DISCONNECTING: 
     235                        break; 
     236                default: 
     237                        g_return_if_reached ();  
    241238        } 
    242239 
    243240        priv->is_online = is_online; 
    244         priv->forced = FALSE; /* is_online is now accurate. */ 
     241 
     242#ifdef DEBUG 
    245243        g_message ("DEBUG: %s: emitting signal CONNECTION_CHANGED: %s",  
    246244                   __FUNCTION__, is_online ? "online" : "offline"); 
    247  
    248         conic_emit_status (TNY_DEVICE (device), is_online); 
    249  
     245#endif 
     246 
     247        if (emit) 
     248                conic_emit_status (TNY_DEVICE (device), is_online); 
     249 
     250        return; 
    250251} 
    251252#endif /* MAEMO_CONIC_DUMMY */ 
     
    284285                 * which will be read later: */ 
    285286                gchar *filename = get_dummy_filename (); 
    286                  
     287 
    287288                g_file_set_contents (filename, "debug id0", -1, &error); 
    288289                if(error) { 
     
    291292                        error = NULL; 
    292293                } 
    293                  
     294 
    294295                g_free (filename); 
    295                  
     296 
    296297                return TRUE; 
    297298        } 
     
    319320tny_maemo_conic_device_connect (TnyMaemoConicDevice *self, const gchar* iap_id) 
    320321{ 
    321        #ifdef MAEMO_CONIC_DUMMY  
     322#ifdef MAEMO_CONIC_DUMMY  
    322323        return dummy_con_ic_connection_connect_by_id (self, iap_id); 
    323        #else 
     324#else 
    324325        TnyMaemoConicDevicePriv *priv = NULL; 
    325326        gboolean request_failed = FALSE; 
     
    365366 
    366367        return priv->is_online; 
    367        #endif /* MAEMO_CONIC_DUMMY */ 
     368#endif /* MAEMO_CONIC_DUMMY */ 
    368369} 
    369370 
     
    413414 
    414415#ifdef MAEMO_CONIC_DUMMY 
    415  
    416416static gboolean 
    417417on_dummy_connection_check (gpointer user_data) 
     
    498498        priv = TNY_MAEMO_CONIC_DEVICE_GET_PRIVATE (self); 
    499499 
    500        #ifdef MAEMO_CONIC_DUMMY 
     500#ifdef MAEMO_CONIC_DUMMY 
    501501        on_dummy_connection_check (self); 
    502502        /* Handle the special "none" text: */ 
    503503        if (priv->iap && (strcmp (priv->iap, MAEMO_CONIC_DUMMY_IAP_ID_NONE) == 0)) 
    504504                return NULL; 
    505        #endif 
     505#endif 
    506506 
    507507        return priv->iap; 
     
    525525tny_maemo_conic_device_get_iap (TnyMaemoConicDevice *self, const gchar *iap_id) 
    526526{ 
    527        #ifdef MAEMO_CONIC_DUMMY 
     527#ifdef MAEMO_CONIC_DUMMY 
    528528        ConIcIap *iap = NULL; 
    529        #else 
     529#else 
    530530        TnyMaemoConicDevicePriv *priv = NULL; 
    531        #endif 
    532          
     531#endif 
     532 
    533533        g_return_val_if_fail (TNY_IS_MAEMO_CONIC_DEVICE(self), NULL); 
    534534        g_return_val_if_fail (iap_id, NULL); 
    535535 
    536        #ifdef MAEMO_CONIC_DUMMY  
     536#ifdef MAEMO_CONIC_DUMMY  
    537537        /* Note that we have re-declared the private struct so that we  
    538538         * can do this, which is very bad and fragile: */ 
    539       iap = g_object_new (CON_IC_TYPE_IAP, NULL); 
    540       iap->id = g_strdup(iap_id); 
    541       iap->name = g_strdup_printf("%s name", iap->id); 
    542       return iap; 
    543        #else 
     539      iap = g_object_new (CON_IC_TYPE_IAP, NULL); 
     540      iap->id = g_strdup(iap_id); 
     541      iap->name = g_strdup_printf("%s name", iap->id); 
     542      return iap; 
     543#else 
    544544        priv = TNY_MAEMO_CONIC_DEVICE_GET_PRIVATE (self); 
    545545        g_return_val_if_fail (priv->cnx, NULL); 
     
    551551         */ 
    552552        return con_ic_connection_get_iap (priv->cnx, iap_id); 
    553        #endif 
     553#endif 
    554554} 
    555555 
     
    578578 
    579579#ifdef MAEMO_CONIC_DUMMY 
     580 
    580581        /* libconic does not return a list of connections when running  
    581582         * in scratchbox, though it might do this in future when  
     
    583584         * So we return a fake list so we can exercise funcationality  
    584585         * that uses connections: */ 
    585         for (i = 0; i < 10; ++i) { 
    586              /* con_ic_iap_new (id) would checkc for a gconf dir and  
    587               * fails, though _new() functions should just call g_object_new(). 
    588               * 
    589              gchar *id = g_strdup_printf("debug id%d", i); 
    590              ConIcIap *iap = con_ic_iap_new (id); 
    591              g_free (id); 
    592              */ 
    593               
    594              /* Note that we have re-declared the private struct so that we  
    595               * can do this, which is very bad and fragile: */ 
    596              ConIcIap *iap = g_object_new (CON_IC_TYPE_IAP, NULL); 
    597              iap->id = g_strdup_printf("debug id%d", i); 
    598              iap->name = g_strdup_printf("%s name", iap->id); 
    599  
    600              result = g_slist_append (result, iap);   
    601        
    602          
    603         return result; 
     586        for (i = 0; i < 10; ++i) { 
     587              /* con_ic_iap_new (id) would checkc for a gconf dir and  
     588               * fails, though _new() functions should just call g_object_new(). 
     589               * 
     590              gchar *id = g_strdup_printf("debug id%d", i); 
     591              ConIcIap *iap = con_ic_iap_new (id); 
     592              g_free (id); 
     593              */ 
     594               
     595              /* Note that we have re-declared the private struct so that we  
     596               * can do this, which is very bad and fragile: */ 
     597              ConIcIap *iap = g_object_new (CON_IC_TYPE_IAP, NULL); 
     598              iap->id = g_strdup_printf("debug id%d", i); 
     599              iap->name = g_strdup_printf("%s name", iap->id); 
     600 
     601              result = g_slist_append (result, iap);   
     602       
     603         
     604        return result; 
    604605#else 
    605606        priv = TNY_MAEMO_CONIC_DEVICE_GET_PRIVATE (self); 
     
    667668#ifdef MAEMO_CONIC_DUMMY 
    668669        return; 
    669 #endif /*MAEMO_CONIC_DUMMY*/ 
    670  
     670#else 
    671671        already_offline = !tny_maemo_conic_device_is_online (device); 
    672  
    673672        priv->forced = TRUE; 
    674  
    675673        /* Signal if it changed: */ 
    676674        if (!already_offline) 
    677675                conic_emit_status (device, FALSE); 
     676        return; 
     677#endif 
    678678} 
    679679 
     
    683683        g_return_val_if_fail (TNY_IS_DEVICE(self), FALSE); 
    684684 
    685        #ifdef MAEMO_CONIC_DUMMY 
     685#ifdef MAEMO_CONIC_DUMMY 
    686686        on_dummy_connection_check (self); 
    687        #endif /* MAEMO_CONIC_DUMMY */ 
    688          
     687#endif /* MAEMO_CONIC_DUMMY */ 
     688 
    689689        return TNY_MAEMO_CONIC_DEVICE_GET_PRIVATE (self)->is_online; 
    690690} 
     
    695695{ 
    696696        TnyMaemoConicDevice *self = (TnyMaemoConicDevice *)instance; 
    697  
    698697        TnyMaemoConicDevicePriv *priv = TNY_MAEMO_CONIC_DEVICE_GET_PRIVATE (self); 
     698 
     699        /* We should not have a real is_online, based on what libconic has told us: */ 
     700        priv->forced = FALSE; 
    699701        priv->iap = NULL; 
    700702        /* priv->is_online = FALSE; */ 
    701          
    702         /* TODO: This is a hack that fixes a problem 
    703          * (Our signal is not emitted before the gtk mainloop is started,  
    704          * because we use an idle handler for that and we don't know how to  
    705          * detect when the mainloop has not yet started. 
    706          * The downside is that we now don't know that we are really offline, 
    707          * so we can't ask the user to go online. 
    708          * This must be fixed properly. 
    709          */ 
     703 
     704        /* TODO: This is a hack that fixes a problem: Our signal is not emitted  
     705         * before the gtk mainloop is started, because we use an idle handler  
     706         * for that and we don't know how to detect when the mainloop has not  
     707         * yet started. The downside is that we now don't know that we are really  
     708         * offline, so we can't ask the user to go online. This must be fixed  
     709         * properly. */ 
     710 
    710711        priv->is_online = TRUE;  
    711          
    712         #ifndef MAEMO_CONIC_DUMMY 
     712 
     713 
     714#ifndef MAEMO_CONIC_DUMMY 
    713715        priv->cnx = con_ic_connection_new (); 
    714716        if (!priv->cnx) { 
     
    716718        } 
    717719 
    718         /* This might be necessary to make the connection object  
    719          * actually emit the signal, though the documentation says  
    720          * that they should be sent even when this is not set,  
    721          * when we explicitly try to connect.  
    722          * The signal still does not seem to be emitted. 
    723          */ 
     720        /* This might be necessary to make the connection object actually emit  
     721         * the signal, though the documentation says that they should be sent  
     722         * even when this is not set, when we explicitly try to connect. The  
     723         * signal still does not seem to be emitted. */ 
     724 
    724725        g_object_set (priv->cnx, "automatic-connection-events", TRUE, NULL); 
    725726 
     
    727728                          G_CALLBACK(on_connection_event), self); 
    728729         
    729         /* 
    730          * This will get us in connected state only if there is already a connection. 
    731          * thus, this will setup our state correctly when we receive the signals. 
    732          */ 
     730        /* This will get us in connected state only if there is already a connection. 
     731         * thus, this will setup our state correctly when we receive the signals. */ 
     732 
    733733        if (!con_ic_connection_connect (priv->cnx, CON_IC_CONNECT_FLAG_AUTOMATICALLY_TRIGGERED)) 
    734734                g_warning ("could not send connect dbus message"); 
    735735                 
    736         #endif /* MAEMO_CONIC_DUMMY */ 
    737          
    738         /* We should not have a real is_online, based on what libconic has told us: */ 
    739         priv->forced = FALSE; 
    740          
    741         #ifdef MAEMO_CONIC_DUMMY 
     736#endif /* MAEMO_CONIC_DUMMY */ 
     737 
     738 
     739#ifdef MAEMO_CONIC_DUMMY 
     740 
    742741        /* Allow debuggers to fake a connection change by setting an environment  
    743          * variable, which we check ever 1 second. 
    744          * This should match one of the fake iap IDs that we created in  
    745          * tny_maemo_conic_device_get_iap_list(). 
    746          */ 
     742         * variable, which we check ever 1 second. This should match one of the  
     743         * fake iap IDs that we created in tny_maemo_conic_device_get_iap_list().*/ 
     744 
    747745        priv->dummy_env_check_timeout =  
    748746                g_timeout_add (1000, on_dummy_connection_check, self); 
    749        #endif /* MAEMO_CONIC_DUMMY */ 
     747#endif /* MAEMO_CONIC_DUMMY */ 
    750748} 
    751749 
     
    767765{ 
    768766        TnyMaemoConicDevicePriv *priv = TNY_MAEMO_CONIC_DEVICE_GET_PRIVATE (obj); 
    769          
    770        #ifndef MAEMO_CONIC_DUMMY 
     767 
     768#ifndef MAEMO_CONIC_DUMMY 
    771769        if (priv->cnx && CON_IC_IS_CONNECTION(priv->cnx)) { 
    772770                if (!tny_maemo_conic_device_disconnect (TNY_MAEMO_CONIC_DEVICE(obj),priv->iap)) 
     
    775773                priv->cnx = NULL; 
    776774        } 
    777         #endif /* MAEMO_CONIC_DUMMY */ 
    778  
    779         #ifdef MAEMO_CONIC_DUMMY 
     775#else 
    780776        if (priv->dummy_env_check_timeout) { 
    781777                g_source_remove (priv->dummy_env_check_timeout); 
    782778                priv->dummy_env_check_timeout = 0; 
    783779        } 
    784        #endif /* MAEMO_CONIC_DUMMY */ 
    785          
     780#endif /* MAEMO_CONIC_DUMMY */ 
     781 
    786782        if (priv->iap) { 
    787783                g_free (priv->iap);