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