RssReader: use charset reported by http server
authorKarlson2k <k2k@narod.ru>
Mon, 28 Oct 2013 18:10:06 +0000 (22:10 +0400)
committerKarlson2k <k2k@narod.ru>
Tue, 5 Nov 2013 11:41:15 +0000 (15:41 +0400)
xbmc/utils/RssReader.cpp
xbmc/utils/RssReader.h

index a524847..d85dd93 100644 (file)
@@ -142,6 +142,7 @@ void CRssReader::Process()
 
     int nRetries = 3;
     CURL url(strUrl);
+    std::string fileCharset;
 
     // we wait for the network to come up
     if ((url.GetProtocol() == "http" || url.GetProtocol() == "https") &&
@@ -162,18 +163,18 @@ void CRssReader::Process()
 
         if (url.GetProtocol() != "http" && url.GetProtocol() != "https")
         {
-          void* bufferPtr;
-          const unsigned int fsize = CFileUtils::LoadFile(strUrl, bufferPtr);
-          if (fsize != 0)
+          CFile file;
+          auto_buffer buffer;
+          if (file.LoadFile(strUrl, buffer))
           {
-            strXML.assign((const char*)bufferPtr, fsize);
-            free(bufferPtr);
+            strXML.assign(buffer.get(), buffer.length());
             break;
           }
         }
         else
           if (http.Get(strUrl, strXML))
           {
+            fileCharset = http.GetServerReportedCharset();
             CLog::Log(LOGDEBUG, "Got rss feed: %s", strUrl.c_str());
             break;
           }
@@ -196,8 +197,7 @@ void CRssReader::Process()
         iStart = strXML.Find("<content:encoded>");
       }
 
-      // TODO: Use server reported charset
-      if (Parse((LPSTR)strXML.c_str(), iFeed))
+      if (Parse(strXML, iFeed, fileCharset))
         CLog::Log(LOGDEBUG, "Parsed rss feed: %s", strUrl.c_str());
     }
   }
@@ -285,7 +285,7 @@ void CRssReader::GetNewsItems(TiXmlElement* channelXmlNode, int iFeed)
 
           CStdStringW unicodeText, unicodeText2;
 
-          fromRSSToUTF16(htmlText, unicodeText2);
+          g_charsetConverter.utf8ToW(htmlText, unicodeText2, m_rtlText);
           html.ConvertHTMLToW(unicodeText2, unicodeText);
 
           mTagElements.insert(StrPair(*i, unicodeText));
@@ -312,23 +312,10 @@ void CRssReader::GetNewsItems(TiXmlElement* channelXmlNode, int iFeed)
   }
 }
 
-void CRssReader::fromRSSToUTF16(const CStdStringA& strSource, CStdStringW& strDest)
-{
-  CStdString flippedStrSource, strSourceUtf8;
-
-  g_charsetConverter.ToUtf8(m_encoding, strSource, strSourceUtf8);
-  if (m_rtlText)
-    g_charsetConverter.utf8logicalToVisualBiDi(strSourceUtf8, flippedStrSource);
-  else
-    flippedStrSource = strSourceUtf8;
-  g_charsetConverter.utf8ToW(flippedStrSource, strDest, false);
-}
-
-bool CRssReader::Parse(LPSTR szBuffer, int iFeed)
+bool CRssReader::Parse(const std::string& data, int iFeed, const std::string& charset)
 {
   m_xml.Clear();
-  m_xml.Parse((LPCSTR)szBuffer);
-  m_encoding = "UTF-8"; // TODO: remove member variable
+  m_xml.Parse(data, charset);
 
   CLog::Log(LOGDEBUG, "RSS feed encoding: %s", m_xml.GetUsedCharset().c_str());
 
@@ -361,7 +348,7 @@ bool CRssReader::Parse(int iFeed)
     {
       CStdString strChannel = titleNode->FirstChild()->Value();
       CStdStringW strChannelUnicode;
-      fromRSSToUTF16(strChannel, strChannelUnicode);
+      g_charsetConverter.utf8ToW(strChannel, strChannelUnicode, m_rtlText);
       AddString(strChannelUnicode, RSS_COLOR_CHANNEL, iFeed);
 
       AddString(":", RSS_COLOR_CHANNEL, iFeed);
index 692e524..2c6f366 100644 (file)
@@ -35,7 +35,7 @@ public:
   virtual ~CRssReader();
 
   void Create(IRssObserver* aObserver, const std::vector<std::string>& aUrl, const std::vector<int>& times, int spacesBetweenFeeds, bool rtl);
-  bool Parse(LPSTR szBuffer, int iFeed);
+  bool Parse(const std::string& data, int iFeed, const std::string& charset);
   void getFeed(vecText &text);
   void AddTag(const CStdString &addTag);
   void AddToQueue(int iAdd);
@@ -46,7 +46,6 @@ public:
   unsigned int m_SavedScrollPos;
 
 private:
-  void fromRSSToUTF16(const CStdStringA& strSource, CStdStringW& strDest);
   void Process();
   bool Parse(int iFeed);
   void GetNewsItems(TiXmlElement* channelXmlNode, int iFeed);
@@ -67,7 +66,6 @@ private:
   std::vector<std::string> m_vecUrls;
   std::vector<int> m_vecQueue;
   bool m_bIsRunning;
-  CStdString m_encoding;
   bool m_rtlText;
   bool m_requestRefresh;