erbrowser/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 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) 2006, 2007 Apple Inc.
00024  * Copyright (C) 2007 Alp Toker <alp@atoker.com>
00025  * Copyright (C) 2008 Collabora Ltd.
00026  * Copyright (C) 2009 iRex Technologies B.V.
00027  * All rights reserved.
00028  */
00029 
00030 //----------------------------------------------------------------------------
00031 // Include Files
00032 //----------------------------------------------------------------------------
00033 
00034 #include "config.h"
00035 
00036 // system include files, between < >
00037 #include <glib.h>
00038 #include <gtk/gtk.h>
00039 #include <signal.h>
00040 #include <unistd.h>
00041 #include <webkit/webkit.h>
00042 #include <libsoup/soup.h>
00043 #include <stdlib.h>
00044 
00045 // ereader include files, between < >
00046 #include <liberutils/display_utils.h>
00047 
00048 // local include files, between " "
00049 #include "log.h"
00050 #include "i18n.h"
00051 #include "ipc.h"
00052 #include "main.h"
00053 #include "menu.h"
00054 #include "view.h"
00055 
00056 #define UNUSED(x) (void)(x)
00057 
00058 
00059 //----------------------------------------------------------------------------
00060 // Type Declarations
00061 //----------------------------------------------------------------------------
00062 
00063 
00064 //----------------------------------------------------------------------------
00065 // Global Constants
00066 //----------------------------------------------------------------------------
00067 
00068 #define COOKIES_FILE "/home/root/cookies.txt"
00069 
00070 static const gfloat zoom_step        = 0.20f;
00071 static const gchar  *rc_filename     = DATADIR "/" PACKAGE_NAME ".rc";
00072 static const gchar  *css_uri         = "file://" DATADIR "/" PACKAGE_NAME ".css";
00073 
00074 
00075 //----------------------------------------------------------------------------
00076 // Static Variables
00077 //---------------------------------------------------------------------------- 
00078 
00079 static WebKitWebSettings *web_settings             = NULL;
00080 static gboolean          g_is_connection_requested = FALSE;
00081 static gboolean          g_run_emall               = FALSE;
00082 
00083 
00084 //============================================================================
00085 // Local Function Definitions
00086 //============================================================================
00087 
00088                                          
00089 //============================================================================
00090 // Functions Implementation
00091 //============================================================================
00092 
00093 void main_quit(void)
00094 {
00095     LOGPRINTF("entry");
00096     
00097     view_destroy();
00098     menu_destroy();
00099     
00100     gtk_main_quit();
00101 }
00102 
00103 
00104 static void on_locale()
00105 {
00106     LOGPRINTF("entry");
00107 
00108     menu_set_text();
00109     view_set_text();
00110 }
00111 
00112 
00113 static void on_connect()
00114 {
00115     LOGPRINTF("entry");
00116     
00117     view_open_last();
00118 }
00119 
00120 
00121 static void on_disconnect()
00122 {
00123     LOGPRINTF("entry");
00124 
00125     if ( g_run_emall || !view_is_page_loaded() )
00126     {
00127         main_quit();
00128     }
00129     
00130     g_is_connection_requested = FALSE;
00131 }
00132 
00133 
00134 gboolean main_request_connection()
00135 {
00136     LOGPRINTF("entry");
00137 
00138     if ( !g_is_connection_requested )
00139     {
00140 #if MACHINE_IS_DR1000SW || MACHINE_IS_DR800SG || MACHINE_IS_DR800SW
00141             if (ipc_sys_connect())
00142             {
00143                 g_is_connection_requested = TRUE;
00144                 return TRUE;
00145             }
00146 #else
00147             WARNPRINTF("Connection requested, but device has no wireless capabilities");
00148 #endif
00149     }
00150     
00151     return FALSE;
00152 }
00153 
00154 // ------------------------------------------------------------------------------------------------
00155 
00156 static void on_terminated(int signo)
00157 {
00158     UNUSED(signo);
00159     WARNPRINTF("    -- entry " PACKAGE_NAME ", my pid [%d]", getpid());
00160     main_quit();
00161 }
00162 
00163 static void on_segment_fault(int signo)
00164 {
00165     UNUSED(signo);
00166     WARNPRINTF("    -- entry " PACKAGE_NAME ", my pid [%d]", getpid());
00167     exit(EXIT_FAILURE);
00168 }
00169 
00170 static void on_fpe(int signo)
00171 {
00172     UNUSED(signo);
00173     WARNPRINTF("    -- entry " PACKAGE_NAME ", my pid [%d]", getpid());
00174     exit(EXIT_FAILURE);
00175 }
00176 
00177 
00178 static gboolean show_web_view_cb(BrowserWindow *window, WebKitWebView *web_view)
00179 {
00180     UNUSED(web_view);
00181     LOGPRINTF("entry");
00182     
00183     gboolean embedded_mode = FALSE;
00184     gboolean locationbar_visible;
00185     gboolean toolbar_visible;
00186     gboolean statusbar_visible;
00187     gboolean scrollbar_visible;
00188     gint width;
00189     gint height;
00190     GtkPolicyType scrollbar_policy;
00191 
00192     embedded_mode = (gboolean)g_object_get_data(G_OBJECT(window->back_listview), "embedded-mode");
00193     g_object_get(G_OBJECT(webkit_web_view_get_window_features(WEBKIT_WEB_VIEW(window->web_view))),
00194                   "locationbar-visible", &locationbar_visible,
00195                   "toolbar-visible", &toolbar_visible,
00196                   "statusbar-visible", &statusbar_visible,
00197                   "scrollbar-visible", &scrollbar_visible,
00198                   "width", &width,
00199                   "height", &height,
00200                   NULL);
00201 
00202     gtk_window_set_default_size(GTK_WINDOW(window->window),
00203                                  width != -1 ? width : 1024,
00204                                  height != -1 ? height : 1280);
00205 
00206     if (scrollbar_visible)
00207     {
00208         scrollbar_policy = GTK_POLICY_AUTOMATIC;
00209     }
00210     else
00211     {
00212         scrollbar_policy = GTK_POLICY_NEVER;
00213     }
00214     gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(window->scrolled_window),
00215                                     scrollbar_policy, scrollbar_policy);
00216 
00217     gtk_widget_show(window->vbox);
00218     if (embedded_mode)
00219     {
00220         gtk_widget_show(window->back_listview);
00221     }
00222     
00223     if (locationbar_visible)
00224     {
00225         gtk_widget_show_all(window->locationbar);
00226     }
00227     
00228     if (toolbar_visible)
00229     {
00230         gtk_widget_show_all(window->toolbar);
00231         // initially hide emall home button
00232         GtkWidget *item = gtk_bin_get_child(GTK_BIN(gtk_toolbar_get_nth_item(GTK_TOOLBAR(window->toolbar), 2)));
00233         gtk_widget_hide(item);
00234     }
00235 
00236     gtk_widget_show_all(window->scrolled_window);
00237 
00238     return TRUE;
00239 }
00240 
00241 
00242 // ------------------------------------------------------------------------------------------------
00243 
00244 int main(int argc, char *argv[])
00245 {
00246     GError              *error              = NULL;
00247     GOptionContext      *context            = NULL;
00248     gboolean            start_fullscreen    = FALSE;
00249     gint                start_zoom          = 100;
00250     gboolean            hide_navbar         = FALSE;
00251     gboolean            hide_scrollbar      = FALSE;
00252     gboolean            embedded_mode       = FALSE;
00253     gchar               *application        = NULL;
00254     gchar               **args              = NULL;
00255     struct sigaction    action;
00256 
00257     GOptionEntry entries[] =  
00258     {
00259         { "fullscreen",    'f', 0, G_OPTION_ARG_NONE, &start_fullscreen, "Start browser fullscreen", NULL },
00260         { "zoom",          'z', 0, G_OPTION_ARG_INT,  &start_zoom,       "Zoom factor in percent ", NULL },
00261         { "no-navbar",     'n', 0, G_OPTION_ARG_NONE, &hide_navbar,      "Don't show navigation bar", NULL },
00262         { "no-scrollbars", 's', 0, G_OPTION_ARG_NONE, &hide_scrollbar,   "Don't show scroll bars", NULL },
00263         { "emall",         'm', 0, G_OPTION_ARG_NONE, &g_run_emall,    "Open browser with eMall", NULL },
00264         { "embedded",      'e', 0, G_OPTION_ARG_STRING, &application,    "Start browser from other application", 
00265                                                         "Application name which launch the browser" },
00266         { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_STRING_ARRAY, &args, NULL, "[URI]"},
00267         { NULL, 0, 0, 0, NULL, NULL, NULL }
00268     };
00269 
00270     // catch the SIGTERM signal
00271     memset(&action, 0x00, sizeof(action));
00272     action.sa_handler = on_terminated;
00273     sigaction(SIGTERM, &action, NULL);
00274 #if LOGGING_ON
00275     sigaction(SIGINT,  &action, NULL);
00276 #endif
00277 
00278     // Catch the SIGSEGV
00279     memset(&action, 0x00, sizeof(action));
00280     action.sa_handler = on_segment_fault; 
00281     sigaction(SIGSEGV, &action, NULL);    
00282 
00283     // Catch the SIGFPE
00284     memset(&action, 0x00, sizeof(action));
00285     action.sa_handler = on_fpe;
00286     sigaction(SIGFPE, &action, NULL);
00287 
00288     // init domain for translations
00289     textdomain(GETTEXT_PACKAGE);
00290     
00291     // init glib and threading
00292     g_type_init();
00293     g_thread_init(NULL);
00294     
00295     // parse commandline arguments
00296     context = g_option_context_new("- list URIs to open in browser");
00297     g_option_context_add_main_entries(context, entries, GETTEXT_PACKAGE);
00298     g_option_context_add_group(context, gtk_get_option_group(TRUE));
00299     if (!g_option_context_parse(context, &argc, &argv, &error)) 
00300     {
00301         WARNPRINTF("Error parsing arguments: %s", error->message);
00302         g_error_free(error);
00303         g_option_context_free(context);
00304         return 1;
00305     }
00306     g_option_context_free(context);
00307     
00308     // init rc files
00309     gchar** files = gtk_rc_get_default_files();
00310     while( *files )
00311     {
00312         LOGPRINTF("gtk_rc_get_default_files [%s]", *files);
00313         files++;
00314     }
00315     
00316     // open the RC file associated with this program
00317     LOGPRINTF("gtk_rc_parse [%s]", rc_filename);
00318     gtk_rc_parse(rc_filename);
00319     
00320     // init modules
00321     ipc_set_services(on_connect, on_disconnect, on_locale);
00322     menu_init();
00323 
00324     // setup persistent cookie jar
00325     SoupSession *session = webkit_get_default_session();    
00326     SoupCookieJar *jar = soup_cookie_jar_text_new(COOKIES_FILE, FALSE);
00327     soup_session_add_feature(session, SOUP_SESSION_FEATURE(jar));
00328     g_object_unref(jar);
00329    
00330     // create web view
00331     BrowserWindow *window = view_create();
00332     g_main_window = window->window;
00333 
00334     // set global settings
00335     web_settings = webkit_web_settings_new();
00336 
00337     // add package name to built-in user-agent string
00338     gchar *user_agent = NULL;
00339     g_object_get(G_OBJECT(web_settings), "user-agent", &user_agent, NULL);
00340     user_agent = g_strconcat(user_agent, " ", PACKAGE_NAME, "/", PACKAGE_VERSION, NULL);
00341     LOGPRINTF("user agent [%s]", user_agent);
00342 
00343     g_object_set(G_OBJECT(web_settings),
00344                  "resizable-text-areas", FALSE,
00345                  "user-stylesheet-uri", css_uri,
00346                  "zoom-step", zoom_step,
00347                  "minimum-font-size", 8,
00348                  "minimum-logical-font-size", 8,
00349                  "user-agent", user_agent,
00350                  NULL);
00351 
00352     webkit_web_view_set_settings (WEBKIT_WEB_VIEW (window->web_view), web_settings);
00353     g_free(user_agent);
00354 
00355     // set window settings
00356     if (g_run_emall)
00357     {
00358         hide_navbar = TRUE;
00359     }
00360 
00361     if (application)
00362     {
00363         embedded_mode = TRUE;
00364         hide_navbar = TRUE;
00365     }
00366     
00367     g_object_set_data(G_OBJECT(window->back_listview), "embedded-mode", (gpointer)embedded_mode);
00368     g_object_set_data(G_OBJECT(window->back_listview), "application", g_strdup(application));
00369     g_object_set(G_OBJECT(webkit_web_view_get_window_features(WEBKIT_WEB_VIEW(window->web_view))),
00370                  "toolbar-visible", g_run_emall, NULL);
00371 
00372     if (hide_navbar)
00373     {
00374         g_object_set(G_OBJECT(webkit_web_view_get_window_features(WEBKIT_WEB_VIEW(window->web_view))),
00375                      "locationbar-visible", FALSE,
00376                      "statusbar-visible", FALSE,
00377                      NULL);
00378     }
00379     
00380     if (hide_scrollbar)
00381     {
00382         g_object_set(G_OBJECT(webkit_web_view_get_window_features(WEBKIT_WEB_VIEW(window->web_view))),
00383                      "scrollbar-visible", FALSE,
00384                      NULL);
00385     }
00386    
00387     if (start_fullscreen)
00388     {
00389         menu_set_full_screen(TRUE);
00390         view_full_screen(TRUE);
00391     }
00392 
00393     display_gain_control();
00394 
00395     // show browser window
00396     view_set_text();
00397     show_web_view_cb(window, WEBKIT_WEB_VIEW(window->web_view));
00398     gtk_widget_show(g_main_window);
00399 
00400     ipc_sys_startup_complete();
00401 
00402     // open url
00403     if (g_run_emall)
00404     {
00405         view_open_emall();
00406     }
00407     else
00408     {
00409         gchar *uri = NULL;
00410         LOGPRINTF("opening URL: %s", uri);
00411         uri = g_strdup((gchar *) (args ? args[0] : "http://www.google.com/"));
00412         view_open_uri(uri);
00413         g_free(uri);
00414     }
00415                             
00416     // run the main loop
00417     LOGPRINTF("before gtk_main");
00418     gdk_threads_enter();
00419     gtk_main();
00420     gdk_threads_leave();    
00421     LOGPRINTF("after gtk_main");
00422 
00423     // clean up
00424     ipc_sys_disconnect();
00425     g_object_unref(web_settings);
00426 
00427     return 0;
00428 }
Generated by  doxygen 1.6.2-20100208