Merge pull request #4857 from t-nelson/Gotham_13.2_backports
[vuplus_xbmc] / xbmc / filesystem / HDHomeRunDirectory.cpp
1 /*
2  *      Copyright (C) 2011-2013 Team XBMC
3  *      http://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, see
17  *  <http://www.gnu.org/licenses/>.
18  *
19  */
20  
21 #include "HDHomeRunDirectory.h"
22 #include "URL.h"
23 #include "FileItem.h"
24 #include "utils/URIUtils.h"
25 #include "utils/StringUtils.h"
26 #include "DllHDHomeRun.h"
27
28 using namespace XFILE;
29 using namespace std;
30
31 // -------------------------------------------
32 // ---------------- Directory ----------------
33 // -------------------------------------------
34
35 CHomeRunDirectory::CHomeRunDirectory()
36 {
37   m_pdll = new DllHdHomeRun;
38   m_pdll->Load();
39 }
40
41 CHomeRunDirectory::~CHomeRunDirectory()
42 {
43   m_pdll->Unload();
44   delete m_pdll;
45 }
46
47 bool CHomeRunDirectory::GetDirectory(const CStdString& strPath, CFileItemList &items)
48 {
49   if(!m_pdll->IsLoaded())
50     return false;
51
52   CURL url(strPath);
53
54   if(url.GetHostName().empty())
55   {
56     // no hostname, list all available devices
57     int target_ip = 0;
58     struct hdhomerun_discover_device_t result_list[64];
59     int count = m_pdll->discover_find_devices_custom(target_ip, HDHOMERUN_DEVICE_TYPE_TUNER, HDHOMERUN_DEVICE_ID_WILDCARD, result_list, 64);
60     if (count < 0)
61       return false;
62
63     for(int i=0;i<count;i++)
64     {
65       CFileItemPtr item;
66       unsigned int ip_addr = result_list[i].ip_addr;
67
68       CStdString device = StringUtils::Format("%x", result_list[i].device_id);
69       CStdString ip = StringUtils::Format("%u.%u.%u.%u",
70             (unsigned int)(ip_addr >> 24) & 0xFF, (unsigned int)(ip_addr >> 16) & 0xFF,
71             (unsigned int)(ip_addr >> 8) & 0xFF, (unsigned int)(ip_addr >> 0) & 0xFF);
72
73       item.reset(new CFileItem("hdhomerun://" + device + "/tuner0/", true));
74       item->SetLabel(device + "-0 On " + ip);
75       item->SetLabelPreformated(true);
76       items.Add(item);
77
78       item.reset(new CFileItem("hdhomerun://" + device + "/tuner1/", true));
79       item->SetLabel(device + "-1 On " + ip);
80       item->SetLabelPreformated(true);
81       items.Add(item);
82     }
83     return true;
84   }
85   else
86   {
87     hdhomerun_device_t* device = m_pdll->device_create_from_str(url.GetHostName().c_str(), NULL);
88     if(!device)
89       return false;
90
91     m_pdll->device_set_tuner_from_str(device, url.GetFileName().c_str());
92
93     hdhomerun_tuner_status_t status;
94     if(!m_pdll->device_get_tuner_status(device, NULL, &status))
95     {
96       m_pdll->device_destroy(device);
97       return true;
98     }
99
100     CStdString label;
101     if(status.signal_present)
102       label = StringUtils::Format("Current Stream: N/A");
103     else
104       label = StringUtils::Format("Current Stream: Channel %s, SNR %d", status.channel, status.signal_to_noise_quality);
105
106     CStdString path = "hdhomerun://" + url.GetHostName() + "/" + url.GetFileName();
107     URIUtils::RemoveSlashAtEnd(path);
108     CFileItemPtr item(new CFileItem(path, false));
109     item->SetLabel(label);
110     item->SetLabelPreformated(true);
111     items.Add(item);
112
113     m_pdll->device_destroy(device);
114     return true;
115   }
116
117   return false;
118 }