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