25 #include <kstandarddirs.h>
26 #include <kdirlister.h>
33 #include <kmimetype.h>
34 #include <kio/netaccess.h>
35 #include <ksharedconfig.h>
36 #include <kconfiggroup.h>
40 class ChatWindowStyleManager::Private
51 styleDirLister->deleteLater();
54 qDeleteAll(stylePool);
57 KDirLister *styleDirLister;
58 QStringList availableStyles;
61 QHash<QString, ChatWindowStyle*> stylePool;
63 QStack<KUrl> styleDirs;
72 ChatWindowStyleManager::ChatWindowStyleManager(
QObject *parent)
73 :
QObject(parent), d(new Private())
88 KStandardDirs::locateLocal(
"appdata", QLatin1String(
"styles/" ) );
90 QStringList chatStyles = KGlobal::dirs()->findDirs(
"appdata", QLatin1String(
"styles" ) );
91 foreach(
const QString &styleDir, chatStyles)
93 kDebug(14000) << styleDir;
94 d->styleDirs.push( KUrl(styleDir) );
97 d->styleDirLister =
new KDirLister(
this);
98 d->styleDirLister->setDirOnlyMode(
true);
100 connect(d->styleDirLister, SIGNAL(newItems(KFileItemList)),
this, SLOT(slotNewStyles(KFileItemList)));
101 connect(d->styleDirLister, SIGNAL(completed()),
this, SLOT(slotDirectoryFinished()));
103 if( !d->styleDirs.isEmpty() )
104 d->styleDirLister->openUrl(d->styleDirs.pop(), KDirLister::Keep);
109 return d->availableStyles;
114 QString localStyleDir;
115 QStringList chatStyles = KGlobal::dirs()->findDirs(
"appdata", QLatin1String(
"styles" ) );
117 foreach(
const QString& styleDir, chatStyles)
119 if(QFileInfo(styleDir).isWritable())
121 localStyleDir = styleDir;
125 if( localStyleDir.isEmpty() )
130 KArchiveEntry *currentEntry = 0L;
131 KArchiveDirectory* currentDir = 0L;
132 KArchive *archive = 0L;
135 QString currentBundleMimeType = KMimeType::findByPath(styleBundlePath, 0,
false)->name();
136 if(currentBundleMimeType ==
"application/zip")
138 archive =
new KZip(styleBundlePath);
140 else if( currentBundleMimeType ==
"application/x-compressed-tar" || currentBundleMimeType ==
"application/x-bzip-compressed-tar" || currentBundleMimeType ==
"application/x-gzip" || currentBundleMimeType ==
"application/x-bzip" )
142 archive =
new KTar(styleBundlePath);
149 if ( !archive->open(QIODevice::ReadOnly) )
156 const KArchiveDirectory* rootDir = archive->directory();
165 const QStringList entries = rootDir->entries();
167 QStringList::ConstIterator entriesIt;
168 for(entriesIt = entries.begin(); entriesIt != entries.end(); ++entriesIt)
170 currentEntry =
const_cast<KArchiveEntry*
>(rootDir->entry(*entriesIt));
172 if (currentEntry->isDirectory())
174 currentDir =
dynamic_cast<KArchiveDirectory*
>( currentEntry );
177 if( currentDir->entry(QString::fromUtf8(
"Contents")) )
182 if( currentDir->entry(QString::fromUtf8(
"Contents/Resources")) )
187 if( currentDir->entry(QString::fromUtf8(
"Contents/Resources/Incoming")) )
192 if( currentDir->entry(QString::fromUtf8(
"Contents/Resources/Outgoing")) )
197 if( currentDir->entry(QString::fromUtf8(
"Contents/Resources/main.css")) )
202 if( currentDir->entry(QString::fromUtf8(
"Contents/Resources/Footer.html")) )
207 if( currentDir->entry(QString::fromUtf8(
"Contents/Resources/Status.html")) )
212 if( currentDir->entry(QString::fromUtf8(
"Contents/Resources/Header.html")) )
217 if( currentDir->entry(QString::fromUtf8(
"Contents/Resources/Incoming/Content.html")) )
222 if( currentDir->entry(QString::fromUtf8(
"Contents/Resources/Outgoing/Content.html")) )
234 bool installOk =
false;
235 for(entriesIt = entries.begin(); entriesIt != entries.end(); ++entriesIt)
237 currentEntry =
const_cast<KArchiveEntry*
>(rootDir->entry(*entriesIt));
238 if(currentEntry && currentEntry->isDirectory())
241 if(currentEntry->name() == QString::fromUtf8(
"__MACOSX"))
247 currentDir =
dynamic_cast<KArchiveDirectory*
>(currentEntry);
250 currentDir->copyTo(localStyleDir + currentDir->name());
284 kDebug(14000) << styleName;
286 int foundStyleIdx = d->availableStyles.indexOf(styleName);
288 if(foundStyleIdx != -1)
290 d->availableStyles.removeAt(foundStyleIdx);
293 if( d->stylePool.contains(styleName) )
296 d->stylePool.remove(styleName);
300 QStringList styleDirs = KGlobal::dirs()->findDirs(
"appdata", QString(
"styles/%1").arg(styleName));
301 if(styleDirs.isEmpty())
303 kDebug(14000) <<
"Failed to find style" << styleName;
309 foreach(
const QString& stylePath, styleDirs )
311 KUrl urlStyle(stylePath);
313 if(KIO::NetAccess::del( urlStyle, 0 ))
316 return numDeleted == styleDirs.count();
331 kDebug(14000) <<
"Trying default style";
337 kDebug(14000) <<
"Trying first valid style";
339 foreach (
const QString& name, d->availableStyles )
346 kDebug(14000) <<
"Valid style not found!";
352 if( d->stylePool.contains(styleName) )
354 kDebug(14000) << styleName <<
" was on the pool";
359 KConfigGroup config(KGlobal::config(),
"KopeteStyleDebug");
360 bool disableCache = config.readEntry(
"disableStyleCache",
false);
363 d->stylePool[styleName]->reload();
366 return d->stylePool[styleName];
373 kDebug(14000) << styleName <<
" is invalid style!";
378 d->stylePool.insert(styleName, style);
379 kDebug(14000) << styleName <<
" is just created";
384 void ChatWindowStyleManager::slotNewStyles(
const KFileItemList &dirList)
386 foreach(
const KFileItem &item, dirList)
389 if( !item.url().fileName().contains(QString::fromUtf8(
"data")) )
391 kDebug(14000) <<
"Listing: " << item.url().fileName();
394 QString styleName = item.url().fileName();
395 if( d->stylePool.contains(styleName) )
397 kDebug(14000) <<
"Updating style: " << styleName;
399 d->stylePool[styleName]->reload();
402 if( d->availableStyles.indexOf(styleName) == -1 )
403 d->availableStyles.append(styleName);
408 d->availableStyles.append(styleName);
414 void ChatWindowStyleManager::slotDirectoryFinished()
417 if( !d->styleDirs.isEmpty() )
419 kDebug(14000) <<
"Starting another directory.";
420 d->styleDirLister->openUrl(d->styleDirs.pop(), KDirLister::Keep);
428 #include "kopetechatwindowstylemanager.moc"
This class represent a single chat window style.
static ChatWindowStyleManager * self()
Singleton access to this class.
void loadStyles()
List all availables styles.
int installStyle(const QString &styleBundlePath)
Install a new style into user style directory Note that you must pass a path to a archive...
bool removeStyle(const QString &styleName)
Remove a style from user style directory.
bool isValid() const
Checks if the style is valid.
ChatWindowStyle * getStyleFromPool(const QString &styleName)
Get a instance of a ChatWindowStyle from the pool.
void loadStylesFinished()
This signal is emitted when all styles finished to list.
QStringList getAvailableStyles() const
Get all available styles.
ChatWindowStyle * getValidStyleFromPool(const QString &styleName)
Get a instance of a ChatWindowStyle from the pool.
~ChatWindowStyleManager()
Destructor.
Sigleton class that handle Chat Window styles.