]> git.sur5r.net Git - glabels/blob - src/glabels.c
Imported Upstream version 2.2.8
[glabels] / src / glabels.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
2
3 /**
4  *  (GLABELS) Label and Business Card Creation program for GNOME
5  *
6  *  glabels.c:  GLabels main module
7  *
8  *  Copyright (C) 2001-2002  Jim Evins <evins@snaught.com>.
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  (at your option) any later version.
14  *
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program; if not, write to the Free Software
22  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
23  */
24
25 #include <config.h>
26
27 #include <glib/gi18n.h>
28 #include <libgnome/libgnome.h>
29 #include <libgnomeui/libgnomeui.h>
30 #include <libgnomeui/gnome-window-icon.h>
31
32 #include "warning-handler.h"
33 #include "critical-error-handler.h"
34 #include "stock.h"
35 #include "merge-init.h"
36 #include "recent.h"
37 #include <libglabels/db.h>
38 #include "mini-preview-pixbuf-cache.h"
39 #include "prefs.h"
40 #include "debug.h"
41 #include "window.h"
42 #include "file.h"
43
44 /*========================================================*/
45 /* Private macros and constants.                          */
46 /*========================================================*/
47
48 /*========================================================*/
49 /* Private globals                                        */
50 /*========================================================*/
51
52 /*========================================================*/
53 /* Local function prototypes                              */
54 /*========================================================*/
55 gboolean save_session_cb (GnomeClient        *client,
56                           gint                phase,
57                           GnomeRestartStyle   save_style,
58                           gint                shutdown,
59                           GnomeInteractStyle  interact_style,
60                           gint                fast,
61                           gpointer            client_data);
62
63 void client_die_cb       (GnomeClient        *client,
64                           gpointer            client_data);
65
66 /****************************************************************************/
67 /* main program                                                             */
68 /****************************************************************************/
69 int
70 main (int argc, char **argv)
71 {
72         gchar **remaining_args = NULL;
73         GOptionEntry option_entries[] = {
74                 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY,
75                   &remaining_args, NULL, N_("[FILE...]") },
76                 { NULL }
77         };
78
79         GOptionContext *option_context;
80         GnomeProgram   *program;
81         gchar          *icon_file;
82         GnomeClient    *client;
83         GList          *file_list = NULL, *p;
84         GtkWidget      *win;
85         gchar          *utf8_filename;
86
87         bindtextdomain (GETTEXT_PACKAGE, GLABELS_LOCALEDIR);
88         bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
89         textdomain (GETTEXT_PACKAGE);
90
91         option_context = g_option_context_new (_("- gLabels label designer"));
92         g_option_context_add_main_entries (option_context, option_entries, GETTEXT_PACKAGE);
93
94
95         /* Initialize gnome program */
96         program = gnome_program_init ("glabels", VERSION,
97                                       LIBGNOMEUI_MODULE, argc, argv,
98                                       GNOME_PARAM_GOPTION_CONTEXT, option_context,
99                                       GNOME_PROGRAM_STANDARD_PROPERTIES,
100                                       NULL);
101
102         /* Install GUI handlers for critical error and warning messages */
103         gl_critical_error_handler_init();
104         gl_warning_handler_init();
105
106         /* Set default icon */
107         icon_file = GLABELS_ICON_DIR "glabels.png";
108         if (!g_file_test (icon_file, G_FILE_TEST_EXISTS))
109         {
110                 g_message ("Could not find %s", icon_file);
111         }
112         else
113         {
114                 gnome_window_icon_set_default_from_file (icon_file);
115         }
116
117         
118         /* Initialize subsystems */
119         gl_debug_init ();
120         gl_stock_init ();
121         lgl_db_init ();
122         gl_prefs_init ();
123         gl_mini_preview_pixbuf_cache_init ();
124         gl_merge_init ();
125         gl_recent_init ();
126         
127
128         client = gnome_master_client();
129
130         g_signal_connect (G_OBJECT (client), "save_yourself",
131                           G_CALLBACK (save_session_cb),
132                           (gpointer)argv[0]);
133
134         g_signal_connect (G_OBJECT (client), "die",
135                           G_CALLBACK (client_die_cb), NULL);
136
137
138         /* Parse args and build the list of files to be loaded at startup */
139         if (remaining_args != NULL) {
140                 gint i, num_args;
141
142                 num_args = g_strv_length (remaining_args);
143                 for (i = 0; i < num_args; ++i) {
144                         utf8_filename = g_filename_to_utf8 (remaining_args[i], -1, NULL, NULL, NULL);
145                         if (utf8_filename)
146                                 file_list = g_list_append (file_list, utf8_filename);
147                 }
148                 g_strfreev (remaining_args);
149                 remaining_args = NULL;
150         }
151
152
153         /* Open files or create empty top-level window. */
154         for (p = file_list; p; p = p->next) {
155                 win = gl_window_new_from_file (p->data);
156                 gtk_widget_show_all (win);
157                 g_free (p->data);
158         }
159         if ( gl_window_get_window_list() == NULL ) {
160                 win = gl_window_new ();
161                 gtk_widget_show_all (win);
162         }
163         g_list_free (file_list);
164
165         
166         /* Begin main loop */
167         gtk_main();
168
169         g_object_unref (G_OBJECT (program));
170
171         return 0;
172 }
173
174 /*---------------------------------------------------------------------------*/
175 /* PRIVATE.  "Save session" callback.                                        */
176 /*---------------------------------------------------------------------------*/
177 gboolean save_session_cb (GnomeClient        *client,
178                           gint                phase,
179                           GnomeRestartStyle   save_style,
180                           gint                shutdown,
181                           GnomeInteractStyle  interact_style,
182                           gint                fast,
183                           gpointer            client_data)
184 {
185         gchar       *argv[128];
186         gint         argc;
187         const GList *window_list;
188         GList       *p;
189         glWindow    *window;
190         glLabel     *label;
191
192         argv[0] = (gchar *)client_data;
193         argc = 1;
194
195         window_list = gl_window_get_window_list();
196         for ( p=(GList *)window_list; p != NULL; p=p->next ) {
197                 window = GL_WINDOW(p->data);
198                 if ( !gl_window_is_empty (window) ) {
199                         label = GL_VIEW(window->view)->label;
200                         argv[argc++] = gl_label_get_filename (label);
201                 }
202         }
203         gnome_client_set_clone_command(client, argc, argv);
204         gnome_client_set_restart_command(client, argc, argv);
205         
206         return TRUE;
207 }
208
209 /*---------------------------------------------------------------------------*/
210 /* PRIVATE.  "Die" callback.                                                 */
211 /*---------------------------------------------------------------------------*/
212 void client_die_cb (GnomeClient *client,
213                     gpointer     client_data)
214 {
215         gl_file_exit ();
216 }
217
218