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