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