00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00026 #include "config.h"
00027 
00028 #include <string.h>
00029 #include <errno.h>
00030 
00031 #include <liberregxml/erregapi.h>
00032 
00033 #include "connectionMgrLog.h"
00034 #include "commonData.h"
00035 #include "connectionMgr.h"
00036 
00037 
00038 static regNetworkConfig_t*   g_netConfig = NULL;
00039 static regNetworkProfile_t** g_networks  = NULL;
00040 static regPCConfig_t*        g_pcConfig = NULL;
00041 static regPCProfile_t**      g_pcshares = NULL;
00042 static gboolean              g_configChanged = FALSE;
00043 
00044 
00045 static guint    common_data_add_profile(void);
00046 
00048 
00049 
00050 
00052 void common_data_init(void)
00053 {
00054     CN_LOGPRINTF("entry");
00055  
00056     
00057     erRegGetNetworkProfiles(&g_netConfig, &g_networks);
00058 
00059     
00060     erRegGetPCProfiles(&g_pcConfig, &g_pcshares);
00061 }
00062 
00063 void common_data_destroy(void)
00064 {
00065     CN_LOGPRINTF("entry");
00066 
00067     if (g_netConfig)
00068     {
00069         erRegFreeNetworkProfiles(g_netConfig, g_networks);
00070         g_networks = NULL;
00071         g_netConfig = NULL;
00072     }
00073 
00074     if (g_pcConfig)
00075     {
00076         erRegFreePCProfiles(g_pcConfig, g_pcshares);
00077         g_pcConfig = NULL;
00078         g_pcshares = NULL;
00079     }
00080 }
00081 
00082 void common_data_store(void)
00083 {
00084     CN_LOGPRINTF("entry");
00085 
00086     if ( g_configChanged == FALSE)
00087     {
00088         
00089         return;
00090     }
00091 
00092     gint index;
00093     char* theID;
00094     gboolean NWStored;
00095     regNetworkConfig_t* theNetworkConfig = NULL;
00096     regNetworkProfile_t* theNetworkProfile = NULL;
00097         
00098     
00099     prepare_registry_write();
00100 
00101     
00102     theNetworkConfig = erRegGetNetworkConfig();
00103     if (theNetworkConfig)
00104     {
00105         
00106         for (index = 0; index < theNetworkConfig->size; index++)
00107         {
00108             theID = theNetworkConfig->networkList[index];
00109             if (erRegRemoveNetworkProfile(theID) == FALSE)
00110             {
00111                 CN_WARNPRINTF("could not remove %s", 
00112                         theNetworkConfig->networkList[index]);
00113             }
00114         }
00115         erRegFreeNetworkConfig(theNetworkConfig);
00116 
00117         
00118         erRegRemoveNetworkConfig();
00119     }
00120     else
00121     {
00122         CN_WARNPRINTF("could not retrieve theNetworkConfig");
00123     }
00124 
00125     
00126     theNetworkConfig = g_new0(regNetworkConfig_t, 1);
00127     theNetworkConfig->size = g_netConfig->size;
00128     CN_STOREPRINTF("theNetworkConfig->size %d", theNetworkConfig->size);
00129     theNetworkConfig->networkList = g_new0(gchar*, 
00130                         (theNetworkConfig->size + 1));
00131 
00132     for (index = 0 ; index < g_netConfig->size ; index++)
00133     {
00134         theNetworkProfile = g_networks[index];
00135 
00136         if (theNetworkProfile == NULL)
00137         {
00138             CN_STOREPRINTF("theNetworkProfile = NULL");
00139         }
00140         else
00141         {
00142             theID = g_strdup_printf("NW_%d", index);
00143             NWStored = erRegSetNetworkProfile(theID, theNetworkProfile);
00144 
00145             if (NWStored)
00146             {
00147                 CN_STOREPRINTF("id %s was stored", theID);
00148                 theNetworkConfig->networkList[index] = g_strdup(theID);
00149             }
00150             else
00151             {
00152                 CN_STOREPRINTF("id %s was NOT stored", theID);
00153                 if (theNetworkConfig->size > 0)
00154                 {
00155                     theNetworkConfig->size = theNetworkConfig->size - 1;
00156                 }
00157             }
00158         }
00159     }
00160     if (theNetworkConfig->size > 0)
00161     {
00162         NWStored = erRegSetNetworkConfig(theNetworkConfig);
00163         CN_STOREPRINTF("erRegSetNetworkConfig() returned %s", 
00164                        (NWStored == TRUE) ? "TRUE" : "FALSE");
00165     }
00166     erRegFreeNetworkConfig(theNetworkConfig);
00167 
00168     
00169     
00170     do_registry_write();
00171     CN_STOREPRINTF("Stored registry");
00172     g_configChanged = FALSE;
00173 }
00174 
00176 
00177 
00178 
00180 int common_data_get_n_profiles(void)
00181 {
00182     CN_LOGPRINTF("entry");
00183 
00184     g_return_val_if_fail((g_netConfig != NULL), 0);
00185     return g_netConfig->size;
00186 }
00187 
00188 static int common_data_get_n_profiles_by_networktype(connection_t networkType)
00189 {
00190     CN_LOGPRINTF("entry networkType[%d]", networkType);
00191 
00192     int                        i, nProfiles = 0;
00193     int                        nRegNetworks;
00194     const regNetworkProfile_t* pRegNetwork; 
00195 
00196     nRegNetworks = common_data_get_n_profiles();
00197     for (i = 0; i < nRegNetworks; i++)
00198     {
00199         pRegNetwork = common_data_get_profiledata(i);
00200         if (pRegNetwork && pRegNetwork->connection == networkType)
00201         {
00202             nProfiles++;
00203         }
00204     }
00205     CN_LOGPRINTF("nProfiles = %d", nProfiles);
00206     return nProfiles;
00207 }
00208 
00209 const regNetworkProfile_t* common_data_get_profiledata(const gint index)
00210 {
00211     CN_LOGPRINTF("entry: index [%d]", index);
00212 
00213     g_return_val_if_fail((g_netConfig != NULL), NULL);
00214 
00215     if (index < g_netConfig->size)
00216     {
00217         return g_networks[index];
00218     }
00219     else
00220     {
00221         return NULL;
00222     }
00223 }
00224 
00225 static guint common_data_add_profile()
00226 {
00227     CN_LOGPRINTF("entry");
00228 
00229     guint index = g_netConfig->size;
00230 
00231     
00232     gchar** newList = g_renew(gchar*, g_netConfig->networkList, index + 1);
00233     g_netConfig->networkList = newList;
00234     g_netConfig->networkList[index] = g_strdup_printf("NW_%d", index);
00235 
00236     
00237     regNetworkProfile_t** newSettings = g_renew(regNetworkProfile_t*, 
00238                                                 g_networks, index + 1);
00239     g_networks = newSettings;
00240     g_networks[index] = NULL;
00241     g_netConfig->size = index + 1;
00242 
00243     
00244     return g_netConfig->size;
00245 }
00246 
00247 guint common_data_remove_profile(guint index)
00248 {
00249     CN_LOGPRINTF("entry: index [%d]", index);
00250     
00251     int i;
00252     guint num_profiles = g_netConfig->size;
00253     g_return_val_if_fail((index < num_profiles), num_profiles);
00254 
00255     
00256     g_free( g_netConfig->networkList[num_profiles-1] );
00257     g_netConfig->networkList[num_profiles-1] = NULL;
00258 
00259     
00260     g_free( g_networks[index] );
00261     for (i = index ; i < num_profiles - 1 ; i++)
00262     {
00263         g_networks[i] = g_networks[i+1];
00264     }
00265     g_networks[num_profiles-1] = NULL;
00266 
00267     
00268     g_netConfig->size = num_profiles - 1;
00269 
00270     g_configChanged = TRUE;
00271 
00272     return g_netConfig->size;
00273 }
00274 
00275 const regPCProfile_t* common_data_get_pcsharedata(const gint index)
00276 {
00277     CN_LOGPRINTF("entry: index [%d]", index);
00278 
00279     g_return_val_if_fail((g_pcConfig != NULL), NULL);
00280 
00281     if (index < g_pcConfig->size)
00282     {
00283         return g_pcshares[index];
00284     }
00285     else
00286     {
00287         return NULL;
00288     }
00289 }
00290 
00291 gboolean common_data_reach_max_profiles(connection_t networkType)
00292 {
00293     CN_LOGPRINTF("entry networkType[%d]", networkType);
00294     int      num, max;
00295 
00296     switch (networkType)
00297     {
00298         case wired_t:
00299             max = MAX_WIRED_PROFILES;
00300             break;
00301         case wireless_t:
00302             max = MAX_WIRELESS_PROFILES;
00303             break;
00304         case dialup_t:
00305             max = MAX_DIALUP_PROFILES;
00306             break;
00307         default:
00308             CN_WARNPRINTF("unknow networkType[%d]", networkType);
00309             max = 0;
00310             break;
00311     }
00312 
00313     num = common_data_get_n_profiles_by_networktype(networkType);
00314     CN_LOGPRINTF("num[%d] >= max[%d] ?", num, max);
00315     return ((num >= max) ? TRUE : FALSE);
00316 }
00317 
00319 
00320 
00322 gboolean common_data_set_profiledata(gint index, 
00323                                      const regNetworkProfile_t* setting, 
00324                                      const gboolean preferred)
00325 {
00326     gboolean addNew = FALSE;
00327     gboolean reachMax = FALSE;
00328     gboolean ok = FALSE;
00329     regNetworkProfile_t* tmp_setting;
00330 
00331     CN_LOGPRINTF("entry: index [%d]", index);
00332 
00333     g_return_val_if_fail((g_netConfig != NULL), ok);
00334     g_return_val_if_fail((index <= g_netConfig->size), ok);
00335     
00336     
00337     while (g_netConfig->size <= index)
00338     {
00339         
00340         g_return_val_if_fail(setting != NULL, ok);
00341         reachMax = common_data_reach_max_profiles(setting->connection);
00342         g_return_val_if_fail(reachMax == FALSE, ok);
00343         
00344         common_data_add_profile();
00345         addNew = TRUE;
00346     }
00347 
00348     
00349     if (preferred  &&  index < g_netConfig->size)
00350     {
00351         tmp_setting = g_networks[index];
00352         while (index > 0)
00353         {
00354             g_networks[index] = g_networks[index-1];
00355             --index;
00356         }
00357         g_networks[index] = tmp_setting;
00358     }
00359     
00360     if (   setting == NULL
00361         || setting->name == NULL
00362         || setting->name[0] == '\0')
00363     {
00364         
00365         common_data_remove_profile(index);
00366     }
00367     else
00368     {
00369         prepare_registry_write();
00370 
00371         
00372         gchar* ID = g_netConfig->networkList[index];
00373         if (   erRegSetNetworkProfile(ID, (regNetworkProfile_t*)setting) 
00374             && erRegValidate() )
00375         {
00376             
00377             erRegFreeNetworkProfile( g_networks[index] );
00378             g_networks[index] = erRegGetNetworkProfile(ID);
00379             ok = TRUE;
00380         }
00381         else
00382         {
00383             CN_LOGPRINTF("Set profile [%d] [%s] failed", index, ID);
00384             if (addNew)
00385             {
00386                 
00387                 common_data_remove_profile(index);
00388             }
00389         }
00390 
00391         do_registry_write();
00392     }
00393 
00394     g_configChanged = TRUE;
00395     return ok;
00396 }
00397 
00399 
00401 static networkProfile_t* new_network_profile_t(int nNetworks)
00402 {
00403     networkProfile_t* pNetworksList = NULL; 
00404     networkProfile_t* pNetwork;
00405     int               i;
00406 
00407     if (nNetworks > 0)
00408     {
00409         pNetworksList = g_new0(networkProfile_t, nNetworks);
00410         if (pNetworksList)
00411         {
00412             for (i = 0; i < nNetworks; i++)
00413             {
00414                 pNetwork = &pNetworksList[i];
00415 
00416 
00417 
00418 
00419                 pNetwork->nActiveIndex = -1;
00420             }
00421         }
00422     }
00423 
00424     return pNetworksList;
00425 }
00426 
00427 static void free_network_profile_t(networkProfile_t* pNetworkList, 
00428                                    int nNetworks)
00429 {
00430     int                  i, j;
00431     networkProfile_t*    pNetwork;
00432    
00433     if ((nNetworks > 0) && (pNetworkList != NULL))  
00434     {
00435         for (i = 0; i < nNetworks; i++)
00436         {
00437             pNetwork = &pNetworkList[i];
00438 
00439             
00440             if (pNetwork->nRegProfiles)
00441             {
00442                 
00443                 if (pNetwork->regSettingsList)
00444                 {
00445                     for (j = 0; j < pNetwork->nRegProfiles; j++)
00446                     {
00447                         erRegFreeNetworkProfile(pNetwork->regSettingsList[j]);
00448                     }
00449                     g_free(pNetwork->regSettingsList);
00450                 }
00451                 
00452                 
00453                 if (pNetwork->regIndexList)
00454                 {
00455                     g_free(pNetwork->regIndexList);
00456                 }
00457             }
00458 
00459             
00460             free_network_spec(pNetwork->scanSettings);
00461         }
00462 
00463         g_free(pNetworkList);
00464     }
00465 }
00466 
00467 int common_data_init_network_profiles(connection_t networkType, 
00468                                       networkProfile_t** pNetworks)
00469 {
00470     int                        i, profileIndex;
00471     int                        nRegNetworks;
00472     const regNetworkProfile_t* pRegNetwork; 
00473     networkProfile_t*          pNetwork;
00474     int                        nNetworks;
00475     networkProfile_t*          pNetworksList;
00476 
00477     CN_LOGPRINTF("entry networkType[%d]", networkType);
00478    
00479     
00480     nRegNetworks = common_data_get_n_profiles();
00481     if (nRegNetworks == 0)
00482     {
00483         CN_WARNPRINTF("No network profiles in registry.");
00484         return 0;
00485     }
00486     
00487     
00488     nNetworks = common_data_get_n_profiles_by_networktype(networkType);
00489    
00490     
00491     if (nNetworks == 0)
00492     {
00493         CN_WARNPRINTF("Can't find any [%d] network profiles.", networkType);
00494         return 0;
00495     }
00496 
00497     
00498     pNetworksList = new_network_profile_t(nNetworks);
00499     g_return_val_if_fail(pNetworksList != NULL, 0);
00500 
00501     profileIndex = 0;
00502     for (i = 0; i < nRegNetworks; i++)
00503     {
00504         pRegNetwork = common_data_get_profiledata(i);
00505         if (pRegNetwork && pRegNetwork->connection == networkType)
00506         {
00507             pNetwork = &pNetworksList[profileIndex];
00508             
00509             
00510             pNetwork->scanSettings = NULL;
00511             
00512             pNetwork->regSettingsList = g_new0(regNetworkProfile_t*, 1);
00513             pNetwork->regSettingsList[0] = erRegDupNetworkProfile(pRegNetwork);
00514             
00515             pNetwork->regIndexList = g_new0(int, 1);
00516             pNetwork->regIndexList[0] =  i;
00517             
00518             pNetwork->nRegProfiles = 1;
00519             
00520             pNetwork->nActiveIndex = 0;
00521 
00522             profileIndex++;
00523         }
00524     }
00525    
00526     CN_LOGPRINTF("return nNetworks=%d", nNetworks);
00527     *pNetworks = pNetworksList;
00528     return nNetworks;
00529 }
00530 
00531 void common_data_destroy_network_profiles(networkProfile_t* pNetworks, 
00532                                           guint nProfiles)
00533 {
00534     CN_LOGPRINTF("entry");
00535     free_network_profile_t(pNetworks, nProfiles);
00536 }
00537 
00538 int common_data_get_profile_index(networkProfile_t* pNetworks, 
00539                                   int nProfiles, 
00540                                   int regIndex)
00541 {
00542     CN_LOGPRINTF("entry");
00543 
00544     const networkProfile_t    *settings;
00545     int                       i, j;
00546     int                       profileIndex = -1;
00547 
00548     if ((nProfiles > 0) && (pNetworks))
00549     {
00550         for (i = 0; i < nProfiles; i++)
00551         {
00552             settings = &pNetworks[i];
00553             if (settings 
00554                 && settings->nRegProfiles
00555                 && settings->regSettingsList
00556                 && settings->regIndexList)
00557             {
00558                 for (j = 0; j < settings->nRegProfiles; j++)
00559                 {
00560                     if (settings->regIndexList[j] == regIndex)
00561                     {
00562                         profileIndex = i;
00563                         break;
00564                     }
00565                 }
00566             }
00567         }
00568     }
00569 
00570     return profileIndex;
00571 }
00572 
00573 int common_data_get_reg_index(networkProfile_t* pNetworks,
00574                               int nProfiles,
00575                               int profileIndex)
00576 {
00577     CN_LOGPRINTF("entry");
00578 
00579     const networkProfile_t    *settings;
00580     int                       regIndex = -1;
00581 
00582     if ((nProfiles > 0) && (pNetworks))
00583     {
00584         if ((profileIndex >= 0) && (profileIndex < nProfiles))
00585         {
00586             settings = &pNetworks[profileIndex];
00587             if (settings 
00588                 && (settings->nRegProfiles > 0)
00589                 && settings->regSettingsList
00590                 && settings->regIndexList)
00591             {
00592                 if ((settings->nActiveIndex >= 0) 
00593                      && (settings->nActiveIndex < settings->nRegProfiles))
00594                 {
00595                     regIndex = settings->regIndexList[settings->nActiveIndex];
00596                 }
00597             }
00598         }
00599     }
00600 
00601     return regIndex;
00602 }
00603