1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- #include "mycache.h"
- #include <QDebug>
- #include <QBuffer>
- #include <QImageReader>
- #include <QFile>
- MyCache::MyCache(QObject *parent) : StoreVCard(parent)
- {
- appName = "meegim";
- pathMeegIMHome = QDir::homePath() + QDir::separator() + ".config" + QDir::separator() + appName;
- pathMeegIMCache = pathMeegIMHome + QDir::separator() + QString("cache");
- this->setCachePath( pathMeegIMCache );
- }
- bool MyCache::createHomeDir() const
- {
- bool retValue = false;
- QDir hD( pathMeegIMHome );
- if( hD.exists() == false ) {
- if( ! hD.mkdir(pathMeegIMHome) ) {
- qCritical() << "Error: Cannot create home directory !";
- return retValue;
- }
- }
- QDir cD( pathMeegIMCache );
- if( cD.exists() == false ) {
- if( ! cD.mkdir(pathMeegIMCache) ) {
- qCritical() << "Error: Cannot create cache directory !";
- return retValue;
- }
- }
- retValue = true;
- return retValue;
- }
- bool MyCache::addCacheJid(const QString &jid)
- {
- if( this->existsCacheJid(jid) ) {
- return true;
- }
- QString jidCache = pathMeegIMCache + QDir::separator() + jid;
- QDir jD( jidCache );
- if( ! jD.mkdir(jidCache) ) {
- qCritical() << "Error: Cannot create cache directory: " << jid;
- return false;
- }
- return true;
- }
- bool MyCache::setAvatarCache(const QString &jid, const QByteArray &avatar) const
- {
- if( !(this->existsCacheJid(jid)) ) {
- return false;
- }
- QBuffer buffer;
- buffer.setData( avatar );
- buffer.open(QIODevice::ReadOnly);
- QImageReader imageReader(&buffer);
- QImage avatarImage = imageReader.read();
- QString avatarJid = pathMeegIMCache + QDir::separator() + jid + QDir::separator() + QString("avatar.png");
- if( avatarImage.save(avatarJid) ) {
- return true;
- }
- return false;
- }
- QString MyCache::getAvatarCache(const QString &jid) const
- {
- QString avatarJid = pathMeegIMCache + QDir::separator() + jid + QDir::separator() + QString("avatar.png");
- if( QFile::exists(avatarJid) ) {
- return avatarJid;
- }
- return QString("");
- }
|