Hi,

I worked on this issue, and please find the proposed patch attached.

Could somebody please try this patch and confirm that its working as
expected?

Thanks,
Prasad



On 11 August 2014 21:29, Prasad Bhat <hlpr...@gmail.com> wrote:

> Hi,
>
> I am interested in working on this feature request - Bug #251199. Link -
> https://bugs.kde.org/show_bug.cgi?id=251199.
>
> Could somebody please provide inputs specific to this?
>
> I am new to KDE SDK, Amarok codebase. But, I am proficient in C++ and know
> Qt reasonably well. I use Kdevelop extensively in my work.
>
> I am going through https://community.kde.org/Amarok/Development, to set
> the development environment.
>
> Thanks,
> Prasad
>



-- 
Prasad H. L.
From 1e0b918b6d459d8a63c19cd8c0e2ee39b5d2e97a Mon Sep 17 00:00:00 2001
From: Prasad Bhat <hlpr...@gmail.com>
Date: Mon, 18 Aug 2014 23:40:14 +0530
Subject: [PATCH] BUG 251199: Fixes to Wikipedia and Lyrics applets in context
 panel. This fix makes them download/update only when in active view.

---
 src/context/Applet.cpp                            | 15 +++++
 src/context/Applet.h                              |  4 ++
 src/context/LyricsManager.h                       |  3 +
 src/context/applets/lyrics/LyricsApplet.cpp       | 25 +++++++++
 src/context/applets/lyrics/LyricsApplet.h         |  2 +
 src/context/applets/wikipedia/WikipediaApplet.cpp | 30 +++++++++-
 src/context/applets/wikipedia/WikipediaApplet.h   |  2 +
 src/context/engines/lyrics/LyricsEngine.cpp       | 16 ++++++
 src/context/engines/lyrics/LyricsEngine.h         |  6 ++
 src/context/engines/wikipedia/WikipediaEngine.cpp | 67 +++++++++++++++++++----
 10 files changed, 156 insertions(+), 14 deletions(-)

diff --git a/src/context/Applet.cpp b/src/context/Applet.cpp
index 0d0fe4e..af9a59c 100644
--- a/src/context/Applet.cpp
+++ b/src/context/Applet.cpp
@@ -371,4 +371,19 @@ Context::Applet::showWarning( const QString &message, const char *slot )
         warning() << "invoking failed:" << sig;
 }
 
+bool Context::Applet::event(QEvent* e)
+{
+    if(e->type() == QEvent::WindowActivate || e->type() == QEvent::WindowDeactivate)
+        windowActivationEvent(e);
+
+    return QGraphicsWidget::event(e);
+}
+
+void Context::Applet::windowActivationEvent(QEvent* e)
+{
+    Q_UNUSED(e);
+}
+
+
+
 #include "Applet.moc"
diff --git a/src/context/Applet.h b/src/context/Applet.h
index 55f95d9..b79a0d9 100644
--- a/src/context/Applet.h
+++ b/src/context/Applet.h
@@ -153,6 +153,10 @@ class AMAROK_EXPORT Applet : public Plasma::Applet
          */
         void showWarning( const QString &message, const char *slot );
 
+        virtual bool event(QEvent *e);
+
+        virtual void windowActivationEvent(QEvent *e);
+
     public Q_SLOTS:
         virtual void destroy();
 
diff --git a/src/context/LyricsManager.h b/src/context/LyricsManager.h
index 940fb78..1d36578 100644
--- a/src/context/LyricsManager.h
+++ b/src/context/LyricsManager.h
@@ -64,6 +64,9 @@ class AMAROK_EXPORT LyricsObserver
          */
         virtual void lyricsMessage( const QString& key, const QString &val ) { Q_UNUSED( key ); Q_UNUSED( val ); }
 
+        virtual void enable() {}
+        virtual void disable() {}
+
     private:
         LyricsSubject *m_subject;
 };
diff --git a/src/context/applets/lyrics/LyricsApplet.cpp b/src/context/applets/lyrics/LyricsApplet.cpp
index 829a166..b39326d 100644
--- a/src/context/applets/lyrics/LyricsApplet.cpp
+++ b/src/context/applets/lyrics/LyricsApplet.cpp
@@ -735,4 +735,29 @@ LyricsApplet::keyPressEvent( QKeyEvent *e )
     Context::Applet::keyPressEvent( e );
 }
 
+void LyricsApplet::windowActivationEvent(QEvent* e)
+{
+    Q_UNUSED(e);
+    Q_D(LyricsApplet);
+
+    debug() << "Activation type = " << e->type();
+
+    LyricsObserver *o = dynamic_cast<LyricsObserver *>(dataEngine("amarok-lyrics"));
+
+    if(o)
+    {
+        if(e->type() == QEvent::WindowActivate)
+        {
+            o->enable();
+        }
+        else if(e->type() == QEvent::WindowDeactivate)
+        {
+            o->disable();
+        }
+    }
+
+    Context::Applet::windowActivationEvent(e);
+}
+
+
 #include "LyricsApplet.moc"
