2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2002-2009 Match Grun and the Claws Mail team
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.
34 #include <glib/gi18n.h>
42 #include "exporthtml.h"
45 #ifdef MKDIR_TAKES_ONE_ARG
47 #define mkdir(a,b) mkdir(a)
50 #define DFL_DIR_CLAWS_OUT "claws-mail-out"
51 #define DFL_FILE_CLAWS_OUT "addressbook.html"
53 #define FMT_BUFSIZE 2048
54 #define SC_HTML_SPACE " "
56 #define CELL_PADDING 2
57 #define CELL_SPACING 2
58 #define CHAR_ENCODING "UTF-8"
60 /* Stylesheet names */
61 #define FILENAME_NONE ""
62 #define FILENAME_DEFAULT "claws-mail.css"
63 #define FILENAME_FULL "full.css"
64 #define FILENAME_CUSTOM "custom.css"
65 #define FILENAME_CUSTOM2 "custom2.css"
66 #define FILENAME_CUSTOM3 "custom3.css"
67 #define FILENAME_CUSTOM4 "custom4.css"
69 /* Settings - properties */
70 #define EXML_PROPFILE_NAME "exporthtml.xml"
71 #define EXMLPROP_DIRECTORY "directory"
72 #define EXMLPROP_FILE "file"
73 #define EXMLPROP_STYLESHEET "stylesheet"
74 #define EXMLPROP_FMT_NAME "format-full-name"
75 #define EXMLPROP_FMT_EMAIL "format-email-links"
76 #define EXMLPROP_FMT_ATTRIB "format-attributes"
77 #define EXMLPROP_BANDING "color-banding"
78 #define EXMLPROP_VALUE_YES "y"
79 #define EXMLPROP_VALUE_NO "n"
81 static gchar *_idTagRowEven_ = "tab-row0";
82 static gchar *_idTagRowOdd_ = "tab-row1";
87 typedef struct _StylesheetEntry StylesheetEntry;
88 struct _StylesheetEntry {
95 * Build stylesheet entry.
96 * Enter: ctl Export control data.
101 static void exporthtml_build_entry(
102 ExportHtmlCtl *ctl, const gchar *file, const gint id,
105 StylesheetEntry *entry;
107 entry = g_new0( StylesheetEntry, 1 );
108 entry->fileName = g_strdup( file );
110 entry->dflValue = dfl;
111 ctl->listStyle = g_list_append( ctl->listStyle, entry );
115 * Free up object by releasing internal memory.
116 * Enter: ctl Export control data.
118 ExportHtmlCtl *exporthtml_create( void ) {
119 ExportHtmlCtl *ctl = g_new0( ExportHtmlCtl, 1 );
122 ctl->dirOutput = NULL;
123 ctl->fileHtml = NULL;
124 ctl->encoding = g_strconcat(CHAR_ENCODING, NULL);
125 ctl->stylesheet = EXPORT_HTML_ID_NONE;
126 ctl->nameFormat = EXPORT_HTML_FIRST_LAST;
127 ctl->banding = FALSE;
128 ctl->linkEMail = FALSE;
129 ctl->showAttribs = FALSE;
130 ctl->retVal = MGU_SUCCESS;
131 ctl->listStyle = NULL;
133 ctl->settingsFile = g_strconcat(
134 get_rc_dir(), G_DIR_SEPARATOR_S, EXML_PROPFILE_NAME, NULL );
136 /* Build stylesheet list */
137 exporthtml_build_entry(
138 ctl, FILENAME_NONE, EXPORT_HTML_ID_NONE, FALSE );
139 exporthtml_build_entry(
140 ctl, FILENAME_DEFAULT, EXPORT_HTML_ID_DEFAULT, TRUE );
141 exporthtml_build_entry(
142 ctl, FILENAME_FULL, EXPORT_HTML_ID_FULL, FALSE );
143 exporthtml_build_entry(
144 ctl, FILENAME_CUSTOM, EXPORT_HTML_ID_CUSTOM, FALSE );
145 exporthtml_build_entry(
146 ctl, FILENAME_CUSTOM2, EXPORT_HTML_ID_CUSTOM2, FALSE );
147 exporthtml_build_entry(
148 ctl, FILENAME_CUSTOM3, EXPORT_HTML_ID_CUSTOM3, FALSE );
149 exporthtml_build_entry(
150 ctl, FILENAME_CUSTOM4, EXPORT_HTML_ID_CUSTOM4, FALSE );
156 * Free up object by releasing internal memory.
157 * Enter: ctl Export control data.
159 void exporthtml_free( ExportHtmlCtl *ctl ) {
161 StylesheetEntry *entry;
163 cm_return_if_fail( ctl != NULL );
165 /* Free stylesheet list */
166 node = ctl->listStyle;
168 entry = ( StylesheetEntry * ) node->data;
169 g_free( entry->fileName );
170 entry->fileName = NULL;
172 entry->dflValue = FALSE;
175 node = g_list_next( node );
177 g_list_free( ctl->listStyle );
178 ctl->listStyle = NULL;
181 g_free( ctl->fileHtml );
182 g_free( ctl->encoding );
183 g_free( ctl->dirOutput );
184 g_free( ctl->settingsFile );
188 ctl->dirOutput = NULL;
189 ctl->fileHtml = NULL;
190 ctl->encoding = NULL;
191 ctl->stylesheet = EXPORT_HTML_ID_NONE;
192 ctl->nameFormat = EXPORT_HTML_FIRST_LAST;
193 ctl->banding = FALSE;
194 ctl->linkEMail = FALSE;
195 ctl->showAttribs = FALSE;
196 ctl->retVal = MGU_SUCCESS;
199 /* Now release object */
205 * Enter: ctl Export control data.
206 * Return: Stylesheet object, or NULL if nothing found. If a default entry is
207 * found in list, it will be returned.
209 static StylesheetEntry *exporthtml_find_stylesheet( ExportHtmlCtl *ctl ) {
210 StylesheetEntry *retVal = NULL;
211 StylesheetEntry *entry;
214 node = ctl->listStyle;
216 entry = ( StylesheetEntry * ) node->data;
217 if( entry->id == ctl->stylesheet ) return entry;
218 if( entry->dflValue ) retVal = entry;
219 node = g_list_next( node );
224 void exporthtml_set_stylesheet( ExportHtmlCtl *ctl, const gint value ) {
225 cm_return_if_fail( ctl != NULL );
226 ctl->stylesheet = value;
228 void exporthtml_set_name_format( ExportHtmlCtl *ctl, const gint value ) {
229 cm_return_if_fail( ctl != NULL );
230 ctl->nameFormat = value;
232 void exporthtml_set_banding( ExportHtmlCtl *ctl, const gboolean value ) {
233 cm_return_if_fail( ctl != NULL );
234 ctl->banding = value;
236 void exporthtml_set_link_email( ExportHtmlCtl *ctl, const gboolean value ) {
237 cm_return_if_fail( ctl != NULL );
238 ctl->linkEMail = value;
240 void exporthtml_set_attributes( ExportHtmlCtl *ctl, const gboolean value ) {
241 cm_return_if_fail( ctl != NULL );
242 ctl->showAttribs = value;
246 * Create default CSS file.
247 * Enter: fileSpec File to create.
248 * Return: Status code.
250 static gint exporthtml_create_css_dfl( const gchar *fileSpec ) {
253 cssFile = g_fopen( fileSpec, "rb" );
258 cssFile = g_fopen( fileSpec, "wb" );
260 return MGU_OPEN_FILE;
263 fprintf( cssFile, "body {\n\tbackground: #ffffe0;\n" );
264 fprintf( cssFile, "\tfont-family: lucida, helvetica, sans-serif;\n" );
265 fprintf( cssFile, "\tfont-size: 10pt;\n" );
266 fprintf( cssFile, "}\n" );
267 fprintf( cssFile, "h1 {\n" );
268 fprintf( cssFile, "\tcolor: #000000;\n" );
269 fprintf( cssFile, "\ttext-align: center;\n" );
270 fprintf( cssFile, "}\n" );
271 fprintf( cssFile, "th {\n" );
272 fprintf( cssFile, "\tfont-size: 10pt;\n" );
273 fprintf( cssFile, "}\n" );
274 fprintf( cssFile, "td {\n" );
275 fprintf( cssFile, "\tfont-size: 10pt;\n" );
276 fprintf( cssFile, "}\n" );
277 fprintf( cssFile, ".fmt-folder {\n" );
278 fprintf( cssFile, "\tcolor: #0000ff;\n" );
279 fprintf( cssFile, "\tfont-size: 18pt;\n" );
280 fprintf( cssFile, "\tfont-weight: bold;\n" );
281 fprintf( cssFile, "}\n" );
282 fprintf( cssFile, ".tab-head {\n" );
283 fprintf( cssFile, "\tbackground: #80c0f0;\n" );
284 fprintf( cssFile, "}\n" );
285 fprintf( cssFile, ".tab-dn {\n" );
286 fprintf( cssFile, "}\n" );
287 fprintf( cssFile, ".tab-addr {\n" );
288 fprintf( cssFile, "\tfont-style: italic;\n" );
289 fprintf( cssFile, "}\n" );
290 fprintf( cssFile, ".tab-email {\n" );
291 fprintf( cssFile, "\tfont-weight: bold;\n" );
292 fprintf( cssFile, "\tfont-style: italic;\n" );
293 fprintf( cssFile, "}\n" );
294 fprintf( cssFile, ".tab-fn {\n" );
295 fprintf( cssFile, "}\n" );
296 fprintf( cssFile, ".tab-attr {\n" );
297 fprintf( cssFile, "}\n" );
304 * Create full CSS file.
305 * Enter: fileSpec File to create.
306 * Return: Status code.
308 static gint exporthtml_create_css_full( const gchar *fileSpec ) {
311 cssFile = g_fopen( fileSpec, "rb" );
316 cssFile = g_fopen( fileSpec, "wb" );
318 return MGU_OPEN_FILE;
321 fprintf( cssFile, "body {\n\tbackground: #ffffe0;\n" );
322 fprintf( cssFile, "\tfont-family: lucida, helvetica, sans-serif;\n" );
323 fprintf( cssFile, "\tfont-size: 10pt;\n" );
324 fprintf( cssFile, "}\n" );
325 fprintf( cssFile, "h1 {\n" );
326 fprintf( cssFile, "\tcolor: #000000;\n" );
327 fprintf( cssFile, "\ttext-align: center;\n" );
328 fprintf( cssFile, "}\n" );
329 fprintf( cssFile, "th {\n" );
330 fprintf( cssFile, "\tfont-size: 10pt;\n" );
331 fprintf( cssFile, "}\n" );
332 fprintf( cssFile, "td {\n" );
333 fprintf( cssFile, "\tfont-size: 10pt;\n" );
334 fprintf( cssFile, "}\n" );
335 fprintf( cssFile, ".fmt-folder {\n" );
336 fprintf( cssFile, "\tcolor: #0000ff;\n" );
337 fprintf( cssFile, "\tfont-size: 18pt;\n" );
338 fprintf( cssFile, "\tfont-weight: bold;\n" );
339 fprintf( cssFile, "}\n" );
340 fprintf( cssFile, ".tab-head {\n" );
341 fprintf( cssFile, "\tbackground: #80c0f0;\n" );
342 fprintf( cssFile, "}\n" );
343 fprintf( cssFile, ".tab-row0 {\n" );
344 fprintf( cssFile, "\tbackground: #f0f0f0;\n" );
345 fprintf( cssFile, "}\n" );
346 fprintf( cssFile, ".tab-row1 {\n" );
347 fprintf( cssFile, "\tbackground: #d0d0d0;\n" );
348 fprintf( cssFile, "}\n" );
349 fprintf( cssFile, ".tab-dn {\n" );
350 fprintf( cssFile, "}\n" );
351 fprintf( cssFile, ".tab-addr {\n" );
352 fprintf( cssFile, "\tfont-style: italic;\n" );
353 fprintf( cssFile, "}\n" );
354 fprintf( cssFile, ".tab-email {\n" );
355 fprintf( cssFile, "\tfont-weight: bold;\n" );
356 fprintf( cssFile, "\tfont-style: italic;\n" );
357 fprintf( cssFile, "}\n" );
358 fprintf( cssFile, ".tab-fn {\n" );
359 fprintf( cssFile, "}\n" );
360 fprintf( cssFile, ".tab-attr {\n" );
361 fprintf( cssFile, "}\n" );
368 * Create stylesheet files.
369 * Enter: ctl Export control data.
371 static void exporthtml_create_css_files( ExportHtmlCtl *ctl ) {
375 node = ctl->listStyle;
377 StylesheetEntry *entry = node->data;
378 node = g_list_next( node );
379 if( strlen( entry->fileName ) ) {
380 fileSpec = g_strconcat(
381 ctl->dirOutput, G_DIR_SEPARATOR_S,
382 entry->fileName, NULL );
383 if( entry->id == EXPORT_HTML_ID_DEFAULT ) {
384 exporthtml_create_css_dfl( fileSpec );
386 else if( entry->id != EXPORT_HTML_ID_NONE ) {
387 exporthtml_create_css_full( fileSpec );
395 * Comparison using linked list elements.
397 static gint exporthtml_compare_name(
398 gconstpointer ptr1, gconstpointer ptr2 )
400 const AddrItemObject *item1 = ptr1;
401 const AddrItemObject *item2 = ptr2;
402 const gchar *name1 = NULL, *name2 = NULL;
403 if( item1 ) name1 = ADDRITEM_NAME( item1 );
404 if( item2 ) name2 = ADDRITEM_NAME( item2 );
405 if( ! name1 ) return ( name2 != NULL );
406 if( ! name2 ) return -1;
407 return g_utf8_collate( name1, name2 );
411 * Comparison using linked list elements.
413 static gint exporthtml_compare_email(
414 gconstpointer ptr1, gconstpointer ptr2 )
416 const ItemEMail *email1 = ptr1;
417 const ItemEMail *email2 = ptr2;
418 const gchar *name1 = NULL, *name2 = NULL;
419 if( email1 ) name1 = email1->address;
420 if( email2 ) name2 = email2->address;
421 if( ! name1 ) return ( name2 != NULL );
422 if( ! name2 ) return -1;
423 return g_utf8_collate( name1, name2 );
427 * Comparison using linked list elements.
429 static gint exporthtml_compare_attrib(
430 gconstpointer ptr1, gconstpointer ptr2 )
432 const UserAttribute *attr1 = ptr1;
433 const UserAttribute *attr2 = ptr2;
434 const gchar *name1 = NULL, *name2 = NULL;
435 if( attr1 ) name1 = attr1->name;
436 if( attr2 ) name2 = attr2->name;
437 if( ! name1 ) return ( name2 != NULL );
438 if( ! name2 ) return -1;
439 return g_utf8_collate( name1, name2 );
443 * Build sorted list of named items.
444 * Enter: list List of items to sorted.
445 * Return: Sorted list.
446 * Note: List should freed after use. Items referenced by list should not be
447 * freed since they are managed by the address cache.
449 static GList *exporthtml_sort_name( const GList *list ) {
451 GList *sorted = NULL;
455 sorted = g_list_insert_sorted(
456 sorted, node->data, exporthtml_compare_name );
457 node = g_list_next( node );
463 * Build sorted list of email items.
464 * Enter: list List of E-Mail items to sorted.
465 * Return: Sorted list.
466 * Note: List should freed after use. Items referenced by list should not be
467 * freed since they are managed by the address cache.
469 static GList *exporthtml_sort_email( const GList *list ) {
471 GList *sorted = NULL;
475 sorted = g_list_insert_sorted(
476 sorted, node->data, exporthtml_compare_email );
477 node = g_list_next( node );
483 * Build sorted list of attributes.
484 * Enter: list List of items to sorted.
485 * Return: Sorted list.
486 * Note: List should freed after use. Items referenced by list should not be
487 * freed since they are managed by the address cache.
489 static GList *exporthtml_sort_attrib( const GList *list ) {
491 GList *sorted = NULL;
496 sorted = g_list_insert_sorted(
497 sorted, node->data, exporthtml_compare_attrib );
498 node = g_list_next( node );
504 * Format a list of E-Mail addresses.
505 * Enter: ctl Export control data.
506 * stream Output stream.
507 * listEMail List of addresses.
508 * sortFlag Set to TRUE if address list should be sorted.
510 static void exporthtml_fmt_email(
511 ExportHtmlCtl *ctl, FILE *stream, const GList *listEMail,
518 if( listEMail == NULL ) {
519 fprintf( stream, SC_HTML_SPACE );
525 node = list = exporthtml_sort_email( listEMail );
532 ItemEMail *email = ( ItemEMail * ) node->data;
533 node = g_list_next( node );
535 name = ADDRITEM_NAME( email );
537 fprintf( stream, "%s ", name );
539 if( ctl->linkEMail ) {
540 fprintf( stream, "<a href=\"mailto:%s\">",
543 fprintf( stream, "<span class=\"tab-email\">" );
544 fprintf( stream, "%s", email->address );
545 fprintf( stream, "</span>" );
546 if( ctl->linkEMail ) {
547 fprintf( stream, "</a>" );
549 if( email->remarks ) {
550 if( strlen( email->remarks ) ) {
551 fprintf( stream, " (%s)", email->remarks );
554 fprintf( stream, "<br>\n" );
560 * Format groups in an address book folder.
561 * Enter: ctl Export control data.
562 * stream Output stream.
564 * prevFlag If FALSE, list of persons were output.
565 * Return: TRUE if no groups were formatted.
567 static gboolean exporthtml_fmt_group(
568 ExportHtmlCtl *ctl, FILE *stream, const ItemFolder *folder,
571 gboolean retVal, band;
573 const gchar *tagName;
576 if( folder->listGroup == NULL ) return retVal;
578 /* Write separator */
580 fprintf( stream, "<br>\n" );
583 /* Write table headers */
584 fprintf( stream, "<table" );
585 fprintf( stream, " border=\"%d\"", BORDER_SIZE );
586 fprintf( stream, " cellpadding=\"%d\"", CELL_PADDING );
587 fprintf( stream, " cellspacing=\"%d\"", CELL_SPACING );
588 fprintf( stream, ">\n" );
590 fprintf( stream, "<tr class=\"tab-head\">\n" );
591 fprintf( stream, " <th width=\"200\">" );
592 fprintf( stream, "%s", _( "Group Name" ) );
593 fprintf( stream, "</th>\n" );
594 fprintf( stream, " <th width=\"300\">" );
595 fprintf( stream, "%s", _( "Email Address" ) );
596 fprintf( stream, "</th>\n" );
597 fprintf( stream, "</tr>\n" );
598 list = exporthtml_sort_name( folder->listGroup );
603 AddrItemObject *aio = node->data;
604 if( aio && aio->type == ITEMTYPE_GROUP ) {
605 ItemGroup *group = ( ItemGroup * ) aio;
607 fprintf( stream, "<tr valign=\"top\"" );
610 tagName = _idTagRowOdd_;
613 tagName = _idTagRowEven_;
615 fprintf( stream, " class=\"%s\"", tagName );
618 fprintf( stream, "\">\n" );
620 fprintf( stream, " <td class=\"tab-dn\">" );
621 fprintf( stream, "%s", ADDRITEM_NAME( group ) );
622 fprintf( stream, "</td>\n" );
623 fprintf( stream, " <td class=\"tab-addr\">" );
624 exporthtml_fmt_email( ctl, stream, group->listEMail, TRUE );
625 fprintf( stream, "</td>\n" );
626 fprintf( stream, "</tr>\n" );
629 node = g_list_next( node );
633 fprintf( stream, "</table>\n" );
638 * Format a list of E-Mail addresses.
639 * Enter: ctl Export control data.
640 * stream Output stream.
641 * listAttr List of attributes.
643 static void exporthtml_fmt_attribs(
644 ExportHtmlCtl *ctl, FILE *stream, const GList *listAttr )
649 if( listAttr == NULL ) {
650 fprintf( stream, SC_HTML_SPACE );
654 fprintf( stream, "<table border=\"0\">\n" );
655 node = list = exporthtml_sort_attrib( listAttr );
657 UserAttribute *attr = ( UserAttribute * ) node->data;
658 node = g_list_next( node );
659 fprintf( stream, "<tr valign=\"top\">" );
660 fprintf( stream, "<td align=\"right\">%s:</td>", attr->name );
661 fprintf( stream, "<td>%s</td>", attr->value );
662 fprintf( stream, "</tr>\n" );
666 fprintf( stream, "</table>" );
671 * Enter: ctl Export control data.
673 * person Person to format.
675 static void exporthtml_fmt_fullname(
676 ExportHtmlCtl *ctl, gchar *buf, const ItemPerson *person )
680 if( ctl->nameFormat == EXPORT_HTML_LAST_FIRST ) {
682 if( person->lastName ) {
683 if( *person->lastName ) {
685 strcat( buf, person->lastName );
689 if( person->firstName ) {
690 if( *person->firstName ) {
694 strcat( buf, person->firstName );
699 if( person->firstName ) {
700 if( *person->firstName ) {
701 strcat( buf, person->firstName );
704 if( person->lastName ) {
705 if( *person->lastName ) {
707 strcat( buf, person->lastName );
714 if( *buf ) flag = TRUE;
715 if( person->nickName ) {
716 if( strlen( person->nickName ) ) {
720 strcat( buf, person->nickName );
730 * Format persons in an address book folder.
731 * Enter: ctl Export control data.
732 * stream Output stream.
734 * Return: TRUE if no persons were formatted.
736 static gboolean exporthtml_fmt_person(
737 ExportHtmlCtl *ctl, FILE *stream, const ItemFolder *folder )
739 gboolean retVal, band;
741 gchar buf[ FMT_BUFSIZE ];
742 const gchar *tagName;
745 if( folder->listPerson == NULL ) return retVal;
747 /* Write table headers */
748 fprintf( stream, "<table" );
749 fprintf( stream, " border=\"%d\"", BORDER_SIZE );
750 fprintf( stream, " cellpadding=\"%d\"", CELL_PADDING );
751 fprintf( stream, " cellspacing=\"%d\"", CELL_SPACING );
752 fprintf( stream, ">\n" );
754 fprintf( stream, "<tr class=\"tab-head\">\n" );
755 fprintf( stream, " <th width=\"200\">" );
756 fprintf( stream, "%s", _( "Display Name" ) );
757 fprintf( stream, "</th>\n" );
758 fprintf( stream, " <th width=\"300\">" );
759 fprintf( stream, "%s", _( "Email Address" ) );
760 fprintf( stream, "</th>\n" );
761 fprintf( stream, " <th width=\"200\">" );
762 fprintf( stream, "%s", _( "Full Name" ) );
763 fprintf( stream, "</th>\n" );
764 if( ctl->showAttribs ) {
765 fprintf( stream, " <th width=\"250\">" );
766 fprintf( stream, "%s", _( "Attributes" ) );
767 fprintf( stream, "</th>\n" );
769 fprintf( stream, "</tr>\n" );
772 node = list = exporthtml_sort_name( folder->listPerson );
774 AddrItemObject *aio = node->data;
775 if( aio && aio->type == ITEMTYPE_PERSON ) {
776 ItemPerson *person = ( ItemPerson * ) aio;
778 /* Format first/last/nick name */
780 exporthtml_fmt_fullname( ctl, buf,person );
782 fprintf( stream, "<tr valign=\"top\"" );
785 tagName = _idTagRowOdd_;
788 tagName = _idTagRowEven_;
790 fprintf( stream, " class=\"%s\"", tagName );
793 fprintf( stream, ">\n" );
795 fprintf( stream, " <td class=\"tab-dn\">" );
796 fprintf( stream, "%s", ADDRITEM_NAME( person ) );
797 fprintf( stream, "</td>\n" );
799 fprintf( stream, " <td class=\"tab-addr\">" );
800 exporthtml_fmt_email( ctl, stream, person->listEMail, FALSE );
801 fprintf( stream, "</td>\n" );
803 fprintf( stream, " <td class=\"tab-fn\">" );
805 fprintf( stream, "%s", buf );
808 fprintf( stream, "%s", SC_HTML_SPACE );
810 fprintf( stream, "</td>\n" );
812 if( ctl->showAttribs ) {
813 fprintf( stream, " <td class=\"tab-attr\">" );
814 exporthtml_fmt_attribs(
815 ctl, stream, person->listAttrib );
816 fprintf( stream, "</td>\n" );
818 fprintf( stream, "</tr>\n" );
822 node = g_list_next( node );
826 fprintf( stream, "</table>\n" );
831 * Format folder heirarchy.
832 * Enter: stream Output stream.
833 * list Heirarchy list.
835 static void exporthtml_fmt_folderhead( FILE *stream, const GList *list ) {
843 AddrItemObject *aio = node->data;
844 if( aio && aio->type == ITEMTYPE_FOLDER ) {
845 ItemFolder *folder = ( ItemFolder * ) aio;
847 name = ADDRITEM_NAME( folder );
850 fprintf( stream, " > " );
852 fprintf( stream, "%s", name );
856 node = g_list_next( node );
861 * Format an address book folder.
862 * Enter: ctl Export control data.
863 * stream Output stream.
866 static void exporthtml_fmt_folder(
867 ExportHtmlCtl *ctl, FILE *stream, const ItemFolder *folder )
870 GList *listHeir, *list;
874 name = ADDRITEM_NAME( folder );
876 listHeir = addritem_folder_path( folder, TRUE );
878 fprintf( stream, "<p class=\"fmt-folder\">" );
879 fprintf( stream, "%s: ", _( "Folder" ) );
880 exporthtml_fmt_folderhead( stream, listHeir );
881 fprintf( stream, "</p>\n" );
882 g_list_free( listHeir );
886 ret1 = exporthtml_fmt_person( ctl, stream, folder );
887 ret2 = exporthtml_fmt_group( ctl, stream, folder, ret1 );
889 node = list = exporthtml_sort_name( folder->listFolder );
891 AddrItemObject *aio = node->data;
892 if( aio && aio->type == ITEMTYPE_FOLDER ) {
893 ItemFolder *subFolder = ( ItemFolder * ) aio;
894 exporthtml_fmt_folder( ctl, stream, subFolder );
896 node = g_list_next( node );
904 * Format header block.
905 * Enter: ctl Export control data.
906 * stream Output stream.
909 static void exporthtml_fmt_header(
910 ExportHtmlCtl *ctl, FILE *stream, gchar *title )
912 StylesheetEntry *entry;
914 entry = exporthtml_find_stylesheet( ctl );
917 "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n" );
919 "\"http://www.w3.org/TR/html4/loose.dtd\">\n" );
920 fprintf( stream, "<html>\n" );
921 fprintf( stream, "<head>\n" );
923 if( ctl->encoding && strlen( ctl->encoding ) > 0 ) {
924 fprintf( stream, "<meta " );
925 fprintf( stream, "http-equiv=\"Content-Type\" " );
926 fprintf( stream, "content=\"text/html; charset=%s\">\n",
930 fprintf( stream, "<title>%s</title>\n", title );
932 if( entry != NULL ) {
933 if( entry->fileName && strlen( entry->fileName ) > 0 ) {
934 fprintf( stream, "<link " );
935 fprintf( stream, "rel=\"stylesheet\" " );
936 fprintf( stream, "type=\"text/css\" " );
937 fprintf( stream, "href=\"%s\" >\n", entry->fileName );
940 fprintf( stream, "</head>\n" );
944 * ============================================================================
945 * Export address book to HTML file.
946 * Enter: ctl Export control data.
947 * cache Address book/data source cache.
949 * ============================================================================
951 void exporthtml_process(
952 ExportHtmlCtl *ctl, AddressCache *cache )
954 ItemFolder *rootFolder;
961 htmlFile = g_fopen( ctl->path, "wb" );
963 /* Cannot open file */
964 g_print( "Cannot open file for write\n" );
965 ctl->retVal = MGU_OPEN_FILE;
969 title = _( "Claws Mail Address Book" );
970 rootFolder = cache->rootFolder;
971 dsName = cache->name;
973 exporthtml_fmt_header( ctl, htmlFile, title );
975 fprintf( htmlFile, "<body>\n" );
976 fprintf( htmlFile, "<h1>%s</h1>\n", title );
978 fprintf( htmlFile, "<p class=\"fmt-folder\">" );
979 fprintf( htmlFile, "%s: ", _( "Address Book" ) );
980 fprintf( htmlFile, "%s", dsName );
981 fprintf( htmlFile, "</p>\n" );
983 exporthtml_fmt_folder( ctl, htmlFile, rootFolder );
986 fprintf( htmlFile, "<p>%s</p>\n", ctime_r( &tt, buf ) );
987 fprintf( htmlFile, "<hr width=\"100%%\">\n" );
989 fprintf( htmlFile, "</body>\n" );
990 fprintf( htmlFile, "</html>\n" );
993 ctl->retVal = MGU_SUCCESS;
995 /* Create stylesheet files */
996 exporthtml_create_css_files( ctl );
1001 * Build full export file specification.
1002 * Enter: ctl Export control data.
1004 static void exporthtml_build_filespec( ExportHtmlCtl *ctl ) {
1007 fileSpec = g_strconcat(
1008 ctl->dirOutput, G_DIR_SEPARATOR_S, ctl->fileHtml, NULL );
1009 ctl->path = mgu_replace_string( ctl->path, fileSpec );
1014 * ============================================================================
1015 * Parse directory and filename from full export file specification.
1016 * Enter: ctl Export control data.
1017 * fileSpec File spec.
1018 * ============================================================================
1020 void exporthtml_parse_filespec( ExportHtmlCtl *ctl, gchar *fileSpec ) {
1022 gchar *base = g_path_get_basename(fileSpec);
1025 mgu_replace_string( ctl->fileHtml, base );
1027 t = g_path_get_dirname( fileSpec );
1028 ctl->dirOutput = mgu_replace_string( ctl->dirOutput, t );
1030 ctl->path = mgu_replace_string( ctl->path, fileSpec );
1034 * ============================================================================
1035 * Test whether directory exists.
1036 * Enter: ctl Export control data.
1037 * Return: TRUE if exists.
1038 * ============================================================================
1040 gboolean exporthtml_test_dir( ExportHtmlCtl *ctl ) {
1045 if((dp = opendir( ctl->dirOutput )) != NULL) {
1053 * ============================================================================
1054 * Create output directory.
1055 * Enter: ctl Export control data.
1056 * Return: TRUE if directory created.
1057 * ============================================================================
1059 gboolean exporthtml_create_dir( ExportHtmlCtl *ctl ) {
1060 gboolean retVal = FALSE;
1063 if( mkdir( ctl->dirOutput, S_IRWXU ) == 0 ) {
1067 ctl->rcCreate = errno;
1073 * ============================================================================
1074 * Retrieve create directory error message.
1075 * Enter: ctl Export control data.
1077 * ============================================================================
1079 gchar *exporthtml_get_create_msg( ExportHtmlCtl *ctl ) {
1082 if( ctl->rcCreate == EEXIST ) {
1083 msg = _( "Name already exists but is not a directory." );
1085 else if( ctl->rcCreate == EACCES ) {
1086 msg = _( "No permissions to create directory." );
1088 else if( ctl->rcCreate == ENAMETOOLONG ) {
1089 msg = _( "Name is too long." );
1092 msg = _( "Not specified." );
1098 * Set default values.
1099 * Enter: ctl Export control data.
1101 static void exporthtml_default_values( ExportHtmlCtl *ctl ) {
1105 get_home_dir(), G_DIR_SEPARATOR_S,
1106 DFL_DIR_CLAWS_OUT, NULL );
1108 ctl->dirOutput = mgu_replace_string( ctl->dirOutput, str );
1112 mgu_replace_string( ctl->fileHtml, DFL_FILE_CLAWS_OUT );
1113 ctl->encoding = NULL;
1114 ctl->stylesheet = EXPORT_HTML_ID_DEFAULT;
1115 ctl->nameFormat = EXPORT_HTML_FIRST_LAST;
1116 ctl->banding = TRUE;
1117 ctl->linkEMail = TRUE;
1118 ctl->showAttribs = TRUE;
1119 ctl->retVal = MGU_SUCCESS;
1123 * ============================================================================
1124 * Load settings from XML properties file.
1125 * Enter: ctl Export control data.
1126 * ============================================================================
1128 void exporthtml_load_settings( ExportHtmlCtl *ctl ) {
1134 props = xmlprops_create();
1135 xmlprops_set_path( props, ctl->settingsFile );
1136 rc = xmlprops_load_file( props );
1139 xmlprops_get_property_s( props, EXMLPROP_DIRECTORY, buf );
1140 ctl->dirOutput = mgu_replace_string( ctl->dirOutput, buf );
1142 xmlprops_get_property_s( props, EXMLPROP_FILE, buf );
1143 ctl->fileHtml = mgu_replace_string( ctl->fileHtml, buf );
1146 xmlprops_get_property_i( props, EXMLPROP_STYLESHEET );
1148 xmlprops_get_property_i( props, EXMLPROP_FMT_NAME );
1150 xmlprops_get_property_b( props, EXMLPROP_BANDING );
1152 xmlprops_get_property_b( props, EXMLPROP_FMT_EMAIL );
1154 xmlprops_get_property_b( props, EXMLPROP_FMT_ATTRIB );
1157 /* Set default values */
1158 exporthtml_default_values( ctl );
1160 exporthtml_build_filespec( ctl );
1161 /* exporthtml_print( ctl, stdout ); */
1163 xmlprops_free( props );
1167 * ============================================================================
1168 * Save settings to XML properties file.
1169 * Enter: ctl Export control data.
1170 * ============================================================================
1172 void exporthtml_save_settings( ExportHtmlCtl *ctl ) {
1175 props = xmlprops_create();
1176 xmlprops_set_path( props, ctl->settingsFile );
1178 xmlprops_set_property( props, EXMLPROP_DIRECTORY, ctl->dirOutput );
1179 xmlprops_set_property( props, EXMLPROP_FILE, ctl->fileHtml );
1180 xmlprops_set_property_i( props, EXMLPROP_STYLESHEET, ctl->stylesheet );
1181 xmlprops_set_property_i( props, EXMLPROP_FMT_NAME, ctl->nameFormat );
1182 xmlprops_set_property_b( props, EXMLPROP_BANDING, ctl->banding );
1183 xmlprops_set_property_b( props, EXMLPROP_FMT_EMAIL, ctl->linkEMail );
1184 xmlprops_set_property_b( props, EXMLPROP_FMT_ATTRIB, ctl->showAttribs );
1185 if (xmlprops_save_file( props ) != MGU_SUCCESS)
1186 g_warning("can't save settings");
1187 xmlprops_free( props );
1191 * ============================================================================
1193 * ============================================================================