]> git.vomp.tv Git - vompclient-marten.git/blob - recman.cc
Select list upgrade, gui and backend support to move recordings
[vompclient-marten.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       success = VDR::getInstance()->moveRecording(toMove->getFileName(), newPath);
173       //success = 1;
174       if (success == 1)
175       {
176         currentDir->recList.erase(i);
177         toDir->recList.push_back(toMove);
178         toDir->sort();
179       }
180       break;
181     }
182   }
183
184
185   return success;
186 }
187
188 bool RecMan::isSubDir()
189 {
190   return (currentDir != rootDir);
191 }
192
193 bool RecMan::down(Directory* downDir)
194 {
195   // Check dir is in currentDir
196   for(DirectoryList::iterator i = currentDir->dirList.begin(); i != currentDir->dirList.end(); i++)
197   {
198     if (*i == downDir)
199     {
200       currentDir = downDir;
201       return true;
202     }
203   }
204   return false;
205 }
206
207 bool RecMan::up()
208 {
209   if (!isSubDir()) return false;
210
211   Directory* oldDir = currentDir;
212   currentDir = currentDir->parent;
213
214   if (oldDir->getNumRecordings() == 0)
215   {
216     // delete the olddir
217     Directory* tempdir;
218     for (DirectoryList::iterator i = currentDir->dirList.begin(); i != currentDir->dirList.end(); i++)
219     {
220       tempdir = *i;
221       if (tempdir == oldDir)
222       {
223         currentDir->dirList.erase(i);
224         delete tempdir;
225         break;
226       }
227     }
228   }
229
230   return true;
231 }
232
233 char* RecMan::getCurDirName()
234 {
235   return currentDir->name;
236 }
237
238 DirectoryList* RecMan::getDirectories()
239 {
240   return &currentDir->dirList;
241 }
242
243 RecordingList* RecMan::getRecordings()
244 {
245   return &currentDir->recList;
246 }
247
248 Directory* RecMan::getRootDir()
249 {
250   return rootDir;
251 }