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

List:       kde-commits
Subject:    [kdeplasma-addons] applets/comic: Refactoring.
From:       Matthias Fuchs <mat69 () gmx ! net>
Date:       2012-02-05 11:46:44
Message-ID: 20120205114644.0BE7EA60B9 () git ! kde ! org
[Download RAW message or body]

Git commit eef499e1f5d4e97a03e76e4deb63a5b4ac0faf80 by Matthias Fuchs.
Committed on 05/02/2012 at 11:20.
Pushed by mfuchs into branch 'master'.

Refactoring.

Creates an own ComicSaver class that takes care of saving
the comic. That way the implementation using Nepomuk if
available is hidden.

M  +1    -0    applets/comic/CMakeLists.txt
M  +3    -92   applets/comic/comic.cpp
A  +132  -0    applets/comic/comicsaver.cpp     [License: GPL (v2+)]
A  +49   -0    applets/comic/comicsaver.h     [License: GPL (v2+)]

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

diff --git a/applets/comic/CMakeLists.txt b/applets/comic/CMakeLists.txt
index 2e1ef7b..ad1ab30 100644
--- a/applets/comic/CMakeLists.txt
+++ b/applets/comic/CMakeLists.txt
@@ -18,6 +18,7 @@ set(comic_SRCS
     comicdata.cpp
     buttonbar.cpp
     comicinfo.cpp
+    comicsaver.cpp
 )
 
 kde4_add_ui_files(comic_SRCS
diff --git a/applets/comic/comic.cpp b/applets/comic/comic.cpp
index c966f5a..f11c294 100644
--- a/applets/comic/comic.cpp
+++ b/applets/comic/comic.cpp
@@ -25,6 +25,7 @@
 #include "checknewstrips.h"
 #include "comicwidgets.h"
 #include "buttonbar.h"
+#include "comicsaver.h"
 
 #include <QtCore/QTimer>
 #include <QtGui/QGraphicsLinearLayout>
@@ -38,29 +39,12 @@
 #include <KConfigDialog>
 #include <KDatePicker>
 #include <KDebug>
-#include <KFileDialog>
 #include <KInputDialog>
-#include <KIO/NetAccess>
 #include <KNotification>
 #include <kuiserverjobtracker.h>
 #include <knuminput.h>
 #include <KRun>
 #include <KStandardShortcut>
-#include <KTemporaryFile>
-
-#ifdef HAVE_NEPOMUK
-#include <Nepomuk/Resource>
-#include <Nepomuk/ResourceManager>
-#include <Nepomuk/Tag>
-#include <Nepomuk/Variant>
-#include <Nepomuk/Vocabulary/NCO>
-#include <Nepomuk/Vocabulary/NIE>
-#include <Nepomuk/Vocabulary/NFO>
-#include <Nepomuk/Vocabulary/PIMO>
-#include <nepomuk/utils.h>
-
-using namespace Nepomuk::Vocabulary;
-#endif
 
 #include <Plasma/Containment>
 #include <Plasma/Theme>
@@ -329,11 +313,6 @@ QGraphicsWidget *ComicApplet::graphicsWidget()
         layout->addItem( mRightArrow );
 
         mMainWidget->setLayout( layout );
-
-    #ifdef HAVE_NEPOMUK
-//         for manually saving the comics
-        Nepomuk::ResourceManager::instance()->init();
-    #endif
     }
 
     return mMainWidget;
