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