hello-world/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 hello-world.
00007  *
00008  * hello-world 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  * hello-world 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 <gtk/gtk.h>
00036 #include <signal.h>
00037 #include <string.h>
00038 #include <stdlib.h>
00039 #include <sys/types.h>
00040 #include <unistd.h>
00041 
00042 // ereader include files, between < >
00043 
00044 // local include files, between " "
00045 #include "log.h"
00046 #include "i18n.h"
00047 #include "ipc.h"
00048 #include "main.h"
00049 #include "menu.h"
00050 #include "db.h"
00051 
00052 
00053 //----------------------------------------------------------------------------
00054 // Type Declarations
00055 //----------------------------------------------------------------------------
00056 
00057 
00058 //----------------------------------------------------------------------------
00059 // Constants
00060 //----------------------------------------------------------------------------
00061 
00062 static const char  *rc_filename = DATADIR "/" PACKAGE_NAME ".rc";
00063 
00064 
00065 //----------------------------------------------------------------------------
00066 // Static Variables
00067 //----------------------------------------------------------------------------
00068 
00069 static GtkWidget        *g_title = NULL;        // screen title
00070 static GtkWidget        *g_menu  = NULL;        // latest menu selection
00071 static GtkWidget        *g_pageturn = NULL ; 
00072 static GtkWidget        *g_portrait = NULL ; 
00073 
00074 static viewtypes_t      g_viewtype = -1;        // currently selected view type
00075 
00076 
00077 //============================================================================
00078 // Local Function Definitions
00079 //============================================================================
00080 
00081 static void     display_view_type       (void);
00082 static void     display_flipbar_portrait(void);
00083 static void     main_set_text           (void);
00084 static gboolean on_startup_complete     (gpointer data);
00085 
00086 
00087 //============================================================================
00088 // Functions Implementation
00089 //============================================================================
00090 
00091 // print usage text and quit
00092 static void usage (const char *argv_0)
00093 {
00094     static const char *usage_text = 
00095                         "\n"
00096                         "usage: %s [options]\n"
00097                         "\n"
00098                         "options:\n"
00099                         "    --help\n"
00100                         "        Print help text and quit\n";
00101 
00102     printf(usage_text, argv_0);
00103 
00104     exit(1);
00105 }
00106 
00107 // get command-line options
00108 static void parse_arguments (int argc, char **argv)
00109 {
00110     int  i;
00111 
00112     // parse options
00113     for (i = 1 ; i < argc ; i++)
00114     {
00115         if (strcmp(argv[i], "--help") == 0)
00116         {
00117             usage(argv[0]);
00118         }
00119         else
00120         {
00121             ERRORPRINTF("ignore unknown option [%s]", argv[i]);
00122             usage(argv[0]);
00123         }
00124     }
00125 }
00126 
00127 
00128 // set locale (language)
00129 void main_set_locale (const char *locale)
00130 {
00131     LOGPRINTF("entry: locale [%s]", locale);
00132 
00133     g_setenv("LANG", locale, TRUE);
00134     setlocale(LC_ALL, "");
00135 
00136     main_set_text();
00137     menu_set_text();
00138 }
00139 
00140 
00141 // create main screen layout
00142 static GtkWidget *create_screen_layout (void)
00143 {
00144     GtkWidget   *background = NULL;  // return value
00145     GtkWidget   *widget;
00146     GtkWidget   **p_widget;
00147     GtkBox      *vbox;
00148 
00149     LOGPRINTF("entry");
00150 
00151     // object hierarchy:
00152     //     background (alignment)
00153     //       |
00154     widget = gtk_alignment_new(0.0, 0.0, 1.0, 1.0);
00155     gtk_alignment_set_padding( GTK_ALIGNMENT(widget), 100, 100, 100, 100);
00156     gtk_widget_show(widget);
00157     background = widget;
00158     //       |
00159     //       |-- vbox
00160     //             |
00161     widget = gtk_vbox_new(FALSE, 100);
00162     gtk_container_add(GTK_CONTAINER(background), widget);
00163     gtk_widget_show(widget);
00164     vbox = GTK_BOX(widget);
00165     //             |
00166     //             |-- g_title (label)
00167     //             |
00168     p_widget = &g_title;
00169     widget = gtk_label_new(NULL);
00170     gtk_widget_set_name(widget, "irex-hello-title");
00171     gtk_box_pack_start(vbox, widget, FALSE, FALSE, 0);
00172     g_signal_connect(widget, "destroy", G_CALLBACK(gtk_widget_destroyed), p_widget);
00173     gtk_widget_show(widget);
00174     g_assert(*p_widget == NULL);
00175     *p_widget = widget;
00176     //             |
00177     //             |-- g_menu (label)
00178     p_widget = &g_menu;
00179     widget = gtk_label_new(NULL);
00180     gtk_widget_set_name(widget, "irex-hello-menu");
00181     gtk_box_pack_start(vbox, widget, FALSE, FALSE, 0);
00182     g_signal_connect(widget, "destroy", G_CALLBACK(gtk_widget_destroyed), p_widget);
00183     gtk_widget_show(widget);
00184     g_assert(*p_widget == NULL);
00185     *p_widget = widget;
00186     //             |
00187     //             |-- g_pageturn (label)
00188     p_widget = &g_pageturn;
00189     widget = gtk_label_new(NULL);
00190     gtk_widget_set_name(widget, "irex-hello-menu");
00191     gtk_box_pack_start(vbox, widget, FALSE, FALSE, 0);
00192     g_signal_connect(widget, "destroy", G_CALLBACK(gtk_widget_destroyed), p_widget);
00193     gtk_widget_show(widget);
00194     g_assert(*p_widget == NULL);
00195     *p_widget = widget;
00196     //             |
00197     //             |-- g_portrait (label)
00198     p_widget = &g_portrait;
00199     widget = gtk_label_new(NULL);
00200     gtk_widget_set_name(widget, "irex-hello-menu");
00201     gtk_box_pack_start(vbox, widget, FALSE, FALSE, 0);
00202     g_signal_connect(widget, "destroy", G_CALLBACK(gtk_widget_destroyed), p_widget);
00203     gtk_widget_show(widget);
00204     g_assert(*p_widget == NULL);
00205     *p_widget = widget;
00206     //             |
00207     //             |-- g_volume (label)
00208     p_widget = &g_volume;
00209     widget = gtk_label_new(NULL);
00210     gtk_box_pack_start(vbox, widget, FALSE, FALSE, 0);
00211     g_signal_connect(widget, "destroy", G_CALLBACK(gtk_widget_destroyed), p_widget);
00212     gtk_widget_show(widget);
00213     g_assert(*p_widget == NULL);
00214     *p_widget = widget;
00215     //             |
00216     //             |-- g_action (label)
00217     p_widget = &g_action;
00218     widget = gtk_label_new(NULL);
00219     gtk_box_pack_start(vbox, widget, FALSE, FALSE, 0);
00220     g_signal_connect(widget, "destroy", G_CALLBACK(gtk_widget_destroyed), p_widget);
00221     gtk_widget_show(widget);
00222     g_assert(*p_widget == NULL);
00223     *p_widget = widget;
00224     
00225     main_set_text();
00226     
00227     return background;
00228 }
00229 
00230 
00231 // set screen texts
00232 static void main_set_text (void)
00233 {
00234     LOGPRINTF("entry");
00235 
00236     if (g_title)
00237     {
00238         gtk_label_set_text( GTK_LABEL(g_title), _("Hello world") );
00239     }
00240     display_view_type();
00241 }
00242 
00243 
00244 // show selected view type
00245 void main_set_view_type ( viewtypes_t view_type )
00246 {
00247     LOGPRINTF("entry: view_type [%d]", view_type);
00248     g_return_if_fail(view_type < N_VIEWTYPES);
00249 
00250     g_viewtype = view_type;
00251     display_view_type();
00252     display_flipbar_portrait();
00253     menu_select_view_type(view_type);
00254 }
00255 
00256 
00257 // show selected view type
00258 static void display_view_type ( void )
00259 {
00260     gchar       *msg  = NULL;
00261     gchar       *type = NULL;
00262 
00263     LOGPRINTF("entry: view_type [%d]", g_viewtype);
00264 
00265     if (g_menu)
00266     {
00267         switch (g_viewtype)
00268         {
00269             case ICONVIEW:
00270                 type = _("thumbnails");
00271                 break;
00272             case LISTVIEW:
00273                 type = _("details");
00274                 break;
00275             default:
00276                 type = _("unknown");
00277         }
00278 
00279         msg = g_strdup_printf( _("Current view type: %d (%s)"), g_viewtype, type);
00280         gtk_label_set_text( GTK_LABEL(g_menu), msg );
00281         g_free(msg);
00282     }
00283 }
00284 
00285 static void display_flipbar_portrait( void)
00286 {
00287     LOGPRINTF("entry: view_type [%d]", g_viewtype);
00288 
00289     gchar *msg = NULL ; 
00290     gchar *portrait = NULL ; 
00291     gchar *pageturning = NULL ; 
00292 
00293     gboolean is_inverted = ipc_sys_is_pageturn_inverted();
00294     gboolean is_portrait = ipc_sys_is_in_portrait_mode();
00295 
00296     if (is_inverted ) 
00297         pageturning = _("inverted");
00298     else 
00299         pageturning = _("normal");
00300 
00301     if (is_portrait ) 
00302         portrait = _("portrait");
00303     else 
00304         portrait = _("landscape");
00305 
00306     msg = g_strdup_printf( _("Pageturn mode: %d (%s)"), is_inverted, pageturning);
00307     gtk_label_set_text( GTK_LABEL(g_pageturn), msg );
00308     g_free(msg);
00309 
00310     msg = g_strdup_printf( _("Initial orientation: %d (%s)"), is_portrait, portrait);
00311     gtk_label_set_text( GTK_LABEL(g_portrait), msg );
00312     g_free(msg);
00313 
00314 }
00315 
00316 
00317 // terminate application
00318 void main_quit (void)
00319 {
00320     WARNPRINTF("entry");
00321     
00322     menu_destroy();
00323     
00324     if (g_main_window)
00325     {
00326         gtk_widget_destroy(g_main_window);
00327         g_main_window = NULL;
00328     }    
00329     
00330     if (gtk_main_level() > 0)
00331     {
00332         WARNPRINTF("quit main loop");
00333         gtk_main_quit();
00334     }
00335     else
00336     {
00337         WARNPRINTF("no main loop to quit, exit directly");
00338         _exit(0);
00339     }
00340 }
00341 
00342 
00343 // terminate signal
00344 static void on_sigterm (int signo)
00345 {
00346     WARNPRINTF("    -- entry " PACKAGE_NAME ", my pid [%d]", getpid());
00347 
00348     // stop main process, prepare to quit application
00349     main_quit();
00350 
00351     WARNPRINTF("    -- leave " PACKAGE_NAME);
00352 }
00353 
00354 
00355 // report startup completed
00356 static gboolean on_startup_complete (gpointer data)
00357 {
00358     LOGPRINTF("entry");
00359 
00360     ipc_sys_startup_complete();
00361 
00362     return FALSE; // remove timer source
00363 }
00364 
00365 
00366 //  do some actions on the metadb
00367 static gboolean do_example_db_actions(gpointer data)
00368 {
00369     // This function demonstrates the necessary steps to update the metadata
00370     // of a document to store/update the:
00371     // - title
00372     // - author 
00373     // - thumbnail (two sizes)
00374     // - number of pages
00375     // - current page
00376     
00377     printf("To run this application, make sure that you have the following files in\n"
00378             "the \"/media/mmcblk0p1/Examples\" dir:\n"
00379             " - \"example.epub\" \n"
00380             " - \"example.png\" \n");
00381 
00382     LOGPRINTF("entry");
00383     gboolean result = TRUE;
00384     erMetadb my_db = open_database("/media/mmcblk0p1");
00385     if ( my_db == NULL ) 
00386     {
00387         WARNPRINTF("Could not open metadb at dir %s\n", "/media/mmcblk0p1");
00388         goto err_open_database;
00389     }
00390 
00391     const gchar* filepath = "Examples";
00392     const gchar* filename = "example.epub";
00393     
00394 #if 0
00395     GString* title = g_string_new(NULL);
00396     GString* author = g_string_new(NULL);
00397 
00398     result = load_file_metadata(my_db,   
00399                                 filepath,
00400                                 filename,
00401                                 title,   
00402                                 author,  
00403                                 NULL,    
00404                                 NULL     
00405                                 );
00406 
00407     if (result == FALSE)
00408     {
00409         WARNPRINTF(" -- FAIL: load_file_metadata: result = %d, could not load metadata for file: %s\n", result, filename );
00410         goto err_load_metadata;
00411     }
00412 #else
00413     GString* title = g_string_new("This is the title");
00414     GString* author = g_string_new("I am the author");
00415 #endif 
00416 
00417     // load PNG file and scale it to thumb sizes -- these functions are blocking
00418     GdkPixbuf* small = NULL;
00419     GdkPixbuf* medium = NULL;
00420     small  = gdk_pixbuf_new_from_file_at_scale("/media/mmcblk0p1/Examples/example.png",  60,  60, TRUE, NULL);
00421     if ( small  == NULL ) { goto err_no_small; }
00422 
00423     medium = gdk_pixbuf_new_from_file_at_scale("/media/mmcblk0p1/EXamples/example.png", 120, 120, TRUE, NULL);
00424     if ( medium == NULL ) { goto err_no_medium; }
00425 
00426     // draw a rectangle around the thumbs
00427     gint width = gdk_pixbuf_get_width(small);
00428     gint height = gdk_pixbuf_get_height(small);
00429     pixbuf_draw_rectangle(small, 0, 0, width, height, 0x00000000);
00430     width = gdk_pixbuf_get_width(medium);
00431     height = gdk_pixbuf_get_height(medium);
00432     pixbuf_draw_rectangle(medium, 0, 0, width, height, 0x00000000);
00433 
00434     thumbType small_thumb = { NULL, 0 };
00435     thumbType medium_thumb= { NULL, 0 } ;
00436 
00437     // convert to BLOB
00438     pixbuf_to_blob(small,  (gchar**) &(small_thumb.data),  (gsize*) &small_thumb.size);
00439     pixbuf_to_blob(medium,  (gchar**) &(medium_thumb.data),  (gsize*) &medium_thumb.size);
00440 
00441     result = save_file_metadata(my_db,              // erMetadb* database
00442                                 filepath,
00443                                 filename,
00444                                 title,              // GString* title
00445                                 author,             // GString* author
00446                                 &small_thumb,       // thumbType* small
00447                                 &medium_thumb       // thumbType* medium
00448                                 );
00449     
00450     if (result == FALSE)
00451     {
00452         WARNPRINTF(" -- FAIL: save_file_metadata: result = %d, could not save metadata for file: %s\n", result, filename);
00453         goto err_save_metadata;
00454     }
00455 
00456     // clean up
00457 err_save_metadata:
00458     g_object_unref(medium);
00459 err_no_medium:
00460     g_object_unref(small);
00461 err_no_small:
00462 #if 0 
00463 err_load_metadata:
00464 #endif 
00465     g_string_free(author, TRUE);
00466     g_string_free(title, TRUE);
00467     close_database(my_db);
00468 err_open_database:
00469     return FALSE;
00470 }
00471 
00472 
00473 // main function
00474 int main (int argc, char *argv[])
00475 {
00476     GtkWidget        *window;
00477     GtkWidget        *widget;
00478     struct sigaction on_term;
00479 
00480     // parse command-line arguments
00481     parse_arguments(argc, argv);
00482 
00483     // catch the SIGTERM signal
00484     memset(&on_term, 0x00, sizeof(on_term));
00485     on_term.sa_handler = on_sigterm;
00486     sigaction(SIGTERM, &on_term, NULL);
00487 #if LOGGING_ON
00488     sigaction(SIGINT,  &on_term, NULL);
00489 #endif
00490 
00491     // init domain for translations
00492     textdomain(GETTEXT_PACKAGE);
00493     
00494     // init gtk, list the default rc files
00495     gtk_init(&argc, &argv);
00496     gchar** files = gtk_rc_get_default_files();
00497     while( *files )
00498     {
00499         WARNPRINTF("gtk_rc_get_default_files [%s]", *files);
00500         files++;
00501     }
00502 
00503     // init modules
00504     g_mountpoint = NULL;
00505     ipc_set_services();
00506     menu_init();
00507 
00508     // open the RC file associated with this program
00509     WARNPRINTF("gtk_rc_parse [%s]", rc_filename);
00510     gtk_rc_parse(rc_filename);
00511 
00512     // create the top level window 
00513     window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
00514     gtk_window_set_title(GTK_WINDOW(window), PACKAGE " " VERSION);
00515     gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
00516     gtk_container_set_border_width(GTK_CONTAINER(window), 0);
00517     g_main_window = window;
00518 
00519     // add screen details
00520     widget = create_screen_layout();
00521     gtk_container_add(GTK_CONTAINER(window), widget);
00522     main_set_view_type(ICONVIEW);
00523 
00524     // make sure everything is visible 
00525     gtk_widget_show(window);
00526 
00527     if (argc > 0)
00528     {
00529         gchar *msg = NULL;
00530         msg = g_strdup_printf("Started with: %s", argv[1]);
00531         gtk_label_set_text( GTK_LABEL(g_action), msg);
00532         LOGPRINTF("%s", msg);
00533         g_free(msg);
00534     }
00535     
00536     // tell system daemon we are ready to go
00537     g_idle_add(on_startup_complete, NULL);
00538 
00539     // do some actions on the metadb
00540     g_idle_add(do_example_db_actions, NULL);
00541     
00542     // run the main loop
00543     LOGPRINTF("before gtk_main");
00544     gtk_main();
00545     LOGPRINTF("after gtk_main");
00546 
00547     return 0;
00548 }
00549 
00550 
00551 // run error dialog
00552 void run_error_dialog (const gchar *msg)
00553 {
00554     GtkWidget   *dialog = NULL;
00555 
00556     ERRORPRINTF("entry: msg [%s]", msg);
00557 
00558     dialog = gtk_message_dialog_new( GTK_WINDOW(g_main_window),
00559                                      GTK_DIALOG_DESTROY_WITH_PARENT,
00560                                      GTK_MESSAGE_ERROR,
00561                                      GTK_BUTTONS_OK,
00562                                      msg );
00563 
00564     gtk_dialog_run( GTK_DIALOG(dialog) );
00565     gtk_widget_destroy( dialog );
00566 }
Generated by  doxygen 1.6.2-20100208