MythTV  master
channelgroup.cpp
Go to the documentation of this file.
1 // c++
2 #include <algorithm>
3 
4 // mythtv
5 #include "mythlogging.h"
6 #include "mythdb.h"
7 #include "channelgroup.h"
8 
9 #define LOC QString("Channel Group: ")
10 
11 inline bool lt_group(const ChannelGroupItem &a, const ChannelGroupItem &b)
12 {
13  return QString::localeAwareCompare(a.m_name, b.m_name) < 0;
14 }
15 
16 bool ChannelGroup::ToggleChannel(uint chanid, int changrpid, bool delete_chan)
17 {
18  // Check if it already exists for that chanid...
20  query.prepare(
21  "SELECT channelgroup.id "
22  "FROM channelgroup "
23  "WHERE channelgroup.chanid = :CHANID AND "
24  "channelgroup.grpid = :GRPID "
25  "LIMIT 1");
26  query.bindValue(":CHANID", chanid);
27  query.bindValue(":GRPID", changrpid);
28 
29  if (!query.exec())
30  {
31  MythDB::DBError("ChannelGroup::ToggleChannel", query);
32  return false;
33  }
34  if (query.next() && delete_chan)
35  {
36  // We have a record...Remove it to toggle...
37  QString id = query.value(0).toString();
38  query.prepare("DELETE FROM channelgroup WHERE id = :CHANID");
39  query.bindValue(":CHANID", id);
40  if (!query.exec())
41  MythDB::DBError("ChannelGroup::ToggleChannel -- delete", query);
42  LOG(VB_GENERAL, LOG_INFO, LOC +
43  QString("Removing channel with id=%1.").arg(id));
44  }
45  else if (query.size() == 0)
46  {
47  // We have no record...Add one to toggle...
48  query.prepare("INSERT INTO channelgroup (chanid,grpid) "
49  "VALUES (:CHANID, :GRPID)");
50  query.bindValue(":CHANID", chanid);
51  query.bindValue(":GRPID", changrpid);
52  if (!query.exec())
53  MythDB::DBError("ChannelGroup::ToggleChannel -- insert", query);
54  LOG(VB_GENERAL, LOG_INFO, LOC +
55  QString("Adding channel %1 to group %2.")
56  .arg(chanid).arg(changrpid));
57  }
58 
59  return true;
60 }
61 
62 bool ChannelGroup::AddChannel(uint chanid, int changrpid)
63 {
64  // Make sure the channel group exists
66  query.prepare(
67  "SELECT grpid, name FROM channelgroupnames "
68  "WHERE grpid = :GRPID");
69  query.bindValue(":GRPID", changrpid);
70 
71  if (!query.exec())
72  {
73  MythDB::DBError("ChannelGroup::AddChannel", query);
74  return false;
75  }
76  if (query.size() == 0)
77  {
78  LOG(VB_GENERAL, LOG_INFO, LOC +
79  QString("AddChannel failed to find channel group %1.").arg(changrpid));
80  return false;
81  }
82 
83  query.first();
84  QString groupName = query.value(1).toString();
85 
86  // Make sure the channel exists
87  query.prepare(
88  "SELECT chanid, name FROM channel "
89  "WHERE chanid = :CHANID");
90  query.bindValue(":CHANID", chanid);
91 
92  if (!query.exec())
93  {
94  MythDB::DBError("ChannelGroup::AddChannel", query);
95  return false;
96  }
97  if (query.size() == 0)
98  {
99  LOG(VB_GENERAL, LOG_INFO, LOC +
100  QString("AddChannel failed to find channel %1.").arg(chanid));
101  return false;
102  }
103 
104  query.first();
105  QString chanName = query.value(1).toString();
106 
107  // Check if it already exists for that chanid...
108  query.prepare(
109  "SELECT channelgroup.id "
110  "FROM channelgroup "
111  "WHERE channelgroup.chanid = :CHANID AND "
112  "channelgroup.grpid = :GRPID "
113  "LIMIT 1");
114  query.bindValue(":CHANID", chanid);
115  query.bindValue(":GRPID", changrpid);
116 
117  if (!query.exec())
118  {
119  MythDB::DBError("ChannelGroup::AddChannel", query);
120  return false;
121  }
122  if (query.size() == 0)
123  {
124  // We have no record...Add one to toggle...
125  query.prepare("INSERT INTO channelgroup (chanid,grpid) "
126  "VALUES (:CHANID, :GRPID)");
127  query.bindValue(":CHANID", chanid);
128  query.bindValue(":GRPID", changrpid);
129  if (!query.exec())
130  MythDB::DBError("ChannelGroup::AddChannel -- insert", query);
131  LOG(VB_GENERAL, LOG_INFO, LOC +
132  QString("Adding channel %1 to group %2.")
133  .arg(chanName).arg(groupName));
134  }
135 
136  return true;
137 }
138 
139 bool ChannelGroup::DeleteChannel(uint chanid, int changrpid)
140 {
141  // Check if it already exists for that chanid...
142  MSqlQuery query(MSqlQuery::InitCon());
143  query.prepare(
144  "SELECT channelgroup.id "
145  "FROM channelgroup "
146  "WHERE channelgroup.chanid = :CHANID AND "
147  "channelgroup.grpid = :GRPID "
148  "LIMIT 1");
149  query.bindValue(":CHANID", chanid);
150  query.bindValue(":GRPID", changrpid);
151 
152  if (!query.exec())
153  {
154  MythDB::DBError("ChannelGroup::DeleteChannel", query);
155  return false;
156  }
157  if (query.next())
158  {
159  // We have a record...Remove it to toggle...
160  QString id = query.value(0).toString();
161  query.prepare("DELETE FROM channelgroup WHERE id = :CHANID");
162  query.bindValue(":CHANID", id);
163  if (!query.exec())
164  MythDB::DBError("ChannelGroup::DeleteChannel -- delete", query);
165  LOG(VB_GENERAL, LOG_INFO, LOC +
166  QString("Removing channel with id=%1.").arg(id));
167  }
168 
169  return true;
170 }
171 
173 {
174  ChannelGroupList list;
175 
176  MSqlQuery query(MSqlQuery::InitCon());
177 
178  QString qstr;
179 
180  if (includeEmpty)
181  qstr = "SELECT grpid, name FROM channelgroupnames ORDER BY name";
182  else
183  qstr = "SELECT DISTINCT t1.grpid, name FROM channelgroupnames t1,channelgroup t2 "
184  "WHERE t1.grpid = t2.grpid ORDER BY name";
185 
186  query.prepare(qstr);
187 
188  if (!query.exec())
189  MythDB::DBError("ChannelGroup::GetChannelGroups", query);
190  else
191  {
192  while (query.next())
193  {
194  ChannelGroupItem group(query.value(0).toUInt(),
195  query.value(1).toString());
196  list.push_back(group);
197  }
198  }
199 
200  return list;
201 }
202 
203 // Cycle through the available groups, then all channels
204 // Will cycle through to end then return -1
205 // To signify all channels.
207 {
208  // If no groups return -1 for all channels
209  if (sorted.empty())
210  return -1;
211 
212  // If grpid is all channels (-1), then return the first grpid
213  if (grpid == -1)
214  return sorted[0].m_grpid;
215 
216  ChannelGroupList::const_iterator it = find(sorted.begin(), sorted.end(), grpid);
217 
218  // If grpid is not in the list, return -1 for all channels
219  if (it == sorted.end())
220  return -1;
221 
222  ++it;
223 
224  // If we reached the end, the next option is all channels (-1)
225  if (it == sorted.end())
226  return -1;
227 
228  return it->m_grpid;
229 }
230 
232 {
233  // All Channels
234  if (grpid == -1)
235  return tr("All Channels");
236 
237  // No group
238  if (grpid == 0)
239  return "";
240 
241  MSqlQuery query(MSqlQuery::InitCon());
242  query.prepare("SELECT name FROM channelgroupnames WHERE grpid = :GROUPID");
243  query.bindValue(":GROUPID", grpid);
244 
245  if (!query.exec())
246  MythDB::DBError("ChannelGroup::GetChannelGroups", query);
247  else if (query.next())
248  return query.value(0).toString();
249 
250  return "";
251 }
252 
253 int ChannelGroup::GetChannelGroupId(const QString& changroupname)
254 {
255  // All Channels
256  if (changroupname == "All Channels")
257  return -1;
258 
259  MSqlQuery query(MSqlQuery::InitCon());
260 
261  query.prepare("SELECT grpid FROM channelgroupnames "
262  "WHERE name = :GROUPNAME");
263  query.bindValue(":GROUPNAME", changroupname);
264 
265  if (!query.exec())
266  MythDB::DBError("ChannelGroup::GetChannelGroups", query);
267  else if (query.next())
268  return query.value(0).toUInt();
269 
270  return 0;
271 }
bool next(void)
Wrap QSqlQuery::next() so we can display the query results.
Definition: mythdbcon.cpp:782
void bindValue(const QString &placeholder, const QVariant &val)
Add a single binding.
Definition: mythdbcon.cpp:863
QSqlQuery wrapper that fetches a DB connection from the connection pool.
Definition: mythdbcon.h:125
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)
int size(void) const
Definition: mythdbcon.h:203
vector< ChannelGroupItem > ChannelGroupList
Definition: channelgroup.h:32
unsigned int uint
Definition: compat.h:140
unsigned char b
Definition: ParseText.cpp:329
bool lt_group(const ChannelGroupItem &a, const ChannelGroupItem &b)
QVariant value(int i) const
Definition: mythdbcon.h:198
static bool DeleteChannel(uint chanid, int changrpid)
static ChannelGroupList GetChannelGroups(bool includeEmpty=true)
static int GetNextChannelGroup(const ChannelGroupList &sorted, int grpid)
static int GetChannelGroupId(const QString &changroupname)
static MSqlQueryInfo InitCon(ConnectionReuse=kNormalConnection)
Only use this in combination with MSqlQuery constructor.
Definition: mythdbcon.cpp:535
static QString GetChannelGroupName(int grpid)
bool first(void)
Wrap QSqlQuery::first() so we can display the query results.
Definition: mythdbcon.cpp:792
bool prepare(const QString &query)
QSqlQuery::prepare() is not thread safe in Qt <= 3.3.2.
Definition: mythdbcon.cpp:807
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: mythlogging.h:41
static bool AddChannel(uint chanid, int changrpid)
bool exec(void)
Wrap QSqlQuery::exec() so we can display SQL.
Definition: mythdbcon.cpp:603
static bool ToggleChannel(uint chanid, int changrpid, bool delete_chan)
static void DBError(const QString &where, const MSqlQuery &query)
Definition: mythdb.cpp:179
#define LOC
Definition: channelgroup.cpp:9