diff --git a/src/context/applets/lyrics/LyricsApplet.h b/src/context/applets/lyrics/LyricsApplet.h
index 20c71d8..773b540 100644
--- a/src/context/applets/lyrics/LyricsApplet.h
+++ b/src/context/applets/lyrics/LyricsApplet.h
@@ -35,6 +35,8 @@ public:
 
     bool hasHeightForWidth() const;
 
+    void windowActivationEvent(QEvent *e);
+
 public slots:
     virtual void init();
     void connectSource( const QString& source );
diff --git a/src/context/applets/wikipedia/WikipediaApplet.cpp b/src/context/applets/wikipedia/WikipediaApplet.cpp
index 21365f2..7ce34d1 100644
--- a/src/context/applets/wikipedia/WikipediaApplet.cpp
+++ b/src/context/applets/wikipedia/WikipediaApplet.cpp
@@ -696,6 +696,9 @@ WikipediaApplet::dataUpdated( const QString &source, const Plasma::DataEngine::D
     Q_UNUSED( source )
     Q_D( WikipediaApplet );
 
+    if(!isActiveWindow())
+        return;
+
     if( data.isEmpty() )
     {
         debug() << "data Empty!";
@@ -736,7 +739,7 @@ WikipediaApplet::dataUpdated( const QString &source, const Plasma::DataEngine::D
         if( !message.isEmpty() )
         {
             setHeaderText( i18n( "Wikipedia: %1", message ) );
-            d->dataContainer->removeAllData();
+            //d->dataContainer->removeAllData();
         }
     }
     else if( data.contains( "sourceUrl" ) )
@@ -755,7 +758,7 @@ WikipediaApplet::dataUpdated( const QString &source, const Plasma::DataEngine::D
             d->_updateWebFonts();
             d->currentUrl = url;
             d->webView->setHtml( data[ "page" ].toString(), url );
-            d->dataContainer->removeAllData();
+            //d->dataContainer->removeAllData();
         }
         setCollapseOff();
     }
@@ -802,4 +805,27 @@ WikipediaApplet::createConfigurationInterface( KConfigDialog *parent )
     QTimer::singleShot( 0, this, SLOT(_configureLangSelector()) );
 }
 
+void WikipediaApplet::windowActivationEvent(QEvent* e)
+{
+    Q_UNUSED(e);
+    Q_D(WikipediaApplet);
+
+    debug() << "Activation type = " << e->type();
+
+    if(e->type() == QEvent::WindowActivate)
+    {
+        d->dataContainer->setData( "enabled", true );
+        d->scheduleEngineUpdate();
+    }
+    else if(e->type() == QEvent::WindowDeactivate)
+    {
+        d->dataContainer->setData( "enabled", false);
+        d->scheduleEngineUpdate();
+    }
+
+
+    Context::Applet::windowActivationEvent(e);
+}
+
+
 #include "WikipediaApplet.moc"
diff --git a/src/context/applets/wikipedia/WikipediaApplet.h b/src/context/applets/wikipedia/WikipediaApplet.h
index 9663ae3..62e0a33 100644
--- a/src/context/applets/wikipedia/WikipediaApplet.h
+++ b/src/context/applets/wikipedia/WikipediaApplet.h
@@ -47,6 +47,8 @@ public:
     bool hasHeightForWidth() const;
     qreal heightForWidth( qreal width ) const;
 
+    void windowActivationEvent(QEvent *e);
+
 public slots:
     virtual void init();
     void dataUpdated( const QString& name, const Plasma::DataEngine::Data& data );
diff --git a/src/context/engines/lyrics/LyricsEngine.cpp b/src/context/engines/lyrics/LyricsEngine.cpp
index 2b78e70..3add076 100644
--- a/src/context/engines/lyrics/LyricsEngine.cpp
+++ b/src/context/engines/lyrics/LyricsEngine.cpp
@@ -79,6 +79,9 @@ void LyricsEngine::update()
     if( m_isUpdateInProgress )
         return;
 
+    if(!m_enabled)
+        return;
+
     m_isUpdateInProgress = true;
 
     // -- get current title and artist
@@ -191,5 +194,18 @@ void LyricsEngine::lyricsMessage( const QString& key, const QString &val )
     setData( "lyrics", key, val );
 }
 
+void LyricsEngine::disable()
+{
+    m_enabled = false;
+}
+
+void LyricsEngine::enable()
+{
+    m_enabled = true;
+    update();
+}
+
+
+
 #include "LyricsEngine.moc"
 
diff --git a/src/context/engines/lyrics/LyricsEngine.h b/src/context/engines/lyrics/LyricsEngine.h
index 97cf413..825ca3d 100644
--- a/src/context/engines/lyrics/LyricsEngine.h
+++ b/src/context/engines/lyrics/LyricsEngine.h
@@ -46,6 +46,10 @@ public:
     void newSuggestions( const QVariantList &suggest );
     void lyricsMessage( const QString& key, const QString& val );
 
