[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-pim
Subject:    [Kde-pim] [patch] vcard parsing in KDE3.5
From:       Martin Koller <m.koller () surfeu ! at>
Date:       2008-01-05 12:52:16
Message-ID: 200801051352.16719.m.koller () surfeu ! at
[Download RAW message or body]

[Attachment #2 (multipart/signed)]

[Attachment #4 (multipart/mixed)]


Hi all,

attached is a patch that corrects the vcard parsing bug (kdelibs/kabc) dealing 
with different charsets in vcards.
It solves e.g. https://bugs.kde.org/show_bug.cgi?id=98790
This is mainly a backport from KDE4 where it's already solved.

It also fixes the kdepim vcard thumbnailcreator and the kfile_vcf plugin.

Is this ok to commit ?

-- 
Best regards/Schöne Grüße

Martin    ()  ascii ribbon campaign - against html mail 
          /\                        - against microsoft attachments

["charset.patch" (text/x-diff)]

Index: vcardformatplugin.cpp
===================================================================
--- vcardformatplugin.cpp	(revision 755969)
+++ vcardformatplugin.cpp	(working copy)
@@ -41,7 +41,7 @@
   QString data;
 
   QTextStream t( file );
-  t.setEncoding( QTextStream::UnicodeUTF8 );
+  t.setEncoding( QTextStream::Latin1 );
   data = t.read();
 
   VCardConverter converter;
@@ -60,7 +60,7 @@
   QString data;
 
   QTextStream t( file );
-  t.setEncoding( QTextStream::UnicodeUTF8 );
+  t.setEncoding( QTextStream::Latin1 );
   data = t.read();
 
   VCardConverter converter;
Index: vcardparser/testread.cpp
===================================================================
--- vcardparser/testread.cpp	(revision 755969)
+++ vcardparser/testread.cpp	(working copy)
@@ -70,7 +70,7 @@
   QString text;
 
   QTextStream s( &file );
-  s.setEncoding( QTextStream::UnicodeUTF8 );
+  s.setEncoding( QTextStream::Latin1 );
   text = s.read();
   file.close();
 
Index: vcardparser/vcardparser.cpp
===================================================================
--- vcardparser/vcardparser.cpp	(revision 755969)
+++ vcardparser/vcardparser.cpp	(working copy)
@@ -19,6 +19,7 @@
 */
 
 #include <qregexp.h>
+#include <qtextcodec.h>
 
 #include <kmdcodec.h>
 
@@ -129,31 +130,44 @@
 
         removeEscapes( value );
 
+        QByteArray output;
+        bool wasBase64Encoded = false;
+
         params = vCardLine.parameterList();
         if ( params.findIndex( "encoding" ) != -1 ) { // have to decode the data
-          QByteArray input, output;
-          input = value.local8Bit();
+          QByteArray input;
+          input = QCString(value.latin1());
           if ( vCardLine.parameter( "encoding" ).lower() == "b" ||
-               vCardLine.parameter( "encoding" ).lower() == "base64" )
+               vCardLine.parameter( "encoding" ).lower() == "base64" ) {
             KCodecs::base64Decode( input, output );
+            wasBase64Encoded = true;
+          }
           else if ( vCardLine.parameter( "encoding" ).lower() == "quoted-printable" ) {
             // join any qp-folded lines
             while ( value.at( value.length() - 1 ) == '=' && it != linesEnd ) {
               value = value.remove( value.length() - 1, 1 ) + (*it);
               ++it;
             }
-            input = value.local8Bit();
+            input = QCString(value.latin1());
             KCodecs::quotedPrintableDecode( input, output );
           }
-          if ( vCardLine.parameter( "charset" ).lower() == "utf-8" ) {
-            vCardLine.setValue( QString::fromUtf8( output.data(), output.size() ) );
+        } else {
+          output = QCString(value.latin1());
+        }
+
+        if ( params.findIndex( "charset" ) != -1 ) { // have to convert the data
+          QTextCodec *codec =
+            QTextCodec::codecForName( vCardLine.parameter( "charset" ).latin1() );
+          if ( codec ) {
+            vCardLine.setValue( codec->toUnicode( output ) );
           } else {
+            vCardLine.setValue( QString::fromUtf8( output ) );
+          }
+        } else if ( wasBase64Encoded ) {
             vCardLine.setValue( output );
-          }
-        } else if ( vCardLine.parameter( "charset" ).lower() == "utf-8" ) {
-          vCardLine.setValue( QString::fromUtf8( value.ascii() ) );
-        } else
-          vCardLine.setValue( value );
+        } else {  // if charset not given, assume it's in UTF-8 (as used in previous KDE versions)
+            vCardLine.setValue( QString::fromUtf8( output ) );
+        }
 
         currentVCard.addLine( vCardLine );
       }

["thumbnail.patch" (text/x-diff)]

Index: thumbnailcreator/ldifvcardcreator.cpp
===================================================================
--- thumbnailcreator/ldifvcardcreator.cpp	(revision 756144)
+++ thumbnailcreator/ldifvcardcreator.cpp	(working copy)
@@ -75,9 +75,7 @@
   text.truncate(0);
 
   // read the file