@@ -940,76 +919,8 @@ void ComicApplet::updateContextMenu()
 
 void ComicApplet::slotSaveComicAs()
 {
-    KTemporaryFile tempFile;
-
-    if ( !tempFile.open() ) {
-        return;
-    }
-
-    // save image to temporary file
-    mCurrent.image().save(tempFile.fileName(), "PNG");
-
-    KUrl srcUrl( tempFile.fileName() );
-
-    QString dir = mSavingDir->getDir();
-    const QString name = mCurrent.title() + " - " + mCurrent.current() + ".png";
-    KUrl destUrl = KUrl( dir );
-    destUrl.addPath( name );
-
-    destUrl = KFileDialog::getSaveUrl( destUrl, "*.png" );
-    if ( !destUrl.isValid() ) {
-        return;
-    }
-
-    mSavingDir->setDir(destUrl.directory());
-
-#ifdef HAVE_NEPOMUK
-    bool worked = KIO::NetAccess::file_copy( srcUrl, destUrl );
-    //store additional data using Nepomuk
-    if (worked) {
-        Nepomuk::Resource res(destUrl, NFO::FileDataObject());
-
-        Nepomuk::Resource comicTopic("Comic", PIMO::Topic());
-        comicTopic.setLabel(i18n("Comic"));
-
-        if (!mCurrent.additionalText().isEmpty() ) {
-            res.setProperty(NIE::description(), mCurrent.additionalText());
-        }
-        if ((mCurrent.type() == Date) && !mCurrent.currentReadable().isEmpty() ) {
-            res.setProperty(NIE::contentCreated(), \
                QDateTime::fromString(mCurrent.currentReadable(), Qt::ISODate));
-        }
-        if (!mCurrent.title().isEmpty()) {
-            Nepomuk::Resource topic(mCurrent.title(), PIMO::Topic());
-            topic.setLabel(mCurrent.title());
-            topic.setProperty(PIMO::superTopic(), comicTopic);
-            res.addTag(topic);
-        } else {
-//             res.addTag(comicTopic);//TODO activate this, see below
-            ;
-        }
-
-        //FIXME also set the comic topic as tag, this is redundant, as topic has \
                this as super topic
-        //though at this point the gui does not manage to show the correct tags
-        res.addTag(comicTopic);
-
-        if (!mCurrent.stripTitle().isEmpty()) {
-            res.setProperty(NIE::title(), mCurrent.stripTitle());
-        }
-        if (!mCurrent.websiteUrl().isEmpty()) {
-            Nepomuk::Resource copyEvent = \
Nepomuk::Utils::createCopyEvent(mCurrent.imageUrl(), destUrl, QDateTime(), \
                mCurrent.websiteUrl());
-        }
-
-        const QStringList authors = mCurrent.author().split(',', \
                QString::SkipEmptyParts);
-        foreach (QString author, authors) {
-            author = author.trimmed();
-            Nepomuk::Resource authorRes(author, NCO::PersonContact());
-            authorRes.setProperty(NCO::fullname(), author);
-            res.addProperty(NCO::creator(), authorRes);
-        }
-    }
-#else
-    KIO::NetAccess::file_copy( srcUrl, destUrl );
-#endif
+    ComicSaver saver(mSavingDir);
+    saver.save(mCurrent);
 }
 
 bool ComicApplet::eventFilter( QObject *receiver, QEvent *event )
