Changeset 16723

Show
Ignore:
Timestamp:
06/27/08 11:11:58 (4 months ago)
Author:
piotras
Message:

Added GError argument to read_file and save_file methods.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/midgard/core/midgard/src/midgard_config.c

    r16466 r16723  
    3131#include "midgard_core_config.h" 
    3232#include "midgard_connection.h" 
     33#include "midgard_error.h" 
    3334 
    3435#define AUTH_NORMAL_STR "Normal" 
     
    9293{ 
    9394        static mdirs *iconf = NULL; 
    94         GDir *dir; 
     95        /* GDir *dir; */ 
    9596         
    9697        if (iconf != NULL) return iconf; 
     
    212213 * @filename: name of the file to read  
    213214 * @user: boolean switch for system or user's config files 
     215 * @error: pointer to store error 
    214216 *  
    215217 * This method reads configuration file from the given name and sets MidgardConfig object's properties. 
     
    223225 * Since: 1.9 
    224226 */  
    225 gboolean midgard_config_read_file(MidgardConfig *self, const gchar *filename, gboolean user
     227gboolean midgard_config_read_file(MidgardConfig *self, const gchar *filename, gboolean user, GError **error
    226228{ 
    227229        const mdirs *iconf; 
     
    230232        GKeyFile *keyfile;       
    231233        gboolean tmpbool; 
    232         GError *error = NULL; 
    233234 
    234235        g_assert(self != NULL); 
     236        g_return_val_if_fail (error == NULL || *error == NULL, FALSE); 
     237 
     238        if(filename == NULL || *filename == '\0') { 
     239                 
     240                *error = g_error_new(MGD_GENERIC_ERROR,  
     241                                MGD_ERR_INVALID_NAME,  
     242                                "Configuration name can not be empty"); 
     243                return FALSE; 
     244        } 
    235245         
    236246        if ((iconf = _midgard_core_config_get_config_dirs()) == NULL) 
     
    247257        } 
    248258 
     259        GError *kf_error = NULL; 
    249260        keyfile = g_key_file_new(); 
    250261        if (!g_key_file_load_from_file ( 
    251                                 keyfile, fname, G_KEY_FILE_NONE, &error)) { 
    252                 g_critical("Can not open %s. %s" , fname, error->message); 
    253                 g_clear_error(&error); 
     262                                keyfile, fname, G_KEY_FILE_NONE, &kf_error)) { 
     263                 
     264                *error = g_error_new(MGD_GENERIC_ERROR, 
     265                                MGD_ERR_INTERNAL, 
     266                                "Can not open %s. %s" , fname, kf_error->message); 
     267                g_clear_error(&kf_error); 
    254268                g_free(fname); 
    255269                return FALSE; 
     
    522536 */ 
    523537gboolean midgard_config_save_file(MidgardConfig *self, 
    524                 const gchar *name, gboolean user
     538                const gchar *name, gboolean user, GError **error
    525539{ 
    526540        g_return_val_if_fail(self != NULL, FALSE); 
     
    658672 
    659673        gsize length; 
    660         GError *error = NULL; 
     674        GError *kf_error = NULL; 
    661675         
    662676        gchar *content = g_key_file_to_data(self->private->keyfile, 
    663                         &length, &error); 
     677                        &length, &kf_error); 
    664678 
    665679        if(length < 1) { 
    666                 g_warning("%s", error->message); 
    667                 g_clear_error(&error); 
     680                g_propagate_error(error, kf_error); 
     681                g_clear_error(&kf_error); 
    668682                return FALSE; 
    669683        } 
    670684 
    671         g_clear_error(&error); 
     685        if(kf_error) 
     686                g_clear_error(&kf_error); 
    672687 
    673688        gboolean saved = g_file_set_contents(cnfpath,  
    674                         content, length, &error); 
     689                        content, length, &kf_error); 
    675690 
    676691        if(!saved) { 
    677                 g_warning("%s", error->message); 
    678                 g_clear_error(&error); 
     692                g_propagate_error(error, kf_error); 
     693                g_clear_error(&kf_error); 
    679694                return FALSE; 
    680695        } 
    681696 
    682 #ifdef MIDGARD_IS_WIN32 
     697#ifdef G_OS_WIN32 
    683698        /* TODO , implement WIN32 */ 
    684699#else  
    685700        g_chmod(cnfpath, 0600); 
    686 #endif /* MIDGARD_IS_WIN32 */ 
     701#endif /* G_OS_WIN32 */ 
    687702 
    688703        return TRUE; 
     
    801816} 
    802817 
    803  
    804  
    805 MidgardConnection *midgard_config_init(const gchar *filename) 
    806 { 
    807         MidgardConnection *mgd = midgard_connection_new(); 
    808  
    809         if(!mgd) 
    810                 g_error("Can not initialize midgard_connection object"); 
    811  
    812         MidgardConfig *config = g_object_new(MIDGARD_TYPE_CONFIG, NULL); 
    813          
    814         if(!midgard_config_read_file(config, (const gchar *)filename, TRUE)){ 
    815                 g_object_unref(mgd); 
    816                 g_object_unref(config); 
    817                 return NULL; 
    818         } 
    819  
    820         if(!midgard_connection_open_config(mgd, config)){ 
    821                 g_object_unref(mgd); 
    822                 g_object_unref(config);  
    823                 return NULL; 
    824         } 
    825  
    826         /* 
    827         g_log_set_handler(G_LOG_DOMAIN, G_LOG_LEVEL_MASK, 
    828                         mgd_log_debug_default, 
    829                         (gpointer)midgard_connection_get_loglevel(mgd)); 
    830         g_log_set_handler("midgard-core", G_LOG_LEVEL_MASK, 
    831                         mgd_log_debug_default, 
    832                         (gpointer)midgard_connection_get_loglevel(mgd)); 
    833         g_log_set_handler("midgard-core-type", G_LOG_LEVEL_MASK, 
    834                         mgd_log_debug_default, 
    835                         (gpointer)midgard_connection_get_loglevel(mgd)); 
    836                         */ 
    837  
    838         return mgd; 
    839 } 
    840818 
    841819/* !! Undocumented workarounds !! */ 
  • trunk/midgard/core/midgard/src/midgard_config.h

    r16277 r16723  
    9595        /* class members */ 
    9696        gboolean (*read_config) (MidgardConfig *self,  
    97                                 const gchar *filename, gboolean user); 
     97                                const gchar *filename, gboolean user, GError **error); 
    9898        gboolean (*save_config) (MidgardConfig *self, 
    99                                 const gchar *name, gboolean user); 
     99                                const gchar *name, gboolean user, GError **error); 
    100100        gchar (**list_files) (gboolean user); 
    101101        gboolean (*create_midgard_tables)(MidgardConfig *self, 
     
    120120} MidgardDBType; 
    121121 
    122 extern GType midgard_config_get_type(void); 
     122GType midgard_config_get_type(void); 
    123123 
    124 extern MidgardConfig *midgard_config_new(void); 
     124MidgardConfig *midgard_config_new(void); 
    125125 
    126 extern gboolean midgard_config_read_file(MidgardConfig *self,  
    127                                 const gchar *filename, gboolean user); 
     126gboolean midgard_config_read_file(MidgardConfig *self,  
     127                                const gchar *filename, gboolean user, GError **error); 
    128128 
    129 extern gchar **midgard_config_list_files(gboolean user); 
     129gchar **midgard_config_list_files(gboolean user); 
    130130 
    131 extern gboolean midgard_config_save_file(MidgardConfig *self, 
    132                                 const gchar *name, gboolean user); 
     131gboolean midgard_config_save_file(MidgardConfig *self, 
     132                                const gchar *name, gboolean user, GError **error); 
    133133 
    134 extern gboolean midgard_config_create_midgard_tables(MidgardConfig *self, 
     134gboolean midgard_config_create_midgard_tables(MidgardConfig *self, 
    135135                                MidgardConnection *mgd); 
    136136 
    137 extern gboolean midgard_config_create_class_table(MidgardConfig *self, 
     137gboolean midgard_config_create_class_table(MidgardConfig *self, 
    138138                                MidgardObjectClass *klass, MidgardConnection *mgd); 
    139139 
  • trunk/midgard/core/midgard/src/midgard_connection.c

    r16500 r16723  
    823823         
    824824        MidgardConfig *config = midgard_config_new(); 
    825         if(!midgard_config_read_file(config, name, FALSE)) 
     825 
     826        GError *error = NULL; 
     827        if(!midgard_config_read_file(config, name, FALSE, &error)) 
    826828                return FALSE; 
    827829         
     
    862864        guint i = 0; 
    863865         
     866        GError *error = NULL; 
     867 
    864868        while(cfgs[i] != NULL) { 
    865869                 
     
    868872                 
    869873                MidgardConfig *config = midgard_config_new(); 
    870                 if(!midgard_config_read_file(config, cfgs[i], userdir)) 
     874                if(!midgard_config_read_file(config, cfgs[i], userdir, &error)) 
    871875                        g_warning("Can not open %s configuration", cfgs[i]); 
    872876 
  • trunk/midgard/core/midgard/test/midgard-query.c

    r16255 r16723  
    7777                        cts = TRUE; 
    7878        } 
    79  
    80         if(!midgard_config_read_file(config, (const gchar *)config_name, cts)) 
     79         
     80        if(!midgard_config_read_file(config, (const gchar *)config_name, cts, &error)) { 
     81                 
     82                if(error) { 
     83                        g_warning("%s", error->message); 
     84                        g_error_free(error); 
     85                } 
    8186                return 1; 
     87        } 
    8288         
    8389        if(!midgard_connection_open_config(mgd, config))  
  • trunk/midgard/core/midgard/test/midgard-schema.c

    r16272 r16723  
    169169        MidgardConfig *config = g_object_new(MIDGARD_TYPE_CONFIG, NULL); 
    170170 
    171         if(!midgard_config_read_file(config, (const gchar *)config_file, TRUE)) 
     171        GError *error = NULL; 
     172        if(!midgard_config_read_file(config, (const gchar *)config_file, TRUE, &error)) { 
     173         
     174                if(error) { 
     175                        g_warning("%s", error->message); 
     176                        g_error_free(error); 
     177                } 
     178 
    172179                return 1; 
     180        } 
    173181 
    174182        /* open config using class member method or function