Changeset 2462

Show
Ignore:
Timestamp:
08/17/07 15:48:42 (1 year ago)
Author:
paule
Message:

Tidy up connect/disconnect code

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • plugins/opie-sync/src/opie_sync.c

    r2450 r2462  
    138138} 
    139139 
    140 static osync_bool _connectDevice(OpiePluginEnv *env, OSyncError **error) 
     140 
     141/** 
     142 * If QCop is enabled, connect to the the QCop bridge on the remote device and 
     143 * signal that a sync is starting 
     144 */ 
     145static osync_bool device_connect(OpiePluginEnv *env, OSyncError **error) 
    141146{ 
    142147        osync_trace(TRACE_ENTRY, "%s(%p, %p)", __func__, env, error); 
    143         char* errmsg = NULL; 
    144          
    145         if (env->qcopconn) 
    146         { 
     148         
     149        if (env->qcopconn) { 
    147150                osync_trace(TRACE_EXIT, "%s: Already connected", __func__); 
    148151                return TRUE; 
    149152        } 
    150153 
    151         /* Connect to QCopBridgeServer to lock GUI and get root path */ 
    152         if ( env->use_qcop )  
    153         { 
     154        if ( env->use_qcop ) { 
     155                /* Connect to QCopBridgeServer to lock GUI and get root path */ 
    154156                osync_trace(TRACE_INTERNAL, "qcop_connect"); 
    155157                env->qcopconn = qcop_connect(env->url, 
    156158                                             env->username, 
    157159                                             env->password); 
    158                 if (env->qcopconn->result) 
    159                 { 
    160                         qcop_start_sync(env->qcopconn, &sync_cancelled); 
    161                         if (!env->qcopconn->result) 
    162                         { 
    163                                 osync_trace(TRACE_INTERNAL, "qcop_start_sync_failed"); 
    164                                 errmsg = g_strdup(env->qcopconn->resultmsg); 
    165                                 qcop_stop_sync(env->qcopconn); 
    166                                 qcop_freeqconn(env->qcopconn); 
    167                                 env->qcopconn = NULL; 
    168                                 osync_error_set(error, OSYNC_ERROR_GENERIC, errmsg); 
    169                                 goto error; 
    170                         } 
    171                 } 
    172                 else 
    173                 { 
    174                         osync_trace(TRACE_INTERNAL, "QCop connection failed"); 
    175                         errmsg = g_strdup(env->qcopconn->resultmsg); 
    176                         qcop_freeqconn(env->qcopconn); 
     160                if(!env->qcopconn->result) { 
     161                        char *errmsg = g_strdup_printf("qcop_connect failed: %s", env->qcopconn->resultmsg); 
     162                        osync_error_set(error, OSYNC_ERROR_GENERIC, errmsg); 
     163                        g_free(errmsg); 
     164                        goto error; 
     165                } 
     166                 
     167                qcop_start_sync(env->qcopconn, &sync_cancelled); 
     168                if(!env->qcopconn->result) { 
     169                        char *errmsg = g_strdup_printf("qcop_start_sync_failed: %s", env->qcopconn->resultmsg); 
     170                        osync_error_set(error, OSYNC_ERROR_GENERIC, errmsg); 
     171                        g_free(errmsg); 
     172                        qcop_stop_sync(env->qcopconn); 
     173                        goto error; 
     174                } 
     175                 
     176        } 
     177 
     178        osync_trace(TRACE_EXIT, "%s", __func__); 
     179        return TRUE; 
     180 
     181error: 
     182        if(env->qcopconn) { 
     183                qcop_freeqconn(env->qcopconn); 
     184                env->qcopconn = NULL; 
     185        } 
     186        osync_trace(TRACE_EXIT_ERROR, "%s: %s", __func__, osync_error_print(error)); 
     187        return FALSE; 
     188
     189 
     190/** 
     191 * If QCop is enabled & connected, signal that syncing has finished 
     192 */ 
     193static osync_bool device_disconnect(OpiePluginEnv *env, OSyncError **error) 
     194
     195        osync_trace(TRACE_ENTRY, "%s(%p, %p)", __func__, env, error); 
     196         
     197        if(env->qcopconn) { 
     198                qcop_stop_sync(env->qcopconn); 
     199                if (!env->qcopconn->result) { 
     200                        char *errmsg = g_strdup_printf("qcop_stop_sync_failed: %s", env->qcopconn->resultmsg); 
     201                        osync_error_set(error, OSYNC_ERROR_GENERIC, errmsg); 
     202                        g_free(errmsg); 
     203                        qcop_disconnect(env->qcopconn); /* frees qcopconn */ 
    177204                        env->qcopconn = NULL; 
    178                         osync_error_set(error, OSYNC_ERROR_GENERIC, errmsg); 
    179205                        goto error; 
    180206                } 
     207                else { 
     208                        qcop_disconnect(env->qcopconn); /* frees qcopconn */ 
     209                        env->qcopconn = NULL; 
     210                } 
    181211        } 
    182212 
    183213        osync_trace(TRACE_EXIT, "%s", __func__); 
    184214        return TRUE; 
    185  
     215         
    186216error: 
    187217        osync_trace(TRACE_EXIT_ERROR, "%s: %s", __func__, osync_error_print(error)); 
     
    189219} 
    190220 
    191 void _disconnectDevice(OpiePluginEnv *env) 
    192 { 
    193         if(env->qcopconn)  
    194         { 
    195                 qcop_stop_sync(env->qcopconn); 
    196                 if (!env->qcopconn->result) 
    197                 { 
    198                         osync_trace(TRACE_INTERNAL, env->qcopconn->resultmsg); 
    199                 } 
    200                 qcop_disconnect(env->qcopconn); /* frees qcopconn */ 
    201                 env->qcopconn = NULL; 
    202         } 
    203 } 
    204221 
    205222static void connect(void *userdata, OSyncPluginInfo *info, OSyncContext *ctx) 
     
    215232        if(!env->plugin_env->connected) { 
    216233                /* We only want to connect once per session */ 
    217                 if (!_connectDevice(env->plugin_env, &error)) { 
     234                 
     235                if (!device_connect(env->plugin_env, &error)) { 
    218236                        g_mutex_unlock(env->plugin_env->plugin_mutex); 
    219237                        goto error; 
     
    234252                /* failed */ 
    235253                char *errmsg; 
    236                 if(env->plugin_env->qcopconn) 
    237                 { 
    238                         qcop_stop_sync(env->plugin_env->qcopconn); 
    239                         if(!env->plugin_env->qcopconn->result) 
    240                         { 
    241                                 osync_trace(TRACE_INTERNAL, "qcop_stop_sync_failed"); 
    242                                 char *errmsg = g_strdup(env->plugin_env->qcopconn->resultmsg); 
    243                                 qcop_freeqconn(env->plugin_env->qcopconn); 
    244                                 env->plugin_env->qcopconn = NULL; 
    245                                 osync_error_set(&error, OSYNC_ERROR_GENERIC, errmsg); 
    246                                 goto error; 
    247                         }  
    248                         qcop_disconnect(env->plugin_env->qcopconn); 
    249                         env->plugin_env->qcopconn = NULL; 
    250                 } 
     254                device_disconnect(env->plugin_env, &error); 
    251255                errmsg = g_strdup_printf("Failed to load data from device %s", env->plugin_env->url); 
    252256                osync_error_set(&error, OSYNC_ERROR_GENERIC, errmsg); 
     
    643647        } 
    644648         
    645         _disconnectDevice(env); 
     649        OSyncError *error = NULL; 
     650        device_disconnect(env, &error); 
    646651 
    647652        comms_shutdown();