hello-world/src/main.c File Reference

#include "config.h"
#include <glib.h>
#include <gtk/gtk.h>
#include <signal.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include "log.h"
#include "i18n.h"
#include "ipc.h"
#include "main.h"
#include "menu.h"
#include "db.h"
Include dependency graph for hello-world/src/main.c:

Go to the source code of this file.

Functions

static void display_view_type (void)
static void display_flipbar_portrait (void)
static void main_set_text (void)
static gboolean on_startup_complete (gpointer data)
static void usage (const char *argv_0)
static void parse_arguments (int argc, char **argv)
void main_set_locale (const char *locale)
static GtkWidget * create_screen_layout (void)
void main_set_view_type (viewtypes_t view_type)
void main_quit (void)
static void on_sigterm (int signo)
static gboolean do_example_db_actions (gpointer data)
int main (int argc, char *argv[])
void run_error_dialog (const gchar *msg)

Variables

static const char * rc_filename = DATADIR "/" PACKAGE_NAME ".rc"
static GtkWidget * g_title = NULL
static GtkWidget * g_menu = NULL
static GtkWidget * g_pageturn = NULL
static GtkWidget * g_portrait = NULL
static viewtypes_t g_viewtype = -1

Function Documentation

static GtkWidget* create_screen_layout ( void   )  [static]

Definition at line 142 of file hello-world/src/main.c.

References g_action, g_menu, g_pageturn, g_portrait, g_title, g_volume, LOGPRINTF, main_set_text(), and widget.

Referenced by main().

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 }

Here is the call graph for this function:

Here is the caller graph for this function:

static void display_flipbar_portrait ( void   )  [static]

Definition at line 285 of file hello-world/src/main.c.

References g_pageturn, g_portrait, g_viewtype, ipc_sys_is_in_portrait_mode(), ipc_sys_is_pageturn_inverted(), LOGPRINTF, and msg.

Referenced by main_set_view_type().

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 }

Here is the call graph for this function:

Here is the caller graph for this function:

static void display_view_type ( void   )  [static]

Definition at line 258 of file hello-world/src/main.c.

References g_menu, g_viewtype, ICONVIEW, LISTVIEW, LOGPRINTF, and msg.

Referenced by main_set_text(), and main_set_view_type().

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 }

Here is the caller graph for this function:

static gboolean do_example_db_actions ( gpointer  data  )  [static]

Definition at line 367 of file hello-world/src/main.c.

References close_database(), thumbType::data, filename, load_file_metadata(), LOGPRINTF, open_database(), pixbuf_draw_rectangle(), pixbuf_to_blob(), save_file_metadata(), thumbType::size, and WARNPRINTF.

Referenced by main().

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 }

Here is the call graph for this function:

Here is the caller graph for this function:

int main ( int  argc,
char *  argv[] 
)

Definition at line 474 of file hello-world/src/main.c.

References create_screen_layout(), do_example_db_actions(), g_action, g_main_window, g_mountpoint, GETTEXT_PACKAGE, ICONVIEW, ipc_set_services(), LOGPRINTF, main_set_view_type(), menu_init(), msg, on_sigterm(), on_startup_complete(), parse_arguments(), rc_filename, WARNPRINTF, and widget.

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 }

Here is the call graph for this function:

void main_quit ( void   ) 

Definition at line 318 of file hello-world/src/main.c.

References g_main_window, menu_destroy(), and WARNPRINTF.

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 }

Here is the call graph for this function:

void main_set_locale ( const char *  locale  ) 

Definition at line 129 of file hello-world/src/main.c.

References fileview_update_screen_texts(), LOGPRINTF, main_set_text(), and menu_set_text().

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 }

Here is the call graph for this function:

static void main_set_text ( void   )  [static]

Definition at line 232 of file hello-world/src/main.c.

References display_view_type(), g_title, and LOGPRINTF.

Referenced by create_screen_layout(), and main_set_locale().

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 }

Here is the call graph for this function:

Here is the caller graph for this function:

void main_set_view_type ( viewtypes_t  view_type  ) 

Definition at line 245 of file hello-world/src/main.c.

References display_flipbar_portrait(), display_view_type(), g_viewtype, LOGPRINTF, menu_select_view_type(), and N_VIEWTYPES.

Referenced by main(), and menu_on_item_activated().

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 }

Here is the call graph for this function:

Here is the caller graph for this function:

static void on_sigterm ( int  signo  )  [static]

Definition at line 344 of file hello-world/src/main.c.

References main_quit(), and WARNPRINTF.

Referenced by main().

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 }

Here is the call graph for this function:

Here is the caller graph for this function:

static gboolean on_startup_complete ( gpointer  data  )  [static]

Definition at line 356 of file hello-world/src/main.c.

References ipc_sys_startup_complete(), and LOGPRINTF.

Referenced by main().

00357 {
00358     LOGPRINTF("entry");
00359 
00360     ipc_sys_startup_complete();
00361 
00362     return FALSE; // remove timer source
00363 }

Here is the call graph for this function:

Here is the caller graph for this function:

static void parse_arguments ( int  argc,
char **  argv 
) [static]

Definition at line 108 of file hello-world/src/main.c.

References ERRORPRINTF, and usage().

Referenced by main().

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 }

Here is the call graph for this function:

Here is the caller graph for this function:

void run_error_dialog ( const gchar *  msg  ) 

Definition at line 552 of file hello-world/src/main.c.

References ERRORPRINTF, and g_main_window.

Referenced by main().

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 }

Here is the caller graph for this function:

static void usage ( const char *  argv_0  )  [static]

Definition at line 92 of file hello-world/src/main.c.

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 }


Variable Documentation

GtkWidget* g_menu = NULL [static]

Definition at line 70 of file hello-world/src/main.c.

Referenced by create_screen_layout(), and display_view_type().

GtkWidget* g_pageturn = NULL [static]

Definition at line 71 of file hello-world/src/main.c.

Referenced by create_screen_layout(), and display_flipbar_portrait().

GtkWidget* g_portrait = NULL [static]

Definition at line 72 of file hello-world/src/main.c.

Referenced by create_screen_layout(), and display_flipbar_portrait().

GtkWidget* g_title = NULL [static]

Definition at line 69 of file hello-world/src/main.c.

Referenced by create_screen_layout(), and main_set_text().

viewtypes_t g_viewtype = -1 [static]
const char* rc_filename = DATADIR "/" PACKAGE_NAME ".rc" [static]

Copyright (C) 2009 iRex Technologies B.V. All rights reserved.

Definition at line 62 of file hello-world/src/main.c.

Referenced by main().

Generated by  doxygen 1.6.2-20100208