]> git.street.me.uk Git - andy/viking.git/blob - src/main.c
Fix crashing on opening multiple files after a .vik file is opened.
[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 #define MAX_WINDOWS 1024
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 static guint window_count = 0;
64
65 static VikWindow *new_window ();
66 static void open_window ( VikWindow *vw, GSList *files );
67 static void destroy( GtkWidget *widget,
68                      gpointer   data );
69
70 /* Callback to mute log message */
71 static 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 }
78
79 /* Another callback */
80 static void destroy( GtkWidget *widget,
81                      gpointer   data )
82 {
83     if ( ! --window_count )
84       gtk_main_quit ();
85 }
86
87 static 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
109 static void open_window ( VikWindow *vw, GSList *files )
110 {
111   gboolean change_fn = (g_slist_length(files) == 1); /* only change fn if one file */
112   GSList *cur_file = files;
113   while ( cur_file ) {
114     // Only open a new window if a viking file
115     gchar *file_name = cur_file->data;
116     if (vw != NULL && check_file_magic_vik ( file_name ) ) {
117       VikWindow *newvw = new_window();
118       if (newvw)
119         vik_window_open_file ( newvw, file_name, change_fn );
120     }
121     else {
122       vik_window_open_file ( vw, file_name, change_fn );
123     }
124     g_free (file_name);
125     cur_file = g_slist_next (cur_file);
126   }
127   g_slist_free (files);
128 }
129
130 /* Options */
131 static GOptionEntry entries[] = 
132 {
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 },
136   { NULL }
137 };
138
139 int main( int argc, char *argv[] )
140 {
141   VikWindow *first_window;
142   GdkPixbuf *main_icon;
143   gboolean dashdash_already = FALSE;
144   int i = 0;
145   GError *error = NULL;
146   gboolean gui_initialized;
147         
148   bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);  
149   bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
150   textdomain (GETTEXT_PACKAGE);
151
152   g_thread_init ( NULL );
153   gdk_threads_init ();
154
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   }
174    
175   if (vik_version)
176   {
177     g_printf ("%s %s\nCopyright (c) 2003-2008 Evan Battaglia\nCopyright (c) 2008-2010 Viking's contributors\n", PACKAGE_NAME, PACKAGE_VERSION);
178     return EXIT_SUCCESS;
179   }
180
181   if (!vik_debug)
182     g_log_set_handler (NULL, G_LOG_LEVEL_DEBUG, mute_log, NULL);
183
184   a_preferences_init ();
185
186   a_vik_preferences_init ();
187
188   a_download_init();
189   curl_download_init();
190
191   a_babel_init ();
192
193   /* Init modules/plugins */
194   modules_init();
195
196   maps_layer_init ();
197   a_mapcache_init ();
198   a_background_init ();
199
200 #ifdef VIK_CONFIG_GEOCACHES
201   a_datasource_gc_init();
202 #endif
203
204   /* Set the icon */
205   main_icon = gdk_pixbuf_from_pixdata(&viking_pixbuf, FALSE, NULL);
206   gtk_window_set_default_icon(main_icon);
207
208   /* Create the first window */
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
222   a_babel_uninit ();
223
224   a_background_uninit ();
225   a_mapcache_uninit ();
226   a_dems_uninit ();
227   a_preferences_uninit ();
228
229   curl_download_uninit();
230
231   return 0;
232 }