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

List:       kde-commits
Subject:    [kdeplasma-addons] applets/comic: Uses own class to retrieve and set the directory to save to.
From:       Matthias Fuchs <mat69 () gmx ! net>
Date:       2012-02-01 19:03:03
Message-ID: 20120201190303.B8759A60A6 () git ! kde ! org
[Download RAW message or body]

Git commit f0b229d859b226eef33bc8dfd0182c08650d56e6 by Matthias Fuchs.
Committed on 01/02/2012 at 19:47.
Pushed by mfuchs into branch 'master'.

Uses own class to retrieve and set the directory to save to.

That way accessing the configuration is hidden as well as the different
checks for retrieving the directory.

M  +1    -0    applets/comic/CMakeLists.txt
M  +11   -27   applets/comic/comic.cpp
M  +2    -2    applets/comic/comic.h
A  +108  -0    applets/comic/comicinfo.cpp     [License: GPL (v2+)]
M  +38   -1    applets/comic/comicinfo.h

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

diff --git a/applets/comic/CMakeLists.txt b/applets/comic/CMakeLists.txt
index d9d4b32..306eda4 100644
--- a/applets/comic/CMakeLists.txt
+++ b/applets/comic/CMakeLists.txt
@@ -16,6 +16,7 @@ set(comic_SRCS
     comicarchivedialog.cpp
     checknewstrips.cpp
     buttonbar.cpp
+    comicinfo.cpp
 )
 
 kde4_add_ui_files(comic_SRCS
diff --git a/applets/comic/comic.cpp b/applets/comic/comic.cpp
index 5eed534..a32d172 100644
--- a/applets/comic/comic.cpp
+++ b/applets/comic/comic.cpp
@@ -39,7 +39,6 @@
 #include <KDatePicker>
 #include <KDebug>
 #include <KFileDialog>
-#include <KGlobalSettings>
 #include <KInputDialog>
 #include <KIO/NetAccess>
 #include <KNotification>
@@ -140,7 +139,8 @@ ComicApplet::ComicApplet( QObject *parent, const QVariantList \
&args )  mActionShop( 0 ),
       mEngine( 0 ),
       mTabAdded( false ),
-      mButtonBar(0)
+      mButtonBar(0),
+      mSavingDir(0)
 {
     setHasConfigurationInterface( true );
     resize( 600, 250 );
@@ -158,6 +158,8 @@ void ComicApplet::init()
     Plasma::ToolTipManager::self()->registerWidget( this );
 
     globalComicUpdater->init( globalConfig() );
+    mSavingDir = new SavingDir(config());
+
     configChanged();
     
     buttonBar();
@@ -246,6 +248,7 @@ void ComicApplet::init()
 ComicApplet::~ComicApplet()
 {
     delete mFullViewWidget;
+    delete mSavingDir;
 }
 
 QGraphicsWidget *ComicApplet::graphicsWidget()
@@ -716,7 +719,6 @@ void ComicApplet::configChanged()
     mLastSize = mMaxSize;
 
     mTabView = cg.readEntry( "tabView", ShowText | ShowIcon );
-    mSavingDir = cg.readEntry( "savingDir", QString() );
 
     globalComicUpdater->load();
 }
@@ -734,7 +736,6 @@ void ComicApplet::saveConfig()
     cg.writeEntry( "middleClick", mMiddleClick );
     cg.writeEntry( "tabIdentifier", mTabIdentifier );
     cg.writeEntry( "tabView", mTabView );
-    cg.writeEntry( "savingDir", mSavingDir );
     cg.writeEntry( "checkNewComicStripsIntervall", mCheckNewComicStripsIntervall );
 
     globalComicUpdater->save();
@@ -903,16 +904,16 @@ void ComicApplet::updateSize()
 
 void ComicApplet::createComicBook()
 {
-    ComicArchiveDialog *dialog = new ComicArchiveDialog( mComicIdentifier, \
mComicTitle, mComicType, mCurrentIdentifierSuffix, mFirstIdentifierSuffix, \
                getSavingDir() );
-    dialog->setAttribute( Qt::WA_DeleteOnClose );//to have destroyed emitted upon \
closing +    ComicArchiveDialog *dialog = new ComicArchiveDialog(mComicIdentifier, \
mComicTitle, mComicType, +                                                        \
mCurrentIdentifierSuffix, mSavingDir->getDir()); +    \
                dialog->setAttribute(Qt::WA_DeleteOnClose);//to have destroyed \
                emitted upon closing
     connect( dialog, SIGNAL(archive(int,KUrl,QString,QString)), this, \
SLOT(slotArchive(int,KUrl,QString,QString)) );  dialog->show();
 }
 
 void ComicApplet::slotArchive( int archiveType, const KUrl &dest, const QString \
&fromIdentifier, const QString &toIdentifier )  {
-    mSavingDir = dest.directory();
-    saveConfig();
+    mSavingDir->setDir(dest.directory());
 
     kDebug() << "Archiving:" << mComicIdentifier <<  archiveType << dest << \
                fromIdentifier << toIdentifier;
     ComicArchiveJob *job = new ComicArchiveJob( dest, mEngine, static_cast< \
ComicArchiveJob::ArchiveType >( archiveType ), mComicType,  mComicIdentifier, this ); \
@@ -1010,22 +1011,6 @@ void ComicApplet::updateContextMenu()  }
 }
 
