Main Page | Modules | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages

prop.h

Go to the documentation of this file.
00001 /*
00002  * $Id: prop.h,v 1.10 2005/11/05 23:29:27 cbiere Exp $
00003  *
00004  * Copyright (c) 2001-2003, Richard Eckart
00005  *
00006  *----------------------------------------------------------------------
00007  * This file is part of gtk-gnutella.
00008  *
00009  *  gtk-gnutella is free software; you can redistribute it and/or modify
00010  *  it under the terms of the GNU General Public License as published by
00011  *  the Free Software Foundation; either version 2 of the License, or
00012  *  (at your option) any later version.
00013  *
00014  *  gtk-gnutella is distributed in the hope that it will be useful,
00015  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  *  GNU General Public License for more details.
00018  *
00019  *  You should have received a copy of the GNU General Public License
00020  *  along with gtk-gnutella; if not, write to the Free Software
00021  *  Foundation, Inc.:
00022  *      59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023  *----------------------------------------------------------------------
00024  */
00025 
00026 #ifndef _prop_h_
00027 #define _prop_h_
00028 
00029 #include "common.h"
00030 #include "event.h"
00031 
00032 #define NO_PROP (0)
00033 
00034 /*
00035  * Handle types.
00036  */
00037 typedef guint32 property_t;
00038 typedef guint32 property_set_t;
00039 
00043 typedef enum {
00044     PROP_TYPE_BOOLEAN,
00045     PROP_TYPE_GUINT32,
00046     PROP_TYPE_GUINT64,
00047     PROP_TYPE_IP,
00048     PROP_TYPE_MULTICHOICE,
00049     PROP_TYPE_STORAGE,
00050     PROP_TYPE_STRING,
00051     PROP_TYPE_TIMESTAMP,
00052 
00053     NUM_PROP_TYPES
00054 } prop_type_t;
00055 
00059 typedef gboolean (*prop_changed_listener_t) (property_t);
00060 
00061 /*
00062  * Event subscription control call signatures.
00063  */
00064 typedef void (*prop_add_prop_changed_listener_t)
00065     (property_t, prop_changed_listener_t, gboolean);
00066 typedef void (*prop_add_prop_changed_listener_full_t)
00067     (property_t, prop_changed_listener_t, gboolean, enum frequency_type, guint32);
00068 typedef void (*prop_remove_prop_changed_listener_t)
00069     (property_t, prop_changed_listener_t);
00070 
00074 typedef struct prop_def_choice {
00075     gchar *title;
00076     guint32 value;
00077 } prop_def_choice_t;
00078 
00079 
00080 typedef struct prop_def_guint32 {
00081     guint32 *def;       
00082     guint32 *value;     
00083     guint32 min;        
00084     guint32 max;        
00085     prop_def_choice_t *choices;
00086 } prop_def_guint32_t;
00087 
00088 typedef void (*prop_set_guint32_t)
00089     (property_t, const guint32 *, size_t, size_t);
00090 typedef guint32 *(*prop_get_guint32_t)
00091     (property_t, guint32 *, size_t, size_t);
00092 
00093 
00094 typedef struct prop_def_guint64 {
00095     guint64 *def;       
00096     guint64 *value;     
00097     guint64 min;        
00098     guint64 max;        
00099     prop_def_choice_t *choices;
00100 } prop_def_guint64_t;
00101 
00102 typedef void (*prop_set_guint64_t)
00103     (property_t, const guint64 *, size_t, size_t);
00104 typedef guint64 *(*prop_get_guint64_t)
00105     (property_t, guint64 *, size_t, size_t);
00106 
00107 typedef struct prop_def_timestamp {
00108     time_t *def;        
00109     time_t *value;      
00110     time_t min;     
00111     time_t max;     
00112     prop_def_choice_t *choices;
00113 } prop_def_timestamp_t;
00114 
00115 typedef void (*prop_set_timestamp_t)
00116     (property_t, const time_t *, size_t, size_t);
00117 typedef time_t *(*prop_get_timestamp_t)
00118     (property_t, time_t *, size_t, size_t);
00119 
00120 
00121 typedef struct prop_def_storage {
00122     gchar *value;       
00123 } prop_def_storage_t;
00124 
00125 typedef void (*prop_set_storage_t)(property_t, const gchar *, size_t);
00126 typedef gchar *(*prop_get_storage_t)(property_t, gchar *, size_t);
00127 
00128 
00129 typedef struct prop_def_string {
00130     gchar **def;        
00131     gchar **value;      
00132 } prop_def_string_t;
00133 
00134 typedef void (*prop_set_string_t)(property_t, const gchar *);
00135 typedef gchar *(*prop_get_string_t)(property_t, gchar *, size_t);
00136 
00137 
00138 typedef struct prop_def_boolean {
00139     gboolean *def;      
00140     gboolean *value;    
00141 } prop_def_boolean_t;
00142 
00143 typedef void (*prop_set_boolean_t)
00144     (property_t, const gboolean *, size_t, size_t);
00145 typedef gboolean *(*prop_get_boolean_t)
00146     (property_t, gboolean *, size_t, size_t);
00147 
00151 typedef struct prop_def {
00152     gchar *name;        
00153     gchar *desc;        
00154     prop_type_t type;
00155     union {
00156         prop_def_guint32_t  guint32;
00157         prop_def_guint64_t  guint64;
00158         prop_def_string_t   string;
00159         prop_def_boolean_t  boolean;
00160         prop_def_storage_t  storage;
00161         prop_def_timestamp_t  timestamp;
00162     } data;
00163     gboolean save; /* persist across sessions */
00164     size_t vector_size; /* number of items in array, 1 for non-vector */
00165     struct event *ev_changed;
00166 } prop_def_t;
00167 
00171 typedef struct prop_set_stub {
00172     size_t size;
00173     size_t offset;
00174     prop_def_t *(*get_def)(property_t);
00175     property_t (*get_by_name)(const char *);
00176     const gchar *(*to_string)(property_t);
00177     struct {
00178         prop_add_prop_changed_listener_t add;
00179         prop_add_prop_changed_listener_full_t add_full;
00180         prop_remove_prop_changed_listener_t remove;
00181     } prop_changed_listener;
00182     struct {
00183         prop_get_boolean_t get;
00184         prop_set_boolean_t set;
00185     } boolean;
00186     struct {
00187         prop_get_guint32_t get;
00188         prop_set_guint32_t set;
00189     } guint32;
00190     struct {
00191         prop_get_guint64_t get;
00192         prop_set_guint64_t set;
00193     } guint64;
00194     struct {
00195         prop_get_storage_t get;
00196         prop_set_storage_t set;
00197     } storage;
00198     struct {
00199         prop_get_string_t get;
00200         prop_set_string_t set;
00201     } string;
00202     struct {
00203         prop_get_timestamp_t get;
00204         prop_set_timestamp_t set;
00205     } timestamp;
00206 } prop_set_stub_t;
00207 
00211 typedef prop_set_stub_t *(*prop_set_get_stub_t)(void);
00212 
00216 typedef struct prop_set {
00217     gchar *name;        
00218     gchar *desc;        
00219     size_t size;        
00220     size_t offset;      
00221     prop_def_t *props;  
00222     GHashTable *byName; 
00223     time_t mtime;       
00224     gboolean dirty;     
00225     prop_set_get_stub_t get_stub;
00226 } prop_set_t;
00227 
00228 /*
00229  * Helpers
00230  */
00231 
00232 prop_def_t *prop_get_def(prop_set_t *, property_t);
00233 void prop_free_def(prop_def_t *);
00234 
00235 const gchar *prop_name(prop_set_t *ps, property_t prop);
00236 const gchar *prop_description(prop_set_t *ps, property_t prop);
00237 const gchar *prop_to_string(prop_set_t *ps, property_t prop);
00238 const gchar *prop_type_to_string(prop_set_t *ps, property_t prop);
00239 const gchar *prop_default_to_string(prop_set_t *ps, property_t prop);
00240 gboolean prop_is_saved(prop_set_t *ps, property_t prop);
00241 
00242 void prop_add_prop_changed_listener(
00243     prop_set_t *, property_t, prop_changed_listener_t, gboolean);
00244 void prop_add_prop_changed_listener_full(
00245     prop_set_t *, property_t, prop_changed_listener_t, gboolean,
00246     enum frequency_type, guint32);
00247 void prop_remove_prop_changed_listener(
00248     prop_set_t *, property_t, prop_changed_listener_t);
00249 
00250 void prop_save_to_file_if_dirty(
00251     prop_set_t *ps, const gchar *dir, const gchar *filename);
00252 void prop_save_to_file(
00253     prop_set_t *ps, const gchar *dir, const gchar *filename);
00254 void prop_load_from_file(
00255     prop_set_t *ps, const gchar *dir, const gchar *filename);
00256 
00257 /*
00258  * get/set functions
00259  */
00260 void prop_set_boolean(
00261     prop_set_t *, property_t, const gboolean *, size_t, size_t);
00262 gboolean *prop_get_boolean(
00263     prop_set_t *, property_t, gboolean *, size_t, size_t);
00264 
00265 void prop_set_string(prop_set_t *, property_t, const gchar *);
00266 gchar *prop_get_string(prop_set_t *, property_t, gchar *, size_t);
00267 
00268 void prop_set_guint32(
00269     prop_set_t *, property_t, const guint32 *, size_t, size_t);
00270 guint32 *prop_get_guint32(
00271     prop_set_t *, property_t, guint32 *, size_t, size_t);
00272 
00273 void prop_set_guint64(
00274     prop_set_t *, property_t, const guint64 *, size_t, size_t);
00275 guint64 *prop_get_guint64(
00276     prop_set_t *, property_t, guint64 *, size_t, size_t);
00277 
00278 void prop_set_timestamp(
00279     prop_set_t *, property_t, const time_t *, size_t, size_t);
00280 time_t *prop_get_timestamp(
00281     prop_set_t *, property_t, time_t *, size_t, size_t);
00282 
00283 void prop_set_storage(prop_set_t *, property_t, const gchar *, size_t);
00284 gchar *prop_get_storage(prop_set_t *, property_t, gchar *, size_t);
00285 
00286 property_t prop_get_by_name(prop_set_t *ps, const char *name);
00287 GSList *prop_get_by_regex(prop_set_t *ps, const gchar *pattern, gint *error);
00288 void prop_set_from_string(prop_set_t *ps, property_t prop, const gchar *val,
00289     gboolean saved_only);
00290 
00291 /*
00292  * Checks if a property is part of a property set.
00293  */
00294 static inline gboolean
00295 prop_in_range(const prop_set_t *ps, property_t prop)
00296 {
00297     return prop >= ps->offset && prop < ps->size + ps->offset;
00298 }
00299 
00300 static inline prop_def_t *
00301 get_prop(prop_set_t *ps, property_t prop, const gchar *loc)
00302 {
00303     if (!ps)
00304         g_error("%s: ps != NULL failed", loc);
00305     if (!prop_in_range(ps, prop))
00306         g_error("%s: unknown property %u", loc, (guint) prop);
00307     return &ps->props[prop - ps->offset];
00308 }
00309 
00315 #define PROP(ps, p) (* get_prop((ps), (p), G_STRLOC))
00316 
00317 #endif /* _prop_h_ */
00318 /* vi: set ts=4 sw=4 cindent: */

Generated on Sun Feb 12 10:49:57 2006 for Gtk-Gnutella by doxygen 1.3.6