mycache.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #include "mycache.h"
  2. #include <QDebug>
  3. #include <QBuffer>
  4. #include <QImageReader>
  5. #include <QFile>
  6. MyCache::MyCache(QObject *parent) : StoreVCard(parent)
  7. {
  8. appName = "meegim";
  9. pathMeegIMHome = QDir::homePath() + QDir::separator() + ".config" + QDir::separator() + appName;
  10. pathMeegIMCache = pathMeegIMHome + QDir::separator() + QString("cache");
  11. this->setCachePath( pathMeegIMCache );
  12. }
  13. bool MyCache::createHomeDir() const
  14. {
  15. bool retValue = false;
  16. QDir hD( pathMeegIMHome );
  17. if( hD.exists() == false ) {
  18. if( ! hD.mkdir(pathMeegIMHome) ) {
  19. qCritical() << "Error: Cannot create home directory !";
  20. return retValue;
  21. }
  22. }
  23. QDir cD( pathMeegIMCache );
  24. if( cD.exists() == false ) {
  25. if( ! cD.mkdir(pathMeegIMCache) ) {
  26. qCritical() << "Error: Cannot create cache directory !";
  27. return retValue;
  28. }
  29. }
  30. retValue = true;
  31. return retValue;
  32. }
  33. bool MyCache::addCacheJid(const QString &jid)
  34. {
  35. if( this->existsCacheJid(jid) ) {
  36. return true;
  37. }
  38. QString jidCache = pathMeegIMCache + QDir::separator() + jid;
  39. QDir jD( jidCache );
  40. if( ! jD.mkdir(jidCache) ) {
  41. qCritical() << "Error: Cannot create cache directory: " << jid;
  42. return false;
  43. }
  44. return true;
  45. }
  46. bool MyCache::setAvatarCache(const QString &jid, const QByteArray &avatar) const
  47. {
  48. if( !(this->existsCacheJid(jid)) ) {
  49. return false;
  50. }
  51. QBuffer buffer;
  52. buffer.setData( avatar );
  53. buffer.open(QIODevice::ReadOnly);
  54. QImageReader imageReader(&buffer);
  55. QImage avatarImage = imageReader.read();
  56. QString avatarJid = pathMeegIMCache + QDir::separator() + jid + QDir::separator() + QString("avatar.png");
  57. if( avatarImage.save(avatarJid) ) {
  58. return true;
  59. }
  60. return false;
  61. }
  62. QString MyCache::getAvatarCache(const QString &jid) const
  63. {
  64. QString avatarJid = pathMeegIMCache + QDir::separator() + jid + QDir::separator() + QString("avatar.png");
  65. if( QFile::exists(avatarJid) ) {
  66. return avatarJid;
  67. }
  68. return QString("");
  69. }