extragear/multimedia/amarok/src

Leo Franchi lfranchi at gmail.com
Mon Sep 8 23:03:15 CEST 2008


SVN commit 858867 by lfranchi:

KIO-aware web access for all scripts! yay! now thiago won't behead me ;)
NOTE: this will require all scripts that want web access to be slightly modified. follow the lyrc.com.ar script or the lyricwiki script, or just talk to me.

CCMAIL: amarok-devel at kde.org


 M  +1 -3      scriptengine/AmarokLyricsScript.cpp  
 M  +128 -20   scriptengine/AmarokNetworkScript.cpp  
 M  +51 -10    scriptengine/AmarokNetworkScript.h  
 M  +13 -14    scripts/lyrics_lyrc/main.js  
 M  +3 -10     scripts/lyrics_lyricwiki/main.js  


--- trunk/extragear/multimedia/amarok/src/scriptengine/AmarokLyricsScript.cpp #858866:858867
@@ -49,7 +49,7 @@
     if( !track )
         return;
     //debug() << "got lyrics: " << lyrics << " and track: " << track;
-    debug() << lyrics;
+//    debug() << lyrics;
     LyricsManager::self()->lyricsResult( lyrics, false );
 }
 
@@ -60,8 +60,6 @@
     if( !track )
         return;
     //debug() << "got lyrics: " << lyrics << " and track: " << track;
-    // TODO how should we cache html lyrics properly?
-    //track->setCachedLyrics( lyrics );
     LyricsManager::self()->lyricsResultHtml( lyrics, false );
 }
 
--- trunk/extragear/multimedia/amarok/src/scriptengine/AmarokNetworkScript.cpp #858866:858867
@@ -1,38 +1,146 @@
 /******************************************************************************
- * Copyright (C) 2008 Peter ZHOU <peterzhoulei at gmail.com>                     *
- *                                                                            *
- * 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/>.      *
- ******************************************************************************/
+* Copyright (C) 2008 Peter ZHOU <peterzhoulei at gmail.com>                     *
+*                                                                            *
+* 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 "AmarokNetworkScript.h"
 
 #include "App.h"
+#include "Debug.h"
 
 #include <QtScript>
 
-namespace AmarokScript
+AmarokDownloadHelper *AmarokDownloadHelper::s_instance = 0;
+
+AmarokNetworkScript::AmarokNetworkScript( QScriptEngine* ScriptEngine )
+: QObject( kapp )
 {
-    AmarokNetworkScript::AmarokNetworkScript( QScriptEngine* ScriptEngine )
-    : QObject( kapp )
+    Q_UNUSED( ScriptEngine );
+}
+
+AmarokNetworkScript::~AmarokNetworkScript()
+{
+}
+
+// Class Downloader
+
+Downloader::Downloader( QScriptEngine* engine )
+    : QObject( kapp ),
+    m_scriptEngine( engine )
+{
+    DEBUG_BLOCK
+    engine->setDefaultPrototype( qMetaTypeId<Downloader*>(), QScriptValue() );
+    const QScriptValue ctor = engine->newFunction( Downloader_prototype_ctor );
+    engine->globalObject().setProperty( "Downloader", ctor );
+
+}
+
+Downloader::~Downloader()
+{
+}
+
+QScriptValue
+Downloader::Downloader_prototype_ctor( QScriptContext* context, QScriptEngine* engine )
+{ // from QtScript API docs
+    DEBUG_BLOCK
+    QScriptValue object;
+    if( context->isCalledAsConstructor() )
     {
-        Q_UNUSED( ScriptEngine );
+        object = context->thisObject();
+    } else {
+        object = engine->newObject();
+        object.setPrototype( context->callee().property("prototype") );
     }
 
-    AmarokNetworkScript::~AmarokNetworkScript()
+    if( context->argumentCount() != 2 )
     {
+        debug() << "ERROR! Constructor not called with exactly 1 argument:" << context->argumentCount();
+        return object;
+    } else if( !context->argument( 0 ).isString() && !context->argument( 1 ).isFunction() )
+    {
+        debug() << "ERROR! Constructor not called with a string and function!";
+        return object;
     }
+    QString url = context->argument( 0 ).toString();
 
+    debug() << "got url:" << url;
+    // start download, and connect to it
+    KIO::Job* job = KIO::storedGet( url , KIO::NoReload, KIO::HideProgressInfo );
+
+    AmarokDownloadHelper::instance()->newDownload( job, engine, context->argument( 1 ) );
+    // connect to a local slot to extract the qstring
+    //qScriptConnect( job, SIGNAL( result( KJob* ) ), object, fetchResult( job ) );
+    connect( job, SIGNAL( result( KJob* ) ), AmarokDownloadHelper::instance(), SLOT( result( KJob* ) ) );
+    return object;
 }
 
