From 3282e1bcc922c140c32ad356b3e1a7392adf9a0b Mon Sep 17 00:00:00 2001 From: Christophe Dumez Date: Sun, 2 Nov 2008 13:19:27 +0000 Subject: [PATCH] - Save and load session state to remember it over different sessions --- src/GUI.cpp | 3 --- src/bittorrent.cpp | 28 ++++++++++++++++++++++++++-- src/bittorrent.h | 2 ++ 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/GUI.cpp b/src/GUI.cpp index f406743f4..f129c1c2b 100644 --- a/src/GUI.cpp +++ b/src/GUI.cpp @@ -642,9 +642,6 @@ void GUI::closeEvent(QCloseEvent *e) { } // Save window size, columns size writeSettings(); - // Do some BT related saving - BTSession->saveDHTEntry(); - BTSession->saveFastResumeData(); // Accept exit e->accept(); qApp->exit(); diff --git a/src/bittorrent.cpp b/src/bittorrent.cpp index 0334554ee..f7a34c4d4 100644 --- a/src/bittorrent.cpp +++ b/src/bittorrent.cpp @@ -56,6 +56,8 @@ bittorrent::bittorrent() : timerScan(0), DHTEnabled(false), preAllocateAll(false // Set severity level of libtorrent session //s->set_alert_mask(alert::all_categories & ~alert::progress_notification); s->set_alert_mask(alert::error_notification | alert::peer_notification | alert::port_mapping_notification | alert::storage_notification | alert::tracker_notification | alert::status_notification | alert::ip_block_notification); + // Load previous state + loadSessionState(); // Enabling metadata plugin s->add_extension(&create_metadata_plugin); timerAlerts = new QTimer(); @@ -76,15 +78,19 @@ bittorrent::bittorrent() : timerScan(0), DHTEnabled(false), preAllocateAll(false // Main destructor bittorrent::~bittorrent() { qDebug("BTSession deletion"); + // Do some BT related saving + saveDHTEntry(); + saveSessionState(); + saveFastResumeData(); // Set Session settings - session_settings ss; + /*session_settings ss; ss.tracker_receive_timeout = 1; ss.stop_tracker_timeout = 1; ss.tracker_completion_timeout = 1; ss.piece_timeout = 1; ss.peer_timeout = 1; ss.urlseed_timeout = 1; - s->set_settings(ss); + s->set_settings(ss);*/ // Disable directory scanning disableDirectoryScanning(); // Delete our objects @@ -710,6 +716,24 @@ void bittorrent::enableLSD(bool b) { } } +void bittorrent::loadSessionState() { + boost::filesystem::ifstream ses_state_file((misc::qBittorrentPath()+QString::fromUtf8("ses_state")).toUtf8().data() + , std::ios_base::binary); + ses_state_file.unsetf(std::ios_base::skipws); + s->load_state(bdecode( + std::istream_iterator(ses_state_file) + , std::istream_iterator())); +} + +void bittorrent::saveSessionState() { + qDebug("Saving session state to disk..."); + entry session_state = s->state(); + boost::filesystem::ofstream out((misc::qBittorrentPath()+QString::fromUtf8("ses_state")).toUtf8().data() + , std::ios_base::binary); + out.unsetf(std::ios_base::skipws); + bencode(std::ostream_iterator(out), session_state); +} + // Enable DHT bool bittorrent::enableDHT(bool b) { if(b) { diff --git a/src/bittorrent.h b/src/bittorrent.h index 7d2c7c35e..cb3e1b45d 100644 --- a/src/bittorrent.h +++ b/src/bittorrent.h @@ -113,6 +113,8 @@ class bittorrent : public QObject { public slots: void addTorrent(QString path, bool fromScanDir = false, QString from_url = QString(), bool resumed = false); + void loadSessionState(); + void saveSessionState(); void downloadFromUrl(QString url); void downloadFromURLList(const QStringList& url_list); void deleteTorrent(QString hash, bool permanent = false);