MythTV  master
ExternalRecChannelFetcher.cpp
Go to the documentation of this file.
1 /* -*- Mode: c++ -*-
2  * Class ExternalFetcher
3  *
4  * Copyright (C) John Poet 2018
5  *
6  * This program 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  * This program 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 this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */
20 
21 // C/C++ includes
22 #include <utility>
23 
24 // Qt includes
25 #include <QString>
26 
27 // MythTV includes
29 #include "ExternalStreamHandler.h"
30 
31 #define LOC QString("ExternalRec[%1](%2): ").arg(m_cardid).arg(m_command)
32 
34  QString cmd)
35  : m_cardid(cardid)
36  , m_command(std::move(cmd))
37 {
40  {
41  LOG(VB_GENERAL, LOG_ERR, LOC + "Open failed");
42  Close();
43  }
44 }
45 
47 {
48  Close();
49 }
50 
52 {
53  if (m_streamHandler)
55 }
56 
58 {
59  if (!m_streamHandler)
60  {
61  LOG(VB_CHANNEL, LOG_ERR, LOC + "Failed to open external app.");
62  return false;
63  }
64 
65  if (!m_streamHandler->HasTuner())
66  {
67  LOG(VB_CHANNEL, LOG_ERR, LOC + "External app does not have a tuner.");
68  return false;
69  }
70 
71  return true;
72 }
73 
74 bool ExternalRecChannelFetcher::FetchChannel(const QString & cmd,
75  QString & channum,
76  QString & name,
77  QString & callsign,
78  QString & xmltvid,
79  QString & icon)
80 {
81  if (!Valid())
82  return false;
83 
84  QString result;
85 
86  if (!m_streamHandler->ProcessCommand(cmd, result))
87  {
88  LOG(VB_CHANNEL, LOG_ERR, LOC + QString("%1 command failed.").arg(cmd));
89  return false;
90  }
91 
92  if (result.startsWith("ERR"))
93  {
94  LOG(VB_CHANNEL, LOG_ERR, LOC + QString("%1: %2")
95  .arg(cmd).arg(result));
96  return false;
97  }
98  if (result.startsWith("OK:DONE"))
99  {
100  LOG(VB_CHANNEL, LOG_INFO, LOC + result);
101  return false;
102  }
103 
104  // Expect csv: channum, name, callsign, xmltvid, icon
105  QStringList fields = result.mid(3).split(",");
106 
107  if (fields.size() != 4 && fields.size() != 5)
108  {
109  LOG(VB_CHANNEL, LOG_ERR, LOC +
110  QString("Expecting channum, name, callsign, xmltvid and "
111  "optionally icon; "
112  "Received '%1").arg(result));
113  return false;
114  }
115 
116  channum = fields[0];
117  name = fields[1];
118  callsign = fields[2];
119  xmltvid = fields[3];
120  if (fields.size() == 5)
121  icon = fields[4];
122 
123  return true;
124 }
125 
127 {
128  if (!Valid())
129  return 0;
130 
131  QString result;
132  int cnt = -1;
133 
134  if (!m_streamHandler->ProcessCommand("LoadChannels", result, 50s))
135  {
136  LOG(VB_CHANNEL, LOG_ERR, LOC + "LoadChannels command failed.");
137  return -1;
138  }
139 
140  if (result.startsWith("FOUND"))
141  cnt = result.mid(6).toInt();
142  else if (result.startsWith("OK"))
143  cnt = result.mid(3).toInt();
144  else
145  {
146  LOG(VB_CHANNEL, LOG_ERR, LOC + QString("LoadChannels: %1").arg(result));
147  return -1;
148  }
149 
150  return cnt;
151 }
ExternalRecChannelFetcher::m_command
QString m_command
Definition: ExternalRecChannelFetcher.h:67
StreamHandler::HasError
bool HasError(void) const
Definition: streamhandler.h:66
ExternalStreamHandler::HasTuner
bool HasTuner(void) const
Definition: ExternalStreamHandler.h:83
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23
ExternalRecChannelFetcher::~ExternalRecChannelFetcher
~ExternalRecChannelFetcher(void)
Definition: ExternalRecChannelFetcher.cpp:46
ExternalStreamHandler.h
LOC
#define LOC
Definition: ExternalRecChannelFetcher.cpp:31
ExternalRecChannelFetcher::ExternalRecChannelFetcher
ExternalRecChannelFetcher(int cardid, QString cmd)
Definition: ExternalRecChannelFetcher.cpp:33
ExternalRecChannelFetcher::m_cardid
int m_cardid
Definition: ExternalRecChannelFetcher.h:66
ExternalStreamHandler::Get
static ExternalStreamHandler * Get(const QString &devname, int inputid, int majorid)
Definition: ExternalStreamHandler.cpp:455
ExternalRecChannelFetcher::LoadChannels
int LoadChannels(void)
Definition: ExternalRecChannelFetcher.cpp:126
ExternalRecChannelFetcher.h
std
Definition: mythchrono.h:23
ExternalRecChannelFetcher::Close
void Close(void)
Definition: ExternalRecChannelFetcher.cpp:51
ExternalStreamHandler::ProcessCommand
bool ProcessCommand(const QString &cmd, QString &result, std::chrono::milliseconds timeout=4s, uint retry_cnt=3)
Definition: ExternalStreamHandler.cpp:1231
ExternalRecChannelFetcher::Valid
bool Valid(void) const
Definition: ExternalRecChannelFetcher.cpp:57
ExternalRecChannelFetcher::FetchChannel
bool FetchChannel(const QString &cmd, QString &channum, QString &name, QString &callsign, QString &xmltvid, QString &icon)
Definition: ExternalRecChannelFetcher.cpp:74
ExternalStreamHandler::Return
static void Return(ExternalStreamHandler *&ref, int inputid)
Definition: ExternalStreamHandler.cpp:486
ExternalRecChannelFetcher::m_streamHandler
ExternalStreamHandler * m_streamHandler
Definition: ExternalRecChannelFetcher.h:69