From 8cbe742d9f0c9333dd430f7c98a05a5568a72329 Mon Sep 17 00:00:00 2001
From: Krzysztof Sobiecki <sobkas@gmail.com>
Date: Wed, 10 Dec 2014 21:57:09 +0100
Subject: [PATCH] Fix errors, needed for a pull

---
 src/podcasts/podcastbackend.cpp    |  1 -
 src/podcasts/podcastdeleter.cpp    | 17 ++++++++-------
 src/podcasts/podcastdownloader.cpp | 35 +++++++++++-------------------
 src/podcasts/podcastdownloader.h   | 12 +++++-----
 4 files changed, 28 insertions(+), 37 deletions(-)

diff --git a/src/podcasts/podcastbackend.cpp b/src/podcasts/podcastbackend.cpp
index 540dbe0d8..7aace691d 100644
--- a/src/podcasts/podcastbackend.cpp
+++ b/src/podcasts/podcastbackend.cpp
@@ -324,7 +324,6 @@ PodcastEpisodeList PodcastBackend::GetOldDownloadedEpisodes(
 
 PodcastEpisode PodcastBackend::GetOldestDownloadedListenedEpisode() {
   PodcastEpisode ret;
-  PodcastEpisodeList list_;
 
   QMutexLocker l(db_->Mutex());
   QSqlDatabase db(db_->Connect());
diff --git a/src/podcasts/podcastdeleter.cpp b/src/podcasts/podcastdeleter.cpp
index 80bf653c6..93d6d87c3 100644
--- a/src/podcasts/podcastdeleter.cpp
+++ b/src/podcasts/podcastdeleter.cpp
@@ -15,14 +15,14 @@
    along with Clementine.  If not, see <http://www.gnu.org/licenses/>.
 */
 
-#include "podcastbackend.h"
-#include "podcastdeleter.h"
 #include "core/application.h"
 #include "core/logging.h"
 #include "core/timeconstants.h"
 #include "core/utilities.h"
 #include "library/librarydirectorymodel.h"
 #include "library/librarymodel.h"
+#include "podcastbackend.h"
+#include "podcastdeleter.h"
 
 #include <QDateTime>
 #include <QDir>
@@ -43,6 +43,7 @@ PodcastDeleter::PodcastDeleter(Application* app, QObject* parent)
       auto_delete_timer_(new QTimer(this)) {
 
   ReloadSettings();
+  auto_delete_timer_->setSingleShot(true);
   AutoDelete();
   connect(auto_delete_timer_, SIGNAL(timeout()), SLOT(AutoDelete()));
   connect(app_, SIGNAL(SettingsChanged()), SLOT(ReloadSettings()));
@@ -76,7 +77,7 @@ void PodcastDeleter::AutoDelete() {
   }
   auto_delete_timer_->stop();
   QDateTime max_date = QDateTime::currentDateTime();
-  QDateTime time_out;
+  qint64 time_out;
   PodcastEpisode oldest_episode;
   QDateTime oldest_episode_time;
   max_date = max_date.addSecs(-delete_after_secs_);
@@ -99,11 +100,11 @@ void PodcastDeleter::AutoDelete() {
     oldest_episode_time = oldest_episode.listened_date();
   }
 
-  time_out = QDateTime::currentDateTime();
-  time_out = time_out.addMSecs(-oldest_episode_time.toMSecsSinceEpoch());
-  time_out.setTime_t(delete_after_secs_ - time_out.toTime_t());
-  if (time_out.isValid()) {
-    auto_delete_timer_->setInterval(time_out.toMSecsSinceEpoch());
+  time_out = QDateTime::currentDateTime().toMSecsSinceEpoch();
+  time_out -= oldest_episode_time.toMSecsSinceEpoch();
+  time_out = (delete_after_secs_ * kMsecPerSec) - time_out;
+  if (time_out >= 0) {
+    auto_delete_timer_->setInterval(time_out);
   } else {
     auto_delete_timer_->setInterval(kAutoDeleteCheckIntervalMsec);
   }
diff --git a/src/podcasts/podcastdownloader.cpp b/src/podcasts/podcastdownloader.cpp
index 2594aa838..525f58a50 100644
--- a/src/podcasts/podcastdownloader.cpp
+++ b/src/podcasts/podcastdownloader.cpp
@@ -17,8 +17,6 @@
    along with Clementine.  If not, see <http://www.gnu.org/licenses/>.
 */
 
-#include "podcastbackend.h"
-#include "podcastdownloader.h"
 #include "core/application.h"
 #include "core/logging.h"
 #include "core/network.h"
@@ -27,6 +25,8 @@
 #include "core/utilities.h"
 #include "library/librarydirectorymodel.h"
 #include "library/librarymodel.h"
+#include "podcastbackend.h"
+#include "podcastdownloader.h"
 
 #include <QDateTime>
 #include <QDir>
@@ -40,24 +40,18 @@ const char* PodcastDownloader::kSettingsGroup = "Podcasts";
 Task::Task(PodcastEpisode episode, QFile* file, PodcastBackend* backend)
   : file_(file),
     episode_(episode),
+    req_(QNetworkRequest(episode.url())),
     backend_(backend),
-    network_(new NetworkAccessManager(this)) {
-  req_ = QNetworkRequest(episode_.url());
-  repl = new RedirectFollower(network_->get(req_));
-  connect(repl, SIGNAL(readyRead()), SLOT(reading()));
-  connect(repl, SIGNAL(finished()), SLOT(finished_()));
-  connect(repl, SIGNAL(downloadProgress(qint64, qint64)),
+    network_(new NetworkAccessManager(this)),
+    repl(new RedirectFollower(network_->get(req_))) {
+  connect(repl.get(), SIGNAL(readyRead()), SLOT(reading()));
+  connect(repl.get(), SIGNAL(finished()), SLOT(finishedInternal()));
+  connect(repl.get(), SIGNAL(downloadProgress(qint64, qint64)),
           SLOT(downloadProgress_(qint64, qint64)));
   emit ProgressChanged(episode_, PodcastDownload::Downloading, 0);
 }
 
-Task::~Task() {
-  delete repl;
-  delete file_;
-  delete network_;
-}
-
-PodcastEpisode Task::episode() {
+const PodcastEpisode Task::episode() {
   return episode_;
 }
 
@@ -71,7 +65,7 @@ void Task::reading() {
   }
 }
 
-void Task::finished_() {
+void Task::finishedInternal() {
   if (repl->error() != QNetworkReply::NoError) {
     qLog(Warning) << "Error downloading episode:" << repl->errorString();
     emit ProgressChanged(episode_, PodcastDownload::NotDownloading, 0);
@@ -177,9 +171,6 @@ QString PodcastDownloader::FilenameForEpisode(const QString& directory,
 }
 
 void PodcastDownloader::DownloadEpisode(const PodcastEpisode& episode) {
-  QFile* file = nullptr;
-  Task* task = nullptr;
-
   for ( Task* tas : list_tasks_ ) {
     if (tas->episode().database_id() == episode.database_id()) {
       return;
@@ -189,7 +180,7 @@ void PodcastDownloader::DownloadEpisode(const PodcastEpisode& episode) {
   Podcast podcast =
       backend_->GetSubscriptionById(episode.podcast_database_id());
   if (!podcast.is_valid()) {
-    qLog(Warning) << "The podcast that contains episode" << task->episode().url()
+    qLog(Warning) << "The podcast that contains episode" << episode.url()
                   << "doesn't exist any more";
     return;
   }
@@ -199,13 +190,13 @@ void PodcastDownloader::DownloadEpisode(const PodcastEpisode& episode) {
 
   // Open the output file
   QDir().mkpath(directory);
-  file = new QFile(filepath);
+  QFile* file = new QFile(filepath);
   if (!file->open(QIODevice::WriteOnly)) {
     qLog(Warning) << "Could not open the file" << filepath << "for writing";
     return;
   }
 
-  task = new Task(episode, file, backend_);
+  Task* task = new Task(episode, file, backend_);
 
   list_tasks_ << task;
   qLog(Info) << "Downloading" << task->episode().url() << "to" << filepath;
diff --git a/src/podcasts/podcastdownloader.h b/src/podcasts/podcastdownloader.h
index 9e1e2e6e4..cfdb4044a 100644
--- a/src/podcasts/podcastdownloader.h
+++ b/src/podcasts/podcastdownloader.h
@@ -24,6 +24,7 @@
 #include "podcast.h"
 #include "podcastepisode.h"
 
+#include <memory>
 #include <QFile>
 #include <QList>
 #include <QObject>
@@ -51,8 +52,7 @@ class Task : public QObject {
 
  public:
   Task(PodcastEpisode episode, QFile* file, PodcastBackend* backend);
-  ~Task();
-  PodcastEpisode episode();
+  const PodcastEpisode episode();
 
  signals:
   void ProgressChanged(const PodcastEpisode& episode,
@@ -61,16 +61,16 @@ class Task : public QObject {
 
  private slots:
   void reading();
-  void finished_();
+  void finishedInternal();
   void downloadProgress_(qint64 received, qint64 total);
 
  private:
-  QFile* file_;
+  std::unique_ptr<QFile> file_;
   PodcastEpisode episode_;
   QNetworkRequest req_;
   PodcastBackend* backend_;
-  NetworkAccessManager* network_;
-  RedirectFollower* repl;
+  std::unique_ptr<NetworkAccessManager> network_;
+  std::unique_ptr<RedirectFollower> repl;
 };
 
 class PodcastDownloader : public QObject {