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