summaryrefslogtreecommitdiff
path: root/epgd.h
blob: 3efca10818e31e68bb975d5465bae9b5f0b150e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
 * epgd.h
 *
 * See the README file for copyright information and how to reach the author.
 *
 */

#ifndef __EPGD_H
#define __EPGD_H

#include <libxslt/transform.h>
#include <libxslt/xsltutils.h>
#include <libexslt/exslt.h>

#include <unistd.h>

#include <vector>

#include "lib/common.h"
#include "lib/db.h"
#include "lib/epgservice.h"
#include "lib/configuration.h"
#include "lib/searchtimer.h"

#include "epgdconfig.h"

#include "series.h"
#include "levenshtein.h"

#include "tvdbmanager.h"
#include "moviedbmanager.h"

#include "HISTORY.h"

#define confDirDefault "/etc/epgd"

extern const char* confDir;

//***************************************************************************
// Statistic
//***************************************************************************

struct Statistic
{
   int bytes;
   int files;
   int rejected;
   int nonUpdates;
};

class cEpgd;

//***************************************************************************
// Plugin
//***************************************************************************

class Plugin
{
   public:

      Plugin()          { obj = 0; utf8 = yes; }
      virtual ~Plugin() {}

      virtual int init(cEpgd* aObject, int aUtf8)
      {
         obj = aObject;
         utf8 = aUtf8;
         return success;
      }

      virtual int exit()   { return done; }
      virtual int initDb() { return done; }
      virtual int exitDb() { return done; }

      virtual int atConfigItem(const char* Name, const char* Value) = 0;
      virtual const char* getSource() = 0;
      virtual int hasSource(const char* source) { return strcmp(getSource(), source) == 0; }

      virtual int getPicture(const char* imagename, const char* fileRef, MemoryStruct* data) = 0;

      virtual char* fsNameOfPicture(const char* imagename)  // caller has to free the result!
      {
         char* buffer = strdup(imagename);
         replaceChars(buffer, "<>:\"/\\:|?*", '_');
         return buffer;
      }

      virtual int processDay(int day, int fullupdate, Statistic* stat) = 0;

      virtual int cleanupBefore()  { return done; }
      virtual int cleanupAfter()   { return done; }

      virtual int ready() = 0;

   protected:

      cEpgd* obj;
      int utf8;
};

//***************************************************************************
// Plugin Loader
//***************************************************************************

class PluginLoader
{
   public:

      PluginLoader(const char* name, Plugin* p = 0);
      virtual ~PluginLoader();

      int load();
      Plugin* getPlugin()    { return plugin; }

   private:

      char* fileName;
      void* handle;
      Plugin* plugin;
};

//***************************************************************************
// EPG Deamon
//***************************************************************************

class cEpgd : public cFrame, public cSystemNotification
{
   public:

      cEpgd();
      virtual ~cEpgd();

      // interface

      int init();
      int initUuid();
      void loop();
      void scheduleAutoUpdate(int wait = 0);
      int atConfigItem(const char* Name, const char* Value);
      int parseEvent(cDbRow* event, xmlNode* node);

      int dbConnected(int force = no)
      { return connection && connection->isConnected() && (!force || connection->check() == success); }

      // static stuff

      static void downF(int signal)     { tell(0, "Shutdown triggered with signal %d", signal); shutdown = yes; }
      static void triggerF(int aSignal);
      static int doShutDown()           { return shutdown; }

      int wakeupVdr(const char* uuid);
      int triggerVdrs(const char* trg, const char* plug = 0, const char* options = 0);
      int __attribute__ ((format(printf, 5, 6)))  message(int level, char type, const char* title, const char* format, ...);
      int sendTccMail(string& mailBody);
      int sendTccTestMail();

      xmlDocPtr transformXml(const char* buffer, int size, xsltStylesheetPtr stylesheet, const char* fileRef);
      int storeToFs(MemoryStruct* data, const char* filename, const char* subPath);
      int loadFromFs(MemoryStruct* data, const char* filename, const char* subPath);
      int downloadFile(const char* url, int& size, MemoryStruct* data, int timeout = 30, const char* userAgent = "libcurl-agent/1.0");

      // public for plugins access

      cDbConnection* connection;

      cDbTable* eventsDb;
      cDbTable* useeventsDb;
      cDbTable* compDb;
      cDbTable* fileDb;
      cDbTable* imageDb;
      cDbTable* imageRefDb;
      cDbTable* episodeDb;
      cDbTable* mapDb;
      cDbTable* vdrDb;
      cDbTable* parameterDb;
      cDbTable* recordingListDb;
      cDbTable* timerDb;
      cDbTable* messageDb;

      cDbStatement* selectAllMap;
      cDbStatement* selectMapByUpdFlg;
      cDbStatement* selectMapByExt;
      cDbStatement* selectByCompTitle;
      cDbStatement* selectMaxUpdSp;
      cDbStatement* selectDistCompname;
      cDbStatement* selectByCompName;
      cDbStatement* selectByCompNames;
      cDbStatement* updateEpisodeAtEvents;
      cDbStatement* updateScrReference;
      cDbStatement* selectMaxMapOrd;
      cDbStatement* selectMapOrdOf;
      cDbStatement* countDvbChanges;
      cDbStatement* selectNewRecordings;
      cDbStatement* countNewRecordings;
      cDbStatement* selectRecordingEvent;
      cDbStatement* selectRecOtherClient;
      cDbStatement* selectActiveVdrs;
      cDbStatement* selectWebUsers;
      cDbStatement* cleanupTimerActions;
      cDbStatement* selectNotAssumedTimers;

      cDbValue changeCount;
      cDbValue newRecCount;

      cDbProcedure* procMergeEpg;
      cDbProcedure* procUser;

   private:

      // functions

      int initDb();
      int exitDb();
      int migrateFromDbApi4();
      int migrateFromDbApi5();
      int migrateFromDbApi6();
      int tryFillEmptyRecTableFields();
      int checkProcedure(const char* name, cDBS::ProcType type, cDbProcedure* fp = 0);
      int checkView(const char* name, const char* file);
      int registerMe();

      int loadPlugins();
      int initPlugins();
      int initPluginDb();
      int exitPluginDb();

      void setState(cEpgdState::State state, time_t lastUpdate = 0, int silent = no);

      int loadChannelmap();
      int updateMapRow(char* extid, const char* source, const char* chan, int merge, int vps);
      int applyChannelmapChanges();
      int checkConnection();

      // work

      int update();
      int cleanupEvents();
      int cleanupPictures();
      int getPictures();
      int storeImageRefs(tEventId evtId, const char* source, const char* images,
                         const char* ext, const char* fileref);
      int scrapNewEvents();
      int cleanupSeriesAndMovies();
      void scrapNewRecordings(int count);

      int updateSearchTimers(int force = yes, const char* reason = "");

      // plugin interface

      int cleanupBefore();
      int cleanupAfter();
      int getPicture(const char* source, const char* imagename,
                     const char* fileRef, MemoryStruct* data);
      int processDay(int day, int fullupdate, Statistic* stat);

      // series

      int evaluateEpisodes();
      int downloadEpisodes();

      // scraper stuff

      int initScrapers();
      void exitScrapers();
      bool checkEventsForRecording(int eventId, string channelId, int &seriesId, int &episodeId, int &movieId);
      bool checkRecOtherClients(string uuid, string recPath, int recStart);

      // data

      int withutf8;

      time_t nextUpdateAt;
      time_t lastUpdateAt;
      time_t lastMergeAt;

      int fullupdate;
      int fullreload;

      static int shutdown;
      static int epgTrigger;
      static int searchTimerTrigger;
      static int recTableFixTrigger;

      std::vector<PluginLoader*> plugins;

      cTVDBManager* tvdbManager;
      cMovieDBManager* movieDbManager;
      cSearchTimer* search;
};

//***************************************************************************
#endif //__EPGD_H