MythTV  master
externrecscanner.cpp
Go to the documentation of this file.
1 // -*- Mode: c++ -*-
2 
3 // Std C headers
4 #include <cmath>
5 #include <unistd.h>
6 #include <utility>
7 
8 // Qt headers
9 #include <QFile>
10 #include <QTextStream>
11 
12 // MythTV headers
13 #include "mythcontext.h"
14 #include "cardutil.h"
15 #include "channelutil.h"
16 #include "externrecscanner.h"
17 #include "scanmonitor.h"
18 #include "mythlogging.h"
20 
21 #define LOC QString("ExternRecChanFetch: ")
22 
24  QString inputname,
25  uint sourceid,
26  ScanMonitor *monitor)
27  : m_scanMonitor(monitor)
28  , m_cardId(cardid)
29  , m_inputName(std::move(inputname))
30  , m_sourceId(sourceid)
31  , m_thread(new MThread("ExternRecChannelScanner", this))
32 {
33  LOG(VB_CHANNEL, LOG_INFO, LOC + QString("Has ScanMonitor %1")
34  .arg(monitor ? "true" : "false"));
35 }
36 
38 {
39  Stop();
40  delete m_thread;
41  m_thread = nullptr;
42 }
43 
48 {
49  m_lock.lock();
50 
51  while (m_threadRunning)
52  {
53  m_stopNow = true;
54  m_lock.unlock();
55  m_thread->wait(5);
56  m_lock.lock();
57  }
58 
59  m_lock.unlock();
60 
61  m_thread->wait();
62 }
63 
66 {
67  Stop();
68  m_stopNow = false;
69  m_thread->start();
70 }
71 
73 {
74  m_lock.lock();
75  m_threadRunning = true;
76  m_lock.unlock();
77 
78 
79  // Step 1/4 : Get info from DB
80  QString cmd = CardUtil::GetVideoDevice(m_cardId);
81 
82  if (m_stopNow || cmd.isEmpty())
83  {
84  LOG(VB_CHANNEL, LOG_INFO, LOC + "Invalid external command");
85  QMutexLocker locker(&m_lock);
86  m_threadRunning = false;
87  m_stopNow = true;
88  return;
89  }
90 
91  LOG(VB_CHANNEL, LOG_INFO, LOC + QString("External Command: '%1'").arg(cmd));
92 
93 
94  // Step 2/4 : Download
95  if (m_scanMonitor)
96  {
98  m_scanMonitor->ScanAppendTextToLog(tr("Creating channel list"));
99  }
100 
102 
103  if ((m_channelTotal = fetch.LoadChannels()) < 1)
104  {
105  LOG(VB_CHANNEL, LOG_ERR, LOC + "Failed to load channels");
106  QMutexLocker locker(&m_lock);
107  m_threadRunning = false;
108  m_stopNow = true;
109  return;
110  }
111 
112  vector<uint> existing = ChannelUtil::GetChanIDs(m_sourceId);
113  vector<uint>::iterator Iold;
114 
115  // Step 3/4 : Process
116  if (m_scanMonitor)
117  m_scanMonitor->ScanAppendTextToLog(tr("Processing channels"));
118 
119  QString channum;
120  QString name;
121  QString callsign;
122  QString xmltvid;
123  QString icon;
124  int cnt = 0;
125 
126  if (!fetch.FirstChannel(channum, name, callsign, xmltvid, icon))
127  {
128  LOG(VB_CHANNEL, LOG_WARNING, LOC + "No channels found.");
129  QMutexLocker locker(&m_lock);
130  m_threadRunning = false;
131  m_stopNow = true;
132  return;
133  }
134 
135  if (m_scanMonitor)
136  m_scanMonitor->ScanAppendTextToLog(tr("Adding Channels"));
137 
138  uint idx = 0;
139  for (;;)
140  {
141  QString msg = tr("Channel #%1 : %2").arg(channum).arg(name);
142 
143  LOG(VB_CHANNEL, LOG_INFO, QString("Handling channel %1 %2")
144  .arg(channum).arg(name));
145 
146  int chanid = ChannelUtil::GetChanID(m_sourceId, channum);
147 
148  if (m_scanMonitor)
150 
151  if (chanid <= 0)
152  {
153  if (m_scanMonitor)
154  m_scanMonitor->ScanAppendTextToLog(tr("Adding %1").arg(msg));
155 
156  chanid = ChannelUtil::CreateChanID(m_sourceId, channum);
157  ChannelUtil::CreateChannel(0, m_sourceId, chanid, callsign, name,
158  channum, 1, 0, 0,
159  false, kChannelVisible, QString(),
160  icon, "Default", xmltvid);
161  }
162  else
163  {
164  if (m_scanMonitor)
165  m_scanMonitor->ScanAppendTextToLog(tr("Updating %1").arg(msg));
166 
167  ChannelUtil::UpdateChannel(0, m_sourceId, chanid, callsign, name,
168  channum, 1, 0, 0,
169  false, kChannelVisible, QString(),
170  icon, "Default", xmltvid);
171  }
172 
174 
175  if ((Iold = std::find(existing.begin(), existing.end(), chanid)) !=
176  existing.end())
177  {
178  existing.erase(Iold);
179  }
180 
181  if (++idx < m_channelTotal)
182  fetch.NextChannel(channum, name, callsign, xmltvid, icon);
183  else
184  break;
185  }
186 
187  // Remove any channels which are no longer valid
188  for (Iold = existing.begin(); Iold != existing.end(); ++Iold)
189  {
190  channum = ChannelUtil::GetChanNum(*Iold);
191 
192  if (m_scanMonitor)
194  (tr("Removing unused Channel #%1").arg(channum));
195 
197  }
198 
199  // Step 4/4 : Finish up
200  LOG(VB_CHANNEL, LOG_INFO, LOC + QString("Found %1 channels").arg(cnt));
201  if (m_scanMonitor)
203  (tr("Found %1 channels.").arg(cnt));
204 
205  if (m_scanMonitor)
206  {
207  m_scanMonitor->ScanAppendTextToLog(tr("Done"));
211  }
212 
213  QMutexLocker locker(&m_lock);
214  m_threadRunning = false;
215  m_stopNow = true;
216 }
217 
219 {
220  uint minval = 35;
221  uint range = 70 - minval;
222  uint pct = minval + (uint) truncf((((float)val) / m_channelCnt) * range);
223  if (m_scanMonitor)
225 }
226 
228 {
229  uint minval = 70;
230  uint range = 100 - minval;
231  uint pct = minval + (uint) truncf((((float)val) / m_channelCnt) * range);
232  if (m_scanMonitor)
234 }
235 
236 void ExternRecChannelScanner::SetMessage(const QString &status)
237 {
238  if (m_scanMonitor)
240 }
241 
242 /* vim: set expandtab tabstop=4 shiftwidth=4: */
MThread::start
void start(QThread::Priority p=QThread::InheritPriority)
Tell MThread to start running the thread in the near future.
Definition: mthread.cpp:292
LOC
#define LOC
Definition: externrecscanner.cpp:21
ExternRecChannelScanner::~ExternRecChannelScanner
~ExternRecChannelScanner() override
Definition: externrecscanner.cpp:37
ExternRecChannelScanner::Stop
void Stop(void)
Stops the scanning thread running.
Definition: externrecscanner.cpp:47
ChannelUtil::DeleteChannel
static bool DeleteChannel(uint channel_id)
Definition: channelutil.cpp:1766
ExternRecChannelScanner::m_lock
QMutex m_lock
Definition: externrecscanner.h:52
ScanMonitor::ScanPercentComplete
void ScanPercentComplete(int pct)
Definition: scanmonitor.cpp:103
arg
arg(title).arg(filename).arg(doDelete))
ExternRecChannelScanner::m_channelTotal
uint m_channelTotal
Definition: externrecscanner.h:47
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23
ExternRecChannelScanner::m_sourceId
uint m_sourceId
Definition: externrecscanner.h:46
ChannelUtil::CreateChannel
static bool CreateChannel(uint db_mplexid, uint db_sourceid, uint new_channel_id, const QString &callsign, const QString &service_name, const QString &chan_num, uint service_id, uint atsc_major_channel, uint atsc_minor_channel, bool use_on_air_guide, ChannelVisibleType visible, const QString &freqid, const QString &icon=QString(), QString format="Default", const QString &xmltvid=QString(), const QString &default_authority=QString(), uint service_type=0)
Definition: channelutil.cpp:1482
ExternRecChannelScanner::m_thread
MThread * m_thread
Definition: externrecscanner.h:51
ExternRecChannelScanner::m_stopNow
bool m_stopNow
Definition: externrecscanner.h:50
ExternRecChannelScanner::m_scanMonitor
ScanMonitor * m_scanMonitor
Definition: externrecscanner.h:43
scanmonitor.h
ChannelUtil::GetChanID
static int GetChanID(int db_mplexid, int service_transport_id, int major_channel, int minor_channel, int program_number)
Definition: channelutil.cpp:1310
mythlogging.h
ExternRecChannelScanner::SetNumChannelsParsed
void SetNumChannelsParsed(uint val)
Definition: externrecscanner.cpp:218
ExternRecChannelScanner::m_channelCnt
uint m_channelCnt
Definition: externrecscanner.h:48
ChannelUtil::CreateChanID
static int CreateChanID(uint sourceid, const QString &chan_num)
Creates a unique channel ID for database use.
Definition: channelutil.cpp:1446
ExternRecChannelScanner::ExternRecChannelScanner
ExternRecChannelScanner(uint cardid, QString inputname, uint sourceid, ScanMonitor *monitor=nullptr)
Definition: externrecscanner.cpp:23
uint
unsigned int uint
Definition: compat.h:140
ExternRecChannelScanner::Scan
void Scan(void)
Scans the list.
Definition: externrecscanner.cpp:65
ScanMonitor::ScanAppendTextToLog
void ScanAppendTextToLog(const QString &status)
Definition: scanmonitor.cpp:109
ExternalRecChannelFetcher::LoadChannels
int LoadChannels(void)
Definition: ExternalRecChannelFetcher.cpp:121
ExternRecChannelScanner::m_cardId
uint m_cardId
Definition: externrecscanner.h:44
channelutil.h
ExternalRecChannelFetcher
Definition: ExternalRecChannelFetcher.h:27
ChannelUtil::UpdateChannel
static bool UpdateChannel(uint db_mplexid, uint source_id, uint channel_id, const QString &callsign, const QString &service_name, const QString &chan_num, uint service_id, uint atsc_major_channel, uint atsc_minor_channel, bool use_on_air_guide, ChannelVisibleType visible, const QString &freqid=QString(), const QString &icon=QString(), QString format=QString(), const QString &xmltvid=QString(), const QString &default_authority=QString(), uint service_type=0)
Definition: channelutil.cpp:1562
ExternRecChannelScanner::SetMessage
void SetMessage(const QString &status)
Definition: externrecscanner.cpp:236
ExternalRecChannelFetcher.h
ExternalRecChannelFetcher::NextChannel
bool NextChannel(QString &channum, QString &name, QString &callsign, QString &xmltvid, QString &icon)
Definition: ExternalRecChannelFetcher.h:44
kChannelVisible
@ kChannelVisible
Definition: channelinfo.h:25
cardutil.h
ExternalRecChannelFetcher::FirstChannel
bool FirstChannel(QString &channum, QString &name, QString &callsign, QString &xmltvid, QString &icon)
Definition: ExternalRecChannelFetcher.h:35
ScanMonitor::ScanComplete
void ScanComplete(void)
Definition: scanmonitor.cpp:98
mythcontext.h
MThread
This is a wrapper around QThread that does several additional things.
Definition: mthread.h:49
ExternRecChannelScanner::run
void run(void) override
Definition: externrecscanner.cpp:72
ExternRecChannelScanner::SetNumChannelsInserted
void SetNumChannelsInserted(uint val)
Definition: externrecscanner.cpp:227
externrecscanner.h
MThread::wait
bool wait(unsigned long time=ULONG_MAX)
Wait for the MThread to exit, with a maximum timeout.
Definition: mthread.cpp:309
ChannelUtil::GetChanNum
static QString GetChanNum(int chan_id)
Returns the channel-number string of the given channel.
Definition: channelutil.cpp:776
ChannelUtil::GetChanIDs
static vector< uint > GetChanIDs(int sourceid=-1, bool onlyVisible=false)
Definition: channelutil.cpp:2137
ExternRecChannelScanner::m_threadRunning
bool m_threadRunning
Definition: externrecscanner.h:49
ScanMonitor
Definition: scanmonitor.h:47
find
static pid_list_t::iterator find(const PIDInfoMap &map, pid_list_t &list, pid_list_t::iterator begin, pid_list_t::iterator end, bool find_open)
Definition: dvbstreamhandler.cpp:356
CardUtil::GetVideoDevice
static QString GetVideoDevice(uint inputid)
Definition: cardutil.h:284