]> git.street.me.uk Git - andy/viking.git/blob - src/main.c
Enable Cache conversion in the Python tool viking-cache.py
[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 "vikrouting.h"
38 #include "vikutils.h"
39 #include "util.h"
40 #include "toolbar.h"
41
42 #ifdef VIK_CONFIG_GEOCACHES
43 void a_datasource_gc_init();
44 #endif
45
46 #ifdef HAVE_STDLIB_H
47 #include <stdlib.h>
48 #endif
49 #ifdef HAVE_STRING_H
50 #include <string.h>
51 #endif
52
53 #include <glib/gprintf.h>
54 #include <glib/gi18n.h>
55
56 #include "modules.h"
57
58 /* FIXME LOCALEDIR must be configured by ./configure --localedir */
59 /* But something does not work actually. */
60 /* So, we need to redefine this variable on windows. */
61 #ifdef WINDOWS
62 #undef LOCALEDIR
63 #define LOCALEDIR "locale"
64 #endif
65
66 #ifdef HAVE_X11_XLIB_H
67 #include "X11/Xlib.h"
68 #endif
69
70 #if GLIB_CHECK_VERSION (2, 32, 0)
71 /* Callback to log message */
72 static void log_debug(const gchar *log_domain,
73                       GLogLevelFlags log_level,
74                       const gchar *message,
75                       gpointer user_data)
76 {
77   g_print("** (viking): DEBUG: %s\n", message);
78 }
79 #else
80 /* Callback to mute log message */
81 static void mute_log(const gchar *log_domain,
82                      GLogLevelFlags log_level,
83                      const gchar *message,
84                      gpointer user_data)
85 {
86   /* Nothing to do, we just want to mute */
87 }
88 #endif
89
90 #if HAVE_X11_XLIB_H
91 static int myXErrorHandler(Display *display, XErrorEvent *theEvent)
92 {
93   g_fprintf (stderr,
94              _("Ignoring Xlib error: error code %d request code %d\n"),
95              theEvent->error_code,
96              theEvent->request_code);
97   // No exit on X errors!
98   //  mainly to handle out of memory error when requesting large pixbuf from user request
99   //  see vikwindow.c::save_image_file ()
100   return 0;
101 }
102 #endif
103
104 /* Options */
105 static GOptionEntry entries[] = 
106 {
107   { "debug", 'd', 0, G_OPTION_ARG_NONE, &vik_debug, N_("Enable debug output"), NULL },
108   { "verbose", 'V', 0, G_OPTION_ARG_NONE, &vik_verbose, N_("Enable verbose output"), NULL },
109   { "version", 'v', 0, G_OPTION_ARG_NONE, &vik_version, N_("Show version"), NULL },
110   { NULL }
111 };
112
113 int main( int argc, char *argv[] )
114 {
115   VikWindow *first_window;
116   GdkPixbuf *main_icon;
117   gboolean dashdash_already = FALSE;
118   int i = 0;
119   GError *error = NULL;
120   gboolean gui_initialized;
121         
122   bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);  
123   bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
124   textdomain (GETTEXT_PACKAGE);
125
126 #if ! GLIB_CHECK_VERSION (2, 32, 0)
127   g_thread_init ( NULL );
128 #endif
129   gdk_threads_init ();
130
131   gui_initialized = gtk_init_with_args (&argc, &argv, "files+", entries, NULL, &error);
132   if (!gui_initialized)
133   {
134     /* check if we have an error message */
135     if (error == NULL)
136     {
137       /* no error message, the GUI initialization failed */
138       const gchar *display_name = gdk_get_display_arg_name ();
139       g_fprintf (stderr, "Failed to open display: %s\n", (display_name != NULL) ? display_name : " ");
140     }
141     else
142     {
143       /* yep, there's an error, so print it */
144       g_fprintf (stderr, "Parsing command line options failed: %s\n", error->message);
145       g_error_free (error);
146       g_fprintf (stderr, "Run \"%s --help\" to see the list of recognized options.\n",argv[0]);
147     }
148     return EXIT_FAILURE;
149   }
150    
151   if (vik_version)
152   {
153     g_printf ("%s %s\nCopyright (c) 2003-2008 Evan Battaglia\nCopyright (c) 2008-2012 Viking's contributors\n", PACKAGE_NAME, PACKAGE_VERSION);
154     return EXIT_SUCCESS;
155   }
156
157 #if GLIB_CHECK_VERSION (2, 32, 0)
158   if (vik_debug)
159     g_log_set_handler (NULL, G_LOG_LEVEL_DEBUG, log_debug, NULL);
160 #else
161   if (!vik_debug)
162     g_log_set_handler (NULL, G_LOG_LEVEL_DEBUG, mute_log, NULL);
163 #endif
164
165 #if HAVE_X11_XLIB_H
166   XSetErrorHandler(myXErrorHandler);
167 #endif
168
169   // Discover if this is the very first run
170   a_vik_very_first_run ();
171
172   a_settings_init ();
173   a_preferences_init ();
174
175   a_vik_preferences_init ();
176
177   a_layer_defaults_init ();
178
179   a_download_init();
180   curl_download_init();
181
182   a_babel_init ();
183
184   /* Init modules/plugins */
185   modules_init();
186
187   maps_layer_init ();
188   a_mapcache_init ();
189   a_background_init ();
190
191 #ifdef VIK_CONFIG_GEOCACHES
192   a_datasource_gc_init();
193 #endif
194
195   a_toolbar_init();
196   vik_routing_prefs_init();
197
198   if ( a_vik_get_time_ref_frame() == VIK_TIME_REF_WORLD )
199     vu_setup_lat_lon_tz_lookup();
200
201   /* Set the icon */
202   main_icon = gdk_pixbuf_from_pixdata(&viking_pixbuf, FALSE, NULL);
203   gtk_window_set_default_icon(main_icon);
204
205   gdk_threads_enter ();
206
207   // Ask for confirmation of default settings on first run
208   vu_set_auto_features_on_first_run ();
209
210   /* Create the first window */
211   first_window = vik_window_new_window();
212
213   vu_check_latest_version ( GTK_WINDOW(first_window) );
214
215   while ( ++i < argc ) {
216     if ( strcmp(argv[i],"--") == 0 && !dashdash_already )
217       dashdash_already = TRUE; /* hack to open '-' */
218     else {
219       VikWindow *newvw = first_window;
220       gboolean change_filename = (i == 1);
221
222       // Open any subsequent .vik files in their own window
223       if ( i > 1 && check_file_magic_vik ( argv[i] ) ) {
224         newvw = vik_window_new_window ();
225         change_filename = TRUE;
226       }
227
228       vik_window_open_file ( newvw, argv[i], change_filename );
229     }
230   }
231
232   vik_window_new_window_finish ( first_window );
233
234   gtk_main ();
235   gdk_threads_leave ();
236
237   a_babel_uninit ();
238   a_toolbar_uninit ();
239   a_background_uninit ();
240   a_mapcache_uninit ();
241   a_dems_uninit ();
242   a_layer_defaults_uninit ();
243   a_preferences_uninit ();
244   a_settings_uninit ();
245
246   curl_download_uninit();
247
248   vu_finalize_lat_lon_tz_lookup ();
249
250   // Clean up any temporary files
251   util_remove_all_in_deletion_list ();
252
253   return 0;
254 }