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