2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2002-2011 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.
38 #include <glib/gi18n.h>
46 #include "exporthtml.h"
49 #ifdef MKDIR_TAKES_ONE_ARG
51 #define mkdir(a,b) mkdir(a)
54 #define DFL_DIR_CLAWS_OUT "claws-mail-out"
55 #define DFL_FILE_CLAWS_OUT "addressbook.html"
57 #define FMT_BUFSIZE 2048
58 #define SC_HTML_SPACE " "
60 #define CELL_PADDING 2
61 #define CELL_SPACING 2
62 #define CHAR_ENCODING "UTF-8"
64 /* Stylesheet names */
65 #define FILENAME_NONE ""
66 #define FILENAME_DEFAULT "claws-mail.css"
67 #define FILENAME_FULL "full.css"
68 #define FILENAME_CUSTOM "custom.css"
69 #define FILENAME_CUSTOM2 "custom2.css"
70 #define FILENAME_CUSTOM3 "custom3.css"
71 #define FILENAME_CUSTOM4 "custom4.css"
73 /* Settings - properties */
74 #define EXML_PROPFILE_NAME "exporthtml.xml"
75 #define EXMLPROP_DIRECTORY "directory"
76 #define EXMLPROP_FILE "file"
77 #define EXMLPROP_STYLESHEET "stylesheet"
78 #define EXMLPROP_FMT_NAME "format-full-name"
79 #define EXMLPROP_FMT_EMAIL "format-email-links"
80 #define EXMLPROP_FMT_ATTRIB "format-attributes"
81 #define EXMLPROP_BANDING "color-banding"
82 #define EXMLPROP_VALUE_YES "y"
83 #define EXMLPROP_VALUE_NO "n"
85 static gchar *_idTagRowEven_ = "tab-row0";
86 static gchar *_idTagRowOdd_ = "tab-row1";
91 typedef struct _StylesheetEntry StylesheetEntry;
92 struct _StylesheetEntry {
99 * Build stylesheet entry.
100 * Enter: ctl Export control data.
105 static void exporthtml_build_entry(
106 ExportHtmlCtl *ctl, const gchar *file, const gint id,
109 StylesheetEntry *entry;
111 entry = g_new0( StylesheetEntry, 1 );
112 entry->fileName = g_strdup( file );
114 entry->dflValue = dfl;
115 ctl->listStyle = g_list_append( ctl->listStyle, entry );
119 * Free up object by releasing internal memory.
120 * Enter: ctl Export control data.
122 ExportHtmlCtl *exporthtml_create( void ) {
123 ExportHtmlCtl *ctl = g_new0( ExportHtmlCtl, 1 );
126 ctl->dirOutput = NULL;
127 ctl->fileHtml = NULL;
128 ctl->encoding = g_strconcat(CHAR_ENCODING, NULL);
129 ctl->stylesheet = EXPORT_HTML_ID_NONE;
130 ctl->nameFormat = EXPORT_HTML_FIRST_LAST;
131 ctl->banding = FALSE;
132 ctl->linkEMail = FALSE;
133 ctl->showAttribs = FALSE;
134 ctl->retVal = MGU_SUCCESS;
135 ctl->listStyle = NULL;
137 ctl->settingsFile = g_strconcat(
138 get_rc_dir(), G_DIR_SEPARATOR_S, EXML_PROPFILE_NAME, NULL );
140 /* Build stylesheet list */
141 exporthtml_build_entry(
142 ctl, FILENAME_NONE, EXPORT_HTML_ID_NONE, FALSE );
143 exporthtml_build_entry(
144 ctl, FILENAME_DEFAULT, EXPORT_HTML_ID_DEFAULT, TRUE );
145 exporthtml_build_entry(
146 ctl, FILENAME_FULL, EXPORT_HTML_ID_FULL, FALSE );
147 exporthtml_build_entry(
148 ctl, FILENAME_CUSTOM, EXPORT_HTML_ID_CUSTOM, FALSE );
149 exporthtml_build_entry(
150 ctl, FILENAME_CUSTOM2, EXPORT_HTML_ID_CUSTOM2, FALSE );
151 exporthtml_build_entry(
152 ctl, FILENAME_CUSTOM3, EXPORT_HTML_ID_CUSTOM3, FALSE );
153 exporthtml_build_entry(
154 ctl, FILENAME_CUSTOM4, EXPORT_HTML_ID_CUSTOM4, FALSE );
160 * Free up object by releasing internal memory.
161 * Enter: ctl Export control data.
163 void exporthtml_free( ExportHtmlCtl *ctl ) {
165 StylesheetEntry *entry;
167 cm_return_if_fail( ctl != NULL );
169 /* Free stylesheet list */
170 node = ctl->listStyle;
172 entry = ( StylesheetEntry * ) node->data;
173 g_free( entry->fileName );
174 entry->fileName = NULL;
176 entry->dflValue = FALSE;
179 node = g_list_next( node );
181 g_list_free( ctl->listStyle );
182 ctl->listStyle = NULL;
185 g_free( ctl->fileHtml );
186 g_free( ctl->encoding );
187 g_free( ctl->dirOutput );
188 g_free( ctl->settingsFile );
192 ctl->dirOutput = NULL;
193 ctl->fileHtml = NULL;
194 ctl->encoding = NULL;
195 ctl->stylesheet = EXPORT_HTML_ID_NONE;
196 ctl->nameFormat = EXPORT_HTML_FIRST_LAST;
197 ctl->banding = FALSE;
198 ctl->linkEMail = FALSE;
199 ctl->showAttribs = FALSE;
200 ctl->retVal = MGU_SUCCESS;
203 /* Now release object */
209 * Enter: ctl Export control data.
210 * Return: Stylesheet object, or NULL if nothing found. If a default entry is
211 * found in list, it will be returned.
213 static StylesheetEntry *exporthtml_find_stylesheet( ExportHtmlCtl *ctl ) {
214 StylesheetEntry *retVal = NULL;
215 StylesheetEntry *entry;
218 node = ctl->listStyle;
220 entry = ( StylesheetEntry * ) node->data;
221 if( entry->id == ctl->stylesheet ) return entry;
222 if( entry->dflValue ) retVal = entry;
223 node = g_list_next( node );
228 void exporthtml_set_stylesheet( ExportHtmlCtl *ctl, const gint value ) {
229 cm_return_if_fail( ctl != NULL );
230 ctl->stylesheet = value;
232 void exporthtml_set_name_format( ExportHtmlCtl *ctl, const gint value ) {
233 cm_return_if_fail( ctl != NULL );
234 ctl->nameFormat = value;
236 void exporthtml_set_banding( ExportHtmlCtl *ctl, const gboolean value ) {
237 cm_return_if_fail( ctl != NULL );
238 ctl->banding = value;
240 void exporthtml_set_link_email( ExportHtmlCtl *ctl, const gboolean value ) {
241 cm_return_if_fail( ctl != NULL );
242 ctl->linkEMail = value;
244 void exporthtml_set_attributes( ExportHtmlCtl *ctl, const gboolean value ) {
245 cm_return_if_fail( ctl != NULL );
246 ctl->showAttribs = value;
250 * Create default CSS file.
251 * Enter: fileSpec File to create.
252 * Return: Status code.
254 static gint exporthtml_create_css_dfl( const gchar *fileSpec ) {
257 cssFile = g_fopen( fileSpec, "rb" );
262 cssFile = g_fopen( fileSpec, "wb" );
264 return MGU_OPEN_FILE;
267 fprintf( cssFile, "body {\n\tbackground: #ffffe0;\n" );
268 fprintf( cssFile, "\tfont-family: lucida, helvetica, sans-serif;\n" );
269 fprintf( cssFile, "\tfont-size: 10pt;\n" );
270 fprintf( cssFile, "}\n" );
271 fprintf( cssFile, "h1 {\n" );
272 fprintf( cssFile, "\tcolor: #000000;\n" );
273 fprintf( cssFile, "\ttext-align: center;\n" );
274 fprintf( cssFile, "}\n" );
275 fprintf( cssFile, "th {\n" );
276 fprintf( cssFile, "\tfont-size: 10pt;\n" );
277 fprintf( cssFile, "}\n" );
278 fprintf( cssFile, "td {\n" );
279 fprintf( cssFile, "\tfont-size: 10pt;\n" );
280 fprintf( cssFile, "}\n" );
281 fprintf( cssFile, ".fmt-folder {\n" );
282 fprintf( cssFile, "\tcolor: #0000ff;\n" );
283 fprintf( cssFile, "\tfont-size: 18pt;\n" );
284 fprintf( cssFile, "\tfont-weight: bold;\n" );
285 fprintf( cssFile, "}\n" );
286 fprintf( cssFile, ".tab-head {\n" );
287 fprintf( cssFile, "\tbackground: #80c0f0;\n" );
288 fprintf( cssFile, "}\n" );
289 fprintf( cssFile, ".tab-dn {\n" );
290 fprintf( cssFile, "}\n" );
291 fprintf( cssFile, ".tab-addr {\n" );
292 fprintf( cssFile, "\tfont-style: italic;\n" );
293 fprintf( cssFile, "}\n" );
294 fprintf( cssFile, ".tab-email {\n" );
295 fprintf( cssFile, "\tfont-weight: bold;\n" );
296 fprintf( cssFile, "\tfont-style: italic;\n" );
297 fprintf( cssFile, "}\n" );
298 fprintf( cssFile, ".tab-fn {\n" );
299 fprintf( cssFile, "}\n" );
300 fprintf( cssFile, ".tab-attr {\n" );
301 fprintf( cssFile, "}\n" );
308 * Create full CSS file.
309 * Enter: fileSpec File to create.
310 * Return: Status code.
312 static gint exporthtml_create_css_full( const gchar *fileSpec ) {
315 cssFile = g_fopen( fileSpec, "rb" );
320 cssFile = g_fopen( fileSpec, "wb" );
322 return MGU_OPEN_FILE;
325 fprintf( cssFile, "body {\n\tbackground: #ffffe0;\n" );
326 fprintf( cssFile, "\tfont-family: lucida, helvetica, sans-serif;\n" );
327 fprintf( cssFile, "\tfont-size: 10pt;\n" );
328 fprintf( cssFile, "}\n" );
329 fprintf( cssFile, "h1 {\n" );
330 fprintf( cssFile, "\tcolor: #000000;\n" );
331 fprintf( cssFile, "\ttext-align: center;\n" );
332 fprintf( cssFile, "}\n" );
333 fprintf( cssFile, "th {\n" );
334 fprintf( cssFile, "\tfont-size: 10pt;\n" );
335 fprintf( cssFile, "}\n" );
336 fprintf( cssFile, "td {\n" );
337 fprintf( cssFile, "\tfont-size: 10pt;\n" );
338 fprintf( cssFile, "}\n" );
339 fprintf( cssFile, ".fmt-folder {\n" );
340 fprintf( cssFile, "\tcolor: #0000ff;\n" );
341 fprintf( cssFile, "\tfont-size: 18pt;\n" );
342 fprintf( cssFile, "\tfont-weight: bold;\n" );
343 fprintf( cssFile, "}\n" );
344 fprintf( cssFile, ".tab-head {\n" );
345 fprintf( cssFile, "\tbackground: #80c0f0;\n" );
346 fprintf( cssFile, "}\n" );
347 fprintf( cssFile, ".tab-row0 {\n" );
348 fprintf( cssFile, "\tbackground: #f0f0f0;\n" );
349 fprintf( cssFile, "}\n" );
350 fprintf( cssFile, ".tab-row1 {\n" );
351 fprintf( cssFile, "\tbackground: #d0d0d0;\n" );
352 fprintf( cssFile, "}\n" );
353 fprintf( cssFile, ".tab-dn {\n" );
354 fprintf( cssFile, "}\n" );
355 fprintf( cssFile, ".tab-addr {\n" );
356 fprintf( cssFile, "\tfont-style: italic;\n" );
357 fprintf( cssFile, "}\n" );
358 fprintf( cssFile, ".tab-email {\n" );
359 fprintf( cssFile, "\tfont-weight: bold;\n" );
360 fprintf( cssFile, "\tfont-style: italic;\n" );
361 fprintf( cssFile, "}\n" );
362 fprintf( cssFile, ".tab-fn {\n" );
363 fprintf( cssFile, "}\n" );
364 fprintf( cssFile, ".tab-attr {\n" );
365 fprintf( cssFile, "}\n" );
372 * Create stylesheet files.
373 * Enter: ctl Export control data.
375 static void exporthtml_create_css_files( ExportHtmlCtl *ctl ) {
379 node = ctl->listStyle;
381 StylesheetEntry *entry = node->data;
382 node = g_list_next( node );
383 if( strlen( entry->fileName ) ) {
384 fileSpec = g_strconcat(
385 ctl->dirOutput, G_DIR_SEPARATOR_S,
386 entry->fileName, NULL );
387 if( entry->id == EXPORT_HTML_ID_DEFAULT ) {
388 exporthtml_create_css_dfl( fileSpec );
390 else if( entry->id != EXPORT_HTML_ID_NONE ) {
391 exporthtml_create_css_full( fileSpec );
399 * Comparison using linked list elements.
401 static gint exporthtml_compare_name(
402 gconstpointer ptr1, gconstpointer ptr2 )
404 const AddrItemObject *item1 = ptr1;
405 const AddrItemObject *item2 = ptr2;
406 const gchar *name1 = NULL, *name2 = NULL;
407 if( item1 ) name1 = ADDRITEM_NAME( item1 );
408 if( item2 ) name2 = ADDRITEM_NAME( item2 );
409 if( ! name1 ) return ( name2 != NULL );
410 if( ! name2 ) return -1;
411 return g_utf8_collate( name1, name2 );
415 * Comparison using linked list elements.
417 static gint exporthtml_compare_email(
418 gconstpointer ptr1, gconstpointer ptr2 )
420 const ItemEMail *email1 = ptr1;
421 const ItemEMail *email2 = ptr2;
422 const gchar *name1 = NULL, *name2 = NULL;
423 if( email1 ) name1 = email1->address;
424 if( email2 ) name2 = email2->address;
425 if( ! name1 ) return ( name2 != NULL );
426 if( ! name2 ) return -1;
427 return g_utf8_collate( name1, name2 );
431 * Comparison using linked list elements.
433 static gint exporthtml_compare_attrib(
434 gconstpointer ptr1, gconstpointer ptr2 )
436 const UserAttribute *attr1 = ptr1;
437 const UserAttribute *attr2 = ptr2;
438 const gchar *name1 = NULL, *name2 = NULL;
439 if( attr1 ) name1 = attr1->name;
440 if( attr2 ) name2 = attr2->name;
441 if( ! name1 ) return ( name2 != NULL );
442 if( ! name2 ) return -1;
443 return g_utf8_collate( name1, name2 );
447 * Build sorted list of named items.
448 * Enter: list List of items to sorted.
449 * Return: Sorted list.
450 * Note: List should freed after use. Items referenced by list should not be
451 * freed since they are managed by the address cache.
453 static GList *exporthtml_sort_name( const GList *list ) {
455 GList *sorted = NULL;
459 sorted = g_list_insert_sorted(
460 sorted, node->data, exporthtml_compare_name );
461 node = g_list_next( node );
467 * Build sorted list of email items.
468 * Enter: list List of E-Mail items to sorted.
469 * Return: Sorted list.
470 * Note: List should freed after use. Items referenced by list should not be
471 * freed since they are managed by the address cache.
473 static GList *exporthtml_sort_email( const GList *list ) {
475 GList *sorted = NULL;
479 sorted = g_list_insert_sorted(
480 sorted, node->data, exporthtml_compare_email );
481 node = g_list_next( node );
487 * Build sorted list of attributes.
488 * Enter: list List of items to sorted.
489 * Return: Sorted list.
490 * Note: List should freed after use. Items referenced by list should not be
491 * freed since they are managed by the address cache.
493 static GList *exporthtml_sort_attrib( const GList *list ) {
495 GList *sorted = NULL;
500 sorted = g_list_insert_sorted(
501 sorted, node->data, exporthtml_compare_attrib );
502 node = g_list_next( node );
508 * Format a list of E-Mail addresses.
509 * Enter: ctl Export control data.
510 * stream Output stream.
511 * listEMail List of addresses.
512 * sortFlag Set to TRUE if address list should be sorted.
514 static void exporthtml_fmt_email(
515 ExportHtmlCtl *ctl, FILE *stream, const GList *listEMail,
522 if( listEMail == NULL ) {
523 fprintf( stream, SC_HTML_SPACE );
529 node = list = exporthtml_sort_email( listEMail );
536 ItemEMail *email = ( ItemEMail * ) node->data;
537 node = g_list_next( node );
539 name = ADDRITEM_NAME( email );
541 fprintf( stream, "%s ", name );
543 if( ctl->linkEMail ) {
544 fprintf( stream, "<a href=\"mailto:%s\">",
547 fprintf( stream, "<span class=\"tab-email\">" );
548 fprintf( stream, "%s", email->address );
549 fprintf( stream, "</span>" );
550 if( ctl->linkEMail ) {
551 fprintf( stream, "</a>" );
553 if( email->remarks ) {
554 if( strlen( email->remarks ) ) {
555 fprintf( stream, " (%s)", email->remarks );
558 fprintf( stream, "<br>\n" );
564 * Format groups in an address book folder.
565 * Enter: ctl Export control data.
566 * stream Output stream.
568 * prevFlag If FALSE, list of persons were output.
569 * Return: TRUE if no groups were formatted.
571 static gboolean exporthtml_fmt_group(
572 ExportHtmlCtl *ctl, FILE *stream, const ItemFolder *folder,
575 gboolean retVal, band;
577 const gchar *tagName;
580 if( folder->listGroup == NULL ) return retVal;
582 /* Write separator */
584 fprintf( stream, "<br>\n" );
587 /* Write table headers */
588 fprintf( stream, "<table" );
589 fprintf( stream, " border=\"%d\"", BORDER_SIZE );
590 fprintf( stream, " cellpadding=\"%d\"", CELL_PADDING );
591 fprintf( stream, " cellspacing=\"%d\"", CELL_SPACING );
592 fprintf( stream, ">\n" );
594 fprintf( stream, "<tr class=\"tab-head\">\n" );
595 fprintf( stream, " <th width=\"200\">" );
596 fprintf( stream, "%s", _( "Group Name" ) );
597 fprintf( stream, "</th>\n" );
598 fprintf( stream, " <th width=\"300\">" );
599 fprintf( stream, "%s", _( "Email Address" ) );
600 fprintf( stream, "</th>\n" );
601 fprintf( stream, "</tr>\n" );
602 list = exporthtml_sort_name( folder->listGroup );
607 AddrItemObject *aio = node->data;
608 if( aio && aio->type == ITEMTYPE_GROUP ) {
609 ItemGroup *group = ( ItemGroup * ) aio;
611 fprintf( stream, "<tr valign=\"top\"" );
614 tagName = _idTagRowOdd_;
617 tagName = _idTagRowEven_;
619 fprintf( stream, " class=\"%s\"", tagName );
622 fprintf( stream, "\">\n" );
624 fprintf( stream, " <td class=\"tab-dn\">" );
625 fprintf( stream, "%s", ADDRITEM_NAME( group ) );
626 fprintf( stream, "</td>\n" );
627 fprintf( stream, " <td class=\"tab-addr\">" );
628 exporthtml_fmt_email( ctl, stream, group->listEMail, TRUE );
629 fprintf( stream, "</td>\n" );
630 fprintf( stream, "</tr>\n" );
633 node = g_list_next( node );
637 fprintf( stream, "</table>\n" );
642 * Format a list of E-Mail addresses.
643 * Enter: ctl Export control data.
644 * stream Output stream.
645 * listAttr List of attributes.
647 static void exporthtml_fmt_attribs(
648 ExportHtmlCtl *ctl, FILE *stream, const GList *listAttr )
653 if( listAttr == NULL ) {
654 fprintf( stream, SC_HTML_SPACE );
658 fprintf( stream, "<table border=\"0\">\n" );
659 node = list = exporthtml_sort_attrib( listAttr );
661 UserAttribute *attr = ( UserAttribute * ) node->data;
662 node = g_list_next( node );
663 fprintf( stream, "<tr valign=\"top\">" );
664 fprintf( stream, "<td align=\"right\">%s:</td>", attr->name );
665 fprintf( stream, "<td>%s</td>", attr->value );
666 fprintf( stream, "</tr>\n" );
670 fprintf( stream, "</table>" );
675 * Enter: ctl Export control data.
677 * person Person to format.
679 static void exporthtml_fmt_fullname(
680 ExportHtmlCtl *ctl, gchar *buf, const ItemPerson *person )
684 if( ctl->nameFormat == EXPORT_HTML_LAST_FIRST ) {
686 if( person->lastName ) {
687 if( *person->lastName ) {
689 strcat( buf, person->lastName );
693 if( person->firstName ) {
694 if( *person->firstName ) {
698 strcat( buf, person->firstName );
703 if( person->firstName ) {
704 if( *person->firstName ) {
705 strcat( buf, person->firstName );
708 if( person->lastName ) {
709 if( *person->lastName ) {
711 strcat( buf, person->lastName );
718 if( *buf ) flag = TRUE;
719 if( person->nickName ) {
720 if( strlen( person->nickName ) ) {
724 strcat( buf, person->nickName );
734 * Format persons in an address book folder.
735 * Enter: ctl Export control data.
736 * stream Output stream.
738 * Return: TRUE if no persons were formatted.
740 static gboolean exporthtml_fmt_person(
741 ExportHtmlCtl *ctl, FILE *stream, const ItemFolder *folder )
743 gboolean retVal, band;
745 gchar buf[ FMT_BUFSIZE ];
746 const gchar *tagName;
749 if( folder->listPerson == NULL ) return retVal;
751 /* Write table headers */
752 fprintf( stream, "<table" );
753 fprintf( stream, " border=\"%d\"", BORDER_SIZE );
754 fprintf( stream, " cellpadding=\"%d\"", CELL_PADDING );
755 fprintf( stream, " cellspacing=\"%d\"", CELL_SPACING );
756 fprintf( stream, ">\n" );
758 fprintf( stream, "<tr class=\"tab-head\">\n" );
759 fprintf( stream, " <th width=\"200\">" );
760 fprintf( stream, "%s", _( "Display Name" ) );
761 fprintf( stream, "</th>\n" );
762 fprintf( stream, " <th width=\"300\">" );
763 fprintf( stream, "%s", _( "Email Address" ) );
764 fprintf( stream, "</th>\n" );
765 fprintf( stream, " <th width=\"200\">" );
766 fprintf( stream, "%s", _( "Full Name" ) );
767 fprintf( stream, "</th>\n" );
768 if( ctl->showAttribs ) {
769 fprintf( stream, " <th width=\"250\">" );
770 fprintf( stream, "%s", _( "Attributes" ) );
771 fprintf( stream, "</th>\n" );
773 fprintf( stream, "</tr>\n" );
776 node = list = exporthtml_sort_name( folder->listPerson );
778 AddrItemObject *aio = node->data;
779 if( aio && aio->type == ITEMTYPE_PERSON ) {
780 ItemPerson *person = ( ItemPerson * ) aio;
782 /* Format first/last/nick name */
784 exporthtml_fmt_fullname( ctl, buf,person );
786 fprintf( stream, "<tr valign=\"top\"" );
789 tagName = _idTagRowOdd_;
792 tagName = _idTagRowEven_;
794 fprintf( stream, " class=\"%s\"", tagName );
797 fprintf( stream, ">\n" );
799 fprintf( stream, " <td class=\"tab-dn\">" );
800 fprintf( stream, "%s", ADDRITEM_NAME( person ) );
801 fprintf( stream, "</td>\n" );
803 fprintf( stream, " <td class=\"tab-addr\">" );
804 exporthtml_fmt_email( ctl, stream, person->listEMail, FALSE );
805 fprintf( stream, "</td>\n" );
807 fprintf( stream, " <td class=\"tab-fn\">" );
809 fprintf( stream, "%s", buf );
812 fprintf( stream, "%s", SC_HTML_SPACE );
814 fprintf( stream, "</td>\n" );
816 if( ctl->showAttribs ) {
817 fprintf( stream, " <td class=\"tab-attr\">" );
818 exporthtml_fmt_attribs(
819 ctl, stream, person->listAttrib );
820 fprintf( stream, "</td>\n" );
822 fprintf( stream, "</tr>\n" );
826 node = g_list_next( node );
830 fprintf( stream, "</table>\n" );
835 * Format folder heirarchy.
836 * Enter: stream Output stream.
837 * list Heirarchy list.
839 static void exporthtml_fmt_folderhead( FILE *stream, const GList *list ) {
847 AddrItemObject *aio = node->data;
848 if( aio && aio->type == ITEMTYPE_FOLDER ) {
849 ItemFolder *folder = ( ItemFolder * ) aio;
851 name = ADDRITEM_NAME( folder );
854 fprintf( stream, " > " );
856 fprintf( stream, "%s", name );
860 node = g_list_next( node );
865 * Format an address book folder.
866 * Enter: ctl Export control data.
867 * stream Output stream.
870 static void exporthtml_fmt_folder(
871 ExportHtmlCtl *ctl, FILE *stream, const ItemFolder *folder )
874 GList *listHeir, *list;
878 name = ADDRITEM_NAME( folder );
880 listHeir = addritem_folder_path( folder, TRUE );
882 fprintf( stream, "<p class=\"fmt-folder\">" );
883 fprintf( stream, "%s: ", _( "Folder" ) );
884 exporthtml_fmt_folderhead( stream, listHeir );
885 fprintf( stream, "</p>\n" );
886 g_list_free( listHeir );
890 ret1 = exporthtml_fmt_person( ctl, stream, folder );
891 ret2 = exporthtml_fmt_group( ctl, stream, folder, ret1 );
893 node = list = exporthtml_sort_name( folder->listFolder );
895 AddrItemObject *aio = node->data;
896 if( aio && aio->type == ITEMTYPE_FOLDER ) {
897 ItemFolder *subFolder = ( ItemFolder * ) aio;
898 exporthtml_fmt_folder( ctl, stream, subFolder );
900 node = g_list_next( node );
908 * Format header block.
909 * Enter: ctl Export control data.
910 * stream Output stream.
913 static void exporthtml_fmt_header(
914 ExportHtmlCtl *ctl, FILE *stream, gchar *title )
916 StylesheetEntry *entry;
918 entry = exporthtml_find_stylesheet( ctl );
921 "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n" );
923 "\"http://www.w3.org/TR/html4/loose.dtd\">\n" );
924 fprintf( stream, "<html>\n" );
925 fprintf( stream, "<head>\n" );
927 if( ctl->encoding && strlen( ctl->encoding ) > 0 ) {
928 fprintf( stream, "<meta " );
929 fprintf( stream, "http-equiv=\"Content-Type\" " );
930 fprintf( stream, "content=\"text/html; charset=%s\">\n",
934 fprintf( stream, "<title>%s</title>\n", title );
936 if( entry != NULL ) {
937 if( entry->fileName && strlen( entry->fileName ) > 0 ) {
938 fprintf( stream, "<link " );
939 fprintf( stream, "rel=\"stylesheet\" " );
940 fprintf( stream, "type=\"text/css\" " );
941 fprintf( stream, "href=\"%s\" >\n", entry->fileName );
944 fprintf( stream, "</head>\n" );
948 * ============================================================================
949 * Export address book to HTML file.
950 * Enter: ctl Export control data.
951 * cache Address book/data source cache.
953 * ============================================================================
955 void exporthtml_process(
956 ExportHtmlCtl *ctl, AddressCache *cache )
958 ItemFolder *rootFolder;
965 htmlFile = g_fopen( ctl->path, "wb" );
967 /* Cannot open file */
968 g_print( "Cannot open file for write\n" );
969 ctl->retVal = MGU_OPEN_FILE;
973 title = _( "Claws Mail Address Book" );
974 rootFolder = cache->rootFolder;
975 dsName = cache->name;
977 exporthtml_fmt_header( ctl, htmlFile, title );
979 fprintf( htmlFile, "<body>\n" );
980 fprintf( htmlFile, "<h1>%s</h1>\n", title );
982 fprintf( htmlFile, "<p class=\"fmt-folder\">" );
983 fprintf( htmlFile, "%s: ", _( "Address Book" ) );
984 fprintf( htmlFile, "%s", dsName );
985 fprintf( htmlFile, "</p>\n" );
987 exporthtml_fmt_folder( ctl, htmlFile, rootFolder );
990 fprintf( htmlFile, "<p>%s</p>\n", ctime_r( &tt, buf ) );
991 fprintf( htmlFile, "<hr width=\"100%%\">\n" );
993 fprintf( htmlFile, "</body>\n" );
994 fprintf( htmlFile, "</html>\n" );
997 ctl->retVal = MGU_SUCCESS;
999 /* Create stylesheet files */
1000 exporthtml_create_css_files( ctl );
1005 * Build full export file specification.
1006 * Enter: ctl Export control data.
1008 static void exporthtml_build_filespec( ExportHtmlCtl *ctl ) {
1011 fileSpec = g_strconcat(
1012 ctl->dirOutput, G_DIR_SEPARATOR_S, ctl->fileHtml, NULL );
1013 ctl->path = mgu_replace_string( ctl->path, fileSpec );
1018 * ============================================================================
1019 * Parse directory and filename from full export file specification.
1020 * Enter: ctl Export control data.
1021 * fileSpec File spec.
1022 * ============================================================================
1024 void exporthtml_parse_filespec( ExportHtmlCtl *ctl, gchar *fileSpec ) {
1026 gchar *base = g_path_get_basename(fileSpec);
1029 mgu_replace_string( ctl->fileHtml, base );
1031 t = g_path_get_dirname( fileSpec );
1032 ctl->dirOutput = mgu_replace_string( ctl->dirOutput, t );
1034 ctl->path = mgu_replace_string( ctl->path, fileSpec );
1038 * ============================================================================
1039 * Test whether directory exists.
1040 * Enter: ctl Export control data.
1041 * Return: TRUE if exists.
1042 * ============================================================================
1044 gboolean exporthtml_test_dir( ExportHtmlCtl *ctl ) {
1049 if((dp = opendir( ctl->dirOutput )) != NULL) {
1057 * ============================================================================
1058 * Create output directory.
1059 * Enter: ctl Export control data.
1060 * Return: TRUE if directory created.
1061 * ============================================================================
1063 gboolean exporthtml_create_dir( ExportHtmlCtl *ctl ) {
1064 gboolean retVal = FALSE;
1067 if( mkdir( ctl->dirOutput, S_IRWXU ) == 0 ) {
1071 ctl->rcCreate = errno;
1077 * ============================================================================
1078 * Retrieve create directory error message.
1079 * Enter: ctl Export control data.
1081 * ============================================================================
1083 gchar *exporthtml_get_create_msg( ExportHtmlCtl *ctl ) {
1086 if( ctl->rcCreate == EEXIST ) {
1087 msg = _( "Name already exists but is not a directory." );
1089 else if( ctl->rcCreate == EACCES ) {
1090 msg = _( "No permissions to create directory." );
1092 else if( ctl->rcCreate == ENAMETOOLONG ) {
1093 msg = _( "Name is too long." );
1096 msg = _( "Not specified." );
1102 * Set default values.
1103 * Enter: ctl Export control data.
1105 static void exporthtml_default_values( ExportHtmlCtl *ctl ) {
1109 get_home_dir(), G_DIR_SEPARATOR_S,
1110 DFL_DIR_CLAWS_OUT, NULL );
1112 ctl->dirOutput = mgu_replace_string( ctl->dirOutput, str );
1116 mgu_replace_string( ctl->fileHtml, DFL_FILE_CLAWS_OUT );
1117 ctl->encoding = NULL;
1118 ctl->stylesheet = EXPORT_HTML_ID_DEFAULT;
1119 ctl->nameFormat = EXPORT_HTML_FIRST_LAST;
1120 ctl->banding = TRUE;
1121 ctl->linkEMail = TRUE;
1122 ctl->showAttribs = TRUE;
1123 ctl->retVal = MGU_SUCCESS;
1127 * ============================================================================
1128 * Load settings from XML properties file.
1129 * Enter: ctl Export control data.
1130 * ============================================================================
1132 void exporthtml_load_settings( ExportHtmlCtl *ctl ) {
1138 props = xmlprops_create();
1139 xmlprops_set_path( props, ctl->settingsFile );
1140 rc = xmlprops_load_file( props );
1143 xmlprops_get_property_s( props, EXMLPROP_DIRECTORY, buf );
1144 ctl->dirOutput = mgu_replace_string( ctl->dirOutput, buf );
1146 xmlprops_get_property_s( props, EXMLPROP_FILE, buf );
1147 ctl->fileHtml = mgu_replace_string( ctl->fileHtml, buf );
1150 xmlprops_get_property_i( props, EXMLPROP_STYLESHEET );
1152 xmlprops_get_property_i( props, EXMLPROP_FMT_NAME );
1154 xmlprops_get_property_b( props, EXMLPROP_BANDING );
1156 xmlprops_get_property_b( props, EXMLPROP_FMT_EMAIL );
1158 xmlprops_get_property_b( props, EXMLPROP_FMT_ATTRIB );
1161 /* Set default values */
1162 exporthtml_default_values( ctl );
1164 exporthtml_build_filespec( ctl );
1165 /* exporthtml_print( ctl, stdout ); */
1167 xmlprops_free( props );
1171 * ============================================================================
1172 * Save settings to XML properties file.
1173 * Enter: ctl Export control data.
1174 * ============================================================================
1176 void exporthtml_save_settings( ExportHtmlCtl *ctl ) {
1179 props = xmlprops_create();
1180 xmlprops_set_path( props, ctl->settingsFile );
1182 xmlprops_set_property( props, EXMLPROP_DIRECTORY, ctl->dirOutput );
1183 xmlprops_set_property( props, EXMLPROP_FILE, ctl->fileHtml );
1184 xmlprops_set_property_i( props, EXMLPROP_STYLESHEET, ctl->stylesheet );
1185 xmlprops_set_property_i( props, EXMLPROP_FMT_NAME, ctl->nameFormat );
1186 xmlprops_set_property_b( props, EXMLPROP_BANDING, ctl->banding );
1187 xmlprops_set_property_b( props, EXMLPROP_FMT_EMAIL, ctl->linkEMail );
1188 xmlprops_set_property_b( props, EXMLPROP_FMT_ATTRIB, ctl->showAttribs );
1189 if (xmlprops_save_file( props ) != MGU_SUCCESS)
1190 g_warning("can't save settings");
1191 xmlprops_free( props );
1195 * ============================================================================
1197 * ============================================================================