MythTV  master
profilegroup.cpp
Go to the documentation of this file.
1 #include "libmythbase/mythdb.h"
3 
4 #include "cardutil.h"
5 #include "profilegroup.h"
6 #include "recordingprofile.h"
7 #include "videosource.h"
8 
10 {
11  QString idTag(":WHEREID");
12  QString query("id = " + idTag);
13 
14  bindings.insert(idTag, m_parent.getProfileNum());
15 
16  return query;
17 }
18 
20 {
21  QString idTag(":SETID");
22  QString colTag(":SET" + GetColumnName().toUpper());
23 
24  QString query("id = " + idTag + ", " +
25  GetColumnName() + " = " + colTag);
26 
27  bindings.insert(idTag, m_parent.getProfileNum());
28  bindings.insert(colTag, m_user->GetDBValue());
29 
30  return query;
31 }
32 
34 {
35  QStringList hostnames;
36  ProfileGroup::getHostNames(&hostnames);
37  for (const auto & hostname : qAsConst(hostnames))
38  this->addSelection(hostname);
39 }
40 
42 {
43  // This must be first because it is needed to load/save the other settings
44  addChild(m_id = new ID());
45  addChild(m_isDefault = new Is_default(*this));
46 
47  setLabel(tr("Profile Group"));
48  addChild(m_name = new Name(*this));
49  auto *cardInfo = new CardInfo(*this);
50  addChild(cardInfo);
51  CardType::fillSelections(cardInfo);
52  m_host = new HostName(*this);
55 };
56 
57 void ProfileGroup::loadByID(int profileId) {
58  m_id->setValue(profileId);
59  Load();
60 }
61 
63 {
64  // We need separate profiles for different V4L2 card types
65  QStringList existing;
67  query.prepare("SELECT DISTINCT cardtype FROM profilegroups");
68 
69  if (!query.exec())
70  {
71  MythDB::DBError("ProfileGroup::createMissingDynamicProfiles", query);
72  return false;
73  }
74 
75  while (query.next())
76  existing.push_back(query.value(0).toString());
77 
78 
79  const std::array<const QString,4> profile_names {
80  "Default", "Live TV", "High Quality", "Low Quality" };
81 
83 
84  for (auto Itype = cardtypes.begin();
85  Itype != cardtypes.end(); ++Itype)
86  {
87  if (Itype.key().startsWith("V4L2:") && existing.indexOf(Itype.key()) == -1)
88  {
89  // Add dynamic profile group
90  query.prepare("INSERT INTO profilegroups SET name = "
91  ":CARDNAME, cardtype = :CARDTYPE, is_default = 1;");
92  query.bindValue(":CARDTYPE", Itype.key());
93  query.bindValue(":CARDNAME", Itype.value());
94  if (!query.exec())
95  {
96  MythDB::DBError("Unable to insert V4L2 profilegroup.", query);
97  return false;
98  }
99 
100  // get the id of the new profile group
101  int groupid = query.lastInsertId().toInt();
102 
103  for (const auto & name : profile_names)
104  {
105  // insert the recording profiles
106  query.prepare("INSERT INTO recordingprofiles SET name = "
107  ":NAME, profilegroup = :GROUPID;");
108  query.bindValue(":NAME", name);
109  query.bindValue(":GROUPID", groupid);
110  if (!query.exec())
111  {
112  MythDB::DBError("Unable to insert 'Default' "
113  "recordingprofile.", query);
114  return false;
115  }
116  }
117  }
118  }
119 
120  return true;
121 }
122 
124 {
126  QString tid;
127 
128  MSqlQuery result(MSqlQuery::InitCon());
129  result.prepare(
130  "SELECT name, id, hostname, is_default, cardtype "
131  "FROM profilegroups");
132 
133  if (!result.exec())
134  {
135  MythDB::DBError("ProfileGroup::fillSelections", result);
136  return;
137  }
138 
139  while (result.next())
140  {
141  QString name = result.value(0).toString();
142  QString id = result.value(1).toString();
143  QString hostname = result.value(2).toString();
144  bool is_default = (bool) result.value(3).toInt();
145  QString cardtype = result.value(4).toString();
146 
147  // Only show default profiles that match installed cards
148  // Workaround for #12481 in fixes/0.27
149  bool have_cardtype = cardtypes.contains(cardtype);
150  if (is_default && (cardtype == "TRANSCODE") && !have_cardtype)
151  {
152  tid = id;
153  }
154  else if (have_cardtype)
155  {
156  if (!hostname.isEmpty())
157  name += QString(" (%1)").arg(result.value(2).toString());
158 
159  if (is_default)
160  {
161  setting->addChild(new RecordingProfileEditor(id.toInt(), name));
162  }
163  else
164  {
165  auto *profileGroup = new ProfileGroup();
166  profileGroup->loadByID(id.toInt());
167  profileGroup->setLabel(name);
168  profileGroup->addChild(
169  new RecordingProfileEditor(id.toInt(), tr("Profiles")));
170  setting->addChild(profileGroup);
171  }
172  }
173  }
174 
175  if (!tid.isEmpty())
176  {
177  setting->addChild(new RecordingProfileEditor(tid.toInt(),
178  tr("Transcoders")));
179  }
180 }
181 
182 QString ProfileGroup::getName(int group)
183 {
184  MSqlQuery result(MSqlQuery::InitCon());
185  result.prepare("SELECT name from profilegroups WHERE id = :PROFILE_ID;");
186  result.bindValue(":PROFILE_ID", group);
187  if (result.exec() && result.next())
188  {
189  return result.value(0).toString();
190  }
191 
192  return nullptr;
193 }
194 
196 {
197  MSqlQuery result(MSqlQuery::InitCon());
198  QString querystr = QString("SELECT DISTINCT id FROM profilegroups WHERE "
199  "name = '%1' AND hostname = '%2';")
200  .arg(getName(), m_host->getValue());
201  result.prepare(querystr);
202 
203  return !(result.exec() && result.next());
204 }
205 
206 void ProfileGroup::getHostNames(QStringList *hostnames)
207 {
208  hostnames->clear();
209 
210  MSqlQuery result(MSqlQuery::InitCon());
211 
212  result.prepare("SELECT DISTINCT hostname from capturecard");
213 
214  if (result.exec())
215  {
216  while (result.next())
217  hostnames->append(result.value(0).toString());
218  }
219 }
220 
222 {
223  clearSettings();
227 }
ProfileGroup::allowedGroupName
bool allowedGroupName(void)
Definition: profilegroup.cpp:195
MSqlBindings
QMap< QString, QVariant > MSqlBindings
typedef for a map of string -> string bindings for generic queries.
Definition: mythdbcon.h:101
ProfileGroup::HostName::fillSelections
void fillSelections()
Definition: profilegroup.cpp:33
MSqlQuery::next
bool next(void)
Wrap QSqlQuery::next() so we can display the query results.
Definition: mythdbcon.cpp:811
MSqlQuery
QSqlQuery wrapper that fetches a DB connection from the connection pool.
Definition: mythdbcon.h:128
bool
bool
Definition: pxsup2dast.c:30
ProfileGroup::fillSelections
static void fillSelections(GroupSetting *setting)
Definition: profilegroup.cpp:123
StandardSetting::setValue
virtual void setValue(const QString &newValue)
Definition: standardsettings.cpp:170
ProfileGroup::getProfileNum
int getProfileNum(void) const
Definition: profilegroup.h:98
mythdb.h
DBStorage::m_user
StorageUser * m_user
Definition: mythstorage.h:50
MSqlQuery::lastInsertId
QVariant lastInsertId()
Return the id of the last inserted row.
Definition: mythdbcon.cpp:934
ProfileGroup::loadByID
virtual void loadByID(int id)
Definition: profilegroup.cpp:57
ProfileGroupStorage::GetWhereClause
QString GetWhereClause(MSqlBindings &bindings) const override
Definition: profilegroup.cpp:9
MSqlQuery::value
QVariant value(int i) const
Definition: mythdbcon.h:205
ProfileGroup::m_name
Name * m_name
Definition: profilegroup.h:115
DBStorage::GetColumnName
QString GetColumnName(void) const
Definition: mythstorage.h:47
MSqlQuery::exec
bool exec(void)
Wrap QSqlQuery::exec() so we can display SQL.
Definition: mythdbcon.cpp:617
StandardSetting::clearSettings
virtual void clearSettings()
Definition: standardsettings.cpp:160
StorageUser::GetDBValue
virtual QString GetDBValue(void) const =0
CardType::fillSelections
static void fillSelections(MythUIComboBoxSetting *setting)
Definition: videosource.cpp:2729
ProfileGroup::ProfileGroup
ProfileGroup()
Definition: profilegroup.cpp:41
ProfileGroup::ID
Definition: profilegroup.h:35
StandardSetting::addChild
virtual void addChild(StandardSetting *child)
Definition: standardsettings.cpp:71
ProfileGroup::Name
Definition: profilegroup.h:57
ProfileGroup::getName
QString getName(void) const
Definition: profilegroup.h:106
ProfileGroup::m_isDefault
Is_default * m_isDefault
Definition: profilegroup.h:117
ProfileGroupStorage::GetSetClause
QString GetSetClause(MSqlBindings &bindings) const override
Definition: profilegroup.cpp:19
MSqlQuery::InitCon
static MSqlQueryInfo InitCon(ConnectionReuse _reuse=kNormalConnection)
Only use this in combination with MSqlQuery constructor.
Definition: mythdbcon.cpp:549
MythDB::DBError
static void DBError(const QString &where, const MSqlQuery &query)
Definition: mythdb.cpp:227
StandardSetting::Load
virtual void Load(void)
Definition: standardsettings.cpp:214
ProfileGroupStorage::m_parent
const ProfileGroup & m_parent
Definition: profilegroup.h:26
StandardSetting::getValue
virtual QString getValue(void) const
Definition: standardsettings.h:52
StandardSetting::setLabel
virtual void setLabel(QString str)
Definition: standardsettings.h:34
ProfileGroup::HostName
Definition: profilegroup.h:67
RecordingProfileEditor
Definition: recordingprofile.h:154
ProfileGroup::CardInfo
Definition: profilegroup.h:79
mythuihelper.h
MythUIComboBoxSetting::addSelection
void addSelection(const QString &label, QString value=QString(), bool select=false)
Definition: standardsettings.cpp:499
ProfileGroup::getHostNames
static void getHostNames(QStringList *hostnames)
Definition: profilegroup.cpp:206
CardUtil::InputTypes
QMap< QString, QString > InputTypes
Definition: cardutil.h:46
ProfileGroup::addMissingDynamicProfiles
static bool addMissingDynamicProfiles(void)
Definition: profilegroup.cpp:62
cardutil.h
MSqlQuery::bindValue
void bindValue(const QString &placeholder, const QVariant &val)
Add a single binding.
Definition: mythdbcon.cpp:887
CardUtil::GetInputTypes
static InputTypes GetInputTypes(void)
Definition: cardutil.cpp:324
ProfileGroupEditor::Load
void Load(void) override
Definition: profilegroup.cpp:221
recordingprofile.h
musicbrainzngs.caa.hostname
string hostname
Definition: caa.py:17
profilegroup.h
videosource.h
ProfileGroup::m_host
HostName * m_host
Definition: profilegroup.h:116
GroupSetting
Definition: standardsettings.h:435
ProfileGroup::Is_default
Definition: profilegroup.h:44
MSqlQuery::prepare
bool prepare(const QString &query)
QSqlQuery::prepare() is not thread safe in Qt <= 3.3.2.
Definition: mythdbcon.cpp:836
ProfileGroup::m_id
ID * m_id
Definition: profilegroup.h:114