2007-10-07 [colin] 3.0.2cvs30
[claws.git] / src / exporthtml.c
index 7f7a1066d5c51e8b72accc427b8b20e8e496cf51..788d459e25057259a67e6be50b3520e93ffb43be 100644 (file)
@@ -1,10 +1,10 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 2002 Match Grun
+ * Copyright (C) 2002-2007 Match Grun and the Claws Mail team
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * the Free Software Foundation; either version 3 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ * 
  */
 
 /*
  * Export address book to HTML file.
  */
 
+#ifdef HAVE_CONFIG_H
+#  include "config.h"
+#endif
+
 #include <sys/stat.h>
 #include <dirent.h>
 #include <errno.h>
 #include <time.h>
 #include <string.h>
 #include <glib.h>
+#include <glib/gi18n.h>
+
+#ifdef G_OS_WIN32
+#  include <w32lib.h>
+#endif
 
-#include "intl.h"
 #include "mgutils.h"
 #include "utils.h"
 #include "exporthtml.h"
-#include "addritem.h"
-#include "addrcache.h"
-#include "addrbook.h"
 #include "xmlprops.h"
 
-#define DFL_DIR_SYLPHEED_OUT  "sylpheed-out"
-#define DFL_FILE_SYLPHEED_OUT "addressbook.html"
+#ifdef MKDIR_TAKES_ONE_ARG
+#undef mkdir
+#define mkdir(a,b) mkdir(a)
+#endif
+
+#define DFL_DIR_CLAWS_OUT  "claws-mail-out"
+#define DFL_FILE_CLAWS_OUT "addressbook.html"
 
 #define FMT_BUFSIZE         2048
-#define HTML_SPACE          "&nbsp;"
+#define SC_HTML_SPACE          "&nbsp;"
 #define BORDER_SIZE         2
 #define CELL_PADDING        2
 #define CELL_SPACING        2
-#define CHAR_ENCODING       "ISO-8859-1"
+#define CHAR_ENCODING       "UTF-8"
 
 /* Stylesheet names */
 #define FILENAME_NONE       ""
-#define FILENAME_DEFAULT    "sylpheed.css"
+#define FILENAME_DEFAULT    "claws-mail.css"
 #define FILENAME_FULL       "full.css"
 #define FILENAME_CUSTOM     "custom.css"
 #define FILENAME_CUSTOM2    "custom2.css"
