fb2temp.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include "fb2temp.hpp"
  2. #include <QCryptographicHash>
  3. //---------------------------------------------------------------------------
  4. // Fb2TemporaryFile
  5. //---------------------------------------------------------------------------
  6. Fb2TemporaryFile::Fb2TemporaryFile(const QString &name)
  7. : QTemporaryFile()
  8. , m_name(name)
  9. {
  10. open();
  11. }
  12. qint64 Fb2TemporaryFile::write(const QByteArray &data)
  13. {
  14. m_hash = QCryptographicHash::hash(data, QCryptographicHash::Md5).toBase64();
  15. qint64 size = QTemporaryFile::write(data);
  16. close();
  17. open();
  18. return size;
  19. }
  20. //---------------------------------------------------------------------------
  21. // Fb2TemporaryList
  22. //---------------------------------------------------------------------------
  23. Fb2TemporaryList::Fb2TemporaryList()
  24. {
  25. }
  26. Fb2TemporaryList::~Fb2TemporaryList()
  27. {
  28. Fb2TemporaryIterator it(*this);
  29. while (it.hasNext()) delete it.next();
  30. }
  31. Fb2TemporaryFile & Fb2TemporaryList::get(const QString &name)
  32. {
  33. Fb2TemporaryIterator it(*this);
  34. while (it.hasNext()) {
  35. Fb2TemporaryFile *file = it.next();
  36. if (file->name() == name) return *file;
  37. }
  38. Fb2TemporaryFile * file = new Fb2TemporaryFile(name);
  39. append(file);
  40. return *file;
  41. }
  42. void Fb2TemporaryList::set(const QString &name, const QByteArray &data)
  43. {
  44. get(name).write(data);
  45. }
  46. QString Fb2TemporaryList::hash(const QString &path) const
  47. {
  48. Fb2TemporaryIterator it(*this);
  49. while (it.hasNext()) {
  50. Fb2TemporaryFile *file = it.next();
  51. if (file->fileName() == path) return file->hash();
  52. }
  53. return QString();
  54. }
  55. QString Fb2TemporaryList::name(const QString &hash) const
  56. {
  57. Fb2TemporaryIterator it(*this);
  58. while (it.hasNext()) {
  59. Fb2TemporaryFile *file = it.next();
  60. if (file->hash() == hash) return file->name();
  61. }
  62. return QString();
  63. }
  64. QString Fb2TemporaryList::data(const QString &name) const
  65. {
  66. Fb2TemporaryIterator it(*this);
  67. while (it.hasNext()) {
  68. Fb2TemporaryFile *file = it.next();
  69. if (file->name() == name) return file->readAll().toBase64();
  70. }
  71. return QString();
  72. }
  73. bool Fb2TemporaryList::exists(const QString &name)
  74. {
  75. Fb2TemporaryIterator it(*this);
  76. while (it.hasNext()) {
  77. Fb2TemporaryFile *file = it.next();
  78. if (file->name() == name) return true;
  79. }
  80. return false;
  81. }