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 : std::as_const(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  //NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer)
53  m_host = new HostName(*this);
56 };
57 
58 void ProfileGroup::loadByID(int profileId) {
59  m_id->setValue(profileId);
60  Load();
61 }
62 
64 {
65  // We need separate profiles for different V4L2 card types
66  QStringList existing;
68  query.prepare("SELECT DISTINCT cardtype FROM profilegroups");
69 
70  if (!query.exec())
71  {
72  MythDB::DBError("ProfileGroup::createMissingDynamicProfiles", query);
73  return false;
74  }
75 
76  while (query.next())
77  existing.push_back(query.value(0).toString());
78 
79 
80  const std::array<const QString,4> profile_names {
81  "Default", "Live TV", "High Quality", "Low Quality" };
82 
84 
85  for (auto Itype = cardtypes.begin();
86  Itype != cardtypes.end(); ++Itype)
87  {
88  if (Itype.key().startsWith("V4L2:") && existing.indexOf(Itype.key()) == -1)
89  {
90  // Add dynamic profile group
91  query.prepare("INSERT INTO profilegroups SET name = "
92  ":CARDNAME, cardtype = :CARDTYPE, is_default = 1;");
93  query.bindValue(":CARDTYPE", Itype.key());
94  query.bindValue(":CARDNAME", Itype.value());
95  if (!query.exec())
96  {
97  MythDB::DBError("Unable to insert V4L2 profilegroup.", query);
98  return false;
99  }
100 
101  // get the id of the new profile group
102  int groupid = query.lastInsertId().toInt();
103 
104  for (const auto & name : profile_names)
105  {
106  // insert the recording profiles
107  query.prepare("INSERT INTO recordingprofiles SET name = "
108  ":NAME, profilegroup = :GROUPID;");
109  query.bindValue(":NAME", name);
110  query.bindValue(":GROUPID", groupid);
111  if (!query.exec())
112  {
113  MythDB::DBError("Unable to insert 'Default' "
114  "recordingprofile.", query);
115  return false;
116  }
117  }
118  }
119  }
120 
121  return true;
122 }
123 
125 {
127  QString tid;
128 
129  MSqlQuery result(MSqlQuery::InitCon());
130  result.prepare(
131  "SELECT name, id, hostname, is_default, cardtype "
132  "FROM profilegroups");
133 
134  if (!result.exec())
135  {
136  MythDB::DBError("ProfileGroup::fillSelections", result);
137  return;
138  }
139 
140  while (result.next())
141  {
142  QString name = result.value(0).toString();
143  QString id = result.value(1).toString();
144  QString hostname = result.value(2).toString();
145  bool is_default = (bool) result.value(3).toInt();
146  QString cardtype = result.value(4).toString();
147 
148  // Only show default profiles that match installed cards
149  // Workaround for #12481 in fixes/0.27
150  bool have_cardtype = cardtypes.contains(cardtype);
151  if (is_default && (cardtype == "TRANSCODE") && !have_cardtype)
152  {
153  tid = id;
154  }
155  else if (have_cardtype)
156  {
157  if (!hostname.isEmpty())
158  name += QString(" (%1)").arg(result.value(2).toString());
159 
160  if (is_default)
161  {
162  setting->addChild(new RecordingProfileEditor(id.toInt(), name));
163  }
164  else
165  {
166  auto *profileGroup = new ProfileGroup();
167  profileGroup->loadByID(id.toInt());
168  profileGroup->setLabel(name);
169  profileGroup->addChild(
170  new RecordingProfileEditor(id.toInt(), tr("Profiles")));
171  setting->addChild(profileGroup);
172  }
173  }
174  }
175 
176  if (!tid.isEmpty())
177  {
178  setting->addChild(new RecordingProfileEditor(tid.toInt(),
179  tr("Transcoders")));
180  }
181 }
182 
183 QString ProfileGroup::getName(int group)
184 {
185  MSqlQuery result(MSqlQuery::InitCon());
186  result.prepare("SELECT name from profilegroups WHERE id = :PROFILE_ID;");
187  result.bindValue(":PROFILE_ID", group);
188  if (result.exec() && result.next())
189  {
190  return result.value(0).toString();
191  }
192 
193  return nullptr;
194 }
195 
197 {
198  MSqlQuery result(MSqlQuery::InitCon());
199  QString querystr = QString("SELECT DISTINCT id FROM profilegroups WHERE "
200  "name = '%1' AND hostname = '%2';")
201  .arg(getName(), m_host->getValue());
202  result.prepare(querystr);
203 
204  return !(result.exec() && result.next());
205 }
206 
207 void ProfileGroup::getHostNames(QStringList *hostnames)
208 {
209  hostnames->clear();
210 
211  MSqlQuery result(MSqlQuery::InitCon());
212 
213  result.prepare("SELECT DISTINCT hostname from capturecard");
214 
215  if (result.exec())
216  {
217  while (result.next())
218  hostnames->append(result.value(0).toString());
219  }
220 }
221 
223 {
224  clearSettings();
228 }
ProfileGroup::allowedGroupName
bool allowedGroupName(void)
Definition: profilegroup.cpp:196
MSqlBindings
QMap< QString, QVariant > MSqlBindings
typedef for a map of string -> string bindings for generic queries.
Definition: mythdbcon.h:100
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:812
MSqlQuery
QSqlQuery wrapper that fetches a DB connection from the connection pool.
Definition: mythdbcon.h:127
bool
bool
Definition: pxsup2dast.c:30
ProfileGroup::fillSelections
static void fillSelections(GroupSetting *setting)
Definition: profilegroup.cpp:124
StandardSetting::setValue
virtual void setValue(const QString &newValue)
Definition: standardsettings.cpp:169
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:935
ProfileGroup::loadByID
virtual void loadByID(int id)
Definition: profilegroup.cpp:58
ProfileGroupStorage::GetWhereClause
QString GetWhereClause(MSqlBindings &bindings) const override
Definition: profilegroup.cpp:9
MSqlQuery::value
QVariant value(int i) const
Definition: mythdbcon.h:204
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:618
StandardSetting::clearSettings
virtual void clearSettings()
Definition: standardsettings.cpp:159
StorageUser::GetDBValue
virtual QString GetDBValue(void) const =0
CardType::fillSelections
static void fillSelections(MythUIComboBoxSetting *setting)
Definition: videosource.cpp:2720
ProfileGroup::ProfileGroup
ProfileGroup()
Definition: profilegroup.cpp:41
ProfileGroup::ID
Definition: profilegroup.h:35
StandardSetting::addChild
virtual void addChild(StandardSetting *child)
Definition: standardsettings.cpp:70
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:550
MythDB::DBError
static void DBError(const QString &where, const MSqlQuery &query)
Definition: mythdb.cpp:225
StandardSetting::Load
virtual void Load(void)
Definition: standardsettings.cpp:213
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:502
ProfileGroup::getHostNames
static void getHostNames(QStringList *hostnames)
Definition: profilegroup.cpp:207
CardUtil::InputTypes
QMap< QString, QString > InputTypes
Definition: cardutil.h:46
ProfileGroup::addMissingDynamicProfiles
static bool addMissingDynamicProfiles(void)
Definition: profilegroup.cpp:63
cardutil.h
MSqlQuery::bindValue
void bindValue(const QString &placeholder, const QVariant &val)
Add a single binding.
Definition: mythdbcon.cpp:888
CardUtil::GetInputTypes
static InputTypes GetInputTypes(void)
Definition: cardutil.cpp:321
ProfileGroupEditor::Load
void Load(void) override
Definition: profilegroup.cpp:222
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:837
ProfileGroup::m_id
ID * m_id
Definition: profilegroup.h:114