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