+public:
+    void enable();
+    void disable();
+
 protected:
     bool sourceRequestEvent( const QString& name );
 
@@ -57,6 +61,8 @@ private:
     LyricsData m_prevLyrics;
     bool m_isUpdateInProgress;
 
+    bool m_enabled;
+
     struct trackMetadata {
         QString artist;
         QString title;
diff --git a/src/context/engines/wikipedia/WikipediaEngine.cpp b/src/context/engines/wikipedia/WikipediaEngine.cpp
index 969d8fc..99339d8 100644
--- a/src/context/engines/wikipedia/WikipediaEngine.cpp
+++ b/src/context/engines/wikipedia/WikipediaEngine.cpp
@@ -47,6 +47,7 @@ public:
         , useMobileVersion( false )
         , useSSL( true )
         , dataContainer( 0 )
+        , enabled( false )
     {}
     ~WikipediaEnginePrivate() {}
 
@@ -92,6 +93,7 @@ public:
     bool useSSL;
 
     Plasma::DataContainer *dataContainer;
+    bool enabled;
 
     QSet< QUrl > urls;
 
@@ -119,6 +121,32 @@ WikipediaEnginePrivate::_dataContainerUpdated( const QString &source, const Plas
         return;
     }
 
+    Plasma::DataEngine::Data::const_iterator i = data.constBegin();
+    for (; i != data.constEnd(); ++i)
+        debug() << i.key() << ": " << i.value() << endl;
+
+    if( data.contains( QLatin1String("enabled") ) )
+    {
+        if( data.value( QLatin1String("enabled") ).toBool() )
+        {
+            enabled = true;
+
+            Meta::TrackPtr currentTrack = The::engineController()->currentTrack();
+            if( !currentTrack )
+                return;
+            else
+                _checkRequireUpdate(currentTrack);
+
+            //updateEngine();
+        }
+        else
+            enabled = false;
+
+        q->removeData( source, QLatin1String("enabled") );
+    }
+
+    debug() << "Enabled = " << enabled;
+
     if( data.contains( QLatin1String("reload") ) )
     {
         if( data.value( QLatin1String("reload") ).toBool() )
@@ -486,6 +514,8 @@ WikipediaEnginePrivate::_parseListingResult( const KUrl &url,
 void
 WikipediaEnginePrivate::_checkRequireUpdate( Meta::TrackPtr track )
 {
+    //Q_Q( WikipediaEngine );
+
     if( !track )
         return;
 
@@ -513,18 +543,9 @@ WikipediaEnginePrivate::_checkRequireUpdate( Meta::TrackPtr track )
 
     if( updateNeeded )
     {
-        m_previousTrackMetadata.clear();
-        if( track->artist() )
-            m_previousTrackMetadata.artist = track->artist()->name();
-        if( track->composer() )
-            m_previousTrackMetadata.composer = track->composer()->name();
-        if( track->album() )
-            m_previousTrackMetadata.album = track->album()->name();
-        m_previousTrackMetadata.track = track->name();
-
-        urls.clear();
         updateEngine();
     }
+
 }
 
 void
@@ -623,6 +644,14 @@ WikipediaEnginePrivate::fetchListing( const QString &title, const QString &hostL
 void
 WikipediaEnginePrivate::updateEngine()
 {
+    Q_Q( WikipediaEngine );
+
+    if(!enabled)
+    {
+        debug() << "Not enabled.";
+        return;
+    }
+
     static QMap<SelectionType, qint64> typeToFieldMap;
     if( typeToFieldMap.isEmpty() )
     {
@@ -632,12 +661,26 @@ WikipediaEnginePrivate::updateEngine()
         typeToFieldMap.insert( Track, Meta::valTitle );
     }
 
-    Q_Q( WikipediaEngine );
-
     Meta::TrackPtr currentTrack = The::engineController()->currentTrack();
     if( !currentTrack )
         return;
 
+    m_previousTrackMetadata.clear();
+    if( currentTrack->artist() )
+        m_previousTrackMetadata.artist = currentTrack->artist()->name();
+    if( currentTrack->composer() )
+        m_previousTrackMetadata.composer = currentTrack->composer()->name();
+    if( currentTrack->album() )
+        m_previousTrackMetadata.album = currentTrack->album()->name();
+    m_previousTrackMetadata.track = currentTrack->name();
+
+    q->setData( QLatin1String("wikipedia"), QLatin1String("busy"), true);
+    
+    urls.clear();
+    q->removeData( QLatin1String("wikipedia"), "message");
+    q->removeData( QLatin1String("wikipedia"), "sourceUrl");
+    q->removeData( QLatin1String("wikipedia"), "page");
+
     //TODO: Look into writing one function that can be used with different arguments for each case in this switch.
     QString tmpWikiStr;
     QString notice = i18nc( "%1 is field name such as 'Artist Name'",
-- 
1.9.1

_______________________________________________
Amarok-devel mailing list
Amarok-devel@kde.org
https://mail.kde.org/mailman/listinfo/amarok-devel

Reply via email to