]> git.vomp.tv Git - vompclient.git/blob - recman.cc
Rename TCP class to TCPOld
[vompclient.git] / recman.cc
1 /*
2     Copyright 2006 Chris Tallon
3
4     This file is part of VOMP.
5
6     VOMP 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 2 of the License, or
9     (at your option) any later version.
10
11     VOMP 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 VOMP.  If not, see <https://www.gnu.org/licenses/>.
18 */
19
20 #include "recman.h"
21
22 #include "vdr.h"
23 #include "log.h"
24
25 RecMan::RecMan()
26 {
27   rootDir = new Directory("/");
28   currentDir = rootDir;
29 }
30
31 RecMan::~RecMan()
32 {
33   delete rootDir;
34 }
35
36 void RecMan::setStats(ULONG ttotalSpace, ULONG tfreeSpace, ULONG tusedPercent)
37 {
38   totalSpace = ttotalSpace;
39   freeSpace = tfreeSpace;
40   usedPercent = tusedPercent;
41 }
42
43 void RecMan::addEntry(bool isNew, ULONG startTime, char* name, char* fileName)
44 {
45   Recording* rec = new Recording();
46   rec->setNew(isNew);
47   rec->setStartTime(startTime);
48   rec->setFileName(fileName);
49
50   // And now for the directory structure & prog name
51
52   char* c;
53   char* d;
54   std::stack<char*> dirNamesStack;
55   char* oneDirName;
56   bool gotProgName = false;
57   for(c = (name + strlen(name) - 1); c >= name; c--)
58   {
59     if ((*c == '~') || (c == name))
60     {
61       // Make d point to the start of the string (i.e. move on 1 from ~ if necessary)
62       if (c == name) d = name;
63       else d = c + 1;
64
65       if (!gotProgName)
66       {
67         rec->setProgName(d); // will copy from d to end of string
68         *c = '\0';
69         gotProgName = true;
70       }
71       else
72       {
73         // get the dir
74         oneDirName = new char[strlen(d) + 1];
75         strcpy(oneDirName, d);
76         dirNamesStack.push(oneDirName);
77         *c = '\0';
78       }
79     }
80   }
81
82   Directory* targetDirectory = rootDir;
83   Directory* getDir;
84
85   while(!dirNamesStack.empty())
86   {
87     oneDirName = dirNamesStack.top();
88     if (!oneDirName) break;
89
90     getDir = targetDirectory->getDirByName(oneDirName);
91     if (getDir)
92     {
93       targetDirectory = getDir;
94     }
95     else
96     {
97       getDir = new Directory(oneDirName); // Make a new directory
98       getDir->parent = targetDirectory;
99       targetDirectory->dirList.push_back(getDir); // Put the new directory in the tree
100       targetDirectory = getDir; // Make the new dir the target
101     }
102
103     dirNamesStack.pop();
104     delete[] oneDirName;
105   }
106
107   targetDirectory->recList.push_back(rec);
108 }
109
110 ULONG RecMan::getTotalSpace()
111 {
112   return totalSpace;
113 }
114
115 ULONG RecMan::getFreeSpace()
116 {
117   return freeSpace;
118 }
119
120 ULONG RecMan::getUsedPercent()
121 {
122   return usedPercent;
123 }
124
125 int RecMan::deleteRecording(Recording* rec)
126 {
127   int success = 0;
128
129   for(RecordingList::iterator i = currentDir->recList.begin(); i != currentDir->recList.end(); i++)
130   {
131     if (*i == rec)
132     {
133       success = VDR::getInstance()->deleteRecording(rec->getFileName());
134       if (success == 1)
135       {
136         currentDir->recList.erase(i);
137         delete rec;
138       }
139       break;
140     }
141   }
142
143   return success;
144 }
145
146 void RecMan::constructPath(char* target, Directory* dir)
147 {
148   if (dir->parent)
149   {
150     constructPath(target, dir->parent);
151     strcat(target, dir->name);
152     strcat(target, "~");
153   }
154 }
155
156 int RecMan::moveRecording(Recording* toMove, Directory* toDir)
157 {
158   char newPath[PATH_MAX];
159   strcpy(newPath, "~");
160   constructPath(&newPath[1], toDir);
161   int success = 0;
162
163   for(RecordingList::iterator i = currentDir->recList.begin(); i != currentDir->recList.end(); i++)
164   {
165     if (*i == toMove)
166     {
167       char* newFileName = VDR::getInstance()->moveRecording(toMove->getFileName(), newPath);
168       if (newFileName)
169       {
170         success = 1;
171
172         toMove->setFileName(newFileName);
173         delete[] newFileName;
174
175         currentDir->recList.erase(i);
176         toDir->recList.push_back(toMove);
177         toDir->sort(chronoSortOrder);
178       }
179       break;
180     }
181   }
182
183
184   return success;
185 }
186
187 bool RecMan::isSubDir()
188 {
189   return (currentDir != rootDir);
190 }
191
192 bool RecMan::down(Directory* downDir)
193 {
194   // Check dir is in currentDir
195   for(DirectoryList::iterator i = currentDir->dirList.begin(); i != currentDir->dirList.end(); i++)
196   {
197     if (*i == downDir)
198     {
199       currentDir = downDir;
200       return true;
201     }
202   }
203   return false;
204 }
205
206 bool RecMan::up()
207 {
208   if (!isSubDir()) return false;
209
210   Directory* oldDir = currentDir;
211   currentDir = currentDir->parent;
212
213   if (oldDir->getNumRecordings() == 0)
214   {
215     // delete the olddir
216     Directory* tempdir;
217     for (DirectoryList::iterator i = currentDir->dirList.begin(); i != currentDir->dirList.end(); i++)
218     {
219       tempdir = *i;
220       if (tempdir == oldDir)
221       {
222         currentDir->dirList.erase(i);
223         delete tempdir;
224         break;
225       }
226     }
227   }
228
229   return true;
230 }
231
232 char* RecMan::getCurDirName()
233 {
234   return currentDir->name;
235 }
236
237 DirectoryList* RecMan::getDirectories()
238 {
239   return &currentDir->dirList;
240 }
241
242 RecordingList* RecMan::getRecordings()
243 {
244   return &currentDir->recList;
245 }
246
247 Directory* RecMan::getRootDir()
248 {
249   return rootDir;
250 }
251
252 void RecMan::setSortOrderChron()
253 {
254   chronoSortOrder = true;
255 }
256
257 void RecMan::toggleSortOrder()
258 {
259   chronoSortOrder = !chronoSortOrder;
260 }
261
262 void RecMan::sort()
263 {
264   Log::getInstance()->log("RecMan", Log::DEBUG, "Sort");
265   rootDir->sort(chronoSortOrder);
266 }