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

List:       kde-commits
Subject:    [kdeplasma-addons/plasma/sreich/youtube-runner] runners/youtube: factor out the tubejob a bit, we'll
From:       Shaun Reich <shaun.reich () kdemail ! net>
Date:       2012-02-26 14:31:49
Message-ID: 20120226143149.F3391A60A6 () git ! kde ! org
[Download RAW message or body]

Git commit edcb9a5faaced805dcd5f7e54a85a9a3ce1109f3 by Shaun Reich.
Committed on 26/02/2012 at 15:31.
Pushed by sreich into branch 'plasma/sreich/youtube-runner'.

factor out the tubejob a bit, we'll see if we can use an eventloop

to fix all of our problems somehow

M  +1    -0    runners/youtube/CMakeLists.txt
M  +15   -56   runners/youtube/youtube.cpp
M  +0    -31   runners/youtube/youtube.h

http://commits.kde.org/kdeplasma-addons/edcb9a5faaced805dcd5f7e54a85a9a3ce1109f3

diff --git a/runners/youtube/CMakeLists.txt b/runners/youtube/CMakeLists.txt
index a84b55d..f2e52c9 100644
--- a/runners/youtube/CMakeLists.txt
+++ b/runners/youtube/CMakeLists.txt
@@ -1,5 +1,6 @@
 set(krunner_youtube_SRCS
     youtube.cpp
+    tubejob.cpp
 )
 
 kde4_add_plugin(krunner_youtube ${krunner_youtube_SRCS})
diff --git a/runners/youtube/youtube.cpp b/runners/youtube/youtube.cpp
index 6418515..a1110ee 100644
--- a/runners/youtube/youtube.cpp
+++ b/runners/youtube/youtube.cpp
@@ -17,6 +17,7 @@
  *****************************************************************************/
 
 #include "youtube.h"
+#include "tubejob.h"
 
 #include <KDebug>
 #include <KToolInvocation>
@@ -30,21 +31,18 @@
 //but seeing as youtube doesn't fully support html5 (only for non-ad'ed videos), i \
guess i'll have to hold off on it?  YouTube::YouTube(QObject *parent, const \
QVariantList& args)  : Plasma::AbstractRunner(parent, args)
-    , m_context(0)
 {
     Q_UNUSED(args);
     setObjectName(QLatin1String("YouTube"));
     setIgnoredTypes(Plasma::RunnerContext::FileSystem | \
Plasma::RunnerContext::Directory | Plasma::RunnerContext::NetworkLocation);  
-    Plasma::RunnerSyntax s(QLatin1String( ":q:" ), i18n("Finds YouTube video \
matching :q:.")); +    Plasma::RunnerSyntax s(QLatin1String(":q:"), i18n("Finds \
YouTube video matching :q:."));  s.addExampleQuery(QLatin1String("youtube :q:"));
     addSyntax(s);
 
-    addSyntax(Plasma::RunnerSyntax(QLatin1String( "youtube" ), i18n("Lists the \
videos matching the query, using YouTube search"))); +    \
addSyntax(Plasma::RunnerSyntax(QLatin1String("youtube"), i18n("Lists the videos \
matching the query, using YouTube search")));  setSpeed(SlowSpeed);
     setPriority(LowPriority);
-
-    qRegisterMetaType<Plasma::RunnerContext*>();
 }
 
 YouTube::~YouTube()
