Browse Source

Try to save images

Kandrashin Denis 13 years ago
parent
commit
766add920e
4 changed files with 64 additions and 20 deletions
  1. 43 8
      source/fb2save.cpp
  2. 1 1
      source/fb2save.h
  3. 15 7
      source/fb2temp.cpp
  4. 5 4
      source/fb2temp.hpp

+ 43 - 8
source/fb2save.cpp

@@ -7,6 +7,7 @@
 #include <QAbstractNetworkCache>
 #include <QAbstractNetworkCache>
 #include <QNetworkReply>
 #include <QNetworkReply>
 #include <QNetworkRequest>
 #include <QNetworkRequest>
+#include <QScopedPointer>
 
 
 //---------------------------------------------------------------------------
 //---------------------------------------------------------------------------
 //  Fb2SaveWriter
 //  Fb2SaveWriter
@@ -78,21 +79,55 @@ QByteArray Fb2SaveWriter::downloadFile(const QString &src)
     return reply->readAll();
     return reply->readAll();
 }
 }
 
 
+QString Fb2SaveWriter::newFileName(const QString &path)
+{
+    QFileInfo info(path);
+    QString name = info.fileName();
+    if (!m_view.files().exists(name) && !m_files.exists(name)) return name;
+    QString base = info.baseName();
+    QString suff = info.suffix();
+    for (int i = 1; ; i++) {
+        QString name = QString("%1(%2).%3").arg(base).arg(i).arg(suff);
+        if (m_view.files().exists(name)) continue;
+        if (m_files.exists(name)) continue;
+        return name;
+    }
+}
+
 QString Fb2SaveWriter::getFileName(const QString &path)
 QString Fb2SaveWriter::getFileName(const QString &path)
 {
 {
     QString hash = m_view.files().hash(path);
     QString hash = m_view.files().hash(path);
-    if (hash.isEmpty()) return QString();
-
-    QString name = m_view.files().name(hash);
-    if (name.isEmpty()) return QString();
-
-    m_names.append(name);
-    return name;
+    if (hash.isEmpty()) {
+        QUrl url(path);
+        QNetworkRequest request(url);
+        QNetworkAccessManager * network = m_view.page()->networkAccessManager();
+        QScopedPointer<QNetworkReply> reply(network->get(request));
+        if (reply.isNull()) return QString();
+
+        QEventLoop loop;
+        QObject::connect(reply.data(), SIGNAL(readyRead()), &loop, SLOT(quit()));
+        loop.exec();
+
+        QByteArray data = reply->readAll();
+
+        hash = Fb2TemporaryFile::md5(data);
+        QString name = newFileName(url.path());
+        m_files.set(name, data, hash);
+        m_names.append(name);
+        return name;
+    } else {
+        QString name = m_view.files().name(hash);
+        if (name.isEmpty()) return QString();
+        if (m_names.indexOf(name) == -1) m_names.append(name);
+        return name;
+    }
 }
 }
 
 
 QString Fb2SaveWriter::getFileData(const QString &name)
 QString Fb2SaveWriter::getFileData(const QString &name)
 {
 {
-    return m_view.files().data(name);
+    QString data = m_view.files().data(name);
+    if (data.isEmpty()) data = m_files.data(name);
+    return data;
 }
 }
 
 
 void Fb2SaveWriter::writeFiles()
 void Fb2SaveWriter::writeFiles()

+ 1 - 1
source/fb2save.h

@@ -30,7 +30,7 @@ private:
     void Init();
     void Init();
     QByteArray downloadFile(const QString &src);
     QByteArray downloadFile(const QString &src);
     QString getFileData(const QString &name);
     QString getFileData(const QString &name);
-    QString newFileName();
+    QString newFileName(const QString &path);
 private:
 private:
     QList<int> *m_folds;
     QList<int> *m_folds;
     Fb2WebView &m_view;
     Fb2WebView &m_view;

+ 15 - 7
source/fb2temp.cpp

@@ -6,22 +6,28 @@
 //  Fb2TemporaryFile
 //  Fb2TemporaryFile
 //---------------------------------------------------------------------------
 //---------------------------------------------------------------------------
 
 
-Fb2TemporaryFile::Fb2TemporaryFile(const QString &name)
+Fb2TemporaryFile::Fb2TemporaryFile(const QString &name, const QString &hash)
     : QTemporaryFile()
     : QTemporaryFile()
     , m_name(name)
     , m_name(name)
