import.c

Go to the documentation of this file.
00001 /*
00002  * File Name: import.c
00003  */
00004 
00005 /*
00006  * This file is part of erconftool.
00007  *
00008  * erconftool is free software: you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation, either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * erconftool is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00020  */
00021  
00022 /**
00023  * Copyright (C) 2008 iRex Technologies B.V.
00024  * All rights reserved.
00025  */
00026 
00027 //----------------------------------------------------------------------------
00028 // Include Files
00029 //----------------------------------------------------------------------------
00030 
00031 // system include files, between < >
00032 #include <glib.h>
00033 #include <stdio.h>
00034 #include <string.h>
00035 #include <stdlib.h>
00036 #include <sys/stat.h>
00037 
00038 #include <gconf/gconf-client.h>
00039 
00040 // ereader include files, between < >
00041 
00042 // local include files, between " "
00043 #include "log.h"
00044 #include "erconftool.h"
00045 
00046 
00047 //----------------------------------------------------------------------------
00048 // Type Declarations
00049 //----------------------------------------------------------------------------
00050 
00051 
00052 //----------------------------------------------------------------------------
00053 // Global Constants
00054 //----------------------------------------------------------------------------
00055 
00056 
00057 //----------------------------------------------------------------------------
00058 // Static Variables
00059 //----------------------------------------------------------------------------
00060 
00061 
00062 //============================================================================
00063 // Local Function Definitions
00064 //============================================================================
00065 
00066 static void import_update_key(GConfClient *client, GKeyFile *keyfile, const char *group, const char *key);
00067 
00068 
00069 //============================================================================
00070 // Functions Implementation
00071 //============================================================================
00072 
00073 void import_registry(GConfClient *client, const char *filename, const char **paths)
00074 {
00075     GKeyFile *key_file = NULL;
00076     GError   *error = NULL;
00077     gsize    length = -1;
00078     char     **keys = NULL; 
00079     
00080     key_file = g_key_file_new();
00081     g_key_file_load_from_file(key_file, filename, G_KEY_FILE_NONE, &error);
00082     if (error != NULL) 
00083     {
00084         g_print("Error loading file: %s\n", error->message);
00085         g_key_file_free(key_file);
00086         g_error_free(error);
00087         return;
00088     }     
00089 
00090     // read and import registry group
00091     //
00092     keys = g_key_file_get_keys(key_file, INI_GROUP_REGISTRY, &length, &error);
00093     if (error != NULL) 
00094     {
00095         g_print("Error loading registry settings: %s\n", error->message);
00096         g_error_free(error);
00097         g_key_file_free(key_file);
00098         return;
00099     }     
00100 
00101     guint i = 0;
00102     for (i=0; i<length; i++)
00103     {
00104         gint j;
00105         for (j = 0; paths[j]; j++)
00106         {
00107             if (strstr(keys[i], paths[j]))
00108             {
00109                 // TODO: make these exceptions implicit through the namespace
00110                 if (strstr(keys[i], "list_locales") || strstr(keys[i], "list_languages"))
00111                 {
00112                     // don't import these keys
00113                 }
00114                 else
00115                 {
00116                     import_update_key((GConfClient*) client, key_file, INI_GROUP_REGISTRY, keys[i]);
00117                 }
00118             }
00119         }
00120     }
00121  
00122     // read and import pointercal
00123 #if 0   // disabled see PR3439
00124     import_pointercal(key_file);
00125 #endif 
00126 
00127     // free memory
00128     g_strfreev(keys);
00129     g_key_file_free(key_file);
00130 }
00131 
00132 
00133 static void import_update_key(GConfClient *client, GKeyFile *key_file, const char *group, const char *key)
00134 {
00135     /**
00136      * NOTE
00137      *  
00138      * Updating keys which do not exist and do not have a schema will
00139      * fail as no current value and data type can be determined for it.
00140      *
00141      * Optionally keys can be created by calling the appropriate 
00142      * gconf_client_set_xxx() function.
00143      */
00144     GError *error = NULL;
00145     
00146     GConfEntry *entry = gconf_client_get_entry(client, key, NULL, TRUE, NULL);
00147     GConfValue *value = gconf_entry_get_value(entry);
00148     gboolean is_writable = gconf_entry_get_is_writable(entry);
00149     GConfValueType type = GCONF_VALUE_INVALID;
00150 
00151     if (value) 
00152     { 
00153         type = value->type;
00154     }
00155     else
00156     {
00157         // try to get type from schema
00158         const char *schema_key = gconf_entry_get_schema_name(entry);
00159         if (schema_key)
00160         {
00161             GConfSchema *schema = gconf_client_get_schema(client, schema_key, NULL);
00162             if (schema)
00163             {
00164                 type = gconf_schema_get_type(schema);
00165                 is_writable = TRUE;
00166             }
00167         }
00168     }
00169     
00170     if (value && is_writable) 
00171     {
00172         switch (type) 
00173         {
00174         case GCONF_VALUE_STRING: 
00175             {
00176                 gchar *ini_value = g_key_file_get_string(key_file, group, key, &error);
00177                 if (!error)
00178                 {
00179                     if (strcmp(gconf_value_get_string(value), ini_value) != 0) 
00180                     {
00181                         LOGPRINTF("key %s updated to `%s`", key, ini_value);
00182                         gconf_client_set_string(client, key, ini_value, &error);
00183                     }
00184                     g_free(ini_value);
00185                 }
00186                 else
00187                 {
00188                     g_print("Error reading key %s, %s\n", key, error->message);
00189                     g_error_free(error);
00190                     error = NULL;
00191                 }
00192             }
00193             break;
00194                 
00195         case GCONF_VALUE_INT:
00196             {
00197                 gint ini_value = g_key_file_get_integer(key_file, group, key, &error);
00198                 if (!error)
00199                 {
00200                     if (gconf_value_get_int(value) != ini_value) 
00201                     {
00202                         LOGPRINTF("key %s updated to %d", key, ini_value);
00203                         gconf_client_set_int(client, key, ini_value, &error);
00204                     }
00205                 }
00206                 else
00207                 {
00208                     g_print("Error reading key %s, %s\n", key, error->message);
00209                     g_error_free(error);
00210                     error = NULL;
00211                 }
00212             }
00213             break;      
00214                 
00215         case GCONF_VALUE_FLOAT:
00216             {
00217                 gdouble ini_value = g_key_file_get_double(key_file, group, key, &error);
00218                 if (!error)
00219                 {
00220                     if (gconf_value_get_float(value) != ini_value) 
00221                     {
00222                         LOGPRINTF("key %s updated to %f", key, ini_value);
00223                         gconf_client_set_float(client, key, ini_value, &error);
00224                     }
00225                 }
00226                 else
00227                 {
00228                     g_print("Error reading key %s, %s\n", key, error->message);
00229                     g_error_free(error);
00230                 }
00231             }
00232             break;      
00233                     
00234         case GCONF_VALUE_BOOL:
00235             {
00236                 gdouble ini_value = g_key_file_get_boolean(key_file, group, key, &error);
00237                 if (!error)
00238                 {
00239                     if (gconf_value_get_bool(value) != ini_value) 
00240                     {
00241                         LOGPRINTF("key %s updated to %s", key, ini_value ? "true" : "false");
00242                         gconf_client_set_bool(client, key, ini_value, &error);
00243                     }
00244                 }
00245                 else
00246                 {
00247                     g_print("Error reading key %s, %s\n", key, error->message);
00248                     g_error_free(error);
00249                     error = NULL;
00250                 }
00251             }
00252             break;            
00253 
00254         case GCONF_VALUE_LIST:
00255             {
00256                 GSList *list = NULL;
00257                 gsize len = 0;
00258                 switch (gconf_value_get_list_type(value))
00259                 {
00260                 case GCONF_VALUE_STRING:
00261                     {
00262                         gchar **lval = g_key_file_get_string_list(key_file, group, key, &len, &error);
00263                         if (lval && len > 0)
00264                         {
00265                             // create list, backwards for speed
00266                             int i;
00267                             for (i=len-1; i>=0; i--)
00268                             {
00269                                 if (lval[i])
00270                                 {
00271                                     GConfValue *conf_value = gconf_value_new(GCONF_VALUE_STRING);
00272                                     gconf_value_set_string(conf_value, lval[i]);
00273                                     list = g_slist_prepend(list, conf_value);
00274                                 }
00275                             }
00276                             
00277                             if (list)
00278                             {
00279                                 LOGPRINTF("key %s updated (%d string entries)", key, len);
00280                                 
00281                                 // add list to registry
00282                                 gconf_value_set_list(value, list);
00283                                 gconf_client_set(client, key, value, &error);
00284 
00285                                 // free allocated memory
00286                                 g_slist_free(list);
00287                                 g_strfreev(lval);
00288                             }
00289                         }
00290                         else if (error)
00291                         {
00292                             g_print("Error reading key %s, %s\n", key, error->message);
00293                             g_error_free(error);
00294                             error = NULL;
00295                         }
00296                     }
00297                     break;
00298                             
00299                 case GCONF_VALUE_INT:
00300                     {
00301                         gint *lval = g_key_file_get_integer_list(key_file, group, key, &len, &error);
00302                         if (lval && len > 0)
00303                         {
00304                             // create list, backwards for speed
00305                             int i;
00306                             for (i=len-1; i>=0; i--)
00307                             {
00308                                 GConfValue *conf_value = gconf_value_new(GCONF_VALUE_INT);
00309                                 gconf_value_set_int(conf_value, lval[i]);
00310                                 list = g_slist_prepend(list, conf_value);
00311                             }
00312                             
00313                             if (list)
00314                             {
00315                                 LOGPRINTF("key %s updated (%d integer entries)", key, len);
00316                                 
00317                                 // add list to registry
00318                                 gconf_value_set_list(value, list);
00319                                 gconf_client_set(client, key, value, &error);
00320 
00321                                 // free allocated memory
00322                                 g_slist_free(list);
00323                                 g_free(lval);
00324                             }
00325                         }
00326                         else if (error)
00327                         {
00328                             g_print("Error reading key %s, %s\n", key, error->message);
00329                             g_error_free(error);
00330                             error = NULL;
00331                         }
00332                     }
00333                     break;
00334                             
00335                 case GCONF_VALUE_BOOL:
00336                     {
00337                         gboolean *lval = g_key_file_get_boolean_list(key_file, group, key, &len, &error);
00338                         if (lval && len > 0)
00339                         {
00340                             // create list, backwards for speed
00341                             int i;
00342                             for (i=len-1; i>=0; i--)
00343                             {
00344                                 GConfValue *conf_value = gconf_value_new(GCONF_VALUE_BOOL);
00345                                 gconf_value_set_bool(conf_value, lval[i]);
00346                                 list = g_slist_prepend(list, conf_value);
00347                             }
00348                             
00349                             if (list)
00350                             {
00351                                 LOGPRINTF("key %s updated (%d boolean entries)", key, len);
00352                                 
00353                                 // add list to registry
00354                                 gconf_value_set_list(value, list);
00355                                 gconf_client_set(client, key, value, &error);
00356 
00357                                 // free allocated memory
00358                                 g_slist_free(list);
00359                                 g_free(lval);
00360                             }
00361                         }
00362                         else if (error)
00363                         {
00364                             g_print("Error reading key %s, %s\n", key, error->message);
00365                             g_error_free(error);
00366                             error = NULL;
00367                         }
00368                     }
00369                     break;
00370                             
00371                 case GCONF_VALUE_FLOAT:
00372                     {
00373                         gdouble *lval = g_key_file_get_double_list(key_file, group, key, &len, &error);
00374                         if (lval && len > 0)
00375                         {
00376                             // create list, backwards for speed
00377                             int i;
00378                             for (i=len-1; i>=0; i--)
00379                             {
00380                                 GConfValue *conf_value = gconf_value_new(GCONF_VALUE_FLOAT);
00381                                 gconf_value_set_float(conf_value, lval[i]);
00382                                 list = g_slist_prepend(list, conf_value);
00383                             }
00384                             
00385                             if (list)
00386                             {
00387                                 LOGPRINTF("key %s updated (%d float entries)", key, len);
00388                                 
00389                                 // add list to registry
00390                                 gconf_value_set_list(value, list);
00391                                 gconf_client_set(client, key, value, &error);
00392 
00393                                 // free allocated memory
00394                                 g_slist_free(list);
00395                                 g_free(lval);
00396                             }
00397                         }
00398                         else if (error)
00399                         {
00400                             g_print("Error reading key %s, %s\n", key, error->message);
00401                             g_error_free(error);
00402                             error = NULL;
00403                         }
00404                     }
00405                     break;
00406                             
00407                 default:
00408                     g_print("Unsupported data type of list for key: %s\n", key);
00409                     break;
00410                 }
00411                             
00412             }
00413             break;
00414             
00415         case GCONF_VALUE_SCHEMA:
00416         case GCONF_VALUE_PAIR:
00417         case GCONF_VALUE_INVALID:
00418         default:
00419             g_print("Invalid or unsupported data type for key: %s\n", key);
00420             break;
00421         }
00422     }
00423     else
00424     {
00425         if (!value) 
00426         {
00427             LOGPRINTF("Key %s does not exist in device, not imported\n", key);
00428         }
00429         
00430         if (!is_writable) 
00431         {
00432             LOGPRINTF("Key %s is read-only in device, not imported\n", key);
00433         }
00434     } 
00435     
00436     if (error) 
00437     {
00438         g_print("Failed to set GConf key: %s\n", error->message);
00439         g_error_free(error);     
00440     }
00441 }
00442 
00443 
00444 #if 0  // disabled see PR3439
00445 static void import_pointercal(GKeyFile *key_file)
00446 {
00447     GError *error = NULL;
00448     FILE   *calfile = NULL;
00449     gint   *arr = NULL;
00450     gsize  a_length = 0;
00451     
00452     arr = g_key_file_get_integer_list(key_file, INI_GROUP_XTSCAL, "pointercal", &a_length, &error);
00453     if (error != NULL) 
00454     {
00455         g_print("Error loading from file: %s\n", error->message);
00456         g_error_free(error);
00457         return;
00458     }     
00459 
00460     if (a_length != 7)
00461     {
00462         g_print("Incorrect number of calibration values\n");
00463         return;
00464     }
00465     
00466     calfile = fopen(CAL_FILE, "w");
00467     if (calfile == NULL)
00468     {
00469         g_print("Failed to open for writing: %s\n", CAL_FILE);
00470         return;
00471     }
00472     
00473     fprintf(calfile, "%d %d %d %d %d %d %d\n", arr[0], arr[1], arr[2], arr[3], arr[4], arr[5], arr[6]);
00474     fclose(calfile); 
00475 }
00476 #endif 
Generated by  doxygen 1.6.2-20100208