fb2temp.cpp 11 KB

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