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