]> git.sur5r.net Git - glabels/blob - src/glabels-batch.c
Refactored glLabel and glView
[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/libglabels.h>
28 #include "merge-init.h"
29 #include "xml-label.h"
30 #include "print.h"
31 #include "print-op.h"
32 #include "file-util.h"
33 #include "prefs.h"
34 #include "debug.h"
35
36 /*============================================*/
37 /* Private globals                            */
38 /*============================================*/
39 static gchar    *output          = "output.pdf";
40 static gint     n_copies         = 1;
41 static gint     n_sheets         = 1;
42 static gint     first            = 1;
43 static gboolean outline_flag     = FALSE;
44 static gboolean reverse_flag     = FALSE;
45 static gboolean crop_marks_flag  = FALSE;
46 static gchar    *input           = NULL;
47 static gchar    **remaining_args = NULL;
48
49 static GOptionEntry option_entries[] = {
50         {"output", 'o', 0, G_OPTION_ARG_STRING, &output,
51          N_("set output filename (default=\"output.pdf\")"), N_("filename")},
52         {"sheets", 's', 0, G_OPTION_ARG_INT, &n_sheets,
53          N_("number of sheets (default=1)"), N_("sheets")},
54         {"copies", 'c', 0, G_OPTION_ARG_INT, &n_copies,
55          N_("number of copies (default=1)"), N_("copies")},
56         {"first", 'f', 0, G_OPTION_ARG_INT, &first,
57          N_("first label on first sheet (default=1)"), N_("first")},
58         {"outline", 'l', 0, G_OPTION_ARG_NONE, &outline_flag,
59          N_("print outlines (to test printer alignment)"), NULL},
60         {"reverse", 'r', 0, G_OPTION_ARG_NONE, &reverse_flag,
61          N_("print in reverse (i.e. a mirror image)"), NULL},
62         {"cropmarks", 'C', 0, G_OPTION_ARG_NONE, &crop_marks_flag,
63          N_("print crop marks"), NULL},
64         {"input", 'i', 0, G_OPTION_ARG_STRING, &input,
65          N_("input file for merging"), N_("filename")},
66         { G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY,
67           &remaining_args, NULL, N_("[FILE...]") },
68         { NULL }
69 };
70
71
72 \f
73 /*****************************************************************************/
74 /* Main                                                                      */
75 /*****************************************************************************/
76 int
77 main (int argc, char **argv)
78 {
79         GOptionContext    *option_context;
80         GList             *p, *file_list = NULL;
81         gchar             *abs_fn;
82         glLabel           *label = NULL;
83         glMerge           *merge = NULL;
84         const lglTemplate *template;
85         lglTemplateFrame  *frame;
86         glXMLLabelStatus   status;
87         glPrintOp         *print_op;
88         gchar             *utf8_filename;
89         GError            *error = NULL;
90
91         bindtextdomain (GETTEXT_PACKAGE, GLABELS_LOCALEDIR);
92         bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
93         textdomain (GETTEXT_PACKAGE);
94
95         option_context = g_option_context_new (NULL);
96         g_option_context_set_summary (option_context,
97                                       _("Print files created with gLabels."));
98         g_option_context_add_main_entries (option_context, option_entries, GETTEXT_PACKAGE);
99
100
101         /* Initialize minimal gnome program */
102         gtk_init (&argc, &argv);
103         if (!g_option_context_parse (option_context, &argc, &argv, &error))
104         {
105                 g_print(_("%s\nRun '%s --help' to see a full list of available command line options.\n"),
106                         error->message, argv[0]);
107                 g_error_free (error);
108                 return 1;
109         }
110
111
112         /* create file list */
113         if (remaining_args != NULL) {
114                 gint i, num_args;
115
116                 num_args = g_strv_length (remaining_args);
117                 for (i = 0; i < num_args; ++i) {
118                         utf8_filename = g_filename_to_utf8 (remaining_args[i], -1, NULL, NULL, NULL);
119                         if (utf8_filename)
120                                 file_list = g_list_append (file_list, utf8_filename);
121                 }
122                 g_strfreev (remaining_args);
123                 remaining_args = NULL;
124         }
125
126         /* initialize components */
127         gl_debug_init ();
128         gl_merge_init ();
129         lgl_db_init ();
130         gl_prefs_init ();
131
132         /* now print the files */
133         for (p = file_list; p; p = p->next) {
134                 g_print ("LABEL FILE = %s\n", (gchar *) p->data);
135                 label = gl_xml_label_open (p->data, &status);
136
137
138                 if ( status == XML_LABEL_OK ) {
139
140                         merge = gl_label_get_merge (label);
141                         if (input != NULL) {
142                                 if (merge != NULL) {
143                                         gl_merge_set_src(merge, input);
144                                         gl_label_set_merge(label, merge);
145                                 } else {
146                                         fprintf ( stderr,
147                                                   _("cannot perform document merge with glabels file %s\n"),
148                                                   (char *)p->data );
149                                 }
150                         }
151                         abs_fn = gl_file_util_make_absolute ( output );
152                         template = gl_label_get_template (label);
153                         frame = (lglTemplateFrame *)template->frames->data;
154
155                         print_op = gl_print_op_new (label);
156                         gl_print_op_set_filename        (print_op, abs_fn);
157                         gl_print_op_set_n_copies        (print_op, n_copies);
158                         gl_print_op_set_first           (print_op, first);
159                         gl_print_op_set_outline_flag    (print_op, outline_flag);
160                         gl_print_op_set_reverse_flag    (print_op, reverse_flag);
161                         gl_print_op_set_crop_marks_flag (print_op, crop_marks_flag);
162                         if (merge)
163                         {
164                                 gl_print_op_set_n_sheets (print_op,
165                                                           ceil ((double)(first-1 + n_copies * gl_merge_get_record_count(merge))
166                                                                 / lgl_template_frame_get_n_labels (frame)));
167                         }
168                         else
169                         {
170                                 gl_print_op_set_n_sheets (print_op, n_sheets);
171                                 gl_print_op_set_last     (print_op,
172                                                           lgl_template_frame_get_n_labels (frame));
173                         }
174                         gtk_print_operation_run (GTK_PRINT_OPERATION (print_op),
175                                                  GTK_PRINT_OPERATION_ACTION_EXPORT,
176                                                  NULL,
177                                                  NULL);
178
179                         g_object_unref (label);
180                 }
181                 else {
182                         fprintf ( stderr, _("cannot open glabels file %s\n"),
183                                   (char *)p->data );
184                 }
185         }
186
187         g_list_free (file_list);
188
189         return 0;
190 }
191
192
193
194
195 /*
196  * Local Variables:       -- emacs
197  * mode: C                -- emacs
198  * c-basic-offset: 8      -- emacs
199  * tab-width: 8           -- emacs
200  * indent-tabs-mode: nil  -- emacs
201  * End:                   -- emacs
202  */