+///////
+// Class AmarokDownloadHelper
+//////
+
+AmarokDownloadHelper::AmarokDownloadHelper()
+{
+    s_instance = this;
+}
+
+void
+AmarokDownloadHelper::newDownload( KJob* download, QScriptEngine* engine, QScriptValue obj )
+{
+    m_jobs[ download ] = obj ;
+    m_engines[ download ] = engine;
+}
+
+void
+AmarokDownloadHelper::result( KJob* job )
+{
+    DEBUG_BLOCK
+
+    KIO::StoredTransferJob* const storedJob = static_cast< KIO::StoredTransferJob* >( job );
+    QString data = QString( storedJob->data() );
+    //debug() << "got fetch result: " << data;
+    
+    // now send the data to the associated script object
+    QScriptValue obj = m_jobs[ job ];
+    QScriptEngine* engine = m_engines[ job ];
+    if( !obj.isFunction() )
+    {
+        debug() << "script object is valid but not a function!!";
+        return;
+    } else if( !engine )
+    {
+        debug() << "stored script engine is not valid!";
+        return;
+    }
+    QScriptValueList args;
+    args << QScriptValue( engine, data );
+    obj.call( obj, args );
+
+    m_jobs.remove( job );
+    m_engines.remove( job );
+}
+
+AmarokDownloadHelper*
+AmarokDownloadHelper::instance()
+{
+    if( !s_instance )
+    {
+        s_instance = new AmarokDownloadHelper();
+        return s_instance;
+    } else
+        return s_instance;
+}
+
+
 #include "AmarokNetworkScript.moc"
--- trunk/extragear/multimedia/amarok/src/scriptengine/AmarokNetworkScript.h #858866:858867
@@ -18,23 +18,64 @@
 #ifndef AMAROK_NETWORK_SCRIPT_H
 #define AMAROK_NETWORK_SCRIPT_H
 
+#include <kio/job.h> // KIO::Job
+
 #include <QObject>
 #include <QtScript>
 
-namespace AmarokScript
+class AmarokDownloadHelper;
+
+class AmarokNetworkScript : public QObject
 {
+    Q_OBJECT
 
-    class AmarokNetworkScript : public QObject
-    {
-        Q_OBJECT
+    public:
+        AmarokNetworkScript( QScriptEngine* ScriptEngine );
+        ~AmarokNetworkScript();
 
-        public:
-            AmarokNetworkScript( QScriptEngine* ScriptEngine );
-            ~AmarokNetworkScript();
 
-        public slots:
+};
 
-    };
-}
 
+class Downloader : public QObject
+{
+    Q_OBJECT
+
+    public:
+        Downloader( QScriptEngine* scriptEngine );
+        ~Downloader();
+
+    private:
+        static QScriptValue Downloader_prototype_ctor( QScriptContext* context, QScriptEngine* engine );
+
+        QScriptEngine* m_scriptEngine;
+
+};
+
+Q_DECLARE_METATYPE( Downloader* )
+
+// this internal class manages multiple downloads from a script.
+// keeps track of each unique download
+class AmarokDownloadHelper : public QObject
+{
+    Q_OBJECT
+
+    static AmarokDownloadHelper *s_instance;
+    
+public:
+    AmarokDownloadHelper();
+
+    static AmarokDownloadHelper *instance();
+    
+    // called by the wrapper class to register a new download
+    void newDownload( KJob* download, QScriptEngine* engine, QScriptValue obj );
+
+public slots:
+    void result( KJob* job );
+    
+private:
+    QHash< KJob*, QScriptEngine* > m_engines;
+    QHash< KJob*, QScriptValue > m_jobs;
+};
+
 #endif