@@ -53,10 +51,7 @@ YouTube::~YouTube()
 
 void YouTube::match(Plasma::RunnerContext &context)
 {
-    m_context = &context;
     kDebug() << "MATCH MADE, emitting matchmade";
-    connect(this, SIGNAL(matchMade(Plasma::RunnerContext*)), this, \
                SLOT(startYouTubeJob(Plasma::RunnerContext*)));
-    emit matchMade(&context);
 
     const QString term = context.query();
     if (term.length() < 3) {
@@ -66,6 +61,18 @@ void YouTube::match(Plasma::RunnerContext &context)
     if (!context.isValid()) {
         return;
     }
+
+    QEventLoop loop;
+    // Wait a second, we don't want to  query on every keypress
+    QMutex mutex;
+    QWaitCondition waiter;
+    mutex.lock();
+    waiter.wait(&mutex, 1000);
+    mutex.unlock();
+
+    TubeJob tubeJob(term);
+    connect(&tubeJob, SIGNAL(finished()), &loop, SLOT(quit()));
+    loop.exec();
 }
 
 void YouTube::run(const Plasma::RunnerContext &context, const Plasma::QueryMatch \
&match) @@ -83,34 +90,6 @@ void YouTube::run(const Plasma::RunnerContext &context, \
const Plasma::QueryMatch  //    }
 }
 
-void YouTube::startYouTubeJob(Plasma::RunnerContext *context)
-{
-
-    kDebug() << "%%%%%% YOUTUBE RUNNING JOB!";
-    TubeJob *job = new \
TubeJob(KUrl("http://gdata.youtube.com/feeds/api/videos?max-results=1&q=taylor \
                swift"), KIO::NoReload, KIO::HideProgressInfo, context);
-    connect(job, SIGNAL(dataReceived(KIO::Job*,QByteArray,Plasma::RunnerContext*)), \
                this, \
                SLOT(dataArrived(KIO::Job*,QByteArray,Plasma::RunnerContext*)));
-    job->start();
-}
-
-void YouTube::dataArrived(KIO::Job* job, const QByteArray& data, \
                Plasma::RunnerContext* context)
-{
-    kDebug()  << "DATA:" << data;
-    if (!data.isEmpty()) {
-        parseXML(data);
-    }
-    const QString term = context->query();
-    Plasma::QueryMatch match(this);
-    match.setType(Plasma::QueryMatch::PossibleMatch);
-
-    //  match.setRelevance(1.0);
-    //  match.setIcon(m_icon);
-//    match.setData("TEST");
-    match.setText(QLatin1String( "YouTube: " ));
-
-    context->addMatch(term, match);
-
-}
-
 void YouTube::parseXML(QByteArray data)
 {
     QXmlStreamReader xml(data);
@@ -187,24 +166,4 @@ void YouTube::parseVideo(QXmlStreamReader& xml)
     }
 }
 
-TubeJob::TubeJob(const KUrl& url, KIO::LoadType type, KIO::JobFlag flags, \
                Plasma::RunnerContext *context)
-  : QObject()
-  , m_context(context)
-  , m_job(0)
-{
-    m_job = KIO::get(url, type, flags);
-    connect(m_job, SIGNAL(data(KIO::Job*,QByteArray)), \
                SLOT(onData(KIO::Job*,QByteArray)));
-}
-
-void TubeJob::onData(KIO::Job* job, QByteArray data)
-{
-    emit dataReceived(job, data, m_context);
-}
-
-void TubeJob::start()
-{
-    m_job->start();
-}
-
-
 #include "youtube.moc"
diff --git a/runners/youtube/youtube.h b/runners/youtube/youtube.h
index cd294d1..57bce0e 100644
--- a/runners/youtube/youtube.h
+++ b/runners/youtube/youtube.h
@@ -43,42 +43,11 @@ public:
 Q_SIGNALS:
     void matchMade(Plasma::RunnerContext *context);
 
-private slots:
-    void dataArrived(KIO::Job* job, const QByteArray& data, Plasma::RunnerContext* \
                context);
-    void startYouTubeJob(Plasma::RunnerContext *context);
-
 private:
     void parseXML(QByteArray);
     void parseVideo(QXmlStreamReader& xml);
-
-    QByteArray buffer;
-
-    Plasma::RunnerContext *m_context;
-};
-
-class TubeJob : public QObject
-{
-    Q_OBJECT
-
-public:
-    TubeJob(const KUrl& url, KIO::LoadType type, KIO::JobFlag flags, \
                Plasma::RunnerContext *context);
-
-    void start();
-
-Q_SIGNALS:
-    void dataReceived(KIO::Job* job, QByteArray data, Plasma::RunnerContext \
                *context);
-
-private Q_SLOTS:
-    void onData(KIO::Job* job, QByteArray data);
-
-private:
-    Plasma::RunnerContext *m_context;
-
-    KIO::TransferJob *m_job;
 };
 
-Q_DECLARE_METATYPE(Plasma::RunnerContext*);
-
 K_EXPORT_PLASMA_RUNNER(youtube, YouTube)
 
 #endif


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

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