]> git.vomp.tv Git - vompclient-marten.git/blob - recman.cc
New recordings managager to support multi level dir structure and more in
[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   printf("%lu %s %s\n", startTime, name, fileName);
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         printf("Got prog name '%s'\n", d);
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       printf("making new dir '%s'\n", oneDirName);
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   // Sort it all.
116   rootDir->sort();
117 }
118
119 ULONG RecMan::getTotalSpace()
120 {
121   return totalSpace;
122 }
123
124 ULONG RecMan::getFreeSpace()
125 {
126   return freeSpace;
127 }
128
129 ULONG RecMan::getUsedPercent()
130 {
131   return usedPercent;
132 }
133
134 int RecMan::deleteRecording(Recording* rec)
135 {
136   int success = 0;
137
138   for(RecordingList::iterator i = currentDir->recList.begin(); i != currentDir->recList.end(); i++)
139   {
140     if (*i == rec)
141     {
142       success = VDR::getInstance()->deleteRecording(rec->getFileName());
143       if (success == 1)
144       {
145         currentDir->recList.erase(i);
146         delete rec;
147       }
148       break;
149     }
150   }
151
152   return success;
153 }
154
155 bool RecMan::isSubDir()
156 {
157   return (currentDir != rootDir);
158 }
159
160 bool RecMan::down(Directory* downDir)
161 {
162   // Check dir is in currentDir
163   for(DirectoryList::iterator i = currentDir->dirList.begin(); i != currentDir->dirList.end(); i++)
164   {
165     if (*i == downDir)
166     {
167       currentDir = downDir;
168       return true;
169     }
170   }
171   return false;
172 }
173
174 bool RecMan::up()
175 {
176   if (!isSubDir()) return false;
177
178   Directory* oldDir = currentDir;
179   currentDir = currentDir->parent;
180
181   if (oldDir->getNumRecordings() == 0)
182   {
183     // delete the olddir
184     Directory* tempdir;
185     for (DirectoryList::iterator i = currentDir->dirList.begin(); i != currentDir->dirList.end(); i++)
186     {
187       tempdir = *i;
188       if (tempdir == oldDir)
189       {
190         currentDir->dirList.erase(i);
191         delete tempdir;
192         break;
193       }
194     }
195   }
196
197   return true;
198 }
199
200 char* RecMan::getCurDirName()
201 {
202   return currentDir->name;
203 }
204
205 DirectoryList* RecMan::getDirectories()
206 {
207   return &currentDir->dirList;
208 }
209
210 RecordingList* RecMan::getRecordings()
211 {
212   return &currentDir->recList;
213 }