]> git.vomp.tv Git - vompclient.git/blob - recman.cc
Removal of Message::REDRAW and BoxStack handler for it
[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
25 RecMan::RecMan()
26 {
27   totalSpace = 0;
28   freeSpace = 0;
29   usedPercent = 0;
30
31   rootDir = new Directory("/");
32   currentDir = rootDir;
33
34   chronoSortOrder = false;
35 }
36
37 RecMan::~RecMan()
38 {
39   delete rootDir;
40 }
41
42 void RecMan::setStats(ULONG ttotalSpace, ULONG tfreeSpace, ULONG tusedPercent)
43 {
44   totalSpace = ttotalSpace;
45   freeSpace = tfreeSpace;
46   usedPercent = tusedPercent;
47 }
48
49 void RecMan::addEntry(ULONG startTime, char* name, char* fileName)
50 {
51   Recording* rec = new Recording();
52   rec->setStartTime(startTime);
53   rec->setFileName(fileName);
54
55   // And now for the directory structure & prog name
56
57   char* c;
58   char* d;
59   stack<char*> dirNamesStack;
60   char* oneDirName;
61   bool gotProgName = false;
62   for(c = (name + strlen(name) - 1); c >= name; c--)
63   {
64     if ((*c == '~') || (c == name))
65     {
66       // Make d point to the start of the string (i.e. move on 1 from ~ if necessary)
67       if (c == name) d = name;
68       else d = c + 1;
69
70       if (!gotProgName)
71       {
72         rec->setProgName(d); // will copy from d to end of string
73         *c = '\0';
74         gotProgName = true;
75       }
76       else
77       {
78         // get the dir
79         oneDirName = new char[strlen(d) + 1];
80         strcpy(oneDirName, d);
81         dirNamesStack.push(oneDirName);
82         *c = '\0';
83       }
84     }
85   }
86
87   Directory* targetDirectory = rootDir;
88   Directory* getDir;
89
90   while(!dirNamesStack.empty())
91   {
92     oneDirName = dirNamesStack.top();
93     if (!oneDirName) break;
94
95     getDir = targetDirectory->getDirByName(oneDirName);
96     if (getDir)
97     {
98       targetDirectory = getDir;
99     }
100     else
101     {
102       getDir = new Directory(oneDirName); // Make a new directory
103       getDir->parent = targetDirectory;
104       targetDirectory->dirList.push_back(getDir); // Put the new directory in the tree
105       targetDirectory = getDir; // Make the new dir the target
106     }
107
108     dirNamesStack.pop();
109     delete[] oneDirName;
110   }
111
112   targetDirectory->recList.push_back(rec);
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(chronoSortOrder);
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 }
256
257 void RecMan::setSortOrderChron()
258 {
259   chronoSortOrder = true;
260 }
261
262 void RecMan::toggleSortOrder()
263 {
264   chronoSortOrder = !chronoSortOrder;
265 }
266
267 void RecMan::sort()
268 {
269   Log::getInstance()->log("RecMan", Log::DEBUG, "Sort");
270   rootDir->sort(chronoSortOrder);
271 }