--- trunk/extragear/multimedia/amarok/src/scripts/lyrics_lyrc/main.js #858866:858867
@@ -120,7 +120,8 @@
     print( "got result from lyrics fetch:" + reply ); 
     try
     {
-        var lyrics = Amarok.Lyrics.toUtf8( reply.readAll(), "ISO 8859-1" );
+        //var lyrics = Amarok.Lyrics.toUtf8( reply.readAll(), "ISO 8859-1" );
+        lyrics = reply;
     } catch( err )
     {
         print( "error converting lyrics: " + err );
@@ -163,23 +164,22 @@
     suggestions_xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><suggestions page_url=\"{provider_url}\" >{suggestions}</suggestions>"
     suggestions_body="<suggestion artist=\"{artist}\" title=\"{title}\" url=\"{url}\" />"
 
-    var connection = new QNetworkAccessManager();
     try{
         if( url == "" )
         {
-            var path = "http://lyrc.com.ar/en/tema1en.php";
-            encodedTitle = Amarok.Lyrics.fromUtf8( title, "ISO 8859-1" );
-            encodedTitleKey = Amarok.Lyrics.fromUtf8( "songname", "ISO 8859-1" );
-            encodedArtist = Amarok.Lyrics.fromUtf8( artist, "ISO 8859-1" )
-            encodedArtistKey = Amarok.Lyrics.fromUtf8( "artist", "ISO 8859-1" );
-            url = new QUrl( path );
-            url.addEncodedQueryItem( encodedArtistKey, encodedArtist );
-            url.addEncodedQueryItem( encodedTitleKey, encodedTitle );
+            path = "http://lyrc.com.ar/en/tema1en.php?songname=" + title + "&artist=" + artist;
+            //encodedTitle = Amarok.Lyrics.fromUtf8( title, "ISO 8859-1" );
+            //encodedTitleKey = Amarok.Lyrics.fromUtf8( "songname", "ISO 8859-1" );
+            //encodedArtist = Amarok.Lyrics.fromUtf8( artist, "ISO 8859-1" )
+            //encodedArtistKey = Amarok.Lyrics.fromUtf8( "artist", "ISO 8859-1" );
+            //url = new QUrl( path );
+            //url.addEncodedQueryItem( encodedArtistKey, encodedArtist );
+            //url.addEncodedQueryItem( encodedTitleKey, encodedTitle );
             //print( "fetching from: " + url.toString() );
         } else
         {   // we are told to fetch a specific url
-            var path = "http://lyrc.com.ar/en/" + url;
-            url = new QUrl( path );
+            path = "http://lyrc.com.ar/en/" + url;
+            //url = new QUrl( path );
             //print( "fetching from given url: " + url.toString() );
         }
     }
@@ -190,8 +190,7 @@
     // TODO for now, ignoring proxy settings
     //page_url = QUrl.toPercentEncoding( page_url )
 
-    connection.finished.connect( lyricsFetchResult );
-    connection.get( new QNetworkRequest( url ) );
+    a = new Downloader( path, lyricsFetchResult );
 }
 
 Amarok.Lyrics.fetchLyrics.connect( fetchLyrics );
\ No newline at end of file
--- trunk/extragear/multimedia/amarok/src/scripts/lyrics_lyricwiki/main.js #858866:858867
@@ -26,14 +26,11 @@
 Importer.loadQtBinding("qt.gui");
 Importer.loadQtBinding("qt.network");
 
-function onFinished( reply )
+function onFinished( dat )
 {
     try
     {
         //Amarok.alert("reply.finished was emitted!");
-        response = reply.readAll();
-        ts = new QTextStream( response, QIODevice.ReadOnly );
-        dat = ts.readAll();
         dat = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><lyric artist=\"artist name\" title=\"song title\" page_url=\"http://lyricwiki.org\">" + dat + "</lyric>"
         //print( "got result: " + dat );
         Amarok.Lyrics.showLyricsHtml(dat);
@@ -47,14 +44,10 @@
 {
     try
     {
-        var connection = new QNetworkAccessManager();
-        var url = new QUrl("http://lyricwiki.org/api.php?func=getSong&artist=" + artist +"&song=" + title +"&fmt=html");
+        var url ="http://lyricwiki.org/api.php?func=getSong&artist=" + artist +"&song=" + title +"&fmt=html";
 
-        connection.finished.connect( onFinished );
-
         //Amarok.alert( "fetching: " + (new QUrl( url )).toString() );
-        connection.get( new QNetworkRequest( new QUrl( url ) ) );
-
+        new Downloader( url, onFinished );
     } catch( err )
     {
         print( "error!: " + err );


More information about the Amarok-devel mailing list