00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00029 #include "config.h"
00030
00031 #include <string.h>
00032 #include <stdlib.h>
00033 #include <glib.h>
00034 #include <gio/gio.h>
00035
00036 #include "perr.h"
00037 #include "dui-initdb.h"
00038
00039 #include "action.h"
00040 #include "estron.h"
00041 #include "duifilter.h"
00042 #include "interface.h"
00043 #include "report.h"
00044 #include "signal.h"
00045 #include "util.h"
00046
00047 struct DuiInterface_s
00048 {
00050 gchar * app_name;
00051
00053 GFile *estron_file;
00054
00055
00056 GList *plugins;
00057
00058 GList *action_handlers;
00059 GList *report_handlers;
00060 GList *databases;
00061
00062
00063
00064
00065 GHashTable *kvp;
00066
00067
00068 GHashTable *gobs;
00069 };
00070
00071
00072
00073
00074 DuiInterface *
00075 dui_interface_new (void)
00076 {
00077 DuiInterface * dui;
00078
00079 dui = g_new (DuiInterface,1);
00080
00081 dui->app_name = NULL;
00082
00083 dui->plugins = NULL;
00084 dui->action_handlers = NULL;
00085 dui->report_handlers = NULL;
00086 dui->databases = NULL;
00087
00088 dui->kvp = g_hash_table_new (g_str_hash, g_str_equal);
00089 dui->gobs = g_hash_table_new (g_str_hash, g_str_equal);
00090
00091
00092 dui_db_init();
00093
00094
00095
00096
00097 return dui;
00098 }
00099
00100
00101
00102
00103 #define FREE_STR(str) if(dui->str) g_free (dui->str); dui->str = NULL;
00104
00105 void
00106 dui_interface_destroy (DuiInterface *dui)
00107 {
00108 GList *node;
00109
00110 if (!dui) return;
00111
00112 FREE_STR (app_name);
00113
00114
00115
00116 g_list_free (dui->action_handlers);
00117 g_list_free (dui->report_handlers);
00118
00119
00120 g_list_free (dui->plugins);
00121
00122 for (node=dui->databases; node; node=node->next)
00123 {
00124 DuiDatabase *db = node->data;
00125 dui_database_destroy (db);
00126 }
00127 g_list_free (dui->databases);
00128
00129
00130 g_hash_table_destroy (dui->kvp);
00131
00132
00133 }
00134
00135
00136
00137 void
00138 dui_interface_set_name (DuiInterface *dui, const gchar * name)
00139 {
00140 dui->app_name = g_strdup (name);
00141 }
00142
00143 void
00144 estron_interface_set_gfile (EstronInterface * est, GFile * gfile)
00145 {
00146 est->estron_file = gfile;
00147 }
00148
00149 gchar *
00150 estron_interface_get_fileparent (EstronInterface * est)
00151 {
00152 g_return_val_if_fail (est, NULL);
00153 return g_file_get_parse_name (g_file_get_parent ((est->estron_file)));
00154 }
00155
00156 gchar *
00157 estron_interface_get_filename (EstronInterface * est)
00158 {
00159 g_return_val_if_fail (est, NULL);
00160 return g_file_get_parse_name ((est->estron_file));
00161 }
00162
00163
00164
00165 void
00166 dui_interface_register_plugin (DuiInterface *dui, DuiInterfacePlugin *plg)
00167 {
00168 if (!dui || !plg) return;
00169
00170 dui->plugins = g_list_append (dui->plugins, plg);
00171 }
00172
00173 void
00174 dui_interface_add_action (DuiInterface *dui, DuiAction *act)
00175 {
00176 if (!dui || !act) return;
00177
00178 dui->action_handlers = g_list_append (dui->action_handlers, act);
00179 }
00180
00181 void
00182 dui_interface_add_database (DuiInterface *dui, DuiDatabase *db)
00183 {
00184 if (!dui || !db) return;
00185 dui->databases = g_list_append (dui->databases, db);
00186 dui_interface_add_object (dui, dui_database_get_name(db), G_OBJECT(db));
00187 }
00188
00189 void
00190 dui_interface_add_report (DuiInterface *dui, DuiReport *rpt)
00191 {
00192 if (!dui || !rpt) return;
00193 dui->report_handlers = g_list_append (dui->report_handlers, rpt);
00194 }
00195
00196
00197
00198 void
00199 dui_interface_add_object (DuiInterface *dui, const gchar *instance_name, GObject *gob)
00200 {
00201 if (!dui || !instance_name) return;
00202
00203
00204 if (!G_IS_OBJECT(gob))
00205 {
00206 PERR ("The instance \'%s\' is not an object!\n", instance_name);
00207 return;
00208 }
00209
00212 g_hash_table_insert (dui->gobs, g_strdup(instance_name), gob);
00213 }
00214
00215 GObject *
00216 dui_interface_find_object_by_name (DuiInterface *dui, const gchar *key)
00217 {
00218 if (!dui) return NULL;
00219
00220
00221 if (!strncmp (key, "/system/", 8))
00222 {
00223 return NULL;
00224 }
00225 return g_hash_table_lookup (dui->gobs, key);
00226 }
00227
00228
00229
00230 void
00231 dui_interface_kvp_insert (DuiInterface *dui, const gchar *key,
00232 const gchar *value)
00233 {
00234 if (!dui || !key || !value) return;
00235
00236
00237 if (!strncmp (key, "/system/", 8)) return;
00238
00239 PINFO ("(key=\'%s\' value=\'%s\')", key, value);
00240 g_hash_table_insert (dui->kvp, g_strdup(key), g_strdup(value));
00241 }
00242
00243 const gchar *
00244 dui_interface_kvp_lookup (DuiInterface *dui, const gchar *key)
00245 {
00246 if (!dui) return NULL;
00247
00248
00249 if (!strncmp (key, "/system/", 8))
00250 {
00251 key += 8;
00252 if (!strcmp (key, "datetime"))
00253 {
00256 static gchar date_string [100];
00257 time_t now = time (0);
00258 xxxgnc_secs_to_iso8601_buff (now, date_string);
00259 return date_string;
00260 }
00261 return NULL;
00262 }
00263 return g_hash_table_lookup (dui->kvp, key);
00264 }
00265
00266
00267
00268 DuiReport *
00269 dui_interface_find_report_by_name (DuiInterface *dui, const gchar * name)
00270 {
00271 GList *node;
00272
00273 if (!dui || !name) return NULL;
00274
00275 for (node=dui->report_handlers; node; node=node->next)
00276 {
00277 DuiReport *rpt = node->data;
00278 const gchar *rptname = dui_report_get_name (rpt);
00279 if (rptname && !strcmp(rptname, name))
00280 {
00281 return rpt;
00282 }
00283 }
00284 return NULL;
00285 }
00286
00287
00288
00289 DuiAction *
00290 dui_interface_find_action_by_name (DuiInterface *dui, const gchar * name)
00291 {
00292 GList *node;
00293
00294 if (!dui || !name) return NULL;
00295
00296 for (node=dui->action_handlers; node; node=node->next)
00297 {
00298 DuiAction *act = node->data;
00299 const gchar *actname = dui_action_get_name (act);
00300 if (actname && !strcmp(actname, name))
00301 {
00302 return act;
00303 }
00304 }
00305 return NULL;
00306 }
00307
00308
00309
00310 DuiDatabase *
00311 dui_interface_find_database_by_name (DuiInterface *dui, const gchar * name)
00312 {
00313 GList *node;
00314
00315 if (!dui || !name) return NULL;
00316
00317 for (node=dui->databases; node; node=node->next)
00318 {
00319 DuiDatabase *db = node->data;
00320 const gchar *dbname = dui_database_get_name (db);
00321 if (dbname && !strcmp(dbname, name))
00322 {
00323 return db;
00324 }
00325 }
00326 return NULL;
00327 }
00328
00329
00330
00331 void
00332 dui_interface_realize (DuiInterface *dui, gint fatal_if_no_main)
00333 {
00334 GList *node;
00335
00336 if (!dui) return;
00337
00338 for (node=dui->action_handlers; node; node=node->next)
00339 {
00340 DuiAction *act = node->data;
00341 dui_action_set_interface (act, dui);
00342 }
00343 for (node=dui->report_handlers; node; node=node->next)
00344 {
00345 DuiReport *rpt = node->data;
00346 dui_report_set_interface (rpt, dui);
00347 }
00348
00349 for (node=dui->plugins; node; node=node->next)
00350 {
00351 DuiInterfacePlugin *plg = node->data;
00352 if (plg->realize)
00353 {
00354 (plg->realize) (plg->self, fatal_if_no_main);
00355 }
00356 }
00357 }
00358
00359