@@ -111,7 +121,7 @@ ExportHtmlCtl *exporthtml_create( void ) {
        ctl->path = NULL;
        ctl->dirOutput = NULL;
        ctl->fileHtml = NULL;
-       ctl->encoding = NULL;
+       ctl->encoding = g_strconcat(CHAR_ENCODING, NULL);
        ctl->stylesheet = EXPORT_HTML_ID_NONE;
        ctl->nameFormat = EXPORT_HTML_FIRST_LAST;
        ctl->banding = FALSE;
@@ -190,25 +200,6 @@ void exporthtml_free( ExportHtmlCtl *ctl ) {
        g_free( ctl );
 }
 
-/*
- * Print control object.
- * Enter: ctl    Export control data.
- *        stream Output stream.
- */
-void exporthtml_print( ExportHtmlCtl *ctl, FILE *stream ) {
-       fprintf( stream, "ExportHtmlCtl:\n" );
-       fprintf( stream, "     path: %s\n", ctl->path );
-       fprintf( stream, "directory: %s\n", ctl->dirOutput );
-       fprintf( stream, "     file: %s\n", ctl->fileHtml );
-       fprintf( stream, " settings: %s\n", ctl->settingsFile );
-       fprintf( stream, " encoding: %s\n", ctl->encoding );
-       fprintf( stream, " css file: %d\n", ctl->stylesheet );
-       fprintf( stream, " name fmt: %d\n", ctl->nameFormat );
-       fprintf( stream, "  banding: %d\n", ctl->banding );
-       fprintf( stream, "link mail: %d\n", ctl->linkEMail );
-       fprintf( stream, "show attr: %d\n", ctl->showAttribs );
-}
-
 /*
  * Find style entry.
  * Enter: ctl Export control data.
@@ -230,34 +221,6 @@ static StylesheetEntry *exporthtml_find_stylesheet( ExportHtmlCtl *ctl ) {
        return retVal;
 }
 
-/*
- * Properties...
- */
-/*
- * Specify path to folder where files are created.
- * Enter: ctl   Export control data.
- *        value Full directory path.
- */
-void exporthtml_set_output_dir( ExportHtmlCtl *ctl, const gchar *value ) {
-       g_return_if_fail( ctl != NULL );
-       ctl->dirOutput = mgu_replace_string( ctl->dirOutput, value );
-       g_strstrip( ctl->dirOutput );
-}
-void exporthtml_set_path( ExportHtmlCtl *ctl, const gchar *value ) {
-       g_return_if_fail( ctl != NULL );
-       ctl->path = mgu_replace_string( ctl->path, value );
-       g_strstrip( ctl->path );
-}
-void exporthtml_set_file_html( ExportHtmlCtl *ctl, const gchar *value ) {
-       g_return_if_fail( ctl != NULL );
-       ctl->fileHtml = mgu_replace_string( ctl->fileHtml, value );
-       g_strstrip( ctl->fileHtml );
-}
-void exporthtml_set_encoding( ExportHtmlCtl *ctl, const gchar *value ) {
-       g_return_if_fail( ctl != NULL );
-       ctl->encoding = mgu_replace_string( ctl->encoding, value );
-       g_strstrip( ctl->encoding );
-}
 void exporthtml_set_stylesheet( ExportHtmlCtl *ctl, const gint value ) {
        g_return_if_fail( ctl != NULL );
        ctl->stylesheet = value;
@@ -287,12 +250,12 @@ void exporthtml_set_attributes( ExportHtmlCtl *ctl, const gboolean value ) {
 static gint exporthtml_create_css_dfl( const gchar *fileSpec ) {
        FILE *cssFile;
 
-       cssFile = fopen( fileSpec, "rb" );
+       cssFile = g_fopen( fileSpec, "rb" );
        if( cssFile ) {
                fclose( cssFile );
                return MGU_SUCCESS;
        }
-       cssFile = fopen( fileSpec, "wb" );
+       cssFile = g_fopen( fileSpec, "wb" );
        if( ! cssFile ) {
                return MGU_OPEN_FILE;
        }
@@ -345,12 +308,12 @@ static gint exporthtml_create_css_dfl( const gchar *fileSpec ) {
 static gint exporthtml_create_css_full( const gchar *fileSpec ) {
        FILE *cssFile;
 
-       cssFile = fopen( fileSpec, "rb" );
+       cssFile = g_fopen( fileSpec, "rb" );
        if( cssFile ) {
                fclose( cssFile );
                return MGU_SUCCESS;
        }
-       cssFile = fopen( fileSpec, "wb" );
+       cssFile = g_fopen( fileSpec, "wb" );
        if( ! cssFile ) {
                return MGU_OPEN_FILE;
        }
@@ -441,7 +404,7 @@ static gint exporthtml_compare_name(
        if( item2 ) name2 = ADDRITEM_NAME( item2 );
        if( ! name1 ) return ( name2 != NULL );
        if( ! name2 ) return -1;
-       return strcasecmp( name1, name2 );
+       return g_utf8_collate( name1, name2 );
 }
 
 /*
@@ -457,7 +420,7 @@ static gint exporthtml_compare_email(
        if( email2 ) name2 = email2->address;
        if( ! name1 ) return ( name2 != NULL );
        if( ! name2 ) return -1;
-       return strcasecmp( name1, name2 );
+       return g_utf8_collate( name1, name2 );
 }
 
 /*
@@ -473,7 +436,7 @@ static gint exporthtml_compare_attrib(
        if( attr2 ) name2 = attr2->name;
        if( ! name1 ) return ( name2 != NULL );
        if( ! name2 ) return -1;
-       return strcasecmp( name1, name2 );
+       return g_utf8_collate( name1, name2 );
 }
 
 /*
@@ -553,7 +516,7 @@ static void exporthtml_fmt_email(
        gchar *name;
 
        if( listEMail == NULL ) {
-               fprintf( stream, HTML_SPACE );
+               fprintf( stream, SC_HTML_SPACE );
                return;
        }
 
@@ -629,7 +592,7 @@ static gboolean exporthtml_fmt_group(
        fprintf( stream, "%s", _( "Group Name" ) );
        fprintf( stream, "</th>\n" );
        fprintf( stream, "  <th width=\"300\">" );
-       fprintf( stream, "%s", _( "E-Mail Address" ) );
+       fprintf( stream, "%s", _( "Email Address" ) );
        fprintf( stream, "</th>\n" );
        fprintf( stream, "</tr>\n" );
        list = exporthtml_sort_name( folder->listGroup );
@@ -684,7 +647,7 @@ static void exporthtml_fmt_attribs(
        GList *list;
 
        if( listAttr == NULL ) {
-               fprintf( stream, HTML_SPACE );
+               fprintf( stream, SC_HTML_SPACE );
                return;
        }
 
@@ -793,7 +756,7 @@ static gboolean exporthtml_fmt_person(
        fprintf( stream, "%s", _( "Display Name" ) );
        fprintf( stream, "</th>\n" );
        fprintf( stream, "  <th width=\"300\">" );
-       fprintf( stream, "%s", _( "E-Mail Address" ) );
+       fprintf( stream, "%s", _( "Email Address" ) );
        fprintf( stream, "</th>\n" );
        fprintf( stream, "  <th width=\"200\">" );
        fprintf( stream, "%s", _( "Full Name" ) );
@@ -842,7 +805,7 @@ static gboolean exporthtml_fmt_person(
                                fprintf( stream, "%s", buf );
                        }
                        else {
-                               fprintf( stream, "%s", HTML_SPACE );
+                               fprintf( stream, "%s", SC_HTML_SPACE );
                        }
                        fprintf( stream, "</td>\n" );
 
@@ -951,7 +914,9 @@ static void exporthtml_fmt_header(
        entry = exporthtml_find_stylesheet( ctl );
 
        fprintf( stream,
-               "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\">\n" );
+               "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n" );
+       fprintf( stream,
+                "\"http://www.w3.org/TR/html4/loose.dtd\">\n" );
        fprintf( stream, "<html>\n" );
        fprintf( stream, "<head>\n" );
 
@@ -972,7 +937,6 @@ static void exporthtml_fmt_header(
                        fprintf( stream, "href=\"%s\" >\n", entry->fileName );
                }
        }
-       fprintf( stream, "<body>\n" );
        fprintf( stream, "</head>\n" );
 }
 
@@ -992,16 +956,17 @@ void exporthtml_process(
        time_t tt;
        gchar *dsName;
        static gchar *title;
+       gchar buf[512];
 
-       htmlFile = fopen( ctl->path, "wb" );
+       htmlFile = g_fopen( ctl->path, "wb" );
        if( ! htmlFile ) {
                /* Cannot open file */
-               printf( "Cannot open file for write\n" );
+               g_print( "Cannot open file for write\n" );
                ctl->retVal = MGU_OPEN_FILE;
                return;
        }
 
-       title = _( "Sylpheed Address Book" );
+       title = _( "Claws Mail Address Book" );
        rootFolder = cache->rootFolder;
        dsName = cache->name;
 
@@ -1018,8 +983,8 @@ void exporthtml_process(
        exporthtml_fmt_folder( ctl, htmlFile, rootFolder );
 
        tt = time( NULL );
-       fprintf( htmlFile, "<p>%s</p>\n", ctime( &tt ) );
-       fprintf( htmlFile, "<hr width=\"100\%\"></hr>\n" );
+       fprintf( htmlFile, "<p>%s</p>\n", ctime_r( &tt, buf ) );
+       fprintf( htmlFile, "<hr width=\"100%%\">\n" );
 
        fprintf( htmlFile, "</body>\n" );
        fprintf( htmlFile, "</html>\n" );
@@ -1054,10 +1019,12 @@ static void exporthtml_build_filespec( ExportHtmlCtl *ctl ) {
  */
 void exporthtml_parse_filespec( ExportHtmlCtl *ctl, gchar *fileSpec ) {
        gchar *t;
+       gchar *base = g_path_get_basename(fileSpec);
 
        ctl->fileHtml =
-               mgu_replace_string( ctl->fileHtml, g_basename( fileSpec ) );
-       t = g_dirname( fileSpec );
+               mgu_replace_string( ctl->fileHtml, base );
+       g_free(base);
+       t = g_path_get_dirname( fileSpec );
        ctl->dirOutput = mgu_replace_string( ctl->dirOutput, t );
        g_free( t );
        ctl->path = mgu_replace_string( ctl->path, fileSpec );
@@ -1135,14 +1102,14 @@ static void exporthtml_default_values( ExportHtmlCtl *ctl ) {
        gchar *str;
 
        str = g_strconcat(
-               g_get_home_dir(), G_DIR_SEPARATOR_S,
-               DFL_DIR_SYLPHEED_OUT, NULL );
+               get_home_dir(), G_DIR_SEPARATOR_S,
+               DFL_DIR_CLAWS_OUT, NULL );
 
        ctl->dirOutput = mgu_replace_string( ctl->dirOutput, str );
        g_free( str );
 
        ctl->fileHtml =
-               mgu_replace_string( ctl->fileHtml, DFL_FILE_SYLPHEED_OUT );
+               mgu_replace_string( ctl->fileHtml, DFL_FILE_CLAWS_OUT );
        ctl->encoding = NULL;
        ctl->stylesheet = EXPORT_HTML_ID_DEFAULT;
        ctl->nameFormat = EXPORT_HTML_FIRST_LAST;
@@ -1215,7 +1182,8 @@ void exporthtml_save_settings( ExportHtmlCtl *ctl ) {
        xmlprops_set_property_b( props, EXMLPROP_BANDING, ctl->banding );
        xmlprops_set_property_b( props, EXMLPROP_FMT_EMAIL, ctl->linkEMail );
        xmlprops_set_property_b( props, EXMLPROP_FMT_ATTRIB, ctl->showAttribs );
-       xmlprops_save_file( props );
+       if (xmlprops_save_file( props ) != MGU_SUCCESS)
+               g_warning("can't save settings");
        xmlprops_free( props );
 }