]> git.street.me.uk Git - andy/viking.git/blob - src/main.c
Shift build configurable bits into modules.c
[andy/viking.git] / src / main.c
1 /*
2  * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
3  *
4  * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG
23 #include "config.h"
24 #endif /* HAVE_CONFIG */
25
26 #include "viking.h"
27 #include "icons/icons.h"
28 #include "mapcache.h"
29 #include "background.h"
30 #include "dems.h"
31 #include "babel.h"
32 #include "curl_download.h"
33 #include "preferences.h"
34 #include "viklayer_defaults.h"
35 #include "globals.h"
36 #include "vikmapslayer.h"
37 #include "vikgeoreflayer.h"
38 #include "vikrouting.h"
39 #include "vikutils.h"
40 #include "util.h"
41 #include "toolbar.h"
42
43 #ifdef HAVE_STDLIB_H
44 #include <stdlib.h>
45 #endif
46 #ifdef HAVE_STRING_H
47 #include <string.h>
48 #endif
49
50 #include <glib/gprintf.h>
51 #include <glib/gi18n.h>
52
53 #include "modules.h"
54
55 /* FIXME LOCALEDIR must be configured by ./configure --localedir */
56 /* But something does not work actually. */
57 /* So, we need to redefine this variable on windows. */
58 #ifdef WINDOWS
59 #undef LOCALEDIR
60 #define LOCALEDIR "locale"
61 #endif
62
63 #ifdef HAVE_X11_XLIB_H
64 #include "X11/Xlib.h"
65 #endif
66
67 #if GLIB_CHECK_VERSION (2, 32, 0)
68 /* Callback to log message */
69 static void log_debug(const gchar *log_domain,
70                       GLogLevelFlags log_level,
71                       const gchar *message,
72                       gpointer user_data)
73 {
74   g_print("** (viking): DEBUG: %s\n", message);
75 }
76 #else
77 /* Callback to mute log message */
78 static void mute_log(const gchar *log_domain,
79                      GLogLevelFlags log_level,
80                      const gchar *message,
81                      gpointer user_data)
82 {
83   /* Nothing to do, we just want to mute */
84 }
85 #endif
86
87 #if HAVE_X11_XLIB_H
88 static int myXErrorHandler(Display *display, XErrorEvent *theEvent)
89 {
90   g_fprintf (stderr,
91              _("Ignoring Xlib error: error code %d request code %d\n"),
92              theEvent->error_code,
93              theEvent->request_code);
94   // No exit on X errors!
95   //  mainly to handle out of memory error when requesting large pixbuf from user request
96   //  see vikwindow.c::save_image_file ()
97   return 0;
98 }
99 #endif
100
101 /* Options */
102 static GOptionEntry entries[] = 
103 {
104   { "debug", 'd', 0, G_OPTION_ARG_NONE, &vik_debug, N_("Enable debug output"), NULL },
105   { "verbose", 'V', 0, G_OPTION_ARG_NONE, &vik_verbose, N_("Enable verbose output"), NULL },
106   { "version", 'v', 0, G_OPTION_ARG_NONE, &vik_version, N_("Show version"), NULL },
107   { NULL }
108 };
109
110 int main( int argc, char *argv[] )
111 {
112   VikWindow *first_window;
113   GdkPixbuf *main_icon;
114   gboolean dashdash_already = FALSE;
115   int i = 0;
116   GError *error = NULL;
117   gboolean gui_initialized;
118         
119   bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);  
120   bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
121   textdomain (GETTEXT_PACKAGE);
122
123 #if ! GLIB_CHECK_VERSION (2, 32, 0)
124   g_thread_init ( NULL );
125 #endif
126   gdk_threads_init ();
127
128   gui_initialized = gtk_init_with_args (&argc, &argv, "files+", entries, NULL, &error);
129   if (!gui_initialized)
130   {
131     /* check if we have an error message */
132     if (error == NULL)
133     {
134       /* no error message, the GUI initialization failed */
135       const gchar *display_name = gdk_get_display_arg_name ();
136       g_fprintf (stderr, "Failed to open display: %s\n", (display_name != NULL) ? display_name : " ");
137     }
138     else
139     {
140       /* yep, there's an error, so print it */
141       g_fprintf (stderr, "Parsing command line options failed: %s\n", error->message);
142       g_error_free (error);
143       g_fprintf (stderr, "Run \"%s --help\" to see the list of recognized options.\n",argv[0]);
144     }
145     return EXIT_FAILURE;
146   }
147    
148   if (vik_version)
149   {
150     g_printf ("%s %s\nCopyright (c) 2003-2008 Evan Battaglia\nCopyright (c) 2008-2012 Viking's contributors\n", PACKAGE_NAME, PACKAGE_VERSION);
151     return EXIT_SUCCESS;
152   }
153
154 #if GLIB_CHECK_VERSION (2, 32, 0)
155   if (vik_debug)
156     g_log_set_handler (NULL, G_LOG_LEVEL_DEBUG, log_debug, NULL);
157 #else
158   if (!vik_debug)
159     g_log_set_handler (NULL, G_LOG_LEVEL_DEBUG, mute_log, NULL);
160 #endif
161
162 #if HAVE_X11_XLIB_H
163   XSetErrorHandler(myXErrorHandler);
164 #endif
165
166   // Discover if this is the very first run
167   a_vik_very_first_run ();
168
169   a_settings_init ();
170   a_preferences_init ();
171
172   a_vik_preferences_init ();
173
174   a_layer_defaults_init ();
175
176   a_download_init();
177   curl_download_init();
178
179   a_babel_init ();
180
181   /* Init modules/plugins */
182   modules_init();
183
184   vik_georef_layer_init ();
185   maps_layer_init ();
186   a_mapcache_init ();
187   a_background_init ();
188
189   a_toolbar_init();
190   vik_routing_prefs_init();
191
192   if ( a_vik_get_time_ref_frame() == VIK_TIME_REF_WORLD )
193     vu_setup_lat_lon_tz_lookup();
194
195   /* Set the icon */
196   main_icon = gdk_pixbuf_from_pixdata(&viking_pixbuf, FALSE, NULL);
197   gtk_window_set_default_icon(main_icon);
198
199   gdk_threads_enter ();
200
201   // Ask for confirmation of default settings on first run
202   vu_set_auto_features_on_first_run ();
203
204   /* Create the first window */
205   first_window = vik_window_new_window();
206
207   vu_check_latest_version ( GTK_WINDOW(first_window) );
208
209   while ( ++i < argc ) {
210     if ( strcmp(argv[i],"--") == 0 && !dashdash_already )
211       dashdash_already = TRUE; /* hack to open '-' */
212     else {
213       VikWindow *newvw = first_window;
214       gboolean change_filename = (i == 1);
215
216       // Open any subsequent .vik files in their own window
217       if ( i > 1 && check_file_magic_vik ( argv[i] ) ) {
218         newvw = vik_window_new_window ();
219         change_filename = TRUE;
220       }
221
222       vik_window_open_file ( newvw, argv[i], change_filename );
223     }
224   }
225
226   vik_window_new_window_finish ( first_window );
227
228   gtk_main ();
229   gdk_threads_leave ();
230
231   a_babel_uninit ();
232   a_toolbar_uninit ();
233   a_background_uninit ();
234   a_mapcache_uninit ();
235   a_dems_uninit ();
236   a_layer_defaults_uninit ();
237   a_preferences_uninit ();
238   a_settings_uninit ();
239
240   curl_download_uninit();
241
242   vu_finalize_lat_lon_tz_lookup ();
243
244   // Clean up any temporary files
245   util_remove_all_in_deletion_list ();
246
247   return 0;
248 }