fb2temp.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. #include "fb2temp.hpp"
  2. #include <QAbstractListModel>
  3. #include <QBuffer>
  4. #include <QCryptographicHash>
  5. #include <QFileInfo>
  6. #include <QImageReader>
  7. #include <QUrl>
  8. #include <QVBoxLayout>
  9. #include <QtDebug>
  10. #include "fb2text.hpp"
  11. //---------------------------------------------------------------------------
  12. // FbTemporaryFile
  13. //---------------------------------------------------------------------------
  14. FbTemporaryFile::FbTemporaryFile(const QString &name)
  15. : QTemporaryFile()
  16. , m_name(name)
  17. , m_size(0)
  18. {
  19. }
  20. qint64 FbTemporaryFile::write(QByteArray &data)
  21. {
  22. open();
  23. if (m_hash.isEmpty()) m_hash = md5(data);
  24. m_size = QTemporaryFile::write(data);
  25. QBuffer buffer(&data);
  26. buffer.open(QIODevice::ReadOnly);
  27. m_type = QImageReader::imageFormat(&buffer);
  28. close();
  29. return m_size;
  30. }
  31. QString FbTemporaryFile::md5(const QByteArray &data)
  32. {
  33. return QCryptographicHash::hash(data, QCryptographicHash::Md5).toBase64();
  34. }
  35. QByteArray FbTemporaryFile::data()
  36. {
  37. open();
  38. QByteArray data = readAll();
  39. close();
  40. return data;
  41. }
  42. //---------------------------------------------------------------------------
  43. // FbTemporaryList
  44. //---------------------------------------------------------------------------
  45. FbTemporaryList::FbTemporaryList()
  46. {
  47. }
  48. FbTemporaryList::~FbTemporaryList()
  49. {
  50. FbTemporaryIterator it(*this);
  51. while (it.hasNext()) delete it.next();
  52. }
  53. QString FbTemporaryList::add(const QString &path, QByteArray &data)
  54. {
  55. QString hash = FbTemporaryFile::md5(data);
  56. QString name = this->name(hash);
  57. if (name.isEmpty()) {
  58. name = newName(path);
  59. FbTemporaryFile * temp = new FbTemporaryFile(name);
  60. temp->setHash(hash);
  61. temp->write(data);
  62. append(temp);
  63. }
  64. return name;
  65. }
  66. QString FbTemporaryList::newName(const QString &path)
  67. {
  68. QFileInfo info(path);
  69. QString name = info.fileName();
  70. if (!exists(name)) return name;
  71. QString base = info.baseName();
  72. QString suff = info.suffix();
  73. for (int i = 1; ; i++) {
  74. QString name = QString("%1(%2).%3").arg(base).arg(i).arg(suff);
  75. if (exists(name)) continue;
  76. return name;
  77. }
  78. }
  79. FbTemporaryFile * FbTemporaryList::get(const QString &name) const
  80. {
  81. FbTemporaryIterator it(*this);
  82. while (it.hasNext()) {
  83. FbTemporaryFile * file = it.next();
  84. if (file->name() == name) return file;
  85. }
  86. return NULL;
  87. }
  88. QByteArray FbTemporaryList::data(const QString &name) const
  89. {
  90. FbTemporaryIterator it(*this);
  91. while (it.hasNext()) {
  92. FbTemporaryFile *file = it.next();
  93. if (file->name() == name) return file->data();
  94. }
  95. return QByteArray();
  96. }
  97. const QString & FbTemporaryList::set(const QString &name, QByteArray &data, const QString &hash)
  98. {
  99. FbTemporaryFile * file = get(name);
  100. if (!file) append(file = new FbTemporaryFile(name));
  101. file->setHash(hash);
  102. file->write(data);
  103. return file->hash();
  104. }
  105. QString FbTemporaryList::name(const QString &hash) const
  106. {
  107. FbTemporaryIterator it(*this);
  108. while (it.hasNext()) {
  109. FbTemporaryFile *file = it.next();
  110. if (file->hash() == hash) return file->name();
  111. }
  112. return QString();
  113. }
  114. bool FbTemporaryList::exists(const QString &name) const
  115. {
  116. FbTemporaryIterator it(*this);
  117. while (it.hasNext()) {
  118. FbTemporaryFile *file = it.next();
  119. if (file->name() == name) return true;
  120. }
  121. return false;
  122. }
  123. #if 0
  124. //---------------------------------------------------------------------------
  125. // FbNetworkDiskCache
  126. //---------------------------------------------------------------------------
  127. QNetworkCacheMetaData FbNetworkDiskCache::metaData(const QUrl &url)
  128. {
  129. qCritical() << url.toString();
  130. return QNetworkDiskCache::metaData(url);
  131. }
  132. QIODevice * FbNetworkDiskCache::data(const QUrl &url)
  133. {
  134. qCritical() << url.toString();
  135. return QNetworkDiskCache::data(url);
  136. }
  137. #endif
  138. //---------------------------------------------------------------------------
  139. // FbImageReply
  140. //---------------------------------------------------------------------------
  141. FbImageReply::FbImageReply(QNetworkAccessManager::Operation op, const QNetworkRequest &request, const QByteArray &data)
  142. : QNetworkReply()
  143. , content(data)
  144. , offset(0)
  145. {
  146. setOperation(op);
  147. setRequest(request);
  148. setUrl(request.url());
  149. open(ReadOnly | Unbuffered);
  150. setHeader(QNetworkRequest::ContentLengthHeader, QVariant(content.size()));
  151. setAttribute(QNetworkRequest::CacheSaveControlAttribute, QVariant(false));
  152. QMetaObject::invokeMethod(this, "readyRead", Qt::QueuedConnection);
  153. QMetaObject::invokeMethod(this, "finished", Qt::QueuedConnection);
  154. }
  155. qint64 FbImageReply::readData(char *data, qint64 maxSize)
  156. {
  157. if (offset >= content.size()) return -1;
  158. QMetaObject::invokeMethod(this, "readyRead", Qt::QueuedConnection);
  159. qint64 number = qMin(maxSize, content.size() - offset);
  160. memcpy(data, content.constData() + offset, number);
  161. offset += number;
  162. return number;
  163. }
  164. //---------------------------------------------------------------------------
  165. // FbNetworkAccessManager
  166. //
  167. // http://doc.trolltech.com/qq/32/qq32-webkit-protocols.html
  168. //---------------------------------------------------------------------------
  169. FbNetworkAccessManager::FbNetworkAccessManager(QObject *parent)
  170. : QNetworkAccessManager(parent)
  171. {
  172. // QWebSettings::clearMemoryCaches();
  173. }
  174. QNetworkReply * FbNetworkAccessManager::createRequest(Operation op, const QNetworkRequest &request, QIODevice *outgoingData)
  175. {
  176. const QUrl &url = request.url();
  177. const QString path = url.path();
  178. if (url.scheme() == "fb2" && path == m_path) return imageRequest(op, request);
  179. return QNetworkAccessManager::createRequest(op, request, outgoingData);
  180. }
  181. QNetworkReply * FbNetworkAccessManager::imageRequest(Operation op, const QNetworkRequest &request)
  182. {
  183. QString name = request.url().fragment();
  184. QByteArray data = m_files.data(name);
  185. return new FbImageReply(op, request, data);
  186. }
  187. QVariant FbNetworkAccessManager::info(int row, int col) const
  188. {
  189. if (0 <= row && row < count()) {
  190. FbTemporaryFile *file = m_files[row];
  191. switch (col) {
  192. case 0: return file->name();
  193. case 1: return file->type();
  194. case 2: return file->size();
  195. }
  196. return m_files[row]->name();
  197. }
  198. return QVariant();
  199. }
  200. QByteArray FbNetworkAccessManager::data(int index) const
  201. {
  202. if (0 <= index && index < count()) {
  203. return m_files[index]->data();
  204. }
  205. return QByteArray();
  206. }
  207. void FbNetworkAccessManager::data(QString name, QByteArray data)
  208. {
  209. m_files.set(name, data);
  210. }
  211. //---------------------------------------------------------------------------
  212. // FbListModel
  213. //---------------------------------------------------------------------------
  214. FbListModel::FbListModel(FbNetworkAccessManager &files, QObject *parent)
  215. : QAbstractListModel(parent)
  216. , m_files(files)
  217. {
  218. }
  219. int FbListModel::columnCount(const QModelIndex &parent) const
  220. {
  221. Q_UNUSED(parent);
  222. return 3;
  223. }
  224. int FbListModel::rowCount(const QModelIndex &parent) const
  225. {
  226. if (parent.isValid()) return 0;
  227. return m_files.count();
  228. }
  229. QVariant FbListModel::headerData(int section, Qt::Orientation orientation, int role) const
  230. {
  231. if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
  232. switch (section) {
  233. case 0: return tr("File name");
  234. case 1: return tr("Type");
  235. case 2: return tr("Size");
  236. }
  237. }
  238. return QVariant();
  239. }
  240. QVariant FbListModel::data(const QModelIndex &index, int role) const
  241. {
  242. if (index.isValid()) {
  243. switch (role) {
  244. case Qt::DisplayRole: {
  245. return m_files.info(index.row(), index.column());
  246. } break;
  247. case Qt::TextAlignmentRole: {
  248. switch (index.column()) {
  249. case 2: return Qt::AlignRight;
  250. default: return Qt::AlignLeft;
  251. }
  252. }
  253. }
  254. }
  255. return QVariant();
  256. }
  257. //---------------------------------------------------------------------------
  258. // FbListView
  259. //---------------------------------------------------------------------------
  260. #include <QSplitter>
  261. #include <QScrollArea>
  262. FbListView::FbListView(FbNetworkAccessManager *files, QWidget *parent)
  263. : QTreeView(parent)
  264. , m_files(*files)
  265. {
  266. m_label = new QLabel(this);
  267. m_label->setScaledContents(true);
  268. }
  269. void FbListView::currentChanged(const QModelIndex &current, const QModelIndex &previous)
  270. {
  271. QTreeView::currentChanged(current, previous);
  272. int row = current.row();
  273. if (0 <= row && row < model()->temp().count()) {
  274. QPixmap pixmap;
  275. pixmap.loadFromData(model()->temp().data(row));
  276. m_label->setPixmap(pixmap);
  277. m_label->resize(pixmap.size());
  278. }
  279. }
  280. FbListModel * FbListView::model() const
  281. {
  282. return qobject_cast<FbListModel*>(QTreeView::model());
  283. }
  284. //---------------------------------------------------------------------------
  285. // FbListWidget
  286. //---------------------------------------------------------------------------
  287. FbListWidget::FbListWidget(FbTextEdit *view, QWidget* parent)
  288. : QWidget(parent)
  289. , m_view(*view)
  290. {
  291. QVBoxLayout *layout = new QVBoxLayout(this);
  292. layout->setSpacing(0);
  293. layout->setContentsMargins(0, 0, 0, 0);
  294. QSplitter *splitter = new QSplitter(Qt::Vertical, this);
  295. m_list = new FbListView(view->files(), splitter);
  296. splitter->addWidget(m_list);
  297. QScrollArea *scroll = new QScrollArea(splitter);
  298. scroll->setWidget(m_list->label());
  299. splitter->addWidget(scroll);
  300. splitter->setSizes(QList<int>() << 1 << 1);
  301. layout->addWidget(splitter);
  302. connect(&m_view, SIGNAL(loadFinished(bool)), SLOT(loadFinished(bool)));
  303. loadFinished(true);
  304. // m_tool = new QToolBar(this);
  305. // layout->addWidget(m_tool);
  306. }
  307. void FbListWidget::loadFinished(bool ok)
  308. {
  309. m_list->setModel(new FbListModel(*m_view.files(), this));
  310. m_list->label()->clear();
  311. m_list->reset();
  312. m_list->resizeColumnToContents(1);
  313. m_list->resizeColumnToContents(2);
  314. }