1 /* This file is part of Clementine.
2    Copyright 2012, David Sansome <me@davidsansome.com>
3    Copyright 2014, John Maguire <john.maguire@gmail.com>
4    Copyright 2014, Krzysztof Sobiecki <sobkas@gmail.com>
5 
6    Clementine is free software: you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation, either version 3 of the License, or
9    (at your option) any later version.
10 
11    Clementine is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15 
16    You should have received a copy of the GNU General Public License
17    along with Clementine.  If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #ifndef INTERNET_PODCASTS_PODCASTBACKEND_H_
21 #define INTERNET_PODCASTS_PODCASTBACKEND_H_
22 
23 #include <QObject>
24 
25 #include "podcast.h"
26 
27 class Application;
28 class Database;
29 
30 class PodcastBackend : public QObject {
31   Q_OBJECT
32 
33  public:
34   explicit PodcastBackend(Application* app, QObject* parent = nullptr);
35 
36   // Adds the podcast and any included Episodes to the database.  Updates the
37   // podcast with a database ID.  If this podcast already has an ID set, this
38   // function does nothing.  If a podcast with this URL already exists in the
39   // database, this function just updates the ID field in the provided podcast.
40   void Subscribe(Podcast* podcast);
41 
42   // Removes the Podcast with the given ID from the database.  Also removes any
43   // episodes associated with this podcast.
44   void Unsubscribe(const Podcast& podcast);
45 
46   // Returns a list of all the subscribed podcasts.  For efficiency the Podcast
47   // objects returned won't contain any PodcastEpisode objects - get them
48   // separately if you want them.
49   PodcastList GetAllSubscriptions();
50   Podcast GetSubscriptionById(int id);
51   Podcast GetSubscriptionByUrl(const QUrl& url);
52 
53   // Returns podcast episodes that match various keys.  All these queries are
54   // indexed.
55   PodcastEpisodeList GetEpisodes(int podcast_id);
56   PodcastEpisode GetEpisodeById(int id);
57   PodcastEpisode GetEpisodeByUrl(const QUrl& url);
58   PodcastEpisode GetEpisodeByUrlOrLocalUrl(const QUrl& url);
59   PodcastEpisode GetOldestDownloadedListenedEpisode();
60 
61   // Returns a list of episodes that have local data (downloaded=true) but were
62   // last listened to before the given QDateTime.  This query is NOT indexed so
63   // it involves a full search of the table.
64   PodcastEpisodeList GetOldDownloadedEpisodes(
65       const QDateTime& max_listened_date);
66   PodcastEpisodeList GetNewDownloadedEpisodes();
67 
68   // Adds episodes to the database.  Every episode must have a valid
69   // podcast_database_id set already.
70   void AddEpisodes(PodcastEpisodeList* episodes);
71 
72   // Updates the editable fields (listened, listened_date, downloaded, and
73   // local_url) on episodes that must already exist in the database.
74   void UpdateEpisodes(const PodcastEpisodeList& episodes);
75 
76  signals:
77   void SubscriptionAdded(const Podcast& podcast);
78   void SubscriptionRemoved(const Podcast& podcast);
79 
80   // Emitted when episodes are added to a subscription that *already exists*.
81   void EpisodesAdded(const PodcastEpisodeList& episodes);
82 
83   // Emitted when existing episodes are updated.
84   void EpisodesUpdated(const PodcastEpisodeList& episodes);
85 
86  private:
87   // Adds each episode to the database, setting their IDs after inserting each
88   // one.
89   void AddEpisodes(PodcastEpisodeList* episodes, QSqlDatabase* db);
90 
91  private:
92   Application* app_;
93   Database* db_;
94 };
95 
96 #endif  // INTERNET_PODCASTS_PODCASTBACKEND_H_
97