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