[Kde-games-devel] [PATCH] extract statics from KCardDialog

Andreas Pakulat apaku at gmx.de
Sat Jan 12 13:34:04 CET 2008


On 12.01.08 12:57:44, Luciano Montanaro wrote:
> Il Saturday 12 January 2008 12:17:44 Andreas Pakulat ha scritto:
> > Hi,
> >
> > the attached patch moves the static methods from KCardDialog into a
> > separate namespace, which IMHO makes more sense as retrieving
> > information about card decks is not bound to the dialog.
> 
> You forgot the patch! ;)

Damn, I really need to learn to not hit "y" so fast. Ok, third try to
get the attachment sent.
 
Andreas

-- 
You will hear good news from one you thought unfriendly to you.
-------------- next part --------------
diff --git a/libkdegames/CMakeLists.txt b/libkdegames/CMakeLists.txt
index 0cbf8b3..fb18d17 100644
--- a/libkdegames/CMakeLists.txt
+++ b/libkdegames/CMakeLists.txt
@@ -53,6 +53,7 @@ set(kdegames_LIB_SRCS
     kgame/dialogs/kgamedialog.cpp
     kgame/dialogs/kgamedialogconfig.cpp
     kgame/dialogs/kgameerrordialog.cpp
+    carddeckinfo.cpp
     kcarddialog.cpp
     kchat.cpp
     kchatbase.cpp
diff --git a/libkdegames/carddeckinfo.cpp b/libkdegames/carddeckinfo.cpp
new file mode 100644
index 0000000..7b0dcbf
--- /dev/null
+++ b/libkdegames/carddeckinfo.cpp
@@ -0,0 +1,409 @@
+/*
+    This file is part of the KDE games library
+    Copyright 2008 Andreas Pakulat <apaku at gmx.de>
+
+    This library 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 library 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
+    Library General Public License for more details.
+
+    You should have received a copy of the GNU Library General Public License
+    along with this library; see the file COPYING.LIB.  If not, write to
+    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+    Boston, MA 02110-1301, USA.
+*/
+
+#include <QFileInfo>
+#include <QDir>
+
+#include <klocale.h>
+#include <kstandarddirs.h>
+#include <krandom.h>
+#include <kdebug.h>
+#include <kconfiggroup.h>
+#include <kglobal.h>
+
+#include "carddeckinfo.h"
+#include "carddeckinfo_p.h"
+
+/**
+ * Local static information.
+ */
+class KCardInfoStatic
+{
+public:
+    KCardInfoStatic()
+    {
+        KGlobal::dirs()->addResourceType( "cards", "data", "carddecks/" );
+        KGlobal::locale()->insertCatalog( "libkdegames" );
+        readBacks();
+        readFronts();
+    }
+    ~KCardInfoStatic()
+    {
+    }
+    
+    
+    // Translate back side
+    QString findi18nBack( QString& name )
+    {
+        if ( name.isNull() ) return name;
+    
+        QMap<QString, KCardInfo> temp = svgBackInfo;
+        temp.unite( pngBackInfo );
+    
+        QMapIterator<QString, KCardInfo> it( temp );
+        while ( it.hasNext() )
+        {
+            it.next();
+            KCardInfo v = it.value();
+            if ( v.noi18Name == name ) return v.name;
+        }
+        kError() << "No translation for back card " << name << "found";
+        return name;
+    }
+
+    void readFronts()
+    {
+        // Empty data
+        pngFrontInfo.clear();
+        svgFrontInfo.clear();
+
+        QStringList svg;
+        // Add SVG card sets
+        svg = KGlobal::dirs()->findAllResources( "cards", "svg*/index.desktop", KStandardDirs::NoDuplicates );
+        QStringList list = svg + KGlobal::dirs()->findAllResources( "cards", "card*/index.desktop", KStandardDirs::NoDuplicates );
+
+        if ( list.isEmpty() ) return;
+
+        for ( QStringList::ConstIterator it = list.begin(); it != list.end(); ++it )
+        {
+            KConfig cfg( *it, KConfig::SimpleConfig );
+            KConfigGroup cfgcg( &cfg, "KDE Backdeck" );
+            QString path = ( *it ).left(( *it ).lastIndexOf( '/' ) + 1 );
+            Q_ASSERT( path[path.length() - 1] == '/' );
+            QPixmap pixmap( path + cfgcg.readEntry( "Preview", "12c.png" ) );
+
+            if ( pixmap.isNull() ) continue;
+
+            QString idx  = cfgcg.readEntryUntranslated( "Name", i18n( "unnamed" ) );
+            QString name = cfgcg.readEntry( "Name", i18n( "unnamed" ) );
+            KCardInfo info;
+            info.name         = name;
+            info.noi18Name    = idx;
+            info.comment      = cfgcg.readEntry( "Comment", i18n( "KDE card deck" ) );
+            info.preview      = pixmap;
+            info.path         = path;
+            info.back         = cfgcg.readEntry( "Back", QString() );
+            // The back name is read UNTRANSLATED...we need to find the right name for it now
+            info.back         = findi18nBack( info.back );
+            // if (!info.back.isNull()) kDebug() << "FOUND BACK " << info.back;
+            info.size         = cfgcg.readEntry( "BackSize", QSizeF( pixmap.size() ) );
+            info.isDefault    = cfgcg.readEntry( "Default", false );
+
+            QString svg    = cfgcg.readEntry( "SVG", QString() );
+            if ( !svg.isNull() )
+            {
+                QFileInfo svgInfo( QDir( path ), svg );
+                info.svgfile = svgInfo.filePath();
+                svgFrontInfo[name] = info;
+            }
+            else
+            {
+                info.svgfile = QString();
+                pngFrontInfo[name] = info;
+            }
+        }
+
+    }
+
+
+    void readBacks()
+    {
+        // Empty data
+        svgBackInfo.clear();
+        pngBackInfo.clear();
+    
+        QStringList list = KGlobal::dirs()->findAllResources( "cards", "decks/*.desktop", KStandardDirs::NoDuplicates );
+        if ( list.isEmpty() ) return;
+    
+        for ( QStringList::ConstIterator it = list.begin(); it != list.end(); ++it )
+        {
+            KConfig cfg( *it, KConfig::SimpleConfig );
+            QString path = ( *it ).left(( *it ).lastIndexOf( '/' ) + 1 );
+            Q_ASSERT( path[path.length() - 1] == '/' );
+            QPixmap pixmap( getBackFileNameFromIndex( *it ) );
+            if ( pixmap.isNull() ) continue;
+            //pixmap = pixmap.scaledToWidth(72, Qt::SmoothTransformation);
+            QPixmap previewPixmap = pixmap.scaled( QSize( 32, 43 ), Qt::IgnoreAspectRatio, Qt::SmoothTransformation );
+    
+            KConfigGroup cfgcg( &cfg, "KDE Fronts" );
+            QString idx  = cfgcg.readEntryUntranslated( "Name", i18n( "unnamed" ) );
+            QString name = cfgcg.readEntry( "Name", i18n( "unnamed" ) );
+            KCardInfo info;
+            info.name         = name;
+            info.noi18Name    = idx;
+            info.path         = getBackFileNameFromIndex( *it );
+            info.comment      = cfgcg.readEntry( "Comment", i18n( "KDE card deck" ) );
+            info.preview      = pixmap;
+            info.size         = cfgcg.readEntry( "Size", QSizeF( pixmap.size() ) );
+            info.isDefault    = cfgcg.readEntry( "Default", false );
+    
+            QString svg    = cfgcg.readEntry( "SVG", QString() );
+            if ( !svg.isNull() )
+            {
+                QFileInfo svgInfo( QDir( path ), svg );
+                info.svgfile = svgInfo.filePath();
+                svgBackInfo[name] = info;
+            }
+            else
+            {
+                info.svgfile = QString();
+                pngBackInfo[name] = info;
+            }
+        }
+    
+    }
+
+    QString getBackFileNameFromIndex( const QString& desktop )
+    {
+        QString entry = desktop.left( desktop.length() - strlen( ".desktop" ) );
+        if ( KStandardDirs::exists( entry + QString::fromLatin1( ".png" ) ) )
+            return entry + QString::fromLatin1( ".png" );
+
+        // rather theoretical
+        if ( KStandardDirs::exists( entry + QString::fromLatin1( ".xpm" ) ) )
+            return entry + QString::fromLatin1( ".xpm" );
+        return QString();
+    }
+
+
+    /** The card front sides for PNG decks.
+     */
+    QMap<QString, KCardInfo> pngFrontInfo;
+
+    /** The card front sides for SVG decks.
+     */
+    QMap<QString, KCardInfo> svgFrontInfo;
+
+    /** The card back sides for PNG decks.
+     */
+    QMap<QString, KCardInfo> pngBackInfo;
+
+    /** The card back sides for SVG decks.
+     */
+    QMap<QString, KCardInfo> svgBackInfo;
+
+    /** The default front side name.
+     */
+    QString defaultFront;
+
+    /** The default back side name.
+     */
+    QString defaultBack;
+};
+
+K_GLOBAL_STATIC( KCardInfoStatic, deckinfoStatic )
+
+namespace CardDeckInfo
+{
+
+// Retrieve default card set name
+QString defaultFrontName( bool pAllowSVG, bool pAllowPNG )
+{
+    QString noDefault;
+    // Count filtered cards
+    QMap<QString, KCardInfo> temp;
+    if ( pAllowSVG )
+    {
+        temp.unite( deckinfoStatic->svgFrontInfo );
+    }
+    if ( pAllowPNG )
+    {
+        temp.unite( deckinfoStatic->pngFrontInfo );
+    }
+    QMapIterator<QString, KCardInfo> it( temp );
+    while ( it.hasNext() )
+    {
+        it.next();
+        KCardInfo v = it.value();
+        // Filter
+        if ( v.isDefault ) return v.name;
+        // Collect any deck if no default is stored
+        noDefault = v.name;
+    }
+    if ( noDefault.isNull() ) kError() << "Could not find default card name";
+    return noDefault;
+}
+
+
+// Retrieve default deck name
+QString defaultBackName( bool pAllowSVG, bool pAllowPNG )
+{
+    QString noDefault;
+    QMap<QString, KCardInfo> temp;
+    if ( pAllowSVG )
+    {
+        temp.unite( deckinfoStatic->svgBackInfo );
+    }
+    if ( pAllowPNG )
+    {
+        temp.unite( deckinfoStatic->pngBackInfo );
+    }
+
+    QMapIterator<QString, KCardInfo> it( temp );
+    while ( it.hasNext() )
+    {
+        it.next();
+        KCardInfo v = it.value();
+        // Filter
+        if ( v.isDefault ) return v.name;
+        // Collect any deck if no default is stored
+        noDefault = v.name;
+    }
+    if ( noDefault.isNull() ) kError() << "Could not find default deck name";
+    return noDefault;
+}
+
+
+// Retrieve a random card name
+QString randomFrontName( bool pAllowSVG, bool pAllowPNG )
+{
+    // Collect matching items
+    QStringList list;
+    if ( pAllowSVG )
+    {
+        list += deckinfoStatic->svgFrontInfo.keys();
+    }
+    if ( pAllowPNG )
+    {
+        list += deckinfoStatic->pngFrontInfo.keys();
+    }
+
+    // Draw random one
+    int d = KRandom::random() % list.count();
+    return list.at( d );
+}
+
+
+// Retrieve a random deck name
+QString randomBackName( bool pAllowSVG, bool pAllowPNG )
+{
+    // Collect matching items
+    QStringList list;
+
+    if ( pAllowSVG )
+    {
+        list += deckinfoStatic->svgBackInfo.keys();
+    }
+    if ( pAllowPNG )
+    {
+        list += deckinfoStatic->pngBackInfo.keys();
+    }
+
+    // Draw random one
+    int d = KRandom::random() % list.count();
+    return list.at( d );
+}
+
+
+// Retrieve the PNG filename for a back side from its index.desktop filename
+QString getBackFileNameFromIndex( const QString &desktop )
+{
+    QString entry = desktop.left( desktop.length() - strlen( ".desktop" ) );
+    if ( KStandardDirs::exists( entry + QString::fromLatin1( ".png" ) ) )
+        return entry + QString::fromLatin1( ".png" );
+
+    // rather theoretical
+    if ( KStandardDirs::exists( entry + QString::fromLatin1( ".xpm" ) ) )
+        return entry + QString::fromLatin1( ".xpm" );
+    return QString();
+}
+
+
+
+// Retrieve the SVG file belonging to the given card back deck.
+QString backSVGFilePath( const QString& name )
+{
+    if ( !deckinfoStatic->svgBackInfo.contains( name ) ) return QString();
+    KCardInfo v = deckinfoStatic->svgBackInfo.value( name );
+    return v.svgfile;
+}
+
+
+// Retrieve the SVG file belonging to the given card fronts.
+QString frontSVGFilePath( const QString& name )
+{
+    if ( !deckinfoStatic->svgFrontInfo.contains( name ) ) return QString();
+    KCardInfo v = deckinfoStatic->svgFrontInfo.value( name );
+    return v.svgfile;
+}
+
+
+// Retrieve the PNG file belonging to the given card back deck.
+QString backFilename( const QString& name )
+{
+    if ( !deckinfoStatic->pngBackInfo.contains( name ) ) return QString();
+    KCardInfo v = deckinfoStatic->pngBackInfo.value( name );
+    return v.path;
+}
+
+
+// Retrieve the directory belonging to the given card fronts.
+QString frontDir( const QString& name )
+{
+    if ( !deckinfoStatic->pngFrontInfo.contains( name ) ) return QString();
+    KCardInfo v = deckinfoStatic->pngFrontInfo.value( name );
+    return v.path;
+}
+
+
+// Check whether a card set is SVG
+bool isSVGFront( const QString& name )
+{
+    if ( !deckinfoStatic->svgFrontInfo.contains( name ) ) return false;
+    else return true;
+}
+
+
+// Check whether a card deck is SVG
+bool isSVGBack( const QString& name )
+{
+    if ( !deckinfoStatic->svgBackInfo.contains( name ) ) return false;
+    else return false;
+}
+
+QStringList frontNames()
+{
+    return ( deckinfoStatic->svgFrontInfo.keys() + deckinfoStatic->pngFrontInfo.keys() );
+}
+
+QStringList backNames()
+{
+    return ( deckinfoStatic->svgBackInfo.keys() + deckinfoStatic->pngBackInfo.keys() );
+}
+
+KCardInfo frontInfo( const QString& name )
+{
+    if ( deckinfoStatic->svgFrontInfo.contains( name ) )
+        return deckinfoStatic->svgFrontInfo.value( name );
+    if ( deckinfoStatic->pngFrontInfo.contains( name ) )
+        return deckinfoStatic->pngFrontInfo.value( name );
+    return KCardInfo();
+}
+
+KCardInfo backInfo( const QString& name )
+{
+    if ( deckinfoStatic->svgFrontInfo.contains( name ) )
+        return deckinfoStatic->svgBackInfo.value( name );
+    if ( deckinfoStatic->pngBackInfo.contains( name ) )
+        return deckinfoStatic->pngBackInfo.value( name );
+    return KCardInfo();
+}
+
+}
+
diff --git a/libkdegames/carddeckinfo.h b/libkdegames/carddeckinfo.h
new file mode 100644
index 0000000..d6ee4b1
--- /dev/null
+++ b/libkdegames/carddeckinfo.h
@@ -0,0 +1,119 @@
+/*
+    This file is part of the KDE games library
+
+    This library 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 library 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
+    Library General Public License for more details.
+
+    You should have received a copy of the GNU Library General Public License
+    along with this library; see the file COPYING.LIB.  If not, write to
+    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+    Boston, MA 02110-1301, USA.
+*/
+#ifndef __CARDDECKINFO_H_
+#define __CARDDECKINFO_H_
+
+#include <QtCore/QString>
+#include <QtCore/QStringList>
+#include <libkdegames_export.h>
+
+/**
+ * Namespace to supply access to card deck information, such as a list of all
+ * card decks as well as allowing to access the actual files to render the
+ * decks.
+ */
+namespace CardDeckInfo
+{
+   /** Retrieve the SVG file belonging to the given card deck (back side). 
+    * @param name The name of the back deck.
+    * @return The file name and path to the SVG file or QString() if not available. 
+    */
+   KDEGAMES_EXPORT QString backSVGFilePath(const QString& name);
+
+   /** Retrieve the SVG file belonging to the given card set (front side). 
+    * The SVG IDs used for the card back is '1_club' for Ace of clubs, '10_spade' for
+    * 10 of spades, 'queen_heart' for Queen of Hearts, '2_diamond' for 2 of diamonds and
+    * so on.
+    * @param name The name of the card set.
+    * @return The file name and path to the SVG file or QString() if not available. 
+    */
+   KDEGAMES_EXPORT QString frontSVGFilePath(const QString& name);
+
+   /** Check whether the card set is SVG or not.
+    * @param name The name of the card set.
+    * @return True if SVG data is available.
+    */
+   KDEGAMES_EXPORT bool isSVGBack(const QString& name);
+
+   /** Check whether the card back deck contains also an SVG file.
+    * @param name The name of the card deck.
+    * @return True if SVG data is available.
+    */
+   KDEGAMES_EXPORT bool isSVGFront(const QString& name);
+   
+   /** Retrieve the name of the default card set (front side).
+    * @param pAllowSVG  Allow selection of scalable cards sets.
+    * @param pAllowPNG  Allow selection of fixed size cards sets.
+    * @return The default card set name.
+    */
+   KDEGAMES_EXPORT QString defaultFrontName(bool pAllowSVG = true, bool pAllowPNG = true);
+   
+   /** Retrieve the name of the default card deck (back side).
+    * @param pAllowSVG  Allow selection of scalable cards sets.
+    * @param pAllowPNG  Allow selection of fixed size cards sets.
+    * @return The default card deck name.
+    */
+   KDEGAMES_EXPORT QString defaultBackName(bool pAllowSVG = true, bool pAllowPNG = true);
+   
+   /** Retrieve a random card set (front side).
+    * @param pAllowSVG  Allow selection of scalable cards sets.
+    * @param pAllowPNG  Allow selection of fixed size cards sets.
+    * @return A radnom card set name.
+    */
+   KDEGAMES_EXPORT QString randomFrontName(bool pAllowSVG = true, bool pAllowPNG = true);
+ 
+   /** Retrieve a random card deck (back side).
+    * @param pAllowSVG  Allow selection of scalable cards sets.
+    * @param pAllowPNG  Allow selection of fixed size cards sets.
+    * @return A radnom card deck name.
+    */
+   KDEGAMES_EXPORT QString randomBackName(bool pAllowSVG = true, bool pAllowPNG = true);
+
+   /**
+    * Retrieve the directory where the card front sides are stored. The cards are
+    * named 1.png, 2.png, etc. For SVG card decks use @ref cardSVGFilePath.
+    * @param name The name of the card set.
+    * @return The directory.
+    */
+   KDEGAMES_EXPORT QString frontDir(const QString& name);
+
+   /**
+    * Retrieve the filename of the card back side. 
+    * For SVG  decks use @ref deckSVGFilePath.
+    * @param name The name of the card deck.
+    * @return The filename.
+    */
+   KDEGAMES_EXPORT QString backFilename(const QString& name);
+
+   /**
+    * retrieve a list of all installed backsides
+    * @returns a list of backside names, which can be 
+    * used as input to the other functions.
+    */
+   KDEGAMES_EXPORT QStringList backNames();
+
+   /**
+    * retrieve a list of all installed frontsides
+    * @return a list of frontside names, which can be
+    * used as input to the other functions.
+    */
+   KDEGAMES_EXPORT QStringList frontNames();
+
+}
+
+#endif
diff --git a/libkdegames/carddeckinfo_p.h b/libkdegames/carddeckinfo_p.h
new file mode 100644
index 0000000..c4c0ec0
--- /dev/null
+++ b/libkdegames/carddeckinfo_p.h
@@ -0,0 +1,80 @@
+/*
+    This file is part of the KDE games library
+    Copyright 2008 Andreas Pakulat <apaku at gmx.de>
+
+    This library 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 library 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
+    Library General Public License for more details.
+
+    You should have received a copy of the GNU Library General Public License
+    along with this library; see the file COPYING.LIB.  If not, write to
+    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+    Boston, MA 02110-1301, USA.
+*/
+
+#ifndef __CARDDECKINFO_P_H_
+#define __CARDDECKINFO_P_H_
+
+#include <QMap>
+#include <QString>
+#include <QPixmap>
+#include <QSize>
+
+
+/**
+ * Stores the information for one card front or back side.
+ */
+class KCardInfo
+{
+  public:
+   /** The translated name.
+    */
+   QString name;
+
+   /** The untranslated name.
+    */
+   QString noi18Name;
+
+   /** The comment (author and description).
+    */
+   QString comment;
+   
+   /** The full path information.
+    */
+   QString path;
+
+   /** The translated name of the back side.
+    */
+   QString back;
+
+   /** The preview image.
+    */
+   QPixmap preview;
+
+   /** The full filename of the SVG file.
+    */
+   QString svgfile;
+   
+   /** The default size.
+   */
+   QSizeF size;
+   
+   /** Is this a default deck or set.
+   */
+   bool isDefault;
+};
+
+namespace CardDeckInfo
+{
+
+KCardInfo frontInfo( const QString& name );
+KCardInfo backInfo( const QString& name );
+
+}
+
+#endif
diff --git a/libkdegames/kcarddialog.cpp b/libkdegames/kcarddialog.cpp
index 542014d..142a579 100644
--- a/libkdegames/kcarddialog.cpp
+++ b/libkdegames/kcarddialog.cpp
@@ -29,6 +29,9 @@
 #include <krandom.h>
 #include <kdebug.h>
 
+#include "carddeckinfo.h"
+#include "carddeckinfo_p.h"
+
 // KConfig entries
 #define CONF_GROUP  QString::fromLatin1("KCardDialog")
 #define CONF_LOCKING QString::fromLatin1("Locking")
@@ -40,49 +43,6 @@
 #define CONF_DECK QString::fromLatin1("Deckname")
 
 /**
- * Stores the information for one card front or back side.
- */
-class KCardInfo
-{
-  public:
-   /** The translated name.
-    */
-   QString name;
-
-   /** The untranslated name.
-    */
-   QString noi18Name;
-
-   /** The comment (author and description).
-    */
-   QString comment;
-   
-   /** The full path information.
-    */
-   QString path;
-
-   /** The translated name of the back side.
-    */
-   QString back;
-
-   /** The preview image.
-    */
-   QPixmap preview;
-
-   /** The full filename of the SVG file.
-    */
-   QString svgfile;
-   
-   /** The default size.
-   */
-   QSizeF size;
-   
-   /** Is this a default deck or set.
-   */
-   bool isDefault;
-};
-
-/**
  * Local information of the dialog.
  */
 class KCardDialogPrivate
@@ -144,53 +104,10 @@ class KCardDialogPrivate
     bool useLocking;
 };
 
