]> git.sur5r.net Git - glabels/blob - glabels2/src/glabels.c
2007-09-27 Jim Evins <evins@snaught.com>
[glabels] / glabels2 / 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/paper.h>
38 #include <libglabels/template.h>
39 #include "mini-preview-pixbuf-cache.h"
40 #include "prefs.h"
41 #include "debug.h"
42 #include "window.h"
43 #include "file.h"
44
45 /*========================================================*/
46 /* Private macros and constants.                          */
47 /*========================================================*/
48
49 /*========================================================*/
50 /* Private globals                                        */
51 /*========================================================*/
52
53 /*========================================================*/
54 /* Local function prototypes                              */
55 /*========================================================*/
56 gboolean save_session_cb (GnomeClient        *client,
57                           gint                phase,
58                           GnomeRestartStyle   save_style,
59                           gint                shutdown,
60                           GnomeInteractStyle  interact_style,
61                           gint                fast,
62                           gpointer            client_data);
63
64 void client_die_cb       (GnomeClient        *client,
65                           gpointer            client_data);
66
67 /****************************************************************************/
68 /* main program                                                             */
69 /****************************************************************************/
70 int
71 main (int argc, char **argv)
72 {
73         gchar **remaining_args = NULL;
74         GOptionEntry option_entries[] = {
75                 { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY,
76                   &remaining_args, NULL, N_("[FILE...]") },
77                 { NULL }
78         };
79
80         GOptionContext *option_context;
81         GnomeProgram   *program;
82         gchar          *icon_file;
83         GnomeClient    *client;
84         GList          *file_list = NULL, *p;
85         GtkWidget      *win;
86         gchar          *utf8_filename;
87
88         bindtextdomain (GETTEXT_PACKAGE, GLABELS_LOCALEDIR);
89         bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
90         textdomain (GETTEXT_PACKAGE);
91
92         option_context = g_option_context_new (_("- gLabels label designer"));
93         g_option_context_add_main_entries (option_context, option_entries, GETTEXT_PACKAGE);
94
95
96         /* Initialize gnome program */
97         program = gnome_program_init ("glabels", VERSION,
98                                       LIBGNOMEUI_MODULE, argc, argv,
99                                       GNOME_PARAM_GOPTION_CONTEXT, option_context,
100                                       GNOME_PROGRAM_STANDARD_PROPERTIES,
101                                       NULL);
102
103         /* Install GUI handlers for critical error and warning messages */
104         gl_critical_error_handler_init();
105         gl_warning_handler_init();
106
107         /* Set default icon */
108         icon_file = GLABELS_ICON_DIR "glabels.png";
109         if (!g_file_test (icon_file, G_FILE_TEST_EXISTS))
110         {
111                 g_message ("Could not find %s", icon_file);
112         }
113         else
114         {
115                 gnome_window_icon_set_default_from_file (icon_file);
116         }
117
118         
119         /* Initialize subsystems */
120         gl_debug_init ();
121         gl_stock_init ();
122         lgl_init ();
123         gl_prefs_init ();
124         gl_mini_preview_pixbuf_cache_init ();
125         gl_merge_init ();
126         gl_recent_init ();
127         
128
129         client = gnome_master_client();
130
131         g_signal_connect (G_OBJECT (client), "save_yourself",
132                           G_CALLBACK (save_session_cb),
133                           (gpointer)argv[0]);
134
135         g_signal_connect (G_OBJECT (client), "die",
136                           G_CALLBACK (client_die_cb), NULL);
137
138
139         /* Parse args and build the list of files to be loaded at startup */
140         if (remaining_args != NULL) {
141                 gint i, num_args;
142
143                 num_args = g_strv_length (remaining_args);
144                 for (i = 0; i < num_args; ++i) {
145                         utf8_filename = g_filename_to_utf8 (remaining_args[i], -1, NULL, NULL, NULL);
146                         if (utf8_filename)
147                                 file_list = g_list_append (file_list, utf8_filename);
148                 }
149                 g_strfreev (remaining_args);
150                 remaining_args = NULL;
151         }
152
153
154         /* Open files or create empty top-level window. */
155         for (p = file_list; p; p = p->next) {
156                 win = gl_window_new_from_file (p->data);
157                 gtk_widget_show_all (win);
158                 g_free (p->data);
159         }
160         if ( gl_window_get_window_list() == NULL ) {
161                 win = gl_window_new ();
162                 gtk_widget_show_all (win);
163         }
164         g_list_free (file_list);
165
166         
167         /* Begin main loop */
168         gtk_main();
169
170         g_object_unref (G_OBJECT (program));
171
172         return 0;
173 }
174
175 /*---------------------------------------------------------------------------*/
176 /* PRIVATE.  "Save session" callback.                                        */
177 /*---------------------------------------------------------------------------*/
178 gboolean save_session_cb (GnomeClient        *client,
179                           gint                phase,
180                           GnomeRestartStyle   save_style,
181                           gint                shutdown,
182                           GnomeInteractStyle  interact_style,
183                           gint                fast,
184                           gpointer            client_data)
185 {
186         gchar       *argv[128];
187         gint         argc;
188         const GList *window_list;
189         GList       *p;
190         glWindow    *window;
191         glLabel     *label;
192
193         argv[0] = (gchar *)client_data;
194         argc = 1;
195
196         window_list = gl_window_get_window_list();
197         for ( p=(GList *)window_list; p != NULL; p=p->next ) {
198                 window = GL_WINDOW(p->data);
199                 if ( !gl_window_is_empty (window) ) {
200                         label = GL_VIEW(window->view)->label;
201                         argv[argc++] = gl_label_get_filename (label);
202                 }
203         }
204         gnome_client_set_clone_command(client, argc, argv);
205         gnome_client_set_restart_command(client, argc, argv);
206         
207         return TRUE;
208 }
209
210 /*---------------------------------------------------------------------------*/
211 /* PRIVATE.  "Die" callback.                                                 */
212 /*---------------------------------------------------------------------------*/
213 void client_die_cb (GnomeClient *client,
214                     gpointer     client_data)
215 {
216         gl_file_exit ();
217 }
218
219