/* * series.h: SERIES plugin for the Video Disk Recorder * * See the README file for copyright information and how to reach the author. * */ #ifndef __SERIES_H_ #define __SERIES_H_ #include #include #include "svdrpclient.h" using std::string; class Table; #define episodeFileExtension ".episodes" //*************************************************************************** // //*************************************************************************** enum SvdrRetrunCodes { codeCommunicationEnd = 0, codeFileContent = 216, codeTransferEnd = 217, codeFileInfo = 218, codeError = 500 }; //*************************************************************************** // Episode File //*************************************************************************** class cEpisodeFile : public cListObject { public: cEpisodeFile(string aName, string aLink, cList* aLines = 0) { name = aName; link = aLink; lines = aLines; } ~cEpisodeFile() { if (lines) delete lines; } int isLink() { return link.length() > 0; } int storeToTable(Table* episodeDb, const cList* linkLines = 0); int storeToFile(const char* aPath) { if (isLink()) { string ln = string(aPath) + "/" + string(link) + episodeFileExtension; string file = string(name) + episodeFileExtension; unlink(ln.c_str()); if (symlink(file.c_str(), ln.c_str()) != 0) tell(0, "SERIES: Failed to create symlink '%s', error was '%m'", ln.c_str()); } else { string file = string(aPath) + "/" + string(name) + episodeFileExtension; FILE* fp; // open file for writing if (!(fp = fopen(file.c_str(), "w+"))) { tell(0, "SERIES: Store '%s' failed, '%m'", file.c_str()); return -1; } for (cLine* l = lines->First(); l; l = lines->Next(l)) { fwrite(l->Text(), l->Length(), 1, fp); fwrite("\n", 1, 1, fp); } fclose(fp); } return 0; } const cList* getLines() { return lines; } const char* getName() { return name.c_str(); } const char* getLink() { return link.c_str(); } protected: string name; string link; cList* lines; }; //*************************************************************************** // cEpisodeFiles //*************************************************************************** class cEpisodeFiles : public cList { public: cEpisodeFile* findByLink(const char* aName) { for (cEpisodeFile* f = First(); f; f = Next(f)) { if (!f->isLink() && strcmp(f->getName(), aName) == 0) return f; } return 0; } int storeToFile(const char* aPath) { for (cEpisodeFile* f = First(); f; f = Next(f)) f->storeToFile(aPath); return 0; } int storeToTable(Table* episodeDb) { for (cEpisodeFile* f = First(); f; f = Next(f)) { if (f->isLink()) { if (cEpisodeFile* l = findByLink(f->getName())) f->storeToTable(episodeDb, l->getLines()); else tell(0, "Warning: Ignoring invalid link '%s' destination '%s' not found", f->getLink(), f->getName()); } else { f->storeToTable(episodeDb); } } return 0; } }; //*************************************************************************** #endif // __SERIES_H_