hi,

This patch enables making comic providers with scripts (supported by kross).

Things to do:
- Get providers from dataengine (and fix script icon paths)
- Make plasma package and add GHNS for comics
- it would be nice to have a 'scale to content size' option

Dilbert with python would look like this:

#!/bin/env python
# -*- coding: utf-8 -*-

import comic
import re

def init():
    comic.comicAuthor = 'Scott Adams'
    comic.firstStripDate = '1994-01-01'
    url = 'http://dilbert.com/fast/%s/' % comic.requestedDate()

    infos = {
        'User-Agent': 'Mozilla/5.0 (compatible; Konqueror/3.5; Linux) 
KHTML/3.5.6 (like Gecko)',
        'Accept': 'text/html, image/jpeg, image/png, text/*, image/*, */*',
        'Accept-Encoding': 'deflate',
        'Accept-Charset': 'iso-8859-15, utf-8;q=0.5, *;q=0.5',
        'Accept-Language': 'en',
        'Host': 'dilbert.com',
        'Connection': 'Keep-Alive'
    }
    comic.requestPage(url, comic.Page, infos)

def identifierType():
    return comic.DateIdentifier

def identifier():
    return 'dilbert_x:%s' % comic.requestedDate()

def websiteUrl():
    return 'http://dilbert.com/strips/comic/%s/' % comic.requestedDate()