diff --git a/applets/comic/comicsaver.cpp b/applets/comic/comicsaver.cpp
new file mode 100644
index 0000000..cf92c76
--- /dev/null
+++ b/applets/comic/comicsaver.cpp
@@ -0,0 +1,132 @@
+/***************************************************************************
+ *   Copyright (C) 2008-2012 Matthias Fuchs <mat69@gmx.net>                *
+ *                                                                         *
+ *   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, write to the                         *
+ *   Free Software Foundation, Inc.,                                       *
+ *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA .        *
+ ***************************************************************************/
+
+#include "comicsaver.h"
+#include "comicdata.h"
+#include "comicinfo.h"
+
+#include <KFileDialog>
+#include <KIO/NetAccess>
+#include <KTemporaryFile>
+
+#ifdef HAVE_NEPOMUK
+#include <Nepomuk/Resource>
+#include <Nepomuk/ResourceManager>
+#include <Nepomuk/Tag>
+#include <Nepomuk/Variant>
+#include <Nepomuk/Vocabulary/NCO>
+#include <Nepomuk/Vocabulary/NIE>
+#include <Nepomuk/Vocabulary/NFO>
+#include <Nepomuk/Vocabulary/PIMO>
+#include <nepomuk/utils.h>
+
+using namespace Nepomuk::Vocabulary;
+#endif
+
+ComicSaver::ComicSaver(SavingDir *savingDir)
+  : mSavingDir(savingDir)
+{
+#ifdef HAVE_NEPOMUK
+    static bool isInit = false;
+    if (!isInit) {
+        isInit = true;
+        // for manually saving the comics
+        Nepomuk::ResourceManager::instance()->init();
+    }
+#endif
+}
+
+bool ComicSaver::save(const ComicData &comic)
+{
+    KTemporaryFile tempFile;
+
+    if (!tempFile.open()) {
+        return false;
+    }
+
+    // save image to temporary file
+    comic.image().save(tempFile.fileName(), "PNG");
+
+    KUrl srcUrl( tempFile.fileName() );
+
+    const QString title = comic.title();
+
+    const QString name = title + " - " + comic.current() + ".png";
+    KUrl destUrl = KUrl(mSavingDir->getDir());
+    destUrl.addPath( name );
+
+    destUrl = KFileDialog::getSaveUrl( destUrl, "*.png" );
+    if ( !destUrl.isValid() ) {
+        return false;
+    }
+
+   mSavingDir->setDir(destUrl.directory());
+
+#ifdef HAVE_NEPOMUK
+    bool worked = KIO::NetAccess::file_copy(srcUrl, destUrl);
+    //store additional data using Nepomuk
+    if (worked) {
+        Nepomuk::Resource res(destUrl, NFO::FileDataObject());
+
+        Nepomuk::Resource comicTopic("Comic", PIMO::Topic());
+        comicTopic.setLabel(i18n("Comic"));
+
+        if (!comic.additionalText().isEmpty()) {
+            res.setProperty(NIE::description(), comic.additionalText());
+        }
+        if ((comic.type() == Date) && !comic.current().isEmpty()) {
+            res.setProperty(NIE::contentCreated(), \
QDateTime::fromString(comic.current(), Qt::ISODate)); +        }
+        if (!title.isEmpty()) {
+            Nepomuk::Resource topic(title, PIMO::Topic());
+            topic.setLabel(title);
+            topic.setProperty(PIMO::superTopic(), comicTopic);
+            res.addTag(topic);
+        } else {
+//             res.addTag(comicTopic);//TODO activate this, see below
+            ;
+        }
+
+        //FIXME also set the comic topic as tag, this is redundant, as topic has \
this as super topic +        //though at this point the gui does not manage to show \
the correct tags +        res.addTag(comicTopic);
+
+        if (!comic.stripTitle().isEmpty()) {
+            res.setProperty(NIE::title(), comic.stripTitle());
+        }
+        if (!comic.websiteUrl().isEmpty()) {
+            Nepomuk::Resource copyEvent = \
Nepomuk::Utils::createCopyEvent(comic.imageUrl(), destUrl, QDateTime(), \
comic.websiteUrl()); +        }
+
+        const QStringList authors = comic.author().split(',', \
QString::SkipEmptyParts); +        foreach (const QString &author, authors) {
+            Nepomuk::Resource authorRes(author, NCO::PersonContact());
+            authorRes.setProperty(NCO::fullname(), author.trimmed());
+            res.addProperty(NCO::creator(), authorRes);
+        }
+        return true;
+    }
+
+    return false;
+#else
+    KIO::NetAccess::file_copy( srcUrl, destUrl );
+
+    return true;
+#endif
+}
diff --git a/applets/comic/comicsaver.h b/applets/comic/comicsaver.h
new file mode 100644
index 0000000..1e86721
--- /dev/null
+++ b/applets/comic/comicsaver.h
@@ -0,0 +1,49 @@
+/***************************************************************************
+ *   Copyright (C) 2008-2012 Matthias Fuchs <mat69@gmx.net>                *
+ *                                                                         *
+ *   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, write to the                         *
+ *   Free Software Foundation, Inc.,                                       *
+ *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA .        *
+ ***************************************************************************/
+
+#ifndef COMIC_SAVER_H
+#define COMIC_SAVER_H
+
+class ComicData;
+class SavingDir;
+
+/**
+ * ComicSaver takes care of saving a comic strip to a user chosen
+ * destination.
+ * Further if available Nepomuk is used to store the title, author
+ * etc.
+ */
+class ComicSaver
+{
+    public:
+        ComicSaver(SavingDir *savingDir);
+
+        /**
+         * Asks the user for a destination to save the specified
+         * comic to. If possible writes it to that destination.
+         * @param comic the comic to save
+         * @return true if saving worked, false if there was a problem
+         */
+        bool save(const ComicData &comic);
+
+    private:
+        SavingDir *mSavingDir;
+};
+
+#endif


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

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