fixed: python window methods add-/removeControls defined but not used
[vuplus_xbmc] / xbmc / filesystem / RTVDirectory.cpp
1 /*
2  *      Copyright (C) 2005-2008 Team XBMC
3  *      http://www.xbmc.org
4  *
5  *  This Program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2, or (at your option)
8  *  any later version.
9  *
10  *  This Program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with XBMC; see the file COPYING.  If not, write to
17  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
18  *  http://www.gnu.org/copyleft/gpl.html
19  *
20  */
21
22 // RTVDirectory.cpp: implementation of the CRTVDirectory class.
23 //
24 //////////////////////////////////////////////////////////////////////
25
26 #include "RTVDirectory.h"
27 #include "utils/URIUtils.h"
28 #include "SectionLoader.h"
29 #include "URL.h"
30 #include "tinyXML/tinyxml.h"
31 #include "FileItem.h"
32
33 using namespace XFILE;
34
35 extern "C"
36 {
37 #include "lib/libRTV/interface.h"
38 }
39
40 //////////////////////////////////////////////////////////////////////
41 // Construction/Destruction
42 //////////////////////////////////////////////////////////////////////
43
44 CRTVDirectory::CRTVDirectory(void)
45 {
46   CSectionLoader::Load("LIBRTV");
47 }
48
49 CRTVDirectory::~CRTVDirectory(void)
50 {
51   CSectionLoader::Unload("LIBRTV");
52 }
53
54 //*********************************************************************************************
55 bool CRTVDirectory::GetDirectory(const CStdString& strPath, CFileItemList &items)
56 {
57   CURL url(strPath);
58
59   CStdString strRoot = strPath;
60   URIUtils::AddSlashAtEnd(strRoot);
61
62   // Host name is "*" so we try to discover all ReplayTVs.  This requires some trickery but works.
63   if (url.GetHostName() == "*")
64   {
65     // Check to see whether the URL's path is blank or "Video"
66     if (url.GetFileName() == "" || url.GetFileName() == "Video")
67     {
68       int iOldSize=items.Size();
69       struct RTV * rtv = NULL;
70       int numRTV;
71
72       // Request that all ReplayTVs on the LAN identify themselves.  Each ReplayTV
73       // is given 3000ms to respond to the request.  rtv_discovery returns an array
74       // of structs containing the IP and friendly name of all ReplayTVs found on the LAN.
75       // For some reason, DVArchive doesn't respond to this request (probably only responds
76       // to requests from an IP address of a real ReplayTV).
77       numRTV = rtv_discovery(&rtv, 3000);
78
79       // Run through the array and add the ReplayTVs found as folders in XBMC.
80       // We must add the IP of each ReplayTV as if it is a file name at the end of a the
81       // auto-discover URL--e.g. rtv://*/192.168.1.100--because XBMC does not permit
82       // dyamically added shares and will not play from them.  This little trickery is
83       // the best workaround I could come up with.
84       for (int i = 0; i < numRTV; i++)
85       {
86         CFileItemPtr pItem(new CFileItem(rtv[i].friendlyName));
87         // This will keep the /Video or / and allow one to set up an auto ReplayTV
88         // share of either type--simple file listing or ReplayGuide listing.
89         pItem->SetPath(strRoot + rtv[i].hostname);
90         pItem->m_bIsFolder = true;
91         pItem->SetLabelPreformated(true);
92         items.Add(pItem);
93       }
94       free(rtv);
95       return (items.Size()>iOldSize);
96       // Else the URL's path should be an IP address of the ReplayTV
97     }
98     else
99     {
100       CStdString strURL, strRTV;
101       int pos;
102
103       // Isolate the IP from the URL and replace the "*" with the real IP
104       // of the ReplayTV.  E.g., rtv://*/Video/192.168.1.100/ becomes
105       // rtv://192.168.1.100/Video/ .  This trickery makes things work.
106       strURL = strRoot.TrimRight('/');
107       pos = strURL.ReverseFind('/');
108       strRTV = strURL.Left(pos + 1);
109       strRTV.Replace("*", strURL.Mid(pos + 1));
110       CURL tmpURL(strRTV);
111
112       // Force the newly constructed share into the right variables to
113       // be further processed by the remainder of GetDirectory.
114       url = tmpURL;
115       strRoot = strRTV;
116     }
117   }
118
119   // Allow for ReplayTVs on ports other than 80
120   CStdString strHostAndPort;
121   strHostAndPort = url.GetHostName();
122   if (url.HasPort())
123   {
124     char buffer[10];
125     strHostAndPort += ':';
126     strHostAndPort += itoa(url.GetPort(), buffer, 10);
127   }
128
129   // No path given, list shows from ReplayGuide
130   if (url.GetFileName() == "")
131   {
132     unsigned char * data = NULL;
133
134     // Get the RTV guide data in XML format
135     rtv_get_guide_xml(&data, strHostAndPort.c_str());
136
137     // Begin parsing the XML data
138     TiXmlDocument xmlDoc;
139     xmlDoc.Parse( (const char *) data );
140     if ( xmlDoc.Error() )
141     {
142       free(data);
143       return false;
144     }
145     TiXmlElement* pRootElement = xmlDoc.RootElement();
146     if (!pRootElement)
147     {
148       free(data);
149       return false;
150     }
151
152     const TiXmlNode *pChild = pRootElement->FirstChild();
153     while (pChild > 0)
154     {
155       CStdString strTagName = pChild->Value();
156
157       if ( !strcmpi(strTagName.c_str(), "ITEM") )
158       {
159         const TiXmlNode *nameNode = pChild->FirstChild("DISPLAYNAME");
160 //        const TiXmlNode *qualityNode = pChild->FirstChild("QUALITY");
161         const TiXmlNode *recordedNode = pChild->FirstChild("RECORDED");
162         const TiXmlNode *pathNode = pChild->FirstChild("PATH");
163 //        const TiXmlNode *durationNode = pChild->FirstChild("DURATION");
164         const TiXmlNode *sizeNode = pChild->FirstChild("SIZE");
165         const TiXmlNode *atrbNode = pChild->FirstChild("ATTRIB");
166
167         SYSTEMTIME dtDateTime;
168         DWORD dwFileSize = 0;
169         memset(&dtDateTime, 0, sizeof(dtDateTime));
170
171         // DISPLAYNAME
172         const char* szName = NULL;
173         if (nameNode)
174         {
175           szName = nameNode->FirstChild()->Value() ;
176         }
177         else
178         {
179           // Something went wrong, the recording has no name
180           free(data);
181           return false;
182         }
183
184         // QUALITY
185 //        const char* szQuality = NULL;
186 //        if (qualityNode)
187 //        {
188 //          szQuality = qualityNode->FirstChild()->Value() ;
189 //        }
190
191         // RECORDED
192         if (recordedNode)
193         {
194           CStdString strRecorded = recordedNode->FirstChild()->Value();
195           int iYear, iMonth, iDay;
196
197           iYear = atoi(strRecorded.Left(4).c_str());
198           iMonth = atoi(strRecorded.Mid(5, 2).c_str());
199           iDay = atoi(strRecorded.Mid(8, 2).c_str());
200           dtDateTime.wYear = iYear;
201           dtDateTime.wMonth = iMonth;
202           dtDateTime.wDay = iDay;
203
204           int iHour, iMin, iSec;
205           iHour = atoi(strRecorded.Mid(11, 2).c_str());
206           iMin = atoi(strRecorded.Mid(14, 2).c_str());
207           iSec = atoi(strRecorded.Mid(17, 2).c_str());
208           dtDateTime.wHour = iHour;
209           dtDateTime.wMinute = iMin;
210           dtDateTime.wSecond = iSec;
211         }
212
213         // PATH
214         const char* szPath = NULL;
215         if (pathNode)
216         {
217           szPath = pathNode->FirstChild()->Value() ;
218         }
219         else
220         {
221           // Something went wrong, the recording has no filename
222           free(data);
223           return false;
224         }
225
226         // DURATION
227 //        const char* szDuration = NULL;
228 //        if (durationNode)
229 //        {
230 //          szDuration = durationNode->FirstChild()->Value() ;
231 //        }
232
233         // SIZE
234         // NOTE: Size here is actually just duration in minutes because
235         // filesize is not reported by the stripped down GuideParser I use
236         if (sizeNode)
237         {
238           dwFileSize = atol( sizeNode->FirstChild()->Value() );
239         }
240
241         // ATTRIB
242         // NOTE: Not currently reported in the XML guide data, nor is it particularly
243         // needed unless someone wants to add the ability to sub-divide the recordings
244         // into categories, as on a real RTV.
245         int attrib = 0;
246         if (atrbNode)
247         {
248           attrib = atoi( atrbNode->FirstChild()->Value() );
249         }
250
251         bool bIsFolder(false);
252         if (attrib & FILE_ATTRIBUTE_DIRECTORY)
253           bIsFolder = true;
254
255         CFileItemPtr pItem(new CFileItem(szName));
256         pItem->m_dateTime=dtDateTime;
257         pItem->SetPath(strRoot + szPath);
258         // Hack to show duration of show in minutes as KB in XMBC because
259         // it doesn't currently permit showing duration in minutes.
260         // E.g., a 30 minute show will show as 29.3 KB in XBMC.
261         pItem->m_dwSize = dwFileSize * 1000;
262         pItem->m_bIsFolder = bIsFolder;
263         pItem->SetLabelPreformated(true);
264         items.Add(pItem);
265       }
266
267       pChild = pChild->NextSibling();
268     }
269
270     free(data);
271
272     // Path given (usually Video), list filenames only
273   }
274   else
275   {
276
277     unsigned char * data;
278     char * p, * q;
279     unsigned long status;
280
281     // Return a listing of all files in the given path
282     status = rtv_list_files(&data, strHostAndPort.c_str(), url.GetFileName().c_str());
283     if (status == 0)
284     {
285       return false;
286     }
287
288     // Loop through the file list using pointers p and q, where p will point to the current
289     // filename and q will point to the next filename
290     p = (char *) data;
291     while (p)
292     {
293       // Look for the end of the current line of the file listing
294       q = strchr(p, '\n');
295       // If found, replace the newline character with the NULL terminator
296       if (q)
297       {
298         *q = '\0';
299         // Increment q so that it points to the next filename
300         q++;
301         // *p should be the current null-terminated filename in the list
302         if (*p)
303         {
304           // Only display MPEG files in XBMC (but not circular.mpg, as that is the RTV
305           // video buffer and XBMC may cause problems if it tries to play it)
306           if (strstr(p, ".mpg") && !strstr(p, "circular"))
307           {
308             CFileItemPtr pItem(new CFileItem(p));
309             pItem->SetPath(strRoot + p);
310             pItem->m_bIsFolder = false;
311             // The list returned by the RTV doesn't include file sizes, unfortunately
312             //pItem->m_dwSize = atol(szSize);
313             pItem->SetLabelPreformated(true);
314             items.Add(pItem);
315           }
316         }
317       }
318       // Point p to the next filename in the list and loop
319       p = q;
320     }
321
322     free(data);
323   }
324
325   return true;
326 }