2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2002-2006 Match Grun and the Sylpheed-Claws 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 2 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, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * Export address book to HTML file.
34 #include <glib/gi18n.h>
38 #include "exporthtml.h"
41 #ifdef MKDIR_TAKES_ONE_ARG
43 #define mkdir(a,b) mkdir(a)
46 #define DFL_DIR_SYLPHEED_OUT "sylpheed-out"
47 #define DFL_FILE_SYLPHEED_OUT "addressbook.html"
49 #define FMT_BUFSIZE 2048
50 #define SC_HTML_SPACE " "
52 #define CELL_PADDING 2
53 #define CELL_SPACING 2
54 #define CHAR_ENCODING "ISO-8859-1"
56 /* Stylesheet names */
57 #define FILENAME_NONE ""
58 #define FILENAME_DEFAULT "sylpheed.css"
59 #define FILENAME_FULL "full.css"
60 #define FILENAME_CUSTOM "custom.css"
61 #define FILENAME_CUSTOM2 "custom2.css"
62 #define FILENAME_CUSTOM3 "custom3.css"
63 #define FILENAME_CUSTOM4 "custom4.css"
65 /* Settings - properties */
66 #define EXML_PROPFILE_NAME "exporthtml.xml"
67 #define EXMLPROP_DIRECTORY "directory"
68 #define EXMLPROP_FILE "file"
69 #define EXMLPROP_STYLESHEET "stylesheet"
70 #define EXMLPROP_FMT_NAME "format-full-name"
71 #define EXMLPROP_FMT_EMAIL "format-email-links"
72 #define EXMLPROP_FMT_ATTRIB "format-attributes"
73 #define EXMLPROP_BANDING "color-banding"
74 #define EXMLPROP_VALUE_YES "y"
75 #define EXMLPROP_VALUE_NO "n"
77 static gchar *_idTagRowEven_ = "tab-row0";
78 static gchar *_idTagRowOdd_ = "tab-row1";
83 typedef struct _StylesheetEntry StylesheetEntry;
84 struct _StylesheetEntry {
91 * Build stylesheet entry.
92 * Enter: ctl Export control data.
97 static void exporthtml_build_entry(
98 ExportHtmlCtl *ctl, const gchar *file, const gint id,
101 StylesheetEntry *entry;
103 entry = g_new0( StylesheetEntry, 1 );
104 entry->fileName = g_strdup( file );
106 entry->dflValue = dfl;
107 ctl->listStyle = g_list_append( ctl->listStyle, entry );
111 * Free up object by releasing internal memory.
112 * Enter: ctl Export control data.
114 ExportHtmlCtl *exporthtml_create( void ) {
115 ExportHtmlCtl *ctl = g_new0( ExportHtmlCtl, 1 );
118 ctl->dirOutput = NULL;
119 ctl->fileHtml = NULL;
120 ctl->encoding = NULL;
121 ctl->stylesheet = EXPORT_HTML_ID_NONE;
122 ctl->nameFormat = EXPORT_HTML_FIRST_LAST;
123 ctl->banding = FALSE;
124 ctl->linkEMail = FALSE;
125 ctl->showAttribs = FALSE;
126 ctl->retVal = MGU_SUCCESS;
127 ctl->listStyle = NULL;
129 ctl->settingsFile = g_strconcat(
130 get_rc_dir(), G_DIR_SEPARATOR_S, EXML_PROPFILE_NAME, NULL );
132 /* Build stylesheet list */
133 exporthtml_build_entry(
134 ctl, FILENAME_NONE, EXPORT_HTML_ID_NONE, FALSE );
135 exporthtml_build_entry(
136 ctl, FILENAME_DEFAULT, EXPORT_HTML_ID_DEFAULT, TRUE );
137 exporthtml_build_entry(
138 ctl, FILENAME_FULL, EXPORT_HTML_ID_FULL, FALSE );
139 exporthtml_build_entry(
140 ctl, FILENAME_CUSTOM, EXPORT_HTML_ID_CUSTOM, FALSE );
141 exporthtml_build_entry(
142 ctl, FILENAME_CUSTOM2, EXPORT_HTML_ID_CUSTOM2, FALSE );
143 exporthtml_build_entry(
144 ctl, FILENAME_CUSTOM3, EXPORT_HTML_ID_CUSTOM3, FALSE );
145 exporthtml_build_entry(
146 ctl, FILENAME_CUSTOM4, EXPORT_HTML_ID_CUSTOM4, FALSE );
152 * Free up object by releasing internal memory.
153 * Enter: ctl Export control data.
155 void exporthtml_free( ExportHtmlCtl *ctl ) {
157 StylesheetEntry *entry;
159 g_return_if_fail( ctl != NULL );
161 /* Free stylesheet list */
162 node = ctl->listStyle;
164 entry = ( StylesheetEntry * ) node->data;
165 g_free( entry->fileName );
166 entry->fileName = NULL;
168 entry->dflValue = FALSE;
171 node = g_list_next( node );
173 g_list_free( ctl->listStyle );
174 ctl->listStyle = NULL;
177 g_free( ctl->fileHtml );
178 g_free( ctl->encoding );
179 g_free( ctl->dirOutput );
180 g_free( ctl->settingsFile );
184 ctl->dirOutput = NULL;
185 ctl->fileHtml = NULL;
186 ctl->encoding = NULL;
187 ctl->stylesheet = EXPORT_HTML_ID_NONE;
188 ctl->nameFormat = EXPORT_HTML_FIRST_LAST;
189 ctl->banding = FALSE;
190 ctl->linkEMail = FALSE;
191 ctl->showAttribs = FALSE;
192 ctl->retVal = MGU_SUCCESS;
195 /* Now release object */
200 * Print control object.
201 * Enter: ctl Export control data.
202 * stream Output stream.
204 void exporthtml_print( ExportHtmlCtl *ctl, FILE *stream ) {
205 fprintf( stream, "ExportHtmlCtl:\n" );
206 fprintf( stream, " path: %s\n", ctl->path );
207 fprintf( stream, "directory: %s\n", ctl->dirOutput );
208 fprintf( stream, " file: %s\n", ctl->fileHtml );
209 fprintf( stream, " settings: %s\n", ctl->settingsFile );
210 fprintf( stream, " encoding: %s\n", ctl->encoding );
211 fprintf( stream, " css file: %d\n", ctl->stylesheet );
212 fprintf( stream, " name fmt: %d\n", ctl->nameFormat );
213 fprintf( stream, " banding: %d\n", ctl->banding );
214 fprintf( stream, "link mail: %d\n", ctl->linkEMail );
215 fprintf( stream, "show attr: %d\n", ctl->showAttribs );
220 * Enter: ctl Export control data.
221 * Return: Stylesheet object, or NULL if nothing found. If a default entry is
222 * found in list, it will be returned.
224 static StylesheetEntry *exporthtml_find_stylesheet( ExportHtmlCtl *ctl ) {
225 StylesheetEntry *retVal = NULL;
226 StylesheetEntry *entry;
229 node = ctl->listStyle;
231 entry = ( StylesheetEntry * ) node->data;
232 if( entry->id == ctl->stylesheet ) return entry;
233 if( entry->dflValue ) retVal = entry;
234 node = g_list_next( node );
243 * Specify path to folder where files are created.
244 * Enter: ctl Export control data.
245 * value Full directory path.
247 void exporthtml_set_output_dir( ExportHtmlCtl *ctl, const gchar *value ) {
248 g_return_if_fail( ctl != NULL );
249 ctl->dirOutput = mgu_replace_string( ctl->dirOutput, value );
250 g_strstrip( ctl->dirOutput );
252 void exporthtml_set_path( ExportHtmlCtl *ctl, const gchar *value ) {
253 g_return_if_fail( ctl != NULL );
254 ctl->path = mgu_replace_string( ctl->path, value );
255 g_strstrip( ctl->path );
257 void exporthtml_set_file_html( ExportHtmlCtl *ctl, const gchar *value ) {
258 g_return_if_fail( ctl != NULL );
259 ctl->fileHtml = mgu_replace_string( ctl->fileHtml, value );
260 g_strstrip( ctl->fileHtml );
262 void exporthtml_set_encoding( ExportHtmlCtl *ctl, const gchar *value ) {
263 g_return_if_fail( ctl != NULL );
264 ctl->encoding = mgu_replace_string( ctl->encoding, value );
265 g_strstrip( ctl->encoding );
267 void exporthtml_set_stylesheet( ExportHtmlCtl *ctl, const gint value ) {
268 g_return_if_fail( ctl != NULL );
269 ctl->stylesheet = value;
271 void exporthtml_set_name_format( ExportHtmlCtl *ctl, const gint value ) {
272 g_return_if_fail( ctl != NULL );
273 ctl->nameFormat = value;
275 void exporthtml_set_banding( ExportHtmlCtl *ctl, const gboolean value ) {
276 g_return_if_fail( ctl != NULL );
277 ctl->banding = value;
279 void exporthtml_set_link_email( ExportHtmlCtl *ctl, const gboolean value ) {
280 g_return_if_fail( ctl != NULL );
281 ctl->linkEMail = value;
283 void exporthtml_set_attributes( ExportHtmlCtl *ctl, const gboolean value ) {
284 g_return_if_fail( ctl != NULL );
285 ctl->showAttribs = value;
289 * Create default CSS file.
290 * Enter: fileSpec File to create.
291 * Return: Status code.
293 static gint exporthtml_create_css_dfl( const gchar *fileSpec ) {
296 cssFile = g_fopen( fileSpec, "rb" );
301 cssFile = g_fopen( fileSpec, "wb" );
303 return MGU_OPEN_FILE;
306 fprintf( cssFile, "body {\n\tbackground: #ffffe0;\n" );
307 fprintf( cssFile, "\tfont-family: lucida, helvetica, sans-serif;\n" );
308 fprintf( cssFile, "\tfont-size: 10pt;\n" );
309 fprintf( cssFile, "}\n" );
310 fprintf( cssFile, "h1 {\n" );
311 fprintf( cssFile, "\tcolor: #000000;\n" );
312 fprintf( cssFile, "\ttext-align: center;\n" );
313 fprintf( cssFile, "}\n" );
314 fprintf( cssFile, "th {\n" );
315 fprintf( cssFile, "\tfont-size: 10pt;\n" );
316 fprintf( cssFile, "}\n" );
317 fprintf( cssFile, "td {\n" );
318 fprintf( cssFile, "\tfont-size: 10pt;\n" );
319 fprintf( cssFile, "}\n" );
320 fprintf( cssFile, ".fmt-folder {\n" );
321 fprintf( cssFile, "\tcolor: #0000ff;\n" );
322 fprintf( cssFile, "\tfont-size: 18pt;\n" );
323 fprintf( cssFile, "\tfont-weight: bold;\n" );
324 fprintf( cssFile, "}\n" );
325 fprintf( cssFile, ".tab-head {\n" );
326 fprintf( cssFile, "\tbackground: #80c0f0;\n" );
327 fprintf( cssFile, "}\n" );
328 fprintf( cssFile, ".tab-dn {\n" );
329 fprintf( cssFile, "}\n" );
330 fprintf( cssFile, ".tab-addr {\n" );
331 fprintf( cssFile, "\tfont-style: italic;\n" );
332 fprintf( cssFile, "}\n" );
333 fprintf( cssFile, ".tab-email {\n" );
334 fprintf( cssFile, "\tfont-weight: bold;\n" );
335 fprintf( cssFile, "\tfont-style: italic;\n" );
336 fprintf( cssFile, "}\n" );
337 fprintf( cssFile, ".tab-fn {\n" );
338 fprintf( cssFile, "}\n" );
339 fprintf( cssFile, ".tab-attr {\n" );
340 fprintf( cssFile, "}\n" );
347 * Create full CSS file.
348 * Enter: fileSpec File to create.
349 * Return: Status code.
351 static gint exporthtml_create_css_full( const gchar *fileSpec ) {
354 cssFile = g_fopen( fileSpec, "rb" );
359 cssFile = g_fopen( fileSpec, "wb" );
361 return MGU_OPEN_FILE;
364 fprintf( cssFile, "body {\n\tbackground: #ffffe0;\n" );
365 fprintf( cssFile, "\tfont-family: lucida, helvetica, sans-serif;\n" );
366 fprintf( cssFile, "\tfont-size: 10pt;\n" );
367 fprintf( cssFile, "}\n" );
368 fprintf( cssFile, "h1 {\n" );
369 fprintf( cssFile, "\tcolor: #000000;\n" );
370 fprintf( cssFile, "\ttext-align: center;\n" );
371 fprintf( cssFile, "}\n" );
372 fprintf( cssFile, "th {\n" );
373 fprintf( cssFile, "\tfont-size: 10pt;\n" );
374 fprintf( cssFile, "}\n" );
375 fprintf( cssFile, "td {\n" );
376 fprintf( cssFile, "\tfont-size: 10pt;\n" );
377 fprintf( cssFile, "}\n" );
378 fprintf( cssFile, ".fmt-folder {\n" );
379 fprintf( cssFile, "\tcolor: #0000ff;\n" );
380 fprintf( cssFile, "\tfont-size: 18pt;\n" );
381 fprintf( cssFile, "\tfont-weight: bold;\n" );
382 fprintf( cssFile, "}\n" );
383 fprintf( cssFile, ".tab-head {\n" );
384 fprintf( cssFile, "\tbackground: #80c0f0;\n" );
385 fprintf( cssFile, "}\n" );
386 fprintf( cssFile, ".tab-row0 {\n" );
387 fprintf( cssFile, "\tbackground: #f0f0f0;\n" );
388 fprintf( cssFile, "}\n" );
389 fprintf( cssFile, ".tab-row1 {\n" );
390 fprintf( cssFile, "\tbackground: #d0d0d0;\n" );
391 fprintf( cssFile, "}\n" );
392 fprintf( cssFile, ".tab-dn {\n" );
393 fprintf( cssFile, "}\n" );
394 fprintf( cssFile, ".tab-addr {\n" );
395 fprintf( cssFile, "\tfont-style: italic;\n" );
396 fprintf( cssFile, "}\n" );
397 fprintf( cssFile, ".tab-email {\n" );
398 fprintf( cssFile, "\tfont-weight: bold;\n" );
399 fprintf( cssFile, "\tfont-style: italic;\n" );
400 fprintf( cssFile, "}\n" );
401 fprintf( cssFile, ".tab-fn {\n" );
402 fprintf( cssFile, "}\n" );
403 fprintf( cssFile, ".tab-attr {\n" );
404 fprintf( cssFile, "}\n" );
411 * Create stylesheet files.
412 * Enter: ctl Export control data.
414 static void exporthtml_create_css_files( ExportHtmlCtl *ctl ) {
418 node = ctl->listStyle;
420 StylesheetEntry *entry = node->data;
421 node = g_list_next( node );
422 if( strlen( entry->fileName ) ) {
423 fileSpec = g_strconcat(
424 ctl->dirOutput, G_DIR_SEPARATOR_S,
425 entry->fileName, NULL );
426 if( entry->id == EXPORT_HTML_ID_DEFAULT ) {
427 exporthtml_create_css_dfl( fileSpec );
429 else if( entry->id != EXPORT_HTML_ID_NONE ) {
430 exporthtml_create_css_full( fileSpec );
438 * Comparison using linked list elements.
440 static gint exporthtml_compare_name(
441 gconstpointer ptr1, gconstpointer ptr2 )
443 const AddrItemObject *item1 = ptr1;
444 const AddrItemObject *item2 = ptr2;
445 const gchar *name1 = NULL, *name2 = NULL;
446 if( item1 ) name1 = ADDRITEM_NAME( item1 );
447 if( item2 ) name2 = ADDRITEM_NAME( item2 );
448 if( ! name1 ) return ( name2 != NULL );
449 if( ! name2 ) return -1;
450 return g_utf8_collate( name1, name2 );
454 * Comparison using linked list elements.
456 static gint exporthtml_compare_email(
457 gconstpointer ptr1, gconstpointer ptr2 )
459 const ItemEMail *email1 = ptr1;
460 const ItemEMail *email2 = ptr2;
461 const gchar *name1 = NULL, *name2 = NULL;
462 if( email1 ) name1 = email1->address;
463 if( email2 ) name2 = email2->address;
464 if( ! name1 ) return ( name2 != NULL );
465 if( ! name2 ) return -1;
466 return g_utf8_collate( name1, name2 );
470 * Comparison using linked list elements.
472 static gint exporthtml_compare_attrib(
473 gconstpointer ptr1, gconstpointer ptr2 )
475 const UserAttribute *attr1 = ptr1;
476 const UserAttribute *attr2 = ptr2;
477 const gchar *name1 = NULL, *name2 = NULL;
478 if( attr1 ) name1 = attr1->name;
479 if( attr2 ) name2 = attr2->name;
480 if( ! name1 ) return ( name2 != NULL );
481 if( ! name2 ) return -1;
482 return g_utf8_collate( name1, name2 );
486 * Build sorted list of named items.
487 * Enter: list List of items to sorted.
488 * Return: Sorted list.
489 * Note: List should freed after use. Items referenced by list should not be
490 * freed since they are managed by the address cache.
492 static GList *exporthtml_sort_name( const GList *list ) {
494 GList *sorted = NULL;
498 sorted = g_list_insert_sorted(
499 sorted, node->data, exporthtml_compare_name );
500 node = g_list_next( node );
506 * Build sorted list of email items.
507 * Enter: list List of E-Mail items to sorted.
508 * Return: Sorted list.
509 * Note: List should freed after use. Items referenced by list should not be
510 * freed since they are managed by the address cache.
512 static GList *exporthtml_sort_email( const GList *list ) {
514 GList *sorted = NULL;
518 sorted = g_list_insert_sorted(
519 sorted, node->data, exporthtml_compare_email );
520 node = g_list_next( node );
526 * Build sorted list of attributes.
527 * Enter: list List of items to sorted.
528 * Return: Sorted list.
529 * Note: List should freed after use. Items referenced by list should not be
530 * freed since they are managed by the address cache.
532 static GList *exporthtml_sort_attrib( const GList *list ) {
534 GList *sorted = NULL;
539 sorted = g_list_insert_sorted(
540 sorted, node->data, exporthtml_compare_attrib );
541 node = g_list_next( node );
547 * Format a list of E-Mail addresses.
548 * Enter: ctl Export control data.
549 * stream Output stream.
550 * listEMail List of addresses.
551 * sortFlag Set to TRUE if address list should be sorted.
553 static void exporthtml_fmt_email(
554 ExportHtmlCtl *ctl, FILE *stream, const GList *listEMail,
561 if( listEMail == NULL ) {
562 fprintf( stream, SC_HTML_SPACE );
568 node = list = exporthtml_sort_email( listEMail );
575 ItemEMail *email = ( ItemEMail * ) node->data;
576 node = g_list_next( node );
578 name = ADDRITEM_NAME( email );
580 fprintf( stream, "%s ", name );
582 if( ctl->linkEMail ) {
583 fprintf( stream, "<a href=\"mailto:%s\">",
586 fprintf( stream, "<span class=\"tab-email\">" );
587 fprintf( stream, "%s", email->address );
588 fprintf( stream, "</span>" );
589 if( ctl->linkEMail ) {
590 fprintf( stream, "</a>" );
592 if( email->remarks ) {
593 if( strlen( email->remarks ) ) {
594 fprintf( stream, " (%s)", email->remarks );
597 fprintf( stream, "<br>\n" );
603 * Format groups in an address book folder.
604 * Enter: ctl Export control data.
605 * stream Output stream.
607 * prevFlag If FALSE, list of persons were output.
608 * Return: TRUE if no groups were formatted.
610 static gboolean exporthtml_fmt_group(
611 ExportHtmlCtl *ctl, FILE *stream, const ItemFolder *folder,
614 gboolean retVal, band;
616 const gchar *tagName;
619 if( folder->listGroup == NULL ) return retVal;
621 /* Write separator */
623 fprintf( stream, "<br>\n" );
626 /* Write table headers */
627 fprintf( stream, "<table" );
628 fprintf( stream, " border=\"%d\"", BORDER_SIZE );
629 fprintf( stream, " cellpadding=\"%d\"", CELL_PADDING );
630 fprintf( stream, " cellspacing=\"%d\"", CELL_SPACING );
631 fprintf( stream, ">\n" );
633 fprintf( stream, "<tr class=\"tab-head\">\n" );
634 fprintf( stream, " <th width=\"200\">" );
635 fprintf( stream, "%s", _( "Group Name" ) );
636 fprintf( stream, "</th>\n" );
637 fprintf( stream, " <th width=\"300\">" );
638 fprintf( stream, "%s", _( "Email Address" ) );
639 fprintf( stream, "</th>\n" );
640 fprintf( stream, "</tr>\n" );
641 list = exporthtml_sort_name( folder->listGroup );
646 AddrItemObject *aio = node->data;
647 if( aio && aio->type == ITEMTYPE_GROUP ) {
648 ItemGroup *group = ( ItemGroup * ) aio;
650 fprintf( stream, "<tr valign=\"top\"" );
653 tagName = _idTagRowOdd_;
656 tagName = _idTagRowEven_;
658 fprintf( stream, " class=\"%s\"", tagName );
661 fprintf( stream, "\">\n" );
663 fprintf( stream, " <td class=\"tab-dn\">" );
664 fprintf( stream, "%s", ADDRITEM_NAME( group ) );
665 fprintf( stream, "</td>\n" );
666 fprintf( stream, " <td class=\"tab-addr\">" );
667 exporthtml_fmt_email( ctl, stream, group->listEMail, TRUE );
668 fprintf( stream, "</td>\n" );
669 fprintf( stream, "</tr>\n" );
672 node = g_list_next( node );
676 fprintf( stream, "</table>\n" );
681 * Format a list of E-Mail addresses.
682 * Enter: ctl Export control data.
683 * stream Output stream.
684 * listAttr List of attributes.
686 static void exporthtml_fmt_attribs(
687 ExportHtmlCtl *ctl, FILE *stream, const GList *listAttr )
692 if( listAttr == NULL ) {
693 fprintf( stream, SC_HTML_SPACE );
697 fprintf( stream, "<table border=\"0\">\n" );
698 node = list = exporthtml_sort_attrib( listAttr );
700 UserAttribute *attr = ( UserAttribute * ) node->data;
701 node = g_list_next( node );
702 fprintf( stream, "<tr valign=\"top\">" );
703 fprintf( stream, "<td align=\"right\">%s:</td>", attr->name );
704 fprintf( stream, "<td>%s</td>", attr->value );
705 fprintf( stream, "</tr>\n" );
709 fprintf( stream, "</table>" );
714 * Enter: ctl Export control data.
716 * person Person to format.
718 static void exporthtml_fmt_fullname(
719 ExportHtmlCtl *ctl, gchar *buf, const ItemPerson *person )
723 if( ctl->nameFormat == EXPORT_HTML_LAST_FIRST ) {
725 if( person->lastName ) {
726 if( *person->lastName ) {
728 strcat( buf, person->lastName );
732 if( person->firstName ) {
733 if( *person->firstName ) {
737 strcat( buf, person->firstName );
742 if( person->firstName ) {
743 if( *person->firstName ) {
744 strcat( buf, person->firstName );
747 if( person->lastName ) {
748 if( *person->lastName ) {
750 strcat( buf, person->lastName );
757 if( *buf ) flag = TRUE;
758 if( person->nickName ) {
759 if( strlen( person->nickName ) ) {
763 strcat( buf, person->nickName );
773 * Format persons in an address book folder.
774 * Enter: ctl Export control data.
775 * stream Output stream.
777 * Return: TRUE if no persons were formatted.
779 static gboolean exporthtml_fmt_person(
780 ExportHtmlCtl *ctl, FILE *stream, const ItemFolder *folder )
782 gboolean retVal, band;
784 gchar buf[ FMT_BUFSIZE ];
785 const gchar *tagName;
788 if( folder->listPerson == NULL ) return retVal;
790 /* Write table headers */
791 fprintf( stream, "<table" );
792 fprintf( stream, " border=\"%d\"", BORDER_SIZE );
793 fprintf( stream, " cellpadding=\"%d\"", CELL_PADDING );
794 fprintf( stream, " cellspacing=\"%d\"", CELL_SPACING );
795 fprintf( stream, ">\n" );
797 fprintf( stream, "<tr class=\"tab-head\">\n" );
798 fprintf( stream, " <th width=\"200\">" );
799 fprintf( stream, "%s", _( "Display Name" ) );
800 fprintf( stream, "</th>\n" );
801 fprintf( stream, " <th width=\"300\">" );
802 fprintf( stream, "%s", _( "Email Address" ) );
803 fprintf( stream, "</th>\n" );
804 fprintf( stream, " <th width=\"200\">" );
805 fprintf( stream, "%s", _( "Full Name" ) );
806 fprintf( stream, "</th>\n" );
807 if( ctl->showAttribs ) {
808 fprintf( stream, " <th width=\"250\">" );
809 fprintf( stream, "%s", _( "Attributes" ) );
810 fprintf( stream, "</th>\n" );
812 fprintf( stream, "</tr>\n" );
815 node = list = exporthtml_sort_name( folder->listPerson );
817 AddrItemObject *aio = node->data;
818 if( aio && aio->type == ITEMTYPE_PERSON ) {
819 ItemPerson *person = ( ItemPerson * ) aio;
821 /* Format first/last/nick name */
823 exporthtml_fmt_fullname( ctl, buf,person );
825 fprintf( stream, "<tr valign=\"top\"" );
828 tagName = _idTagRowOdd_;
831 tagName = _idTagRowEven_;
833 fprintf( stream, " class=\"%s\"", tagName );
836 fprintf( stream, ">\n" );
838 fprintf( stream, " <td class=\"tab-dn\">" );
839 fprintf( stream, "%s", ADDRITEM_NAME( person ) );
840 fprintf( stream, "</td>\n" );
842 fprintf( stream, " <td class=\"tab-addr\">" );
843 exporthtml_fmt_email( ctl, stream, person->listEMail, FALSE );
844 fprintf( stream, "</td>\n" );
846 fprintf( stream, " <td class=\"tab-fn\">" );
848 fprintf( stream, "%s", buf );
851 fprintf( stream, "%s", SC_HTML_SPACE );
853 fprintf( stream, "</td>\n" );
855 if( ctl->showAttribs ) {
856 fprintf( stream, " <td class=\"tab-attr\">" );
857 exporthtml_fmt_attribs(
858 ctl, stream, person->listAttrib );
859 fprintf( stream, "</td>\n" );
861 fprintf( stream, "</tr>\n" );
865 node = g_list_next( node );
869 fprintf( stream, "</table>\n" );
874 * Format folder heirarchy.
875 * Enter: stream Output stream.
876 * list Heirarchy list.
878 static void exporthtml_fmt_folderhead( FILE *stream, const GList *list ) {
886 AddrItemObject *aio = node->data;
887 if( aio && aio->type == ITEMTYPE_FOLDER ) {
888 ItemFolder *folder = ( ItemFolder * ) aio;
890 name = ADDRITEM_NAME( folder );
893 fprintf( stream, " > " );
895 fprintf( stream, "%s", name );
899 node = g_list_next( node );
904 * Format an address book folder.
905 * Enter: ctl Export control data.
906 * stream Output stream.
909 static void exporthtml_fmt_folder(
910 ExportHtmlCtl *ctl, FILE *stream, const ItemFolder *folder )
913 GList *listHeir, *list;
917 name = ADDRITEM_NAME( folder );
919 listHeir = addritem_folder_path( folder, TRUE );
921 fprintf( stream, "<p class=\"fmt-folder\">" );
922 fprintf( stream, "%s: ", _( "Folder" ) );
923 exporthtml_fmt_folderhead( stream, listHeir );
924 fprintf( stream, "</p>\n" );
925 g_list_free( listHeir );
929 ret1 = exporthtml_fmt_person( ctl, stream, folder );
930 ret2 = exporthtml_fmt_group( ctl, stream, folder, ret1 );
932 node = list = exporthtml_sort_name( folder->listFolder );
934 AddrItemObject *aio = node->data;
935 if( aio && aio->type == ITEMTYPE_FOLDER ) {
936 ItemFolder *subFolder = ( ItemFolder * ) aio;
937 exporthtml_fmt_folder( ctl, stream, subFolder );
939 node = g_list_next( node );
947 * Format header block.
948 * Enter: ctl Export control data.
949 * stream Output stream.
952 static void exporthtml_fmt_header(
953 ExportHtmlCtl *ctl, FILE *stream, gchar *title )
955 StylesheetEntry *entry;
957 entry = exporthtml_find_stylesheet( ctl );
960 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\">\n" );
961 fprintf( stream, "<html>\n" );
962 fprintf( stream, "<head>\n" );
964 if( ctl->encoding && strlen( ctl->encoding ) > 0 ) {
965 fprintf( stream, "<meta " );
966 fprintf( stream, "http-equiv=\"Content-Type\" " );
967 fprintf( stream, "content=\"text/html; charset=%s\">\n",
971 fprintf( stream, "<title>%s</title>\n", title );
973 if( entry != NULL ) {
974 if( entry->fileName && strlen( entry->fileName ) > 0 ) {
975 fprintf( stream, "<link " );
976 fprintf( stream, "rel=\"stylesheet\" " );
977 fprintf( stream, "type=\"text/css\" " );
978 fprintf( stream, "href=\"%s\" >\n", entry->fileName );
981 fprintf( stream, "<body>\n" );
982 fprintf( stream, "</head>\n" );
986 * ============================================================================
987 * Export address book to HTML file.
988 * Enter: ctl Export control data.
989 * cache Address book/data source cache.
991 * ============================================================================
993 void exporthtml_process(
994 ExportHtmlCtl *ctl, AddressCache *cache )
996 ItemFolder *rootFolder;
1000 static gchar *title;
1002 htmlFile = g_fopen( ctl->path, "wb" );
1004 /* Cannot open file */
1005 printf( "Cannot open file for write\n" );
1006 ctl->retVal = MGU_OPEN_FILE;
1010 title = _( "Sylpheed-Claws Address Book" );
1011 rootFolder = cache->rootFolder;
1012 dsName = cache->name;
1014 exporthtml_fmt_header( ctl, htmlFile, title );
1016 fprintf( htmlFile, "<body>\n" );
1017 fprintf( htmlFile, "<h1>%s</h1>\n", title );
1019 fprintf( htmlFile, "<p class=\"fmt-folder\">" );
1020 fprintf( htmlFile, "%s: ", _( "Address Book" ) );
1021 fprintf( htmlFile, "%s", dsName );
1022 fprintf( htmlFile, "</p>\n" );
1024 exporthtml_fmt_folder( ctl, htmlFile, rootFolder );
1027 fprintf( htmlFile, "<p>%s</p>\n", ctime( &tt ) );
1028 fprintf( htmlFile, "<hr width=\"100%%\"></hr>\n" );
1030 fprintf( htmlFile, "</body>\n" );
1031 fprintf( htmlFile, "</html>\n" );
1034 ctl->retVal = MGU_SUCCESS;
1036 /* Create stylesheet files */
1037 exporthtml_create_css_files( ctl );
1042 * Build full export file specification.
1043 * Enter: ctl Export control data.
1045 static void exporthtml_build_filespec( ExportHtmlCtl *ctl ) {
1048 fileSpec = g_strconcat(
1049 ctl->dirOutput, G_DIR_SEPARATOR_S, ctl->fileHtml, NULL );
1050 ctl->path = mgu_replace_string( ctl->path, fileSpec );
1055 * ============================================================================
1056 * Parse directory and filename from full export file specification.
1057 * Enter: ctl Export control data.
1058 * fileSpec File spec.
1059 * ============================================================================
1061 void exporthtml_parse_filespec( ExportHtmlCtl *ctl, gchar *fileSpec ) {
1063 gchar *base = g_path_get_basename(fileSpec);
1066 mgu_replace_string( ctl->fileHtml, base );
1068 t = g_path_get_dirname( fileSpec );
1069 ctl->dirOutput = mgu_replace_string( ctl->dirOutput, t );
1071 ctl->path = mgu_replace_string( ctl->path, fileSpec );
1075 * ============================================================================
1076 * Test whether directory exists.
1077 * Enter: ctl Export control data.
1078 * Return: TRUE if exists.
1079 * ============================================================================
1081 gboolean exporthtml_test_dir( ExportHtmlCtl *ctl ) {
1086 if((dp = opendir( ctl->dirOutput )) != NULL) {
1094 * ============================================================================
1095 * Create output directory.
1096 * Enter: ctl Export control data.
1097 * Return: TRUE if directory created.
1098 * ============================================================================
1100 gboolean exporthtml_create_dir( ExportHtmlCtl *ctl ) {
1101 gboolean retVal = FALSE;
1104 if( mkdir( ctl->dirOutput, S_IRWXU ) == 0 ) {
1108 ctl->rcCreate = errno;
1114 * ============================================================================
1115 * Retrieve create directory error message.
1116 * Enter: ctl Export control data.
1118 * ============================================================================
1120 gchar *exporthtml_get_create_msg( ExportHtmlCtl *ctl ) {
1123 if( ctl->rcCreate == EEXIST ) {
1124 msg = _( "Name already exists but is not a directory." );
1126 else if( ctl->rcCreate == EACCES ) {
1127 msg = _( "No permissions to create directory." );
1129 else if( ctl->rcCreate == ENAMETOOLONG ) {
1130 msg = _( "Name is too long." );
1133 msg = _( "Not specified." );
1139 * Set default values.
1140 * Enter: ctl Export control data.
1142 static void exporthtml_default_values( ExportHtmlCtl *ctl ) {
1146 g_get_home_dir(), G_DIR_SEPARATOR_S,
1147 DFL_DIR_SYLPHEED_OUT, NULL );
1149 ctl->dirOutput = mgu_replace_string( ctl->dirOutput, str );
1153 mgu_replace_string( ctl->fileHtml, DFL_FILE_SYLPHEED_OUT );
1154 ctl->encoding = NULL;
1155 ctl->stylesheet = EXPORT_HTML_ID_DEFAULT;
1156 ctl->nameFormat = EXPORT_HTML_FIRST_LAST;
1157 ctl->banding = TRUE;
1158 ctl->linkEMail = TRUE;
1159 ctl->showAttribs = TRUE;
1160 ctl->retVal = MGU_SUCCESS;
1164 * ============================================================================
1165 * Load settings from XML properties file.
1166 * Enter: ctl Export control data.
1167 * ============================================================================
1169 void exporthtml_load_settings( ExportHtmlCtl *ctl ) {
1175 props = xmlprops_create();
1176 xmlprops_set_path( props, ctl->settingsFile );
1177 rc = xmlprops_load_file( props );
1180 xmlprops_get_property_s( props, EXMLPROP_DIRECTORY, buf );
1181 ctl->dirOutput = mgu_replace_string( ctl->dirOutput, buf );
1183 xmlprops_get_property_s( props, EXMLPROP_FILE, buf );
1184 ctl->fileHtml = mgu_replace_string( ctl->fileHtml, buf );
1187 xmlprops_get_property_i( props, EXMLPROP_STYLESHEET );
1189 xmlprops_get_property_i( props, EXMLPROP_FMT_NAME );
1191 xmlprops_get_property_b( props, EXMLPROP_BANDING );
1193 xmlprops_get_property_b( props, EXMLPROP_FMT_EMAIL );
1195 xmlprops_get_property_b( props, EXMLPROP_FMT_ATTRIB );
1198 /* Set default values */
1199 exporthtml_default_values( ctl );
1201 exporthtml_build_filespec( ctl );
1202 /* exporthtml_print( ctl, stdout ); */
1204 xmlprops_free( props );
1208 * ============================================================================
1209 * Save settings to XML properties file.
1210 * Enter: ctl Export control data.
1211 * ============================================================================
1213 void exporthtml_save_settings( ExportHtmlCtl *ctl ) {
1216 props = xmlprops_create();
1217 xmlprops_set_path( props, ctl->settingsFile );
1219 xmlprops_set_property( props, EXMLPROP_DIRECTORY, ctl->dirOutput );
1220 xmlprops_set_property( props, EXMLPROP_FILE, ctl->fileHtml );
1221 xmlprops_set_property_i( props, EXMLPROP_STYLESHEET, ctl->stylesheet );
1222 xmlprops_set_property_i( props, EXMLPROP_FMT_NAME, ctl->nameFormat );
1223 xmlprops_set_property_b( props, EXMLPROP_BANDING, ctl->banding );
1224 xmlprops_set_property_b( props, EXMLPROP_FMT_EMAIL, ctl->linkEMail );
1225 xmlprops_set_property_b( props, EXMLPROP_FMT_ATTRIB, ctl->showAttribs );
1226 xmlprops_save_file( props );
1227 xmlprops_free( props );
1231 * ============================================================================
1233 * ============================================================================