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 // Qt includes
22 #include <QString>
23 
24 // MythTV includes
26 #include "ExternalStreamHandler.h"
27 
28 #define LOC QString("ExternalRec[%1](%2): ").arg(m_cardid).arg(m_command)
29 
31  const QString & cmd)
32  : m_cardid(cardid)
33  , m_command(cmd)
34 {
36 }
37 
39 {
40  Close();
41 }
42 
44 {
45  if (m_stream_handler)
47 }
48 
50 {
51  if (!m_stream_handler)
52  {
53  LOG(VB_CHANNEL, LOG_ERR, LOC + "Failed to open external app.");
54  return false;
55  }
56 
57  if (!m_stream_handler->HasTuner())
58  {
59  LOG(VB_CHANNEL, LOG_ERR, LOC + "External app does not have a tuner.");
60  return false;
61  }
62 
63  return true;
64 }
65 
66 bool ExternalRecChannelFetcher::FetchChannel(const QString & cmd,
67  QString & channum,
68  QString & name,
69  QString & callsign,
70  QString & xmltvid)
71 {
72  if (!Valid())
73  return false;
74 
75  QString result;
76 
77  if (!m_stream_handler->ProcessCommand(cmd, result))
78  {
79  LOG(VB_CHANNEL, LOG_ERR, LOC + QString("%1 command failed.").arg(cmd));
80  return false;
81  }
82 
83  if (result.startsWith("ERR"))
84  {
85  LOG(VB_CHANNEL, LOG_ERR, LOC + QString("%1: %2")
86  .arg(cmd).arg(result));
87  return false;
88  }
89  if (result.startsWith("OK:DONE"))
90  {
91  LOG(VB_CHANNEL, LOG_INFO, LOC + result);
92  return false;
93  }
94 
95  // Expect csv: channum, name, callsign, xmltvid
96  QStringList fields = result.mid(3).split(",");
97 
98  if (fields.size() != 4)
99  {
100  LOG(VB_CHANNEL, LOG_ERR, LOC +
101  QString("Expecting channum, name, callsign, xmltvid; "
102  "Received '%1").arg(result));
103  return false;
104  }
105 
106  channum = fields[0];
107  name = fields[1];
108  callsign = fields[2];
109  xmltvid = fields[3];
110 
111  return true;
112 }
113 
115 {
116  if (!Valid())
117  return 0;
118 
119  QString result;
120  int cnt = -1;
121 
122  if (!m_stream_handler->ProcessCommand("LoadChannels", result, 50000))
123  {
124  LOG(VB_CHANNEL, LOG_ERR, LOC + "LoadChannels command failed.");
125  return -1;
126  }
127 
128  if (result.startsWith("FOUND"))
129  cnt = result.mid(6).toInt();
130  else if (result.startsWith("OK"))
131  cnt = result.mid(3).toInt();
132  else
133  {
134  LOG(VB_CHANNEL, LOG_ERR, LOC + QString("LoadChannels: %1").arg(result));
135  return -1;
136  }
137 
138  return cnt;
139 }
ExternalStreamHandler * m_stream_handler
bool FetchChannel(const QString &cmd, QString &channum, QString &name, QString &callsign, QString &xmltvid)
const char * name
Definition: ParseText.cpp:328
static ExternalStreamHandler * Get(const QString &devname, int inputid, int majorid)
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: mythlogging.h:41
bool ProcessCommand(const QString &cmd, QString &result, int timeout=4000, uint retry_cnt=3)
ExternalRecChannelFetcher(int cardid, const QString &cmd)
static void Return(ExternalStreamHandler *&ref, int inputid)