erconftool/src/main.c

Go to the documentation of this file.
00001 /*
00002  * File Name: main.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 #include "config.h"
00032 
00033 // system include files, between < >
00034 #include <glib.h>
00035 #include <stdio.h>
00036 #include <stdlib.h>
00037 #include <unistd.h>
00038 #include <gconf/gconf-client.h>
00039 
00040 // ereader include files, between < >
00041 
00042 // local include files, between " "
00043 #include "log.h"
00044 #include "export.h"
00045 #include "import.h"
00046 #include "show.h"
00047 
00048 #define UNUSED(x) (void)(x)
00049 
00050 
00051 //----------------------------------------------------------------------------
00052 // Type Declarations
00053 //----------------------------------------------------------------------------
00054 
00055 
00056 //----------------------------------------------------------------------------
00057 // Global Constants
00058 //----------------------------------------------------------------------------
00059 
00060 #define INI_FILE_DEFAULT        "/media/mmcblk0p1/System/dr.ini"
00061 
00062 
00063 //----------------------------------------------------------------------------
00064 // Static Variables
00065 //----------------------------------------------------------------------------
00066 
00067 
00068 //============================================================================
00069 // Local Function Definitions
00070 //============================================================================
00071 
00072 
00073 //============================================================================
00074 // Functions Implementation
00075 //============================================================================
00076 
00077 void key_changed_callback(GConfClient *client,
00078                           guint cnxn_id,
00079                           GConfEntry *entry,
00080                           gpointer user_data)
00081 {
00082     UNUSED(cnxn_id);
00083     UNUSED(user_data);
00084     show_entry(client, entry);   
00085 }
00086 
00087 
00088 int main (int argc, char *argv[])
00089 {
00090     GMainLoop      *mainloop       = NULL;
00091     GError         *error          = NULL;
00092     GConfClient    *gconf_client   = NULL;
00093     gboolean       force_defaults  = FALSE;
00094     gboolean       start_monitor   = FALSE;
00095     gchar          *ini_file       = NULL;
00096     gchar          **delete_paths  = NULL;
00097     gchar          **show_paths    = NULL;
00098     gchar          **import_paths  = NULL;
00099     gchar          **export_paths  = NULL;
00100     GOptionContext *context        = NULL;
00101     gint           i               = 0;
00102     
00103     GOptionEntry entries[] =  
00104     {
00105         { "config",    'c', 0, G_OPTION_ARG_STRING,       &ini_file,        "Fully qualified name of config file", "<filename>" },
00106         { "import",    'i', 0, G_OPTION_ARG_STRING_ARRAY, &import_paths,    "Force export of schema defaults", "<paths>" },
00107         { "delete",    'd', 0, G_OPTION_ARG_STRING_ARRAY, &delete_paths,    "Delete GConf preferences (revert to defaults)", "<paths>" },
00108         { "export",    'e', 0, G_OPTION_ARG_STRING_ARRAY, &export_paths,    "Export settings to the config file", "<paths>" },
00109         { "show",      's', 0, G_OPTION_ARG_STRING_ARRAY, &show_paths,      "Show GConf preferences", "<paths>" },
00110         { "monitor",   'm', 0, G_OPTION_ARG_NONE,         &start_monitor,   "Monitor updated GConf preferences", NULL },
00111         { "force",     'f', 0, G_OPTION_ARG_NONE,         &force_defaults,  "Force export of schema defaults", NULL },
00112         { NULL, 0, 0, 0, NULL, NULL, NULL }
00113     };
00114 
00115     if (argc <= 1)
00116     {
00117         g_print("Run with '--help' to see a full list of available command line options.\n");
00118         exit(0);
00119     }
00120     
00121     // init 
00122     g_type_init();
00123     mainloop = g_main_loop_new(NULL, FALSE);
00124     
00125     // parse commandline arguments
00126     context = g_option_context_new("- Tool to manipulate ereader configuration");
00127     g_option_context_add_main_entries(context, entries, NULL);
00128     if (!g_option_context_parse(context, &argc, &argv, &error)) 
00129     {
00130         g_print("Error parsing arguments: %s\n", error->message);
00131         g_error_free(error);
00132         error = NULL;
00133     }
00134     g_option_context_free(context);
00135     
00136     // init GConf for full repository 
00137     gconf_client = gconf_client_get_default();
00138     if (gconf_client==NULL) 
00139     {
00140         g_error("Failed to connect GConf");
00141         return 1;
00142     }   
00143 
00144     gconf_client_add_dir(gconf_client,
00145                          "/",
00146                          GCONF_CLIENT_PRELOAD_NONE,
00147                          NULL);
00148     
00149     if (!ini_file)
00150     {
00151         ini_file = g_strdup(INI_FILE_DEFAULT);
00152     }
00153 
00154     if (import_paths)
00155     {
00156         import_registry(gconf_client, ini_file, (const char**) import_paths);
00157     }
00158     
00159     if (delete_paths)
00160     {
00161         for (i = 0; delete_paths[i]; i++)
00162         {
00163             gconf_client_recursive_unset(gconf_client, delete_paths[i], 0, &error);
00164             if (error != NULL) 
00165             {
00166                 g_print("Error deleting keys for %s\n", error->message);
00167                 g_error_free(error);
00168                 error = NULL;
00169             }     
00170         }
00171     }
00172     
00173     if (export_paths)
00174     {
00175         export_registry(gconf_client, ini_file, (const char**) export_paths, force_defaults);
00176     }
00177     
00178     if (show_paths)
00179     {
00180         show_registry(gconf_client, (const char**) show_paths);
00181     }
00182     
00183     if (start_monitor)
00184     {
00185         gconf_client_notify_add(gconf_client, "/",
00186                                 key_changed_callback,
00187                                 NULL,
00188                                 NULL, &error);
00189         if (error != NULL) 
00190         {
00191             g_print("Error installing monitor for %s\n", error->message);
00192             g_error_free(error);
00193             error = NULL;
00194         }     
00195         else
00196         {
00197             g_main_loop_run(mainloop);
00198         }
00199     }
00200 
00201     return 0;
00202 }
Generated by  doxygen 1.6.2-20100208