OSDN Git Service

kget: partially deal with TODO related to torrent files model
authorIvailo Monev <xakepa10@gmail.com>
Wed, 11 Aug 2021 16:00:18 +0000 (19:00 +0300)
committerIvailo Monev <xakepa10@gmail.com>
Wed, 11 Aug 2021 16:00:18 +0000 (19:00 +0300)
Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
kget/transfer-plugins/torrent/transferTorrent.cpp
kget/transfer-plugins/torrent/transferTorrent.h

index aaf45e6..0f6abb2 100644 (file)
@@ -33,7 +33,7 @@ TransferTorrent::TransferTorrent(TransferGroup* parent, TransferFactory* factory
                          Scheduler* scheduler, const KUrl &source, const KUrl &dest,
                          const QDomElement* e)
     : Transfer(parent, factory, scheduler, source, dest, e),
-    m_timerid(0), m_ltsession(nullptr)
+    m_timerid(0), m_ltsession(nullptr), m_filemodel(nullptr)
 {
     setCapabilities(Transfer::Cap_SpeedLimit | Transfer::Cap_Resuming);
 
@@ -185,17 +185,18 @@ bool TransferTorrent::isWorking() const
     return (m_timerid != 0);
 }
 
-// TODO: model for the files
 QList<KUrl> TransferTorrent::files() const
 {
     kDebug(5001) << "TransferTorrent::files";
 
     QList<KUrl> result;
 
-    const lt::file_storage ltstorage = m_lthandle.torrent_file()->files();
-    if (ltstorage.is_valid()) {
-        for (int i = 0; i < ltstorage.num_files(); i++) {
-            result.append(KUrl(ltstorage.file_path(i).c_str()));
+    if (m_lthandle.torrent_file()) {
+        const lt::file_storage ltstorage = m_lthandle.torrent_file()->files();
+        if (ltstorage.is_valid()) {
+            for (int i = 0; i < ltstorage.num_files(); i++) {
+                result.append(KUrl(ltstorage.file_path(i).c_str()));
+            }
         }
     }
 
@@ -203,6 +204,40 @@ QList<KUrl> TransferTorrent::files() const
     return result;
 }
 
+FileModel* TransferTorrent::fileModel()
+{
+    kDebug(5001) << "TransferTorrent::fileModel";
+
+    if (!m_filemodel) {
+        m_filemodel = new FileModel(TransferTorrent::files(), m_dest.upUrl(), this);
+
+        // TODO: disable downloading based on check state
+        // TODO: file state should not be based on global transfer state
+        if (m_lthandle.torrent_file()) {
+            const lt::file_storage ltstorage = m_lthandle.torrent_file()->files();
+            if (ltstorage.is_valid()) {
+                for (int i = 0; i < ltstorage.num_files(); i++) {
+                    const KUrl filepath = KUrl(ltstorage.file_path(i).c_str());
+
+                    const Qt::CheckState filestate = Qt::Checked;
+                    QModelIndex fileindex = m_filemodel->index(filepath, FileItem::File);
+                    m_filemodel->setData(fileindex, filestate, Qt::CheckStateRole);
+
+                    const Job::Status filestatus = Transfer::status();
+                    QModelIndex statusindex = m_filemodel->index(filepath, FileItem::Status);
+                    m_filemodel->setData(statusindex, filestatus);
+
+                    const qlonglong filesize = ltstorage.file_size(i);
+                    QModelIndex sizeindex = m_filemodel->index(filepath, FileItem::Size);
+                    m_filemodel->setData(sizeindex, filesize);
+                }
+            }
+        }
+    }
+
+    return m_filemodel;
+}
+
 void TransferTorrent::timerEvent(QTimerEvent *event)
 {
     if (event->timerId() != m_timerid) {
index 14df05f..6b891cd 100644 (file)
@@ -20,6 +20,7 @@
 #define TRANSFER_TORRENT_H
 
 #include "core/transfer.h"
+#include "core/filemodel.h"
 
 #include <QTimerEvent>
 
@@ -44,8 +45,9 @@ public:
     void stop() final;
     bool isStalled() const final;
     bool isWorking() const final;
-    // Transfer reimplementation
+    // Transfer reimplementations
     QList<KUrl> files() const final;
+    FileModel* fileModel() final;
 
 protected:
     // QObject reimplementation
@@ -57,6 +59,7 @@ private:
     int m_timerid;
     lt::session* m_ltsession;
     lt::torrent_handle m_lthandle;
+    FileModel* m_filemodel;
 };
 
 #endif // TRANSFER_TORRENT_H