Debian Codegraph @

src/core/podcasts/PodcastReader.cpp

raw
1 /****************************************************************************************
2 * Copyright (c) 2007 Bart Cerneels <[email protected]> *
3 * 2009 Mathias Panzenböck <[email protected]> *
4 * 2013 Ralf Engels <[email protected]> *
5 * *
6 * This program is free software; you can redistribute it and/or modify it under *
7 * the terms of the GNU General Public License as published by the Free Software *
8 * Foundation; either version 2 of the License, or (at your option) any later *
9 * version. *
10 * *
11 * This program is distributed in the hope that it will be useful, but WITHOUT ANY *
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A *
13 * PARTICULAR PURPOSE. See the GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License along with *
16 * this program. If not, see <http://www.gnu.org/licenses/>. *
17 ****************************************************************************************/
18
19 #include "core/podcasts/PodcastReader.h"
20
21 #include "core/support/Amarok.h"
22 #include "core/support/Components.h"
23 #include "core/support/Debug.h"
24 #include "core/meta/support/MetaUtility.h"
25
26 #include <QUrl>
27
28 #include <QDate>
29 #include <QSet>
30
31 #include <algorithm>
32
33 using namespace Podcasts;
34
35 #define ITUNES_NS QStringLiteral("http://www.itunes.com/dtds/podcast-1.0.dtd")
36 #define RDF_NS "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
37 #define RSS10_NS QStringLiteral("http://purl.org/rss/1.0/")
38 #define RSS20_NS QStringLiteral("")
39 #define ATOM_NS "http://www.w3.org/2005/Atom"
40 #define ENC_NS "http://purl.oclc.org/net/rss_2.0/enc#"
41 #define CONTENT_NS QStringLiteral("http://purl.org/rss/1.0/modules/content")
42 #define DC_NS QStringLiteral("http://purl.org/dc/elements/1.1/")
43
44 // regular expressions for linkification:
45 #define RE_USER "[-+_%\\.\\w]+"
46 #define RE_PASSWD RE_USER
47 #define RE_DOMAIN "[-a-zA-Z0-9]+(?:\\.[-a-zA-Z0-9]+)*"
48 #define RE_PROT "[a-zA-Z]+://"
49 #define RE_URL RE_PROT "(?:" RE_USER "(?::" RE_PASSWD ")?@)?" RE_DOMAIN \
50 "(?::\\d+)?(?:/[-\\w\\?&=%+.,;:_#~/!@]*)?"
51 #define RE_MAIL RE_USER "@" RE_DOMAIN
52
53 const PodcastReader::StaticData PodcastReader::sd;
54
55 PodcastReader::PodcastReader( PodcastProvider *podcastProvider, QObject *parent )
56 : QObject( parent )
57 , m_xmlReader()
58 , m_podcastProvider( podcastProvider )
59 , m_transferJob( )
60 , m_current( nullptr )
61 , m_actionStack()
62 , m_contentType( TextContent )
63 , m_buffer()
64 {}
65
66 void
67 PodcastReader::Action::begin( PodcastReader *podcastReader ) const
68 {
69 if( m_begin )
70 (( *podcastReader ).*m_begin )();
71 }
72
73 void
74 PodcastReader::Action::end( PodcastReader *podcastReader ) const
75 {
76 if( m_end )
77 (( *podcastReader ).*m_end )();
78 }
79
80 void
81 PodcastReader::Action::characters( PodcastReader *podcastReader ) const
82 {
83 if( m_characters )
84 (( *podcastReader ).*m_characters )();
85 }
86
87 // initialization of the feed parser automata:
88 PodcastReader::StaticData::StaticData()
89 : removeScripts( QStringLiteral("<script[^<]*</script>|<script[^>]*>"), QRegularExpression::CaseInsensitiveOption )
90 , mightBeHtml( QStringLiteral("<\\?xml[^>]*\\?>|<br[^>]*>|<p[^>]*>|&lt;|&gt;|&amp;|&quot;|"
91 "<([-:\\w\\d]+)[^>]*(/>|>.*</\\1>)|<hr[>]*>|&#\\d+;|&#x[a-fA-F\\d]+;"), QRegularExpression::CaseInsensitiveOption )
92 , linkify( QStringLiteral("\\b(" RE_URL ")|\\b(" RE_MAIL ")|(\n)") )
93
94 , startAction( rootMap )
95
96 , docAction(
97 docMap,
98 nullptr,
99 &PodcastReader::endDocument )
100 , xmlAction(
101 xmlMap,
102 &PodcastReader::beginXml,
103 &PodcastReader::endXml,
104 &PodcastReader::readEscapedCharacters )
105 , skipAction( skipMap )
106 , noContentAction(
107 noContentMap,
108 &PodcastReader::beginNoElement,
109 nullptr,
110 &PodcastReader::readNoCharacters )
111
112 , rdfAction(
113 rdfMap,
114 &PodcastReader::beginRdf )
115 , rssAction(
116 rssMap,
117 &PodcastReader::beginRss )
118 , feedAction(
119 feedMap,
120 &PodcastReader::beginFeed )
121 , htmlAction(
122 skipMap,
123 &PodcastReader::beginHtml )
124 , unknownFeedTypeAction(
125 skipMap,
126 &PodcastReader::beginUnknownFeedType )
127
128 // RSS 1.0+2.0
129 , rss10ChannelAction(
130 rss10ChannelMap,
131 &PodcastReader::beginChannel )
132 , rss20ChannelAction(
133 rss20ChannelMap,
134 &PodcastReader::beginChannel )
135
136 , titleAction(
137 textMap,
138 &PodcastReader::beginText,
139 &PodcastReader::endTitle,
140 &PodcastReader::readCharacters )
141 , subtitleAction(
142 textMap,
143 &PodcastReader::beginText,
144 &PodcastReader::endSubtitle,
145 &PodcastReader::readCharacters )
146 , descriptionAction(
147 textMap,
148 &PodcastReader::beginText,
149 &PodcastReader::endDescription,
150 &PodcastReader::readCharacters )
151 , encodedAction(
152 textMap,
153 &PodcastReader::beginText,
154 &PodcastReader::endEncoded,
155 &PodcastReader::readCharacters )
156 , bodyAction(
157 xmlMap,
158 &PodcastReader::beginText,
159 &PodcastReader::endBody,
160 &PodcastReader::readEscapedCharacters )
161 , linkAction(
162 textMap,
163 &PodcastReader::beginText,
164 &PodcastReader::endLink,
165 &PodcastReader::readCharacters )
166 , imageAction( imageMap,
167 &PodcastReader::beginImage )
168 , itemAction(
169 itemMap,
170 &PodcastReader::beginItem,
171 &PodcastReader::endItem )
172 , urlAction(
173 textMap,
174 &PodcastReader::beginText,
175 &PodcastReader::endImageUrl,
176 &PodcastReader::readCharacters )
177 , authorAction(
178 textMap,
179 &PodcastReader::beginText,
180 &PodcastReader::endAuthor,
181 &PodcastReader::readCharacters )
182 , creatorAction(
183 textMap,
184 &PodcastReader::beginText,
185 &PodcastReader::endCreator,
186 &PodcastReader::readCharacters )
187 , enclosureAction(
188 noContentMap,
189 &PodcastReader::beginEnclosure )
190 , guidAction(
191 textMap,
192 &PodcastReader::beginText,
193 &PodcastReader::endGuid,
194 &PodcastReader::readCharacters )
195 , pubDateAction(
196 textMap,
197 &PodcastReader::beginText,
198 &PodcastReader::endPubDate,
199 &PodcastReader::readCharacters )
200 , keywordsAction(
201 textMap,
202 &PodcastReader::beginText,
203 &PodcastReader::endKeywords,
204 &PodcastReader::readCharacters )
205 , newFeedUrlAction(
206 textMap,
207 &PodcastReader::beginText,
208 &PodcastReader::endNewFeedUrl,
209 &PodcastReader::readCharacters )
210
211 // Atom
212 , atomLogoAction(
213 textMap,
214 &PodcastReader::beginText,
215 &PodcastReader::endImageUrl,
216 &PodcastReader::readCharacters )
217 , atomIconAction(
218 textMap,
219 &PodcastReader::beginText,
220 &PodcastReader::endAtomIcon,
221 &PodcastReader::readCharacters )
222 , atomEntryAction(
223 atomEntryMap,
224 &PodcastReader::beginItem,
225 &PodcastReader::endItem )
226 , atomTitleAction(
227 atomTextMap,
228 &PodcastReader::beginAtomText,
229 &PodcastReader::endAtomTitle,
230 &PodcastReader::readAtomTextCharacters )
231 , atomSubtitleAction(
232 atomTextMap,
233 &PodcastReader::beginAtomText,
234 &PodcastReader::endAtomSubtitle,
235 &PodcastReader::readAtomTextCharacters )
236 , atomAuthorAction(
237 atomAuthorMap )
238 , atomFeedLinkAction(
239 noContentMap,
240 &PodcastReader::beginAtomFeedLink,
241 nullptr,
242 &PodcastReader::readNoCharacters )
243 , atomEntryLinkAction(
244 noContentMap,
245 &PodcastReader::beginAtomEntryLink,
246 nullptr,
247 &PodcastReader::readNoCharacters )
248 , atomIdAction(
249 textMap,
250 &PodcastReader::beginText,
251 &PodcastReader::endGuid,
252 &PodcastReader::readCharacters )
253 , atomPublishedAction(
254 textMap,
255 &PodcastReader::beginText,
256 &PodcastReader::endAtomPublished,
257 &PodcastReader::readCharacters )
258 , atomUpdatedAction(
259 textMap,
260 &PodcastReader::beginText,
261 &PodcastReader::endAtomUpdated,
262 &PodcastReader::readCharacters )
263 , atomSummaryAction(
264 atomTextMap,
265 &PodcastReader::beginAtomText,
266 &PodcastReader::endAtomSummary,
267 &PodcastReader::readAtomTextCharacters )
268 , atomContentAction(
269 atomTextMap,
270 &PodcastReader::beginAtomText,
271 &PodcastReader::endAtomContent,
272 &PodcastReader::readAtomTextCharacters )
273 , atomTextAction(
274 atomTextMap,
275 &PodcastReader::beginAtomTextChild,
276 &PodcastReader::endAtomTextChild,
277 &PodcastReader::readAtomTextCharacters )
278 {
279 // known elements:
280 knownElements[ QStringLiteral("rss") ] = Rss;
281 knownElements[ QStringLiteral("RDF") ] = Rdf;
282 knownElements[ QStringLiteral("feed") ] = Feed;
283 knownElements[ QStringLiteral("channel") ] = Channel;
284 knownElements[ QStringLiteral("item") ] = Item;
285 knownElements[ QStringLiteral("image") ] = Image;
286 knownElements[ QStringLiteral("link") ] = Link;
287 knownElements[ QStringLiteral("url") ] = Url;
288 knownElements[ QStringLiteral("title") ] = Title;
289 knownElements[ QStringLiteral("author") ] = Author;
290 knownElements[ QStringLiteral("enclosure") ] = EnclosureElement;
291 knownElements[ QStringLiteral("guid") ] = Guid;
292 knownElements[ QStringLiteral("pubDate") ] = PubDate;
293 knownElements[ QStringLiteral("description") ] = Description;
294 knownElements[ QStringLiteral("summary") ] = Summary;
295 knownElements[ QStringLiteral("body") ] = Body;
296 knownElements[ QStringLiteral("entry") ] = Entry;
297 knownElements[ QStringLiteral("content") ] = Content;
298 knownElements[ QStringLiteral("name") ] = Name;
299 knownElements[ QStringLiteral("id") ] = Id;
300 knownElements[ QStringLiteral("subtitle") ] = Subtitle;
301 knownElements[ QStringLiteral("updated") ] = Updated;
302 knownElements[ QStringLiteral("published") ] = Published;
303 knownElements[ QStringLiteral("logo") ] = Logo;
304 knownElements[ QStringLiteral("icon") ] = Icon;
305 knownElements[ QStringLiteral("encoded") ] = Encoded;
306 knownElements[ QStringLiteral("creator") ] = Creator;
307 knownElements[ QStringLiteral("keywords") ] = Keywords;
308 knownElements[ QStringLiteral("new-feed-url") ] = NewFeedUrl;
309 knownElements[ QStringLiteral("html") ] = Html;
310 knownElements[ QStringLiteral("HTML") ] = Html;
311
312 // before start document/after end document
313 rootMap.insert( Document, &docAction );
314
315 // parse document
316 docMap.insert( Rss, &rssAction );
317 docMap.insert( Html, &htmlAction );
318 docMap.insert( Rdf, &rdfAction );
319 docMap.insert( Feed, &feedAction );
320 docMap.insert( Any, &unknownFeedTypeAction );
321
322 // parse <rss> "RSS 2.0"
323 rssMap.insert( Channel, &rss20ChannelAction );
324
325 // parse <RDF> "RSS 1.0"
326 rdfMap.insert( Channel, &rss10ChannelAction );
327 rdfMap.insert( Item, &itemAction );
328
329 // parse <channel> "RSS 2.0"
330 rss20ChannelMap.insert( Title, &titleAction );
331 rss20ChannelMap.insert( ItunesSubtitle, &subtitleAction );
332 rss20ChannelMap.insert( ItunesAuthor, &authorAction );
333 rss20ChannelMap.insert( Creator, &creatorAction );
334 rss20ChannelMap.insert( Description, &descriptionAction );
335 rss20ChannelMap.insert( Encoded, &encodedAction );
336 rss20ChannelMap.insert( ItunesSummary, &descriptionAction );
337 rss20ChannelMap.insert( Body, &bodyAction );
338 rss20ChannelMap.insert( Link, &linkAction );
339 rss20ChannelMap.insert( Image, &imageAction );
340 rss20ChannelMap.insert( ItunesKeywords, &keywordsAction );
341 rss20ChannelMap.insert( NewFeedUrl, &newFeedUrlAction );
342 rss20ChannelMap.insert( Item, &itemAction );
343
344 // parse <channel> "RSS 1.0"
345 rss10ChannelMap.insert( Title, &titleAction );
346 rss10ChannelMap.insert( ItunesSubtitle, &subtitleAction );
347 rss10ChannelMap.insert( ItunesAuthor, &authorAction );
348 rss10ChannelMap.insert( Creator, &creatorAction );
349 rss10ChannelMap.insert( Description, &descriptionAction );
350 rss10ChannelMap.insert( Encoded, &encodedAction );
351 rss10ChannelMap.insert( ItunesSummary, &descriptionAction );
352 rss10ChannelMap.insert( Body, &bodyAction );
353 rss10ChannelMap.insert( Link, &linkAction );
354 rss10ChannelMap.insert( Image, &imageAction );
355 rss10ChannelMap.insert( ItunesKeywords, &keywordsAction );
356 rss10ChannelMap.insert( NewFeedUrl, &newFeedUrlAction );
357
358 // parse <image>
359 imageMap.insert( Title, &skipAction );
360 imageMap.insert( Link, &skipAction );
361 imageMap.insert( Url, &urlAction );
362
363 // parse <item>
364 itemMap.insert( Title, &titleAction );
365 itemMap.insert( ItunesSubtitle, &subtitleAction );
366 itemMap.insert( Author, &authorAction );
367 itemMap.insert( ItunesAuthor, &authorAction );
368 itemMap.insert( Creator, &creatorAction );
369 itemMap.insert( Description, &descriptionAction );
370 itemMap.insert( Encoded, &encodedAction );
371 itemMap.insert( ItunesSummary, &descriptionAction );
372 itemMap.insert( Body, &bodyAction );
373 itemMap.insert( EnclosureElement, &enclosureAction );
374 itemMap.insert( Guid, &guidAction );
375 itemMap.insert( PubDate, &pubDateAction );
376 itemMap.insert( ItunesKeywords, &keywordsAction );
377 // TODO: move the link field from PodcastChannel to PodcastMetaCommon
378 // itemMap.insert( Link, &linkAction );
379
380 // parse <feed> "Atom"
381 feedMap.insert( Title, &atomTitleAction );
382 feedMap.insert( Subtitle, &atomSubtitleAction );
383 feedMap.insert( Icon, &atomIconAction );
384 feedMap.insert( Logo, &atomLogoAction );
385 feedMap.insert( Author, &atomAuthorAction );
386 feedMap.insert( Link, &atomFeedLinkAction );
387 feedMap.insert( Entry, &atomEntryAction );
388
389 // parse <entry> "Atom"
390 atomEntryMap.insert( Title, &atomTitleAction );
391 atomEntryMap.insert( Subtitle, &atomSubtitleAction );
392 atomEntryMap.insert( Author, &atomAuthorAction );
393 atomEntryMap.insert( Id, &atomIdAction );
394 atomEntryMap.insert( Published, &atomPublishedAction );
395 atomEntryMap.insert( Updated, &atomUpdatedAction );
396 atomEntryMap.insert( Summary, &atomSummaryAction );
397 atomEntryMap.insert( Link, &atomEntryLinkAction );
398 atomEntryMap.insert( SupportedContent, &atomContentAction );
399
400 // parse <author> "Atom"
401 atomAuthorMap.insert( Name, &authorAction );
402
403 // parse atom text
404 atomTextMap.insert( Any, &atomTextAction );
405
406 // parse arbitrary xml
407 xmlMap.insert( Any, &xmlAction );
408
409 // skip elements
410 skipMap.insert( Any, &skipAction );
411 }
412
413 PodcastReader::~PodcastReader()
414 {
415 DEBUG_BLOCK
416 }
417
418 bool
419 PodcastReader::mightBeHtml( const QString& text ) //Static
420 {
421 return text.indexOf( sd.mightBeHtml ) != -1;
422 }
423
424 bool PodcastReader::read( QIODevice *device )
425 {
426 DEBUG_BLOCK
427
428 m_xmlReader.setDevice( device );
429 return read();
430 }
431
432 bool
433 PodcastReader::read( const QUrl &url )
434 {
435 DEBUG_BLOCK
436
437 m_url = url;
438
439 m_transferJob = KIO::get( m_url, KIO::Reload, KIO::HideProgressInfo );
440
441 connect( m_transferJob, &KIO::TransferJob::data,
442 this, &PodcastReader::slotAddData );
443
444 connect( m_transferJob, &KIO::TransferJob::result,
445 this, &PodcastReader::downloadResult );
446
447 connect( m_transferJob, &KIO::TransferJob::redirection,
448 this, &PodcastReader::slotRedirection );
449
450 connect( m_transferJob, &KIO::TransferJob::permanentRedirection,
451 this, &PodcastReader::slotPermanentRedirection );
452
453 QString description = i18n( "Importing podcast channel from %1", url.url() );
454 if( m_channel )
455 {
456 description = m_channel->title().isEmpty()
457 ? i18n( "Updating podcast channel" )
458 : i18n( "Updating \"%1\"", m_channel->title() );
459 }
460
461 Q_EMIT statusBarNewProgressOperation( m_transferJob, description, this );
462
463 // parse data
464 return read();
465 }
466
467 void
468 PodcastReader::slotAbort()
469 {
470 DEBUG_BLOCK
471 }
472
473 bool
474 PodcastReader::update( const PodcastChannelPtr &channel )
475 {
476 DEBUG_BLOCK
477 m_channel = channel;
478
479 return read( m_channel->url() );
480 }
481
482 void
483 PodcastReader::slotAddData( KIO::Job *job, const QByteArray &data )
484 {
485 DEBUG_BLOCK
486 Q_UNUSED( job )
487
488 m_xmlReader.addData( data );
489
490 // parse more data
491 continueRead();
492 }
493
494 void
495 PodcastReader::downloadResult( KJob * job )
496 {
497 DEBUG_BLOCK
498
499 // parse more data
500 continueRead();
501
502 KIO::TransferJob *transferJob = dynamic_cast<KIO::TransferJob *>( job );
503 if( transferJob && transferJob->isErrorPage() )
504 {
505 QString errorMessage =
506 i18n( "Importing podcast from %1 failed with error:\n", m_url.url() );
507 if( m_channel )
508 {
509 errorMessage = m_channel->title().isEmpty()
510 ? i18n( "Updating podcast from %1 failed with error:\n", m_url.url() )
511 : i18n( "Updating \"%1\" failed with error:\n", m_channel->title() );
512 }
513 errorMessage = errorMessage.append( job->errorString() );
514
515 Q_EMIT statusBarErrorMessage( errorMessage );
516 }
517 else if( job->error() )
518 {
519 QString errorMessage =
520 i18n( "Importing podcast from %1 failed with error:\n", m_url.url() );
521 if( m_channel )
522 {
523 errorMessage = m_channel->title().isEmpty()
524 ? i18n( "Updating podcast from %1 failed with error:\n", m_url.url() )
525 : i18n( "Updating \"%1\" failed with error:\n", m_channel->title() );
526 }
527 errorMessage = errorMessage.append( job->errorString() );
528
529 Q_EMIT statusBarErrorMessage( errorMessage );
530 }
531
532 m_transferJob = nullptr;
533 }
534
535 PodcastReader::ElementType
536 PodcastReader::elementType() const
537 {
538 if( m_xmlReader.isEndDocument() || m_xmlReader.isStartDocument() )
539 return Document;
540
541 if( m_xmlReader.isCDATA() || m_xmlReader.isCharacters() )
542 return CharacterData;
543
544 ElementType elementType = sd.knownElements[ m_xmlReader.name().toString()];
545
546 // This is a bit hacky because my automata does not support conditions.
547 // Therefore I put the decision logic in here and declare some pseudo elements.
548 // I don't think it is worth it to extend the automata to support such conditions.
549 switch( elementType )
550 {
551 case Summary:
552 if( m_xmlReader.namespaceUri() == ITUNES_NS )
553 {
554 elementType = ItunesSummary;
555 }
556 break;
557
558 case Subtitle:
559 if( m_xmlReader.namespaceUri() == ITUNES_NS )
560 {
561 elementType = ItunesSubtitle;
562 }
563 break;
564
565 case Author:
566 if( m_xmlReader.namespaceUri() == ITUNES_NS )
567 {
568 elementType = ItunesAuthor;
569 }
570 break;
571
572 case Keywords:
573 if( m_xmlReader.namespaceUri() == ITUNES_NS )
574 {
575 elementType = ItunesKeywords;
576 }
577 break;
578
579 case Content:
580 if( m_xmlReader.namespaceUri() == QStringLiteral( ATOM_NS ) &&
581 // ignore atom:content elements that do not
582 // have content but only refer to some url:
583 !hasAttribute( ATOM_NS, "src" ) )
584 {
585 // Atom supports arbitrary Base64 encoded content.
586 // Because we can only something with text/html/xhtml I ignore
587 // anything else.
588 // See:
589 // http://tools.ietf.org/html/rfc4287#section-4.1.3
590 if( hasAttribute( ATOM_NS, "type" ) )
591 {
592 QStringView type( attribute( ATOM_NS, "type" ) );
593
594 if( type == QStringLiteral("text") || type == QStringLiteral("html") || type == QStringLiteral("xhtml") )
595 {
596 elementType = SupportedContent;
597 }
598 }
599 else
600 {
601 elementType = SupportedContent;
602 }
603 }
604 break;
605
606 default:
607 break;
608 }
609
610 return elementType;
611 }
612
613 bool
614 PodcastReader::read()
615 {
616 DEBUG_BLOCK
617
618 m_current = nullptr;
619 m_item = nullptr;
620 m_contentType = TextContent;
621 m_buffer.clear();
622 m_actionStack.clear();
623 m_actionStack.push( &( PodcastReader::sd.startAction ) );
624 m_xmlReader.setNamespaceProcessing( true );
625
626 return continueRead();
627 }
628
629 bool
630 PodcastReader::continueRead()
631 {
632 // this is some kind of pushdown automata
633 // with this it should be possible to parse feeds in parallel
634 // without using threads
635 DEBUG_BLOCK
636
637 while( !m_xmlReader.atEnd() && m_xmlReader.error() != QXmlStreamReader::CustomError )
638 {
639 QXmlStreamReader::TokenType token = m_xmlReader.readNext();
640
641 if( m_xmlReader.error() == QXmlStreamReader::PrematureEndOfDocumentError && m_transferJob )
642 {
643 return true;
644 }
645
646 if( m_xmlReader.hasError() )
647 {
648 Q_EMIT finished( this );
649 return false;
650 }
651
652 if( m_actionStack.isEmpty() )
653 {
654 debug() << "expected element on stack!";
655 return false;
656 }
657
658 const Action* action = m_actionStack.top();
659 const Action* subAction = nullptr;
660
661 switch( token )
662 {
663 case QXmlStreamReader::Invalid:
664 return false;
665
666 case QXmlStreamReader::StartDocument:
667 case QXmlStreamReader::StartElement:
668 subAction = action->actionMap()[ elementType()];
669
670 if( !subAction )
671 subAction = action->actionMap()[ Any ];
672
673 if( !subAction )
674 subAction = &( PodcastReader::sd.skipAction );
675
676 m_actionStack.push( subAction );
677
678 subAction->begin( this );
679 break;
680
681 case QXmlStreamReader::EndDocument:
682 case QXmlStreamReader::EndElement:
683 action->end( this );
684
685 if( m_actionStack.pop() != action )
686 {
687 debug() << "popped other element than expected!";
688 }
689 break;
690
691 case QXmlStreamReader::Characters:
692 if( !m_xmlReader.isWhitespace() || m_xmlReader.isCDATA() )
693 {
694 action->characters( this );
695 }
696 break;
697 // ignorable whitespaces
698 case QXmlStreamReader::Comment:
699 case QXmlStreamReader::EntityReference:
700 case QXmlStreamReader::ProcessingInstruction:
701 case QXmlStreamReader::DTD:
702 case QXmlStreamReader::NoToken:
703 // ignore
704 break;
705 }
706 }
707
708 return !m_xmlReader.hasError();
709 }
710
711 void
712 PodcastReader::stopWithError( const QString &message )
713 {
714 m_xmlReader.raiseError( message );
715
716 if( m_transferJob )
717 {
718 m_transferJob->kill(KJob::EmitResult);
719 m_transferJob = nullptr;
720 }
721
722 Q_EMIT finished( this );
723 }
724
725 void
726 PodcastReader::beginText()
727 {
728 m_buffer.clear();
729 }
730
731 void
732 PodcastReader::endTitle()
733 {
734 m_current->setTitle( m_buffer.trimmed() );
735 }
736
737 void
738 PodcastReader::endSubtitle()
739 {
740 m_current->setSubtitle( m_buffer.trimmed() );
741 }
742
743 QString
744 PodcastReader::atomTextAsText()
745 {
746 switch( m_contentType )
747 {
748 case HtmlContent:
749 case XHtmlContent:
750 // TODO: strip tags (there should not be any non-xml entities here)
751 return unescape( m_buffer );
752
753 case TextContent:
754 default:
755 return m_buffer;
756 }
757 }
758
759 QString
760 PodcastReader::atomTextAsHtml()
761 {
762 switch( m_contentType )
763 {
764 case HtmlContent:
765 case XHtmlContent:
766 // strip <script> elements
767 // This will work because there aren't <![CDATA[ ]]> sections
768 // in m_buffer, because we have (re)escape the code manually.
769 // XXX: But it does not remove event handlers like onclick="..."
770 // and JavaScript links like href="javascript:..."
771 return m_buffer.remove( sd.removeScripts );
772
773 case TextContent:
774 default:
775 return textToHtml( m_buffer );
776 }
777 }
778
779 QString
780 PodcastReader::unescape( const QString &text )
781 {
782 // TODO: resolve predefined html entities
783 QString buf;
784
785 for ( int i = 0; i < text.size(); ++ i )
786 {
787 QChar c( text[ i ] );
788
789 if( c == QLatin1Char( '&' ) )
790 {
791 int endIndex = text.indexOf( QLatin1Char(';'), i );
792
793 if( endIndex == -1 )
794 {
795 // fix invalid input
796 buf += c;
797 }
798 else if( text[ i + 1 ] == QLatin1Char( '#' ) )
799 {
800 int num = 0;
801 bool ok = false;
802 if( text[ i + 2 ] == QLatin1Char( 'x' ) )
803 {
804 QString entity( text.mid( i + 3, endIndex - i - 3 ) );
805 num = entity.toInt( &ok, 16 );
806 }
807 else
808 {
809 QString entity( text.mid( i + 2, endIndex - i - 2 ) );
810 num = entity.toInt( &ok, 10 );
811 }
812
813 if( !ok || num < 0 )
814 {
815 // fix invalid input
816 buf += c;
817 }
818 else
819 {
820 buf += QChar( num );
821 i = endIndex;
822 }
823 }
824 else
825 {
826 QString entity( text.mid( i + 1, endIndex - i - 1 ) );
827
828 if( entity == QLatin1String("lt") )
829 {
830 buf += QLatin1Char('<');
831 i = endIndex;
832 }
833 else if( entity == QLatin1String("gt") )
834 {
835 buf += QLatin1Char('>');
836 i = endIndex;
837 }
838 else if( entity == QLatin1String("amp") )
839 {
840 buf += QLatin1Char('&');
841 i = endIndex;
842 }
843 else if( entity == QLatin1String("apos") )
844 {
845 buf += QLatin1Char('\'');
846 i = endIndex;
847 }
848 else if( entity == QLatin1String("quot") )
849 {
850 buf += QLatin1Char('"');
851 i = endIndex;
852 }
853 else
854 {
855 // fix invalid input
856 buf += c;
857 }
858 }
859 }
860 else
861 {
862 buf += c;
863 }
864 }
865
866 return buf;
867 }
868
869 void
870 PodcastReader::setSummary( const QString &description )
871 {
872 if( m_current->summary().size() < description.size() )
873 {
874 m_current->setSummary( description );
875 }
876 }
877
878 void
879 PodcastReader::setDescription( const QString &description )
880 {
881 // The content of the <description>, <itunes:summary> or <body>
882 // elements might be assigned to the field description, unless
883 // there is already longer data in it. Then it will be assigned
884 // to summary, unless summary depending on whether there
885 // already is some (longer) information in the description
886 // field.
887 // If there is already data in the description field, instead of
888 // overwriting, it will be moved to the summary field, unless
889 // there is already longer data there.
890 if( m_current->description().size() < description.size() )
891 {
892 setSummary( m_current->description() );
893 m_current->setDescription( description );
894 }
895 else
896 {
897 setSummary( description );
898 }
899 }
900
901 void
902 PodcastReader::endDescription()
903 {
904 QString description( m_buffer.trimmed() );
905
906 if( !mightBeHtml( description ) )
907 {
908 // content type is plain text
909 description = textToHtml( description );
910 }
911 // else: content type is html
912 setDescription( description );
913 }
914
915 QString
916 PodcastReader::textToHtml( const QString &text )
917 {
918 QString buf;
919 QRegularExpression re( sd.linkify );
920 int index = 0;
921
922 for(;;)
923 {
924 int next = text.indexOf( re, index );
925
926 if( next == -1 )
927 break;
928
929 if( next != index )
930 {
931 buf += text.mid( index, next - index ).toHtmlEscaped();
932 }
933
934 QString s;
935 QRegularExpressionMatch rmatch = re.match( text, index );
936
937 if( !(s = rmatch.captured( 1 )).isEmpty() )
938 {
939 if( s.startsWith( QLatin1String( "javascript:" ), Qt::CaseInsensitive ) ||
940 s.startsWith( QLatin1String( "exec:" ), Qt::CaseInsensitive ) )
941 {
942 buf += s.toHtmlEscaped();
943 }
944 else
945 {
946 buf += QStringLiteral( "<a href=\"%1\">%1</a>" )
947 .arg( s.toHtmlEscaped() );
948 }
949 }
950 else if( !(s = rmatch.captured( 2 )).isEmpty() )
951 {
952 buf += QStringLiteral( "<a href=\"mailto:%1\">%1</a>" )
953 .arg( s.toHtmlEscaped() );
954 }
955 else if( !rmatch.captured( 3 ).isEmpty() )
956 {
957 buf += QLatin1String("<br/>\n");
958 }
959
960 index = text.indexOf( re, index ) + rmatch.capturedLength();
961 }
962
963 buf += text.mid( index ).toHtmlEscaped();
964
965 return buf;
966 }
967
968 void
969 PodcastReader::endEncoded()
970 {
971 // content type is html
972 setDescription( m_buffer.trimmed() );
973 }
974
975 void
976 PodcastReader::endBody()
977 {
978 // content type is xhtml
979 // always prefer <body>, because it's likely to
980 // contain nice html formatted information
981 setSummary( m_current->description() );
982 m_current->setDescription( m_buffer.trimmed() );
983 }
984
985 void
986 PodcastReader::endLink()
987 {
988 // TODO: change to m_current->... when the field
989 // is moved to the PodcastMetaCommon class.
990 m_channel->setWebLink( QUrl( m_buffer ) );
991 }
992
993 void
994 PodcastReader::beginHtml()
995 {
996 stopWithError( i18n( "While parsing %1, a feed was expected but an HTML page was received."
997 "\nDid you enter the correct URL?", m_url.url() ) );
998 }
999
1000 void
1001 PodcastReader::beginUnknownFeedType()
1002 {
1003 stopWithError( i18n( "Feed has an unknown type: %1", m_url.url() ) );
1004 }
1005
1006 void
1007 PodcastReader::beginRss()
1008 {
1009 if( m_xmlReader.attributes().value( QStringLiteral("version") ) != QStringLiteral( "2.0" ) )
1010 {
1011 // TODO: change this string once we support more
1012 stopWithError( i18n( "%1 is not an RSS version 2.0 feed.", m_url.url() ) );
1013 }
1014 }
1015
1016 void
1017 PodcastReader::beginRdf()
1018 {
1019 bool ok = true;
1020 if( m_xmlReader.namespaceUri() != QStringLiteral( RDF_NS ) )
1021 {
1022 ok = false;
1023 }
1024
1025 if( ok )
1026 {
1027 bool found = false;
1028 for( const QXmlStreamNamespaceDeclaration &nsdecl : m_xmlReader.namespaceDeclarations() )
1029 {
1030 if( nsdecl.namespaceUri() == RSS10_NS )
1031 {
1032 found = true;
1033 break;
1034 }
1035 }
1036
1037 if( !found )
1038 ok = false;
1039 }
1040
1041 if( !ok )
1042 stopWithError( i18n( "%1 is not a valid RSS version 1.0 feed.", m_url.url() ) );
1043 }
1044
1045 void
1046 PodcastReader::beginFeed()
1047 {
1048 if( m_xmlReader.namespaceUri() != QStringLiteral( ATOM_NS ) )
1049 {
1050 stopWithError( i18n( "%1 is not a valid Atom feed.", m_url.url() ) );
1051 }
1052 else
1053 {
1054 beginChannel();
1055 }
1056 }
1057
1058 void
1059 PodcastReader::endDocument()
1060 {
1061 debug() << "successfully parsed feed: " << m_url.url();
1062 Q_EMIT finished( this );
1063 }
1064
1065 void
1066 PodcastReader::createChannel()
1067 {
1068 if( !m_channel )
1069 {
1070 debug() << "new channel";
1071
1072 Podcasts::PodcastChannelPtr channel( new Podcasts::PodcastChannel() );
1073 channel->setUrl( m_url );
1074 channel->setSubscribeDate( QDate::currentDate() );
1075 /* add this new channel to the provider, we get a pointer to a
1076 * PodcastChannelPtr of the correct type which we will use from now on.
1077 */
1078 m_channel = m_podcastProvider->addChannel( channel );
1079 }
1080 }
1081
1082 void
1083 PodcastReader::beginChannel()
1084 {
1085 createChannel();
1086
1087 m_current = m_channel.data();
1088
1089 // Because the summary and description fields are read from several elements
1090 // they only get changed when longer information is read as there is stored in
1091 // the appropriate field already. In order to still be able to correctly update
1092 // the feed's description/summary I set it here to the empty string:
1093 m_channel->setDescription( QLatin1String("") );
1094 m_channel->setSummary( QLatin1String("") );
1095 m_channel->setKeywords( QStringList() );
1096 }
1097
1098 void
1099 PodcastReader::beginItem()
1100 {
1101 // theoretically it is possible that an ugly RSS 1.0 feed has
1102 // first the <item> elements followed by the <channel> element:
1103 createChannel();
1104
1105 m_item = new Podcasts::PodcastEpisode( m_channel );
1106 m_current = m_item.data();
1107
1108 m_enclosures.clear();
1109 }
1110
1111 void
1112 PodcastReader::endItem()
1113 {
1114 // TODO: change superclass of PodcastEpisode to MultiTrack
1115
1116 /* some feeds contain normal blogposts without
1117 enclosures alongside of podcasts */
1118
1119 if( !m_enclosures.isEmpty() )
1120 {
1121 // just take the first enclosure on multi
1122 m_item->setUidUrl( m_enclosures[ 0 ].url() );
1123 m_item->setFilesize( m_enclosures[ 0 ].fileSize() );
1124 m_item->setMimeType( m_enclosures[ 0 ].mimeType() );
1125
1126 m_enclosures.removeAt( 0 );
1127
1128 // append alternative enclosures to description
1129 if( !m_enclosures.isEmpty() )
1130 {
1131 QString description( m_item->description() );
1132 description += QLatin1String("\n<p><b>");
1133 description += i18n( "Alternative Enclosures:" );
1134 description += QLatin1String("</b><br/>\n<ul>");
1135
1136 for( const Enclosure& enclosure : m_enclosures )
1137 {
1138 description += QStringLiteral( "<li><a href=\"%1\">%2</a> (%3, %4)</li>" )
1139 .arg( enclosure.url().url().toHtmlEscaped(),
1140 enclosure.url().fileName().toHtmlEscaped(),
1141 Meta::prettyFilesize( enclosure.fileSize() ),
1142 enclosure.mimeType().isEmpty() ?
1143 i18n( "unknown type" ) :
1144 enclosure.mimeType().toHtmlEscaped() );
1145 }
1146
1147 description += QLatin1String("</ul></p>");
1148 m_item->setDescription( description );
1149 }
1150
1151 Podcasts::PodcastEpisodePtr episode;
1152 QString guid = m_item->guid();
1153 if( guid.isEmpty() )
1154 {
1155 episode = Podcasts::PodcastEpisodePtr::dynamicCast(
1156 m_podcastProvider->trackForUrl( QUrl::fromUserInput(m_item->uidUrl()) )
1157 );
1158 }
1159 else
1160 {
1161 episode = m_podcastProvider->episodeForGuid( guid );
1162 }
1163
1164 //make sure that the episode is not a bogus match. The channel has to be correct.
1165 // See https://bugs.kde.org/show_bug.cgi?id=227515
1166 if( !episode.isNull() && episode->channel() == m_channel )
1167 {
1168 debug() << "updating episode: " << episode->title();
1169
1170 episode->setTitle( m_item->title() );
1171 episode->setSubtitle( m_item->subtitle() );
1172 episode->setSummary( m_item->summary() );
1173 episode->setDescription( m_item->description() );
1174 episode->setAuthor( m_item->author() );
1175 episode->setUidUrl( QUrl::fromUserInput(m_item->uidUrl()) );
1176 episode->setFilesize( m_item->filesize() );
1177 episode->setMimeType( m_item->mimeType() );
1178 episode->setPubDate( m_item->pubDate() );
1179 episode->setKeywords( m_item->keywords() );
1180
1181 // set the guid in case it was empty (for some buggy reason):
1182 episode->setGuid( m_item->guid() );
1183 }
1184 else
1185 {
1186 debug() << "new episode: " << m_item->title();
1187
1188 episode = m_channel->addEpisode( m_item );
1189 // also let the provider know an episode has been added
1190 // TODO: change into a signal
1191 m_podcastProvider->addEpisode( episode );
1192 }
1193 }
1194
1195 m_current = m_channel.data();
1196 m_item = nullptr;
1197 }
1198
1199 void
1200 PodcastReader::beginEnclosure()
1201 {
1202 // This should read both, RSS 2.0 and RSS 1.0 with mod_enclosure
1203 // <enclosure> elements.
1204 // See:
1205 // http://www.rssboard.org/rss-specification
1206 // http://www.xs4all.nl/~foz/mod_enclosure.html
1207 QStringView str;
1208
1209 str = m_xmlReader.attributes().value( QStringLiteral("url") );
1210
1211 if( str.isEmpty() )
1212 str = attribute( RDF_NS, "about" );
1213
1214 if( str.isEmpty() )
1215 {
1216 debug() << "invalid enclosure containing no/empty url";
1217 return;
1218 }
1219
1220 QUrl url( str.toString() );
1221
1222 str = m_xmlReader.attributes().value( QStringLiteral("length") );
1223
1224 if( str.isEmpty() )
1225 str = attribute( ENC_NS, "length" );
1226
1227 int length = str.toString().toInt();
1228
1229 str = m_xmlReader.attributes().value( QStringLiteral("type") );
1230
1231 if( str.isEmpty() )
1232 str = attribute( ENC_NS, "type" );
1233
1234 QString mimeType( str.toString().trimmed() );
1235
1236 m_enclosures.append( Enclosure( url, length, mimeType ) );
1237 }
1238
1239 void
1240 PodcastReader::endGuid()
1241 {
1242 m_item->setGuid( m_buffer );
1243 }
1244
1245 void
1246 PodcastReader::endPubDate()
1247 {
1248 QDateTime pubDate( parsePubDate( m_buffer ) );
1249
1250 if( !pubDate.isValid() )
1251 {
1252 debug() << "invalid podcast episode pubDate: " << m_buffer;
1253 return;
1254 }
1255
1256 m_item->setPubDate( pubDate );
1257 }
1258
1259 void
1260 PodcastReader::beginImage()
1261 {
1262 if( m_xmlReader.namespaceUri() == ITUNES_NS )
1263 {
1264 m_channel->setImageUrl( QUrl( m_xmlReader.attributes().value( QStringLiteral("href") ).toString() ) );
1265 }
1266 }
1267
1268 void
1269 PodcastReader::endImageUrl()
1270 {
1271 // TODO save image data
1272 m_channel->setImageUrl( QUrl( m_buffer ) );
1273 }
1274
1275 void
1276 PodcastReader::endKeywords()
1277 {
1278 QList<QString> keywords( m_current->keywords() );
1279
1280 for( const QString &keyword : m_buffer.split( QLatin1Char(',') ) )
1281 {
1282 QString kwd( keyword.simplified() );
1283 if( !kwd.isEmpty() && !keywords.contains( kwd ) )
1284 keywords.append( kwd );
1285 }
1286
1287 std::sort( keywords.begin(), keywords.end() );
1288 m_current->setKeywords( keywords );
1289
1290 }
1291
1292 void
1293 PodcastReader::endNewFeedUrl()
1294 {
1295 if( m_xmlReader.namespaceUri() == ITUNES_NS )
1296 {
1297 m_url = QUrl( m_buffer.trimmed() );
1298
1299 if( m_channel && m_channel->url() != m_url )
1300 {
1301 debug() << "feed url changed to: " << m_url.url();
1302 m_channel->setUrl( m_url );
1303 }
1304 }
1305 }
1306
1307 void
1308 PodcastReader::endAuthor()
1309 {
1310 m_current->setAuthor( m_buffer.trimmed() );
1311 }
1312
1313 void
1314 PodcastReader::endCreator()
1315 {
1316 // there are funny people that do not use <author> but <dc:creator>
1317 if( m_xmlReader.namespaceUri() == DC_NS )
1318 {
1319 endAuthor();
1320 }
1321 }
1322
1323 void
1324 PodcastReader::beginXml()
1325 {
1326 m_buffer += QLatin1Char( '<' );
1327 m_buffer += m_xmlReader.name().toString();
1328
1329 for( const QXmlStreamAttribute &attr : m_xmlReader.attributes() )
1330 {
1331 m_buffer += QStringLiteral( " %1=\"%2\"" )
1332 .arg( attr.name().toString(),
1333 attr.value().toString().toHtmlEscaped() );
1334 }
1335
1336 m_buffer += QLatin1Char( '>' );
1337 }
1338
1339 void
1340 PodcastReader::beginNoElement()
1341 {
1342 DEBUG_BLOCK
1343 debug() << "no element expected here, but got element: "
1344 << m_xmlReader.name();
1345 }
1346
1347 void
1348 PodcastReader::beginAtomText()
1349 {
1350 if( hasAttribute( ATOM_NS, "type" ) )
1351 {
1352 QStringView type( attribute( ATOM_NS, "type" ) );
1353
1354 if( type == QStringLiteral("text") )
1355 {
1356 m_contentType = TextContent;
1357 }
1358 else if( type == QStringLiteral("html") )
1359 {
1360 m_contentType = HtmlContent;
1361 }
1362 else if( type == QStringLiteral("xhtml") )
1363 {
1364 m_contentType = XHtmlContent;
1365 }
1366 else
1367 {
1368 // this should not happen, see elementType()
1369 debug() << "unsupported atom:content type: " << type.toString();
1370 m_contentType = TextContent;
1371 }
1372 }
1373 else
1374 {
1375 m_contentType = TextContent;
1376 }
1377
1378 m_buffer.clear();
1379 }
1380
1381 void
1382 PodcastReader::beginAtomTextChild()
1383 {
1384 switch( m_contentType )
1385 {
1386 case XHtmlContent:
1387 beginXml();
1388 break;
1389
1390 case HtmlContent:
1391 case TextContent:
1392 // stripping illegal tags
1393 debug() << "read unexpected open tag in atom text: " << m_xmlReader.name();
1394
1395 default:
1396 break;
1397 }
1398 }
1399
1400 void
1401 PodcastReader::endAtomTextChild()
1402 {
1403 switch( m_contentType )
1404 {
1405 case XHtmlContent:
1406 endXml();
1407 break;
1408
1409 case HtmlContent:
1410 case TextContent:
1411 // stripping illegal tags
1412 debug() << "read unexpected close tag in atom text: " << m_xmlReader.name();
1413
1414 default:
1415 break;
1416 }
1417 }
1418
1419 void
1420 PodcastReader::readAtomTextCharacters()
1421 {
1422 switch( m_contentType )
1423 {
1424 case XHtmlContent:
1425 m_buffer += m_xmlReader.text().toString().toHtmlEscaped();
1426 break;
1427
1428 case HtmlContent:
1429 m_buffer += m_xmlReader.text();
1430 break;
1431
1432 case TextContent:
1433 m_buffer += m_xmlReader.text();
1434
1435 default:
1436 break;
1437 }
1438 }
1439
1440 void
1441 PodcastReader::beginAtomFeedLink()
1442 {
1443 if( !hasAttribute( ATOM_NS, "rel" ) ||
1444 attribute( ATOM_NS, "rel" ) == QStringLiteral("alternate") )
1445 {
1446 m_channel->setWebLink( QUrl( attribute( ATOM_NS, "href" ).toString() ) );
1447 }
1448 else if( attribute( ATOM_NS, "rel" ) == QStringLiteral("self") )
1449 {
1450 m_url = QUrl( attribute( ATOM_NS, "href" ).toString() );
1451
1452 if( m_channel && m_channel->url() != m_url )
1453 {
1454 debug() << "feed url changed to: " << m_url.url();
1455 m_channel->setUrl( m_url );
1456 }
1457 }
1458 }
1459
1460 void
1461 PodcastReader::beginAtomEntryLink()
1462 {
1463 if( attribute( ATOM_NS, "rel" ) == QStringLiteral("enclosure") )
1464 {
1465 QUrl url( attribute( ATOM_NS, "href" ).toString() );
1466 int filesize = 0;
1467 QString mimeType;
1468
1469 if( hasAttribute( ATOM_NS, "length" ) )
1470 {
1471 filesize = attribute( ATOM_NS, "length" ).toString().toInt();
1472 }
1473
1474 if( hasAttribute( ATOM_NS, "type" ) )
1475 {
1476 mimeType = attribute( ATOM_NS, "type" ).toString();
1477 }
1478
1479 m_enclosures.append( Enclosure( url, filesize, mimeType ) );
1480 }
1481 }
1482
1483 void
1484 PodcastReader::endAtomIcon()
1485 {
1486 if( !m_channel->hasImage() )
1487 {
1488 endImageUrl();
1489 }
1490 }
1491
1492 void
1493 PodcastReader::endAtomTitle()
1494 {
1495 // TODO: don't convert text but store m_contentType
1496 m_current->setTitle( atomTextAsText().trimmed() );
1497 }
1498
1499 void
1500 PodcastReader::endAtomSubtitle()
1501 {
1502 // TODO: don't convert text but store m_contentType
1503 m_current->setSubtitle( atomTextAsText().trimmed() );
1504 }
1505
1506 void
1507 PodcastReader::endAtomSummary()
1508 {
1509 // TODO: don't convert text but store m_contentType
1510 m_current->setSummary( atomTextAsHtml().trimmed() );
1511 }
1512
1513 void
1514 PodcastReader::endAtomContent()
1515 {
1516 // TODO: don't convert text but store m_contentType
1517 m_current->setDescription( atomTextAsHtml() );
1518 }
1519
1520 void
1521 PodcastReader::endAtomPublished()
1522 {
1523 QDateTime date = QDateTime::fromString( m_buffer, Qt::ISODate );
1524
1525 if( !date.isValid() )
1526 {
1527 debug() << "invalid podcast episode atom:published date: " << m_buffer;
1528 return;
1529 }
1530
1531 if( !m_item->pubDate().isValid() || m_item->pubDate() < date )
1532 {
1533 m_item->setPubDate( date );
1534 }
1535 }
1536
1537 void
1538 PodcastReader::endAtomUpdated()
1539 {
1540 QDateTime date = QDateTime::fromString( m_buffer, Qt::ISODate );
1541
1542 if( !date.isValid() )
1543 {
1544 debug() << "invalid podcast episode atom:updated date: " << m_buffer;
1545 return;
1546 }
1547
1548 if( !m_item->pubDate().isValid() || m_item->pubDate() < date )
1549 {
1550 // TODO: add field updatedDate and use this (throughout amarok)
1551 m_item->setPubDate( date );
1552 }
1553 }
1554
1555 void
1556 PodcastReader::readNoCharacters()
1557 {
1558 DEBUG_BLOCK
1559 debug() << "no characters expected here";
1560 }
1561
1562 void
1563 PodcastReader::endXml()
1564 {
1565 m_buffer += QLatin1String("</");
1566 m_buffer += m_xmlReader.name().toString();
1567 m_buffer += QLatin1Char('>');
1568 }
1569
1570 void
1571 PodcastReader::readCharacters()
1572 {
1573 m_buffer += m_xmlReader.text();
1574 }
1575
1576 void
1577 PodcastReader::readEscapedCharacters()
1578 {
1579 m_buffer += m_xmlReader.text().toString().toHtmlEscaped() ;
1580 }
1581
1582 QStringView
1583 PodcastReader::attribute( const char *namespaceUri, const char *name ) const
1584 {
1585 // workaround, because Qt seems to have a bug:
1586 // when the default namespace is used attributes
1587 // aren't inside this namespace for some reason
1588 if( m_xmlReader.attributes().hasAttribute( QLatin1String(namespaceUri), QLatin1String(name) ) )
1589 return m_xmlReader.attributes().value( QLatin1String(namespaceUri), QLatin1String(name) );
1590 else
1591 return m_xmlReader.attributes().value( QString(), QLatin1String(name) );
1592 }
1593
1594 bool
1595 PodcastReader::hasAttribute( const char *namespaceUri, const char *name ) const
1596 {
1597 // see PodcastReader::attribute()
1598 if( m_xmlReader.attributes().hasAttribute( QLatin1String(namespaceUri), QLatin1String(name) ) )
1599 return true;
1600 else
1601 return m_xmlReader.attributes().hasAttribute( QString(), QLatin1String(name) );
1602 }
1603
1604 QDateTime
1605 PodcastReader::parsePubDate( const QString &dateString )
1606 {
1607 DEBUG_BLOCK
1608 QString parseInput = dateString;
1609 debug() << "Parsing pubdate: " << parseInput;
1610
1611 QRegularExpression rfcDateDayRegex( QStringLiteral("^[A-Z]{1}[a-z]{2}\\s*,\\s*(.*)") );
1612 QRegularExpressionMatch dateMatch = rfcDateDayRegex.match( parseInput );
1613 if( dateMatch.hasMatch() )
1614 {
1615 parseInput = dateMatch.captured(1);
1616 }
1617 //Hack around a to strict RFCDate implementation in KDateTime.
1618 //See https://bugs.kde.org/show_bug.cgi?id=231062
1619 QRegularExpression rfcMonthLowercase( QStringLiteral("^\\d+\\s+\\b(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)\\b") );
1620 QRegularExpressionMatch monthMatch = rfcDateDayRegex.match( parseInput );
1621 if( monthMatch.hasMatch() )
1622 {
1623 QString lowerMonth = monthMatch.captured( 1 );
1624 QString upperMonth = lowerMonth;
1625 upperMonth.replace( 0, 1, lowerMonth.at( 0 ).toUpper() );
1626 parseInput.replace( lowerMonth, upperMonth );
1627 }
1628
1629 // Remove any GMT suffix to prevent RFC2822 parsing from breaking. Replacing with +0000 could also be option, but would
1630 // break any date strings where the timezone is e.g. GMT+0000, and current parser considers no timezone GMT/UTC anyhow.
1631 QDateTime pubDate = QDateTime::fromString( parseInput.remove( QStringLiteral("GMT") ), Qt::RFC2822Date );
1632
1633 debug() << "result: " << pubDate.toString();
1634 return pubDate;
1635 }
1636
1637 void
1638 PodcastReader::slotRedirection( KIO::Job * job, const QUrl &url )
1639 {
1640 DEBUG_BLOCK
1641 Q_UNUSED( job );
1642 debug() << "redirected to: " << url.url();
1643 }
1644
1645 void
1646 PodcastReader::slotPermanentRedirection( KIO::Job * job, const QUrl &fromUrl,
1647 const QUrl &toUrl )
1648 {
1649 DEBUG_BLOCK
1650 Q_UNUSED( job );
1651 Q_UNUSED( fromUrl );
1652 debug() << "permanently redirected to: " << toUrl.url();
1653 m_url = toUrl;
1654 /* change the url for existing feeds as well. Permanent redirection means the old one
1655 might disappear soon. */
1656 if( m_channel )
1657 m_channel->setUrl( m_url );
1658 }
1659
1660 Podcasts::PodcastEpisodePtr
1661 PodcastReader::podcastEpisodeCheck( Podcasts::PodcastEpisodePtr episode )
1662 {
1663 // DEBUG_BLOCK
1664 Podcasts::PodcastEpisodePtr episodeMatch = episode;
1665 Podcasts::PodcastEpisodeList episodes = m_channel->episodes();
1666
1667 // debug() << "episode title: " << episode->title();
1668 // debug() << "episode url: " << episode->prettyUrl();
1669 // debug() << "episode guid: " << episode->guid();
1670
1671 for( PodcastEpisodePtr match : episodes )
1672 {
1673 // debug() << "match title: " << match->title();
1674 // debug() << "match url: " << match->prettyUrl();
1675 // debug() << "match guid: " << match->guid();
1676
1677 int score = 0;
1678 if( !episode->title().isEmpty() && episode->title() == match->title() )
1679 score += 1;
1680 if( !episode->prettyUrl().isEmpty() && episode->prettyUrl() == match->prettyUrl() )
1681 score += 3;
1682 if( !episode->guid().isEmpty() && episode->guid() == match->guid() )
1683 score += 3;
1684
1685 // debug() << "score: " << score;
1686 if( score >= 3 )
1687 {
1688 episodeMatch = match;
1689 break;
1690 }
1691 }
1692
1693 return episodeMatch;
1694 }
1695