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