1 /*MT*
2 
3     MediaTomb - http://www.mediatomb.cc/
4 
5     online_service.h - this file is part of MediaTomb.
6 
7     Copyright (C) 2005 Gena Batyan <bgeradz@mediatomb.cc>,
8                        Sergey 'Jin' Bostandzhyan <jin@mediatomb.cc>
9 
10     Copyright (C) 2006-2010 Gena Batyan <bgeradz@mediatomb.cc>,
11                             Sergey 'Jin' Bostandzhyan <jin@mediatomb.cc>,
12                             Leonhard Wimmer <leo@mediatomb.cc>
13 
14     MediaTomb is free software; you can redistribute it and/or modify
15     it under the terms of the GNU General Public License version 2
16     as published by the Free Software Foundation.
17 
18     MediaTomb is distributed in the hope that it will be useful,
19     but WITHOUT ANY WARRANTY; without even the implied warranty of
20     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21     GNU General Public License for more details.
22 
23     You should have received a copy of the GNU General Public License
24     version 2 along with MediaTomb; if not, write to the Free Software
25     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
26 
27     $Id$
28 */
29 
30 /// \file online_service.h
31 /// \brief Definition of the OnlineService class.
32 
33 #ifdef ONLINE_SERVICES
34 
35 #ifndef __ONLINE_SERVICE_H__
36 #define __ONLINE_SERVICE_H__
37 
38 #include <memory>
39 #include <vector>
40 
41 #include "util/timer.h"
42 
43 // forward declaration
44 class Config;
45 class Database;
46 class ContentManager;
47 class Layout;
48 
49 #define ONLINE_SERVICE_AUX_ID "ols"
50 #define ONLINE_SERVICE_LAST_UPDATE "lu"
51 
52 // make sure to add the database prefixes when adding new services
53 enum service_type_t {
54     OS_None = 0,
55     OS_YouTube = 1,
56     OS_SopCast = 2,
57 
58     OS_ATrailers = 4,
59     OS_Max
60 };
61 
62 /// \brief This is an interface for all online services, the function
63 /// handles adding/refreshing content in the database.
64 class OnlineService {
65 public:
66     explicit OnlineService(std::shared_ptr<ContentManager> content);
67     virtual ~OnlineService() = default;
68 
69     /// \brief Retrieves user specified content from the service and adds
70     /// the items to the database.
71     ///
72     /// Depending on the service, the content may be retrieved in chunks,
73     /// thus allowing to split the retrieval into multiple tasks without
74     /// completely blocking other tasks for a longer period of time.
75     /// This function will return true if there are more chunks to get and
76     /// false if all chunks have been processed and there is no more data
77     /// to get. Another call to this function after it returned true will
78     /// reset the internal counters and thus make it fetch the content from
79     /// the beginning.
80     virtual bool refreshServiceData(const std::shared_ptr<Layout>& layout) = 0;
81 
82     /// \brief Returns the service type
83     virtual service_type_t getServiceType() const = 0;
84 
85     /// \brief Returns the service name
86     virtual std::string getServiceName() const = 0;
87 
88     /// \brief Get the database service prefix for a particular service
89     char getDatabasePrefix() const;
90 
91     /// \brief Get the database prefix for a given service type
92     static char getDatabasePrefix(service_type_t service);
93 
94     /// \brief Increments the task count.
95     ///
96     /// When recursive autoscan is in progress, we only want to subcribe to
97     /// a timer event when the scan is finished. However, recursive scans
98     /// spawn tasks for each directory. When adding a rescan task for
99     /// subdirectories, the taskCount will be incremented. When a task is
100     /// done the count will be decremented. When timer gets to zero,
101     /// we will resubscribe.
incTaskCount()102     void incTaskCount() { taskCount++; }
103 
decTaskCount()104     void decTaskCount() { taskCount--; }
105 
getTaskCount()106     int getTaskCount() const { return taskCount; }
107 
setTaskCount(int taskCount)108     void setTaskCount(int taskCount) { this->taskCount = taskCount; }
109 
110     /// Parameter that can be used by timerNotify
setTimerParameter(const std::shared_ptr<Timer::Parameter> & param)111     void setTimerParameter(const std::shared_ptr<Timer::Parameter>& param)
112     {
113         timer_parameter = param;
114     }
115 
getTimerParameter()116     std::shared_ptr<Timer::Parameter> getTimerParameter() const { return timer_parameter; }
117 
118     /// \brief Sets the service refresh interval in seconds
setRefreshInterval(std::chrono::seconds interval)119     void setRefreshInterval(std::chrono::seconds interval) { refresh_interval = interval; }
120 
121     /// \brief Retrieves the service refresh interval in seconds
getRefreshInterval()122     std::chrono::seconds getRefreshInterval() const { return refresh_interval; }
123 
124     /// \brief Sets the "purge after" interval in seconds
setItemPurgeInterval(std::chrono::seconds interval)125     void setItemPurgeInterval(std::chrono::seconds interval) { purge_interval = interval; }
126 
127     /// \brief Retrieves the "purge after" interval in seconds
getItemPurgeInterval()128     std::chrono::seconds getItemPurgeInterval() const { return purge_interval; }
129 
130 protected:
131     std::shared_ptr<Config> config;
132     std::shared_ptr<Database> database;
133     std::shared_ptr<ContentManager> content;
134 
135     int taskCount {};
136     std::chrono::seconds refresh_interval {};
137     std::chrono::seconds purge_interval {};
138     std::shared_ptr<Timer::Parameter> timer_parameter;
139 };
140 
141 class OnlineServiceList {
142 public:
143     /// \brief Adds a service to the service list.
144     void registerService(const std::shared_ptr<OnlineService>& service);
145 
146     /// \brief Retrieves a service given by the service ID from the list
147     std::shared_ptr<OnlineService> getService(service_type_t service) const;
148 
149 protected:
150     std::vector<std::shared_ptr<OnlineService>> service_list { OS_Max };
151 };
152 
153 #endif //__ONLINE_SERVICE_H__
154 
155 #endif //ONLINE_SERVICE
156