-QString ComicApplet::getSavingDir() const
-{
-    QString dir = mSavingDir;
-    if ( dir.isEmpty() ) {
-        dir = KGlobalSettings::picturesPath();
-    }
-    if ( dir.isEmpty() ) {
-        dir = KGlobalSettings::downloadPath();
-    }
-    if ( dir.isEmpty() ) {
-        dir = QDir::homePath();
-    }
-
-    return dir;
-}
-
 void ComicApplet::slotSaveComicAs()
 {
     KTemporaryFile tempFile;
@@ -1039,7 +1024,7 @@ void ComicApplet::slotSaveComicAs()
 
     KUrl srcUrl( tempFile.fileName() );
 
-    QString dir = getSavingDir();
+    QString dir = mSavingDir->getDir();
     const QString name = mComicTitle + " - " + mCurrentIdentifierSuffix + ".png";
     KUrl destUrl = KUrl( dir );
     destUrl.addPath( name );
@@ -1049,8 +1034,7 @@ void ComicApplet::slotSaveComicAs()
         return;
     }
 
-    mSavingDir = destUrl.directory();
-    saveConfig();
+    mSavingDir->setDir(destUrl.directory());
 
 #ifdef HAVE_NEPOMUK
     bool worked = KIO::NetAccess::file_copy( srcUrl, destUrl );
diff --git a/applets/comic/comic.h b/applets/comic/comic.h
index a30ba50..9abe2a4 100644
--- a/applets/comic/comic.h
+++ b/applets/comic/comic.h
@@ -46,6 +46,7 @@ class QAction;
 class QGraphicsLayout;
 class QSortFilterProxyModel;
 class QTimer;
+class SavingDir;
 
 class ComicApplet : public Plasma::PopupApplet
 {
@@ -107,7 +108,6 @@ class ComicApplet : public Plasma::PopupApplet
         void saveConfig();
         bool isInPanel() const;
         void setTabBarVisible( bool isVisible );//HACK what was in 4.4 does not seem \
                to work anymore, so this was added
-        QString getSavingDir() const;
 
     private:
         static const int CACHE_LIMIT;
@@ -133,7 +133,6 @@ class ComicApplet : public Plasma::PopupApplet
         QString mCurrentIdentifierSuffix;
         QString mStoredIdentifierSuffix;
         QString mIdentifierError;
-        QString mSavingDir;
         QString mOldSource;
         ConfigWidget *mConfigWidget;
         bool mDifferentComic;
@@ -187,6 +186,7 @@ class ComicApplet : public Plasma::PopupApplet
         };
         int mTabView;
         ButtonBar *mButtonBar;
