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

List:       kde-commits
Subject:    [Amarok]  Add PodcastImageFetcher worker class.
From:       Bart Cerneels <bart.cerneels () kde ! org>
Date:       2009-11-14 16:15:43
Message-ID: 200911141615.nAEGFh3M017547 () Wurst ! kollide ! net
[Download RAW message or body]

commit c5812ca5f1f3efa1aa34b847dbf83225733fd42b
Author:     Bart Cerneels <bart.cerneels@kde.org>
AuthorDate: Thu Nov 12 22:20:04 2009 +0100
Commit:     Bart Cerneels <bart.cerneels@kde.org>
CommitDate: Sat Nov 14 16:57:19 2009 +0100

    Add PodcastImageFetcher worker class.

diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index a0b7e5d..21c5e1c 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -377,6 +377,7 @@ set( libcontextview_SRCS
 set(libpodcasts_SRCS
     podcasts/PodcastReader.cpp
     podcasts/PodcastMeta.cpp
+    podcasts/PodcastImageFetcher.cpp
     podcasts/sql/SqlPodcastMeta.cpp
     podcasts/sql/SqlPodcastProvider.cpp
     podcasts/sql/PodcastSettingsDialog.cpp
diff --git a/src/podcasts/PodcastImageFetcher.cpp b/src/podcasts/PodcastImageFetcher.cpp
new file mode 100644
index 0000000..8fd46b9
--- /dev/null
+++ b/src/podcasts/PodcastImageFetcher.cpp
@@ -0,0 +1,42 @@
+/****************************************************************************************
+ * Copyright (c) 2009 Bart Cerneels <bart.cerneels@kde.org>                             *
+ *                                                                                      *
+ * This program is free software; you can redistribute it and/or modify it under        *
+ * the terms of the GNU General Public License as published by the Free Software        *
+ * Foundation; either version 2 of the License, or (at your option) any later           *
+ * version.                                                                             *
+ *                                                                                      *
+ * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
+ * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
+ *                                                                                      *
+ * You should have received a copy of the GNU General Public License along with         *
+ * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
+ ****************************************************************************************/
+
+#include "PodcastImageFetcher.h"
+
+#include "Debug.h"
+
+PodcastImageFetcher::PodcastImageFetcher()
+{
+}
+
+void
+PodcastImageFetcher::addChannel( Meta::PodcastChannelPtr channel )
+{
+    m_channel = channel;
+}
+
+void
+PodcastImageFetcher::addEpisode( Meta::PodcastEpisodePtr episode )
+{
+    Q_UNUSED( episode );
+}
+
+void
+PodcastImageFetcher::run()
+{
+    DEBUG_BLOCK
+
+}
diff --git a/src/podcasts/PodcastImageFetcher.h b/src/podcasts/PodcastImageFetcher.h
new file mode 100644
index 0000000..054c76a
--- /dev/null
+++ b/src/podcasts/PodcastImageFetcher.h
@@ -0,0 +1,49 @@
+/****************************************************************************************
+ * Copyright (c) 2009 Bart Cerneels <bart.cerneels@kde.org>                             *
+ *                                                                                      *
+ * This program is free software; you can redistribute it and/or modify it under        *
+ * the terms of the GNU General Public License as published by the Free Software        *
+ * Foundation; either version 2 of the License, or (at your option) any later           *
+ * version.                                                                             *
+ *                                                                                      *
+ * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
+ * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
+ *                                                                                      *
+ * You should have received a copy of the GNU General Public License along with         *
+ * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
+ ****************************************************************************************/
+
+#ifndef PODCASTIMAGEFETCHER_H
+#define PODCASTIMAGEFETCHER_H
+
+#include "PodcastMeta.h"
+
+#include <KJob>
+
+class PodcastImageFetcher : public QObject
+{
+Q_OBJECT
+public:
+    PodcastImageFetcher();
+
+    void addChannel( Meta::PodcastChannelPtr channel );
+    void addEpisode( Meta::PodcastEpisodePtr episode );
+
+    void run();
+
+signals:
+    void imageReady( Meta::PodcastChannelPtr channel, QPixmap image );
+    void imageReady( Meta::PodcastEpisodePtr episode, QPixmap image );
+    void done();
+
+private slots:
+    void slotDownloadFinished( KJob *job );
+
+private:
+    Meta::PodcastChannelPtr m_channel;
+    QMap<KJob *, Meta::PodcastChannelPtr> m_jobChannelMap;
+    QMap<KJob *, Meta::PodcastEpisodePtr> m_jobEpisodeMap;
+};
+
+#endif // PODCASTIMAGEFETCHER_H
diff --git a/src/podcasts/sql/SqlPodcastProvider.cpp b/src/podcasts/sql/SqlPodcastProvider.cpp
index 83f646e..d7a7eb7 100644
--- a/src/podcasts/sql/SqlPodcastProvider.cpp
+++ b/src/podcasts/sql/SqlPodcastProvider.cpp
@@ -23,6 +23,7 @@
 #include "context/popupdropper/libpud/PopupDropper.h"
 #include "Debug.h"
 #include "EngineController.h"
+#include "PodcastImageFetcher.h"
 #include "PodcastModel.h"
 #include "PodcastReader.h"
 #include "PodcastSettingsDialog.h"
@@ -36,6 +37,8 @@
 #include <KIO/DeleteJob>
 #include <KIO/Job>
 #include <KIO/NetAccess>
+#include <KMD5>
+#include <KStandardDirs>
 #include <KUrl>
 #include <Solid/Networking>
 
@@ -60,6 +63,7 @@ SqlPodcastProvider::SqlPodcastProvider()
     , m_renameAction( 0 )
     , m_updateAction( 0 )
     , m_writeTagsAction( 0 )
+    , m_podcastImageFetcher( 0 )
 {
     connect( m_updateTimer, SIGNAL( timeout() ), SLOT( autoUpdate() ) );
 
@@ -134,7 +138,14 @@ SqlPodcastProvider::loadPodcasts()
     for(int i=0; i < results.size(); i+=rowLength)
     {
         QStringList channelResult = results.mid( i, rowLength );
-        m_channels << SqlPodcastChannelPtr( new SqlPodcastChannel( channelResult ) );
+        SqlPodcastChannelPtr channel =
+                SqlPodcastChannelPtr( new SqlPodcastChannel( channelResult ) );
+        if( hasCachedImage( channel ) )
+            channel->setImage( loadImage( channel ) );
+        else
+            fetchImage( channel );
+
+        m_channels << channel;
     }
     emit( updated() );
 }
@@ -976,4 +987,43 @@ SqlPodcastProvider::updateDatabase( int fromVersion, int toVersion )
     loadPodcasts();
 }
 
+QString
+SqlPodcastProvider::imagePath( SqlPodcastChannelPtr channel )
+{
+    KUrl imagePath = channel->saveLocation();
+    KMD5 md5( channel->url().url().toLocal8Bit() );
+    imagePath.addPath( md5.hexDigest() + "png" );
+    return imagePath.toLocalFile();
+}
+
+bool
+SqlPodcastProvider::hasCachedImage( SqlPodcastChannelPtr channel )
+{
+    DEBUG_BLOCK
+    return QFile( imagePath( channel ) ).exists();
+}
+
+QPixmap
+SqlPodcastProvider::loadImage( SqlPodcastChannelPtr channel )
+{
+    DEBUG_BLOCK
+    if( !hasCachedImage( channel ) )
+        return QPixmap();
+
+    return QPixmap( imagePath( channel ) );
+}
+
+void
+SqlPodcastProvider::fetchImage( SqlPodcastChannelPtr channel )
+{
+    DEBUG_BLOCK
+
+    if( m_podcastImageFetcher == 0 )
+        m_podcastImageFetcher = new PodcastImageFetcher();
+
+    m_podcastImageFetcher->addChannel( PodcastChannelPtr::dynamicCast( channel ) );
+
+    m_podcastImageFetcher->run();
+}
+
 #include "SqlPodcastProvider.moc"
diff --git a/src/podcasts/sql/SqlPodcastProvider.h b/src/podcasts/sql/SqlPodcastProvider.h
index 6aaa51b..a44bbae 100644
--- a/src/podcasts/sql/SqlPodcastProvider.h
+++ b/src/podcasts/sql/SqlPodcastProvider.h
@@ -24,6 +24,8 @@
 #include <kio/jobclasses.h>
 #include <klocale.h>
 
+class PodcastImageFetcher;
+
 class KUrl;
 class PodcastReader;
 class SqlStorage;
@@ -106,6 +108,10 @@ class SqlPodcastProvider : public PodcastProvider, public EngineObserver
         void createTables() const;
         void loadPodcasts();
         void updateDatabase( int fromVersion, int toVersion );
+        QString imagePath( Meta::SqlPodcastChannelPtr channel );
+        bool hasCachedImage( Meta::SqlPodcastChannelPtr channel );
+        QPixmap loadImage( Meta::SqlPodcastChannelPtr channel );
+        void fetchImage( Meta::SqlPodcastChannelPtr channel );
 
         Meta::SqlPodcastChannelList m_channels;
 
@@ -122,6 +128,8 @@ class SqlPodcastProvider : public PodcastProvider, public EngineObserver
         QAction * m_renameAction; //rename a Channel or Episode
         QAction * m_updateAction;
         QAction * m_writeTagsAction; //write feed information to downloaded file
+
+        PodcastImageFetcher *m_podcastImageFetcher;
 };
 
 #endif


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

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