2007-01-21 [colin] 2.7.1cvs43
[claws.git] / src / exporthtml.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2002-2007 Match Grun and the Claws Mail team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19
20 /*
21  * Export address book to HTML file.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #  include "config.h"
26 #endif
27
28 #include <sys/stat.h>
29 #include <dirent.h>
30 #include <errno.h>
31 #include <time.h>
32 #include <string.h>
33 #include <glib.h>
34 #include <glib/gi18n.h>
35
36 #include "mgutils.h"
37 #include "utils.h"
38 #include "exporthtml.h"
39 #include "xmlprops.h"
40
41 #ifdef MKDIR_TAKES_ONE_ARG
42 #undef mkdir
43 #define mkdir(a,b) mkdir(a)
44 #endif
45
46 #define DFL_DIR_CLAWS_OUT  "claws-mail-out"
47 #define DFL_FILE_CLAWS_OUT "addressbook.html"
48
49 #define FMT_BUFSIZE         2048
50 #define SC_HTML_SPACE          "&nbsp;"
51 #define BORDER_SIZE         2
52 #define CELL_PADDING        2
53 #define CELL_SPACING        2
54 #define CHAR_ENCODING       "ISO-8859-1"
55
56 /* Stylesheet names */
57 #define FILENAME_NONE       ""
58 #define FILENAME_DEFAULT    "claws-mail.css"
59 #define FILENAME_FULL       "full.css"
60 #define FILENAME_CUSTOM     "custom.css"
61 #define FILENAME_CUSTOM2    "custom2.css"
62 #define FILENAME_CUSTOM3    "custom3.css"
63 #define FILENAME_CUSTOM4    "custom4.css"
64
65 /* Settings - properties */
66 #define EXML_PROPFILE_NAME  "exporthtml.xml"
67 #define EXMLPROP_DIRECTORY  "directory"
68 #define EXMLPROP_FILE       "file"
69 #define EXMLPROP_STYLESHEET "stylesheet"
70 #define EXMLPROP_FMT_NAME   "format-full-name"
71 #define EXMLPROP_FMT_EMAIL  "format-email-links"
72 #define EXMLPROP_FMT_ATTRIB "format-attributes"
73 #define EXMLPROP_BANDING    "color-banding"
74 #define EXMLPROP_VALUE_YES  "y"
75 #define EXMLPROP_VALUE_NO   "n"
76
77 static gchar *_idTagRowEven_ = "tab-row0";
78 static gchar *_idTagRowOdd_  = "tab-row1";
79
80 /*
81  * Header entry.
82  */
83 typedef struct _StylesheetEntry StylesheetEntry;
84 struct _StylesheetEntry {
85         gchar    *fileName;
86         gint     id;
87         gboolean dflValue;
88 };
89
90 /*
91  * Build stylesheet entry.
92  * Enter: ctl   Export control data.
93  *        file  Filename.
94  *        id    File id.
95  *        dfl   Default flag.
96  */
97 static void exporthtml_build_entry(
98                 ExportHtmlCtl *ctl, const gchar *file, const gint id,
99                 const gboolean dfl )
100 {
101         StylesheetEntry *entry;
102
103         entry = g_new0( StylesheetEntry, 1 );
104         entry->fileName = g_strdup( file );
105         entry->id = id;
106         entry->dflValue = dfl;
107         ctl->listStyle = g_list_append( ctl->listStyle, entry );
108 }
109
110 /*
111  * Free up object by releasing internal memory.
112  * Enter: ctl Export control data.
113  */
114 ExportHtmlCtl *exporthtml_create( void ) {
115         ExportHtmlCtl *ctl = g_new0( ExportHtmlCtl, 1 );
116
117         ctl->path = NULL;
118         ctl->dirOutput = NULL;
119         ctl->fileHtml = NULL;
120         ctl->encoding = NULL;
121         ctl->stylesheet = EXPORT_HTML_ID_NONE;
122         ctl->nameFormat = EXPORT_HTML_FIRST_LAST;
123         ctl->banding = FALSE;
124         ctl->linkEMail = FALSE;
125         ctl->showAttribs = FALSE;
126         ctl->retVal = MGU_SUCCESS;
127         ctl->listStyle = NULL;
128         ctl->rcCreate = 0;
129         ctl->settingsFile = g_strconcat(
130                 get_rc_dir(), G_DIR_SEPARATOR_S, EXML_PROPFILE_NAME, NULL );
131
132         /* Build stylesheet list */
133         exporthtml_build_entry(
134                 ctl, FILENAME_NONE,    EXPORT_HTML_ID_NONE, FALSE );
135         exporthtml_build_entry(
136                 ctl, FILENAME_DEFAULT, EXPORT_HTML_ID_DEFAULT, TRUE );
137         exporthtml_build_entry(
138                 ctl, FILENAME_FULL,    EXPORT_HTML_ID_FULL, FALSE );
139         exporthtml_build_entry(
140                 ctl, FILENAME_CUSTOM,  EXPORT_HTML_ID_CUSTOM, FALSE );
141         exporthtml_build_entry(
142                 ctl, FILENAME_CUSTOM2, EXPORT_HTML_ID_CUSTOM2, FALSE );
143         exporthtml_build_entry(
144                 ctl, FILENAME_CUSTOM3, EXPORT_HTML_ID_CUSTOM3, FALSE );
145         exporthtml_build_entry(
146                 ctl, FILENAME_CUSTOM4, EXPORT_HTML_ID_CUSTOM4, FALSE );
147
148         return ctl;
149 }
150
151 /*
152  * Free up object by releasing internal memory.
153  * Enter: ctl Export control data.
154  */
155 void exporthtml_free( ExportHtmlCtl *ctl ) {
156         GList *node;
157         StylesheetEntry *entry;
158
159         g_return_if_fail( ctl != NULL );
160
161         /* Free stylesheet list */
162         node = ctl->listStyle;
163         while( node ) {
164                 entry = ( StylesheetEntry * ) node->data;
165                 g_free( entry->fileName );
166                 entry->fileName = NULL;
167                 entry->id = 0;
168                 entry->dflValue = FALSE;
169                 g_free( entry );
170                 node->data = NULL;
171                 node = g_list_next( node );
172         }
173         g_list_free( ctl->listStyle );
174         ctl->listStyle = NULL;
175
176         g_free( ctl->path );
177         g_free( ctl->fileHtml );
178         g_free( ctl->encoding );
179         g_free( ctl->dirOutput );
180         g_free( ctl->settingsFile );
181
182         /* Clear pointers */
183         ctl->path = NULL;
184         ctl->dirOutput = NULL;
185         ctl->fileHtml = NULL;
186         ctl->encoding = NULL;
187         ctl->stylesheet = EXPORT_HTML_ID_NONE;
188         ctl->nameFormat = EXPORT_HTML_FIRST_LAST;
189         ctl->banding = FALSE;
190         ctl->linkEMail = FALSE;
191         ctl->showAttribs = FALSE;
192         ctl->retVal = MGU_SUCCESS;
193         ctl->rcCreate = 0;
194
195         /* Now release object */
196         g_free( ctl );
197 }
198
199 /*
200  * Find style entry.
201  * Enter: ctl Export control data.
202  * Return: Stylesheet object, or NULL if nothing found. If a default entry is
203  * found in list, it will be returned.
204  */
205 static StylesheetEntry *exporthtml_find_stylesheet( ExportHtmlCtl *ctl ) {
206         StylesheetEntry *retVal = NULL;
207         StylesheetEntry *entry;
208         GList *node;
209
210         node = ctl->listStyle;
211         while( node ) {
212                 entry = ( StylesheetEntry * ) node->data;
213                 if( entry->id == ctl->stylesheet ) return entry;
214                 if( entry->dflValue ) retVal = entry;
215                 node = g_list_next( node );
216         }
217         return retVal;
218 }
219
220 void exporthtml_set_path( ExportHtmlCtl *ctl, const gchar *value ) {
221         g_return_if_fail( ctl != NULL );
222         ctl->path = mgu_replace_string( ctl->path, value );
223         g_strstrip( ctl->path );
224 }
225 void exporthtml_set_file_html( ExportHtmlCtl *ctl, const gchar *value ) {
226         g_return_if_fail( ctl != NULL );
227         ctl->fileHtml = mgu_replace_string( ctl->fileHtml, value );
228         g_strstrip( ctl->fileHtml );
229 }
230 void exporthtml_set_encoding( ExportHtmlCtl *ctl, const gchar *value ) {
231         g_return_if_fail( ctl != NULL );
232         ctl->encoding = mgu_replace_string( ctl->encoding, value );
233         g_strstrip( ctl->encoding );
234 }
235 void exporthtml_set_stylesheet( ExportHtmlCtl *ctl, const gint value ) {
236         g_return_if_fail( ctl != NULL );
237         ctl->stylesheet = value;
238 }
239 void exporthtml_set_name_format( ExportHtmlCtl *ctl, const gint value ) {
240         g_return_if_fail( ctl != NULL );
241         ctl->nameFormat = value;
242 }
243 void exporthtml_set_banding( ExportHtmlCtl *ctl, const gboolean value ) {
244         g_return_if_fail( ctl != NULL );
245         ctl->banding = value;
246 }
247 void exporthtml_set_link_email( ExportHtmlCtl *ctl, const gboolean value ) {
248         g_return_if_fail( ctl != NULL );
249         ctl->linkEMail = value;
250 }
251 void exporthtml_set_attributes( ExportHtmlCtl *ctl, const gboolean value ) {
252         g_return_if_fail( ctl != NULL );
253         ctl->showAttribs = value;
254 }
255
256 /*
257  * Create default CSS file.
258  * Enter:  fileSpec File to create.
259  * Return: Status code.
260  */
261 static gint exporthtml_create_css_dfl( const gchar *fileSpec ) {
262         FILE *cssFile;
263
264         cssFile = g_fopen( fileSpec, "rb" );
265         if( cssFile ) {
266                 fclose( cssFile );
267                 return MGU_SUCCESS;
268         }
269         cssFile = g_fopen( fileSpec, "wb" );
270         if( ! cssFile ) {
271                 return MGU_OPEN_FILE;
272         }
273
274         fprintf( cssFile, "body {\n\tbackground: #ffffe0;\n" );
275         fprintf( cssFile, "\tfont-family: lucida, helvetica, sans-serif;\n" );
276         fprintf( cssFile, "\tfont-size: 10pt;\n" );
277         fprintf( cssFile, "}\n" );
278         fprintf( cssFile, "h1 {\n" );
279         fprintf( cssFile, "\tcolor: #000000;\n" );
280         fprintf( cssFile, "\ttext-align: center;\n" );
281         fprintf( cssFile, "}\n" );
282         fprintf( cssFile, "th {\n" );
283         fprintf( cssFile, "\tfont-size: 10pt;\n" );
284         fprintf( cssFile, "}\n" );
285         fprintf( cssFile, "td {\n" );
286         fprintf( cssFile, "\tfont-size: 10pt;\n" );
287         fprintf( cssFile, "}\n" );
288         fprintf( cssFile, ".fmt-folder {\n" );
289         fprintf( cssFile, "\tcolor: #0000ff;\n" );
290         fprintf( cssFile, "\tfont-size: 18pt;\n" );
291         fprintf( cssFile, "\tfont-weight: bold;\n" );
292         fprintf( cssFile, "}\n" );
293         fprintf( cssFile, ".tab-head {\n" );
294         fprintf( cssFile, "\tbackground: #80c0f0;\n" );
295         fprintf( cssFile, "}\n" );
296         fprintf( cssFile, ".tab-dn {\n" );
297         fprintf( cssFile, "}\n" );
298         fprintf( cssFile, ".tab-addr {\n" );
299         fprintf( cssFile, "\tfont-style: italic;\n" );
300         fprintf( cssFile, "}\n" );
301         fprintf( cssFile, ".tab-email {\n" );
302         fprintf( cssFile, "\tfont-weight: bold;\n" );
303         fprintf( cssFile, "\tfont-style: italic;\n" );
304         fprintf( cssFile, "}\n" );
305         fprintf( cssFile, ".tab-fn {\n" );
306         fprintf( cssFile, "}\n" );
307         fprintf( cssFile, ".tab-attr {\n" );
308         fprintf( cssFile, "}\n" );
309
310         fclose( cssFile );
311         return MGU_SUCCESS;
312 }
313
314 /*
315  * Create full CSS file.
316  * Enter:  fileSpec File to create.
317  * Return: Status code.
318  */
319 static gint exporthtml_create_css_full( const gchar *fileSpec ) {
320         FILE *cssFile;
321
322         cssFile = g_fopen( fileSpec, "rb" );
323         if( cssFile ) {
324                 fclose( cssFile );
325                 return MGU_SUCCESS;
326         }
327         cssFile = g_fopen( fileSpec, "wb" );
328         if( ! cssFile ) {
329                 return MGU_OPEN_FILE;
330         }
331
332         fprintf( cssFile, "body {\n\tbackground: #ffffe0;\n" );
333         fprintf( cssFile, "\tfont-family: lucida, helvetica, sans-serif;\n" );
334         fprintf( cssFile, "\tfont-size: 10pt;\n" );
335         fprintf( cssFile, "}\n" );
336         fprintf( cssFile, "h1 {\n" );
337         fprintf( cssFile, "\tcolor: #000000;\n" );
338         fprintf( cssFile, "\ttext-align: center;\n" );
339         fprintf( cssFile, "}\n" );
340         fprintf( cssFile, "th {\n" );
341         fprintf( cssFile, "\tfont-size: 10pt;\n" );
342         fprintf( cssFile, "}\n" );
343         fprintf( cssFile, "td {\n" );
344         fprintf( cssFile, "\tfont-size: 10pt;\n" );
345         fprintf( cssFile, "}\n" );
346         fprintf( cssFile, ".fmt-folder {\n" );
347         fprintf( cssFile, "\tcolor: #0000ff;\n" );
348         fprintf( cssFile, "\tfont-size: 18pt;\n" );
349         fprintf( cssFile, "\tfont-weight: bold;\n" );
350         fprintf( cssFile, "}\n" );
351         fprintf( cssFile, ".tab-head {\n" );
352         fprintf( cssFile, "\tbackground: #80c0f0;\n" );
353         fprintf( cssFile, "}\n" );
354         fprintf( cssFile, ".tab-row0 {\n" );
355         fprintf( cssFile, "\tbackground: #f0f0f0;\n" );
356         fprintf( cssFile, "}\n" );
357         fprintf( cssFile, ".tab-row1 {\n" );
358         fprintf( cssFile, "\tbackground: #d0d0d0;\n" );
359         fprintf( cssFile, "}\n" );
360         fprintf( cssFile, ".tab-dn {\n" );
361         fprintf( cssFile, "}\n" );
362         fprintf( cssFile, ".tab-addr {\n" );
363         fprintf( cssFile, "\tfont-style: italic;\n" );
364         fprintf( cssFile, "}\n" );
365         fprintf( cssFile, ".tab-email {\n" );
366         fprintf( cssFile, "\tfont-weight: bold;\n" );
367         fprintf( cssFile, "\tfont-style: italic;\n" );
368         fprintf( cssFile, "}\n" );
369         fprintf( cssFile, ".tab-fn {\n" );
370         fprintf( cssFile, "}\n" );
371         fprintf( cssFile, ".tab-attr {\n" );
372         fprintf( cssFile, "}\n" );
373
374         fclose( cssFile );
375         return MGU_SUCCESS;
376 }
377
378 /*
379  * Create stylesheet files.
380  * Enter:  ctl  Export control data.
381  */
382 static void exporthtml_create_css_files( ExportHtmlCtl *ctl ) {
383         gchar *fileSpec;
384         GList *node;
385
386         node = ctl->listStyle;
387         while( node ) {
388                 StylesheetEntry *entry = node->data;
389                 node = g_list_next( node );
390                 if( strlen( entry->fileName ) ) {
391                         fileSpec = g_strconcat(
392                                         ctl->dirOutput, G_DIR_SEPARATOR_S, 
393                                         entry->fileName, NULL );
394                         if( entry->id == EXPORT_HTML_ID_DEFAULT ) {
395                                 exporthtml_create_css_dfl( fileSpec );
396                         }
397                         else if( entry->id != EXPORT_HTML_ID_NONE ) {
398                                 exporthtml_create_css_full( fileSpec );
399                         }
400                         g_free( fileSpec );
401                 }
402         }
403 }
404
405 /*
406  * Comparison using linked list elements.
407  */
408 static gint exporthtml_compare_name(
409         gconstpointer ptr1, gconstpointer ptr2 )
410 {
411         const AddrItemObject *item1 = ptr1;
412         const AddrItemObject *item2 = ptr2;
413         const gchar *name1 = NULL, *name2 = NULL;
414         if( item1 ) name1 = ADDRITEM_NAME( item1 );
415         if( item2 ) name2 = ADDRITEM_NAME( item2 );
416         if( ! name1 ) return ( name2 != NULL );
417         if( ! name2 ) return -1;
418         return g_utf8_collate( name1, name2 );
419 }
420
421 /*
422  * Comparison using linked list elements.
423  */
424 static gint exporthtml_compare_email(
425         gconstpointer ptr1, gconstpointer ptr2 )
426 {
427         const ItemEMail *email1 = ptr1;
428         const ItemEMail *email2 = ptr2;
429         const gchar *name1 = NULL, *name2 = NULL;
430         if( email1 ) name1 = email1->address;
431         if( email2 ) name2 = email2->address;
432         if( ! name1 ) return ( name2 != NULL );
433         if( ! name2 ) return -1;
434         return g_utf8_collate( name1, name2 );
435 }
436
437 /*
438  * Comparison using linked list elements.
439  */
440 static gint exporthtml_compare_attrib(
441         gconstpointer ptr1, gconstpointer ptr2 )
442 {
443         const UserAttribute *attr1 = ptr1;
444         const UserAttribute *attr2 = ptr2;
445         const gchar *name1 = NULL, *name2 = NULL;
446         if( attr1 ) name1 = attr1->name;
447         if( attr2 ) name2 = attr2->name;
448         if( ! name1 ) return ( name2 != NULL );
449         if( ! name2 ) return -1;
450         return g_utf8_collate( name1, name2 );
451 }
452
453 /*
454  * Build sorted list of named items.
455  * Enter:  list  List of items to sorted.
456  * Return: Sorted list.
457  * Note: List should freed after use. Items referenced by list should not be
458  * freed since they are managed by the address cache.
459  */
460 static GList *exporthtml_sort_name( const GList *list ) {
461         const GList *node;
462         GList *sorted = NULL;
463
464         node = list;
465         while( node ) {
466                 sorted = g_list_insert_sorted(
467                                 sorted, node->data, exporthtml_compare_name );
468                 node = g_list_next( node );
469         }
470         return sorted;
471 }
472
473 /*
474  * Build sorted list of email items.
475  * Enter:  list  List of E-Mail items to sorted.
476  * Return: Sorted list.
477  * Note: List should freed after use. Items referenced by list should not be
478  * freed since they are managed by the address cache.
479  */
480 static GList *exporthtml_sort_email( const GList *list ) {
481         const GList *node;
482         GList *sorted = NULL;
483
484         node = list;
485         while( node ) {
486                 sorted = g_list_insert_sorted(
487                                 sorted, node->data, exporthtml_compare_email );
488                 node = g_list_next( node );
489         }
490         return sorted;
491 }
492
493 /*
494  * Build sorted list of attributes.
495  * Enter:  list  List of items to sorted.
496  * Return: Sorted list.
497  * Note: List should freed after use. Items referenced by list should not be
498  * freed since they are managed by the address cache.
499  */
500 static GList *exporthtml_sort_attrib( const GList *list ) {
501         const GList *node;
502         GList *sorted = NULL;
503
504         sorted = NULL;
505         node = list;
506         while( node ) {
507                 sorted = g_list_insert_sorted(
508                                 sorted, node->data, exporthtml_compare_attrib );
509                 node = g_list_next( node );
510         }
511         return sorted;
512 }
513
514 /*
515  * Format a list of E-Mail addresses.
516  * Enter: ctl       Export control data.
517  *        stream    Output stream.
518  *        listEMail List of addresses.
519  *        sortFlag  Set to TRUE if address list should be sorted.
520  */
521 static void exporthtml_fmt_email(
522                 ExportHtmlCtl *ctl, FILE *stream, const GList *listEMail,
523                 gboolean sortFlag )
524 {
525         const GList *node;
526         GList *list;
527         gchar *name;
528
529         if( listEMail == NULL ) {
530                 fprintf( stream, SC_HTML_SPACE );
531                 return;
532         }
533
534         list = NULL;
535         if( sortFlag ) {
536                 node = list = exporthtml_sort_email( listEMail );
537         }
538         else {
539                 node = listEMail;
540         }
541
542         while( node ) {
543                 ItemEMail *email = ( ItemEMail * ) node->data;
544                 node = g_list_next( node );
545
546                 name = ADDRITEM_NAME( email );
547                 if( name ) {
548                         fprintf( stream, "%s ", name );
549                 }
550                 if( ctl->linkEMail ) {
551                         fprintf( stream, "<a href=\"mailto:%s\">",
552                                 email->address );
553                 }
554                 fprintf( stream, "<span class=\"tab-email\">" );
555                 fprintf( stream, "%s", email->address );
556                 fprintf( stream, "</span>" );
557                 if( ctl->linkEMail ) {
558                         fprintf( stream, "</a>" );
559                 }
560                 if( email->remarks ) {
561                         if( strlen( email->remarks ) ) {
562                                 fprintf( stream, " (%s)", email->remarks );
563                         }
564                 }
565                 fprintf( stream, "<br>\n" );
566         }
567         g_list_free( list );
568 }
569
570 /*
571  * Format groups in an address book folder.
572  * Enter:  ctl      Export control data.
573  *         stream   Output stream.
574  *         folder   Folder.
575  *         prevFlag If FALSE, list of persons were output.
576  * Return: TRUE if no groups were formatted.
577  */
578 static gboolean exporthtml_fmt_group(
579                 ExportHtmlCtl *ctl, FILE *stream, const ItemFolder *folder,
580                 gboolean prevFlag )
581 {
582         gboolean retVal, band;
583         GList *node, *list;
584         const gchar *tagName;
585
586         retVal = TRUE;
587         if( folder->listGroup == NULL ) return retVal;
588
589         /* Write separator */
590         if( ! prevFlag ) {
591                 fprintf( stream, "<br>\n" );
592         }
593
594         /* Write table headers */
595         fprintf( stream, "<table" );
596         fprintf( stream, " border=\"%d\"", BORDER_SIZE );
597         fprintf( stream, " cellpadding=\"%d\"", CELL_PADDING );
598         fprintf( stream, " cellspacing=\"%d\"", CELL_SPACING );
599         fprintf( stream, ">\n" );
600
601         fprintf( stream, "<tr class=\"tab-head\">\n" );
602         fprintf( stream, "  <th width=\"200\">" );
603         fprintf( stream, "%s", _( "Group Name" ) );
604         fprintf( stream, "</th>\n" );
605         fprintf( stream, "  <th width=\"300\">" );
606         fprintf( stream, "%s", _( "Email Address" ) );
607         fprintf( stream, "</th>\n" );
608         fprintf( stream, "</tr>\n" );
609         list = exporthtml_sort_name( folder->listGroup );
610
611         band = FALSE;
612         node = list;
613         while( node ) {
614                 AddrItemObject *aio = node->data;
615                 if( aio && aio->type == ITEMTYPE_GROUP ) {
616                         ItemGroup *group = ( ItemGroup * ) aio;
617
618                         fprintf( stream, "<tr valign=\"top\"" );
619                         if( ctl->banding ) {
620                                 if( band ) {
621                                         tagName = _idTagRowOdd_;
622                                 }
623                                 else {
624                                         tagName = _idTagRowEven_;
625                                 }
626                                 fprintf( stream, " class=\"%s\"", tagName );
627                                 band = ! band;
628                         }
629                         fprintf( stream, "\">\n" );
630
631                         fprintf( stream, "  <td class=\"tab-dn\">" );
632                         fprintf( stream, "%s", ADDRITEM_NAME( group ) );
633                         fprintf( stream, "</td>\n" );
634                         fprintf( stream, "  <td class=\"tab-addr\">" );
635                         exporthtml_fmt_email( ctl, stream, group->listEMail, TRUE );
636                         fprintf( stream, "</td>\n" );
637                         fprintf( stream, "</tr>\n" );
638                         retVal = FALSE;
639                 }
640                 node = g_list_next( node );
641         }
642
643         g_list_free( list );
644         fprintf( stream, "</table>\n" );
645         return retVal;
646 }
647
648 /*
649  * Format a list of E-Mail addresses.
650  * Enter: ctl       Export control data.
651  *        stream    Output stream.
652  *        listAttr  List of attributes.
653  */
654 static void exporthtml_fmt_attribs(
655                 ExportHtmlCtl *ctl, FILE *stream, const GList *listAttr )
656 {
657         const GList *node;
658         GList *list;
659
660         if( listAttr == NULL ) {
661                 fprintf( stream, SC_HTML_SPACE );
662                 return;
663         }
664
665         fprintf( stream, "<table border=\"0\">\n" );
666         node = list = exporthtml_sort_attrib( listAttr );
667         while( node ) {
668                 UserAttribute *attr = ( UserAttribute * ) node->data;
669                 node = g_list_next( node );
670                 fprintf( stream, "<tr valign=\"top\">" );
671                 fprintf( stream, "<td align=\"right\">%s:</td>", attr->name );
672                 fprintf( stream, "<td>%s</td>", attr->value );
673                 fprintf( stream, "</tr>\n" );
674         }
675
676         g_list_free( list );
677         fprintf( stream, "</table>" );
678 }
679
680 /*
681  * Format full name.
682  * Enter:  ctl     Export control data.
683  *         buf     Output buffer.
684  *         person  Person to format.
685  */
686 static void exporthtml_fmt_fullname(
687                 ExportHtmlCtl *ctl, gchar *buf, const ItemPerson *person )
688 {
689         gboolean flag;
690
691         if( ctl->nameFormat == EXPORT_HTML_LAST_FIRST ) {
692                 flag = FALSE;
693                 if( person->lastName ) {
694                         if( *person->lastName ) {
695                                 strcat( buf, " " );
696                                 strcat( buf, person->lastName );
697                                 flag = TRUE;
698                         }
699                 }
700                 if( person->firstName ) {
701                         if( *person->firstName ) {
702                                 if( flag ) {
703                                         strcat( buf, ", " );
704                                 }
705                                 strcat( buf, person->firstName );
706                         }
707                 }
708         }
709         else {
710                 if( person->firstName ) {
711                         if( *person->firstName ) {
712                                 strcat( buf, person->firstName );
713                         }
714                 }
715                 if( person->lastName ) {
716                         if( *person->lastName ) {
717                                 strcat( buf, " " );
718                                 strcat( buf, person->lastName );
719                         }
720                 }
721         }
722         g_strstrip( buf );
723
724         flag = FALSE;
725         if( *buf ) flag = TRUE;
726         if( person->nickName ) {
727                 if( strlen( person->nickName ) ) {
728                         if( flag ) {
729                                 strcat( buf, " (" );
730                         }
731                         strcat( buf, person->nickName );
732                         if( flag ) {
733                                 strcat( buf, ")" );
734                         }
735                 }
736         }
737         g_strstrip( buf );
738 }
739
740 /*
741  * Format persons in an address book folder.
742  * Enter:  ctl     Export control data.
743  *         stream  Output stream.
744  *         folder  Folder.
745  * Return: TRUE if no persons were formatted.
746  */
747 static gboolean exporthtml_fmt_person(
748                 ExportHtmlCtl *ctl, FILE *stream, const ItemFolder *folder )
749 {
750         gboolean retVal, band;
751         GList *node, *list;
752         gchar buf[ FMT_BUFSIZE ];
753         const gchar *tagName;
754
755         retVal = TRUE;
756         if( folder->listPerson == NULL ) return retVal;
757
758         /* Write table headers */
759         fprintf( stream, "<table" );
760         fprintf( stream, " border=\"%d\"", BORDER_SIZE );
761         fprintf( stream, " cellpadding=\"%d\"", CELL_PADDING );
762         fprintf( stream, " cellspacing=\"%d\"", CELL_SPACING );
763         fprintf( stream, ">\n" );
764
765         fprintf( stream, "<tr class=\"tab-head\">\n" );
766         fprintf( stream, "  <th width=\"200\">" );
767         fprintf( stream, "%s", _( "Display Name" ) );
768         fprintf( stream, "</th>\n" );
769         fprintf( stream, "  <th width=\"300\">" );
770         fprintf( stream, "%s", _( "Email Address" ) );
771         fprintf( stream, "</th>\n" );
772         fprintf( stream, "  <th width=\"200\">" );
773         fprintf( stream, "%s", _( "Full Name" ) );
774         fprintf( stream, "</th>\n" );
775         if( ctl->showAttribs ) {
776                 fprintf( stream, "  <th width=\"250\">" );
777                 fprintf( stream, "%s", _( "Attributes" ) );
778                 fprintf( stream, "</th>\n" );
779         }
780         fprintf( stream, "</tr>\n" );
781
782         band = FALSE;
783         node = list = exporthtml_sort_name( folder->listPerson );
784         while( node ) {
785                 AddrItemObject *aio = node->data;
786                 if( aio && aio->type == ITEMTYPE_PERSON ) {
787                         ItemPerson *person = ( ItemPerson * ) aio;
788
789                         /* Format first/last/nick name */
790                         *buf = '\0';
791                         exporthtml_fmt_fullname( ctl, buf,person );
792
793                         fprintf( stream, "<tr valign=\"top\"" );
794                         if( ctl->banding ) {
795                                 if( band ) {
796                                         tagName = _idTagRowOdd_;
797                                 }
798                                 else {
799                                         tagName = _idTagRowEven_;
800                                 }
801                                 fprintf( stream, " class=\"%s\"", tagName );
802                                 band = ! band;
803                         }
804                         fprintf( stream, ">\n" );
805
806                         fprintf( stream, "  <td class=\"tab-dn\">" );
807                         fprintf( stream, "%s", ADDRITEM_NAME( person ) );
808                         fprintf( stream, "</td>\n" );
809
810                         fprintf( stream, "  <td class=\"tab-addr\">" );
811                         exporthtml_fmt_email( ctl, stream, person->listEMail, FALSE );
812                         fprintf( stream, "</td>\n" );
813
814                         fprintf( stream, "  <td class=\"tab-fn\">" );
815                         if( *buf ) {
816                                 fprintf( stream, "%s", buf );
817                         }
818                         else {
819                                 fprintf( stream, "%s", SC_HTML_SPACE );
820                         }
821                         fprintf( stream, "</td>\n" );
822
823                         if( ctl->showAttribs ) {
824                                 fprintf( stream, "  <td class=\"tab-attr\">" );
825                                 exporthtml_fmt_attribs(
826                                         ctl, stream, person->listAttrib );
827                                 fprintf( stream, "</td>\n" );
828                         }
829                         fprintf( stream, "</tr>\n" );
830
831                         retVal = FALSE;
832                 }
833                 node = g_list_next( node );
834         }
835
836         g_list_free( list );
837         fprintf( stream, "</table>\n" );
838         return retVal;
839 }
840
841 /*
842  * Format folder heirarchy.
843  * Enter: stream Output stream.
844  *        list   Heirarchy list.
845  */
846 static void exporthtml_fmt_folderhead( FILE *stream, const GList *list ) {
847         const GList *node;
848         gboolean flag;
849         gchar *name;
850
851         flag = FALSE;
852         node = list;
853         while( node ) {
854                 AddrItemObject *aio = node->data;
855                 if( aio && aio->type == ITEMTYPE_FOLDER ) {
856                         ItemFolder *folder = ( ItemFolder * ) aio;
857
858                         name = ADDRITEM_NAME( folder );
859                         if( name ) {
860                                 if( flag ) {
861                                         fprintf( stream, "&nbsp;&gt;&nbsp;" );
862                                 }
863                                 fprintf( stream, "%s", name );
864                                 flag = TRUE;
865                         }
866                 }
867                 node = g_list_next( node );
868         }
869 }
870
871 /*
872  * Format an address book folder.
873  * Enter: ctl    Export control data.
874  *        stream Output stream.
875  *        folder Folder.
876  */
877 static void exporthtml_fmt_folder(
878                 ExportHtmlCtl *ctl, FILE *stream, const ItemFolder *folder )
879 {
880         const GList *node;
881         GList *listHeir, *list;
882         const gchar *name;
883         gboolean ret1, ret2;
884
885         name = ADDRITEM_NAME( folder );
886         if( name ) {
887                 listHeir = addritem_folder_path( folder, TRUE );
888                 if( listHeir ) {
889                         fprintf( stream, "<p class=\"fmt-folder\">" );
890                         fprintf( stream, "%s: ", _( "Folder" ) );
891                         exporthtml_fmt_folderhead( stream, listHeir );
892                         fprintf( stream, "</p>\n" );
893                         g_list_free( listHeir );
894                 }
895         }
896
897         ret1 = exporthtml_fmt_person( ctl, stream, folder );
898         ret2 = exporthtml_fmt_group( ctl, stream, folder, ret1 );
899
900         node = list = exporthtml_sort_name( folder->listFolder );
901         while( node ) {
902                 AddrItemObject *aio = node->data;
903                 if( aio && aio->type == ITEMTYPE_FOLDER ) {
904                         ItemFolder *subFolder = ( ItemFolder * ) aio;
905                         exporthtml_fmt_folder( ctl, stream, subFolder );
906                 }
907                 node = g_list_next( node );
908         }
909         if( list ) {
910                 g_list_free( list );
911         }
912 }
913
914 /*
915  * Format header block.
916  * Enter:  ctl    Export control data.
917  *         stream Output stream.
918  *         title  Page title.
919  */
920 static void exporthtml_fmt_header(
921                 ExportHtmlCtl *ctl, FILE *stream, gchar *title )
922 {
923         StylesheetEntry *entry;
924
925         entry = exporthtml_find_stylesheet( ctl );
926
927         fprintf( stream,
928                 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\">\n" );
929         fprintf( stream, "<html>\n" );
930         fprintf( stream, "<head>\n" );
931
932         if( ctl->encoding && strlen( ctl->encoding ) > 0 ) {
933                 fprintf( stream, "<meta " );
934                 fprintf( stream, "http-equiv=\"Content-Type\" " );
935                 fprintf( stream, "content=\"text/html; charset=%s\">\n",
936                         ctl->encoding );
937         }
938
939         fprintf( stream, "<title>%s</title>\n", title );
940
941         if( entry != NULL ) {
942                 if( entry->fileName && strlen( entry->fileName ) > 0 ) {
943                         fprintf( stream, "<link " );
944                         fprintf( stream, "rel=\"stylesheet\" " );
945                         fprintf( stream, "type=\"text/css\" " );
946                         fprintf( stream, "href=\"%s\" >\n", entry->fileName );
947                 }
948         }
949         fprintf( stream, "<body>\n" );
950         fprintf( stream, "</head>\n" );
951 }
952
953 /*
954  * ============================================================================
955  * Export address book to HTML file.
956  * Enter:  ctl   Export control data.
957  *         cache Address book/data source cache.
958  * Return: Status.
959  * ============================================================================
960  */
961 void exporthtml_process(
962         ExportHtmlCtl *ctl, AddressCache *cache )
963 {
964         ItemFolder *rootFolder;
965         FILE *htmlFile;
966         time_t tt;
967         gchar *dsName;
968         static gchar *title;
969
970         htmlFile = g_fopen( ctl->path, "wb" );
971         if( ! htmlFile ) {
972                 /* Cannot open file */
973                 printf( "Cannot open file for write\n" );
974                 ctl->retVal = MGU_OPEN_FILE;
975                 return;
976         }
977
978         title = _( "Claws Mail Address Book" );
979         rootFolder = cache->rootFolder;
980         dsName = cache->name;
981
982         exporthtml_fmt_header( ctl, htmlFile, title );
983
984         fprintf( htmlFile, "<body>\n" );
985         fprintf( htmlFile, "<h1>%s</h1>\n", title );
986
987         fprintf( htmlFile, "<p class=\"fmt-folder\">" );
988         fprintf( htmlFile, "%s: ", _( "Address Book" ) );
989         fprintf( htmlFile, "%s", dsName );
990         fprintf( htmlFile, "</p>\n" );
991
992         exporthtml_fmt_folder( ctl, htmlFile, rootFolder );
993
994         tt = time( NULL );
995         fprintf( htmlFile, "<p>%s</p>\n", ctime( &tt ) );
996         fprintf( htmlFile, "<hr width=\"100%%\"></hr>\n" );
997
998         fprintf( htmlFile, "</body>\n" );
999         fprintf( htmlFile, "</html>\n" );
1000
1001         fclose( htmlFile );
1002         ctl->retVal = MGU_SUCCESS;
1003
1004         /* Create stylesheet files */
1005         exporthtml_create_css_files( ctl );
1006
1007 }
1008
1009 /*
1010  * Build full export file specification.
1011  * Enter:  ctl  Export control data.
1012  */
1013 static void exporthtml_build_filespec( ExportHtmlCtl *ctl ) {
1014         gchar *fileSpec;
1015
1016         fileSpec = g_strconcat(
1017                 ctl->dirOutput, G_DIR_SEPARATOR_S, ctl->fileHtml, NULL );
1018         ctl->path = mgu_replace_string( ctl->path, fileSpec );
1019         g_free( fileSpec );
1020 }
1021
1022 /*
1023  * ============================================================================
1024  * Parse directory and filename from full export file specification.
1025  * Enter:  ctl      Export control data.
1026  *         fileSpec File spec.
1027  * ============================================================================
1028  */
1029 void exporthtml_parse_filespec( ExportHtmlCtl *ctl, gchar *fileSpec ) {
1030         gchar *t;
1031         gchar *base = g_path_get_basename(fileSpec);
1032
1033         ctl->fileHtml =
1034                 mgu_replace_string( ctl->fileHtml, base );
1035         g_free(base);
1036         t = g_path_get_dirname( fileSpec );
1037         ctl->dirOutput = mgu_replace_string( ctl->dirOutput, t );
1038         g_free( t );
1039         ctl->path = mgu_replace_string( ctl->path, fileSpec );
1040 }
1041
1042 /*
1043  * ============================================================================
1044  * Test whether directory exists.
1045  * Enter:  ctl  Export control data.
1046  * Return: TRUE if exists.
1047  * ============================================================================
1048  */
1049 gboolean exporthtml_test_dir( ExportHtmlCtl *ctl ) {
1050         gboolean retVal;
1051         DIR *dp;
1052
1053         retVal = FALSE;
1054         if((dp = opendir( ctl->dirOutput )) != NULL) {
1055                 retVal = TRUE;
1056                 closedir( dp );
1057         }
1058         return retVal;
1059 }
1060
1061 /*
1062  * ============================================================================
1063  * Create output directory.
1064  * Enter:  ctl  Export control data.
1065  * Return: TRUE if directory created.
1066  * ============================================================================
1067  */
1068 gboolean exporthtml_create_dir( ExportHtmlCtl *ctl ) {
1069         gboolean retVal = FALSE;
1070
1071         ctl->rcCreate = 0;
1072         if( mkdir( ctl->dirOutput, S_IRWXU ) == 0 ) {
1073                 retVal = TRUE;
1074         }
1075         else {
1076                 ctl->rcCreate = errno;
1077         }
1078         return retVal;
1079 }
1080
1081 /*
1082  * ============================================================================
1083  * Retrieve create directory error message.
1084  * Enter:  ctl  Export control data.
1085  * Return: Message.
1086  * ============================================================================
1087  */
1088 gchar *exporthtml_get_create_msg( ExportHtmlCtl *ctl ) {
1089         gchar *msg;
1090
1091         if( ctl->rcCreate == EEXIST ) {
1092                 msg = _( "Name already exists but is not a directory." );
1093         }
1094         else if( ctl->rcCreate == EACCES ) {
1095                 msg = _( "No permissions to create directory." );
1096         }
1097         else if( ctl->rcCreate == ENAMETOOLONG ) {
1098                 msg = _( "Name is too long." );
1099         }
1100         else {
1101                 msg = _( "Not specified." );
1102         }
1103         return msg;
1104 }
1105
1106 /*
1107  * Set default values.
1108  * Enter: ctl Export control data.
1109  */
1110 static void exporthtml_default_values( ExportHtmlCtl *ctl ) {
1111         gchar *str;
1112
1113         str = g_strconcat(
1114                 get_home_dir(), G_DIR_SEPARATOR_S,
1115                 DFL_DIR_CLAWS_OUT, NULL );
1116
1117         ctl->dirOutput = mgu_replace_string( ctl->dirOutput, str );
1118         g_free( str );
1119
1120         ctl->fileHtml =
1121                 mgu_replace_string( ctl->fileHtml, DFL_FILE_CLAWS_OUT );
1122         ctl->encoding = NULL;
1123         ctl->stylesheet = EXPORT_HTML_ID_DEFAULT;
1124         ctl->nameFormat = EXPORT_HTML_FIRST_LAST;
1125         ctl->banding = TRUE;
1126         ctl->linkEMail = TRUE;
1127         ctl->showAttribs = TRUE;
1128         ctl->retVal = MGU_SUCCESS;
1129 }
1130
1131 /*
1132  * ============================================================================
1133  * Load settings from XML properties file.
1134  * Enter: ctl  Export control data.
1135  * ============================================================================
1136  */
1137 void exporthtml_load_settings( ExportHtmlCtl *ctl ) {
1138         XmlProperty *props;
1139         gint rc;
1140         gchar buf[256];
1141
1142         *buf = '\0';
1143         props = xmlprops_create();
1144         xmlprops_set_path( props, ctl->settingsFile );
1145         rc = xmlprops_load_file( props );
1146         if( rc == 0 ) {
1147                 /* Read settings */
1148                 xmlprops_get_property_s( props, EXMLPROP_DIRECTORY, buf );
1149                 ctl->dirOutput = mgu_replace_string( ctl->dirOutput, buf );
1150
1151                 xmlprops_get_property_s( props, EXMLPROP_FILE, buf );
1152                 ctl->fileHtml = mgu_replace_string( ctl->fileHtml, buf );
1153
1154                 ctl->stylesheet =
1155                         xmlprops_get_property_i( props, EXMLPROP_STYLESHEET );
1156                 ctl->nameFormat =
1157                         xmlprops_get_property_i( props, EXMLPROP_FMT_NAME );
1158                 ctl->banding =
1159                         xmlprops_get_property_b( props, EXMLPROP_BANDING );
1160                 ctl->linkEMail =
1161                         xmlprops_get_property_b( props, EXMLPROP_FMT_EMAIL );
1162                 ctl->showAttribs =
1163                         xmlprops_get_property_b( props, EXMLPROP_FMT_ATTRIB );
1164         }
1165         else {
1166                 /* Set default values */
1167                 exporthtml_default_values( ctl );
1168         }
1169         exporthtml_build_filespec( ctl );
1170         /* exporthtml_print( ctl, stdout ); */
1171
1172         xmlprops_free( props );
1173 }
1174
1175 /*
1176  * ============================================================================
1177  * Save settings to XML properties file.
1178  * Enter: ctl  Export control data.
1179  * ============================================================================
1180  */
1181 void exporthtml_save_settings( ExportHtmlCtl *ctl ) {
1182         XmlProperty *props;
1183
1184         props = xmlprops_create();
1185         xmlprops_set_path( props, ctl->settingsFile );
1186
1187         xmlprops_set_property( props, EXMLPROP_DIRECTORY, ctl->dirOutput );
1188         xmlprops_set_property( props, EXMLPROP_FILE, ctl->fileHtml );
1189         xmlprops_set_property_i( props, EXMLPROP_STYLESHEET, ctl->stylesheet );
1190         xmlprops_set_property_i( props, EXMLPROP_FMT_NAME, ctl->nameFormat );
1191         xmlprops_set_property_b( props, EXMLPROP_BANDING, ctl->banding );
1192         xmlprops_set_property_b( props, EXMLPROP_FMT_EMAIL, ctl->linkEMail );
1193         xmlprops_set_property_b( props, EXMLPROP_FMT_ATTRIB, ctl->showAttribs );
1194         xmlprops_save_file( props );
1195         xmlprops_free( props );
1196 }
1197
1198 /*
1199  * ============================================================================
1200  * End of Source.
1201  * ============================================================================
1202  */
1203
1204