+    , m_hash(hash)
 {
 {
     open();
     open();
 }
 }
 
 
 qint64 Fb2TemporaryFile::write(const QByteArray &data)
 qint64 Fb2TemporaryFile::write(const QByteArray &data)
 {
 {
-    m_hash = QCryptographicHash::hash(data, QCryptographicHash::Md5).toBase64();
+    if (m_hash.isEmpty()) m_hash = md5(data);
     qint64 size = QTemporaryFile::write(data);
     qint64 size = QTemporaryFile::write(data);
     close();
     close();
     open();
     open();
     return size;
     return size;
 }
 }
 
 
+QString Fb2TemporaryFile::md5(const QByteArray &data)
+{
+    return QCryptographicHash::hash(data, QCryptographicHash::Md5).toBase64();
+}
+
 //---------------------------------------------------------------------------
 //---------------------------------------------------------------------------
 //  Fb2TemporaryList
 //  Fb2TemporaryList
 //---------------------------------------------------------------------------
 //---------------------------------------------------------------------------
@@ -36,21 +42,23 @@ Fb2TemporaryList::~Fb2TemporaryList()
     while (it.hasNext()) delete it.next();
     while (it.hasNext()) delete it.next();
 }
 }
 
 
-Fb2TemporaryFile & Fb2TemporaryList::get(const QString &name)
+Fb2TemporaryFile & Fb2TemporaryList::get(const QString &name, const QString &hash)
 {
 {
     Fb2TemporaryIterator it(*this);
     Fb2TemporaryIterator it(*this);
     while (it.hasNext()) {
     while (it.hasNext()) {
         Fb2TemporaryFile *file = it.next();
         Fb2TemporaryFile *file = it.next();
         if (file->name() == name) return *file;
         if (file->name() == name) return *file;
     }
     }
-    Fb2TemporaryFile * file = new Fb2TemporaryFile(name);
+    Fb2TemporaryFile * file = new Fb2TemporaryFile(name, hash);
     append(file);
     append(file);
     return *file;
     return *file;
 }
 }
 
 
-void Fb2TemporaryList::set(const QString &name, const QByteArray &data)
+QString Fb2TemporaryList::set(const QString &name, const QByteArray &data, const QString &hash)
 {
 {
-    get(name).write(data);
+    Fb2TemporaryFile & file = get(name, hash);
+    file.write(data);
+    return file.hash();
 }
 }
 
 
 QString Fb2TemporaryList::hash(const QString &path) const
 QString Fb2TemporaryList::hash(const QString &path) const
@@ -83,7 +91,7 @@ QString Fb2TemporaryList::data(const QString &name) const
     return QString();
     return QString();
 }
 }
 
 
-bool Fb2TemporaryList::exists(const QString &name)
+bool Fb2TemporaryList::exists(const QString &name) const
 {
 {
     Fb2TemporaryIterator it(*this);
     Fb2TemporaryIterator it(*this);
     while (it.hasNext()) {
     while (it.hasNext()) {

+ 5 - 4
source/fb2temp.hpp

@@ -10,10 +10,11 @@ class Fb2TemporaryFile : public QTemporaryFile
 {
 {
     Q_OBJECT
     Q_OBJECT
 public:
 public:
-    explicit Fb2TemporaryFile(const QString &name);
+    explicit Fb2TemporaryFile(const QString &name, const QString &hash = QString());
     inline qint64 write(const QByteArray &data);
     inline qint64 write(const QByteArray &data);
     const QString & hash() const { return m_hash; }
     const QString & hash() const { return m_hash; }
     const QString & name() const { return m_name; }
     const QString & name() const { return m_name; }
+    static QString md5(const QByteArray &data);
 private:
 private:
     const QString m_name;
     const QString m_name;
     QString m_hash;
     QString m_hash;
@@ -25,9 +26,9 @@ public:
     explicit Fb2TemporaryList();
     explicit Fb2TemporaryList();
     virtual ~Fb2TemporaryList();
     virtual ~Fb2TemporaryList();
 
 
-    bool exists(const QString &name);
-    Fb2TemporaryFile & get(const QString &name);
-    void set(const QString &name, const QByteArray &data);
+    bool exists(const QString &name) const;
+    Fb2TemporaryFile & get(const QString &name, const QString &hash = QString());
+    QString set(const QString &name, const QByteArray &data, const QString &hash = QString());
 
 
     QString hash(const QString &path) const;
     QString hash(const QString &path) const;
     QString name(const QString &hash) const;
     QString name(const QString &hash) const;