-/**
- * Local static information.
- */
-class KCardDialogStatic
-{
-  public:
-    /** The card front sides.
-     */
-    QMap<QString, KCardInfo> cardInfo;
-
-    /** The card back sides.
-     */
-    QMap<QString, KCardInfo> deckInfo;
-
-    /** The default front side name.
-     */
-    QString defaultCard;
-    
-    /** The default back side name.
-     */
-    QString defaultDeck;
-
-    /** Filter a fiven card front/back depending on its scalable
-      * or non-scalable properties.
-      * @param v The card info structure.
-      * @param useSVGOnly Are only SVG cards shown
-      * @param usePNGOnly Are only PNG cards shown
-      * @return True if the card should bve discarded.
-      */
-    bool filterOutCard(const KCardInfo& v, bool useSVGOnly, bool usePNGOnly)
-    {
-      if (usePNGOnly && !v.svgfile.isNull()) return true;
-      if (useSVGOnly && v.svgfile.isNull()) return true;
-      return false;
-    }
-};
-
-// Store local static information.
-static KCardDialogStatic ds;
-
-
 // Create the dialog
 KCardDialog::KCardDialog(QWidget *parent, bool pAllowSVG, bool pAllowPNG, bool pLock, QString defFront, QString defBack)
            : KDialog( parent ), d( new KCardDialogPrivate )
 {
-  KCardDialog::init();
-
   // Copy parameter
   d->useLocking = pLock;
   d->allowPNG = pAllowPNG;
@@ -206,8 +123,6 @@ KCardDialog::KCardDialog(QWidget *parent, bool pAllowSVG, bool pAllowPNG, bool p
 KCardDialog::KCardDialog(KConfigGroup& group, QWidget* parent)
            : KDialog( parent ), d( new KCardDialogPrivate )
 {
-  KCardDialog::init();
-
   d->useLocking  = group.readEntry(CONF_LOCKING, true);
   d->allowPNG    = group.readEntry(CONF_ALLOW_FIXED_CARDS, true);
   d->allowSVG    = group.readEntry(CONF_ALLOW_SCALED_CARDS, true);
@@ -278,8 +193,8 @@ void KCardDialog::setupGUI()
   ui->backList->setEnabled(!d->useLocking);
 
   // Set lists and preview
-   if (d->currentCard.isNull()) d->currentCard = defaultCardName(d->allowSVG, d->allowPNG);
-   if (d->currentDeck.isNull()) d->currentDeck = defaultDeckName(d->allowSVG, d->allowPNG);
+   if (d->currentCard.isNull()) d->currentCard = CardDeckInfo::defaultFrontName(d->allowSVG, d->allowPNG);
+   if (d->currentDeck.isNull()) d->currentDeck = CardDeckInfo::defaultBackName(d->allowSVG, d->allowPNG);
   insertCardIcons();
   insertDeckIcons();
   updateFront(d->currentCard);
@@ -310,27 +225,6 @@ KCardDialog::~KCardDialog()
 }
 
 
-// Perform static initialization
-void KCardDialog::init()
-{
-  static bool _inited = false;
-  if (_inited) return;
-  _inited = true;
-
-  KGlobal::dirs()->addResourceType("cards", "data", "carddecks/");
-  KGlobal::locale()->insertCatalog("libkdegames");
-  reset();
-}
-
-
-// Reset the static information. Reread cards.
-void KCardDialog::reset()
-{
-  // Important to read backs first
-  readBacks();
-  readFronts();
-}
-
 
 // Retrieve card set and deck from dialog.
 int KCardDialog::getCardDeck(QString &pFrontName,
@@ -342,14 +236,11 @@ int KCardDialog::getCardDeck(QString &pFrontName,
                              bool pRandom 
                              )
 {
-  KCardDialog::init();
-
-
   // If random cards we need no dialog (KDE 3.x compatibility)
   if (pRandom)
   {
-    pFrontName = randomCardName();
-    pBackName  = randomDeckName();
+    pFrontName = CardDeckInfo::randomFrontName();
+    pBackName  = CardDeckInfo::randomBackName();
     return QDialog::Accepted;
   }
 
@@ -359,99 +250,26 @@ int KCardDialog::getCardDeck(QString &pFrontName,
   int result=dlg.exec();
   if (result==QDialog::Accepted)
   {
-    pFrontName = dlg.cardName();
-    pBackName  = dlg.deckName();
+    pFrontName = dlg.frontName();
+    pBackName  = dlg.backName();
   }
   return result;
 }
 
 
 // Retrieve selected deck name
-QString KCardDialog::deckName() const
+QString KCardDialog::backName() const
 {
   return d->currentDeck; 
 }
 
 
 // Retrieve selected card name
-QString KCardDialog::cardName() const
+QString KCardDialog::frontName() const
 { 
   return d->currentCard; 
 }
 
-
-// Read card front side data
-void KCardDialog::readFronts()
-{
-    // Empty data
-    ds.cardInfo.clear();
-
-    QStringList svg;
-    // Add SVG card sets
-    svg = KGlobal::dirs()->findAllResources("cards", "svg*/index.desktop", KStandardDirs::NoDuplicates);
-    QStringList list = svg+KGlobal::dirs()->findAllResources("cards", "card*/index.desktop", KStandardDirs::NoDuplicates);
-
-    if (list.isEmpty()) return;
-
-    for (QStringList::ConstIterator it = list.begin(); it != list.end(); ++it)
-    {
-        KConfig cfg(*it, KConfig::SimpleConfig);
-        KConfigGroup cfgcg(&cfg, "KDE Backdeck");
-        QString path = (*it).left((*it).lastIndexOf('/') + 1);
-        Q_ASSERT(path[path.length() - 1] == '/');
-        QPixmap pixmap(path + cfgcg.readEntry("Preview", "12c.png"));
-
-        if (pixmap.isNull()) continue;
-
-        QString idx  = cfgcg.readEntryUntranslated("Name", i18n("unnamed"));
-        QString name = cfgcg.readEntry("Name", i18n("unnamed"));
-        KCardInfo info;
-        info.name         = name;
-        info.noi18Name    = idx;
-        info.comment      = cfgcg.readEntry("Comment",i18n("KDE card deck"));
-        info.preview      = pixmap; 
-        info.path         = path;
-        info.back         = cfgcg.readEntry("Back",QString());
-        // The back name is read UNTRANSLATED...we need to find the right name for it now
-        info.back         = findi18nBack(info.back);
-        // if (!info.back.isNull()) kDebug() << "FOUND BACK " << info.back;
-        info.size         = cfgcg.readEntry("BackSize", QSizeF(pixmap.size()));
-        info.isDefault    = cfgcg.readEntry("Default", false);
-
-        QString svg    = cfgcg.readEntry("SVG", QString());
-        if (!svg.isNull())
-        {
-          QFileInfo svgInfo(QDir(path), svg);
-          info.svgfile = svgInfo.filePath();
-        }
-        else
-        {
-          info.svgfile = QString();
-        }
-
-        ds.cardInfo[name] = info;
-
-    }
-}
-
-
-// Translate back side
-QString KCardDialog::findi18nBack(QString& name)
-{
-  if (name.isNull()) return name;
-
-  QMapIterator<QString, KCardInfo> it(ds.deckInfo);
-  while (it.hasNext())
-  {
-      it.next();
-      KCardInfo v = it.value();
-      if (v.noi18Name == name) return v.name;
-  }
-  kError() << "No translation for back card " << name << "found";
-  return name;
-}
-
-
 // Build list widget
 void KCardDialog::insertCardIcons()
 {
@@ -461,11 +279,9 @@ void KCardDialog::insertCardIcons()
 
     // Rebuild list
     QSize itemSize;
-    QMapIterator<QString, KCardInfo> it(ds.cardInfo);
-    while (it.hasNext())
+    foreach( QString name, CardDeckInfo::frontNames() )
     {
-        it.next();
-        KCardInfo v = it.value();
+        KCardInfo v = CardDeckInfo::frontInfo( name );
         // Show only SVG files?
         if (d->filterOutCard(v)) continue;
 
@@ -480,10 +296,10 @@ void KCardDialog::insertCardIcons()
     d->ui.frontList->setIconSize(itemSize);
     
     // Prevent empty preview
-    if (d->useSVGOnly && !isSVGCard(d->currentCard)) 
-        updateFront(defaultCardName(!d->usePNGOnly, !d->useSVGOnly));
-    else if (d->usePNGOnly && isSVGCard(d->currentCard)) 
-        updateFront(defaultCardName(!d->usePNGOnly, !d->useSVGOnly));
+    if (d->useSVGOnly && !CardDeckInfo::isSVGFront(d->currentCard)) 
+        updateFront(CardDeckInfo::defaultFrontName(!d->usePNGOnly, !d->useSVGOnly));
+    else if (d->usePNGOnly && CardDeckInfo::isSVGFront(d->currentCard)) 
+        updateFront(CardDeckInfo::defaultFrontName(!d->usePNGOnly, !d->useSVGOnly));
     else
         updateFront(d->currentCard);
 }
@@ -517,7 +333,7 @@ void KCardDialog::updateFront(QString item)
     QList<QListWidgetItem*> items = d->ui.frontList->findItems(item, Qt::MatchExactly );
     if( !items.isEmpty() )
         items.first()->setSelected( true );
-    KCardInfo info = ds.cardInfo[item];
+    KCardInfo info = CardDeckInfo::frontInfo(item);
     QFont font;
     font.setBold(true);
     d->ui.cardName->setText(info.name);
@@ -539,7 +355,7 @@ void KCardDialog::updateFront(QString item)
     else if (d->useLocking)
     {
       // QMap<QString, KCardInfo>::const_iterator it = d->deckInfo.constBegin();
-      QString name = defaultDeckName(!d->usePNGOnly, !d->useSVGOnly);
+      QString name = CardDeckInfo::defaultBackName(!d->usePNGOnly, !d->useSVGOnly);
       updateBack(name);
     }
   }
@@ -550,113 +366,16 @@ void KCardDialog::updateFront(QString item)
 // Retrieve default card directory
 QString KCardDialog::getDefaultCardDir(bool pAllowSVG, bool pAllowPNG)
 {
-  KCardDialog::init();
-  QString name = defaultCardName(pAllowSVG, pAllowPNG);
-  return cardDir(name);
+  QString name = CardDeckInfo::defaultFrontName(pAllowSVG, pAllowPNG);
+  return CardDeckInfo::frontDir(name);
 }
 
 
 // Retrieve default deck file name
 QString KCardDialog::getDefaultDeck(bool pAllowSVG, bool pAllowPNG)
 {
-  KCardDialog::init();
-  QString name = defaultDeckName(pAllowSVG, pAllowPNG);
-  return deckFilename(name);
-}
-
-
-// Retrieve default card set name
-QString KCardDialog::defaultCardName(bool pAllowSVG, bool pAllowPNG)
-{
-  KCardDialog::init();
-  QString noDefault;
-  // Count filtered cards
-  QMapIterator<QString, KCardInfo> it(ds.cardInfo);
-  while (it.hasNext())
-  {
-      it.next();
-      KCardInfo v = it.value();
-      // Filter
-      if (ds.filterOutCard(v, !pAllowPNG, !pAllowSVG)) continue;
-      if (v.isDefault) return v.name;
-      // Collect any deck if no default is stored
-      noDefault = v.name;
-  }
-  if (noDefault.isNull()) kError() << "Could not find default card name";
-  return noDefault;
-}
-
-
-// Retrieve default deck name
-QString KCardDialog::defaultDeckName(bool pAllowSVG, bool pAllowPNG)
-{
-  KCardDialog::init();
-  QString noDefault;
-  // Count filtered cards
-  QMapIterator<QString, KCardInfo> it(ds.deckInfo);
-  while (it.hasNext())
-  {
-      it.next();
-      KCardInfo v = it.value();
-      // Filter
-      if (ds.filterOutCard(v, !pAllowPNG, !pAllowSVG)) continue;
-      if (v.isDefault) 
-      {
-        return v.name;
-      }
-      // Collect any deck if no default is stored
-      noDefault = v.name;
-  }
-  if (noDefault.isNull()) kError() << "Could not find default deck name";
-  return noDefault;
-}
-
-
-// Retrieve a random card name
-QString KCardDialog::randomCardName(bool pAllowSVG, bool pAllowPNG)
-{
-  KCardDialog::init();
-  // Collect matching items
-  QStringList list;
-
-  // Count filtered cards
-  QMapIterator<QString, KCardInfo> it(ds.cardInfo);
-  while (it.hasNext())
-  {
-      it.next();
-      KCardInfo v = it.value();
-      // Filter
-      if (ds.filterOutCard(v, !pAllowPNG, !pAllowSVG)) continue;
-      list.append(v.name);
-  }
-
-  // Draw random one
-  int d = KRandom::random() % list.count();
-  return list.at(d);
-}
-
-
-// Retrieve a random deck name
-QString KCardDialog::randomDeckName(bool pAllowSVG, bool pAllowPNG)
-{
-  KCardDialog::init();
-  // Collect matching items
-  QStringList list;
-
-  // Count filtered cards
-  QMapIterator<QString, KCardInfo> it(ds.deckInfo);
-  while (it.hasNext())
-  {
-      it.next();
-      KCardInfo v = it.value();
-      // Filter
-      if (ds.filterOutCard(v, !pAllowPNG, !pAllowSVG)) continue;
-      list.append(v.name);
-  }
-
-  // Draw random one
-  int d = KRandom::random() % list.count();
-  return list.at(d);
+  QString name = CardDeckInfo::defaultBackName(pAllowSVG, pAllowPNG);
+  return CardDeckInfo::backFilename(name);
 }
 
 
@@ -745,7 +464,7 @@ void KCardDialog::updateBack(QString item)
     QList<QListWidgetItem*> items = d->ui.backList->findItems(item, Qt::MatchExactly );
     if( !items.isEmpty() )
         items.first()->setSelected( true );
-    KCardInfo info = ds.deckInfo[item];
+    KCardInfo info = CardDeckInfo::backInfo(item);
     QPixmap pixmap= info.preview;
     if (pixmap.height() > d->ui.backPreview->height())
       pixmap = pixmap.scaledToHeight(d->ui.backPreview->height(), Qt::SmoothTransformation);
@@ -765,11 +484,9 @@ void KCardDialog::insertDeckIcons()
 
     // Rebuild list
     QSize itemSize;
-    QMapIterator<QString, KCardInfo> it(ds.deckInfo);
-    while (it.hasNext())
+    foreach( QString name, CardDeckInfo::backNames() )
     {
-        it.next();
-        KCardInfo v = it.value();
+        KCardInfo v = CardDeckInfo::backInfo( name );
         // Show only SVG files?
         if (d->filterOutCard(v)) continue;
         QPixmap previewPixmap = v.preview.scaled(QSize(32,43), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
@@ -784,134 +501,14 @@ void KCardDialog::insertDeckIcons()
     d->ui.backList->setIconSize(itemSize);
     
     // Prevent empty preview
-    if (d->useSVGOnly && !isSVGDeck(d->currentDeck)) 
-        updateBack(defaultDeckName(!d->usePNGOnly, !d->useSVGOnly));
-    else if (d->usePNGOnly && isSVGDeck(d->currentDeck)) 
-        updateBack(defaultDeckName(!d->usePNGOnly, !d->useSVGOnly));
+    if (d->useSVGOnly && !CardDeckInfo::isSVGBack(d->currentDeck)) 
+        updateBack(CardDeckInfo::defaultBackName(!d->usePNGOnly, !d->useSVGOnly));
+    else if (d->usePNGOnly && CardDeckInfo::isSVGBack(d->currentDeck)) 
+        updateBack(CardDeckInfo::defaultBackName(!d->usePNGOnly, !d->useSVGOnly));
     else
         updateBack(d->currentDeck);
 
 }
 
 
-// Read the card deck data
-void KCardDialog::readBacks()
-{
-    // Empty data
-    ds.deckInfo.clear();
-
-    QStringList list = KGlobal::dirs()->findAllResources("cards", "decks/*.desktop", KStandardDirs::NoDuplicates);
-    if (list.isEmpty()) return;
-
-    for (QStringList::ConstIterator it = list.begin(); it != list.end(); ++it)
-    {
-        KConfig cfg(*it, KConfig::SimpleConfig);
-        QString path = (*it).left((*it).lastIndexOf('/') + 1);
-        Q_ASSERT(path[path.length() - 1] == '/');
-        QPixmap pixmap(getDeckFileNameFromIndex(*it));
-        if (pixmap.isNull()) continue;
-        //pixmap = pixmap.scaledToWidth(72, Qt::SmoothTransformation);
-        QPixmap previewPixmap = pixmap.scaled(QSize(32,43), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
-
-        KConfigGroup cfgcg(&cfg, "KDE Cards");
-        QString idx  = cfgcg.readEntryUntranslated("Name", i18n("unnamed"));
-        QString name = cfgcg.readEntry("Name", i18n("unnamed"));
-        KCardInfo info;
-        info.name         = name;
-        info.noi18Name    = idx;
-        info.path         = getDeckFileNameFromIndex(*it);
-        info.comment      = cfgcg.readEntry("Comment",i18n("KDE card deck"));
-        info.preview      = pixmap; 
-        info.size         = cfgcg.readEntry("Size", QSizeF(pixmap.size()));
-        info.isDefault    = cfgcg.readEntry("Default", false);
-
-        QString svg    = cfgcg.readEntry("SVG", QString());
-        if (!svg.isNull())
-        {
-          QFileInfo svgInfo(QDir(path), svg);
-          info.svgfile = svgInfo.filePath();
-        }
-        else
-        {
-          info.svgfile = QString();
-        }
-        ds.deckInfo[name] = info;
-    }
-}
-
-
-// Retrieve the PNG filename for a back side from its index.desktop filename
-QString KCardDialog::getDeckFileNameFromIndex(const QString &desktop)
-{
-    QString entry = desktop.left(desktop.length() - strlen(".desktop"));
-    if (KStandardDirs::exists(entry + QString::fromLatin1(".png")))
-        return entry + QString::fromLatin1(".png");
-
-    // rather theoretical
-    if (KStandardDirs::exists(entry + QString::fromLatin1(".xpm")))
-        return entry + QString::fromLatin1(".xpm");
-    return QString();
-}
-
-
-
-// Retrieve the SVG file belonging to the given card back deck.
-QString KCardDialog::deckSVGFilePath(const QString& name)
-{
-  KCardDialog::init();
-  if (!ds.deckInfo.contains(name)) return QString();
-  KCardInfo v = ds.deckInfo.value(name);
-  return v.svgfile;
-}
-
-
-// Retrieve the SVG file belonging to the given card fronts.
-QString KCardDialog::cardSVGFilePath(const QString& name)
-{
-  KCardDialog::init();
-  if (!ds.cardInfo.contains(name)) return QString();
-  KCardInfo v = ds.cardInfo.value(name);
-  return v.svgfile;
-}
-
-
-// Retrieve the PNG file belonging to the given card back deck.
-QString KCardDialog::deckFilename(const QString& name)
-{
-  KCardDialog::init();
-  if (!ds.deckInfo.contains(name)) return QString();
-  KCardInfo v = ds.deckInfo.value(name);
-  return v.path;
-}
-
-
-// Retrieve the directory belonging to the given card fronts.
-QString KCardDialog::cardDir(const QString& name)
-{
-  KCardDialog::init();
-  if (!ds.cardInfo.contains(name)) return QString();
-  KCardInfo v = ds.cardInfo.value(name);
-  return v.path;
-}
-
-
-// Check whether a card set is SVG
-bool KCardDialog::isSVGCard(const QString& name)
-{
-  KCardDialog::init();
-  if (!ds.cardInfo.contains(name)) return false;
-  KCardInfo v = ds.cardInfo.value(name);
-  return !v.svgfile.isNull();
-}
-
-
-// Check whether a card deck is SVG
-bool KCardDialog::isSVGDeck(const QString& name)
-{
-  KCardDialog::init();
-  if (!ds.deckInfo.contains(name)) return false;
-  KCardInfo v = ds.deckInfo.value(name);
-  return !v.svgfile.isNull();
-}
-
 #include "kcarddialog.moc"
diff --git a/libkdegames/kcarddialog.h b/libkdegames/kcarddialog.h
index 181f8c7..a5e49f0 100644
--- a/libkdegames/kcarddialog.h
+++ b/libkdegames/kcarddialog.h
@@ -153,100 +153,33 @@ public:
                           bool pRandom = false);
 
    /**
-    * Saves the KCardDialog config into a config file. 
-    * These settings are used by @ref KCardDialog.
-    */
-   void saveSettings(KConfigGroup& group);
-
-   /** Retrieve the SVG file belonging to the given card deck (back side). 
-    * @param name The name of the back deck.
-    * @return The file name and path to the SVG file or QString() if not available. 
-    */
-   static QString deckSVGFilePath(const QString& name);
-
-   /** Retrieve the SVG file belonging to the given card set (front side). 
-    * The SVG IDs used for the card back is '1_club' for Ace of clubs, '10_spade' for
-    * 10 of spades, 'queen_heart' for Queen of Hearts, '2_diamond' for 2 of diamonds and
-    * so on.
-    * @param name The name of the card set.
-    * @return The file name and path to the SVG file or QString() if not available. 
-    */
-   static QString cardSVGFilePath(const QString& name);
-
-   /** Check whether the card set is SVG or not.
-    * @param name The name of the card set.
-    * @return True if SVG data is available.
-    */
-   static bool isSVGCard(const QString& name);
-
-   /** Check whether the card back deck contains also an SVG file.
-    * @param name The name of the card deck.
-    * @return True if SVG data is available.
-    */
-   static bool isSVGDeck(const QString& name);
-   
-   /** Retrieve the name of the default card set (front side).
-    * @param pAllowSVG  Allow selection of scalable cards sets.
-    * @param pAllowPNG  Allow selection of fixed size cards sets.
-    * @return The default card set name.
-    */
-   static QString defaultCardName(bool pAllowSVG = true, bool pAllowPNG = true);
-   
-   /** Retrieve the name of the default card deck (back side).
-    * @param pAllowSVG  Allow selection of scalable cards sets.
-    * @param pAllowPNG  Allow selection of fixed size cards sets.
-    * @return The default card deck name.
-    */
-   static QString defaultDeckName(bool pAllowSVG = true, bool pAllowPNG = true);
-   
-   /** Retrieve a random card set (front side).
-    * @param pAllowSVG  Allow selection of scalable cards sets.
-    * @param pAllowPNG  Allow selection of fixed size cards sets.
-    * @return A radnom card set name.
-    */
-   static QString randomCardName(bool pAllowSVG = true, bool pAllowPNG = true);
- 
-   /** Retrieve a random card deck (back side).
-    * @param pAllowSVG  Allow selection of scalable cards sets.
-    * @param pAllowPNG  Allow selection of fixed size cards sets.
-    * @return A radnom card deck name.
-    */
-   static QString randomDeckName(bool pAllowSVG = true, bool pAllowPNG = true);
-
-   /**
-    * Retrieve the directory where the card front sides are stored. The cards are
-    * named 1.png, 2.png, etc. For SVG card decks use @ref cardSVGFilePath.
-    * @param name The name of the card set.
-    * @return The directory.
+    * Convenience function, works like the above function.
+    *
+    * Creates a modal carddeck dialog, reading the settings from the
+    * config @p group.
+    *
+    * @param group the KConfigGroup to read the settings from.
+    * @return QDialog::result();
     */
-   static QString cardDir(const QString& name);
+   static int getCardDeck(const KConfigGroup& group);
 
    /**
-    * Retrieve the filename of the card back side. 
-    * For SVG  decks use @ref deckSVGFilePath.
-    * @param name The name of the card deck.
-    * @return The filename.
+    * Saves the KCardDialog config into a config file. 
+    * These settings are used by @ref KCardDialog.
     */
-   static QString deckFilename(const QString& name);
+   void saveSettings(KConfigGroup& group);
 
    /**
     * Retrieve the name of the card deck (back side) from the dialog.
     * @return The deck name.
     */
-   QString deckName() const;
+   QString backName() const;
 
    /**
     * Retrieve the name of the card set (front side) from the dialog.
     * @return The card set name.
     */
-   QString cardName() const;
-
-   /**
-    * Reset the static information of the dialog. In particular reread the
-    * card information. Only necessary to call if something changed there,
-    * like installing new card decks.
-    */
-   static void reset();
+   QString frontName() const;
 
    /**
     * Retreive the default card directory.
@@ -262,19 +195,6 @@ public:
 
 
 protected:
-    /**
-     * Read in all front side card sets.
-     */
-    static void readFronts();
-    
-    /**
-     * Read in all back side card decks.
-     */
-    static void readBacks();
-    
-    /** 
-     * Insert the front sides into the list view.
-     */
     void insertCardIcons();
     
     /**
@@ -314,11 +234,6 @@ protected:
     */
     static QString getDeckFileNameFromIndex(const QString& desktop);
 
-    /**
-     * @return the groupname.
-     **/
-    static QString group();
-
 protected Q_SLOTS:
     /**
      * Called by the card set list view when a new item was selected.
@@ -347,15 +262,11 @@ protected Q_SLOTS:
     /**
      * Called by the checkboxes when the state of the PNG filter changed.
      * @param state The new PNG filter state.
-     */    void updatePNG(int state);
+     */
+    void updatePNG(int state);
 
 private:
    /**
-    * Setup some static information.
-    */
-   static void init();
-
-   /**
     * The dialog data.
     */
    KCardDialogPrivate* const d;


More information about the kde-games-devel mailing list