Changeset 3643

Show
Ignore:
Timestamp:
09/07/08 19:42:22 (3 months ago)
Author:
paule
Message:

Change to use new OSyncPluginConfig API to read configuration (untested and no implementation of resources yet)

Location:
plugins/opie-sync/src
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • plugins/opie-sync/src/opie-sync

    r2624 r3643  
    11<?xml version="1.0"?> 
    22<config> 
    3   <username>root</username> 
    4   <password>Qtopia</password> 
    5   <hostname>192.168.0.202</hostname> 
    6   <device>opie</device> 
    7   <port>4242</port> 
    8   <conntype>ftp</conntype> 
    9   <notestype>basic</notestype> 
     3  <AdvancedOptions> 
     4    <AdvancedOption> 
     5      <DisplayName>Client Type</DisplayName> 
     6      <Name>ClientType</Name> 
     7      <ValEnum>opie</ValEnum> 
     8      <ValEnum>qtopia2</ValEnum> 
     9      <Type>string</Type> 
     10      <Value>opie</Value> 
     11    </AdvancedOption> 
     12    <AdvancedOption> 
     13      <DisplayName>Connection Type</DisplayName> 
     14      <Name>ConnType</Name> 
     15      <ValEnum>ftp</ValEnum> 
     16      <ValEnum>scp</ValEnum> 
     17      <ValEnum>none</ValEnum> 
     18      <Type>string</Type> 
     19      <Value>ftp</Value> 
     20    </AdvancedOption> 
     21    <AdvancedOption> 
     22      <DisplayName>Notes Type</DisplayName> 
     23      <Name>NotesType</Name> 
     24      <ValEnum>basic</ValEnum> 
     25      <ValEnum>opie-notes</ValEnum> 
     26      <Type>string</Type> 
     27      <Value>basic</Value> 
     28    </AdvancedOption> 
     29    <AdvancedOption> 
     30      <DisplayName>Backup Location (optional)</DisplayName> 
     31      <Name>BackupDir</Name> 
     32      <Type>string</Type> 
     33      <Value></Value> 
     34    </AdvancedOption> 
     35    <AdvancedOption> 
     36      <DisplayName>Local directory (debug only)</DisplayName> 
     37      <Name>LocalDir</Name> 
     38      <Type>string</Type> 
     39      <Value></Value> 
     40    </AdvancedOption> 
     41    <AdvancedOption> 
     42      <DisplayName>Disable QCop (debug only)</DisplayName> 
     43      <Name>DisableQcop</Name> 
     44      <Type>bool</Type> 
     45      <Value>0</Value> 
     46    </AdvancedOption> 
     47  </AdvancedOptions> 
     48  <Authentication> 
     49    <Username>root</Username> 
     50    <Password>Qtopia</Password> 
     51  </Authentication> 
     52  <Connection> 
     53    <Network> 
     54      <Address>192.168.0.202</Address> 
     55      <Port>4242</Port> 
     56    </Network> 
     57  </Connection> 
     58  <Resources> 
     59  </Resources> 
    1060</config> 
  • plugins/opie-sync/src/opie_sync.c

    r3301 r3643  
    11/* 
    22 
    3    Copyright 2005 Paul Eggleton & Holger Hans Peter Freyther 
     3   Copyright 2008 Paul Eggleton & Holger Hans Peter Freyther 
    44 
    55Permission is hereby granted, free of charge, to any person obtaining a copy 
     
    4545} 
    4646 
    47 static osync_bool opie_sync_settings_parse(OpiePluginEnv *env, const char *config, OSyncError **error) 
    48 { 
    49         osync_trace(TRACE_ENTRY, "%s(%p, %s, %p)", __func__, env, config, error); 
    50         xmlDoc *doc = NULL; 
    51         xmlNode *cur = NULL; 
     47static osync_bool opie_sync_read_config(OpiePluginEnv *env, OSyncPluginInfo *info, OSyncError **error) 
     48{ 
     49        osync_trace(TRACE_ENTRY, "%s(%p, %p, %p)", __func__, env, info, error); 
    5250 
    5351        /* Set defaults */ 
     
    6361        env->notes_type = NOTES_TYPE_BASIC; 
    6462 
    65         doc = xmlParseMemory(config, strlen(config)); 
    66  
    67         if (!doc) { 
    68                 osync_error_set(error, OSYNC_ERROR_GENERIC, "Unable to parse settings"); 
     63        OSyncPluginConfig *config = osync_plugin_info_get_config(info); 
     64        if (!config) { 
     65                osync_error_set(error, OSYNC_ERROR_GENERIC, "Unable to get configuration"); 
    6966                goto error; 
    7067        } 
    7168 
    72         cur = xmlDocGetRootElement(doc); 
    73  
    74         if (!cur) { 
    75                 osync_error_set(error, OSYNC_ERROR_GENERIC, "Unable to get configuration XML root element"); 
    76                 goto error_free_doc; 
    77         } 
    78  
    79         if (xmlStrcmp(cur->name, (xmlChar*)"config")) { 
    80                 osync_error_set(error, OSYNC_ERROR_GENERIC, "Configuration file root node name is invalid"); 
    81                 goto error_free_doc; 
    82         } 
    83  
    84         cur = cur->xmlChildrenNode; 
    85  
    86         while (cur != NULL) { 
    87                 if(cur->type == XML_ELEMENT_NODE) { 
    88                         char *str = (char *)xmlNodeGetContent(cur); 
    89                         if (str) { 
    90                                 if (!xmlStrcmp(cur->name, (const xmlChar *)"username")) { 
    91                                         g_free(env->username); 
    92                                         env->username = g_strdup(str); 
    93                                 } else if (!xmlStrcmp(cur->name, (const xmlChar *)"password")) { 
    94                                         g_free(env->password); 
    95                                         env->password = g_strdup(str); 
    96                                 } else if (!xmlStrcmp(cur->name, (const xmlChar *)"hostname")) { 
    97                                         g_free(env->host); 
    98                                         env->host = g_strdup(str); 
    99                                 } else if (!xmlStrcmp(cur->name, (const xmlChar *)"url")) { 
    100                                         osync_trace(TRACE_INTERNAL, "The 'url' configuration parameter is deprecated - please use 'hostname' instead"); 
    101                                         g_free(env->host); 
    102                                         env->host = g_strdup(str); 
    103                                 } else if (!xmlStrcmp(cur->name, (const xmlChar *)"port")) { 
    104                                         env->device_port = atoi(str); 
    105                                 } else if (!xmlStrcmp(cur->name, (const xmlChar *)"device")) { 
    106                                         if (!strcasecmp(str, "qtopia2")) 
    107                                                 env->device_type = OPIE_SYNC_QTOPIA_2; 
    108                                         else 
    109                                                 env->device_type = OPIE_SYNC_OPIE; 
    110                                 } else if (!xmlStrcmp(cur->name, (const xmlChar *)"conntype")) { 
    111                                         if (!strcasecmp(str, "scp")) 
    112                                                 env->conn_type = OPIE_CONN_SCP; 
    113                                         else if ( strcasecmp(str, "none") == 0 ) 
    114                                                 env->conn_type = OPIE_CONN_NONE; 
    115                                         else 
    116                                                 env->conn_type = OPIE_CONN_FTP; 
    117                                 } else if (!xmlStrcmp(cur->name, (const xmlChar *)"use_qcop")) { 
    118                                         if ( strcasecmp(str, "false") == 0 ) 
    119                                                 env->use_qcop = FALSE; 
    120                                         else  
    121                                                 env->use_qcop = TRUE; 
    122                                 } else if (!xmlStrcmp(cur->name, (const xmlChar *)"backupdir")) { 
    123                                         if(strlen(str) > 0) 
    124                                                 env->backupdir = g_strdup(str); 
    125                                 } else if (!xmlStrcmp(cur->name, (const xmlChar *)"localdir")) { 
    126                                         g_free(env->localdir); 
    127                                         env->localdir = g_strdup(str); 
    128                                 } else if (!xmlStrcmp(cur->name, (const xmlChar *)"notestype")) { 
    129                                         if ( (!strcasecmp(str, "opie-notes")) || (!strcasecmp(str, "opie_notes")) ) 
    130                                                 env->notes_type = NOTES_TYPE_OPIE_NOTES; 
    131                                         else if ( strcasecmp(str, "basic") == 0 ) 
    132                                                 env->notes_type = NOTES_TYPE_BASIC; 
    133                                         else { 
    134                                                 osync_error_set(error, OSYNC_ERROR_GENERIC, "Invalid value \"%s\" for configuration option \"%s\"", str, cur->name); 
    135                                                 goto error_free_doc; 
    136                                         } 
    137                                 } else { 
    138                                         osync_error_set(error, OSYNC_ERROR_GENERIC, "Invalid configuration file option \"%s\"", cur->name); 
    139                                         goto error_free_doc; 
    140                                 } 
    141                                 xmlFree(str); 
    142                         } 
    143                 } 
    144                 cur = cur->next; 
    145         } 
    146  
    147         xmlFreeDoc(doc); 
    148         osync_trace(TRACE_EXIT, "%s", __func__); 
    149         return TRUE; 
    150          
    151 error_free_doc: 
    152         xmlFreeDoc(doc); 
     69        OSyncPluginConnection *conn = osync_plugin_config_get_connection(config); 
     70        if(conn) { 
     71                const char *addr = osync_plugin_connection_net_get_address(conn); 
     72                if(addr) { 
     73                        g_free(env->host); 
     74                        env->host = g_strdup(addr); 
     75                } 
     76                unsigned int port = osync_plugin_connection_net_get_port(conn); 
     77                if(port > 0) 
     78                        env->device_port = port; 
     79        } 
     80 
     81        OSyncPluginAuthentication *auth = osync_plugin_config_get_authentication(config); 
     82        if(auth) { 
     83                const char *username = osync_plugin_authentication_get_username(auth); 
     84                if(username) { 
     85                        g_free(env->username); 
     86                        env->username = g_strdup(username); 
     87                } 
     88                const char *password = osync_plugin_authentication_get_password(auth); 
     89                if(password) { 
     90                        g_free(env->password); 
     91                        env->password = g_strdup(password); 
     92                } 
     93        } 
     94         
     95        OSyncList *advoptions = osync_plugin_config_get_advancedoptions(config); 
     96        for (; advoptions; advoptions = advoptions->next) { 
     97                OSyncPluginAdvancedOption *option = advoptions->data; 
     98 
     99                const char *val = osync_plugin_advancedoption_get_value(option); 
     100                const char *name = osync_plugin_advancedoption_get_name(option); 
     101                g_assert(name); 
     102                g_assert(val); 
     103                const char *key = NULL; 
     104 
     105                if (!strcmp(name, "ClientType")) { 
     106                        if (!strcasecmp(val, "qtopia2")) 
     107                                env->device_type = OPIE_SYNC_QTOPIA_2; 
     108                        else 
     109                                env->device_type = OPIE_SYNC_OPIE; 
     110                }  
     111                else if (!strcmp(name, "ConnType")) { 
     112                        if (!strcasecmp(val, "scp")) 
     113                                env->conn_type = OPIE_CONN_SCP; 
     114                        else if ( strcasecmp(val, "none") == 0 ) 
     115                                env->conn_type = OPIE_CONN_NONE; 
     116                        else 
     117                                env->conn_type = OPIE_CONN_FTP; 
     118                }  
     119                else if (!strcmp(name, "NotesType")) { 
     120                        if ( (!strcasecmp(val, "opie-notes")) || (!strcasecmp(val, "opie_notes")) ) 
     121                                env->notes_type = NOTES_TYPE_OPIE_NOTES; 
     122                        else if ( strcasecmp(val, "basic") == 0 ) 
     123                                env->notes_type = NOTES_TYPE_BASIC; 
     124                        else { 
     125                                osync_error_set(error, OSYNC_ERROR_GENERIC, "Invalid value \"%s\" for configuration option \"%s\"", val, name); 
     126                                goto error; 
     127                        } 
     128                }  
     129                else if (!strcmp(name, "BackupDir")) { 
     130                        if(strlen(val) > 0) 
     131                                env->backupdir = g_strdup(val); 
     132                }  
     133                else if (!strcmp(name, "LocalDir")) { 
     134                        if(strlen(val) > 0) { 
     135                                g_free(env->localdir); 
     136                                env->localdir = g_strdup(val); 
     137                        } 
     138                }  
     139                else if (!strcmp(name, "DisableQcop")) { 
     140                        if(atoi(val)) 
     141                                env->use_qcop = FALSE; 
     142                        else 
     143                                env->use_qcop = TRUE; 
     144                }  
     145                else { 
     146                        osync_error_set(error, OSYNC_ERROR_GENERIC, "Invalid configuration file advanced option \"%s\"", name); 
     147                        goto error; 
     148                } 
     149        } 
     150         
    153151error: 
    154152        osync_trace(TRACE_EXIT_ERROR, "%s: %s", __func__, osync_error_print(error)); 
     
    644642                goto error; 
    645643         
    646         const char *configdata = osync_plugin_info_get_config(info); 
    647         if (!configdata) { 
    648                 osync_error_set(error, OSYNC_ERROR_GENERIC, "Unable to get config data."); 
    649                 goto error_free_env; 
    650         } 
    651  
    652         osync_trace(TRACE_INTERNAL, "The config: %s", osync_plugin_info_get_config(info)); 
    653          
    654         if (!opie_sync_settings_parse(env, configdata, error)) 
     644        if (!opie_sync_read_config(env, info, error)) 
    655645                goto error_free_env; 
    656646