1
0

fb2temp.cpp 10 KB

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