]> git.street.me.uk Git - andy/viking.git/blob - src/main.c
Merge branch 'WindowsInstaller'
[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, const gchar **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, const gchar **files )
110 {
111   gboolean change_fn = (!files[1]); /* only change fn if one file */
112   while ( *files ) {
113     // Only open a new window if a viking file
114     if (vw != NULL && check_file_magic_vik ( *(files) ) ) {
115       VikWindow *newvw = new_window();
116       if (newvw)
117         vik_window_open_file ( newvw, *(files++), change_fn );
118     }
119     else {
120       vik_window_open_file ( vw, *(files++), change_fn );
121     }
122   }
123 }
124
125 /* Options */
126 static GOptionEntry entries[] = 
127 {
128   { "debug", 'd', 0, G_OPTION_ARG_NONE, &vik_debug, N_("Enable debug output"), NULL },
129   { "verbose", 'V', 0, G_OPTION_ARG_NONE, &vik_verbose, N_("Enable verbose output"), NULL },
130   { "version", 'v', 0, G_OPTION_ARG_NONE, &vik_version, N_("Show version"), NULL },
131   { NULL }
132 };
133
134 int main( int argc, char *argv[] )
135 {
136   VikWindow *first_window;
137   GdkPixbuf *main_icon;
138   gboolean dashdash_already = FALSE;
139   int i = 0;
140   GError *error = NULL;
141   gboolean gui_initialized;
142         
143   bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);  
144   bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
145   textdomain (GETTEXT_PACKAGE);
146
147   g_thread_init ( NULL );
148   gdk_threads_init ();
149
150   gui_initialized = gtk_init_with_args (&argc, &argv, "files+", entries, NULL, &error);
151   if (!gui_initialized)
152   {
153     /* check if we have an error message */
154     if (error == NULL)
155     {
156       /* no error message, the GUI initialization failed */
157       const gchar *display_name = gdk_get_display_arg_name ();
158       g_fprintf (stderr, "Failed to open display: %s\n", (display_name != NULL) ? display_name : " ");
159     }
160     else
161     {
162       /* yep, there's an error, so print it */
163       g_fprintf (stderr, "Parsing command line options failed: %s\n", error->message);
164       g_error_free (error);
165       g_fprintf (stderr, "Run \"%s --help\" to see the list of recognized options.\n",argv[0]);
166     }
167     return EXIT_FAILURE;
168   }
169    
170   if (vik_version)
171   {
172     g_printf ("%s %s\nCopyright (c) 2003-2008 Evan Battaglia\nCopyright (c) 2008-2010 Viking's contributors\n", PACKAGE_NAME, PACKAGE_VERSION);
173     return EXIT_SUCCESS;
174   }
175
176   if (!vik_debug)
177     g_log_set_handler (NULL, G_LOG_LEVEL_DEBUG, mute_log, NULL);
178
179   a_preferences_init ();
180
181   a_vik_preferences_init ();
182
183   a_download_init();
184   curl_download_init();
185
186   a_babel_init ();
187
188   /* Init modules/plugins */
189   modules_init();
190
191   maps_layer_init ();
192   a_mapcache_init ();
193   a_background_init ();
194
195 #ifdef VIK_CONFIG_GEOCACHES
196   a_datasource_gc_init();
197 #endif
198
199   /* Set the icon */
200   main_icon = gdk_pixbuf_from_pixdata(&viking_pixbuf, FALSE, NULL);
201   gtk_window_set_default_icon(main_icon);
202
203   /* Create the first window */
204   first_window = new_window();
205
206   gdk_threads_enter ();
207   while ( ++i < argc ) {
208     if ( strcmp(argv[i],"--") == 0 && !dashdash_already )
209       dashdash_already = TRUE; /* hack to open '-' */
210     else
211       vik_window_open_file ( first_window, argv[i], argc == 2 );
212   }
213
214   gtk_main ();
215   gdk_threads_leave ();
216
217   a_babel_uninit ();
218
219   a_background_uninit ();
220   a_mapcache_uninit ();
221   a_dems_uninit ();
222   a_preferences_uninit ();
223
224   curl_download_uninit();
225
226   return 0;
227 }