Fix CID 1491093: attrib leaked if attvalue is null
[claws.git] / src / exporthtml.c
blob4fc48db4d814c7d397f26b20c4c1e3d571a6bc0a
1 /*
2 * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3 * Copyright (C) 2002-2024 the Claws Mail team and Match Grun
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.
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.
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/>.
20 * Export address book to HTML file.
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #include "claws-features.h"
26 #endif
28 #ifdef USE_PTHREAD
29 #include <pthread.h>
30 #endif
32 #include <sys/stat.h>
33 #include <errno.h>
34 #include <time.h>
35 #include <string.h>
36 #include <glib.h>
37 #include <glib/gi18n.h>
39 #include "mgutils.h"
40 #include "utils.h"
41 #include "exporthtml.h"
42 #include "xmlprops.h"
43 #include "file-utils.h"
45 #define DFL_DIR_CLAWS_OUT "claws-mail-out"
46 #define DFL_FILE_CLAWS_OUT "addressbook.html"
48 #define FMT_BUFSIZE 2048
49 #define SC_HTML_SPACE "&nbsp;"
50 #define BORDER_SIZE 2
51 #define CELL_PADDING 2
52 #define CELL_SPACING 2
53 #define CHAR_ENCODING "UTF-8"
55 /* Stylesheet names */
56 #define FILENAME_NONE ""
57 #define FILENAME_DEFAULT "claws-mail.css"
58 #define FILENAME_FULL "full.css"
59 #define FILENAME_CUSTOM "custom.css"
60 #define FILENAME_CUSTOM2 "custom2.css"
61 #define FILENAME_CUSTOM3 "custom3.css"
62 #define FILENAME_CUSTOM4 "custom4.css"
64 /* Settings - properties */
65 #define EXML_PROPFILE_NAME "exporthtml.xml"
66 #define EXMLPROP_DIRECTORY "directory"
67 #define EXMLPROP_FILE "file"
68 #define EXMLPROP_STYLESHEET "stylesheet"
69 #define EXMLPROP_FMT_NAME "format-full-name"
70 #define EXMLPROP_FMT_EMAIL "format-email-links"
71 #define EXMLPROP_FMT_ATTRIB "format-attributes"
72 #define EXMLPROP_BANDING "color-banding"
73 #define EXMLPROP_VALUE_YES "y"
74 #define EXMLPROP_VALUE_NO "n"
76 static gchar *_idTagRowEven_ = "tab-row0";
77 static gchar *_idTagRowOdd_ = "tab-row1";
80 * Header entry.
82 typedef struct _StylesheetEntry StylesheetEntry;
83 struct _StylesheetEntry {
84 gchar *fileName;
85 gint id;
86 gboolean dflValue;
90 * Build stylesheet entry.
91 * Enter: ctl Export control data.
92 * file Filename.
93 * id File id.
94 * dfl Default flag.
96 static void exporthtml_build_entry(
97 ExportHtmlCtl *ctl, const gchar *file, const gint id,
98 const gboolean dfl )
100 StylesheetEntry *entry;
102 entry = g_new0( StylesheetEntry, 1 );
103 entry->fileName = g_strdup( file );
104 entry->id = id;
105 entry->dflValue = dfl;
106 ctl->listStyle = g_list_append( ctl->listStyle, entry );
110 * Free up object by releasing internal memory.
111 * Enter: ctl Export control data.
113 ExportHtmlCtl *exporthtml_create( void ) {
114 ExportHtmlCtl *ctl = g_new0( ExportHtmlCtl, 1 );
116 ctl->path = NULL;
117 ctl->dirOutput = NULL;
118 ctl->fileHtml = NULL;
119 ctl->encoding = g_strconcat(CHAR_ENCODING, NULL);
120 ctl->stylesheet = EXPORT_HTML_ID_NONE;
121 ctl->nameFormat = EXPORT_HTML_FIRST_LAST;
122 ctl->banding = FALSE;
123 ctl->linkEMail = FALSE;
124 ctl->showAttribs = FALSE;
125 ctl->retVal = MGU_SUCCESS;
126 ctl->listStyle = NULL;
127 ctl->rcCreate = 0;
128 ctl->settingsFile = g_strconcat(
129 get_rc_dir(), G_DIR_SEPARATOR_S, EXML_PROPFILE_NAME, NULL );
131 /* Build stylesheet list */
132 exporthtml_build_entry(
133 ctl, FILENAME_NONE, EXPORT_HTML_ID_NONE, FALSE );
134 exporthtml_build_entry(
135 ctl, FILENAME_DEFAULT, EXPORT_HTML_ID_DEFAULT, TRUE );
136 exporthtml_build_entry(
137 ctl, FILENAME_FULL, EXPORT_HTML_ID_FULL, FALSE );
138 exporthtml_build_entry(
139 ctl, FILENAME_CUSTOM, EXPORT_HTML_ID_CUSTOM, FALSE );
140 exporthtml_build_entry(
141 ctl, FILENAME_CUSTOM2, EXPORT_HTML_ID_CUSTOM2, FALSE );
142 exporthtml_build_entry(
143 ctl, FILENAME_CUSTOM3, EXPORT_HTML_ID_CUSTOM3, FALSE );
144 exporthtml_build_entry(
145 ctl, FILENAME_CUSTOM4, EXPORT_HTML_ID_CUSTOM4, FALSE );
147 return ctl;
151 * Free up object by releasing internal memory.
152 * Enter: ctl Export control data.
154 void exporthtml_free( ExportHtmlCtl *ctl ) {
155 GList *node;
156 StylesheetEntry *entry;
158 cm_return_if_fail( ctl != NULL );
160 /* Free stylesheet list */
161 node = ctl->listStyle;
162 while( node ) {
163 entry = ( StylesheetEntry * ) node->data;
164 g_free( entry->fileName );
165 entry->fileName = NULL;
166 entry->id = 0;
167 entry->dflValue = FALSE;
168 g_free( entry );
169 node->data = NULL;
170 node = g_list_next( node );
172 g_list_free( ctl->listStyle );
173 ctl->listStyle = NULL;
175 g_free( ctl->path );
176 g_free( ctl->fileHtml );
177 g_free( ctl->encoding );
178 g_free( ctl->dirOutput );
179 g_free( ctl->settingsFile );
181 /* Clear pointers */
182 ctl->path = NULL;
183 ctl->dirOutput = NULL;
184 ctl->fileHtml = NULL;
185 ctl->encoding = NULL;
186 ctl->stylesheet = EXPORT_HTML_ID_NONE;
187 ctl->nameFormat = EXPORT_HTML_FIRST_LAST;
188 ctl->banding = FALSE;
189 ctl->linkEMail = FALSE;
190 ctl->showAttribs = FALSE;
191 ctl->retVal = MGU_SUCCESS;
192 ctl->rcCreate = 0;
194 /* Now release object */
195 g_free( ctl );
199 * Find style entry.
200 * Enter: ctl Export control data.
201 * Return: Stylesheet object, or NULL if nothing found. If a default entry is
202 * found in list, it will be returned.
204 static StylesheetEntry *exporthtml_find_stylesheet( ExportHtmlCtl *ctl ) {
205 StylesheetEntry *retVal = NULL;
206 StylesheetEntry *entry;
207 GList *node;
209 node = ctl->listStyle;
210 while( node ) {
211 entry = ( StylesheetEntry * ) node->data;
212 if( entry->id == ctl->stylesheet ) return entry;
213 if( entry->dflValue ) retVal = entry;
214 node = g_list_next( node );
216 return retVal;
219 void exporthtml_set_stylesheet( ExportHtmlCtl *ctl, const gint value ) {
220 cm_return_if_fail( ctl != NULL );
221 ctl->stylesheet = value;
223 void exporthtml_set_name_format( ExportHtmlCtl *ctl, const gint value ) {
224 cm_return_if_fail( ctl != NULL );
225 ctl->nameFormat = value;
227 void exporthtml_set_banding( ExportHtmlCtl *ctl, const gboolean value ) {
228 cm_return_if_fail( ctl != NULL );
229 ctl->banding = value;
231 void exporthtml_set_link_email( ExportHtmlCtl *ctl, const gboolean value ) {
232 cm_return_if_fail( ctl != NULL );
233 ctl->linkEMail = value;
235 void exporthtml_set_attributes( ExportHtmlCtl *ctl, const gboolean value ) {
236 cm_return_if_fail( ctl != NULL );
237 ctl->showAttribs = value;
241 * Create default CSS file.
242 * Enter: fileSpec File to create.
243 * Return: Status code.
245 static gint exporthtml_create_css_dfl( const gchar *fileSpec ) {
246 FILE *cssFile;
248 cssFile = claws_fopen( fileSpec, "rb" );
249 if( cssFile ) {
250 claws_fclose( cssFile );
251 return MGU_SUCCESS;
253 cssFile = claws_fopen( fileSpec, "wb" );
254 if( ! cssFile ) {
255 return MGU_OPEN_FILE;
258 fprintf( cssFile, "body {\n\tbackground: #ffffe0;\n" );
259 fprintf( cssFile, "\tfont-family: lucida, helvetica, sans-serif;\n" );
260 fprintf( cssFile, "\tfont-size: 10pt;\n" );
261 fprintf( cssFile, "}\n" );
262 fprintf( cssFile, "h1 {\n" );
263 fprintf( cssFile, "\tcolor: #000000;\n" );
264 fprintf( cssFile, "\ttext-align: center;\n" );
265 fprintf( cssFile, "}\n" );
266 fprintf( cssFile, "th {\n" );
267 fprintf( cssFile, "\tfont-size: 10pt;\n" );
268 fprintf( cssFile, "}\n" );
269 fprintf( cssFile, "td {\n" );
270 fprintf( cssFile, "\tfont-size: 10pt;\n" );
271 fprintf( cssFile, "}\n" );
272 fprintf( cssFile, ".fmt-folder {\n" );
273 fprintf( cssFile, "\tcolor: #0000ff;\n" );
274 fprintf( cssFile, "\tfont-size: 18pt;\n" );
275 fprintf( cssFile, "\tfont-weight: bold;\n" );
276 fprintf( cssFile, "}\n" );
277 fprintf( cssFile, ".tab-head {\n" );
278 fprintf( cssFile, "\tbackground: #80c0f0;\n" );
279 fprintf( cssFile, "}\n" );
280 fprintf( cssFile, ".tab-dn {\n" );
281 fprintf( cssFile, "}\n" );
282 fprintf( cssFile, ".tab-addr {\n" );
283 fprintf( cssFile, "\tfont-style: italic;\n" );
284 fprintf( cssFile, "}\n" );
285 fprintf( cssFile, ".tab-email {\n" );
286 fprintf( cssFile, "\tfont-weight: bold;\n" );
287 fprintf( cssFile, "\tfont-style: italic;\n" );
288 fprintf( cssFile, "}\n" );
289 fprintf( cssFile, ".tab-fn {\n" );
290 fprintf( cssFile, "}\n" );
291 fprintf( cssFile, ".tab-attr {\n" );
292 fprintf( cssFile, "}\n" );
294 claws_safe_fclose( cssFile );
295 return MGU_SUCCESS;
299 * Create full CSS file.
300 * Enter: fileSpec File to create.
301 * Return: Status code.
303 static gint exporthtml_create_css_full( const gchar *fileSpec ) {
304 FILE *cssFile;
306 cssFile = claws_fopen( fileSpec, "rb" );
307 if( cssFile ) {
308 claws_fclose( cssFile );
309 return MGU_SUCCESS;
311 cssFile = claws_fopen( fileSpec, "wb" );
312 if( ! cssFile ) {
313 return MGU_OPEN_FILE;
316 fprintf( cssFile, "body {\n\tbackground: #ffffe0;\n" );
317 fprintf( cssFile, "\tfont-family: lucida, helvetica, sans-serif;\n" );
318 fprintf( cssFile, "\tfont-size: 10pt;\n" );
319 fprintf( cssFile, "}\n" );
320 fprintf( cssFile, "h1 {\n" );
321 fprintf( cssFile, "\tcolor: #000000;\n" );
322 fprintf( cssFile, "\ttext-align: center;\n" );
323 fprintf( cssFile, "}\n" );
324 fprintf( cssFile, "th {\n" );
325 fprintf( cssFile, "\tfont-size: 10pt;\n" );
326 fprintf( cssFile, "}\n" );
327 fprintf( cssFile, "td {\n" );
328 fprintf( cssFile, "\tfont-size: 10pt;\n" );
329 fprintf( cssFile, "}\n" );
330 fprintf( cssFile, ".fmt-folder {\n" );
331 fprintf( cssFile, "\tcolor: #0000ff;\n" );
332 fprintf( cssFile, "\tfont-size: 18pt;\n" );
333 fprintf( cssFile, "\tfont-weight: bold;\n" );
334 fprintf( cssFile, "}\n" );
335 fprintf( cssFile, ".tab-head {\n" );
336 fprintf( cssFile, "\tbackground: #80c0f0;\n" );
337 fprintf( cssFile, "}\n" );
338 fprintf( cssFile, ".tab-row0 {\n" );
339 fprintf( cssFile, "\tbackground: #f0f0f0;\n" );
340 fprintf( cssFile, "}\n" );
341 fprintf( cssFile, ".tab-row1 {\n" );
342 fprintf( cssFile, "\tbackground: #d0d0d0;\n" );
343 fprintf( cssFile, "}\n" );
344 fprintf( cssFile, ".tab-dn {\n" );
345 fprintf( cssFile, "}\n" );
346 fprintf( cssFile, ".tab-addr {\n" );
347 fprintf( cssFile, "\tfont-style: italic;\n" );
348 fprintf( cssFile, "}\n" );
349 fprintf( cssFile, ".tab-email {\n" );
350 fprintf( cssFile, "\tfont-weight: bold;\n" );
351 fprintf( cssFile, "\tfont-style: italic;\n" );
352 fprintf( cssFile, "}\n" );
353 fprintf( cssFile, ".tab-fn {\n" );
354 fprintf( cssFile, "}\n" );
355 fprintf( cssFile, ".tab-attr {\n" );
356 fprintf( cssFile, "}\n" );
358 claws_safe_fclose( cssFile );
359 return MGU_SUCCESS;
363 * Create stylesheet files.
364 * Enter: ctl Export control data.
366 static void exporthtml_create_css_files( ExportHtmlCtl *ctl ) {
367 gchar *fileSpec;
368 GList *node;
370 node = ctl->listStyle;
371 while( node ) {
372 StylesheetEntry *entry = node->data;
373 node = g_list_next( node );
374 if( strlen( entry->fileName ) ) {
375 fileSpec = g_strconcat(
376 ctl->dirOutput, G_DIR_SEPARATOR_S,
377 entry->fileName, NULL );
378 if( entry->id == EXPORT_HTML_ID_DEFAULT ) {
379 exporthtml_create_css_dfl( fileSpec );
381 else if( entry->id != EXPORT_HTML_ID_NONE ) {
382 exporthtml_create_css_full( fileSpec );
384 g_free( fileSpec );
390 * Comparison using linked list elements.
392 static gint exporthtml_compare_name(
393 gconstpointer ptr1, gconstpointer ptr2 )
395 const AddrItemObject *item1 = ptr1;
396 const AddrItemObject *item2 = ptr2;
397 const gchar *name1 = NULL, *name2 = NULL;
398 if( item1 ) name1 = ADDRITEM_NAME( item1 );
399 if( item2 ) name2 = ADDRITEM_NAME( item2 );
400 if( ! name1 ) return ( name2 != NULL );
401 if( ! name2 ) return -1;
402 return g_utf8_collate( name1, name2 );
406 * Comparison using linked list elements.
408 static gint exporthtml_compare_email(
409 gconstpointer ptr1, gconstpointer ptr2 )
411 const ItemEMail *email1 = ptr1;
412 const ItemEMail *email2 = ptr2;
413 const gchar *name1 = NULL, *name2 = NULL;
414 if( email1 ) name1 = email1->address;
415 if( email2 ) name2 = email2->address;
416 if( ! name1 ) return ( name2 != NULL );
417 if( ! name2 ) return -1;
418 return g_utf8_collate( name1, name2 );
422 * Comparison using linked list elements.
424 static gint exporthtml_compare_attrib(
425 gconstpointer ptr1, gconstpointer ptr2 )
427 const UserAttribute *attr1 = ptr1;
428 const UserAttribute *attr2 = ptr2;
429 const gchar *name1 = NULL, *name2 = NULL;
430 if( attr1 ) name1 = attr1->name;
431 if( attr2 ) name2 = attr2->name;
432 if( ! name1 ) return ( name2 != NULL );
433 if( ! name2 ) return -1;
434 return g_utf8_collate( name1, name2 );
438 * Build sorted list of named items.
439 * Enter: list List of items to sorted.
440 * Return: Sorted list.
441 * Note: List should freed after use. Items referenced by list should not be
442 * freed since they are managed by the address cache.
444 static GList *exporthtml_sort_name( const GList *list ) {
445 const GList *node;
446 GList *sorted = NULL;
448 node = list;
449 while( node ) {
450 sorted = g_list_insert_sorted(
451 sorted, node->data, exporthtml_compare_name );
452 node = g_list_next( node );
454 return sorted;
458 * Build sorted list of email items.
459 * Enter: list List of E-Mail items to sorted.
460 * Return: Sorted list.
461 * Note: List should freed after use. Items referenced by list should not be
462 * freed since they are managed by the address cache.
464 static GList *exporthtml_sort_email( const GList *list ) {
465 const GList *node;
466 GList *sorted = NULL;
468 node = list;
469 while( node ) {
470 sorted = g_list_insert_sorted(
471 sorted, node->data, exporthtml_compare_email );
472 node = g_list_next( node );
474 return sorted;
478 * Build sorted list of attributes.
479 * Enter: list List of items to sorted.
480 * Return: Sorted list.
481 * Note: List should freed after use. Items referenced by list should not be
482 * freed since they are managed by the address cache.
484 static GList *exporthtml_sort_attrib( const GList *list ) {
485 const GList *node;
486 GList *sorted = NULL;
488 sorted = NULL;
489 node = list;
490 while( node ) {
491 sorted = g_list_insert_sorted(
492 sorted, node->data, exporthtml_compare_attrib );
493 node = g_list_next( node );
495 return sorted;
499 * Format a list of E-Mail addresses.
500 * Enter: ctl Export control data.
501 * stream Output stream.
502 * listEMail List of addresses.
503 * sortFlag Set to TRUE if address list should be sorted.
505 static void exporthtml_fmt_email(
506 ExportHtmlCtl *ctl, FILE *stream, const GList *listEMail,
507 gboolean sortFlag )
509 const GList *node;
510 GList *list;
511 gchar *name;
513 if( listEMail == NULL ) {
514 fprintf( stream, SC_HTML_SPACE );
515 return;
518 list = NULL;
519 if( sortFlag ) {
520 node = list = exporthtml_sort_email( listEMail );
522 else {
523 node = listEMail;
526 while( node ) {
527 ItemEMail *email = ( ItemEMail * ) node->data;
528 node = g_list_next( node );
530 name = ADDRITEM_NAME( email );
531 if( name ) {
532 fprintf( stream, "%s ", name );
534 if( ctl->linkEMail ) {
535 fprintf( stream, "<a href=\"mailto:%s\">",
536 email->address );
538 fprintf( stream, "<span class=\"tab-email\">" );
539 fprintf( stream, "%s", email->address );
540 fprintf( stream, "</span>" );
541 if( ctl->linkEMail ) {
542 fprintf( stream, "</a>" );
544 if( email->remarks ) {
545 if( strlen( email->remarks ) ) {
546 fprintf( stream, " (%s)", email->remarks );
549 fprintf( stream, "<br>\n" );
551 g_list_free( list );
555 * Format groups in an address book folder.
556 * Enter: ctl Export control data.
557 * stream Output stream.
558 * folder Folder.
559 * prevFlag If FALSE, list of persons were output.
560 * Return: TRUE if no groups were formatted.
562 static gboolean exporthtml_fmt_group(
563 ExportHtmlCtl *ctl, FILE *stream, const ItemFolder *folder,
564 gboolean prevFlag )
566 gboolean retVal, band;
567 GList *node, *list;
568 const gchar *tagName;
570 retVal = TRUE;
571 if( folder->listGroup == NULL ) return retVal;
573 /* Write separator */
574 if( ! prevFlag ) {
575 fprintf( stream, "<br>\n" );
578 /* Write table headers */
579 fprintf( stream, "<table" );
580 fprintf( stream, " border=\"%d\"", BORDER_SIZE );
581 fprintf( stream, " cellpadding=\"%d\"", CELL_PADDING );
582 fprintf( stream, " cellspacing=\"%d\"", CELL_SPACING );
583 fprintf( stream, ">\n" );
585 fprintf( stream, "<tr class=\"tab-head\">\n" );
586 fprintf( stream, " <th width=\"200\">" );
587 fprintf( stream, "%s", _( "Group Name" ) );
588 fprintf( stream, "</th>\n" );
589 fprintf( stream, " <th width=\"300\">" );
590 fprintf( stream, "%s", _( "Email Address" ) );
591 fprintf( stream, "</th>\n" );
592 fprintf( stream, "</tr>\n" );
593 list = exporthtml_sort_name( folder->listGroup );
595 band = FALSE;
596 node = list;
597 while( node ) {
598 AddrItemObject *aio = node->data;
599 if( aio && aio->type == ITEMTYPE_GROUP ) {
600 ItemGroup *group = ( ItemGroup * ) aio;
602 fprintf( stream, "<tr valign=\"top\"" );
603 if( ctl->banding ) {
604 if( band ) {
605 tagName = _idTagRowOdd_;
607 else {
608 tagName = _idTagRowEven_;
610 fprintf( stream, " class=\"%s\"", tagName );
611 band = ! band;
613 fprintf( stream, "\">\n" );
615 fprintf( stream, " <td class=\"tab-dn\">" );
616 fprintf( stream, "%s", ADDRITEM_NAME( group ) );
617 fprintf( stream, "</td>\n" );
618 fprintf( stream, " <td class=\"tab-addr\">" );
619 exporthtml_fmt_email( ctl, stream, group->listEMail, TRUE );
620 fprintf( stream, "</td>\n" );
621 fprintf( stream, "</tr>\n" );
622 retVal = FALSE;
624 node = g_list_next( node );
627 g_list_free( list );
628 fprintf( stream, "</table>\n" );
629 return retVal;
633 * Format a list of E-Mail addresses.
634 * Enter: ctl Export control data.
635 * stream Output stream.
636 * listAttr List of attributes.
638 static void exporthtml_fmt_attribs(
639 ExportHtmlCtl *ctl, FILE *stream, const GList *listAttr )
641 const GList *node;
642 GList *list;
644 if( listAttr == NULL ) {
645 fprintf( stream, SC_HTML_SPACE );
646 return;
649 fprintf( stream, "<table border=\"0\">\n" );
650 node = list = exporthtml_sort_attrib( listAttr );
651 while( node ) {
652 UserAttribute *attr = ( UserAttribute * ) node->data;
653 node = g_list_next( node );
654 fprintf( stream, "<tr valign=\"top\">" );
655 fprintf( stream, "<td align=\"right\">%s:</td>", attr->name );
656 fprintf( stream, "<td>%s</td>", attr->value );
657 fprintf( stream, "</tr>\n" );
660 g_list_free( list );
661 fprintf( stream, "</table>" );
665 * Format full name.
666 * Enter: ctl Export control data.
667 * buf Output buffer.
668 * person Person to format.
670 static void exporthtml_fmt_fullname(
671 ExportHtmlCtl *ctl, gchar *buf, const ItemPerson *person )
673 gboolean flag;
675 if( ctl->nameFormat == EXPORT_HTML_LAST_FIRST ) {
676 flag = FALSE;
677 if( person->lastName ) {
678 if( *person->lastName ) {
679 strcat( buf, " " );
680 strcat( buf, person->lastName );
681 flag = TRUE;
684 if( person->firstName ) {
685 if( *person->firstName ) {
686 if( flag ) {
687 strcat( buf, ", " );
689 strcat( buf, person->firstName );
693 else {
694 if( person->firstName ) {
695 if( *person->firstName ) {
696 strcat( buf, person->firstName );
699 if( person->lastName ) {
700 if( *person->lastName ) {
701 strcat( buf, " " );
702 strcat( buf, person->lastName );
706 g_strstrip( buf );
708 flag = FALSE;
709 if( *buf ) flag = TRUE;
710 if( person->nickName ) {
711 if( strlen( person->nickName ) ) {
712 if( flag ) {
713 strcat( buf, " (" );
715 strcat( buf, person->nickName );
716 if( flag ) {
717 strcat( buf, ")" );
721 g_strstrip( buf );
725 * Format persons in an address book folder.
726 * Enter: ctl Export control data.
727 * stream Output stream.
728 * folder Folder.
729 * Return: TRUE if no persons were formatted.
731 static gboolean exporthtml_fmt_person(
732 ExportHtmlCtl *ctl, FILE *stream, const ItemFolder *folder )
734 gboolean retVal, band;
735 GList *node, *list;
736 gchar buf[ FMT_BUFSIZE ];
737 const gchar *tagName;
739 retVal = TRUE;
740 if( folder->listPerson == NULL ) return retVal;
742 /* Write table headers */
743 fprintf( stream, "<table" );
744 fprintf( stream, " border=\"%d\"", BORDER_SIZE );
745 fprintf( stream, " cellpadding=\"%d\"", CELL_PADDING );
746 fprintf( stream, " cellspacing=\"%d\"", CELL_SPACING );
747 fprintf( stream, ">\n" );
749 fprintf( stream, "<tr class=\"tab-head\">\n" );
750 fprintf( stream, " <th width=\"200\">" );
751 fprintf( stream, "%s", _( "Display Name" ) );
752 fprintf( stream, "</th>\n" );
753 fprintf( stream, " <th width=\"300\">" );
754 fprintf( stream, "%s", _( "Email Address" ) );
755 fprintf( stream, "</th>\n" );
756 fprintf( stream, " <th width=\"200\">" );
757 fprintf( stream, "%s", _( "Full Name" ) );
758 fprintf( stream, "</th>\n" );
759 if( ctl->showAttribs ) {
760 fprintf( stream, " <th width=\"250\">" );
761 fprintf( stream, "%s", _( "Attributes" ) );
762 fprintf( stream, "</th>\n" );
764 fprintf( stream, "</tr>\n" );
766 band = FALSE;
767 node = list = exporthtml_sort_name( folder->listPerson );
768 while( node ) {
769 AddrItemObject *aio = node->data;
770 if( aio && aio->type == ITEMTYPE_PERSON ) {
771 ItemPerson *person = ( ItemPerson * ) aio;
773 /* Format first/last/nick name */
774 *buf = '\0';
775 exporthtml_fmt_fullname( ctl, buf,person );
777 fprintf( stream, "<tr valign=\"top\"" );
778 if( ctl->banding ) {
779 if( band ) {
780 tagName = _idTagRowOdd_;
782 else {
783 tagName = _idTagRowEven_;
785 fprintf( stream, " class=\"%s\"", tagName );
786 band = ! band;
788 fprintf( stream, ">\n" );
790 fprintf( stream, " <td class=\"tab-dn\">" );
791 fprintf( stream, "%s", ADDRITEM_NAME( person ) );
792 fprintf( stream, "</td>\n" );
794 fprintf( stream, " <td class=\"tab-addr\">" );
795 exporthtml_fmt_email( ctl, stream, person->listEMail, FALSE );
796 fprintf( stream, "</td>\n" );
798 fprintf( stream, " <td class=\"tab-fn\">" );
799 if( *buf ) {
800 fprintf( stream, "%s", buf );
802 else {
803 fprintf( stream, "%s", SC_HTML_SPACE );
805 fprintf( stream, "</td>\n" );
807 if( ctl->showAttribs ) {
808 fprintf( stream, " <td class=\"tab-attr\">" );
809 exporthtml_fmt_attribs(
810 ctl, stream, person->listAttrib );
811 fprintf( stream, "</td>\n" );
813 fprintf( stream, "</tr>\n" );
815 retVal = FALSE;
817 node = g_list_next( node );
820 g_list_free( list );
821 fprintf( stream, "</table>\n" );
822 return retVal;
826 * Format folder heirarchy.
827 * Enter: stream Output stream.
828 * list Heirarchy list.
830 static void exporthtml_fmt_folderhead( FILE *stream, const GList *list ) {
831 const GList *node;
832 gboolean flag;
833 gchar *name;
835 flag = FALSE;
836 node = list;
837 while( node ) {
838 AddrItemObject *aio = node->data;
839 if( aio && aio->type == ITEMTYPE_FOLDER ) {
840 ItemFolder *folder = ( ItemFolder * ) aio;
842 name = ADDRITEM_NAME( folder );
843 if( name ) {
844 if( flag ) {
845 fprintf( stream, "&nbsp;&gt;&nbsp;" );
847 fprintf( stream, "%s", name );
848 flag = TRUE;
851 node = g_list_next( node );
856 * Format an address book folder.
857 * Enter: ctl Export control data.
858 * stream Output stream.
859 * folder Folder.
861 static void exporthtml_fmt_folder(
862 ExportHtmlCtl *ctl, FILE *stream, const ItemFolder *folder )
864 const GList *node;
865 GList *listHeir, *list;
866 const gchar *name;
867 gboolean ret1;
869 name = ADDRITEM_NAME( folder );
870 if( name ) {
871 listHeir = addritem_folder_path( folder, TRUE );
872 if( listHeir ) {
873 fprintf( stream, "<p class=\"fmt-folder\">" );
874 fprintf( stream, "%s: ", _( "Folder" ) );
875 exporthtml_fmt_folderhead( stream, listHeir );
876 fprintf( stream, "</p>\n" );
877 g_list_free( listHeir );
881 ret1 = exporthtml_fmt_person( ctl, stream, folder );
882 exporthtml_fmt_group( ctl, stream, folder, ret1 );
884 node = list = exporthtml_sort_name( folder->listFolder );
885 while( node ) {
886 AddrItemObject *aio = node->data;
887 if( aio && aio->type == ITEMTYPE_FOLDER ) {
888 ItemFolder *subFolder = ( ItemFolder * ) aio;
889 exporthtml_fmt_folder( ctl, stream, subFolder );
891 node = g_list_next( node );
893 if( list ) {
894 g_list_free( list );
899 * Format header block.
900 * Enter: ctl Export control data.
901 * stream Output stream.
902 * title Page title.
904 static void exporthtml_fmt_header(
905 ExportHtmlCtl *ctl, FILE *stream, gchar *title )
907 StylesheetEntry *entry;
909 entry = exporthtml_find_stylesheet( ctl );
911 fprintf( stream,
912 "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n" );
913 fprintf( stream,
914 "\"http://www.w3.org/TR/html4/loose.dtd\">\n" );
915 fprintf( stream, "<html>\n" );
916 fprintf( stream, "<head>\n" );
918 if( ctl->encoding && strlen( ctl->encoding ) > 0 ) {
919 fprintf( stream, "<meta " );
920 fprintf( stream, "http-equiv=\"Content-Type\" " );
921 fprintf( stream, "content=\"text/html; charset=%s\">\n",
922 ctl->encoding );
925 fprintf( stream, "<title>%s</title>\n", title );
927 if( entry != NULL ) {
928 if( entry->fileName && strlen( entry->fileName ) > 0 ) {
929 fprintf( stream, "<link " );
930 fprintf( stream, "rel=\"stylesheet\" " );
931 fprintf( stream, "type=\"text/css\" " );
932 fprintf( stream, "href=\"%s\" >\n", entry->fileName );
935 fprintf( stream, "</head>\n" );
939 * ============================================================================
940 * Export address book to HTML file.
941 * Enter: ctl Export control data.
942 * cache Address book/data source cache.
943 * Return: Status.
944 * ============================================================================
946 void exporthtml_process(
947 ExportHtmlCtl *ctl, AddressCache *cache )
949 ItemFolder *rootFolder;
950 FILE *htmlFile;
951 time_t tt;
952 gchar *dsName;
953 static gchar *title;
954 gchar buf[512];
956 htmlFile = claws_fopen( ctl->path, "wb" );
957 if( ! htmlFile ) {
958 /* Cannot open file */
959 g_print( "Cannot open file for write\n" );
960 ctl->retVal = MGU_OPEN_FILE;
961 return;
964 title = _( "Claws Mail Address Book" );
965 rootFolder = cache->rootFolder;
966 dsName = cache->name;
968 exporthtml_fmt_header( ctl, htmlFile, title );
970 fprintf( htmlFile, "<body>\n" );
971 fprintf( htmlFile, "<h1>%s</h1>\n", title );
973 fprintf( htmlFile, "<p class=\"fmt-folder\">" );
974 fprintf( htmlFile, "%s: ", _( "Address Book" ) );
975 fprintf( htmlFile, "%s", dsName );
976 fprintf( htmlFile, "</p>\n" );
978 exporthtml_fmt_folder( ctl, htmlFile, rootFolder );
980 tt = time( NULL );
981 fprintf( htmlFile, "<p>%s</p>\n", ctime_r( &tt, buf ) );
982 fprintf( htmlFile, "<hr width=\"100%%\">\n" );
984 fprintf( htmlFile, "</body>\n" );
985 fprintf( htmlFile, "</html>\n" );
987 claws_safe_fclose( htmlFile );
988 ctl->retVal = MGU_SUCCESS;
990 /* Create stylesheet files */
991 exporthtml_create_css_files( ctl );
996 * Build full export file specification.
997 * Enter: ctl Export control data.
999 static void exporthtml_build_filespec( ExportHtmlCtl *ctl ) {
1000 gchar *fileSpec;
1002 fileSpec = g_strconcat(
1003 ctl->dirOutput, G_DIR_SEPARATOR_S, ctl->fileHtml, NULL );
1004 ctl->path = mgu_replace_string( ctl->path, fileSpec );
1005 g_free( fileSpec );
1009 * ============================================================================
1010 * Parse directory and filename from full export file specification.
1011 * Enter: ctl Export control data.
1012 * fileSpec File spec.
1013 * ============================================================================
1015 void exporthtml_parse_filespec( ExportHtmlCtl *ctl, gchar *fileSpec ) {
1016 gchar *t;
1017 gchar *base = g_path_get_basename(fileSpec);
1019 ctl->fileHtml =
1020 mgu_replace_string( ctl->fileHtml, base );
1021 g_free(base);
1022 t = g_path_get_dirname( fileSpec );
1023 ctl->dirOutput = mgu_replace_string( ctl->dirOutput, t );
1024 g_free( t );
1025 ctl->path = mgu_replace_string( ctl->path, fileSpec );
1029 * ============================================================================
1030 * Create output directory.
1031 * Enter: ctl Export control data.
1032 * Return: TRUE if directory created.
1033 * ============================================================================
1035 gboolean exporthtml_create_dir( ExportHtmlCtl *ctl ) {
1036 gboolean retVal = FALSE;
1038 ctl->rcCreate = 0;
1039 if( g_mkdir( ctl->dirOutput, S_IRWXU ) == 0 ) {
1040 retVal = TRUE;
1042 else {
1043 ctl->rcCreate = errno;
1045 return retVal;
1049 * ============================================================================
1050 * Retrieve create directory error message.
1051 * Enter: ctl Export control data.
1052 * Return: Message.
1053 * ============================================================================
1055 gchar *exporthtml_get_create_msg( ExportHtmlCtl *ctl ) {
1056 gchar *msg;
1058 if( ctl->rcCreate == EEXIST ) {
1059 msg = _( "Name already exists but is not a directory." );
1061 else if( ctl->rcCreate == EACCES ) {
1062 msg = _( "No permissions to create directory." );
1064 else if( ctl->rcCreate == ENAMETOOLONG ) {
1065 msg = _( "Name is too long." );
1067 else {
1068 msg = _( "Not specified." );
1070 return msg;
1074 * Set default values.
1075 * Enter: ctl Export control data.
1077 static void exporthtml_default_values( ExportHtmlCtl *ctl ) {
1078 gchar *str;
1080 str = g_strconcat(
1081 get_home_dir(), G_DIR_SEPARATOR_S,
1082 DFL_DIR_CLAWS_OUT, NULL );
1084 ctl->dirOutput = mgu_replace_string( ctl->dirOutput, str );
1085 g_free( str );
1087 ctl->fileHtml =
1088 mgu_replace_string( ctl->fileHtml, DFL_FILE_CLAWS_OUT );
1089 ctl->encoding = NULL;
1090 ctl->stylesheet = EXPORT_HTML_ID_DEFAULT;
1091 ctl->nameFormat = EXPORT_HTML_FIRST_LAST;
1092 ctl->banding = TRUE;
1093 ctl->linkEMail = TRUE;
1094 ctl->showAttribs = TRUE;
1095 ctl->retVal = MGU_SUCCESS;
1099 * ============================================================================
1100 * Load settings from XML properties file.
1101 * Enter: ctl Export control data.
1102 * ============================================================================
1104 void exporthtml_load_settings( ExportHtmlCtl *ctl ) {
1105 XmlProperty *props;
1106 gint rc;
1107 gchar buf[256];
1109 *buf = '\0';
1110 props = xmlprops_create();
1111 xmlprops_set_path( props, ctl->settingsFile );
1112 rc = xmlprops_load_file( props );
1113 if( rc == 0 ) {
1114 /* Read settings */
1115 xmlprops_get_property_s( props, EXMLPROP_DIRECTORY, buf );
1116 ctl->dirOutput = mgu_replace_string( ctl->dirOutput, buf );
1118 xmlprops_get_property_s( props, EXMLPROP_FILE, buf );
1119 ctl->fileHtml = mgu_replace_string( ctl->fileHtml, buf );
1121 ctl->stylesheet =
1122 xmlprops_get_property_i( props, EXMLPROP_STYLESHEET );
1123 ctl->nameFormat =
1124 xmlprops_get_property_i( props, EXMLPROP_FMT_NAME );
1125 ctl->banding =
1126 xmlprops_get_property_b( props, EXMLPROP_BANDING );
1127 ctl->linkEMail =
1128 xmlprops_get_property_b( props, EXMLPROP_FMT_EMAIL );
1129 ctl->showAttribs =
1130 xmlprops_get_property_b( props, EXMLPROP_FMT_ATTRIB );
1132 else {
1133 /* Set default values */
1134 exporthtml_default_values( ctl );
1136 exporthtml_build_filespec( ctl );
1137 /* exporthtml_print( ctl, stdout ); */
1139 xmlprops_free( props );
1143 * ============================================================================
1144 * Save settings to XML properties file.
1145 * Enter: ctl Export control data.
1146 * ============================================================================
1148 void exporthtml_save_settings( ExportHtmlCtl *ctl ) {
1149 XmlProperty *props;
1151 props = xmlprops_create();
1152 xmlprops_set_path( props, ctl->settingsFile );
1154 xmlprops_set_property( props, EXMLPROP_DIRECTORY, ctl->dirOutput );
1155 xmlprops_set_property( props, EXMLPROP_FILE, ctl->fileHtml );
1156 xmlprops_set_property_i( props, EXMLPROP_STYLESHEET, ctl->stylesheet );
1157 xmlprops_set_property_i( props, EXMLPROP_FMT_NAME, ctl->nameFormat );
1158 xmlprops_set_property_b( props, EXMLPROP_BANDING, ctl->banding );
1159 xmlprops_set_property_b( props, EXMLPROP_FMT_EMAIL, ctl->linkEMail );
1160 xmlprops_set_property_b( props, EXMLPROP_FMT_ATTRIB, ctl->showAttribs );
1161 if (xmlprops_save_file( props ) != MGU_SUCCESS)
1162 g_warning("can't save settings");
1163 xmlprops_free( props );
1167 * ============================================================================
1168 * End of Source.
1169 * ============================================================================