Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 383 lines (317 sloc) 9.105 kb
e688c17 runtime: Added a motherload of utils classes. All of these classes are ...
runtime authored
1 // RssReader.cpp: implementation of the CRssReader class.
2 //
3 //////////////////////////////////////////////////////////////////////
4
2bf9126 fixed: rss feed could crash
bobbin007 authored
5 #include "../stdafx.h"
e688c17 runtime: Added a motherload of utils classes. All of these classes are ...
runtime authored
6 #include "RssReader.h"
7 #include "Http.h"
eadc897 rss feeds now handle special html characters
jwnmulder authored
8 #include "../utils/HTMLUtil.h"
e688c17 runtime: Added a motherload of utils classes. All of these classes are ...
runtime authored
9
10 //////////////////////////////////////////////////////////////////////
11 // Construction/Destruction
12 //////////////////////////////////////////////////////////////////////
13
14 CRssReader::CRssReader() : CThread()
15 {
cf4743e Formatting for tabs -> 2 spaces
jmarshallnz authored
16 m_pObserver = NULL;
17 m_iLeadingSpaces = 0;
c7e2430 added: ability to select update interval for rss feeds.
spiff_ authored
18 m_bIsRunning = false;
47e12be fixed: SF bug [ 1065692 ] RSS does not work for non-Latin character. You...
yuvalt authored
19 m_iconv = (iconv_t) -1;
20 m_shouldFlip = false;
e688c17 runtime: Added a motherload of utils classes. All of these classes are ...
runtime authored
21 }
22
23 CRssReader::~CRssReader()
c7e2430 added: ability to select update interval for rss feeds.
spiff_ authored
24 {
25 if (m_bIsRunning)
26 {
27 StopThread();
28 m_bIsRunning = false;
29 }
30 }
e688c17 runtime: Added a motherload of utils classes. All of these classes are ...
runtime authored
31
c7e2430 added: ability to select update interval for rss feeds.
spiff_ authored
32 void CRssReader::Create(IRssObserver* aObserver, const vector<wstring>& aUrls, INT iLeadingSpaces)
e688c17 runtime: Added a motherload of utils classes. All of these classes are ...
runtime authored
33 {
cf4743e Formatting for tabs -> 2 spaces
jmarshallnz authored
34 m_pObserver = aObserver;
35 m_iLeadingSpaces = iLeadingSpaces;
729886c added: sf.net patch [1209466] Multiple RSS feeds concatenated - thanks t...
spiff_ authored
36 m_vecUrls = aUrls;
c7e2430 added: ability to select update interval for rss feeds.
spiff_ authored
37 m_strFeed.resize(aUrls.size());
38 m_strColors.resize(aUrls.size());
e688c17 runtime: Added a motherload of utils classes. All of these classes are ...
runtime authored
39 }
40
c7e2430 added: ability to select update interval for rss feeds.
spiff_ authored
41 void CRssReader::AddToQueue(int iAdd)
e688c17 runtime: Added a motherload of utils classes. All of these classes are ...
runtime authored
42 {
c7e2430 added: ability to select update interval for rss feeds.
spiff_ authored
43 if (iAdd < (int)m_vecUrls.size())
44 m_vecQueue.push_back(iAdd);
45 if (!m_bIsRunning)
46 {
47 m_bIsRunning = true;
48 StopThread();
49 CThread::Create();
50 }
729886c added: sf.net patch [1209466] Multiple RSS feeds concatenated - thanks t...
spiff_ authored
51 }
cf4743e Formatting for tabs -> 2 spaces
jmarshallnz authored
52
c7e2430 added: ability to select update interval for rss feeds.
spiff_ authored
53 void CRssReader::OnExit()
729886c added: sf.net patch [1209466] Multiple RSS feeds concatenated - thanks t...
spiff_ authored
54 {
c7e2430 added: ability to select update interval for rss feeds.
spiff_ authored
55 m_bIsRunning = false;
729886c added: sf.net patch [1209466] Multiple RSS feeds concatenated - thanks t...
spiff_ authored
56 }
cf4743e Formatting for tabs -> 2 spaces
jmarshallnz authored
57
729886c added: sf.net patch [1209466] Multiple RSS feeds concatenated - thanks t...
spiff_ authored
58 void CRssReader::Process()
59 {
60 int tempLeading = m_iLeadingSpaces;
47e12be fixed: SF bug [ 1065692 ] RSS does not work for non-Latin character. You...
yuvalt authored
61 CStdStringW strFeed;
c7e2430 added: ability to select update interval for rss feeds.
spiff_ authored
62 LPBYTE pbColors = NULL;
63 while (m_vecQueue.size())
cf4743e Formatting for tabs -> 2 spaces
jmarshallnz authored
64 {
c7e2430 added: ability to select update interval for rss feeds.
spiff_ authored
65 int iFeed = m_vecQueue.front();
66 m_vecQueue.erase(m_vecQueue.begin());
cf4743e Formatting for tabs -> 2 spaces
jmarshallnz authored
67
c7e2430 added: ability to select update interval for rss feeds.
spiff_ authored
68 m_strFeed[iFeed] = "";
69 m_strColors[iFeed] = "";
cf4743e Formatting for tabs -> 2 spaces
jmarshallnz authored
70
c7e2430 added: ability to select update interval for rss feeds.
spiff_ authored
71 if (iFeed > 0)
cf4743e Formatting for tabs -> 2 spaces
jmarshallnz authored
72 {
c7e2430 added: ability to select update interval for rss feeds.
spiff_ authored
73 m_iLeadingSpaces = tempLeading / 2;
74 }
75 else
76 {
77 m_iLeadingSpaces = tempLeading;
78 }
cf4743e Formatting for tabs -> 2 spaces
jmarshallnz authored
79
c7e2430 added: ability to select update interval for rss feeds.
spiff_ authored
80 CHTTP http;
81 CStdString strXML;
82 CStdString strUrl = m_vecUrls[iFeed];
cf4743e Formatting for tabs -> 2 spaces
jmarshallnz authored
83
c7e2430 added: ability to select update interval for rss feeds.
spiff_ authored
84 int nRetries = 3;
cf4743e Formatting for tabs -> 2 spaces
jmarshallnz authored
85
c7e2430 added: ability to select update interval for rss feeds.
spiff_ authored
86 while ( (!m_bStop) && (nRetries > 0) )
87 {
88 nRetries--;
729886c added: sf.net patch [1209466] Multiple RSS feeds concatenated - thanks t...
spiff_ authored
89
c7e2430 added: ability to select update interval for rss feeds.
spiff_ authored
90 if (http.Get(strUrl, strXML))
91 {
92 CLog::Log(LOGDEBUG, "Got rss feed: %s", strUrl.c_str());
93 break;
729886c added: sf.net patch [1209466] Multiple RSS feeds concatenated - thanks t...
spiff_ authored
94 }
c7e2430 added: ability to select update interval for rss feeds.
spiff_ authored
95 }
729886c added: sf.net patch [1209466] Multiple RSS feeds concatenated - thanks t...
spiff_ authored
96
c7e2430 added: ability to select update interval for rss feeds.
spiff_ authored
97 if ((!strXML.IsEmpty()) && m_pObserver)
98 {
99 // remove CDATA sections from our buffer (timyXML is not able to parse these)
100 CStdString strCDATAElement;
101 int iStart = strXML.Find("<![CDATA[");
102 int iEnd = 0;
103 while (iStart > 0)
729886c added: sf.net patch [1209466] Multiple RSS feeds concatenated - thanks t...
spiff_ authored
104 {
c7e2430 added: ability to select update interval for rss feeds.
spiff_ authored
105 // get CDATA end position
106 iEnd = strXML.Find("]]>", iStart) + 3;
cf4743e Formatting for tabs -> 2 spaces
jmarshallnz authored
107
c7e2430 added: ability to select update interval for rss feeds.
spiff_ authored
108 // get data in CDATA section
109 strCDATAElement = strXML.substr(iStart + 9, iEnd - iStart - 12);
729886c added: sf.net patch [1209466] Multiple RSS feeds concatenated - thanks t...
spiff_ authored
110
c7e2430 added: ability to select update interval for rss feeds.
spiff_ authored
111 // replace CDATA section with new string
112 strXML = strXML.erase(iStart, iEnd - iStart).insert(iStart, strCDATAElement);
729886c added: sf.net patch [1209466] Multiple RSS feeds concatenated - thanks t...
spiff_ authored
113
c7e2430 added: ability to select update interval for rss feeds.
spiff_ authored
114 iStart = strXML.Find("<![CDATA[");
115 }
729886c added: sf.net patch [1209466] Multiple RSS feeds concatenated - thanks t...
spiff_ authored
116
f1c5545 - 19-07-2005 fixed: UTF16 RSS feeds should at least display (no idea if...
jmarshallnz authored
117 // erase any <content:encoded> tags (also unsupported by tinyxml)
118 iStart = strXML.Find("<content:encoded>");
119 iEnd = 0;
120 while (iStart > 0)
121 {
122 // get <content:encoded> end position
123 iEnd = strXML.Find("</content:encoded>", iStart) + 18;
124
125 // erase the section
126 strXML = strXML.erase(iStart, iEnd - iStart);
127
128 iStart = strXML.Find("<content:encoded>");
129 }
130
47e12be fixed: SF bug [ 1065692 ] RSS does not work for non-Latin character. You...
yuvalt authored
131 if (Parse((LPSTR)strXML.c_str(),iFeed))
c7e2430 added: ability to select update interval for rss feeds.
spiff_ authored
132 {
133 CLog::Log(LOGDEBUG, "Parsed rss feed: %s", strUrl.c_str());
729886c added: sf.net patch [1209466] Multiple RSS feeds concatenated - thanks t...
spiff_ authored
134 }
135 }
c7e2430 added: ability to select update interval for rss feeds.
spiff_ authored
136 }
137 getFeed(strFeed,pbColors);
138 if (strFeed.size() > 0)
139 {
140 g_graphicsContext.Lock();
141 m_pObserver->OnFeedUpdate(strFeed, pbColors);
142 g_graphicsContext.Unlock();
cf4743e Formatting for tabs -> 2 spaces
jmarshallnz authored
143 }
e9e2b06 fixed: cosmetic problem introduced by previous commit.
spiff_ authored
144 m_iLeadingSpaces = tempLeading;
e688c17 runtime: Added a motherload of utils classes. All of these classes are ...
runtime authored
145 }
146
47e12be fixed: SF bug [ 1065692 ] RSS does not work for non-Latin character. You...
yuvalt authored
147 void CRssReader::getFeed(CStdStringW& strText, LPBYTE& pbColors)
e688c17 runtime: Added a motherload of utils classes. All of these classes are ...
runtime authored
148 {
c7e2430 added: ability to select update interval for rss feeds.
spiff_ authored
149 strText.Empty();
150 for (unsigned int i=0;i<m_strFeed.size();++i)
151 strText += m_strFeed[i];
152 int nTextLength = strText.GetLength();
cf4743e Formatting for tabs -> 2 spaces
jmarshallnz authored
153 pbColors = new BYTE[nTextLength];
c7e2430 added: ability to select update interval for rss feeds.
spiff_ authored
154 int k=0;
155 for (unsigned int j=0;j<m_strColors.size();++j)
156 for (unsigned int i = 0; i < m_strColors[j].size(); i++)
157 {
158 pbColors[k++] = m_strColors[j][i] - 48;
159 }
e688c17 runtime: Added a motherload of utils classes. All of these classes are ...
runtime authored
160 }
161
cf4743e Formatting for tabs -> 2 spaces
jmarshallnz authored
162 void CRssReader::AddTag(const CStdString aString)
0d4e1ac - 07-09-2004 added: SF patch 1020481: Allow XML configuration of displ...
jmarshallnz authored
163 {
cf4743e Formatting for tabs -> 2 spaces
jmarshallnz authored
164 m_tagSet.push_back(aString);
0d4e1ac - 07-09-2004 added: SF patch 1020481: Allow XML configuration of displ...
jmarshallnz authored
165 }
166
47e12be fixed: SF bug [ 1065692 ] RSS does not work for non-Latin character. You...
yuvalt authored
167 void CRssReader::AddString(CStdStringW aString, int aColour, int iFeed)
e688c17 runtime: Added a motherload of utils classes. All of these classes are ...
runtime authored
168 {
c7e2430 added: ability to select update interval for rss feeds.
spiff_ authored
169 m_strFeed[iFeed] += aString;
e688c17 runtime: Added a motherload of utils classes. All of these classes are ...
runtime authored
170
cf4743e Formatting for tabs -> 2 spaces
jmarshallnz authored
171 int nStringLength = aString.GetLength();
e688c17 runtime: Added a motherload of utils classes. All of these classes are ...
runtime authored
172
cf4743e Formatting for tabs -> 2 spaces
jmarshallnz authored
173 for (int i = 0;i < nStringLength;i++)
174 {
175 aString[i] = (CHAR) (48 + aColour);
176 }
177
c7e2430 added: ability to select update interval for rss feeds.
spiff_ authored
178 m_strColors[iFeed] += aString;
e688c17 runtime: Added a motherload of utils classes. All of these classes are ...
runtime authored
179 }
180
c7e2430 added: ability to select update interval for rss feeds.
spiff_ authored
181 void CRssReader::GetNewsItems(TiXmlElement* channelXmlNode, int iFeed)
e688c17 runtime: Added a motherload of utils classes. All of these classes are ...
runtime authored
182 {
47e12be fixed: SF bug [ 1065692 ] RSS does not work for non-Latin character. You...
yuvalt authored
183 HTML::CHTMLUtil html;
184
cf4743e Formatting for tabs -> 2 spaces
jmarshallnz authored
185 TiXmlElement * itemNode = channelXmlNode->FirstChildElement("item");
47e12be fixed: SF bug [ 1065692 ] RSS does not work for non-Latin character. You...
yuvalt authored
186 map <CStdString, CStdStringW> mTagElements;
187 typedef pair <CStdString, CStdStringW> StrPair;
cf4743e Formatting for tabs -> 2 spaces
jmarshallnz authored
188 list <CStdString>::iterator i;
189
b69e8ba - 06-05-2005 added: SF patch [1196172] remove trailing dash for rss-fe...
jmarshallnz authored
190 bool bEmpty=true;
191
cf4743e Formatting for tabs -> 2 spaces
jmarshallnz authored
192 // Add the title tag in if we didn't pass any tags in at all
193 // Represents default behaviour before configurability
194
195 if (m_tagSet.empty())
196 AddTag("title");
197
198 while (itemNode > 0)
199 {
b69e8ba - 06-05-2005 added: SF patch [1196172] remove trailing dash for rss-fe...
jmarshallnz authored
200 bEmpty = false;
cf4743e Formatting for tabs -> 2 spaces
jmarshallnz authored
201 TiXmlNode* childNode = itemNode->FirstChild();
202 mTagElements.clear();
203 while (childNode > 0)
204 {
205 CStdString strName = childNode->Value();
206
207 for (i = m_tagSet.begin(); i != m_tagSet.end(); i++)
208 {
2bf9126 fixed: rss feed could crash
bobbin007 authored
209 if (!childNode->NoChildren() && i->Equals(strName))
cf4743e Formatting for tabs -> 2 spaces
jmarshallnz authored
210 {
47e12be fixed: SF bug [ 1065692 ] RSS does not work for non-Latin character. You...
yuvalt authored
211 CStdString htmlText = childNode->FirstChild()->Value();
212
213 // This usually happens in right-to-left languages where they want to
214 // specify in the RSS body that the text should be RTL.
215 // <title>
216 // <div dir="RTL">òìå áøùú: ùîøå òì òöîëí</div>
217 // </title>
218 if (htmlText.Equals("div") || htmlText.Equals("span"))
219 {
220 m_shouldFlip = true;
221 htmlText = childNode->FirstChild()->FirstChild()->Value();
222 }
223
224 CStdString text;
225 CStdStringW unicodeText;
226
227 html.ConvertHTMLToAnsi(htmlText, text);
228 fromRSSToUTF16(text, unicodeText);
229
230 mTagElements.insert(StrPair(*i, unicodeText));
cf4743e Formatting for tabs -> 2 spaces
jmarshallnz authored
231 }
232 }
233 childNode = childNode->NextSibling();
234 }
235
236 int rsscolour = RSS_COLOR_HEADLINE;
237 for (i = m_tagSet.begin();i != m_tagSet.end();i++)
238 {
47e12be fixed: SF bug [ 1065692 ] RSS does not work for non-Latin character. You...
yuvalt authored
239 map <CStdString, CStdStringW>::iterator j = mTagElements.find(*i);
cf4743e Formatting for tabs -> 2 spaces
jmarshallnz authored
240
241 if (j == mTagElements.end())
242 continue;
47e12be fixed: SF bug [ 1065692 ] RSS does not work for non-Latin character. You...
yuvalt authored
243
244 CStdStringW& text = j->second;
c7e2430 added: ability to select update interval for rss feeds.
spiff_ authored
245 AddString(text, rsscolour, iFeed);
cf4743e Formatting for tabs -> 2 spaces
jmarshallnz authored
246 rsscolour = RSS_COLOR_BODY;
247 text = " - ";
c7e2430 added: ability to select update interval for rss feeds.
spiff_ authored
248 AddString(text, rsscolour, iFeed);
cf4743e Formatting for tabs -> 2 spaces
jmarshallnz authored
249 }
250 itemNode = itemNode->NextSiblingElement("item");
251 }
47e12be fixed: SF bug [ 1065692 ] RSS does not work for non-Latin character. You...
yuvalt authored
252
b69e8ba - 06-05-2005 added: SF patch [1196172] remove trailing dash for rss-fe...
jmarshallnz authored
253 // spiff - avoid trailing ' - '
254 if( bEmpty )
255 {
c7e2430 added: ability to select update interval for rss feeds.
spiff_ authored
256 m_strFeed[iFeed].erase(m_strFeed[iFeed].length()-3);
257 m_strColors[iFeed].erase(m_strColors[iFeed].length()-3);
b69e8ba - 06-05-2005 added: SF patch [1196172] remove trailing dash for rss-fe...
jmarshallnz authored
258 }
e688c17 runtime: Added a motherload of utils classes. All of these classes are ...
runtime authored
259 }
260
47e12be fixed: SF bug [ 1065692 ] RSS does not work for non-Latin character. You...
yuvalt authored
261 void CRssReader::fromRSSToUTF16(const CStdStringA& strSource, CStdStringW& strDest)
262 {
263 CStdString flippedStrSource;
264
265 if (m_shouldFlip)
266 {
22b20e4 Fix small bug with the text direction in bidi strings. This bug was caus...
yuvalt authored
267 g_charsetConverter.logicalToVisualBiDi(strSource, flippedStrSource, m_encoding, FRIBIDI_TYPE_RTL);
47e12be fixed: SF bug [ 1065692 ] RSS does not work for non-Latin character. You...
yuvalt authored
268 }
269 else
270 {
271 flippedStrSource = strSource;
272 }
273
274 if (m_iconv != (iconv_t) - 1)
275 {
276 const char* src = flippedStrSource.c_str();
277 size_t inBytes = flippedStrSource.length() + 1;
278
279 wchar_t outBuf[1024];
280 char* dst = (char*) &outBuf[0];
281 size_t outBytes=1024;
282 size_t originalOutBytes = outBytes;
283
284 iconv(m_iconv, NULL, &inBytes, NULL, &outBytes);
285
286 if (iconv(m_iconv, &src, &inBytes, &dst, &outBytes) == -1)
287 {
288 // For some reason it failed (maybe wrong charset?). Nothing to do but
289 // return the original..
290 strDest = flippedStrSource;
291 return;
292 }
293
294 outBuf[(originalOutBytes - outBytes) / 2] = '\0';
295 strDest = outBuf;
296 }
297 else
298 {
299 strDest = flippedStrSource;
300 return;
301 }
302 }
303
304 bool CRssReader::Parse(LPSTR szBuffer, int iFeed)
e688c17 runtime: Added a motherload of utils classes. All of these classes are ...
runtime authored
305 {
729886c added: sf.net patch [1209466] Multiple RSS feeds concatenated - thanks t...
spiff_ authored
306 m_xml.Clear();
47e12be fixed: SF bug [ 1065692 ] RSS does not work for non-Latin character. You...
yuvalt authored
307 m_xml.Parse((LPCSTR)szBuffer, 0, TIXML_ENCODING_LEGACY);
308
309 if (m_iconv != (iconv_t) -1)
310 {
311 iconv_close(m_iconv);
312 m_iconv = (iconv_t) -1;
313 m_shouldFlip = false;
314 }
315
316 m_encoding = "UTF-8";
317 if (m_xml.RootElement())
318 {
319 TiXmlDeclaration *tiXmlDeclaration = m_xml.RootElement()->Parent()->FirstChild()->ToDeclaration();
320 if (tiXmlDeclaration != NULL && strlen(tiXmlDeclaration->Encoding()) > 0)
321 {
322 m_encoding = tiXmlDeclaration->Encoding();
323 }
324 }
325
326 CLog::Log(LOGDEBUG, "RSS feed encoding: %s", m_encoding.c_str());
327 m_iconv = iconv_open("UTF-16LE", m_encoding.c_str());
328
329 if (g_charsetConverter.isBidiCharset(m_encoding))
330 {
331 m_shouldFlip = true;
332 }
333
c7e2430 added: ability to select update interval for rss feeds.
spiff_ authored
334 return Parse(iFeed);
e688c17 runtime: Added a motherload of utils classes. All of these classes are ...
runtime authored
335 }
336
c7e2430 added: ability to select update interval for rss feeds.
spiff_ authored
337 bool CRssReader::Parse(int iFeed)
e688c17 runtime: Added a motherload of utils classes. All of these classes are ...
runtime authored
338 {
cf4743e Formatting for tabs -> 2 spaces
jmarshallnz authored
339 CStdString strLeadingSpace = " ";
340 for (int i = 0; i < m_iLeadingSpaces; i++)
341 {
c7e2430 added: ability to select update interval for rss feeds.
spiff_ authored
342 AddString(strLeadingSpace, 0, iFeed);
cf4743e Formatting for tabs -> 2 spaces
jmarshallnz authored
343 }
344
345 TiXmlElement* rootXmlNode = m_xml.RootElement();
346
347 if (!rootXmlNode)
348 return false;
349
350 TiXmlElement* rssXmlNode = NULL;
351
352 CStdString strValue = rootXmlNode->Value();
353 if (( strValue.Find("rss") >= 0 ) || ( strValue.Find("rdf") >= 0 ))
354 {
355 rssXmlNode = rootXmlNode;
356 }
357 else
358 {
359 // Unable to find root <rss> or <rdf> node
360 return false;
361 }
362
363 TiXmlElement* channelXmlNode = rssXmlNode->FirstChildElement("channel");
364 if (channelXmlNode)
365 {
366 TiXmlElement* titleNode = channelXmlNode->FirstChildElement("title");
2bf9126 fixed: rss feed could crash
bobbin007 authored
367 if (titleNode && !titleNode->NoChildren())
cf4743e Formatting for tabs -> 2 spaces
jmarshallnz authored
368 {
47e12be fixed: SF bug [ 1065692 ] RSS does not work for non-Latin character. You...
yuvalt authored
369 CStdString strChannel = titleNode->FirstChild()->Value();
370 CStdStringW strChannelUnicode;
371 fromRSSToUTF16(strChannel, strChannelUnicode);
372 AddString(strChannelUnicode, RSS_COLOR_CHANNEL, iFeed);
373
374 AddString(": ", RSS_COLOR_CHANNEL, iFeed);
cf4743e Formatting for tabs -> 2 spaces
jmarshallnz authored
375 }
376
c7e2430 added: ability to select update interval for rss feeds.
spiff_ authored
377 GetNewsItems(channelXmlNode,iFeed);
cf4743e Formatting for tabs -> 2 spaces
jmarshallnz authored
378 }
379
c7e2430 added: ability to select update interval for rss feeds.
spiff_ authored
380 GetNewsItems(rssXmlNode,iFeed);
cf4743e Formatting for tabs -> 2 spaces
jmarshallnz authored
381 return true;
382 }
Something went wrong with that request. Please try again.