2006-12-12 [paul] 2.6.1cvs21
[claws.git] / src / exporthtml.c
blob6ae4294f84d8d2b1497bfabd7fbe2ffc3f8633c5
1 /*
2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2002-2006 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 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.
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
28 #include <sys/stat.h>
29 #include <dirent.h>
30 #include <errno.h>
31 #include <time.h>
32 #include <string.h>
33 #include <glib.h>
34 #include <glib/gi18n.h>
36 #include "mgutils.h"
37 #include "utils.h"
38 #include "exporthtml.h"
39 #include "xmlprops.h"
41 #ifdef MKDIR_TAKES_ONE_ARG
42 #undef mkdir
43 #define mkdir(a,b) mkdir(a)
44 #endif
46 #define DFL_DIR_CLAWS_OUT "claws-mail-out"
47 #define DFL_FILE_CLAWS_OUT "addressbook.html"
49 #define FMT_BUFSIZE 2048
50 #define SC_HTML_SPACE "&nbsp;"
51 #define BORDER_SIZE 2
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 "claws-mail.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";
81 * Header entry.
83 typedef struct _StylesheetEntry StylesheetEntry;
84 struct _StylesheetEntry {
85 gchar *fileName;
86 gint id;
87 gboolean dflValue;
91 * Build stylesheet entry.
92 * Enter: ctl Export control data.
93 * file Filename.
94 * id File id.
95 * dfl Default flag.
97 static void exporthtml_build_entry(
98 ExportHtmlCtl *ctl, const gchar *file, const gint id,
99 const gboolean dfl )
101 StylesheetEntry *entry;
103 entry = g_new0( StylesheetEntry, 1 );
104 entry->fileName = g_strdup( file );
105 entry->id = id;
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 );
117 ctl->path = NULL;
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;
128 ctl->rcCreate = 0;
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 );
148 return ctl;
152 * Free up object by releasing internal memory.
153 * Enter: ctl Export control data.
155 void exporthtml_free( ExportHtmlCtl *ctl ) {
156 GList *node;
157 StylesheetEntry *entry;
159 g_return_if_fail( ctl != NULL );
161 /* Free stylesheet list */
162 node = ctl->listStyle;
163 while( node ) {
164 entry = ( StylesheetEntry * ) node->data;
165 g_free( entry->fileName );
166 entry->fileName = NULL;
167 entry->id = 0;
168 entry->dflValue = FALSE;
169 g_free( entry );
170 node->data = NULL;
171 node = g_list_next( node );
173 g_list_free( ctl->listStyle );
174 ctl->listStyle = NULL;
176 g_free( ctl->path );
177 g_free( ctl->fileHtml );
178 g_free( ctl->encoding );
179 g_free( ctl->dirOutput );
180 g_free( ctl->settingsFile );
182 /* Clear pointers */
183 ctl->path = NULL;
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;
193 ctl->rcCreate = 0;
195 /* Now release object */
196 g_free( ctl );
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 );
219 * Find style entry.
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;
227 GList *node;
229 node = ctl->listStyle;
230 while( node ) {
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 );
236 return retVal;
240 * Properties...
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 ) {
294 FILE *cssFile;
296 cssFile = g_fopen( fileSpec, "rb" );
297 if( cssFile ) {
298 fclose( cssFile );
299 return MGU_SUCCESS;
301 cssFile = g_fopen( fileSpec, "wb" );
302 if( ! cssFile ) {
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" );
342 fclose( cssFile );
343 return MGU_SUCCESS;
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 ) {
352 FILE *cssFile;
354 cssFile = g_fopen( fileSpec, "rb" );
355 if( cssFile ) {
356 fclose( cssFile );
357 return MGU_SUCCESS;
359 cssFile = g_fopen( fileSpec, "wb" );
360 if( ! cssFile ) {
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" );
406 fclose( cssFile );
407 return MGU_SUCCESS;
411 * Create stylesheet files.
412 * Enter: ctl Export control data.
414 static void exporthtml_create_css_files( ExportHtmlCtl *ctl ) {
415 gchar *fileSpec;
416 GList *node;
418 node = ctl->listStyle;
419 while( node ) {
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 );
432 g_free( 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 ) {
493 const GList *node;
494 GList *sorted = NULL;
496 node = list;
497 while( node ) {
498 sorted = g_list_insert_sorted(
499 sorted, node->data, exporthtml_compare_name );
500 node = g_list_next( node );
502 return sorted;
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 ) {
513 const GList *node;
514 GList *sorted = NULL;
516 node = list;
517 while( node ) {
518 sorted = g_list_insert_sorted(
519 sorted, node->data, exporthtml_compare_email );
520 node = g_list_next( node );
522 return sorted;
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 ) {
533 const GList *node;
534 GList *sorted = NULL;
536 sorted = NULL;
537 node = list;
538 while( node ) {
539 sorted = g_list_insert_sorted(
540 sorted, node->data, exporthtml_compare_attrib );
541 node = g_list_next( node );
543 return sorted;
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,
555 gboolean sortFlag )
557 const GList *node;
558 GList *list;
559 gchar *name;
561 if( listEMail == NULL ) {
562 fprintf( stream, SC_HTML_SPACE );
563 return;
566 list = NULL;
567 if( sortFlag ) {
568 node = list = exporthtml_sort_email( listEMail );
570 else {
571 node = listEMail;
574 while( node ) {
575 ItemEMail *email = ( ItemEMail * ) node->data;
576 node = g_list_next( node );
578 name = ADDRITEM_NAME( email );
579 if( name ) {
580 fprintf( stream, "%s ", name );
582 if( ctl->linkEMail ) {
583 fprintf( stream, "<a href=\"mailto:%s\">",
584 email->address );
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" );
599 g_list_free( list );
603 * Format groups in an address book folder.
604 * Enter: ctl Export control data.
605 * stream Output stream.
606 * folder Folder.
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,
612 gboolean prevFlag )
614 gboolean retVal, band;
615 GList *node, *list;
616 const gchar *tagName;
618 retVal = TRUE;
619 if( folder->listGroup == NULL ) return retVal;
621 /* Write separator */
622 if( ! prevFlag ) {
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 );
643 band = FALSE;
644 node = list;
645 while( node ) {
646 AddrItemObject *aio = node->data;
647 if( aio && aio->type == ITEMTYPE_GROUP ) {
648 ItemGroup *group = ( ItemGroup * ) aio;
650 fprintf( stream, "<tr valign=\"top\"" );
651 if( ctl->banding ) {
652 if( band ) {
653 tagName = _idTagRowOdd_;
655 else {
656 tagName = _idTagRowEven_;
658 fprintf( stream, " class=\"%s\"", tagName );
659 band = ! band;
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" );
670 retVal = FALSE;
672 node = g_list_next( node );
675 g_list_free( list );
676 fprintf( stream, "</table>\n" );
677 return retVal;
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 )
689 const GList *node;
690 GList *list;
692 if( listAttr == NULL ) {
693 fprintf( stream, SC_HTML_SPACE );
694 return;
697 fprintf( stream, "<table border=\"0\">\n" );
698 node = list = exporthtml_sort_attrib( listAttr );
699 while( node ) {
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" );
708 g_list_free( list );
709 fprintf( stream, "</table>" );
713 * Format full name.
714 * Enter: ctl Export control data.
715 * buf Output buffer.
716 * person Person to format.
718 static void exporthtml_fmt_fullname(
719 ExportHtmlCtl *ctl, gchar *buf, const ItemPerson *person )
721 gboolean flag;
723 if( ctl->nameFormat == EXPORT_HTML_LAST_FIRST ) {
724 flag = FALSE;
725 if( person->lastName ) {
726 if( *person->lastName ) {
727 strcat( buf, " " );
728 strcat( buf, person->lastName );
729 flag = TRUE;
732 if( person->firstName ) {
733 if( *person->firstName ) {
734 if( flag ) {
735 strcat( buf, ", " );
737 strcat( buf, person->firstName );
741 else {
742 if( person->firstName ) {
743 if( *person->firstName ) {
744 strcat( buf, person->firstName );
747 if( person->lastName ) {
748 if( *person->lastName ) {
749 strcat( buf, " " );
750 strcat( buf, person->lastName );
754 g_strstrip( buf );
756 flag = FALSE;
757 if( *buf ) flag = TRUE;
758 if( person->nickName ) {
759 if( strlen( person->nickName ) ) {
760 if( flag ) {
761 strcat( buf, " (" );
763 strcat( buf, person->nickName );
764 if( flag ) {
765 strcat( buf, ")" );
769 g_strstrip( buf );
773 * Format persons in an address book folder.
774 * Enter: ctl Export control data.
775 * stream Output stream.
776 * folder Folder.
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;
783 GList *node, *list;
784 gchar buf[ FMT_BUFSIZE ];
785 const gchar *tagName;
787 retVal = TRUE;
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" );
814 band = FALSE;
815 node = list = exporthtml_sort_name( folder->listPerson );
816 while( node ) {
817 AddrItemObject *aio = node->data;
818 if( aio && aio->type == ITEMTYPE_PERSON ) {
819 ItemPerson *person = ( ItemPerson * ) aio;
821 /* Format first/last/nick name */
822 *buf = '\0';
823 exporthtml_fmt_fullname( ctl, buf,person );
825 fprintf( stream, "<tr valign=\"top\"" );
826 if( ctl->banding ) {
827 if( band ) {
828 tagName = _idTagRowOdd_;
830 else {
831 tagName = _idTagRowEven_;
833 fprintf( stream, " class=\"%s\"", tagName );
834 band = ! band;
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\">" );
847 if( *buf ) {
848 fprintf( stream, "%s", buf );
850 else {
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" );
863 retVal = FALSE;
865 node = g_list_next( node );
868 g_list_free( list );
869 fprintf( stream, "</table>\n" );
870 return retVal;
874 * Format folder heirarchy.
875 * Enter: stream Output stream.
876 * list Heirarchy list.
878 static void exporthtml_fmt_folderhead( FILE *stream, const GList *list ) {
879 const GList *node;
880 gboolean flag;
881 gchar *name;
883 flag = FALSE;
884 node = list;
885 while( node ) {
886 AddrItemObject *aio = node->data;
887 if( aio && aio->type == ITEMTYPE_FOLDER ) {
888 ItemFolder *folder = ( ItemFolder * ) aio;
890 name = ADDRITEM_NAME( folder );
891 if( name ) {
892 if( flag ) {
893 fprintf( stream, "&nbsp;&gt;&nbsp;" );
895 fprintf( stream, "%s", name );
896 flag = TRUE;
899 node = g_list_next( node );
904 * Format an address book folder.
905 * Enter: ctl Export control data.
906 * stream Output stream.
907 * folder Folder.
909 static void exporthtml_fmt_folder(
910 ExportHtmlCtl *ctl, FILE *stream, const ItemFolder *folder )
912 const GList *node;
913 GList *listHeir, *list;
914 const gchar *name;
915 gboolean ret1, ret2;
917 name = ADDRITEM_NAME( folder );
918 if( name ) {
919 listHeir = addritem_folder_path( folder, TRUE );
920 if( listHeir ) {
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 );
933 while( node ) {
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 );
941 if( list ) {
942 g_list_free( list );
947 * Format header block.
948 * Enter: ctl Export control data.
949 * stream Output stream.
950 * title Page title.
952 static void exporthtml_fmt_header(
953 ExportHtmlCtl *ctl, FILE *stream, gchar *title )
955 StylesheetEntry *entry;
957 entry = exporthtml_find_stylesheet( ctl );
959 fprintf( stream,
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",
968 ctl->encoding );
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.
990 * Return: Status.
991 * ============================================================================
993 void exporthtml_process(
994 ExportHtmlCtl *ctl, AddressCache *cache )
996 ItemFolder *rootFolder;
997 FILE *htmlFile;
998 time_t tt;
999 gchar *dsName;
1000 static gchar *title;
1002 htmlFile = g_fopen( ctl->path, "wb" );
1003 if( ! htmlFile ) {
1004 /* Cannot open file */
1005 printf( "Cannot open file for write\n" );
1006 ctl->retVal = MGU_OPEN_FILE;
1007 return;
1010 title = _( "Claws Mail 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 );
1026 tt = time( NULL );
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" );
1033 fclose( htmlFile );
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 ) {
1046 gchar *fileSpec;
1048 fileSpec = g_strconcat(
1049 ctl->dirOutput, G_DIR_SEPARATOR_S, ctl->fileHtml, NULL );
1050 ctl->path = mgu_replace_string( ctl->path, fileSpec );
1051 g_free( 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 ) {
1062 gchar *t;
1063 gchar *base = g_path_get_basename(fileSpec);
1065 ctl->fileHtml =
1066 mgu_replace_string( ctl->fileHtml, base );
1067 g_free(base);
1068 t = g_path_get_dirname( fileSpec );
1069 ctl->dirOutput = mgu_replace_string( ctl->dirOutput, t );
1070 g_free( 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 ) {
1082 gboolean retVal;
1083 DIR *dp;
1085 retVal = FALSE;
1086 if((dp = opendir( ctl->dirOutput )) != NULL) {
1087 retVal = TRUE;
1088 closedir( dp );
1090 return retVal;
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;
1103 ctl->rcCreate = 0;
1104 if( mkdir( ctl->dirOutput, S_IRWXU ) == 0 ) {
1105 retVal = TRUE;
1107 else {
1108 ctl->rcCreate = errno;
1110 return retVal;
1114 * ============================================================================
1115 * Retrieve create directory error message.
1116 * Enter: ctl Export control data.
1117 * Return: Message.
1118 * ============================================================================
1120 gchar *exporthtml_get_create_msg( ExportHtmlCtl *ctl ) {
1121 gchar *msg;
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." );
1132 else {
1133 msg = _( "Not specified." );
1135 return msg;
1139 * Set default values.
1140 * Enter: ctl Export control data.
1142 static void exporthtml_default_values( ExportHtmlCtl *ctl ) {
1143 gchar *str;
1145 str = g_strconcat(
1146 get_home_dir(), G_DIR_SEPARATOR_S,
1147 DFL_DIR_CLAWS_OUT, NULL );
1149 ctl->dirOutput = mgu_replace_string( ctl->dirOutput, str );
1150 g_free( str );
1152 ctl->fileHtml =
1153 mgu_replace_string( ctl->fileHtml, DFL_FILE_CLAWS_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 ) {
1170 XmlProperty *props;
1171 gint rc;
1172 gchar buf[256];
1174 *buf = '\0';
1175 props = xmlprops_create();
1176 xmlprops_set_path( props, ctl->settingsFile );
1177 rc = xmlprops_load_file( props );
1178 if( rc == 0 ) {
1179 /* Read settings */
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 );
1186 ctl->stylesheet =
1187 xmlprops_get_property_i( props, EXMLPROP_STYLESHEET );
1188 ctl->nameFormat =
1189 xmlprops_get_property_i( props, EXMLPROP_FMT_NAME );
1190 ctl->banding =
1191 xmlprops_get_property_b( props, EXMLPROP_BANDING );
1192 ctl->linkEMail =
1193 xmlprops_get_property_b( props, EXMLPROP_FMT_EMAIL );
1194 ctl->showAttribs =
1195 xmlprops_get_property_b( props, EXMLPROP_FMT_ATTRIB );
1197 else {
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 ) {
1214 XmlProperty *props;
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 * ============================================================================
1232 * End of Source.
1233 * ============================================================================