-  QTextStream t( &file );
-  t.setEncoding( QTextStream::UnicodeUTF8 );
-  QString contents = t.read();
+  QString contents = file.readAll();
   file.close();
 
   // convert the file contents to a KABC::Addressee address

["vcf.patch" (text/x-diff)]

Index: Makefile.am
===================================================================
--- Makefile.am	(revision 752148)
+++ Makefile.am	(working copy)
@@ -10,7 +10,7 @@
 
 kfile_vcf_la_SOURCES = kfile_vcf.cpp 
 kfile_vcf_la_LDFLAGS = $(all_libraries) -module $(KDE_PLUGIN)
-kfile_vcf_la_LIBADD = $(LIB_KSYCOCA)
+kfile_vcf_la_LIBADD = $(LIB_KSYCOCA) $(LIB_KABC)
 
 # let automoc handle all of the meta source files (moc)
 METASOURCES = AUTO
Index: kfile_vcf.cpp
===================================================================
--- kfile_vcf.cpp	(revision 752148)
+++ kfile_vcf.cpp	(working copy)
@@ -24,20 +24,11 @@
 #include <kprocess.h>
 #include <klocale.h>
 #include <kgenericfactory.h>
-#include <kstringvalidator.h>
+#include <kabc/vcardconverter.h>
 
 #include <qdict.h>
-#include <qvalidator.h>
-#include <qcstring.h>
 #include <qfile.h>
-#include <qdatetime.h>
 
-#if !defined(__osf__)
-#include <inttypes.h>
-#else
-typedef unsigned short uint32_t;
-#endif
-
 typedef KGenericFactory<KVcfPlugin> VcfFactory;
 
 K_EXPORT_COMPONENT_FACTORY(kfile_vcf, VcfFactory( "kfile_vcf" ))
@@ -63,7 +54,6 @@
 
 bool KVcfPlugin::readInfo( KFileMetaInfo& info, uint /*what*/ )
 {
-
     QFile file(info.path());
 
     if (!file.open(IO_ReadOnly))
@@ -72,58 +62,38 @@
         return false;
     }
 
-    char id_name[] = "FN:";
-    char id_email[] = "EMAIL;INTERNET:";
+    // even the vcard thumbnail QString::fromUtf8(buf_name));creator reads the full file ...
+    // The following is partly copied from there
+    QString contents = file.readAll();
+    file.close();
 
-    // we need a buffer for lines
-    char linebuf[1000];
+    KABC::VCardConverter converter;
+    KABC::Addressee addr = converter.parseVCard(contents);
 
-    // we need a buffer for other stuff
-    char buf_name[1000] = "";
-    char buf_email[1000] = "";
-    buf_name[999] = '\0';
-    buf_email[999] = '\0';
-    char * myptr;
+    KFileMetaInfoGroup group = appendGroup(info, "Technical");
 
-    // FIXME: This is intensely inefficient!!!
+    // prepare the text
+    QString name = addr.formattedName().simplifyWhiteSpace();
+    if ( name.isEmpty() )
+      name = addr.givenName() + " " + addr.familyName();
+    name = name.simplifyWhiteSpace();
 
-    bool done=false;
-    while (!done) {
+    if ( ! name.isEmpty() )
+        appendItem(group, "Name", name);
 
-        // read a line
-        int r = file.readLine(linebuf, sizeof( linebuf ));
+    if ( ! addr.preferredEmail().isEmpty() )
+        appendItem(group, "Email", addr.preferredEmail());
 
-        if ( r < 0 ) {
-            done = true;
-            break;
-        }
-
-        // have we got something useful?
-        if (memcmp(linebuf, id_name, 3) == 0) {
-            // we have a name
-            myptr = linebuf + 3;
-            strlcpy(buf_name, myptr, sizeof( buf_name ));
-        } else if (memcmp(linebuf, id_email, 15) == 0) {
-            // we have an email
-            myptr = linebuf + 15;
-            strlcpy(buf_email, myptr, sizeof( buf_email ));
-        }
-
-        // are we done yet?
-        if ((strlen(buf_name) > 0 && strlen(buf_email) > 0) || file.atEnd())
-            done = true;
-
+    KABC::PhoneNumber::List pnList = addr.phoneNumbers();
+    QStringList phoneNumbers;
+    for (unsigned int no=0; no<pnList.count(); ++no) {
+      QString pn = pnList[no].number().simplifyWhiteSpace();
+      if (!pn.isEmpty() && !phoneNumbers.contains(pn))
+        phoneNumbers.append(pn);
     }
+    if ( !phoneNumbers.isEmpty() )
+        appendItem(group, "Telephone", phoneNumbers.join("\n"));
 
-
-    KFileMetaInfoGroup group = appendGroup(info, "Technical");
-
-    if (strlen(buf_name) > 0)
-        appendItem(group, "Name", QString::fromUtf8(buf_name));
-
-    if (strlen(buf_email) > 0)
-        appendItem(group, "Email", buf_email);
-
     return true;
 }
 

["signature.asc" (application/pgp-signature)]

_______________________________________________
KDE PIM mailing list kde-pim@kde.org
https://mail.kde.org/mailman/listinfo/kde-pim
KDE PIM home page at http://pim.kde.org/

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic