diff options
-rw-r--r-- | pending_notifications.c | 190 | ||||
-rw-r--r-- | pending_notifications.h | 76 |
2 files changed, 266 insertions, 0 deletions
diff --git a/pending_notifications.c b/pending_notifications.c new file mode 100644 index 0000000..27863c0 --- /dev/null +++ b/pending_notifications.c @@ -0,0 +1,190 @@ +/* +Copyright (C) 2004-2009 Christian Wieninger + +This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License +as published by the Free Software Foundation; either version 2 +of the License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +Or, point your browser to http://www.gnu.org/licenses/old-licenses/gpl-2.0.html + +The author can be reached at cwieninger@gmx.de + +The project's page is at http://winni.vdr-developer.org/epgsearch +*/ + +#include <string> +#include "pending_notifications.h" +#include "epgsearchtools.h" + + +cPendingNotifications PendingNotifications; +char *cPendingNotification::buffer = NULL; +// -- cPendingNotifications ----------------------------------------------------------------- + +cPendingNotification::~cPendingNotification(void) +{ + if (buffer) { + free(buffer); + buffer = NULL; + } +} +bool cPendingNotification::Parse(const char *s) +{ + char *t = skipspace(s + 1); + switch (*s) { + case 'F': strreplace(t, '|', '\n'); + formatted = strdup(t); + break; + default: LogFile.eSysLog("ERROR: unexpected tag while reading epgsearch pending notifications data: %s", s); + return false; + } + return true; +} + +bool cPendingNotification::Read(FILE *f) +{ + cPendingNotification *p = NULL; + char *s; + cReadLine ReadLine; + while ((s = ReadLine.Read(f)) != NULL) { + char *t = skipspace(s + 1); + switch (*s) { + case 'N': if (!p) { + tEventID EventID; + int Type, TimerMod, SearchID; + time_t Start; + int n = sscanf(t, "%d %u %d %d %ld", &Type, &EventID, &TimerMod, &SearchID, &Start); + if (n == 5) { + p = new cPendingNotification; + if (p) + { + p->type = Type; + p->eventID = EventID; + p->timerMod = TimerMod; + p->searchID = SearchID; + p->start = Start; + + PendingNotifications.Add(p); + } + } + } + break; + case 'C': + { + s = skipspace(s + 1); + char *pC = strchr(s, ' '); + if (pC) + *pC = 0; // strips optional channel name + if (*s) { + tChannelID channelID = tChannelID::FromString(s); + if (channelID.Valid()) { + if (p) + p->channelID = channelID; + } + else { + LogFile.Log(3, "ERROR: illegal channel ID: %s", s); + return false; + } + } + } + break; + case 'n': + p = NULL; + break; + default: if (p && !p->Parse(s)) + { + LogFile.Log(1,"ERROR: parsing %s", s); + return false; + } + } + } + return true; +} + + +const char *cPendingNotification::ToText(void) const +{ + char* tmpFormatted = formatted!=""?strdup(formatted.c_str()):NULL; + if (tmpFormatted) + strreplace(tmpFormatted, '\n', '|'); + + if (buffer) + free(buffer); + buffer = NULL; + + cChannel *channel = Channels.GetByChannelID(channelID, true, true); + if (!channel) + LogFile.Log(3,"invalid channel in pending notifications!"); + + msprintf(&buffer, "N %d %u %d %d %ld\nC %s\n%s%s%sn", + type, eventID, timerMod, searchID, start, + channel?CHANNELSTRING(channel):"", + tmpFormatted?"F ":"",tmpFormatted?tmpFormatted:"", tmpFormatted?"\n":""); + + if (tmpFormatted) + free(tmpFormatted); + + return buffer; +} + +bool cPendingNotification::Save(FILE *f) +{ + return fprintf(f, "%s\n", ToText()) > 0; +} + +bool cPendingNotifications::Load(const char *FileName) +{ + Clear(); + if (FileName) { + free(fileName); + fileName = strdup(FileName); + } + + if (fileName && access(fileName, F_OK) == 0) { + LogFile.iSysLog("loading %s", fileName); + FILE *f = fopen(fileName, "r"); + bool result = false; + if (f) + result = cPendingNotification::Read(f); + fclose(f); + if (result) + LogFile.Log(2,"loaded pending notifications from %s (count: %d)", fileName, Count()); + else + LogFile.Log(1,"error loading pending notifications from %s (count: %d)", fileName, Count()); + return result; + } + return false; +} + +bool cPendingNotifications::Save(void) +{ + bool result = true; + cPendingNotification* l = (cPendingNotification*)this->First(); + cSafeFile f(fileName); + if (f.Open()) { + while (l) { + if (!l->Save(f)) { + result = false; + break; + } + l = (cPendingNotification*)l->Next(); + } + if (!f.Close()) + result = false; + } + else + result = false; + LogFile.Log(2,"saved pending notifications (count: %d)", Count()); + return result; +} + + diff --git a/pending_notifications.h b/pending_notifications.h new file mode 100644 index 0000000..9930ec6 --- /dev/null +++ b/pending_notifications.h @@ -0,0 +1,76 @@ +/* +Copyright (C) 2004-2009 Christian Wieninger + +This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License +as published by the Free Software Foundation; either version 2 +of the License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +Or, point your browser to http://www.gnu.org/licenses/old-licenses/gpl-2.0.html + +The author can be reached at cwieninger@gmx.de + +The project's page is at http://winni.vdr-developer.org/epgsearch +*/ + +#ifndef __PENDING_NOTIFICATIONS_H +#define __PENDING_NOTIFICATIONS_H + +#include <vdr/plugin.h> + +using std::string; + +// --- cPendingNotification -------------------------------------------------------- +class cPendingNotification : public cListObject { +public: + int type; + tEventID eventID; + tChannelID channelID; + time_t start; + uint timerMod; + int searchID; + string formatted; + + + static char *buffer; + + cPendingNotification() + : type(-1), start(-1), timerMod(-1), searchID(-1) {} + cPendingNotification(int Type, tEventID EventID, tChannelID ChannelID, time_t Start, uint TimerMod = -1, + int SearchID = -1, string Formatted = "") + : type(Type), eventID(EventID), channelID(ChannelID), start(Start), timerMod(TimerMod), + searchID(SearchID), formatted(Formatted) {} + ~cPendingNotification(); + + static bool Read(FILE *f); + bool Parse(const char *s); + const char *ToText(void) const; + bool Save(FILE *f); +}; + +class cPendingNotifications : public cList<cPendingNotification> { + public: + private: + char *fileName; + public: + void Clear(void) + { + free(fileName); + fileName = NULL; + cList<cPendingNotification>::Clear(); + } + bool Load(const char *FileName = NULL); + bool Save(void); +}; + +extern cPendingNotifications PendingNotifications; + +#endif |