odt: Restore writing creator metadata.
[pspp-builds.git] / src / output / odt.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2009, 2010 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 /* A driver for creating OpenDocument Format text files from PSPP's output */
20
21 #include <errno.h>
22 #include <libgen.h>
23 #include <libxml/xmlwriter.h>
24 #ifdef HAVE_PWD_H
25 #include <pwd.h>
26 #endif
27 #include <sys/stat.h>
28 #include <sys/types.h>
29 #include <time.h>
30 #include <unistd.h>
31
32 #include "libpspp/assertion.h"
33 #include "libpspp/cast.h"
34 #include "libpspp/str.h"
35 #include "libpspp/version.h"
36 #include "output/driver-provider.h"
37 #include "output/message-item.h"
38 #include "output/options.h"
39 #include "output/tab.h"
40 #include "output/table-item.h"
41 #include "output/table-provider.h"
42 #include "output/text-item.h"
43
44 #include "gl/xalloc.h"
45 #include "gl/error.h"
46
47 #include "gettext.h"
48 #define _(msgid) gettext (msgid)
49
50 #define _xml(X) (const xmlChar *)(X)
51
52 struct odt_driver
53 {
54   struct output_driver driver;
55
56   char *file_name;            /* Output file name. */
57   bool debug;
58
59   /* The name of the temporary directory used to construct the ODF */
60   char *dirname;
61
62   /* Writer for the content.xml file */
63   xmlTextWriterPtr content_wtr;
64
65   /* Writer fot the manifest.xml file */
66   xmlTextWriterPtr manifest_wtr;
67
68   /* Number of tables so far. */
69   int table_num;
70
71   /* Name of current command. */
72   char *command_name;
73 };
74
75 static const struct output_driver_class odt_driver_class;
76
77 static struct odt_driver *
78 odt_driver_cast (struct output_driver *driver)
79 {
80   assert (driver->class == &odt_driver_class);
81   return UP_CAST (driver, struct odt_driver, driver);
82 }
83
84 /* Create the "mimetype" file needed by ODF */
85 static bool
86 create_mimetype (const char *dirname)
87 {
88   FILE *fp;
89   struct string filename;
90   ds_init_cstr (&filename, dirname);
91   ds_put_cstr (&filename, "/mimetype");
92   fp = fopen (ds_cstr (&filename), "w");
93
94   if (fp == NULL)
95     {
96       error (0, errno, _("error opening output file \"%s\""),
97              ds_cstr (&filename));
98       ds_destroy (&filename);
99       return false;
100     }
101   ds_destroy (&filename);
102
103   fprintf (fp, "application/vnd.oasis.opendocument.text");
104   fclose (fp);
105
106   return true;
107 }
108
109 /* Create a new XML file called FILENAME in the temp directory, and return a writer for it */
110 static xmlTextWriterPtr
111 create_writer (const struct odt_driver *driver, const char *filename)
112 {
113   char *copy = NULL;
114   xmlTextWriterPtr w;
115   struct string str;
116   ds_init_cstr (&str, driver->dirname);
117   ds_put_cstr (&str, "/");
118   ds_put_cstr (&str, filename);
119
120   /* dirname modifies its argument, so we must copy it */
121   copy = xstrdup (ds_cstr (&str));
122   mkdir (dirname (copy), 0700);
123   free (copy);
124
125   w = xmlNewTextWriterFilename (ds_cstr (&str), 0);
126
127   ds_destroy (&str);
128
129   xmlTextWriterStartDocument (w, NULL, "UTF-8", NULL);
130
131   return w;
132 }
133
134
135 static void
136 register_file (struct odt_driver *odt, const char *filename)
137 {
138   assert (odt->manifest_wtr);
139   xmlTextWriterStartElement (odt->manifest_wtr, _xml("manifest:file-entry"));
140   xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("manifest:media-type"),  _xml("text/xml"));
141   xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("manifest:full-path"),  _xml (filename));
142   xmlTextWriterEndElement (odt->manifest_wtr);
143 }
144
145 static void
146 write_style_data (struct odt_driver *odt)
147 {
148   xmlTextWriterPtr w = create_writer (odt, "styles.xml");
149   register_file (odt, "styles.xml");
150
151   xmlTextWriterStartElement (w, _xml ("office:document-styles"));
152   xmlTextWriterWriteAttribute (w, _xml ("xmlns:office"),
153                                _xml ("urn:oasis:names:tc:opendocument:xmlns:office:1.0"));
154
155   xmlTextWriterWriteAttribute (w, _xml ("xmlns:style"),
156                                _xml ("urn:oasis:names:tc:opendocument:xmlns:style:1.0"));
157
158   xmlTextWriterWriteAttribute (w, _xml ("xmlns:fo"),
159                                _xml ("urn:oasis:names:tc:opendocument:xmlns:xsl-fo-compatible:1.0") );
160
161   xmlTextWriterWriteAttribute (w, _xml ("office:version"),  _xml ("1.1"));
162                                
163
164
165   xmlTextWriterStartElement (w, _xml ("office:styles"));
166
167
168   {
169     xmlTextWriterStartElement (w, _xml ("style:style"));
170     xmlTextWriterWriteAttribute (w, _xml ("style:name"),
171                                  _xml ("Standard"));
172
173     xmlTextWriterWriteAttribute (w, _xml ("style:family"),
174                                  _xml ("paragraph"));
175
176     xmlTextWriterWriteAttribute (w, _xml ("style:class"),
177                                  _xml ("text"));
178
179     xmlTextWriterEndElement (w); /* style:style */
180   }
181
182   {
183     xmlTextWriterStartElement (w, _xml ("style:style"));
184     xmlTextWriterWriteAttribute (w, _xml ("style:name"),
185                                  _xml ("Table_20_Contents"));
186
187     xmlTextWriterWriteAttribute (w, _xml ("style:display-name"),
188                                  _xml ("Table Contents"));
189
190     xmlTextWriterWriteAttribute (w, _xml ("style:family"),
191                                  _xml ("paragraph"));
192
193     xmlTextWriterWriteAttribute (w, _xml ("style:parent-style-name"),
194                                  _xml ("Standard"));
195
196     xmlTextWriterWriteAttribute (w, _xml ("style:class"),
197                                  _xml ("extra"));
198
199     xmlTextWriterEndElement (w); /* style:style */
200   }
201
202   {
203     xmlTextWriterStartElement (w, _xml ("style:style"));
204     xmlTextWriterWriteAttribute (w, _xml ("style:name"),
205                                  _xml ("Table_20_Heading"));
206
207     xmlTextWriterWriteAttribute (w, _xml ("style:display-name"),
208                                  _xml ("Table Heading"));
209
210     xmlTextWriterWriteAttribute (w, _xml ("style:family"),
211                                  _xml ("paragraph"));
212
213     xmlTextWriterWriteAttribute (w, _xml ("style:parent-style-name"),
214                                  _xml ("Table_20_Contents"));
215
216     xmlTextWriterWriteAttribute (w, _xml ("style:class"),
217                                  _xml ("extra"));
218
219
220     xmlTextWriterStartElement (w, _xml ("style:text-properties"));
221     xmlTextWriterWriteAttribute (w, _xml ("fo:font-weight"), _xml ("bold"));
222     xmlTextWriterWriteAttribute (w, _xml ("style:font-weight-asian"), _xml ("bold"));
223     xmlTextWriterWriteAttribute (w, _xml ("style:font-weight-complex"), _xml ("bold"));
224     xmlTextWriterEndElement (w); /* style:text-properties */
225
226     xmlTextWriterEndElement (w); /* style:style */
227   }
228
229
230   xmlTextWriterEndElement (w); /* office:styles */
231   xmlTextWriterEndElement (w); /* office:document-styles */
232
233   xmlTextWriterEndDocument (w);
234   xmlFreeTextWriter (w);
235 }
236
237 static void
238 write_meta_data (struct odt_driver *odt)
239 {
240   xmlTextWriterPtr w = create_writer (odt, "meta.xml");
241   register_file (odt, "meta.xml");
242
243   xmlTextWriterStartElement (w, _xml ("office:document-meta"));
244   xmlTextWriterWriteAttribute (w, _xml ("xmlns:office"), _xml ("urn:oasis:names:tc:opendocument:xmlns:office:1.0"));
245   xmlTextWriterWriteAttribute (w, _xml ("xmlns:dc"),  _xml ("http://purl.org/dc/elements/1.1/"));
246   xmlTextWriterWriteAttribute (w, _xml ("xmlns:meta"), _xml ("urn:oasis:names:tc:opendocument:xmlns:meta:1.0"));
247   xmlTextWriterWriteAttribute (w, _xml ("xmlns:ooo"), _xml("http://openoffice.org/2004/office"));
248   xmlTextWriterWriteAttribute (w, _xml ("office:version"),  _xml("1.1"));
249
250   xmlTextWriterStartElement (w, _xml ("office:meta"));
251   {
252     xmlTextWriterStartElement (w, _xml ("meta:generator"));
253     xmlTextWriterWriteString (w, _xml (stat_version));
254     xmlTextWriterEndElement (w);
255   }
256
257
258   {
259     char buf[30];
260     time_t t = time (NULL);
261     struct tm *tm =  localtime (&t);
262
263     strftime (buf, 30, "%Y-%m-%dT%H:%M:%S", tm);
264
265     xmlTextWriterStartElement (w, _xml ("meta:creation-date"));
266     xmlTextWriterWriteString (w, _xml (buf));
267     xmlTextWriterEndElement (w);
268
269     xmlTextWriterStartElement (w, _xml ("dc:date"));
270     xmlTextWriterWriteString (w, _xml (buf));
271     xmlTextWriterEndElement (w);
272   }
273
274 #ifdef HAVE_PWD_H
275   {
276     struct passwd *pw = getpwuid (getuid ());
277     if (pw != NULL)
278       {
279         xmlTextWriterStartElement (w, _xml ("meta:initial-creator"));
280         xmlTextWriterWriteString (w, _xml (strtok (pw->pw_gecos, ",")));
281         xmlTextWriterEndElement (w);
282
283         xmlTextWriterStartElement (w, _xml ("dc:creator"));
284         xmlTextWriterWriteString (w, _xml (strtok (pw->pw_gecos, ",")));
285         xmlTextWriterEndElement (w);
286       }
287   }
288 #endif
289
290   xmlTextWriterEndElement (w);
291   xmlTextWriterEndElement (w);
292   xmlTextWriterEndDocument (w);
293   xmlFreeTextWriter (w);
294 }
295
296 enum
297 {
298   output_file_arg,
299   boolean_arg,
300 };
301
302 static struct driver_option *
303 opt (struct output_driver *d, struct string_map *options, const char *key,
304      const char *default_value)
305 {
306   return driver_option_get (d, options, key, default_value);
307 }
308
309 static struct output_driver *
310 odt_create (const char *file_name, enum settings_output_devices device_type,
311             struct string_map *o)
312 {
313   struct output_driver *d;
314   struct odt_driver *odt;
315
316   odt = xzalloc (sizeof *odt);
317   d = &odt->driver;
318   output_driver_init (d, &odt_driver_class, file_name, device_type);
319
320   odt->file_name = xstrdup (file_name);
321   odt->debug = parse_boolean (opt (d, o, "debug", "false"));
322
323   odt->dirname = xstrdup ("odt-XXXXXX");
324   mkdtemp (odt->dirname);
325
326   if (!create_mimetype (odt->dirname))
327     {
328       output_driver_destroy (d);
329       return NULL;
330     }
331
332   /* Create the manifest */
333   odt->manifest_wtr = create_writer (odt, "META-INF/manifest.xml");
334
335   xmlTextWriterStartElement (odt->manifest_wtr, _xml("manifest:manifest"));
336   xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("xmlns:manifest"),
337                                _xml("urn:oasis:names:tc:opendocument:xmlns:manifest:1.0"));
338
339
340   /* Add a manifest entry for the document as a whole */
341   xmlTextWriterStartElement (odt->manifest_wtr, _xml("manifest:file-entry"));
342   xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("manifest:media-type"),  _xml("application/vnd.oasis.opendocument.text"));
343   xmlTextWriterWriteAttribute (odt->manifest_wtr, _xml("manifest:full-path"),  _xml("/"));
344   xmlTextWriterEndElement (odt->manifest_wtr);
345
346
347   write_meta_data (odt);
348   write_style_data (odt);
349
350   odt->content_wtr = create_writer (odt, "content.xml");
351   register_file (odt, "content.xml");
352
353
354   /* Some necessary junk at the start */
355   xmlTextWriterStartElement (odt->content_wtr, _xml("office:document-content"));
356   xmlTextWriterWriteAttribute (odt->content_wtr, _xml("xmlns:office"),
357                                _xml("urn:oasis:names:tc:opendocument:xmlns:office:1.0"));
358
359   xmlTextWriterWriteAttribute (odt->content_wtr, _xml("xmlns:text"),
360                                _xml("urn:oasis:names:tc:opendocument:xmlns:text:1.0"));
361
362   xmlTextWriterWriteAttribute (odt->content_wtr, _xml("xmlns:table"),
363                                _xml("urn:oasis:names:tc:opendocument:xmlns:table:1.0"));
364
365   xmlTextWriterWriteAttribute (odt->content_wtr, _xml("office:version"), _xml("1.1"));
366
367   xmlTextWriterStartElement (odt->content_wtr, _xml("office:body"));
368   xmlTextWriterStartElement (odt->content_wtr, _xml("office:text"));
369
370
371
372   /* Close the manifest */
373   xmlTextWriterEndElement (odt->manifest_wtr);
374   xmlTextWriterEndDocument (odt->manifest_wtr);
375   xmlFreeTextWriter (odt->manifest_wtr);
376
377   return d;
378 }
379
380 static void
381 odt_destroy (struct output_driver *driver)
382 {
383   struct odt_driver *odt = odt_driver_cast (driver);
384
385   if (odt->content_wtr != NULL)
386     {
387       struct string zip_cmd;
388
389       xmlTextWriterEndElement (odt->content_wtr); /* office:text */
390       xmlTextWriterEndElement (odt->content_wtr); /* office:body */
391       xmlTextWriterEndElement (odt->content_wtr); /* office:document-content */
392
393       xmlTextWriterEndDocument (odt->content_wtr);
394       xmlFreeTextWriter (odt->content_wtr);
395
396       /* Zip up the directory */
397       ds_init_empty (&zip_cmd);
398       ds_put_format (&zip_cmd,
399                      "cd %s ; rm -f ../%s; zip -q -X ../%s mimetype; zip -q -X -u -r ../%s .",
400                      odt->dirname, odt->file_name, odt->file_name, odt->file_name);
401       system (ds_cstr (&zip_cmd));
402       ds_destroy (&zip_cmd);
403     }
404
405   if ( !odt->debug )
406     {
407       /* Remove the temp dir */
408       struct string rm_cmd;
409
410       ds_init_empty (&rm_cmd);
411       ds_put_format (&rm_cmd, "rm -r %s", odt->dirname);
412       system (ds_cstr (&rm_cmd));
413       ds_destroy (&rm_cmd);
414     }
415   else
416     fprintf (stderr, "Not removing directory %s\n", odt->dirname);
417
418   free (odt->command_name);
419   free (odt->dirname);
420   free (odt);
421 }
422
423 static void
424 odt_submit_table (struct odt_driver *odt, struct table_item *item)
425 {
426   const struct table *tab = table_item_get_table (item);
427   const char *caption = table_item_get_caption (item);
428   int r, c;
429
430   /* Write a heading for the table */
431   if (caption != NULL)
432     {
433       xmlTextWriterStartElement (odt->content_wtr, _xml("text:h"));
434       xmlTextWriterWriteFormatAttribute (odt->content_wtr, _xml("text:level"),
435                                          "%d", 2);
436       xmlTextWriterWriteString (odt->content_wtr,
437                                 _xml (table_item_get_caption (item)) );
438       xmlTextWriterEndElement (odt->content_wtr);
439     }
440
441   /* Start table */
442   xmlTextWriterStartElement (odt->content_wtr, _xml("table:table"));
443   xmlTextWriterWriteFormatAttribute (odt->content_wtr, _xml("table:name"), 
444                                      "TABLE-%d", odt->table_num++);
445
446
447   /* Start column definitions */
448   xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-column"));
449   xmlTextWriterWriteFormatAttribute (odt->content_wtr, _xml("table:number-columns-repeated"), "%d", table_nc (tab));
450   xmlTextWriterEndElement (odt->content_wtr);
451
452
453   /* Deal with row headers */
454   if ( table_ht (tab) > 0)
455     xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-header-rows"));
456     
457
458   /* Write all the rows */
459   for (r = 0 ; r < table_nr (tab); ++r)
460     {
461       /* Start row definition */
462       xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-row"));
463
464       /* Write all the columns */
465       for (c = 0 ; c < table_nc (tab) ; ++c)
466         {
467           struct table_cell cell;
468
469           table_get_cell (tab, c, r, &cell);
470
471           if (c == cell.d[TABLE_HORZ][0] && r == cell.d[TABLE_VERT][0])
472             {
473               int colspan = table_cell_colspan (&cell);
474               int rowspan = table_cell_rowspan (&cell);
475
476               xmlTextWriterStartElement (odt->content_wtr, _xml("table:table-cell"));
477               xmlTextWriterWriteAttribute (odt->content_wtr, _xml("office:value-type"), _xml("string"));
478
479               if (colspan > 1)
480                 xmlTextWriterWriteFormatAttribute (
481                   odt->content_wtr, _xml("table:number-columns-spanned"),
482                   "%d", colspan);
483
484               if (rowspan > 1)
485                 xmlTextWriterWriteFormatAttribute (
486                   odt->content_wtr, _xml("table:number-rows-spanned"),
487                   "%d", rowspan);
488
489               xmlTextWriterStartElement (odt->content_wtr, _xml("text:p"));
490
491               if ( r < table_ht (tab) || c < table_hl (tab) )
492                 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:style-name"), _xml("Table_20_Heading"));
493               else
494                 xmlTextWriterWriteAttribute (odt->content_wtr, _xml("text:style-name"), _xml("Table_20_Contents"));
495
496               xmlTextWriterWriteString (odt->content_wtr, _xml(cell.contents));
497
498               xmlTextWriterEndElement (odt->content_wtr); /* text:p */
499               xmlTextWriterEndElement (odt->content_wtr); /* table:table-cell */
500             }
501           else
502             {
503               xmlTextWriterStartElement (odt->content_wtr, _xml("table:covered-table-cell"));
504               xmlTextWriterEndElement (odt->content_wtr);
505             }
506
507           table_cell_free (&cell);
508         }
509   
510       xmlTextWriterEndElement (odt->content_wtr); /* row */
511
512       if ( table_ht (tab) > 0 && r == table_ht (tab) - 1)
513         xmlTextWriterEndElement (odt->content_wtr); /* table-header-rows */
514     }
515
516   xmlTextWriterEndElement (odt->content_wtr); /* table */
517 }
518
519 static void
520 odt_output_text (struct odt_driver *odt, const char *text)
521 {
522   xmlTextWriterStartElement (odt->content_wtr, _xml("text:p"));
523   xmlTextWriterWriteString (odt->content_wtr, _xml(text));
524   xmlTextWriterEndElement (odt->content_wtr);
525 }
526
527 /* Submit a table to the ODT driver */
528 static void
529 odt_submit (struct output_driver *driver,
530             const struct output_item *output_item)
531 {
532   struct odt_driver *odt = odt_driver_cast (driver);
533
534   output_driver_track_current_command (output_item, &odt->command_name);
535
536   if (is_table_item (output_item))
537     odt_submit_table (odt, to_table_item (output_item));
538   else if (is_text_item (output_item))
539     {
540       /* XXX apply different styles based on text_item's type.  */
541       odt_output_text (odt, text_item_get_text (to_text_item (output_item)));
542     }
543   else if (is_message_item (output_item))
544     {
545       const struct message_item *message_item = to_message_item (output_item);
546       const struct msg *msg = message_item_get_msg (message_item);
547       char *s = msg_to_string (msg, odt->command_name);
548       odt_output_text (odt, s);
549       free (s);
550     }
551 }
552
553 struct output_driver_factory odt_driver_factory = { "odt", odt_create };
554
555 static const struct output_driver_class odt_driver_class =
556 {
557   "odf",
558   odt_destroy,
559   odt_submit,
560   NULL,
561 };