metadata.c

Go to the documentation of this file.
00001 /*
00002  * File Name: metadata.c
00003  */
00004 
00005 /*
00006  * This file is part of erbrowser.
00007  *
00008  * erbrowser 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  * erbrowser 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) 2009 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 <stdlib.h>
00036 
00037 // ereader include files, between < >
00038 
00039 // local include files, between " "
00040 #include "log.h"
00041 #include "metadata.h"
00042 
00043 
00044 //----------------------------------------------------------------------------
00045 // Type Declarations
00046 //----------------------------------------------------------------------------
00047 
00048 #define DEFAULT_H_POSITION   0.0
00049 #define DEFAULT_V_POSITION   0.0
00050 #define DEFAULT_ZOOM_LEVEL   1.0
00051 #define DEFAULT_FULL_SCREEN  FALSE
00052 
00053 
00054 //----------------------------------------------------------------------------
00055 // Global Constants
00056 //----------------------------------------------------------------------------
00057  
00058 static const gchar *META_H_POSITION     = "erbrowser_h_position";
00059 static const gchar *META_V_POSITION     = "erbrowser_v_position";
00060 static const gchar *META_ZOOM_LEVEL     = "erbrowser_zoom_level";
00061 static const gchar *META_FULL_SCREEN    = "erbrowser_full_screem";
00062  
00063  
00064 //----------------------------------------------------------------------------
00065 // Static Variables
00066 //---------------------------------------------------------------------------- 
00067 
00068 static gchar*    g_path         = NULL;
00069 static gchar*    g_filename     = NULL;
00070 static gboolean  g_should_save  = FALSE;
00071  
00072 static gdouble   g_h_position   = DEFAULT_H_POSITION;
00073 static gdouble   g_v_position   = DEFAULT_V_POSITION;
00074 static gdouble   g_zoom_level   = DEFAULT_ZOOM_LEVEL;
00075 static gboolean  g_full_screen  = DEFAULT_FULL_SCREEN;
00076 
00077 
00078 //============================================================================
00079 // Local Function Definitions
00080 //============================================================================
00081 
00082 static const metadata_cell *get_column_data(const metadata_table *table, const char *column_name)
00083 {
00084     return metadata_table_get_cell(table, metadata_table_find_column(table, column_name));
00085 }
00086 
00087 
00088 static gint get_double(const metadata_table *table, const char *column_name, gdouble *value)
00089 {
00090     const metadata_cell *cell = get_column_data(table, column_name);
00091     if (cell && (cell->type == METADATA_DOUBLE))
00092     {
00093         *value  = cell->value.v_double;
00094         return ER_OK;
00095     }
00096     return ER_INVALID_DATA;
00097 }
00098 
00099 
00100 static gint get_integer(const metadata_table *table, const char *column_name, gint *value)
00101 {
00102     const metadata_cell *cell = get_column_data(table, column_name);
00103     if (cell && (cell->type == METADATA_INT64))
00104     {
00105         *value  = cell->value.v_int64;
00106         return ER_OK;
00107     }
00108     return ER_INVALID_DATA;
00109 }
00110 
00111 
00112 static gint get_boolean(const metadata_table *table, const char *column_name, gboolean *value)
00113 {
00114     gint int_value;
00115     gint ret = get_integer(table, column_name, &int_value);
00116     if (ret == ER_OK)
00117     {
00118         *value = int_value ? TRUE : FALSE;
00119     }
00120     
00121     return ER_INVALID_DATA;
00122 }
00123 
00124 
00125 static gint set_double(const metadata_table *table, const char *column_name, gdouble value)
00126 {
00127     return metadata_table_set_double((metadata_table *) table, metadata_table_find_column(table, column_name), value);
00128 }
00129 
00130 
00131 static gint set_boolean(const metadata_table *table, const char *column_name, gboolean value)
00132 {
00133     return metadata_table_set_int64((metadata_table *) table, metadata_table_find_column(table, column_name), value ? 1 : 0 );
00134 }
00135 
00136 
00137 static void meta_file_close(void)
00138 {
00139     LOGPRINTF("entry");
00140 
00141     if (!g_should_save || !g_path || !g_filename) goto out;
00142     
00143     // open database
00144     erMetadb db = ermetadb_local_open(g_path, TRUE);
00145     if (db == NULL) {
00146         ERRORPRINTF("Error storing data for %s", g_filename);
00147         goto out;
00148     }
00149 
00150     // copy values to table
00151     metadata_table *values_table = metadata_table_new();
00152     metadata_table_add_column(values_table, META_H_POSITION);
00153     metadata_table_add_column(values_table, META_V_POSITION);
00154     metadata_table_add_column(values_table, META_ZOOM_LEVEL);
00155     metadata_table_add_column(values_table, META_FULL_SCREEN);
00156 
00157     set_double (values_table, META_H_POSITION,  g_h_position);
00158     set_double (values_table, META_V_POSITION,  g_v_position);
00159     set_double (values_table, META_ZOOM_LEVEL,  g_zoom_level);
00160     set_boolean(values_table, META_FULL_SCREEN, g_full_screen);
00161     
00162     // update database
00163     int ret = ermetadb_local_set_application_data(db, g_filename, values_table);
00164     if (ret != ER_OK)
00165     {
00166         ERRORPRINTF("Error storing application data for %s, error %d", g_filename, ret);
00167     }
00168 
00169     metadata_table_free(values_table);
00170     ermetadb_close(db);
00171 out:
00172     g_free(g_path);
00173     g_path = NULL;
00174     g_free(g_filename);
00175     g_filename = NULL;
00176 }
00177 
00178 
00179 void meta_initialize(void)
00180 {
00181     LOGPRINTF("entry");
00182 }
00183 
00184 
00185 void meta_finalize(void)
00186 {
00187     LOGPRINTF("entry");
00188 
00189     if (g_path || g_filename)
00190     {
00191         meta_file_close();
00192     }
00193 }
00194 
00195 
00196 void meta_file_open(const gchar* filepath)
00197 {
00198     LOGPRINTF("entry file='%s'", filepath);
00199     
00200     // close if already open
00201     if (g_path || g_filename)
00202     {
00203         meta_file_close();
00204     }
00205 
00206     g_path = g_path_get_dirname(filepath);
00207     g_filename = g_path_get_basename(filepath);
00208 
00209     // initialize to default values
00210     g_should_save = FALSE; 
00211     g_h_position  = DEFAULT_H_POSITION;
00212     g_v_position  = DEFAULT_V_POSITION;
00213     g_zoom_level  = DEFAULT_ZOOM_LEVEL;
00214     g_full_screen = DEFAULT_FULL_SCREEN;
00215 
00216     // try to open database
00217     erMetadb db = ermetadb_local_open(g_path, FALSE);
00218     if (db == NULL) {   // db doesn't exist, ok
00219         LOGPRINTF("No (valid) metadata found for %s", filepath);
00220         return;
00221     }
00222 
00223     // read values from db
00224     metadata_table *names_table = metadata_table_new();
00225     metadata_table_add_column(names_table, META_H_POSITION);
00226     metadata_table_add_column(names_table, META_V_POSITION);
00227     metadata_table_add_column(names_table, META_ZOOM_LEVEL);
00228     metadata_table_add_column(names_table, META_FULL_SCREEN);
00229 
00230     metadata_table *results_table = NULL;
00231     int ret = ermetadb_local_get_application_data(db,
00232                                                   g_filename,
00233                                                   names_table,
00234                                                   &results_table);
00235     if (ret == ER_OK && results_table) {
00236         // assign to static variables
00237         get_double (results_table, META_H_POSITION,  &g_h_position);
00238         get_double (results_table, META_V_POSITION,  &g_v_position);
00239         get_double (results_table, META_ZOOM_LEVEL,  &g_zoom_level);
00240         get_boolean(results_table, META_FULL_SCREEN, &g_full_screen);
00241     } else {
00242         LOGPRINTF("No (valid) application data found for %s, error %d", filepath, ret);
00243     }
00244 
00245     metadata_table_free(names_table);
00246     metadata_table_free(results_table);
00247     ermetadb_close(db);
00248 }
00249 
00250 
00251 gdouble meta_get_h_position(void)
00252 {
00253     return g_h_position;
00254 }
00255 
00256 
00257 gdouble meta_get_v_position(void)
00258 {
00259     return g_v_position;
00260 }
00261 
00262 
00263 gfloat meta_get_zoom_level(void)
00264 {
00265     return (gfloat) g_zoom_level;
00266 }
00267 
00268 
00269 gboolean meta_get_full_screen(void)
00270 {
00271     return g_full_screen;
00272 }
00273 
00274 
00275 void meta_set_h_position(gdouble position)
00276 {
00277     LOGPRINTF("entry");
00278     
00279     if (position != g_h_position)
00280     {
00281         LOGPRINTF("saved h position: %f", position);
00282         g_h_position  = position;
00283         g_should_save = TRUE;
00284     }
00285 }
00286 
00287 
00288 void meta_set_v_position(gdouble position)
00289 {
00290     LOGPRINTF("entry");
00291     
00292     if (position != g_v_position)
00293     {
00294         LOGPRINTF("saved v position: %f", position);
00295         g_v_position  = position;
00296         g_should_save = TRUE;
00297     }
00298 }
00299 
00300 
00301 void meta_set_zoom_level(gfloat factor)
00302 {
00303     LOGPRINTF("entry");
00304     
00305     if (factor != g_zoom_level)
00306     {
00307         LOGPRINTF("saved zoom: %f", factor);
00308         g_zoom_level = factor;
00309         g_should_save = TRUE;
00310     }
00311 }
00312 
00313 
00314 void meta_set_full_screen(gboolean is_full_screen)
00315 {
00316     LOGPRINTF("entry");
00317     
00318     if (is_full_screen != g_full_screen)
00319     {
00320         LOGPRINTF("saved full screen: %d", is_full_screen);
00321         g_full_screen = is_full_screen;
00322         g_should_save = TRUE;
00323     }
00324 }
00325 
00326 
Generated by  doxygen 1.6.2-20100208