+        SavingDir *mSavingDir;
 };
 
 K_EXPORT_PLASMA_APPLET( comic, ComicApplet )
diff --git a/applets/comic/comicinfo.cpp b/applets/comic/comicinfo.cpp
new file mode 100644
index 0000000..1709c25
--- /dev/null
+++ b/applets/comic/comicinfo.cpp
@@ -0,0 +1,108 @@
+/***************************************************************************
+ *   Copyright (C) 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 "comicinfo.h"
+
+#include <KConfigGroup>
+#include <KGlobalSettings>
+
+#include <QtCore/QDir>
+
+class SavingDir::SavingDirPrivate
+{
+    public:
+        SavingDirPrivate(const KConfigGroup &cfg);
+
+        void init();
+
+        QString getDir() const;
+        void setDir(const QString &dir);
+        SavingDir *instance();
+
+    private:
+        void load();
+        void save();
+
+    private:
+        KConfigGroup mCfg;
+        QString mDir;
+};
+
+
+SavingDir::SavingDirPrivate::SavingDirPrivate(const KConfigGroup &cfg)
+  : mCfg(cfg)
+{
+}
+
+
+void SavingDir::SavingDirPrivate::init()
+{
+    load();
+    save();
+}
+
+QString SavingDir::SavingDirPrivate::getDir() const
+{
+    return mDir;
+}
+
+void SavingDir::SavingDirPrivate::setDir(const QString &dir)
+{
+    mDir = dir;
+    save();
+}
+
+void SavingDir::SavingDirPrivate::load()
+{
+    mDir = mCfg.readEntry("savingDir", QString());
+    if (mDir.isEmpty()) {
+        mDir = KGlobalSettings::picturesPath();
+    }
+    if (mDir.isEmpty()) {
+        mDir = KGlobalSettings::downloadPath();
+    }
+    if (mDir.isEmpty()) {
+        mDir = QDir::homePath();
+    }
+}
+
+void SavingDir::SavingDirPrivate::save()
+{
+     mCfg.writeEntry("savingDir", mDir);
+}
+
+SavingDir::SavingDir(const KConfigGroup &cfg)
+  : d(new SavingDirPrivate(cfg))
+{
+    d->init();
+}
+
+SavingDir::~SavingDir()
+{
+}
+
+QString SavingDir::getDir() const
+{
+    return d->getDir();
+}
+
+void SavingDir::setDir(const QString &dir)
+{
+    d->setDir(dir);
+}
diff --git a/applets/comic/comicinfo.h b/applets/comic/comicinfo.h
index bd9d2c3..99cc53e 100644
--- a/applets/comic/comicinfo.h
+++ b/applets/comic/comicinfo.h
@@ -1,5 +1,5 @@
 /***************************************************************************
- *   Copyright (C) 2011 Matthias Fuchs <mat69@gmx.net>                     *
+ *   Copyright (C) 2011-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  *
@@ -20,10 +20,47 @@
 #ifndef COMIC_TYPE_H
 #define COMIC_TYPE_H
 
+class KConfigGroup;
+class QString;
+
 enum IdentifierType {
     Date = 0,
     Number,
     String
 };
 
+/**
+ * Provides access (read/write) to the directory that should be used
+ * whenever the user is presented with a file selection dialog.
+ */
+class SavingDir
+{
+    public:
+        /**
+         * @param config the config that should be used to retrieve
+         * the saving directory and to store it to in case of changes
+         */
+        SavingDir(const KConfigGroup &config);
+
+        ~SavingDir();
+
+        /**
+         * @return the directory to be displayed to the user
+         */
+        QString getDir() const;
+
+        /**
+         * Set the directory that should be displayed to the user first
+         * when choosing a destination. Automatically writes the directory
+         * to the config, if one was specified in init.
+         * @param dir the directory to display the user first
+         * @see init
+         */
+        void setDir(const QString &dir);
+
+    private:
+        class SavingDirPrivate;
+        SavingDirPrivate *d;
+};
+
 #endif


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

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