[mythtv] [mythvideo PATCH] Fixed problems with non ASCII characters in meta data

Mikael Magnusson 6e0qv4o102 at sneakemail.com
Sat Jul 19 01:30:26 EDT 2003


This patch solves the following problems.

1. Expressions of the form "{" appearing in the meta data.
I have implemented a decoder for such numeric character references. They 
are used by www.imdb.com for non ASCII characters.

2. Non ASCII characters are changed to "?" in the meta data.
QString::sprintf expects the %s string parameters to be in UTF8 format. 
Fixed

2. Can't play movies which file name contains non ASCII characters.
The system command should be converted to local8Bit. Fixed

I have tested the patch on an UTF-8 locale and briefly on ISO-8859-1, 
and it seems to work.

The program table in the mythconverg database is encoded in UTF8. I 
think the meta data should be in UTF8 also. Currently the latin1 is 
used.

/Mikael
-------------- next part --------------
Index: metadata.cpp
===================================================================
RCS file: /var/lib/mythcvs/mythvideo/mythvideo/metadata.cpp,v
retrieving revision 1.3
diff -u -r1.3 metadata.cpp
--- metadata.cpp	12 Jul 2003 02:19:16 -0000	1.3
+++ metadata.cpp	18 Jul 2003 21:38:11 -0000
@@ -136,10 +136,12 @@
     thequery.sprintf("INSERT INTO videometadata (title,director,plot,"
                               "rating,year,userrating,length,filename,showlevel,coverfile,inetref) VALUES "
                               "(\"%s\",\"%s\",\"%s\",\"%s\",%d,%f,%d,\"%s\",%d,\"%s\",\"%s\");",
-                              title.latin1(), director.latin1(),
-                              plot.latin1(), rating.latin1(), year,
-                              userrating, length, sqlfilename.ascii(), showlevel,
-                              sqlcoverfile.ascii(), inetref.ascii());
+                              title.utf8().data(), director.utf8().data(),
+                              plot.utf8().data(), rating.utf8().data(), year,
+                              userrating, length, sqlfilename.utf8().data(), 
+		              showlevel,
+                              sqlcoverfile.utf8().data(),
+		              inetref.utf8().data());
 
     db->exec(thequery);
 
@@ -188,10 +190,12 @@
     thequery.sprintf("UPDATE videometadata SET title=\"%s\",director=\"%s\",plot=\"%s\","
                               "rating=\"%s\",year=%d,userrating=%f,length=%d,filename=\"%s\","
                               "showlevel=%d,coverfile=\"%s\",inetref=\"%s\" WHERE intid=%d",
-                              title.latin1(), director.latin1(),
-                              plot.latin1(), rating.latin1(), year,
-                              userrating, length, sqlfilename.ascii(), showlevel,
-                              sqlcoverfile.ascii(), inetref.ascii(), id);
+                              title.utf8().data(), director.utf8().data(),
+                              plot.utf8().data(), rating.utf8().data(), year,
+                              userrating, length, sqlfilename.utf8().data(),
+		              showlevel,
+                              sqlcoverfile.utf8().data(),
+		              inetref.utf8().data(), id);
 
     db->exec(thequery);
 }
Index: videobrowser.cpp
===================================================================
RCS file: /var/lib/mythcvs/mythvideo/mythvideo/videobrowser.cpp,v
retrieving revision 1.6
diff -u -r1.6 videobrowser.cpp
--- videobrowser.cpp	12 Jul 2003 02:19:16 -0000	1.6
+++ videobrowser.cpp	18 Jul 2003 21:38:11 -0000
@@ -218,7 +218,7 @@
   }
   else if (m_state == 4)
   {
-    system((QString("%1 ") .arg(m_cmd)).ascii());
+    system((QString("%1 ") .arg(m_cmd)).local8Bit());
 
     backup.begin(this);
     backup.drawPixmap(0, 0, myBackground);
@@ -497,7 +497,8 @@
 
     QString handler = gContext->GetSetting("VideoDefaultPlayer");
     QString arg;
-    arg.sprintf("\"%s\"", filename.replace(QRegExp("\""), "\\\"").ascii());
+    arg.sprintf("\"%s\"", 
+		filename.replace(QRegExp("\""), "\\\"").utf8().data());
     QString command = handler.replace(QRegExp("%s"), arg);
 
     cout << "command:" << command << endl;
Index: videomanager.cpp
===================================================================
RCS file: /var/lib/mythcvs/mythvideo/mythvideo/videomanager.cpp,v
retrieving revision 1.8
diff -u -r1.8 videomanager.cpp
--- videomanager.cpp	12 Jul 2003 02:19:16 -0000	1.8
+++ videomanager.cpp	18 Jul 2003 21:38:13 -0000
@@ -191,6 +191,41 @@
     updateML = false;
 }
 
+
+// Replace the numeric character references
+// See http://www.w3.org/TR/html4/charset.html#h-5.3.1
+static void replaceNumCharRefs(QString &str)
+{
+    QString &ret = str;
+    int pos = 0;
+    QRegExp re("&#(\\d+|(x|X)[0-9a-fA-F]+);");
+    
+    while ((pos = re.search(ret, pos)) != -1) {
+	int len = re.matchedLength();
+	QString numStr = re.cap(1);
+	bool ok = false;
+	int num = -1;
+
+	if (numStr[0] == 'x' || numStr[0] == 'X') {
+	    QString hexStr = numStr.right(numStr.length() - 1);
+	    num = hexStr.toInt(&ok, 16);
+	} else {
+	    num = numStr.toInt(&ok, 10);
+	}
+
+	QChar rep('X');
+
+	if (ok) {
+	    rep = QChar(num);
+	}
+
+	ret.replace(pos, len, rep);
+
+	pos += 1;
+    }
+}
+
+
 QString VideoManager::parseData(QString data, QString beg, QString end)
 {
     bool debug = false;
@@ -206,8 +241,7 @@
     {
         ret = data.mid(start, endint - start);
 
-        ret.replace(QRegExp("&#38;"), "&");
-        ret.replace(QRegExp("&#34;"), "\"");
+	replaceNumCharRefs(ret);
 
         if (debug == true)
             cout << "MythVideo: Parse HTML : Returning : " << ret << endl;
Index: videotree.cpp
===================================================================
RCS file: /var/lib/mythcvs/mythvideo/mythvideo/videotree.cpp,v
retrieving revision 1.8
diff -u -r1.8 videotree.cpp
--- videotree.cpp	12 Jul 2003 02:19:16 -0000	1.8
+++ videotree.cpp	18 Jul 2003 21:38:14 -0000
@@ -183,7 +183,8 @@
         QString filename = node_data->Filename();
         QString handler = gContext->GetSetting("VideoDefaultPlayer");
         QString arg;
-        arg.sprintf("\"%s\"", filename.replace(QRegExp("\""), "\\\"").ascii());
+        arg.sprintf("\"%s\"", 
+		    filename.replace(QRegExp("\""), "\\\"").utf8().data());
         QString command = handler.replace(QRegExp("%s"), arg);
 
         // cout << "command:" << command << endl;
@@ -192,7 +193,7 @@
         //  Run the player
         //
         
-        system((QString("%1 ").arg(command)).ascii());
+        system((QString("%1 ").arg(command)).local8Bit());
         video_tree_list->deactivate();
         raise();
         setActiveWindow();


More information about the mythtv-dev mailing list