]> git.street.me.uk Git - andy/viking.git/blame - src/main.c
Fix error identified by cppcheck 1.52.
[andy/viking.git] / src / main.c
CommitLineData
50a14534
EB
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
a5daec1c
GB
22#ifdef HAVE_CONFIG
23#include "config.h"
24#endif /* HAVE_CONFIG */
25
50a14534 26#include "viking.h"
b6e6dfbd 27#include "icons/icons.h"
50a14534
EB
28#include "mapcache.h"
29#include "background.h"
ad0a8c2d 30#include "dems.h"
18ec873d 31#include "babel.h"
94a036d4 32#include "curl_download.h"
17a1f8f9 33#include "preferences.h"
a58aaed4 34#include "globals.h"
55ddef4e 35#include "vikmapslayer.h"
50a14534 36
a5c8699d
EB
37#ifdef VIK_CONFIG_GEOCACHES
38void a_datasource_gc_init();
39#endif
40
51539ae0 41#ifdef HAVE_STDLIB_H
fd0a7199 42#include <stdlib.h>
51539ae0
GB
43#endif
44#ifdef HAVE_STRING_H
50a14534 45#include <string.h>
51539ae0 46#endif
50a14534 47
57e87d1d 48#include <glib/gprintf.h>
a5daec1c 49#include <glib/gi18n.h>
57e87d1d 50
cdcaf41c
QT
51#include "modules.h"
52
50a14534
EB
53#define MAX_WINDOWS 1024
54
6c6f8d24
MA
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
50a14534
EB
63static guint window_count = 0;
64
65static VikWindow *new_window ();
08812b09 66static void open_window ( VikWindow *vw, GSList *files );
50a14534
EB
67static void destroy( GtkWidget *widget,
68 gpointer data );
69
2936913d
GB
70/* Callback to mute log message */
71static void mute_log(const gchar *log_domain,
72 GLogLevelFlags log_level,
73 const gchar *message,
74 gpointer user_data)
75{
76 /* Nothing to do, we just want to mute */
77}
50a14534
EB
78
79/* Another callback */
80static void destroy( GtkWidget *widget,
81 gpointer data )
82{
83 if ( ! --window_count )
84 gtk_main_quit ();
85}
86
87static VikWindow *new_window ()
88{
89 if ( window_count < MAX_WINDOWS )
90 {
91 VikWindow *vw = vik_window_new ();
92
93 g_signal_connect (G_OBJECT (vw), "destroy",
94 G_CALLBACK (destroy), NULL);
95 g_signal_connect (G_OBJECT (vw), "newwindow",
96 G_CALLBACK (new_window), NULL);
97 g_signal_connect (G_OBJECT (vw), "openwindow",
98 G_CALLBACK (open_window), NULL);
99
100 gtk_widget_show_all ( GTK_WIDGET(vw) );
101
102 window_count++;
103
104 return vw;
105 }
106 return NULL;
107}
108
08812b09 109static void open_window ( VikWindow *vw, GSList *files )
50a14534 110{
08812b09
RN
111 gboolean change_fn = (g_slist_length(files) == 1); /* only change fn if one file */
112 GSList *cur_file = files;
113 while ( cur_file ) {
49bf732a 114 // Only open a new window if a viking file
08812b09
RN
115 gchar *file_name = cur_file->data;
116 if (vw != NULL && check_file_magic_vik ( file_name ) ) {
49bf732a
RN
117 VikWindow *newvw = new_window();
118 if (newvw)
08812b09 119 vik_window_open_file ( newvw, file_name, change_fn );
50a14534 120 }
49bf732a 121 else {
08812b09 122 vik_window_open_file ( vw, file_name, change_fn );
49bf732a 123 }
08812b09
RN
124 g_free (file_name);
125 cur_file = g_slist_next (cur_file);
49bf732a 126 }
08812b09 127 g_slist_free (files);
50a14534
EB
128}
129
53c54171 130/* Options */
fd0a7199
GB
131static GOptionEntry entries[] =
132{
2936913d
GB
133 { "debug", 'd', 0, G_OPTION_ARG_NONE, &vik_debug, N_("Enable debug output"), NULL },
134 { "verbose", 'V', 0, G_OPTION_ARG_NONE, &vik_verbose, N_("Enable verbose output"), NULL },
135 { "version", 'v', 0, G_OPTION_ARG_NONE, &vik_version, N_("Show version"), NULL },
fd0a7199
GB
136 { NULL }
137};
138
50a14534
EB
139int main( int argc, char *argv[] )
140{
141 VikWindow *first_window;
260703bf 142 GdkPixbuf *main_icon;
50a14534
EB
143 gboolean dashdash_already = FALSE;
144 int i = 0;
fd0a7199
GB
145 GError *error = NULL;
146 gboolean gui_initialized;
a5daec1c
GB
147
148 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
149 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
150 textdomain (GETTEXT_PACKAGE);
50a14534
EB
151
152 g_thread_init ( NULL );
153 gdk_threads_init ();
154
fd0a7199
GB
155 gui_initialized = gtk_init_with_args (&argc, &argv, "files+", entries, NULL, &error);
156 if (!gui_initialized)
157 {
158 /* check if we have an error message */
159 if (error == NULL)
160 {
161 /* no error message, the GUI initialization failed */
162 const gchar *display_name = gdk_get_display_arg_name ();
163 g_fprintf (stderr, "Failed to open display: %s\n", (display_name != NULL) ? display_name : " ");
164 }
165 else
166 {
167 /* yep, there's an error, so print it */
168 g_fprintf (stderr, "Parsing command line options failed: %s\n", error->message);
169 g_error_free (error);
170 g_fprintf (stderr, "Run \"%s --help\" to see the list of recognized options.\n",argv[0]);
171 }
172 return EXIT_FAILURE;
173 }
53c54171 174
2936913d 175 if (vik_version)
53c54171 176 {
8192ebe3 177 g_printf ("%s %s\nCopyright (c) 2003-2008 Evan Battaglia\nCopyright (c) 2008-2010 Viking's contributors\n", PACKAGE_NAME, PACKAGE_VERSION);
53c54171
GB
178 return EXIT_SUCCESS;
179 }
50a14534 180
2936913d
GB
181 if (!vik_debug)
182 g_log_set_handler (NULL, G_LOG_LEVEL_DEBUG, mute_log, NULL);
183
843b99df
GB
184 a_preferences_init ();
185
a58aaed4
GB
186 a_vik_preferences_init ();
187
6693f5f9
GB
188 a_download_init();
189 curl_download_init();
190
18ec873d
GB
191 a_babel_init ();
192
cdcaf41c
QT
193 /* Init modules/plugins */
194 modules_init();
195
55ddef4e 196 maps_layer_init ();
50a14534
EB
197 a_mapcache_init ();
198 a_background_init ();
a5c8699d
EB
199
200#ifdef VIK_CONFIG_GEOCACHES
201 a_datasource_gc_init();
202#endif
203
260703bf 204 /* Set the icon */
10f9bcb6 205 main_icon = gdk_pixbuf_from_pixdata(&viking_pixbuf, FALSE, NULL);
260703bf
GB
206 gtk_window_set_default_icon(main_icon);
207
208 /* Create the first window */
50a14534
EB
209 first_window = new_window();
210
211 gdk_threads_enter ();
212 while ( ++i < argc ) {
213 if ( strcmp(argv[i],"--") == 0 && !dashdash_already )
214 dashdash_already = TRUE; /* hack to open '-' */
215 else
216 vik_window_open_file ( first_window, argv[i], argc == 2 );
217 }
218
219 gtk_main ();
220 gdk_threads_leave ();
221
18ec873d
GB
222 a_babel_uninit ();
223
f5e80a61 224 a_background_uninit ();
50a14534 225 a_mapcache_uninit ();
ad0a8c2d 226 a_dems_uninit ();
17a1f8f9 227 a_preferences_uninit ();
50a14534 228
3eba9bbf
RN
229 curl_download_uninit();
230
50a14534
EB
231 return 0;
232}