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