• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

libkdegames

carddeckinfo.cpp

Go to the documentation of this file.
00001 /*
00002     This file is part of the KDE games library
00003     Copyright 2008 Andreas Pakulat <apaku@gmx.de>
00004 
00005     This library is free software; you can redistribute it and/or
00006     modify it under the terms of the GNU Library General Public
00007     License version 2 as published by the Free Software Foundation.
00008 
00009     This library is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012     Library General Public License for more details.
00013 
00014     You should have received a copy of the GNU Library General Public License
00015     along with this library; see the file COPYING.LIB.  If not, write to
00016     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017     Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include <QFileInfo>
00021 #include <QDir>
00022 
00023 #include <klocale.h>
00024 #include <kstandarddirs.h>
00025 #include <krandom.h>
00026 #include <kdebug.h>
00027 #include <kconfiggroup.h>
00028 #include <kglobal.h>
00029 
00030 #include "carddeckinfo.h"
00031 #include "carddeckinfo_p.h"
00032 
00033 // KConfig entries
00034 #define CONF_LOCKING QString::fromLatin1("Locking")
00035 #define CONF_ALLOW_FIXED_CARDS QString::fromLatin1("AllowFixed")
00036 #define CONF_CARD QString::fromLatin1("Cardname")
00037 #define CONF_DECK QString::fromLatin1("Deckname")
00038 
00042 class KCardThemeInfoStatic
00043 {
00044 public:
00045     KCardThemeInfoStatic()
00046     {
00047         KGlobal::dirs()->addResourceType( "cards", "data", "carddecks/" );
00048         KGlobal::locale()->insertCatalog( "libkdegames" );
00049         readBacks();
00050         readFronts();
00051     }
00052     ~KCardThemeInfoStatic()
00053     {
00054     }
00055     
00056     
00057     // Translate back side
00058     QString findi18nBack( QString& name )
00059     {
00060         if ( name.isNull() ) return name;
00061     
00062         QMap<QString, KCardThemeInfo> temp = svgBackInfo;
00063         temp.unite( pngBackInfo );
00064     
00065         QMapIterator<QString, KCardThemeInfo> it( temp );
00066         while ( it.hasNext() )
00067         {
00068             it.next();
00069             KCardThemeInfo v = it.value();
00070             if ( v.noi18Name == name ) return v.name;
00071         }
00072         kError() << "No translation for back card " << name << "found";
00073         return name;
00074     }
00075 
00076     void readFronts()
00077     {
00078         // Empty data
00079         pngFrontInfo.clear();
00080         svgFrontInfo.clear();
00081 
00082         QStringList svg;
00083         // Add SVG card sets
00084         svg = KGlobal::dirs()->findAllResources( "cards", "svg*/index.desktop", KStandardDirs::NoDuplicates );
00085         QStringList list = svg + KGlobal::dirs()->findAllResources( "cards", "card*/index.desktop", KStandardDirs::NoDuplicates );
00086 
00087         if ( list.isEmpty() ) return;
00088 
00089         for ( QStringList::ConstIterator it = list.begin(); it != list.end(); ++it )
00090         {
00091             KConfig cfg( *it, KConfig::SimpleConfig );
00092             KConfigGroup cfgcg( &cfg, "KDE Backdeck" );
00093             QString path = ( *it ).left(( *it ).lastIndexOf( '/' ) + 1 );
00094             Q_ASSERT( path[path.length() - 1] == '/' );
00095             QPixmap pixmap( path + cfgcg.readEntry( "Preview", "12c.png" ) );
00096             if ( pixmap.isNull() ) continue;
00097 
00098             QString idx  = cfgcg.readEntryUntranslated( "Name", i18n( "unnamed" ) );
00099             QString name = cfgcg.readEntry( "Name", i18n( "unnamed" ) );
00100             KCardThemeInfo info;
00101             info.name         = name;
00102             info.noi18Name    = idx;
00103             info.comment      = cfgcg.readEntry( "Comment", i18n( "KDE card deck" ) );
00104             info.preview      = pixmap;
00105             info.path         = path;
00106             info.back         = cfgcg.readEntry( "Back", QString() );
00107             // The back name is read UNTRANSLATED...we need to find the right name for it now
00108             info.back         = findi18nBack( info.back );
00109             // if (!info.back.isNull()) kDebug() << "FOUND BACK " << info.back;
00110             info.size         = cfgcg.readEntry( "BackSize", QSizeF( pixmap.size() ) );
00111             info.isDefault    = cfgcg.readEntry( "Default", false );
00112 
00113             QString svg    = cfgcg.readEntry( "SVG", QString() );
00114             if ( !svg.isEmpty() )
00115             {
00116                 QFileInfo svgInfo( QDir( path ), svg );
00117                 info.svgfile = svgInfo.filePath();
00118                 svgFrontInfo[name] = info;
00119             }
00120             else
00121             {
00122                 info.svgfile = QString();
00123                 pngFrontInfo[name] = info;
00124             }
00125         }
00126 
00127     }
00128 
00129 
00130     void readBacks()
00131     {
00132         // Empty data
00133         svgBackInfo.clear();
00134         pngBackInfo.clear();
00135     
00136         QStringList list = KGlobal::dirs()->findAllResources( "cards", "decks/*.desktop", KStandardDirs::NoDuplicates );
00137 
00138         if ( list.isEmpty() ) return;
00139     
00140         for ( QStringList::ConstIterator it = list.begin(); it != list.end(); ++it )
00141         {
00142             KConfig cfg( *it, KConfig::SimpleConfig );
00143             QString path = ( *it ).left(( *it ).lastIndexOf( '/' ) + 1 );
00144             Q_ASSERT( path[path.length() - 1] == '/' );
00145             QPixmap pixmap( getBackFileNameFromIndex( *it ) );
00146             if ( pixmap.isNull() ) continue;
00147             //pixmap = pixmap.scaledToWidth(72, Qt::SmoothTransformation);
00148             QPixmap previewPixmap = pixmap.scaled( QSize( 32, 43 ), Qt::IgnoreAspectRatio, Qt::SmoothTransformation );
00149     
00150             KConfigGroup cfgcg( &cfg, "KDE Cards" );
00151             QString idx  = cfgcg.readEntryUntranslated( "Name", i18n( "unnamed" ) );
00152             QString name = cfgcg.readEntry( "Name", i18n( "unnamed" ) );
00153             KCardThemeInfo info;
00154             info.name         = name;
00155             info.noi18Name    = idx;
00156             info.path         = getBackFileNameFromIndex( *it );
00157             info.comment      = cfgcg.readEntry( "Comment", i18n( "KDE card deck" ) );
00158             info.preview      = pixmap;
00159             info.size         = cfgcg.readEntry( "Size", QSizeF( pixmap.size() ) );
00160             info.isDefault    = cfgcg.readEntry( "Default", false );
00161     
00162             QString svg    = cfgcg.readEntry( "SVG", QString() );
00163             if ( !svg.isEmpty() )
00164             {
00165                 QFileInfo svgInfo( QDir( path ), svg );
00166                 info.svgfile = svgInfo.filePath();
00167                 svgBackInfo[name] = info;
00168             }
00169             else
00170             {
00171                 info.svgfile = QString();
00172                 pngBackInfo[name] = info;
00173             }
00174         }
00175     
00176     }
00177 
00178     QString getBackFileNameFromIndex( const QString& desktop )
00179     {
00180         QString entry = desktop.left( desktop.length() - strlen( ".desktop" ) );
00181         if ( KStandardDirs::exists( entry + QString::fromLatin1( ".png" ) ) )
00182             return entry + QString::fromLatin1( ".png" );
00183 
00184         // rather theoretical
00185         if ( KStandardDirs::exists( entry + QString::fromLatin1( ".xpm" ) ) )
00186             return entry + QString::fromLatin1( ".xpm" );
00187         return QString();
00188     }
00189 
00190 
00193     QMap<QString, KCardThemeInfo> pngFrontInfo;
00194 
00197     QMap<QString, KCardThemeInfo> svgFrontInfo;
00198 
00201     QMap<QString, KCardThemeInfo> pngBackInfo;
00202 
00205     QMap<QString, KCardThemeInfo> svgBackInfo;
00206 
00209     QString defaultFront;
00210 
00213     QString defaultBack;
00214 };
00215 
00216 K_GLOBAL_STATIC( KCardThemeInfoStatic, deckinfoStatic )
00217 
00218 namespace CardDeckInfo
00219 {
00220 
00221 // Retrieve default card set name
00222 QString defaultFrontName( bool pAllowPNG )
00223 {
00224     QString noDefault;
00225     // Count filtered cards
00226     QMap<QString, KCardThemeInfo> temp = deckinfoStatic->svgFrontInfo;
00227     if ( pAllowPNG )
00228     {
00229         temp.unite( deckinfoStatic->pngFrontInfo );
00230     }
00231     QMapIterator<QString, KCardThemeInfo> it( temp );
00232     while ( it.hasNext() )
00233     {
00234         it.next();
00235         KCardThemeInfo v = it.value();
00236         // Filter
00237         if ( v.isDefault ) return v.name;
00238         // Collect any deck if no default is stored
00239         noDefault = v.name;
00240     }
00241     if ( noDefault.isNull() ) kError() << "Could not find default card name";
00242     return noDefault;
00243 }
00244 
00245 
00246 // Retrieve default deck name
00247 QString defaultBackName( bool pAllowPNG )
00248 {
00249     QString noDefault;
00250     QMap<QString, KCardThemeInfo> temp = deckinfoStatic->svgBackInfo;
00251     if ( pAllowPNG )
00252     {
00253         temp.unite( deckinfoStatic->pngBackInfo );
00254     }
00255 
00256     QMapIterator<QString, KCardThemeInfo> it( temp );
00257     while ( it.hasNext() )
00258     {
00259         it.next();
00260         KCardThemeInfo v = it.value();
00261         // Filter
00262         if ( v.isDefault ) return v.name;
00263         // Collect any deck if no default is stored
00264         noDefault = v.name;
00265     }
00266     if ( noDefault.isNull() ) kError() << "Could not find default deck name";
00267     return noDefault;
00268 }
00269 
00270 
00271 // Retrieve a random card name
00272 QString randomFrontName( bool pAllowPNG )
00273 {
00274     // Collect matching items
00275     QStringList list = deckinfoStatic->svgFrontInfo.keys();
00276     if ( pAllowPNG )
00277     {
00278         list += deckinfoStatic->pngFrontInfo.keys();
00279     }
00280 
00281     // Draw random one
00282     int d = KRandom::random() % list.count();
00283     return list.at( d );
00284 }
00285 
00286 
00287 // Retrieve a random deck name
00288 QString randomBackName( bool pAllowPNG )
00289 {
00290     // Collect matching items
00291     QStringList list = deckinfoStatic->svgBackInfo.keys();
00292     if ( pAllowPNG )
00293     {
00294         list += deckinfoStatic->pngBackInfo.keys();
00295     }
00296 
00297     // Draw random one
00298     int d = KRandom::random() % list.count();
00299     return list.at( d );
00300 }
00301 
00302 
00303 // Retrieve the PNG filename for a back side from its index.desktop filename
00304 QString getBackFileNameFromIndex( const QString &desktop )
00305 {
00306     QString entry = desktop.left( desktop.length() - strlen( ".desktop" ) );
00307     if ( KStandardDirs::exists( entry + QString::fromLatin1( ".png" ) ) )
00308         return entry + QString::fromLatin1( ".png" );
00309 
00310     // rather theoretical
00311     if ( KStandardDirs::exists( entry + QString::fromLatin1( ".xpm" ) ) )
00312         return entry + QString::fromLatin1( ".xpm" );
00313     return QString();
00314 }
00315 
00316 
00317 
00318 // Retrieve the SVG file belonging to the given card back deck.
00319 QString backSVGFilePath( const QString& name )
00320 {
00321     if ( !deckinfoStatic->svgBackInfo.contains( name ) ) return QString();
00322     KCardThemeInfo v = deckinfoStatic->svgBackInfo.value( name );
00323     return v.svgfile;
00324 }
00325 
00326 
00327 // Retrieve the SVG file belonging to the given card fronts.
00328 QString frontSVGFilePath( const QString& name )
00329 {
00330     if ( !deckinfoStatic->svgFrontInfo.contains( name ) ) return QString();
00331     KCardThemeInfo v = deckinfoStatic->svgFrontInfo.value( name );
00332     return v.svgfile;
00333 }
00334 
00335 
00336 // Retrieve the PNG file belonging to the given card back deck.
00337 QString backFilename( const QString& name )
00338 {
00339     if ( !deckinfoStatic->pngBackInfo.contains( name ) ) return QString();
00340     KCardThemeInfo v = deckinfoStatic->pngBackInfo.value( name );
00341     return v.path;
00342 }
00343 
00344 
00345 // Retrieve the directory belonging to the given card fronts.
00346 QString frontDir( const QString& name )
00347 {
00348     if ( !deckinfoStatic->pngFrontInfo.contains( name ) ) return QString();
00349     KCardThemeInfo v = deckinfoStatic->pngFrontInfo.value( name );
00350     return v.path;
00351 }
00352 
00353 
00354 // Check whether a card set is SVG
00355 bool isSVGFront( const QString& name )
00356 {
00357     return deckinfoStatic->svgFrontInfo.contains( name );
00358 }
00359 
00360 
00361 // Check whether a card deck is SVG
00362 bool isSVGBack( const QString& name )
00363 {
00364     return deckinfoStatic->svgBackInfo.contains( name );
00365 }
00366 
00367 QStringList frontNames()
00368 {
00369     return ( deckinfoStatic->svgFrontInfo.keys() + deckinfoStatic->pngFrontInfo.keys() );
00370 }
00371 
00372 QStringList backNames()
00373 {
00374     return ( deckinfoStatic->svgBackInfo.keys() + deckinfoStatic->pngBackInfo.keys() );
00375 }
00376 
00377 KCardThemeInfo frontInfo( const QString& name )
00378 {
00379     if ( deckinfoStatic->svgFrontInfo.contains( name ) )
00380         return deckinfoStatic->svgFrontInfo.value( name );
00381     if ( deckinfoStatic->pngFrontInfo.contains( name ) )
00382         return deckinfoStatic->pngFrontInfo.value( name );
00383     return KCardThemeInfo();
00384 }
00385 
00386 KCardThemeInfo backInfo( const QString& name )
00387 {
00388     if ( deckinfoStatic->svgFrontInfo.contains( name ) )
00389         return deckinfoStatic->svgBackInfo.value( name );
00390     if ( deckinfoStatic->pngBackInfo.contains( name ) )
00391         return deckinfoStatic->pngBackInfo.value( name );
00392     return KCardThemeInfo();
00393 }
00394 
00395 QString frontTheme( const KConfigGroup& group, const QString& defaultTheme )
00396 {
00397   return group.readEntry( CONF_CARD, defaultTheme );
00398 }
00399 
00400 QString backTheme( const KConfigGroup& group, const QString& defaultTheme )
00401 {
00402   return group.readEntry( CONF_DECK, defaultTheme );
00403 }
00404 
00405 bool allowFixedSizeDecks( const KConfigGroup& group, bool lockDefault )
00406 {
00407   return group.readEntry( CONF_ALLOW_FIXED_CARDS, lockDefault );
00408 }
00409 
00410 bool lockFrontToBackside(const KConfigGroup& group, bool lockDefault)
00411 {
00412   return group.readEntry( CONF_LOCKING, lockDefault );
00413 }
00414 
00415 void writeFrontTheme( KConfigGroup& group, const QString& theme )
00416 {
00417   group.writeEntry( CONF_CARD, theme );
00418 }
00419 
00420 void writeBackTheme( KConfigGroup& group, const QString& theme )
00421 {
00422   group.writeEntry( CONF_DECK, theme );
00423 }
00424 
00425 void writeAllowFixedSizeDecks( KConfigGroup& group, bool allowFixedSize )
00426 {
00427   group.writeEntry( CONF_ALLOW_FIXED_CARDS, allowFixedSize );
00428 }
00429 
00430 void writeLockFrontToBackside( KConfigGroup& group, bool lock )
00431 {
00432   group.writeEntry( CONF_LOCKING, lock );
00433 }
00434 
00435 }
00436 

libkdegames

Skip menu "libkdegames"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

API Reference

Skip menu "API Reference"
  • kblackbox
  • kgoldrunner
  • kmahjongg
  • ksquares
  • libkdegames
  •   highscore
  •   kgame
  •   kggzgames
  •   kggzmod
  •   kggznet
  • libkmahjongg
Generated for API Reference by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal