Remove wrong initialization from max_message_size
[claws.git] / src / exporthtml.c
blobb2ea892688ff7d0e2081a08f64545876b9508146
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/>.
21 * Export address book to HTML file.
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #include "claws-features.h"
27 #endif
29 #ifdef USE_PTHREAD
30 #include <pthread.h>
31 #endif
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>
41 #include "mgutils.h"
42 #include "utils.h"
43 #include "exporthtml.h"
44 #include "xmlprops.h"
45 #include "file-utils.h"
47 #define DFL_DIR_CLAWS_OUT "claws-mail-out"
48 #define DFL_FILE_CLAWS_OUT "addressbook.html"
50 #define FMT_BUFSIZE 2048
51 #define SC_HTML_SPACE "&nbsp;"
52 #define BORDER_SIZE 2
53 #define CELL_PADDING 2
54 #define CELL_SPACING 2
55 #define CHAR_ENCODING "UTF-8"
57 /* Stylesheet names */
58 #define FILENAME_NONE ""
59 #define FILENAME_DEFAULT "claws-mail.css"
60 #define FILENAME_FULL "full.css"
61 #define FILENAME_CUSTOM "custom.css"
62 #define FILENAME_CUSTOM2 "custom2.css"
63 #define FILENAME_CUSTOM3 "custom3.css"
64 #define FILENAME_CUSTOM4 "custom4.css"
66 /* Settings - properties */
67 #define EXML_PROPFILE_NAME "exporthtml.xml"
68 #define EXMLPROP_DIRECTORY "directory"
69 #define EXMLPROP_FILE "file"
70 #define EXMLPROP_STYLESHEET "stylesheet"
71 #define EXMLPROP_FMT_NAME "format-full-name"
72 #define EXMLPROP_FMT_EMAIL "format-email-links"
73 #define EXMLPROP_FMT_ATTRIB "format-attributes"
74 #define EXMLPROP_BANDING "color-banding"
75 #define EXMLPROP_VALUE_YES "y"
76 #define EXMLPROP_VALUE_NO "n"
78 static gchar *_idTagRowEven_ = "tab-row0";
79 static gchar *_idTagRowOdd_ = "tab-row1";
82 * Header entry.
84 typedef struct _StylesheetEntry StylesheetEntry;
85 struct _StylesheetEntry {
86 gchar *fileName;
87 gint id;
88 gboolean dflValue;
92 * Build stylesheet entry.
93 * Enter: ctl Export control data.
94 * file Filename.
95 * id File id.
96 * dfl Default flag.
98 static void exporthtml_build_entry(
99 ExportHtmlCtl *ctl, const gchar *file, const gint id,
100 const gboolean dfl )
102 StylesheetEntry *entry;
104 entry = g_new0( StylesheetEntry, 1 );
105 entry->fileName = g_strdup( file );
106 entry->id = id;
107 entry->dflValue = dfl;
108 ctl->listStyle = g_list_append( ctl->listStyle, entry );
112 * Free up object by releasing internal memory.
113 * Enter: ctl Export control data.
115 ExportHtmlCtl *exporthtml_create( void ) {
116 ExportHtmlCtl *ctl = g_new0( ExportHtmlCtl, 1 );
118 ctl->path = NULL;
119 ctl->dirOutput = NULL;
120 ctl->fileHtml = NULL;
121 ctl->encoding = g_strconcat(CHAR_ENCODING, NULL);
122 ctl->stylesheet = EXPORT_HTML_ID_NONE;
123 ctl->nameFormat = EXPORT_HTML_FIRST_LAST;
124 ctl->banding = FALSE;
125 ctl->linkEMail = FALSE;
126 ctl->showAttribs = FALSE;
127 ctl->retVal = MGU_SUCCESS;
128 ctl->listStyle = NULL;
129 ctl->rcCreate = 0;
130 ctl->settingsFile = g_strconcat(
131 get_rc_dir(), G_DIR_SEPARATOR_S, EXML_PROPFILE_NAME, NULL );
133 /* Build stylesheet list */
134 exporthtml_build_entry(
135 ctl, FILENAME_NONE, EXPORT_HTML_ID_NONE, FALSE );
136 exporthtml_build_entry(
137 ctl, FILENAME_DEFAULT, EXPORT_HTML_ID_DEFAULT, TRUE );
138 exporthtml_build_entry(
139 ctl, FILENAME_FULL, EXPORT_HTML_ID_FULL, FALSE );
140 exporthtml_build_entry(
141 ctl, FILENAME_CUSTOM, EXPORT_HTML_ID_CUSTOM, FALSE );
142 exporthtml_build_entry(
143 ctl, FILENAME_CUSTOM2, EXPORT_HTML_ID_CUSTOM2, FALSE );
144 exporthtml_build_entry(
145 ctl, FILENAME_CUSTOM3, EXPORT_HTML_ID_CUSTOM3, FALSE );
146 exporthtml_build_entry(
147 ctl, FILENAME_CUSTOM4, EXPORT_HTML_ID_CUSTOM4, FALSE );
149 return ctl;
153 * Free up object by releasing internal memory.
154 * Enter: ctl Export control data.
156 void exporthtml_free( ExportHtmlCtl *ctl ) {
157 GList *node;
158 StylesheetEntry *entry;
160 cm_return_if_fail( ctl != NULL );
162 /* Free stylesheet list */
163 node = ctl->listStyle;
164 while( node ) {
165 entry = ( StylesheetEntry * ) node->data;
166 g_free( entry->fileName );
167 entry->fileName = NULL;
168 entry->id = 0;
169 entry->dflValue = FALSE;
170 g_free( entry );
171 node->data = NULL;
172 node = g_list_next( node );
174 g_list_free( ctl->listStyle );
175 ctl->listStyle = NULL;
177 g_free( ctl->path );
178 g_free( ctl->fileHtml );
179 g_free( ctl->encoding );
180 g_free( ctl->dirOutput );
181 g_free( ctl->settingsFile );
183 /* Clear pointers */
184 ctl->path = NULL;
185 ctl->dirOutput = NULL;
186 ctl->fileHtml = NULL;
187 ctl->encoding = NULL;
188 ctl->stylesheet = EXPORT_HTML_ID_NONE;
189 ctl->nameFormat = EXPORT_HTML_FIRST_LAST;
190 ctl->banding = FALSE;
191 ctl->linkEMail = FALSE;
192 ctl->showAttribs = FALSE;
193 ctl->retVal = MGU_SUCCESS;
194 ctl->rcCreate = 0;
196 /* Now release object */
197 g_free( ctl );
201 * Find style entry.
202 * Enter: ctl Export control data.
203 * Return: Stylesheet object, or NULL if nothing found. If a default entry is
204 * found in list, it will be returned.
206 static StylesheetEntry *exporthtml_find_stylesheet( ExportHtmlCtl *ctl ) {
207 StylesheetEntry *retVal = NULL;
208 StylesheetEntry *entry;
209 GList *node;
211 node = ctl->listStyle;
212 while( node ) {
213 entry = ( StylesheetEntry * ) node->data;
214 if( entry->id == ctl->stylesheet ) return entry;
215 if( entry->dflValue ) retVal = entry;
216 node = g_list_next( node );
218 return retVal;
221 void exporthtml_set_stylesheet( ExportHtmlCtl *ctl, const gint value ) {
222 cm_return_if_fail( ctl != NULL );
223 ctl->stylesheet = value;
225 void exporthtml_set_name_format( ExportHtmlCtl *ctl, const gint value ) {
226 cm_return_if_fail( ctl != NULL );
227 ctl->nameFormat = value;
229 void exporthtml_set_banding( ExportHtmlCtl *ctl, const gboolean value ) {
230 cm_return_if_fail( ctl != NULL );
231 ctl->banding = value;
233 void exporthtml_set_link_email( ExportHtmlCtl *ctl, const gboolean value ) {
234 cm_return_if_fail( ctl != NULL );
235 ctl->linkEMail = value;
237 void exporthtml_set_attributes( ExportHtmlCtl *ctl, const gboolean value ) {
238 cm_return_if_fail( ctl != NULL );
239 ctl->showAttribs = value;
243 * Create default CSS file.
244 * Enter: fileSpec File to create.
245 * Return: Status code.
247 static gint exporthtml_create_css_dfl( const gchar *fileSpec ) {
248 FILE *cssFile;
250 cssFile = claws_fopen( fileSpec, "rb" );
251 if( cssFile ) {
252 claws_fclose( cssFile );
253 return MGU_SUCCESS;
255 cssFile = claws_fopen( fileSpec, "wb" );
256 if( ! cssFile ) {
257 return MGU_OPEN_FILE;
260 fprintf( cssFile, "body {\n\tbackground: #ffffe0;\n" );
261 fprintf( cssFile, "\tfont-family: lucida, helvetica, sans-serif;\n" );
262 fprintf( cssFile, "\tfont-size: 10pt;\n" );
263 fprintf( cssFile, "}\n" );
264 fprintf( cssFile, "h1 {\n" );
265 fprintf( cssFile, "\tcolor: #000000;\n" );
266 fprintf( cssFile, "\ttext-align: center;\n" );
267 fprintf( cssFile, "}\n" );
268 fprintf( cssFile, "th {\n" );
269 fprintf( cssFile, "\tfont-size: 10pt;\n" );
270 fprintf( cssFile, "}\n" );
271 fprintf( cssFile, "td {\n" );
272 fprintf( cssFile, "\tfont-size: 10pt;\n" );
273 fprintf( cssFile, "}\n" );
274 fprintf( cssFile, ".fmt-folder {\n" );
275 fprintf( cssFile, "\tcolor: #0000ff;\n" );
276 fprintf( cssFile, "\tfont-size: 18pt;\n" );
277 fprintf( cssFile, "\tfont-weight: bold;\n" );
278 fprintf( cssFile, "}\n" );
279 fprintf( cssFile, ".tab-head {\n" );
280 fprintf( cssFile, "\tbackground: #80c0f0;\n" );
281 fprintf( cssFile, "}\n" );
282 fprintf( cssFile, ".tab-dn {\n" );
283 fprintf( cssFile, "}\n" );
284 fprintf( cssFile, ".tab-addr {\n" );
285 fprintf( cssFile, "\tfont-style: italic;\n" );
286 fprintf( cssFile, "}\n" );
287 fprintf( cssFile, ".tab-email {\n" );
288 fprintf( cssFile, "\tfont-weight: bold;\n" );
289 fprintf( cssFile, "\tfont-style: italic;\n" );
290 fprintf( cssFile, "}\n" );
291 fprintf( cssFile, ".tab-fn {\n" );
292 fprintf( cssFile, "}\n" );
293 fprintf( cssFile, ".tab-attr {\n" );
294 fprintf( cssFile, "}\n" );
296 claws_safe_fclose( cssFile );
297 return MGU_SUCCESS;
301 * Create full CSS file.
302 * Enter: fileSpec File to create.
303 * Return: Status code.
305 static gint exporthtml_create_css_full( const gchar *fileSpec ) {
306 FILE *cssFile;
308 cssFile = claws_fopen( fileSpec, "rb" );
309 if( cssFile ) {
310 claws_fclose( cssFile );
311 return MGU_SUCCESS;
313 cssFile = claws_fopen( fileSpec, "wb" );
314 if( ! cssFile ) {
315 return MGU_OPEN_FILE;
318 fprintf( cssFile, "body {\n\tbackground: #ffffe0;\n" );
319 fprintf( cssFile, "\tfont-family: lucida, helvetica, sans-serif;\n" );
320 fprintf( cssFile, "\tfont-size: 10pt;\n" );
321 fprintf( cssFile, "}\n" );
322 fprintf( cssFile, "h1 {\n" );
323 fprintf( cssFile, "\tcolor: #000000;\n" );
324 fprintf( cssFile, "\ttext-align: center;\n" );
325 fprintf( cssFile, "}\n" );
326 fprintf( cssFile, "th {\n" );
327 fprintf( cssFile, "\tfont-size: 10pt;\n" );
328 fprintf( cssFile, "}\n" );
329 fprintf( cssFile, "td {\n" );
330 fprintf( cssFile, "\tfont-size: 10pt;\n" );
331 fprintf( cssFile, "}\n" );
332 fprintf( cssFile, ".fmt-folder {\n" );
333 fprintf( cssFile, "\tcolor: #0000ff;\n" );
334 fprintf( cssFile, "\tfont-size: 18pt;\n" );
335 fprintf( cssFile, "\tfont-weight: bold;\n" );
336 fprintf( cssFile, "}\n" );
337 fprintf( cssFile, ".tab-head {\n" );
338 fprintf( cssFile, "\tbackground: #80c0f0;\n" );
339 fprintf( cssFile, "}\n" );
340 fprintf( cssFile, ".tab-row0 {\n" );
341 fprintf( cssFile, "\tbackground: #f0f0f0;\n" );
342 fprintf( cssFile, "}\n" );
343 fprintf( cssFile, ".tab-row1 {\n" );
344 fprintf( cssFile, "\tbackground: #d0d0d0;\n" );
345 fprintf( cssFile, "}\n" );
346 fprintf( cssFile, ".tab-dn {\n" );
347 fprintf( cssFile, "}\n" );
348 fprintf( cssFile, ".tab-addr {\n" );
349 fprintf( cssFile, "\tfont-style: italic;\n" );
350 fprintf( cssFile, "}\n" );
351 fprintf( cssFile, ".tab-email {\n" );
352 fprintf( cssFile, "\tfont-weight: bold;\n" );
353 fprintf( cssFile, "\tfont-style: italic;\n" );
354 fprintf( cssFile, "}\n" );
355 fprintf( cssFile, ".tab-fn {\n" );
356 fprintf( cssFile, "}\n" );
357 fprintf( cssFile, ".tab-attr {\n" );
358 fprintf( cssFile, "}\n" );
360 claws_safe_fclose( cssFile );
361 return MGU_SUCCESS;
365 * Create stylesheet files.
366 * Enter: ctl Export control data.
368 static void exporthtml_create_css_files( ExportHtmlCtl *ctl ) {
369 gchar *fileSpec;
370 GList *node;
372 node = ctl->listStyle;
373 while( node ) {
374 StylesheetEntry *entry = node->data;
375 node = g_list_next( node );
376 if( strlen( entry->fileName ) ) {
377 fileSpec = g_strconcat(
378 ctl->dirOutput, G_DIR_SEPARATOR_S,
379 entry->fileName, NULL );
380 if( entry->id == EXPORT_HTML_ID_DEFAULT ) {
381 exporthtml_create_css_dfl( fileSpec );
383 else if( entry->id != EXPORT_HTML_ID_NONE ) {
384 exporthtml_create_css_full( fileSpec );
386 g_free( fileSpec );
392 * Comparison using linked list elements.
394 static gint exporthtml_compare_name(
395 gconstpointer ptr1, gconstpointer ptr2 )
397 const AddrItemObject *item1 = ptr1;
398 const AddrItemObject *item2 = ptr2;
399 const gchar *name1 = NULL, *name2 = NULL;
400 if( item1 ) name1 = ADDRITEM_NAME( item1 );
401 if( item2 ) name2 = ADDRITEM_NAME( item2 );
402 if( ! name1 ) return ( name2 != NULL );
403 if( ! name2 ) return -1;
404 return g_utf8_collate( name1, name2 );
408 * Comparison using linked list elements.
410 static gint exporthtml_compare_email(
411 gconstpointer ptr1, gconstpointer ptr2 )
413 const ItemEMail *email1 = ptr1;
414 const ItemEMail *email2 = ptr2;
415 const gchar *name1 = NULL, *name2 = NULL;
416 if( email1 ) name1 = email1->address;
417 if( email2 ) name2 = email2->address;
418 if( ! name1 ) return ( name2 != NULL );
419 if( ! name2 ) return -1;
420 return g_utf8_collate( name1, name2 );
424 * Comparison using linked list elements.
426 static gint exporthtml_compare_attrib(
427 gconstpointer ptr1, gconstpointer ptr2 )
429 const UserAttribute *attr1 = ptr1;
430 const UserAttribute *attr2 = ptr2;
431 const gchar *name1 = NULL, *name2 = NULL;
432 if( attr1 ) name1 = attr1->name;
433 if( attr2 ) name2 = attr2->name;
434 if( ! name1 ) return ( name2 != NULL );
435 if( ! name2 ) return -1;
436 return g_utf8_collate( name1, name2 );
440 * Build sorted list of named items.
441 * Enter: list List of items to sorted.
442 * Return: Sorted list.
443 * Note: List should freed after use. Items referenced by list should not be
444 * freed since they are managed by the address cache.
446 static GList *exporthtml_sort_name( const GList *list ) {
447 const GList *node;
448 GList *sorted = NULL;
450 node = list;
451 while( node ) {
452 sorted = g_list_insert_sorted(
453 sorted, node->data, exporthtml_compare_name );
454 node = g_list_next( node );
456 return sorted;
460 * Build sorted list of email items.
461 * Enter: list List of E-Mail items to sorted.
462 * Return: Sorted list.
463 * Note: List should freed after use. Items referenced by list should not be
464 * freed since they are managed by the address cache.
466 static GList *exporthtml_sort_email( const GList *list ) {
467 const GList *node;
468 GList *sorted = NULL;
470 node = list;
471 while( node ) {
472 sorted = g_list_insert_sorted(
473 sorted, node->data, exporthtml_compare_email );
474 node = g_list_next( node );
476 return sorted;
480 * Build sorted list of attributes.
481 * Enter: list List of items to sorted.
482 * Return: Sorted list.
483 * Note: List should freed after use. Items referenced by list should not be
484 * freed since they are managed by the address cache.
486 static GList *exporthtml_sort_attrib( const GList *list ) {
487 const GList *node;
488 GList *sorted = NULL;
490 sorted = NULL;
491 node = list;
492 while( node ) {
493 sorted = g_list_insert_sorted(
494 sorted, node->data, exporthtml_compare_attrib );
495 node = g_list_next( node );
497 return sorted;
501 * Format a list of E-Mail addresses.
502 * Enter: ctl Export control data.
503 * stream Output stream.
504 * listEMail List of addresses.
505 * sortFlag Set to TRUE if address list should be sorted.
507 static void exporthtml_fmt_email(
508 ExportHtmlCtl *ctl, FILE *stream, const GList *listEMail,
509 gboolean sortFlag )
511 const GList *node;
512 GList *list;
513 gchar *name;
515 if( listEMail == NULL ) {
516 fprintf( stream, SC_HTML_SPACE );
517 return;
520 list = NULL;
521 if( sortFlag ) {
522 node = list = exporthtml_sort_email( listEMail );
524 else {
525 node = listEMail;
528 while( node ) {
529 ItemEMail *email = ( ItemEMail * ) node->data;
530 node = g_list_next( node );
532 name = ADDRITEM_NAME( email );
533 if( name ) {
534 fprintf( stream, "%s ", name );
536 if( ctl->linkEMail ) {
537 fprintf( stream, "<a href=\"mailto:%s\">",
538 email->address );
540 fprintf( stream, "<span class=\"tab-email\">" );
541 fprintf( stream, "%s", email->address );
542 fprintf( stream, "</span>" );
543 if( ctl->linkEMail ) {
544 fprintf( stream, "</a>" );
546 if( email->remarks ) {
547 if( strlen( email->remarks ) ) {
548 fprintf( stream, " (%s)", email->remarks );
551 fprintf( stream, "<br>\n" );
553 g_list_free( list );
557 * Format groups in an address book folder.
558 * Enter: ctl Export control data.
559 * stream Output stream.
560 * folder Folder.
561 * prevFlag If FALSE, list of persons were output.
562 * Return: TRUE if no groups were formatted.
564 static gboolean exporthtml_fmt_group(
565 ExportHtmlCtl *ctl, FILE *stream, const ItemFolder *folder,
566 gboolean prevFlag )
568 gboolean retVal, band;
569 GList *node, *list;
570 const gchar *tagName;
572 retVal = TRUE;
573 if( folder->listGroup == NULL ) return retVal;
575 /* Write separator */
576 if( ! prevFlag ) {
577 fprintf( stream, "<br>\n" );
580 /* Write table headers */
581 fprintf( stream, "<table" );
582 fprintf( stream, " border=\"%d\"", BORDER_SIZE );
583 fprintf( stream, " cellpadding=\"%d\"", CELL_PADDING );
584 fprintf( stream, " cellspacing=\"%d\"", CELL_SPACING );
585 fprintf( stream, ">\n" );
587 fprintf( stream, "<tr class=\"tab-head\">\n" );
588 fprintf( stream, " <th width=\"200\">" );
589 fprintf( stream, "%s", _( "Group Name" ) );
590 fprintf( stream, "</th>\n" );
591 fprintf( stream, " <th width=\"300\">" );
592 fprintf( stream, "%s", _( "Email Address" ) );
593 fprintf( stream, "</th>\n" );
594 fprintf( stream, "</tr>\n" );
595 list = exporthtml_sort_name( folder->listGroup );
597 band = FALSE;
598 node = list;
599 while( node ) {
600 AddrItemObject *aio = node->data;
601 if( aio && aio->type == ITEMTYPE_GROUP ) {
602 ItemGroup *group = ( ItemGroup * ) aio;
604 fprintf( stream, "<tr valign=\"top\"" );
605 if( ctl->banding ) {
606 if( band ) {
607 tagName = _idTagRowOdd_;
609 else {
610 tagName = _idTagRowEven_;
612 fprintf( stream, " class=\"%s\"", tagName );
613 band = ! band;
615 fprintf( stream, "\">\n" );
617 fprintf( stream, " <td class=\"tab-dn\">" );
618 fprintf( stream, "%s", ADDRITEM_NAME( group ) );
619 fprintf( stream, "</td>\n" );
620 fprintf( stream, " <td class=\"tab-addr\">" );
621 exporthtml_fmt_email( ctl, stream, group->listEMail, TRUE );
622 fprintf( stream, "</td>\n" );
623 fprintf( stream, "</tr>\n" );
624 retVal = FALSE;
626 node = g_list_next( node );
629 g_list_free( list );
630 fprintf( stream, "</table>\n" );
631 return retVal;
635 * Format a list of E-Mail addresses.
636 * Enter: ctl Export control data.
637 * stream Output stream.
638 * listAttr List of attributes.
640 static void exporthtml_fmt_attribs(
641 ExportHtmlCtl *ctl, FILE *stream, const GList *listAttr )
643 const GList *node;
644 GList *list;
646 if( listAttr == NULL ) {
647 fprintf( stream, SC_HTML_SPACE );
648 return;
651 fprintf( stream, "<table border=\"0\">\n" );
652 node = list = exporthtml_sort_attrib( listAttr );
653 while( node ) {
654 UserAttribute *attr = ( UserAttribute * ) node->data;
655 node = g_list_next( node );
656 fprintf( stream, "<tr valign=\"top\">" );
657 fprintf( stream, "<td align=\"right\">%s:</td>", attr->name );
658 fprintf( stream, "<td>%s</td>", attr->value );
659 fprintf( stream, "</tr>\n" );
662 g_list_free( list );
663 fprintf( stream, "</table>" );
667 * Format full name.
668 * Enter: ctl Export control data.
669 * buf Output buffer.
670 * person Person to format.
672 static void exporthtml_fmt_fullname(
673 ExportHtmlCtl *ctl, gchar *buf, const ItemPerson *person )
675 gboolean flag;
677 if( ctl->nameFormat == EXPORT_HTML_LAST_FIRST ) {
678 flag = FALSE;
679 if( person->lastName ) {
680 if( *person->lastName ) {
681 strcat( buf, " " );
682 strcat( buf, person->lastName );
683 flag = TRUE;
686 if( person->firstName ) {
687 if( *person->firstName ) {
688 if( flag ) {
689 strcat( buf, ", " );
691 strcat( buf, person->firstName );
695 else {
696 if( person->firstName ) {
697 if( *person->firstName ) {
698 strcat( buf, person->firstName );
701 if( person->lastName ) {
702 if( *person->lastName ) {
703 strcat( buf, " " );
704 strcat( buf, person->lastName );
708 g_strstrip( buf );
710 flag = FALSE;
711 if( *buf ) flag = TRUE;
712 if( person->nickName ) {
713 if( strlen( person->nickName ) ) {
714 if( flag ) {
715 strcat( buf, " (" );
717 strcat( buf, person->nickName );
718 if( flag ) {
719 strcat( buf, ")" );
723 g_strstrip( buf );
727 * Format persons in an address book folder.
728 * Enter: ctl Export control data.
729 * stream Output stream.
730 * folder Folder.
731 * Return: TRUE if no persons were formatted.
733 static gboolean exporthtml_fmt_person(
734 ExportHtmlCtl *ctl, FILE *stream, const ItemFolder *folder )
736 gboolean retVal, band;
737 GList *node, *list;
738 gchar buf[ FMT_BUFSIZE ];
739 const gchar *tagName;
741 retVal = TRUE;
742 if( folder->listPerson == NULL ) return retVal;
744 /* Write table headers */
745 fprintf( stream, "<table" );
746 fprintf( stream, " border=\"%d\"", BORDER_SIZE );
747 fprintf( stream, " cellpadding=\"%d\"", CELL_PADDING );
748 fprintf( stream, " cellspacing=\"%d\"", CELL_SPACING );
749 fprintf( stream, ">\n" );
751 fprintf( stream, "<tr class=\"tab-head\">\n" );
752 fprintf( stream, " <th width=\"200\">" );
753 fprintf( stream, "%s", _( "Display Name" ) );
754 fprintf( stream, "</th>\n" );
755 fprintf( stream, " <th width=\"300\">" );
756 fprintf( stream, "%s", _( "Email Address" ) );
757 fprintf( stream, "</th>\n" );
758 fprintf( stream, " <th width=\"200\">" );
759 fprintf( stream, "%s", _( "Full Name" ) );
760 fprintf( stream, "</th>\n" );
761 if( ctl->showAttribs ) {
762 fprintf( stream, " <th width=\"250\">" );
763 fprintf( stream, "%s", _( "Attributes" ) );
764 fprintf( stream, "</th>\n" );
766 fprintf( stream, "</tr>\n" );
768 band = FALSE;
769 node = list = exporthtml_sort_name( folder->listPerson );
770 while( node ) {
771 AddrItemObject *aio = node->data;
772 if( aio && aio->type == ITEMTYPE_PERSON ) {
773 ItemPerson *person = ( ItemPerson * ) aio;
775 /* Format first/last/nick name */
776 *buf = '\0';
777 exporthtml_fmt_fullname( ctl, buf,person );
779 fprintf( stream, "<tr valign=\"top\"" );
780 if( ctl->banding ) {
781 if( band ) {
782 tagName = _idTagRowOdd_;
784 else {
785 tagName = _idTagRowEven_;
787 fprintf( stream, " class=\"%s\"", tagName );
788 band = ! band;
790 fprintf( stream, ">\n" );
792 fprintf( stream, " <td class=\"tab-dn\">" );
793 fprintf( stream, "%s", ADDRITEM_NAME( person ) );
794 fprintf( stream, "</td>\n" );
796 fprintf( stream, " <td class=\"tab-addr\">" );
797 exporthtml_fmt_email( ctl, stream, person->listEMail, FALSE );
798 fprintf( stream, "</td>\n" );
800 fprintf( stream, " <td class=\"tab-fn\">" );
801 if( *buf ) {
802 fprintf( stream, "%s", buf );
804 else {
805 fprintf( stream, "%s", SC_HTML_SPACE );
807 fprintf( stream, "</td>\n" );
809 if( ctl->showAttribs ) {
810 fprintf( stream, " <td class=\"tab-attr\">" );
811 exporthtml_fmt_attribs(
812 ctl, stream, person->listAttrib );
813 fprintf( stream, "</td>\n" );
815 fprintf( stream, "</tr>\n" );
817 retVal = FALSE;
819 node = g_list_next( node );
822 g_list_free( list );
823 fprintf( stream, "</table>\n" );
824 return retVal;
828 * Format folder heirarchy.
829 * Enter: stream Output stream.
830 * list Heirarchy list.
832 static void exporthtml_fmt_folderhead( FILE *stream, const GList *list ) {
833 const GList *node;
834 gboolean flag;
835 gchar *name;
837 flag = FALSE;
838 node = list;
839 while( node ) {
840 AddrItemObject *aio = node->data;
841 if( aio && aio->type == ITEMTYPE_FOLDER ) {
842 ItemFolder *folder = ( ItemFolder * ) aio;
844 name = ADDRITEM_NAME( folder );
845 if( name ) {
846 if( flag ) {
847 fprintf( stream, "&nbsp;&gt;&nbsp;" );
849 fprintf( stream, "%s", name );
850 flag = TRUE;
853 node = g_list_next( node );
858 * Format an address book folder.
859 * Enter: ctl Export control data.
860 * stream Output stream.
861 * folder Folder.
863 static void exporthtml_fmt_folder(
864 ExportHtmlCtl *ctl, FILE *stream, const ItemFolder *folder )
866 const GList *node;
867 GList *listHeir, *list;
868 const gchar *name;
869 gboolean ret1;
871 name = ADDRITEM_NAME( folder );
872 if( name ) {
873 listHeir = addritem_folder_path( folder, TRUE );
874 if( listHeir ) {
875 fprintf( stream, "<p class=\"fmt-folder\">" );
876 fprintf( stream, "%s: ", _( "Folder" ) );
877 exporthtml_fmt_folderhead( stream, listHeir );
878 fprintf( stream, "</p>\n" );
879 g_list_free( listHeir );
883 ret1 = exporthtml_fmt_person( ctl, stream, folder );
884 exporthtml_fmt_group( ctl, stream, folder, ret1 );
886 node = list = exporthtml_sort_name( folder->listFolder );
887 while( node ) {
888 AddrItemObject *aio = node->data;
889 if( aio && aio->type == ITEMTYPE_FOLDER ) {
890 ItemFolder *subFolder = ( ItemFolder * ) aio;
891 exporthtml_fmt_folder( ctl, stream, subFolder );
893 node = g_list_next( node );
895 if( list ) {
896 g_list_free( list );
901 * Format header block.
902 * Enter: ctl Export control data.
903 * stream Output stream.
904 * title Page title.
906 static void exporthtml_fmt_header(
907 ExportHtmlCtl *ctl, FILE *stream, gchar *title )
909 StylesheetEntry *entry;
911 entry = exporthtml_find_stylesheet( ctl );
913 fprintf( stream,
914 "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n" );
915 fprintf( stream,
916 "\"http://www.w3.org/TR/html4/loose.dtd\">\n" );
917 fprintf( stream, "<html>\n" );
918 fprintf( stream, "<head>\n" );
920 if( ctl->encoding && strlen( ctl->encoding ) > 0 ) {
921 fprintf( stream, "<meta " );
922 fprintf( stream, "http-equiv=\"Content-Type\" " );
923 fprintf( stream, "content=\"text/html; charset=%s\">\n",
924 ctl->encoding );
927 fprintf( stream, "<title>%s</title>\n", title );
929 if( entry != NULL ) {
930 if( entry->fileName && strlen( entry->fileName ) > 0 ) {
931 fprintf( stream, "<link " );
932 fprintf( stream, "rel=\"stylesheet\" " );
933 fprintf( stream, "type=\"text/css\" " );
934 fprintf( stream, "href=\"%s\" >\n", entry->fileName );
937 fprintf( stream, "</head>\n" );
941 * ============================================================================
942 * Export address book to HTML file.
943 * Enter: ctl Export control data.
944 * cache Address book/data source cache.
945 * Return: Status.
946 * ============================================================================
948 void exporthtml_process(
949 ExportHtmlCtl *ctl, AddressCache *cache )
951 ItemFolder *rootFolder;
952 FILE *htmlFile;
953 time_t tt;
954 gchar *dsName;
955 static gchar *title;
956 gchar buf[512];
958 htmlFile = claws_fopen( ctl->path, "wb" );
959 if( ! htmlFile ) {
960 /* Cannot open file */
961 g_print( "Cannot open file for write\n" );
962 ctl->retVal = MGU_OPEN_FILE;
963 return;
966 title = _( "Claws Mail Address Book" );
967 rootFolder = cache->rootFolder;
968 dsName = cache->name;
970 exporthtml_fmt_header( ctl, htmlFile, title );
972 fprintf( htmlFile, "<body>\n" );
973 fprintf( htmlFile, "<h1>%s</h1>\n", title );
975 fprintf( htmlFile, "<p class=\"fmt-folder\">" );
976 fprintf( htmlFile, "%s: ", _( "Address Book" ) );
977 fprintf( htmlFile, "%s", dsName );
978 fprintf( htmlFile, "</p>\n" );
980 exporthtml_fmt_folder( ctl, htmlFile, rootFolder );
982 tt = time( NULL );
983 fprintf( htmlFile, "<p>%s</p>\n", ctime_r( &tt, buf ) );
984 fprintf( htmlFile, "<hr width=\"100%%\">\n" );
986 fprintf( htmlFile, "</body>\n" );
987 fprintf( htmlFile, "</html>\n" );
989 claws_safe_fclose( htmlFile );
990 ctl->retVal = MGU_SUCCESS;
992 /* Create stylesheet files */
993 exporthtml_create_css_files( ctl );
998 * Build full export file specification.
999 * Enter: ctl Export control data.
1001 static void exporthtml_build_filespec( ExportHtmlCtl *ctl ) {
1002 gchar *fileSpec;
1004 fileSpec = g_strconcat(
1005 ctl->dirOutput, G_DIR_SEPARATOR_S, ctl->fileHtml, NULL );
1006 ctl->path = mgu_replace_string( ctl->path, fileSpec );
1007 g_free( fileSpec );
1011 * ============================================================================
1012 * Parse directory and filename from full export file specification.
1013 * Enter: ctl Export control data.
1014 * fileSpec File spec.
1015 * ============================================================================
1017 void exporthtml_parse_filespec( ExportHtmlCtl *ctl, gchar *fileSpec ) {
1018 gchar *t;
1019 gchar *base = g_path_get_basename(fileSpec);
1021 ctl->fileHtml =
1022 mgu_replace_string( ctl->fileHtml, base );
1023 g_free(base);
1024 t = g_path_get_dirname( fileSpec );
1025 ctl->dirOutput = mgu_replace_string( ctl->dirOutput, t );
1026 g_free( t );
1027 ctl->path = mgu_replace_string( ctl->path, fileSpec );
1031 * ============================================================================
1032 * Create output directory.
1033 * Enter: ctl Export control data.
1034 * Return: TRUE if directory created.
1035 * ============================================================================
1037 gboolean exporthtml_create_dir( ExportHtmlCtl *ctl ) {
1038 gboolean retVal = FALSE;
1040 ctl->rcCreate = 0;
1041 if( g_mkdir( ctl->dirOutput, S_IRWXU ) == 0 ) {
1042 retVal = TRUE;
1044 else {
1045 ctl->rcCreate = errno;
1047 return retVal;
1051 * ============================================================================
1052 * Retrieve create directory error message.
1053 * Enter: ctl Export control data.
1054 * Return: Message.
1055 * ============================================================================
1057 gchar *exporthtml_get_create_msg( ExportHtmlCtl *ctl ) {
1058 gchar *msg;
1060 if( ctl->rcCreate == EEXIST ) {
1061 msg = _( "Name already exists but is not a directory." );
1063 else if( ctl->rcCreate == EACCES ) {
1064 msg = _( "No permissions to create directory." );
1066 else if( ctl->rcCreate == ENAMETOOLONG ) {
1067 msg = _( "Name is too long." );
1069 else {
1070 msg = _( "Not specified." );
1072 return msg;
1076 * Set default values.
1077 * Enter: ctl Export control data.
1079 static void exporthtml_default_values( ExportHtmlCtl *ctl ) {
1080 gchar *str;
1082 str = g_strconcat(
1083 get_home_dir(), G_DIR_SEPARATOR_S,
1084 DFL_DIR_CLAWS_OUT, NULL );
1086 ctl->dirOutput = mgu_replace_string( ctl->dirOutput, str );
1087 g_free( str );
1089 ctl->fileHtml =
1090 mgu_replace_string( ctl->fileHtml, DFL_FILE_CLAWS_OUT );
1091 ctl->encoding = NULL;
1092 ctl->stylesheet = EXPORT_HTML_ID_DEFAULT;
1093 ctl->nameFormat = EXPORT_HTML_FIRST_LAST;
1094 ctl->banding = TRUE;
1095 ctl->linkEMail = TRUE;
1096 ctl->showAttribs = TRUE;
1097 ctl->retVal = MGU_SUCCESS;
1101 * ============================================================================
1102 * Load settings from XML properties file.
1103 * Enter: ctl Export control data.
1104 * ============================================================================
1106 void exporthtml_load_settings( ExportHtmlCtl *ctl ) {
1107 XmlProperty *props;
1108 gint rc;
1109 gchar buf[256];
1111 *buf = '\0';
1112 props = xmlprops_create();
1113 xmlprops_set_path( props, ctl->settingsFile );
1114 rc = xmlprops_load_file( props );
1115 if( rc == 0 ) {
1116 /* Read settings */
1117 xmlprops_get_property_s( props, EXMLPROP_DIRECTORY, buf );
1118 ctl->dirOutput = mgu_replace_string( ctl->dirOutput, buf );
1120 xmlprops_get_property_s( props, EXMLPROP_FILE, buf );
1121 ctl->fileHtml = mgu_replace_string( ctl->fileHtml, buf );
1123 ctl->stylesheet =
1124 xmlprops_get_property_i( props, EXMLPROP_STYLESHEET );
1125 ctl->nameFormat =
1126 xmlprops_get_property_i( props, EXMLPROP_FMT_NAME );
1127 ctl->banding =
1128 xmlprops_get_property_b( props, EXMLPROP_BANDING );
1129 ctl->linkEMail =
1130 xmlprops_get_property_b( props, EXMLPROP_FMT_EMAIL );
1131 ctl->showAttribs =
1132 xmlprops_get_property_b( props, EXMLPROP_FMT_ATTRIB );
1134 else {
1135 /* Set default values */
1136 exporthtml_default_values( ctl );
1138 exporthtml_build_filespec( ctl );
1139 /* exporthtml_print( ctl, stdout ); */
1141 xmlprops_free( props );
1145 * ============================================================================
1146 * Save settings to XML properties file.
1147 * Enter: ctl Export control data.
1148 * ============================================================================
1150 void exporthtml_save_settings( ExportHtmlCtl *ctl ) {
1151 XmlProperty *props;
1153 props = xmlprops_create();
1154 xmlprops_set_path( props, ctl->settingsFile );
1156 xmlprops_set_property( props, EXMLPROP_DIRECTORY, ctl->dirOutput );
1157 xmlprops_set_property( props, EXMLPROP_FILE, ctl->fileHtml );
1158 xmlprops_set_property_i( props, EXMLPROP_STYLESHEET, ctl->stylesheet );
1159 xmlprops_set_property_i( props, EXMLPROP_FMT_NAME, ctl->nameFormat );
1160 xmlprops_set_property_b( props, EXMLPROP_BANDING, ctl->banding );
1161 xmlprops_set_property_b( props, EXMLPROP_FMT_EMAIL, ctl->linkEMail );
1162 xmlprops_set_property_b( props, EXMLPROP_FMT_ATTRIB, ctl->showAttribs );
1163 if (xmlprops_save_file( props ) != MGU_SUCCESS)
1164 g_warning("can't save settings");
1165 xmlprops_free( props );
1169 * ============================================================================
1170 * End of Source.
1171 * ============================================================================