]> git.sur5r.net Git - glabels/blob - src/glabels-batch.c
Fix incomplete library dependencies.
[glabels] / src / glabels-batch.c
1 /*
2  *  glabels-batch.c
3  *  Copyright (C) 2001-2009  Jim Evins <evins@snaught.com>.
4  *
5  *  This file is part of gLabels.
6  *
7  *  gLabels is free software: you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation, either version 3 of the License, or
10  *  (at your option) any later version.
11  *
12  *  gLabels is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with gLabels.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include <config.h>
22
23 #include <glib/gi18n.h>
24
25 #include <math.h>
26
27 #include <libglabels.h>
28 #include "merge-init.h"
29 #include "template-history.h"
30 #include "font-history.h"
31 #include "xml-label.h"
32 #include "print.h"
33 #include "print-op.h"
34 #include "file-util.h"
35 #include "prefs.h"
36 #include "debug.h"
37
38 /*============================================*/
39 /* Private globals                            */
40 /*============================================*/
41 static gchar    *output          = "output.pdf";
42 static gint     n_copies         = 1;
43 static gint     n_sheets         = 1;
44 static gint     first            = 1;
45 static gboolean outline_flag     = FALSE;
46 static gboolean reverse_flag     = FALSE;
47 static gboolean crop_marks_flag  = FALSE;
48 static gchar    *input           = NULL;
49 static gchar    **remaining_args = NULL;
50
51 static GOptionEntry option_entries[] = {
52         {"output", 'o', 0, G_OPTION_ARG_STRING, &output,
53          N_("set output filename (default=\"output.pdf\")"), N_("filename")},
54         {"sheets", 's', 0, G_OPTION_ARG_INT, &n_sheets,
55          N_("number of sheets (default=1)"), N_("sheets")},
56         {"copies", 'c', 0, G_OPTION_ARG_INT, &n_copies,
57          N_("number of copies (default=1)"), N_("copies")},
58         {"first", 'f', 0, G_OPTION_ARG_INT, &first,
59          N_("first label on first sheet (default=1)"), N_("first")},
60         {"outline", 'l', 0, G_OPTION_ARG_NONE, &outline_flag,
61          N_("print outlines (to test printer alignment)"), NULL},
62         {"reverse", 'r', 0, G_OPTION_ARG_NONE, &reverse_flag,
63          N_("print in reverse (i.e. a mirror image)"), NULL},
64         {"cropmarks", 'C', 0, G_OPTION_ARG_NONE, &crop_marks_flag,
65          N_("print crop marks"), NULL},
66         {"input", 'i', 0, G_OPTION_ARG_STRING, &input,
67          N_("input file for merging"), N_("filename")},
68         { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY,
69           &remaining_args, NULL, N_("[FILE...]") },
70         { NULL }
71 };
72
73
74 \f
75 /*****************************************************************************/
76 /* Main                                                                      */
77 /*****************************************************************************/
78 int
79 main (int argc, char **argv)
80 {
81         GOptionContext    *option_context;
82         GList             *p, *file_list = NULL;
83         gchar             *abs_fn;
84         glLabel           *label = NULL;
85         glMerge           *merge = NULL;
86         const lglTemplate *template;
87         lglTemplateFrame  *frame;
88         glXMLLabelStatus   status;
89         glPrintOp         *print_op;
90         gchar             *utf8_filename;
91         GError            *error = NULL;
92
93         bindtextdomain (GETTEXT_PACKAGE, GLABELS_LOCALE_DIR);
94         bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
95         textdomain (GETTEXT_PACKAGE);
96
97         option_context = g_option_context_new (NULL);
98         g_option_context_set_summary (option_context,
99                                       _("Print files created with gLabels."));
100         g_option_context_add_main_entries (option_context, option_entries, GETTEXT_PACKAGE);
101
102
103         /* Initialize minimal gnome program */
104         gtk_init_check (&argc, &argv);
105         if (!g_option_context_parse (option_context, &argc, &argv, &error))
106         {
107                 g_print(_("%s\nRun '%s --help' to see a full list of available command line options.\n"),
108                         error->message, argv[0]);
109                 g_error_free (error);
110                 return 1;
111         }
112
113
114         /* create file list */
115         if (remaining_args != NULL) {
116                 gint i, num_args;
117
118                 num_args = g_strv_length (remaining_args);
119                 for (i = 0; i < num_args; ++i) {
120                         utf8_filename = g_filename_to_utf8 (remaining_args[i], -1, NULL, NULL, NULL);
121                         if (utf8_filename)
122                                 file_list = g_list_append (file_list, utf8_filename);
123                 }
124                 g_strfreev (remaining_args);
125                 remaining_args = NULL;
126         }
127
128         /* initialize components */
129         gl_debug_init ();
130         gl_merge_init ();
131         lgl_db_init ();
132         gl_prefs_init ();
133         gl_template_history_init ();
134         gl_font_history_init ();
135
136         /* now print the files */
137         for (p = file_list; p; p = p->next) {
138                 g_print ("LABEL FILE = %s\n", (gchar *) p->data);
139                 label = gl_xml_label_open (p->data, &status);
140
141
142                 if ( status == XML_LABEL_OK ) {
143
144                         merge = gl_label_get_merge (label);
145                         if (input != NULL) {
146                                 if (merge != NULL) {
147                                         gl_merge_set_src(merge, input);
148                                         gl_label_set_merge(label, merge, FALSE);
149                                 } else {
150                                         fprintf ( stderr,
151                                                   _("cannot perform document merge with glabels file %s\n"),
152                                                   (char *)p->data );
153                                 }
154                         }
155                         abs_fn = gl_file_util_make_absolute ( output );
156                         template = gl_label_get_template (label);
157                         frame = (lglTemplateFrame *)template->frames->data;
158
159                         print_op = gl_print_op_new (label);
160                         gl_print_op_set_filename        (print_op, abs_fn);
161                         gl_print_op_set_n_copies        (print_op, n_copies);
162                         gl_print_op_set_first           (print_op, first);
163                         gl_print_op_set_outline_flag    (print_op, outline_flag);
164                         gl_print_op_set_reverse_flag    (print_op, reverse_flag);
165                         gl_print_op_set_crop_marks_flag (print_op, crop_marks_flag);
166                         if (merge)
167                         {
168                                 gl_print_op_set_n_sheets (print_op,
169                                                           ceil ((double)(first-1 + n_copies * gl_merge_get_record_count(merge))
170                                                                 / lgl_template_frame_get_n_labels (frame)));
171                         }
172                         else
173                         {
174                                 gl_print_op_set_n_sheets (print_op, n_sheets);
175                                 gl_print_op_set_last     (print_op,
176                                                           lgl_template_frame_get_n_labels (frame));
177                         }
178                         gtk_print_operation_run (GTK_PRINT_OPERATION (print_op),
179                                                  GTK_PRINT_OPERATION_ACTION_EXPORT,
180                                                  NULL,
181                                                  NULL);
182
183                         g_object_unref (label);
184                 }
185                 else {
186                         fprintf ( stderr, _("cannot open glabels file %s\n"),
187                                   (char *)p->data );
188                 }
189         }
190
191         g_list_free (file_list);
192
193         return 0;
194 }
195
196
197
198
199 /*
200  * Local Variables:       -- emacs
201  * mode: C                -- emacs
202  * c-basic-offset: 8      -- emacs
203  * tab-width: 8           -- emacs
204  * indent-tabs-mode: nil  -- emacs
205  * End:                   -- emacs
206  */