2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2002 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 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 * Export address book to HTML file.
34 #include "exporthtml.h"
36 #include "addrcache.h"
40 #define DFL_DIR_SYLPHEED_OUT "sylpheed-out"
41 #define DFL_FILE_SYLPHEED_OUT "addressbook.html"
43 #define FMT_BUFSIZE 2048
44 #define HTML_SPACE " "
46 #define CELL_PADDING 2
47 #define CELL_SPACING 2
48 #define CHAR_ENCODING "ISO-8859-1"
50 /* Stylesheet names */
51 #define FILENAME_NONE ""
52 #define FILENAME_DEFAULT "sylpheed.css"
53 #define FILENAME_FULL "full.css"
54 #define FILENAME_CUSTOM "custom.css"
55 #define FILENAME_CUSTOM2 "custom2.css"
56 #define FILENAME_CUSTOM3 "custom3.css"
57 #define FILENAME_CUSTOM4 "custom4.css"
59 /* Settings - properties */
60 #define EXML_PROPFILE_NAME "exporthtml.xml"
61 #define EXMLPROP_DIRECTORY "directory"
62 #define EXMLPROP_FILE "file"
63 #define EXMLPROP_STYLESHEET "stylesheet"
64 #define EXMLPROP_FMT_NAME "format-full-name"
65 #define EXMLPROP_FMT_EMAIL "format-email-links"
66 #define EXMLPROP_FMT_ATTRIB "format-attributes"
67 #define EXMLPROP_BANDING "color-banding"
68 #define EXMLPROP_VALUE_YES "y"
69 #define EXMLPROP_VALUE_NO "n"
71 static gchar *_idTagRowEven_ = "tab-row0";
72 static gchar *_idTagRowOdd_ = "tab-row1";
77 typedef struct _StylesheetEntry StylesheetEntry;
78 struct _StylesheetEntry {
85 * Build stylesheet entry.
86 * Enter: ctl Export control data.
91 static void exporthtml_build_entry(
92 ExportHtmlCtl *ctl, const gchar *file, const gint id,
95 StylesheetEntry *entry;
97 entry = g_new0( StylesheetEntry, 1 );
98 entry->fileName = g_strdup( file );
100 entry->dflValue = dfl;
101 ctl->listStyle = g_list_append( ctl->listStyle, entry );
105 * Free up object by releasing internal memory.
106 * Enter: ctl Export control data.
108 ExportHtmlCtl *exporthtml_create( void ) {
109 ExportHtmlCtl *ctl = g_new0( ExportHtmlCtl, 1 );
112 ctl->dirOutput = NULL;
113 ctl->fileHtml = NULL;
114 ctl->encoding = NULL;
115 ctl->stylesheet = EXPORT_HTML_ID_NONE;
116 ctl->nameFormat = EXPORT_HTML_FIRST_LAST;
117 ctl->banding = FALSE;
118 ctl->linkEMail = FALSE;
119 ctl->showAttribs = FALSE;
120 ctl->retVal = MGU_SUCCESS;
121 ctl->listStyle = NULL;
123 ctl->settingsFile = g_strconcat(
124 get_rc_dir(), G_DIR_SEPARATOR_S, EXML_PROPFILE_NAME, NULL );
126 /* Build stylesheet list */
127 exporthtml_build_entry(
128 ctl, FILENAME_NONE, EXPORT_HTML_ID_NONE, FALSE );
129 exporthtml_build_entry(
130 ctl, FILENAME_DEFAULT, EXPORT_HTML_ID_DEFAULT, TRUE );
131 exporthtml_build_entry(
132 ctl, FILENAME_FULL, EXPORT_HTML_ID_FULL, FALSE );
133 exporthtml_build_entry(
134 ctl, FILENAME_CUSTOM, EXPORT_HTML_ID_CUSTOM, FALSE );
135 exporthtml_build_entry(
136 ctl, FILENAME_CUSTOM2, EXPORT_HTML_ID_CUSTOM2, FALSE );
137 exporthtml_build_entry(
138 ctl, FILENAME_CUSTOM3, EXPORT_HTML_ID_CUSTOM3, FALSE );
139 exporthtml_build_entry(
140 ctl, FILENAME_CUSTOM4, EXPORT_HTML_ID_CUSTOM4, FALSE );
146 * Free up object by releasing internal memory.
147 * Enter: ctl Export control data.
149 void exporthtml_free( ExportHtmlCtl *ctl ) {
151 StylesheetEntry *entry;
153 g_return_if_fail( ctl != NULL );
155 /* Free stylesheet list */
156 node = ctl->listStyle;
158 entry = ( StylesheetEntry * ) node->data;
159 g_free( entry->fileName );
160 entry->fileName = NULL;
162 entry->dflValue = FALSE;
165 node = g_list_next( node );
167 g_list_free( ctl->listStyle );
168 ctl->listStyle = NULL;
171 g_free( ctl->fileHtml );
172 g_free( ctl->encoding );
173 g_free( ctl->dirOutput );
174 g_free( ctl->settingsFile );
178 ctl->dirOutput = NULL;
179 ctl->fileHtml = NULL;
180 ctl->encoding = NULL;
181 ctl->stylesheet = EXPORT_HTML_ID_NONE;
182 ctl->nameFormat = EXPORT_HTML_FIRST_LAST;
183 ctl->banding = FALSE;
184 ctl->linkEMail = FALSE;
185 ctl->showAttribs = FALSE;
186 ctl->retVal = MGU_SUCCESS;
189 /* Now release object */
194 * Print control object.
195 * Enter: ctl Export control data.
196 * stream Output stream.
198 void exporthtml_print( ExportHtmlCtl *ctl, FILE *stream ) {
199 fprintf( stream, "ExportHtmlCtl:\n" );
200 fprintf( stream, " path: %s\n", ctl->path );
201 fprintf( stream, "directory: %s\n", ctl->dirOutput );
202 fprintf( stream, " file: %s\n", ctl->fileHtml );
203 fprintf( stream, " settings: %s\n", ctl->settingsFile );
204 fprintf( stream, " encoding: %s\n", ctl->encoding );
205 fprintf( stream, " css file: %d\n", ctl->stylesheet );
206 fprintf( stream, " name fmt: %d\n", ctl->nameFormat );
207 fprintf( stream, " banding: %d\n", ctl->banding );
208 fprintf( stream, "link mail: %d\n", ctl->linkEMail );
209 fprintf( stream, "show attr: %d\n", ctl->showAttribs );
214 * Enter: ctl Export control data.
215 * Return: Stylesheet object, or NULL if nothing found. If a default entry is
216 * found in list, it will be returned.
218 static StylesheetEntry *exporthtml_find_stylesheet( ExportHtmlCtl *ctl ) {
219 StylesheetEntry *retVal = NULL;
220 StylesheetEntry *entry;
223 node = ctl->listStyle;
225 entry = ( StylesheetEntry * ) node->data;
226 if( entry->id == ctl->stylesheet ) return entry;
227 if( entry->dflValue ) retVal = entry;
228 node = g_list_next( node );
237 * Specify path to folder where files are created.
238 * Enter: ctl Export control data.
239 * value Full directory path.
241 void exporthtml_set_output_dir( ExportHtmlCtl *ctl, const gchar *value ) {
242 g_return_if_fail( ctl != NULL );
243 ctl->dirOutput = mgu_replace_string( ctl->dirOutput, value );
244 g_strstrip( ctl->dirOutput );
246 void exporthtml_set_path( ExportHtmlCtl *ctl, const gchar *value ) {
247 g_return_if_fail( ctl != NULL );
248 ctl->path = mgu_replace_string( ctl->path, value );
249 g_strstrip( ctl->path );
251 void exporthtml_set_file_html( ExportHtmlCtl *ctl, const gchar *value ) {
252 g_return_if_fail( ctl != NULL );
253 ctl->fileHtml = mgu_replace_string( ctl->fileHtml, value );
254 g_strstrip( ctl->fileHtml );
256 void exporthtml_set_encoding( ExportHtmlCtl *ctl, const gchar *value ) {
257 g_return_if_fail( ctl != NULL );
258 ctl->encoding = mgu_replace_string( ctl->encoding, value );
259 g_strstrip( ctl->encoding );
261 void exporthtml_set_stylesheet( ExportHtmlCtl *ctl, const gint value ) {
262 g_return_if_fail( ctl != NULL );
263 ctl->stylesheet = value;
265 void exporthtml_set_name_format( ExportHtmlCtl *ctl, const gint value ) {
266 g_return_if_fail( ctl != NULL );
267 ctl->nameFormat = value;
269 void exporthtml_set_banding( ExportHtmlCtl *ctl, const gboolean value ) {
270 g_return_if_fail( ctl != NULL );
271 ctl->banding = value;
273 void exporthtml_set_link_email( ExportHtmlCtl *ctl, const gboolean value ) {
274 g_return_if_fail( ctl != NULL );
275 ctl->linkEMail = value;
277 void exporthtml_set_attributes( ExportHtmlCtl *ctl, const gboolean value ) {
278 g_return_if_fail( ctl != NULL );
279 ctl->showAttribs = value;
283 * Create default CSS file.
284 * Enter: fileSpec File to create.
285 * Return: Status code.
287 static gint exporthtml_create_css_dfl( const gchar *fileSpec ) {
290 cssFile = fopen( fileSpec, "rb" );
295 cssFile = fopen( fileSpec, "wb" );
297 return MGU_OPEN_FILE;
300 fprintf( cssFile, "body {\n\tbackground: #ffffe0;\n" );
301 fprintf( cssFile, "\tfont-family: lucida, helvetica, sans-serif;\n" );
302 fprintf( cssFile, "\tfont-size: 10pt;\n" );
303 fprintf( cssFile, "}\n" );
304 fprintf( cssFile, "h1 {\n" );
305 fprintf( cssFile, "\tcolor: #000000;\n" );
306 fprintf( cssFile, "\ttext-align: center;\n" );
307 fprintf( cssFile, "}\n" );
308 fprintf( cssFile, "th {\n" );
309 fprintf( cssFile, "\tfont-size: 10pt;\n" );
310 fprintf( cssFile, "}\n" );
311 fprintf( cssFile, "td {\n" );
312 fprintf( cssFile, "\tfont-size: 10pt;\n" );
313 fprintf( cssFile, "}\n" );
314 fprintf( cssFile, ".fmt-folder {\n" );
315 fprintf( cssFile, "\tcolor: #0000ff;\n" );
316 fprintf( cssFile, "\tfont-size: 18pt;\n" );
317 fprintf( cssFile, "\tfont-weight: bold;\n" );
318 fprintf( cssFile, "}\n" );
319 fprintf( cssFile, ".tab-head {\n" );
320 fprintf( cssFile, "\tbackground: #80c0f0;\n" );
321 fprintf( cssFile, "}\n" );
322 fprintf( cssFile, ".tab-dn {\n" );
323 fprintf( cssFile, "}\n" );
324 fprintf( cssFile, ".tab-addr {\n" );
325 fprintf( cssFile, "\tfont-style: italic;\n" );
326 fprintf( cssFile, "}\n" );
327 fprintf( cssFile, ".tab-email {\n" );
328 fprintf( cssFile, "\tfont-weight: bold;\n" );
329 fprintf( cssFile, "\tfont-style: italic;\n" );
330 fprintf( cssFile, "}\n" );
331 fprintf( cssFile, ".tab-fn {\n" );
332 fprintf( cssFile, "}\n" );
333 fprintf( cssFile, ".tab-attr {\n" );
334 fprintf( cssFile, "}\n" );
341 * Create full CSS file.
342 * Enter: fileSpec File to create.
343 * Return: Status code.
345 static gint exporthtml_create_css_full( const gchar *fileSpec ) {
348 cssFile = fopen( fileSpec, "rb" );
353 cssFile = fopen( fileSpec, "wb" );
355 return MGU_OPEN_FILE;
358 fprintf( cssFile, "body {\n\tbackground: #ffffe0;\n" );
359 fprintf( cssFile, "\tfont-family: lucida, helvetica, sans-serif;\n" );
360 fprintf( cssFile, "\tfont-size: 10pt;\n" );
361 fprintf( cssFile, "}\n" );
362 fprintf( cssFile, "h1 {\n" );
363 fprintf( cssFile, "\tcolor: #000000;\n" );
364 fprintf( cssFile, "\ttext-align: center;\n" );
365 fprintf( cssFile, "}\n" );
366 fprintf( cssFile, "th {\n" );
367 fprintf( cssFile, "\tfont-size: 10pt;\n" );
368 fprintf( cssFile, "}\n" );
369 fprintf( cssFile, "td {\n" );
370 fprintf( cssFile, "\tfont-size: 10pt;\n" );
371 fprintf( cssFile, "}\n" );
372 fprintf( cssFile, ".fmt-folder {\n" );
373 fprintf( cssFile, "\tcolor: #0000ff;\n" );
374 fprintf( cssFile, "\tfont-size: 18pt;\n" );
375 fprintf( cssFile, "\tfont-weight: bold;\n" );
376 fprintf( cssFile, "}\n" );
377 fprintf( cssFile, ".tab-head {\n" );
378 fprintf( cssFile, "\tbackground: #80c0f0;\n" );
379 fprintf( cssFile, "}\n" );
380 fprintf( cssFile, ".tab-row0 {\n" );
381 fprintf( cssFile, "\tbackground: #f0f0f0;\n" );
382 fprintf( cssFile, "}\n" );
383 fprintf( cssFile, ".tab-row1 {\n" );
384 fprintf( cssFile, "\tbackground: #d0d0d0;\n" );
385 fprintf( cssFile, "}\n" );
386 fprintf( cssFile, ".tab-dn {\n" );
387 fprintf( cssFile, "}\n" );
388 fprintf( cssFile, ".tab-addr {\n" );
389 fprintf( cssFile, "\tfont-style: italic;\n" );
390 fprintf( cssFile, "}\n" );
391 fprintf( cssFile, ".tab-email {\n" );
392 fprintf( cssFile, "\tfont-weight: bold;\n" );
393 fprintf( cssFile, "\tfont-style: italic;\n" );
394 fprintf( cssFile, "}\n" );
395 fprintf( cssFile, ".tab-fn {\n" );
396 fprintf( cssFile, "}\n" );
397 fprintf( cssFile, ".tab-attr {\n" );
398 fprintf( cssFile, "}\n" );
405 * Create stylesheet files.
406 * Enter: ctl Export control data.
408 static void exporthtml_create_css_files( ExportHtmlCtl *ctl ) {
412 node = ctl->listStyle;
414 StylesheetEntry *entry = node->data;
415 node = g_list_next( node );
416 if( strlen( entry->fileName ) ) {
417 fileSpec = g_strconcat(
418 ctl->dirOutput, G_DIR_SEPARATOR_S,
419 entry->fileName, NULL );
420 if( entry->id == EXPORT_HTML_ID_DEFAULT ) {
421 exporthtml_create_css_dfl( fileSpec );
423 else if( entry->id != EXPORT_HTML_ID_NONE ) {
424 exporthtml_create_css_full( fileSpec );
432 * Comparison using linked list elements.
434 static gint exporthtml_compare_name(
435 gconstpointer ptr1, gconstpointer ptr2 )
437 const AddrItemObject *item1 = ptr1;
438 const AddrItemObject *item2 = ptr2;
439 const gchar *name1 = NULL, *name2 = NULL;
440 if( item1 ) name1 = ADDRITEM_NAME( item1 );
441 if( item2 ) name2 = ADDRITEM_NAME( item2 );
442 if( ! name1 ) return ( name2 != NULL );
443 if( ! name2 ) return -1;
444 return strcasecmp( name1, name2 );
448 * Comparison using linked list elements.
450 static gint exporthtml_compare_email(
451 gconstpointer ptr1, gconstpointer ptr2 )
453 const ItemEMail *email1 = ptr1;
454 const ItemEMail *email2 = ptr2;
455 const gchar *name1 = NULL, *name2 = NULL;
456 if( email1 ) name1 = email1->address;
457 if( email2 ) name2 = email2->address;
458 if( ! name1 ) return ( name2 != NULL );
459 if( ! name2 ) return -1;
460 return strcasecmp( name1, name2 );
464 * Comparison using linked list elements.
466 static gint exporthtml_compare_attrib(
467 gconstpointer ptr1, gconstpointer ptr2 )
469 const UserAttribute *attr1 = ptr1;
470 const UserAttribute *attr2 = ptr2;
471 const gchar *name1 = NULL, *name2 = NULL;
472 if( attr1 ) name1 = attr1->name;
473 if( attr2 ) name2 = attr2->name;
474 if( ! name1 ) return ( name2 != NULL );
475 if( ! name2 ) return -1;
476 return strcasecmp( name1, name2 );
480 * Build sorted list of named items.
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_name( const GList *list ) {
488 GList *sorted = NULL;
492 sorted = g_list_insert_sorted(
493 sorted, node->data, exporthtml_compare_name );
494 node = g_list_next( node );
500 * Build sorted list of email items.
501 * Enter: list List of E-Mail items to sorted.
502 * Return: Sorted list.
503 * Note: List should freed after use. Items referenced by list should not be
504 * freed since they are managed by the address cache.
506 static GList *exporthtml_sort_email( const GList *list ) {
508 GList *sorted = NULL;
512 sorted = g_list_insert_sorted(
513 sorted, node->data, exporthtml_compare_email );
514 node = g_list_next( node );
520 * Build sorted list of attributes.
521 * Enter: list List of items to sorted.
522 * Return: Sorted list.
523 * Note: List should freed after use. Items referenced by list should not be
524 * freed since they are managed by the address cache.
526 static GList *exporthtml_sort_attrib( const GList *list ) {
528 GList *sorted = NULL;
533 sorted = g_list_insert_sorted(
534 sorted, node->data, exporthtml_compare_attrib );
535 node = g_list_next( node );
541 * Format a list of E-Mail addresses.
542 * Enter: ctl Export control data.
543 * stream Output stream.
544 * listEMail List of addresses.
545 * sortFlag Set to TRUE if address list should be sorted.
547 static void exporthtml_fmt_email(
548 ExportHtmlCtl *ctl, FILE *stream, const GList *listEMail,
555 if( listEMail == NULL ) {
556 fprintf( stream, HTML_SPACE );
562 node = list = exporthtml_sort_email( listEMail );
569 ItemEMail *email = ( ItemEMail * ) node->data;
570 node = g_list_next( node );
572 name = ADDRITEM_NAME( email );
574 fprintf( stream, "%s ", name );
576 if( ctl->linkEMail ) {
577 fprintf( stream, "<a href=\"mailto:%s\">",
580 fprintf( stream, "<span class=\"tab-email\">" );
581 fprintf( stream, "%s", email->address );
582 fprintf( stream, "</span>" );
583 if( ctl->linkEMail ) {
584 fprintf( stream, "</a>" );
586 if( email->remarks ) {
587 if( strlen( email->remarks ) ) {
588 fprintf( stream, " (%s)", email->remarks );
591 fprintf( stream, "<br>\n" );
597 * Format groups in an address book folder.
598 * Enter: ctl Export control data.
599 * stream Output stream.
601 * prevFlag If FALSE, list of persons were output.
602 * Return: TRUE if no groups were formatted.
604 static gboolean exporthtml_fmt_group(
605 ExportHtmlCtl *ctl, FILE *stream, const ItemFolder *folder,
608 gboolean retVal, band;
610 const gchar *tagName;
613 if( folder->listGroup == NULL ) return retVal;
615 /* Write separator */
617 fprintf( stream, "<br>\n" );
620 /* Write table headers */
621 fprintf( stream, "<table" );
622 fprintf( stream, " border=\"%d\"", BORDER_SIZE );
623 fprintf( stream, " cellpadding=\"%d\"", CELL_PADDING );
624 fprintf( stream, " cellspacing=\"%d\"", CELL_SPACING );
625 fprintf( stream, ">\n" );
627 fprintf( stream, "<tr class=\"tab-head\">\n" );
628 fprintf( stream, " <th width=\"200\">" );
629 fprintf( stream, "%s", _( "Group Name" ) );
630 fprintf( stream, "</th>\n" );
631 fprintf( stream, " <th width=\"300\">" );
632 fprintf( stream, "%s", _( "E-Mail Address" ) );
633 fprintf( stream, "</th>\n" );
634 fprintf( stream, "</tr>\n" );
635 list = exporthtml_sort_name( folder->listGroup );
640 AddrItemObject *aio = node->data;
641 if( aio && aio->type == ITEMTYPE_GROUP ) {
642 ItemGroup *group = ( ItemGroup * ) aio;
644 fprintf( stream, "<tr valign=\"top\"" );
647 tagName = _idTagRowOdd_;
650 tagName = _idTagRowEven_;
652 fprintf( stream, " class=\"%s\"", tagName );
655 fprintf( stream, "\">\n" );
657 fprintf( stream, " <td class=\"tab-dn\">" );
658 fprintf( stream, "%s", ADDRITEM_NAME( group ) );
659 fprintf( stream, "</td>\n" );
660 fprintf( stream, " <td class=\"tab-addr\">" );
661 exporthtml_fmt_email( ctl, stream, group->listEMail, TRUE );
662 fprintf( stream, "</td>\n" );
663 fprintf( stream, "</tr>\n" );
666 node = g_list_next( node );
670 fprintf( stream, "</table>\n" );
675 * Format a list of E-Mail addresses.
676 * Enter: ctl Export control data.
677 * stream Output stream.
678 * listAttr List of attributes.
680 static void exporthtml_fmt_attribs(
681 ExportHtmlCtl *ctl, FILE *stream, const GList *listAttr )
686 if( listAttr == NULL ) {
687 fprintf( stream, HTML_SPACE );
691 fprintf( stream, "<table border=\"0\">\n" );
692 node = list = exporthtml_sort_attrib( listAttr );
694 UserAttribute *attr = ( UserAttribute * ) node->data;
695 node = g_list_next( node );
696 fprintf( stream, "<tr valign=\"top\">" );
697 fprintf( stream, "<td align=\"right\">%s:</td>", attr->name );
698 fprintf( stream, "<td>%s</td>", attr->value );
699 fprintf( stream, "</tr>\n" );
703 fprintf( stream, "</table>" );
708 * Enter: ctl Export control data.
710 * person Person to format.
712 static void exporthtml_fmt_fullname(
713 ExportHtmlCtl *ctl, gchar *buf, const ItemPerson *person )
717 if( ctl->nameFormat == EXPORT_HTML_LAST_FIRST ) {
719 if( person->lastName ) {
720 if( *person->lastName ) {
722 strcat( buf, person->lastName );
726 if( person->firstName ) {
727 if( *person->firstName ) {
731 strcat( buf, person->firstName );
736 if( person->firstName ) {
737 if( *person->firstName ) {
738 strcat( buf, person->firstName );
741 if( person->lastName ) {
742 if( *person->lastName ) {
744 strcat( buf, person->lastName );
751 if( *buf ) flag = TRUE;
752 if( person->nickName ) {
753 if( strlen( person->nickName ) ) {
757 strcat( buf, person->nickName );
767 * Format persons in an address book folder.
768 * Enter: ctl Export control data.
769 * stream Output stream.
771 * Return: TRUE if no persons were formatted.
773 static gboolean exporthtml_fmt_person(
774 ExportHtmlCtl *ctl, FILE *stream, const ItemFolder *folder )
776 gboolean retVal, band;
778 gchar buf[ FMT_BUFSIZE ];
779 const gchar *tagName;
782 if( folder->listPerson == NULL ) return retVal;
784 /* Write table headers */
785 fprintf( stream, "<table" );
786 fprintf( stream, " border=\"%d\"", BORDER_SIZE );
787 fprintf( stream, " cellpadding=\"%d\"", CELL_PADDING );
788 fprintf( stream, " cellspacing=\"%d\"", CELL_SPACING );
789 fprintf( stream, ">\n" );
791 fprintf( stream, "<tr class=\"tab-head\">\n" );
792 fprintf( stream, " <th width=\"200\">" );
793 fprintf( stream, "%s", _( "Display Name" ) );
794 fprintf( stream, "</th>\n" );
795 fprintf( stream, " <th width=\"300\">" );
796 fprintf( stream, "%s", _( "E-Mail Address" ) );
797 fprintf( stream, "</th>\n" );
798 fprintf( stream, " <th width=\"200\">" );
799 fprintf( stream, "%s", _( "Full Name" ) );
800 fprintf( stream, "</th>\n" );
801 if( ctl->showAttribs ) {
802 fprintf( stream, " <th width=\"250\">" );
803 fprintf( stream, "%s", _( "Attributes" ) );
804 fprintf( stream, "</th>\n" );
806 fprintf( stream, "</tr>\n" );
809 node = list = exporthtml_sort_name( folder->listPerson );
811 AddrItemObject *aio = node->data;
812 if( aio && aio->type == ITEMTYPE_PERSON ) {
813 ItemPerson *person = ( ItemPerson * ) aio;
815 /* Format first/last/nick name */
817 exporthtml_fmt_fullname( ctl, buf,person );
819 fprintf( stream, "<tr valign=\"top\"" );
822 tagName = _idTagRowOdd_;
825 tagName = _idTagRowEven_;
827 fprintf( stream, " class=\"%s\"", tagName );
830 fprintf( stream, ">\n" );
832 fprintf( stream, " <td class=\"tab-dn\">" );
833 fprintf( stream, "%s", ADDRITEM_NAME( person ) );
834 fprintf( stream, "</td>\n" );
836 fprintf( stream, " <td class=\"tab-addr\">" );
837 exporthtml_fmt_email( ctl, stream, person->listEMail, FALSE );
838 fprintf( stream, "</td>\n" );
840 fprintf( stream, " <td class=\"tab-fn\">" );
842 fprintf( stream, "%s", buf );
845 fprintf( stream, "%s", HTML_SPACE );
847 fprintf( stream, "</td>\n" );
849 if( ctl->showAttribs ) {
850 fprintf( stream, " <td class=\"tab-attr\">" );
851 exporthtml_fmt_attribs(
852 ctl, stream, person->listAttrib );
853 fprintf( stream, "</td>\n" );
855 fprintf( stream, "</tr>\n" );
859 node = g_list_next( node );
863 fprintf( stream, "</table>\n" );
868 * Format folder heirarchy.
869 * Enter: stream Output stream.
870 * list Heirarchy list.
872 static void exporthtml_fmt_folderhead( FILE *stream, const GList *list ) {
880 AddrItemObject *aio = node->data;
881 if( aio && aio->type == ITEMTYPE_FOLDER ) {
882 ItemFolder *folder = ( ItemFolder * ) aio;
884 name = ADDRITEM_NAME( folder );
887 fprintf( stream, " > " );
889 fprintf( stream, "%s", name );
893 node = g_list_next( node );
898 * Format an address book folder.
899 * Enter: ctl Export control data.
900 * stream Output stream.
903 static void exporthtml_fmt_folder(
904 ExportHtmlCtl *ctl, FILE *stream, const ItemFolder *folder )
907 GList *listHeir, *list;
911 name = ADDRITEM_NAME( folder );
913 listHeir = addritem_folder_path( folder, TRUE );
915 fprintf( stream, "<p class=\"fmt-folder\">" );
916 fprintf( stream, "%s: ", _( "Folder" ) );
917 exporthtml_fmt_folderhead( stream, listHeir );
918 fprintf( stream, "</p>\n" );
919 g_list_free( listHeir );
923 ret1 = exporthtml_fmt_person( ctl, stream, folder );
924 ret2 = exporthtml_fmt_group( ctl, stream, folder, ret1 );
926 node = list = exporthtml_sort_name( folder->listFolder );
928 AddrItemObject *aio = node->data;
929 if( aio && aio->type == ITEMTYPE_FOLDER ) {
930 ItemFolder *subFolder = ( ItemFolder * ) aio;
931 exporthtml_fmt_folder( ctl, stream, subFolder );
933 node = g_list_next( node );
941 * Format header block.
942 * Enter: ctl Export control data.
943 * stream Output stream.
946 static void exporthtml_fmt_header(
947 ExportHtmlCtl *ctl, FILE *stream, gchar *title )
949 StylesheetEntry *entry;
951 entry = exporthtml_find_stylesheet( ctl );
954 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\">\n" );
955 fprintf( stream, "<html>\n" );
956 fprintf( stream, "<head>\n" );
958 if( ctl->encoding && strlen( ctl->encoding ) > 0 ) {
959 fprintf( stream, "<meta " );
960 fprintf( stream, "http-equiv=\"Content-Type\" " );
961 fprintf( stream, "content=\"text/html; charset=%s\">\n",
965 fprintf( stream, "<title>%s</title>\n", title );
967 if( entry != NULL ) {
968 if( entry->fileName && strlen( entry->fileName ) > 0 ) {
969 fprintf( stream, "<link " );
970 fprintf( stream, "rel=\"stylesheet\" " );
971 fprintf( stream, "type=\"text/css\" " );
972 fprintf( stream, "href=\"%s\" >\n", entry->fileName );
975 fprintf( stream, "<body>\n" );
976 fprintf( stream, "</head>\n" );
980 * ============================================================================
981 * Export address book to HTML file.
982 * Enter: ctl Export control data.
983 * cache Address book/data source cache.
985 * ============================================================================
987 void exporthtml_process(
988 ExportHtmlCtl *ctl, AddressCache *cache )
990 ItemFolder *rootFolder;
996 htmlFile = fopen( ctl->path, "wb" );
998 /* Cannot open file */
999 printf( "Cannot open file for write\n" );
1000 ctl->retVal = MGU_OPEN_FILE;
1004 title = _( "Sylpheed Address Book" );
1005 rootFolder = cache->rootFolder;
1006 dsName = cache->name;
1008 exporthtml_fmt_header( ctl, htmlFile, title );
1010 fprintf( htmlFile, "<body>\n" );
1011 fprintf( htmlFile, "<h1>%s</h1>\n", title );
1013 fprintf( htmlFile, "<p class=\"fmt-folder\">" );
1014 fprintf( htmlFile, "%s: ", _( "Address Book" ) );
1015 fprintf( htmlFile, "%s", dsName );
1016 fprintf( htmlFile, "</p>\n" );
1018 exporthtml_fmt_folder( ctl, htmlFile, rootFolder );
1021 fprintf( htmlFile, "<p>%s</p>\n", ctime( &tt ) );
1022 fprintf( htmlFile, "<hr width=\"100%%\"></hr>\n" );
1024 fprintf( htmlFile, "</body>\n" );
1025 fprintf( htmlFile, "</html>\n" );
1028 ctl->retVal = MGU_SUCCESS;
1030 /* Create stylesheet files */
1031 exporthtml_create_css_files( ctl );
1036 * Build full export file specification.
1037 * Enter: ctl Export control data.
1039 static void exporthtml_build_filespec( ExportHtmlCtl *ctl ) {
1042 fileSpec = g_strconcat(
1043 ctl->dirOutput, G_DIR_SEPARATOR_S, ctl->fileHtml, NULL );
1044 ctl->path = mgu_replace_string( ctl->path, fileSpec );
1049 * ============================================================================
1050 * Parse directory and filename from full export file specification.
1051 * Enter: ctl Export control data.
1052 * fileSpec File spec.
1053 * ============================================================================
1055 void exporthtml_parse_filespec( ExportHtmlCtl *ctl, gchar *fileSpec ) {
1059 mgu_replace_string( ctl->fileHtml, g_basename( fileSpec ) );
1060 t = g_dirname( fileSpec );
1061 ctl->dirOutput = mgu_replace_string( ctl->dirOutput, t );
1063 ctl->path = mgu_replace_string( ctl->path, fileSpec );
1067 * ============================================================================
1068 * Test whether directory exists.
1069 * Enter: ctl Export control data.
1070 * Return: TRUE if exists.
1071 * ============================================================================
1073 gboolean exporthtml_test_dir( ExportHtmlCtl *ctl ) {
1078 if((dp = opendir( ctl->dirOutput )) != NULL) {
1086 * ============================================================================
1087 * Create output directory.
1088 * Enter: ctl Export control data.
1089 * Return: TRUE if directory created.
1090 * ============================================================================
1092 gboolean exporthtml_create_dir( ExportHtmlCtl *ctl ) {
1093 gboolean retVal = FALSE;
1096 if( mkdir( ctl->dirOutput, S_IRWXU ) == 0 ) {
1100 ctl->rcCreate = errno;
1106 * ============================================================================
1107 * Retrieve create directory error message.
1108 * Enter: ctl Export control data.
1110 * ============================================================================
1112 gchar *exporthtml_get_create_msg( ExportHtmlCtl *ctl ) {
1115 if( ctl->rcCreate == EEXIST ) {
1116 msg = _( "Name already exists but is not a directory." );
1118 else if( ctl->rcCreate == EACCES ) {
1119 msg = _( "No permissions to create directory." );
1121 else if( ctl->rcCreate == ENAMETOOLONG ) {
1122 msg = _( "Name is too long." );
1125 msg = _( "Not specified." );
1131 * Set default values.
1132 * Enter: ctl Export control data.
1134 static void exporthtml_default_values( ExportHtmlCtl *ctl ) {
1138 g_get_home_dir(), G_DIR_SEPARATOR_S,
1139 DFL_DIR_SYLPHEED_OUT, NULL );
1141 ctl->dirOutput = mgu_replace_string( ctl->dirOutput, str );
1145 mgu_replace_string( ctl->fileHtml, DFL_FILE_SYLPHEED_OUT );
1146 ctl->encoding = NULL;
1147 ctl->stylesheet = EXPORT_HTML_ID_DEFAULT;
1148 ctl->nameFormat = EXPORT_HTML_FIRST_LAST;
1149 ctl->banding = TRUE;
1150 ctl->linkEMail = TRUE;
1151 ctl->showAttribs = TRUE;
1152 ctl->retVal = MGU_SUCCESS;
1156 * ============================================================================
1157 * Load settings from XML properties file.
1158 * Enter: ctl Export control data.
1159 * ============================================================================
1161 void exporthtml_load_settings( ExportHtmlCtl *ctl ) {
1167 props = xmlprops_create();
1168 xmlprops_set_path( props, ctl->settingsFile );
1169 rc = xmlprops_load_file( props );
1172 xmlprops_get_property_s( props, EXMLPROP_DIRECTORY, buf );
1173 ctl->dirOutput = mgu_replace_string( ctl->dirOutput, buf );
1175 xmlprops_get_property_s( props, EXMLPROP_FILE, buf );
1176 ctl->fileHtml = mgu_replace_string( ctl->fileHtml, buf );
1179 xmlprops_get_property_i( props, EXMLPROP_STYLESHEET );
1181 xmlprops_get_property_i( props, EXMLPROP_FMT_NAME );
1183 xmlprops_get_property_b( props, EXMLPROP_BANDING );
1185 xmlprops_get_property_b( props, EXMLPROP_FMT_EMAIL );
1187 xmlprops_get_property_b( props, EXMLPROP_FMT_ATTRIB );
1190 /* Set default values */
1191 exporthtml_default_values( ctl );
1193 exporthtml_build_filespec( ctl );
1194 /* exporthtml_print( ctl, stdout ); */
1196 xmlprops_free( props );
1200 * ============================================================================
1201 * Save settings to XML properties file.
1202 * Enter: ctl Export control data.
1203 * ============================================================================
1205 void exporthtml_save_settings( ExportHtmlCtl *ctl ) {
1208 props = xmlprops_create();
1209 xmlprops_set_path( props, ctl->settingsFile );
1211 xmlprops_set_property( props, EXMLPROP_DIRECTORY, ctl->dirOutput );
1212 xmlprops_set_property( props, EXMLPROP_FILE, ctl->fileHtml );
1213 xmlprops_set_property_i( props, EXMLPROP_STYLESHEET, ctl->stylesheet );
1214 xmlprops_set_property_i( props, EXMLPROP_FMT_NAME, ctl->nameFormat );
1215 xmlprops_set_property_b( props, EXMLPROP_BANDING, ctl->banding );
1216 xmlprops_set_property_b( props, EXMLPROP_FMT_EMAIL, ctl->linkEMail );
1217 xmlprops_set_property_b( props, EXMLPROP_FMT_ATTRIB, ctl->showAttribs );
1218 xmlprops_save_file( props );
1219 xmlprops_free( props );
1223 * ============================================================================
1225 * ============================================================================