def pageRetrieved(id, data):
    if id == comic.Page:
        a = re.findall('<img 
src="(/dyn/str_strip/[0-9/]+/[0-9]+\.strip\.print\.gif)"', data);
        if len(a) > 0:
            url = 'http://dilbert.com/%s' % a[0]
        else:
            url = 'http://dilbert.com/img/v1/404.gif'
        comic.requestPage(url, comic.Image)

Should I continue on this road?

Petri

Index: comicproviderkross.cpp
===================================================================
--- comicproviderkross.cpp	(revision 0)
+++ comicproviderkross.cpp	(revision 0)
@@ -0,0 +1,100 @@
+/*
+ *   Copyright (C) 2008 Petri Damstén <[EMAIL PROTECTED]>
+ *
+ *   This program is free software; you can redistribute it and/or modify
+ *   it under the terms of the GNU Library General Public License version 2 as
+ *   published by the Free Software Foundation
+ *
+ *   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 Library General Public
+ *   License along with this program; if not, write to the
+ *   Free Software Foundation, Inc.,
+ *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+#include "comicproviderkross.h"
+
+COMICPROVIDER_EXPORT_PLUGIN(ComicProviderKross, "ComicProviderKross", "")
+
+ComicProviderKross::ComicProviderKross(QObject *parent, const QVariantList &args)
+: ComicProvider(parent, args)
+, m_wrapper(this)
+{
+}
+
+ComicProviderKross::~ComicProviderKross()
+{
+}
+
+ComicProvider::IdentifierType ComicProviderKross::identifierType() const
+{
+    return m_wrapper.identifierType();
+}
+
+KUrl ComicProviderKross::websiteUrl() const
+{
+    return m_wrapper.websiteUrl();
+}
+
+QImage ComicProviderKross::image() const
+{
+    return m_wrapper.image();
+}
+
+QString ComicProviderKross::identifier() const
+{
+    return m_wrapper.identifier();
+}
+
+QString ComicProviderKross::nextIdentifier() const
+{
+    QString result = m_wrapper.nextIdentifier();
+    if (!m_wrapper.functionCalled()) {
+        return ComicProvider::nextIdentifier();
+    }
+    return result;
+}
+
+QString ComicProviderKross::previousIdentifier() const
+{
+    QString result = m_wrapper.previousIdentifier();
+    if (!m_wrapper.functionCalled()) {
+        return ComicProvider::previousIdentifier();
+    }
+    return result;
+}
+
+QString ComicProviderKross::firstStripIdentifier() const
+{
+    QString result = m_wrapper.firstStripIdentifier();
+    if (!m_wrapper.functionCalled()) {
+        return ComicProvider::firstStripIdentifier();
+    }
+    return result;
+}
+
+QString ComicProviderKross::stripTitle() const
+{
+    return m_wrapper.stripTitle();
+}
+
+QString ComicProviderKross::additionalText() const
+{
+    return m_wrapper.additionalText();
+}
+
+void ComicProviderKross::pageRetrieved(int id, const QByteArray &data)
+{
+    m_wrapper.pageRetrieved(id, data);
+}
+
+void ComicProviderKross::pageError(int id, const QString &message)
+{
+    m_wrapper.pageError(id, message);
+}
+
+#include "comicproviderkross.moc"
Index: comicproviderwrapper.cpp
===================================================================
--- comicproviderwrapper.cpp	(revision 0)
+++ comicproviderwrapper.cpp	(revision 0)
@@ -0,0 +1,217 @@
+/*
+ *   Copyright (C) 2008 Petri Damstén <[EMAIL PROTECTED]>
+ *
+ *   This program is free software; you can redistribute it and/or modify
+ *   it under the terms of the GNU Library General Public License version 2 as
+ *   published by the Free Software Foundation
+ *
+ *   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 Library General Public
+ *   License along with this program; if not, write to the
+ *   Free Software Foundation, Inc.,
+ *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+#include "comicproviderwrapper.h"
+#include "comicproviderkross.h"
+#include "comicprovider.h"
+
+#include <QTimer>
+#include <KDebug>
+#include <KStandardDirs>
+#include <kross/core/action.h>
+
+ComicProviderWrapper::ComicProviderWrapper(ComicProviderKross *parent)
+: QObject(parent)
+, m_action(0)
+, m_provider(parent)
+, m_useDefaultImageHandler(true)
+{
+    QTimer::singleShot(0, this, SLOT(init()));
+}
+
+ComicProviderWrapper::~ComicProviderWrapper()
+{
+}
+
+void ComicProviderWrapper::init()
+{
+    // TODO: get this from comic package?
+    QString mainscript = QString("plasma/comics/%1/code/main*").arg(m_provider->comicIdentifier());
+    QStringList scriptFiles = KGlobal::dirs()->findAllResources("data", mainscript);
+    if (scriptFiles.size() < 1) {
+        return;
+    }
+    m_action = new Kross::Action(parent(), m_provider->comicIdentifier());
+    if (m_action) {
+        m_action->addObject(this, "comic");
+        m_action->setFile(scriptFiles[0]);
+        m_action->trigger();
+        m_functions = m_action->functionNames();
+
+        callFunction("init");
+    }
+}
+
+ComicProvider::IdentifierType ComicProviderWrapper::identifierType()
+{
+    return (ComicProvider::IdentifierType)callFunction("identifierType").toInt();
+}
+
+KUrl ComicProviderWrapper::websiteUrl()
+{
+    return KUrl(callFunction("websiteUrl").toString());
+}
+
+QImage ComicProviderWrapper::image()
+{
+    if (m_useDefaultImageHandler) {
+        return m_image;
+    }
+    return QImage::fromData(callFunction("image").toByteArray());
+}
+
+QString ComicProviderWrapper::identifier()
+{
+    return callFunction("identifier").toString();
+}
+
+QString ComicProviderWrapper::nextIdentifier()
+{
+    return callFunction("nextIdentifier").toString();
+}
+
+QString ComicProviderWrapper::previousIdentifier()
+{
+    return callFunction("previousIdentifier").toString();
+}
+
+QString ComicProviderWrapper::firstStripIdentifier()
+{
+    return callFunction("firstStripIdentifier").toString();
+}
+
+QString ComicProviderWrapper::stripTitle()
+{
+    return callFunction("stripTitle").toString();
+}
+
+QString ComicProviderWrapper::additionalText()
+{
+    return callFunction("additionalText").toString();
+}
+
+void ComicProviderWrapper::pageRetrieved(int id, const QByteArray &data)
+{
+    if (m_useDefaultImageHandler && id == Image) {
+        m_image = QImage::fromData(data);
+        emit m_provider->finished(m_provider);
+    } else {
+        callFunction("pageRetrieved", QVariantList() << id << data);
+    }
+}
+
+void ComicProviderWrapper::pageError(int id, const QString &message)
+{
+    callFunction("pageError", QVariantList() << id << message);
+    if (!functionCalled()) {
+        emit m_provider->error(m_provider);
+    }
+}
+
+QString ComicProviderWrapper::firstStripDate()
+{
+    return m_provider->firstStripDate().toString(Qt::ISODate);
+}
+
+void ComicProviderWrapper::setFirstStripDate(const QString &date)
+{
+    m_provider->setFirstStripDate(QDate::fromString(date, Qt::ISODate));
+}
+
+int ComicProviderWrapper::firstStripNumber()
+{
+    return m_provider->firstStripNumber();
+}
+
+void ComicProviderWrapper::setFirstStripNumber(int number)
+{
+    m_provider->setFirstStripNumber(number);
+}
+
+QString ComicProviderWrapper::comicAuthor()
+{
+    return m_provider->comicAuthor();
+}
+
+void ComicProviderWrapper::setComicAuthor(const QString &author)
+{
+    m_provider->setComicAuthor(author);
+}
+
+void ComicProviderWrapper::finished()
+{
+    emit m_provider->finished(m_provider);
+}
+
+void ComicProviderWrapper::error()
+{
+    emit m_provider->error(m_provider);
+}
+
+QString ComicProviderWrapper::requestedDate()
+{
+    return m_provider->requestedDate().toString(Qt::ISODate);
+}
+
+int ComicProviderWrapper::requestedNumber()
+{
+    return m_provider->requestedNumber();
+}
+
+QString ComicProviderWrapper::requestedString()
+{
+    return m_provider->requestedString();
+}
+
+void ComicProviderWrapper::requestPage(const QString &url, int id, const QVariantMap &infos)
+{
+    QMap<QString, QString> map;
+
+    foreach (const QString& key, infos.keys()) {
+        map[key] = infos[key].toString();
+    }
+    m_provider->requestPage(KUrl(url), id, map);
+}
+
+bool ComicProviderWrapper::functionCalled()
+{
+    return m_funcFound;
+}
+
+QVariant ComicProviderWrapper::callFunction(const QString &name, const QVariantList &args)
+{
+    if (m_action) {
+        m_funcFound = m_functions.contains(name);
+        if (m_funcFound) {
+            return m_action->callFunction(name, args);
+        }
+    }
+    return QVariant();
+}
+
+void ComicProviderWrapper::setUseDefaultImageHandler(bool useDefaultImageHandler)
+{
+    m_useDefaultImageHandler = useDefaultImageHandler;
+}
+
+bool ComicProviderWrapper::useDefaultImageHandler()
+{
+    return m_useDefaultImageHandler;
+}
+
+#include "comicproviderwrapper.moc"
Index: comicprovider.h
===================================================================
--- comicprovider.h	(revision 864584)
+++ comicprovider.h	(working copy)
@@ -129,6 +129,11 @@
          */
         bool isCurrent() const;
 
+        /**
+         * Returns the identifier for the comic
+         */
+        QString comicIdentifier() const;
+
     Q_SIGNALS:
         /**
          * This signal is emitted whenever a request has been finished
Index: comicprovider.cpp
===================================================================
--- comicprovider.cpp	(revision 864584)
+++ comicprovider.cpp	(working copy)
@@ -18,16 +18,19 @@
 
 #include "comicprovider.h"
 
+#include <KDebug>
+#include <KPluginInfo>
 #include <KIO/Job>
 #include <KIO/StoredTransferJob>
 
 class ComicProvider::Private
 {
     public:
-        Private( ComicProvider *parent )
+        Private( KService::Ptr service, ComicProvider *parent )
             : mParent( parent ),
               mIsCurrent( false ),
-              mFirstStripNumber( 1 )
+              mFirstStripNumber( 1 ),
+              mcomicDescription(service)
         {
         }
 
@@ -50,14 +53,15 @@
         QDate mFirstStripDate;
         int mRequestedNumber;
         int mFirstStripNumber;
+        KPluginInfo mcomicDescription;
 };
 
 ComicProvider::ComicProvider( QObject *parent, const QVariantList &args )
-    : QObject( parent ), d( new Private( this ) )
+    : QObject( parent ), d( new Private( KService::serviceByStorageId(args[ 2 ].toString()), this ) )
 {
-    Q_ASSERT( args.count() == 2 );
+    Q_ASSERT( args.count() == 3 );
+    const QString type = args[ 0 ].toString();
 
-    const QString type = args[ 0 ].toString();
     if ( type == "Date" )
         d->mRequestedDate = args[ 1 ].toDate();
     else if ( type == "Number" )
@@ -197,4 +201,12 @@
 {
 }
 
+QString ComicProvider::comicIdentifier() const
+{
+    if (!d->mcomicDescription.isValid()) {
+        return QString();
+    }
+    return d->mcomicDescription.property( "X-KDE-PlasmaComicProvider-Identifier" ).toString();
+}
+
 #include "comicprovider.moc"
Index: comicproviderwrapper.h
===================================================================
--- comicproviderwrapper.h	(revision 0)
+++ comicproviderwrapper.h	(revision 0)
@@ -0,0 +1,102 @@
+/*
+ *   Copyright (C) 2008 Petri Damstén <[EMAIL PROTECTED]>
+ *
+ *   This program is free software; you can redistribute it and/or modify
+ *   it under the terms of the GNU Library General Public License version 2 as
+ *   published by the Free Software Foundation
+ *
+ *   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 Library General Public
+ *   License along with this program; if not, write to the
+ *   Free Software Foundation, Inc.,
+ *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+#ifndef COMICPROVIDERWRAPPER_H
+#define COMICPROVIDERWRAPPER_H
+
+#include "comicprovider.h"
+
+#include <KUrl>
+#include <QImage>
+
+namespace Kross {
+    class Action;
+}
+class ComicProviderKross;
+
+class ComicProviderWrapper : public QObject
+{
+        Q_OBJECT
+        Q_ENUMS(IdentifierType)
+        Q_ENUMS(RequestType)
+        Q_PROPERTY(QString firstStripDate READ firstStripDate WRITE setFirstStripDate)
+        Q_PROPERTY(int firstStripNumber READ firstStripNumber WRITE setFirstStripNumber)
+        Q_PROPERTY(QString comicAuthor READ comicAuthor WRITE setComicAuthor)
+        Q_PROPERTY(bool useDefaultImageHandler READ useDefaultImageHandler WRITE setUseDefaultImageHandler)
+    public:
+        enum RequestType {
+            Page = 0,
+            Image,
+            User
+        };
+        enum IdentifierType {
+            DateIdentifier   = ComicProvider::DateIdentifier,
+            NumberIdentifier = ComicProvider::NumberIdentifier,
+            StringIdentifier = ComicProvider::StringIdentifier
+        };
+
+        ComicProviderWrapper(ComicProviderKross *parent);
+        ~ComicProviderWrapper();
+
+        ComicProvider::IdentifierType identifierType();
+        KUrl websiteUrl();
+        QImage image();
+        QString identifier();
+        QString nextIdentifier();
+        QString previousIdentifier();
+        QString firstStripIdentifier();
+        QString stripTitle();
+        QString additionalText();
+        void pageRetrieved(int id, const QByteArray &data);
+        void pageError(int id, const QString &message);
+
+        QString firstStripDate();
+        void setFirstStripDate(const QString &date);
+        int firstStripNumber();
+        void setFirstStripNumber(int number);
+        QString comicAuthor();
+        void setComicAuthor(const QString &author);
+        void setUseDefaultImageHandler(bool useDefaultImageHandler);
+        bool useDefaultImageHandler();
+
+        bool functionCalled();
+
+    public slots:
+        void finished();
+        void error();
+
+        QString requestedDate();
+        int requestedNumber();
+        QString requestedString();
+        void requestPage(const QString &url, int id, const QVariantMap &infos = QVariantMap());
+
+        void init();
+
+    protected:
+        QVariant callFunction(const QString &name, const QVariantList &args = QVariantList());
+
+    private:
+        Kross::Action *m_action;
+        ComicProviderKross *m_provider;
+        QStringList m_functions;
+        bool m_funcFound;
+        bool m_useDefaultImageHandler;
+        QImage m_image;
+};
+
+#endif
Index: CMakeLists.txt
===================================================================
--- CMakeLists.txt	(revision 864584)
+++ CMakeLists.txt	(working copy)
@@ -17,8 +17,8 @@
 )
 
 kde4_add_library( plasmacomicprovidercore SHARED ${comic_provider_core_SRCS} )
-target_link_libraries( plasmacomicprovidercore ${KDE4_KDEUI_LIBS} ${KDE4_KIO_LIBS} )
-set_target_properties( 
+target_link_libraries( plasmacomicprovidercore ${KDE4_KDEUI_LIBS} ${KDE4_KIO_LIBS} ${KDE4_KROSSCORE_LIBS} )
+set_target_properties(
    plasmacomicprovidercore PROPERTIES VERSION 1.0.0 SOVERSION 1
    ${KDE4_DISABLE_PROPERTY_}LINK_INTERFACE_LIBRARIES "${KDE4_KDECORE_LIBS}"
 )
@@ -26,7 +26,17 @@
 install( TARGETS plasmacomicprovidercore ${INSTALL_TARGETS_DEFAULT_ARGS} )
 install( FILES plasma_comicprovider.desktop DESTINATION ${SERVICETYPES_INSTALL_DIR} )
 
+########### kross ###############
 
+set( plasma_comic_krossprovider_SRCS
+  comicproviderkross.cpp
+  comicproviderwrapper.cpp
+)
+
+kde4_add_plugin( plasma_comic_krossprovider ${plasma_comic_krossprovider_SRCS} )
+target_link_libraries( plasma_comic_krossprovider plasmacomicprovidercore ${KDE4_KDECORE_LIBS} ${QT_QTGUI_LIBRARY} ${KDE4_KIO_LIBS} ${KDE4_KROSSCORE_LIBS} )
+install( TARGETS plasma_comic_krossprovider DESTINATION ${PLUGIN_INSTALL_DIR} )
+
 ########### plugins ###############
 
 set( comic_dilbert_provider_SRCS
Index: comic.cpp
===================================================================
--- comic.cpp	(revision 864584)
+++ comic.cpp	(working copy)
@@ -86,6 +86,7 @@
     } else if ( type == "Number" ) {
         args << "Number" << parts[ 1 ].toInt();
     }
+    args << service->storageId();
 
     provider = service->createInstance<ComicProvider>( this, args );
     if ( !provider )
@@ -95,7 +96,6 @@
 
     connect( provider, SIGNAL( finished( ComicProvider* ) ), this, SLOT( finished( ComicProvider* ) ) );
     connect( provider, SIGNAL( error( ComicProvider* ) ), this, SLOT( error( ComicProvider* ) ) );
-
     return true;
 }
 
@@ -154,7 +154,6 @@
 
         CachedProvider::storeInCache( provider->identifier(), provider->image(), info );
     }
-
     provider->deleteLater();
 }
 
Index: comicproviderkross.h
===================================================================
--- comicproviderkross.h	(revision 0)
+++ comicproviderkross.h	(revision 0)
@@ -0,0 +1,54 @@
+/*
+ *   Copyright (C) 2008 Petri Damstén <[EMAIL PROTECTED]>
+ *
+ *   This program is free software; you can redistribute it and/or modify
+ *   it under the terms of the GNU Library General Public License version 2 as
+ *   published by the Free Software Foundation
+ *
+ *   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 Library General Public
+ *   License along with this program; if not, write to the
+ *   Free Software Foundation, Inc.,
+ *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+#ifndef COMICPROVIDERKROSS_H
+#define COMICPROVIDERKROSS_H
+
+#include "comicprovider.h"
+#include "comicproviderwrapper.h"
+
+#include <KUrl>
+#include <QImage>
+
+class ComicProviderKross : public ComicProvider
+{
+        friend class ComicProviderWrapper;
+        Q_OBJECT
+    public:
+        ComicProviderKross(QObject *parent, const QVariantList &args);
+        virtual ~ComicProviderKross();
+
+        virtual IdentifierType identifierType() const;
+        virtual KUrl websiteUrl() const;
+        virtual QImage image() const;
+        virtual QString identifier() const;
+        virtual QString nextIdentifier() const;
+        virtual QString previousIdentifier() const;
+        virtual QString firstStripIdentifier() const;
+        virtual QString stripTitle() const;
+        virtual QString additionalText() const;
+
+    protected:
+        virtual void pageRetrieved(int id, const QByteArray &data);
+        virtual void pageError(int id, const QString &message);
+
+    private:
+        mutable ComicProviderWrapper m_wrapper;
+};
+
+#endif

Attachment: signature.asc
Description: This is a digitally signed message part.

_______________________________________________
Plasma-devel mailing list
Plasma-devel@kde.org
https://mail.kde.org/mailman/listinfo/plasma-devel

Reply via email to