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 
00036 class KCardThemeInfoStatic
00037 {
00038 public:
00039     KCardThemeInfoStatic()
00040     {
00041         KGlobal::dirs()->addResourceType( "cards", "data", "carddecks/" );
00042         KGlobal::locale()->insertCatalog( "libkdegames" );
00043         readBacks();
00044         readFronts();
00045     }
00046     ~KCardThemeInfoStatic()
00047     {
00048     }
00049     
00050     
00051     // Translate back side
00052     QString findi18nBack( QString& name )
00053     {
00054         if ( name.isNull() ) return name;
00055     
00056         QMap<QString, KCardThemeInfo> temp = svgBackInfo;
00057         temp.unite( pngBackInfo );
00058     
00059         QMapIterator<QString, KCardThemeInfo> it( temp );
00060         while ( it.hasNext() )
00061         {
00062             it.next();
00063             KCardThemeInfo v = it.value();
00064             if ( v.noi18Name == name ) return v.name;
00065         }
00066         kError() << "No translation for back card " << name << "found";
00067         return name;
00068     }
00069 
00070     void readFronts()
00071     {
00072         // Empty data
00073         pngFrontInfo.clear();
00074         svgFrontInfo.clear();
00075 
00076         QStringList svg;
00077         // Add SVG card sets
00078         svg = KGlobal::dirs()->findAllResources( "cards", "svg*/index.desktop", KStandardDirs::NoDuplicates );
00079         QStringList list = svg + KGlobal::dirs()->findAllResources( "cards", "card*/index.desktop", KStandardDirs::NoDuplicates );
00080 
00081         if ( list.isEmpty() ) return;
00082 
00083         for ( QStringList::ConstIterator it = list.begin(); it != list.end(); ++it )
00084         {
00085             KConfig cfg( *it, KConfig::SimpleConfig );
00086             KConfigGroup cfgcg( &cfg, "KDE Backdeck" );
00087             QString path = ( *it ).left(( *it ).lastIndexOf( '/' ) + 1 );
00088             Q_ASSERT( path[path.length() - 1] == '/' );
00089             QPixmap pixmap( path + cfgcg.readEntry( "Preview", "12c.png" ) );
00090             if ( pixmap.isNull() ) continue;
00091 
00092             QString idx  = cfgcg.readEntryUntranslated( "Name", i18n( "unnamed" ) );
00093             QString name = cfgcg.readEntry( "Name", i18n( "unnamed" ) );
00094             KCardThemeInfo info;
00095             info.name         = name;
00096             info.noi18Name    = idx;
00097             info.comment      = cfgcg.readEntry( "Comment", i18n( "KDE card deck" ) );
00098             info.preview      = pixmap;
00099             info.path         = path;
00100             info.back         = cfgcg.readEntry( "Back", QString() );
00101             // The back name is read UNTRANSLATED...we need to find the right name for it now
00102             info.back         = findi18nBack( info.back );
00103             // if (!info.back.isNull()) kDebug() << "FOUND BACK " << info.back;
00104             info.size         = cfgcg.readEntry( "BackSize", QSizeF( pixmap.size() ) );
00105             info.isDefault    = cfgcg.readEntry( "Default", false );
00106 
00107             QString svg    = cfgcg.readEntry( "SVG", QString() );
00108             if ( !svg.isEmpty() )
00109             {
00110                 QFileInfo svgInfo( QDir( path ), svg );
00111                 info.svgfile = svgInfo.filePath();
00112                 svgFrontInfo[name] = info;
00113             }
00114             else
00115             {
00116                 info.svgfile = QString();
00117                 pngFrontInfo[name] = info;
00118             }
00119         }
00120 
00121     }
00122 
00123 
00124     void readBacks()
00125     {
00126         // Empty data
00127         svgBackInfo.clear();
00128         pngBackInfo.clear();
00129     
00130         QStringList list = KGlobal::dirs()->findAllResources( "cards", "decks/*.desktop", KStandardDirs::NoDuplicates );
00131 
00132         if ( list.isEmpty() ) return;
00133     
00134         for ( QStringList::ConstIterator it = list.begin(); it != list.end(); ++it )
00135         {
00136             KConfig cfg( *it, KConfig::SimpleConfig );
00137             QString path = ( *it ).left(( *it ).lastIndexOf( '/' ) + 1 );
00138             Q_ASSERT( path[path.length() - 1] == '/' );
00139             QPixmap pixmap( getBackFileNameFromIndex( *it ) );
00140             if ( pixmap.isNull() ) continue;
00141             //pixmap = pixmap.scaledToWidth(72, Qt::SmoothTransformation);
00142             QPixmap previewPixmap = pixmap.scaled( QSize( 32, 43 ), Qt::IgnoreAspectRatio, Qt::SmoothTransformation );
00143     
00144             KConfigGroup cfgcg( &cfg, "KDE Cards" );
00145             QString idx  = cfgcg.readEntryUntranslated( "Name", i18n( "unnamed" ) );
00146             QString name = cfgcg.readEntry( "Name", i18n( "unnamed" ) );
00147             KCardThemeInfo info;
00148             info.name         = name;
00149             info.noi18Name    = idx;
00150             info.path         = getBackFileNameFromIndex( *it );
00151             info.comment      = cfgcg.readEntry( "Comment", i18n( "KDE card deck" ) );
00152             info.preview      = pixmap;
00153             info.size         = cfgcg.readEntry( "Size", QSizeF( pixmap.size() ) );
00154             info.isDefault    = cfgcg.readEntry( "Default", false );
00155     
00156             QString svg    = cfgcg.readEntry( "SVG", QString() );
00157             if ( !svg.isEmpty() )
00158             {
00159                 QFileInfo svgInfo( QDir( path ), svg );
00160                 info.svgfile = svgInfo.filePath();
00161                 svgBackInfo[name] = info;
00162             }
00163             else
00164             {
00165                 info.svgfile = QString();
00166                 pngBackInfo[name] = info;
00167             }
00168         }
00169     
00170     }
00171 
00172     QString getBackFileNameFromIndex( const QString& desktop )
00173     {
00174         QString entry = desktop.left( desktop.length() - strlen( ".desktop" ) );
00175         if ( KStandardDirs::exists( entry + QString::fromLatin1( ".png" ) ) )
00176             return entry + QString::fromLatin1( ".png" );
00177 
00178         // rather theoretical
00179         if ( KStandardDirs::exists( entry + QString::fromLatin1( ".xpm" ) ) )
00180             return entry + QString::fromLatin1( ".xpm" );
00181         return QString();
00182     }
00183 
00184 
00187     QMap<QString, KCardThemeInfo> pngFrontInfo;
00188 
00191     QMap<QString, KCardThemeInfo> svgFrontInfo;
00192 
00195     QMap<QString, KCardThemeInfo> pngBackInfo;
00196 
00199     QMap<QString, KCardThemeInfo> svgBackInfo;
00200 
00203     QString defaultFront;
00204 
00207     QString defaultBack;
00208 };
00209 
00210 K_GLOBAL_STATIC( KCardThemeInfoStatic, deckinfoStatic )
00211 
00212 namespace CardDeckInfo
00213 {
00214 
00215 // Retrieve default card set name
00216 QString defaultFrontName( bool pAllowSVG, bool pAllowPNG )
00217 {
00218     QString noDefault;
00219     // Count filtered cards
00220     QMap<QString, KCardThemeInfo> temp;
00221     if ( pAllowSVG )
00222     {
00223         temp.unite( deckinfoStatic->svgFrontInfo );
00224     }
00225     if ( pAllowPNG )
00226     {
00227         temp.unite( deckinfoStatic->pngFrontInfo );
00228     }
00229     QMapIterator<QString, KCardThemeInfo> it( temp );
00230     while ( it.hasNext() )
00231     {
00232         it.next();
00233         KCardThemeInfo v = it.value();
00234         // Filter
00235         if ( v.isDefault ) return v.name;
00236         // Collect any deck if no default is stored
00237         noDefault = v.name;
00238     }
00239     if ( noDefault.isNull() ) kError() << "Could not find default card name";
00240     return noDefault;
00241 }
00242 
00243 
00244 // Retrieve default deck name
00245 QString defaultBackName( bool pAllowSVG, bool pAllowPNG )
00246 {
00247     QString noDefault;
00248     QMap<QString, KCardThemeInfo> temp;
00249     if ( pAllowSVG )
00250     {
00251         temp.unite( deckinfoStatic->svgBackInfo );
00252     }
00253     if ( pAllowPNG )
00254     {
00255         temp.unite( deckinfoStatic->pngBackInfo );
00256     }
00257 
00258     QMapIterator<QString, KCardThemeInfo> it( temp );
00259     while ( it.hasNext() )
00260     {
00261         it.next();
00262         KCardThemeInfo v = it.value();
00263         // Filter
00264         if ( v.isDefault ) return v.name;
00265         // Collect any deck if no default is stored
00266         noDefault = v.name;
00267     }
00268     if ( noDefault.isNull() ) kError() << "Could not find default deck name";
00269     return noDefault;
00270 }
00271 
00272 
00273 // Retrieve a random card name
00274 QString randomFrontName( bool pAllowSVG, bool pAllowPNG )
00275 {
00276     // Collect matching items
00277     QStringList list;
00278     if ( pAllowSVG )
00279     {
00280         list += deckinfoStatic->svgFrontInfo.keys();
00281     }
00282     if ( pAllowPNG )
00283     {
00284         list += deckinfoStatic->pngFrontInfo.keys();
00285     }
00286 
00287     // Draw random one
00288     int d = KRandom::random() % list.count();
00289     return list.at( d );
00290 }
00291 
00292 
00293 // Retrieve a random deck name
00294 QString randomBackName( bool pAllowSVG, bool pAllowPNG )
00295 {
00296     // Collect matching items
00297     QStringList list;
00298 
00299     if ( pAllowSVG )
00300     {
00301         list += deckinfoStatic->svgBackInfo.keys();
00302     }
00303     if ( pAllowPNG )
00304     {
00305         list += deckinfoStatic->pngBackInfo.keys();
00306     }
00307 
00308     // Draw random one
00309     int d = KRandom::random() % list.count();
00310     return list.at( d );
00311 }
00312 
00313 
00314 // Retrieve the PNG filename for a back side from its index.desktop filename
00315 QString getBackFileNameFromIndex( const QString &desktop )
00316 {
00317     QString entry = desktop.left( desktop.length() - strlen( ".desktop" ) );
00318     if ( KStandardDirs::exists( entry + QString::fromLatin1( ".png" ) ) )
00319         return entry + QString::fromLatin1( ".png" );
00320 
00321     // rather theoretical
00322     if ( KStandardDirs::exists( entry + QString::fromLatin1( ".xpm" ) ) )
00323         return entry + QString::fromLatin1( ".xpm" );
00324     return QString();
00325 }
00326 
00327 
00328 
00329 // Retrieve the SVG file belonging to the given card back deck.
00330 QString backSVGFilePath( const QString& name )
00331 {
00332     if ( !deckinfoStatic->svgBackInfo.contains( name ) ) return QString();
00333     KCardThemeInfo v = deckinfoStatic->svgBackInfo.value( name );
00334     return v.svgfile;
00335 }
00336 
00337 
00338 // Retrieve the SVG file belonging to the given card fronts.
00339 QString frontSVGFilePath( const QString& name )
00340 {
00341     if ( !deckinfoStatic->svgFrontInfo.contains( name ) ) return QString();
00342     KCardThemeInfo v = deckinfoStatic->svgFrontInfo.value( name );
00343     return v.svgfile;
00344 }
00345 
00346 
00347 // Retrieve the PNG file belonging to the given card back deck.
00348 QString backFilename( const QString& name )
00349 {
00350     if ( !deckinfoStatic->pngBackInfo.contains( name ) ) return QString();
00351     KCardThemeInfo v = deckinfoStatic->pngBackInfo.value( name );
00352     return v.path;
00353 }
00354 
00355 
00356 // Retrieve the directory belonging to the given card fronts.
00357 QString frontDir( const QString& name )
00358 {
00359     if ( !deckinfoStatic->pngFrontInfo.contains( name ) ) return QString();
00360     KCardThemeInfo v = deckinfoStatic->pngFrontInfo.value( name );
00361     return v.path;
00362 }
00363 
00364 
00365 // Check whether a card set is SVG
00366 bool isSVGFront( const QString& name )
00367 {
00368     return deckinfoStatic->svgFrontInfo.contains( name );
00369 }
00370 
00371 
00372 // Check whether a card deck is SVG
00373 bool isSVGBack( const QString& name )
00374 {
00375     return deckinfoStatic->svgBackInfo.contains( name );
00376 }
00377 
00378 QStringList frontNames()
00379 {
00380     return ( deckinfoStatic->svgFrontInfo.keys() + deckinfoStatic->pngFrontInfo.keys() );
00381 }
00382 
00383 QStringList backNames()
00384 {
00385     return ( deckinfoStatic->svgBackInfo.keys() + deckinfoStatic->pngBackInfo.keys() );
00386 }
00387 
00388 KCardThemeInfo frontInfo( const QString& name )
00389 {
00390     if ( deckinfoStatic->svgFrontInfo.contains( name ) )
00391         return deckinfoStatic->svgFrontInfo.value( name );
00392     if ( deckinfoStatic->pngFrontInfo.contains( name ) )
00393         return deckinfoStatic->pngFrontInfo.value( name );
00394     return KCardThemeInfo();
00395 }
00396 
00397 KCardThemeInfo backInfo( const QString& name )
00398 {
00399     if ( deckinfoStatic->svgFrontInfo.contains( name ) )
00400         return deckinfoStatic->svgBackInfo.value( name );
00401     if ( deckinfoStatic->pngBackInfo.contains( name ) )
00402         return deckinfoStatic->pngBackInfo.value( name );
00403     return KCardThemeInfo();
00404 }
00405 
00406 }
00407 

Generated on Sun Mar 16 08:02:52 2008 for Libkdegames by  doxygen 1.5.3