MythTV  master
restoredata.cpp
Go to the documentation of this file.
1 /*
2  * $Id$
3  * vim: set expandtab tabstop=4 shiftwidth=4:
4  *
5  * Original Project
6  * MythTV http://www.mythtv.org
7  *
8  * Description:
9  * Restore data from deleted channels after scan for new channels
10  *
11  * When all channels are deleted and a new scan is done then
12  * all the non-scanned data is lost.
13  * This are the following fields:
14  * xmltvid Channel identification for XMLTV including Schedules Direct
15  * iconpath File name of icon for this channel
16  * visible Visible status
17  * When a channel is deleted it is not immediately deleted from the database;
18  * it is kept for a while with the "deleted" field set to the date at which it
19  * is deleted.
20  * This makes it possible to retrieve this data from the deleted records
21  * when the same channel is found again in a new scan.
22  *
23  * This program is free software; you can redistribute it and/or
24  * modify it under the terms of the GNU General Public License
25  * as published by the Free Software Foundation; either version 2
26  * of the License, or (at your option) any later version.
27  *
28  * This program is distributed in the hope that it will be useful,
29  * but WITHOUT ANY WARRANTY; without even the implied warranty of
30  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31  * GNU General Public License for more details.
32  *
33  * You should have received a copy of the GNU General Public License
34  * along with this program; if not, write to the Free Software
35  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
36  * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
37  *
38  */
39 
40 // C++ includes
41 #include <vector>
42 #include <iostream>
43 
44 // MythTV includes
46 #include "libmythbase/mythdb.h"
48 #include "libmythui/mythuiimage.h"
49 #include "libmythui/mythuitext.h"
50 #include "libmythui/themeinfo.h"
51 
52 #include "restoredata.h"
53 #include "sourceutil.h"
54 #include "videosource.h"
55 
56 #define LOC QString("RestoreData: ")
57 
59 {
60  public:
62  {
63  setLabel(QObject::tr("Restore XMLTV ID"));
65  QObject::tr(
66  "If checked, copy the XMLTV ID in field \"xmltvid\" "
67  "from a deleted channel "
68  "or from a channel in another video source."));
69  setValue(false);
70  };
71 };
72 
74 {
75  public:
77  {
78  setLabel(QObject::tr("Restore Visible status"));
80  QObject::tr(
81  "If checked, copy the Visible status in field \"visible\" "
82  "from a deleted channel in this video source."));
83  setValue(false);
84  };
85 };
86 
88 {
89  public:
91  {
92  setLabel(QObject::tr("Restore Icon filename"));
94  QObject::tr(
95  "If checked, copy the Icon filename in field \"icon\" "
96  "from a deleted channel "
97  "or from a channel in another video source."));
98  setValue(false);
99  };
100 };
101 
103  m_sourceid(sourceid)
104 {
105  setLabel(tr("Restore Data"));
106 
109  QObject::tr(
110  "The video source is selected in the Channel Editor page. "
111  "Searching for non-scanned data is done for all channels in this video source."
112  ));
114 
117 
120 
121  m_restoreIcon = new RestoreIcon();
123 
124  auto *newTransport = new ButtonStandardSetting(tr("Search"));
125  newTransport->setHelpText(
126  QObject::tr(
127  "Start searching for non-scanned data. The data is written to the database "
128  "when \'Save and Exit\' is selected in the \'Exit Settings?\' dialog box."
129  ));
130  addChild(newTransport);
131  connect(newTransport, &ButtonStandardSetting::clicked, this, &RestoreData::Restore);
132 }
133 
135 {
136  bool do_xmltvid = m_restoreXMLTVID->boolValue();
137  bool do_icon = m_restoreIcon->boolValue();
138  bool do_visible = m_restoreVisible->boolValue();
139 
140  if (do_xmltvid || do_icon || do_visible)
141  {
142  QString msg = QString("Restore data from deleted channels for fields ");
143  if (do_xmltvid)
144  {
145  msg += "xmltvid ";
146  }
147  if (do_icon)
148  {
149  msg += "icon ";
150  }
151  if (do_visible)
152  {
153  msg += "visible ";
154  }
155  LOG(VB_GENERAL, LOG_INFO, LOC + msg);
156  }
157 
158  // Old Channel Data
159  m_ocd.clear();
160 
161  MSqlQuery query(MSqlQuery::InitCon());
162  MSqlQuery query2(MSqlQuery::InitCon());
163 
164  // For all channels in this videosource
165  query.prepare(
166  "SELECT chanid, channum, name, serviceid, transportid, networkid, "
167  " channel.xmltvid, "
168  " channel.icon, "
169  " channel.visible "
170  "FROM channel, dtv_multiplex "
171  "WHERE channel.sourceid = :SOURCEID "
172  " AND channel.mplexid = dtv_multiplex.mplexid "
173  " AND deleted IS NULL ");
174  query.bindValue(":SOURCEID", m_sourceid);
175  if (!query.exec() || !query.isActive())
176  {
177  MythDB::DBError("RestoreData::Restore(1)", query);
178  return;
179  }
180  while (query.next())
181  {
182  OldChannelData cd;
183  cd.chanid = query.value(0).toUInt();
184  cd.channum = query.value(1).toString();
185  cd.name = query.value(2).toString();
186  cd.serviceid = query.value(3).toUInt();
187  cd.transportid = query.value(4).toUInt();
188  cd.networkid = query.value(5).toUInt();
189  cd.xmltvid = query.value(6).toString();
190  cd.icon = query.value(7).toString();
191  cd.visible = query.value(8).toInt();
192 
193  cd.found_xmltvid = false;
194  cd.found_icon = false;
195  cd.found_visible = false;
196 
197  // Get xmltvid from the last deleted channel
198  // from any video source or from a channel
199  // on a different video source
200  if (do_xmltvid && cd.xmltvid.isEmpty())
201  {
202  query2.prepare(
203  "SELECT xmltvid "
204  "FROM channel, dtv_multiplex "
205  "WHERE serviceid = :SERVICEID "
206  " AND transportid = :TRANSPORTID "
207  " AND networkid = :NETWORKID "
208  " AND channel.mplexid = dtv_multiplex.mplexid "
209  " AND xmltvid != ''"
210  " AND (deleted IS NOT NULL OR "
211  " channel.sourceid != :SOURCEID)"
212  "ORDER BY deleted DESC;");
213  query2.bindValue(":SERVICEID", cd.serviceid);
214  query2.bindValue(":TRANSPORTID", cd.transportid);
215  query2.bindValue(":NETWORKID", cd.networkid);
216  query2.bindValue(":SOURCEID", m_sourceid);
217  if (!query2.exec() || !query2.isActive())
218  {
219  MythDB::DBError("RestoreData::Restore(2)", query);
220  return;
221  }
222  if (query2.next())
223  {
224  cd.xmltvid = query2.value(0).toString();
225  cd.found_xmltvid = true;
226  }
227  }
228 
229  // Get icon from the last deleted channel
230  // from any video source or from a channel
231  // on a different video source
232  if (do_icon && cd.icon.isEmpty())
233  {
234  query2.prepare(
235  "SELECT icon "
236  "FROM channel, dtv_multiplex "
237  "WHERE serviceid = :SERVICEID "
238  " AND transportid = :TRANSPORTID "
239  " AND networkid = :NETWORKID "
240  " AND channel.mplexid = dtv_multiplex.mplexid "
241  " AND icon != ''"
242  " AND (deleted IS NOT NULL OR "
243  " channel.sourceid != :SOURCEID)"
244  "ORDER BY deleted DESC;");
245  query2.bindValue(":SERVICEID", cd.serviceid);
246  query2.bindValue(":TRANSPORTID", cd.transportid);
247  query2.bindValue(":NETWORKID", cd.networkid);
248  query2.bindValue(":SOURCEID", m_sourceid);
249  if (!query2.exec() || !query2.isActive())
250  {
251  MythDB::DBError("RestoreData::Restore(3)", query);
252  return;
253  }
254  if (query2.next())
255  {
256  cd.icon = query2.value(0).toString();
257  cd.found_icon = true;
258  }
259  }
260 
261  // Get visible from the last deleted channel
262  // but only from the same video source
263  if (do_visible)
264  {
265  query2.prepare(
266  "SELECT channel.visible "
267  "FROM channel, dtv_multiplex "
268  "WHERE serviceid = :SERVICEID "
269  " AND transportid = :TRANSPORTID "
270  " AND networkid = :NETWORKID "
271  " AND channel.sourceid = :SOURCEID "
272  " AND channel.mplexid = dtv_multiplex.mplexid "
273  " AND deleted IS NOT NULL "
274  "ORDER BY deleted DESC;");
275  query2.bindValue(":SERVICEID", cd.serviceid);
276  query2.bindValue(":TRANSPORTID", cd.transportid);
277  query2.bindValue(":NETWORKID", cd.networkid);
278  query2.bindValue(":SOURCEID", m_sourceid);
279  if (!query2.exec() || !query2.isActive())
280  {
281  MythDB::DBError("RestoreData::Restore(4)", query);
282  return;
283  }
284  if (query2.next())
285  {
286  int visible = query2.value(0).toInt();
287  if (visible != cd.visible)
288  {
289  cd.visible = visible;
290  cd.found_visible = true;
291  }
292  }
293  }
294 
295  if (cd.found_xmltvid || cd.found_icon || cd.found_visible)
296  {
297  m_ocd.push_back(cd);
298  }
299  }
300 
301  // Header line for log of all changes
302  if (m_ocd.empty())
303  {
304  LOG(VB_GENERAL, LOG_INFO, LOC + "No data found in deleted channels or no data needed");
305  }
306  else
307  {
308  LOG(VB_GENERAL, LOG_INFO, LOC +
309  QString("Restoring data in %1 channels from deleted channels:")
310  .arg(m_ocd.size()));
311  }
312 
313  // Log of all channels that will be updated
314  int num_xmltvid = 0;
315  int num_icon = 0;
316  int num_visible = 0;
317  for (auto & cd : m_ocd)
318  {
319  QString msg = QString("Channel %1 \'%2\' update ").arg(cd.channum, cd.name);
320  if (cd.found_xmltvid)
321  {
322  msg += QString("xmltvid(%1) ").arg(cd.xmltvid);
323  num_xmltvid++;
324  }
325  if (cd.found_icon)
326  {
327  msg += QString("icon(%1) ").arg(cd.icon);
328  num_icon++;
329  }
330  if (cd.found_visible)
331  {
332  msg += QString("visible(%1) ").arg(cd.visible);
333  num_visible++;
334  }
335  LOG(VB_GENERAL, LOG_INFO, LOC + msg);
336  }
337 
338  // Show totals of what has been found.
339  {
340  QString msg;
341  if (m_ocd.empty())
342  {
343  msg = "No data found";
344  }
345  else
346  {
347  msg = QString("Found data for %1 channels\n").arg(m_ocd.size());
348  if (num_xmltvid > 0)
349  {
350  msg += QString("xmltvid: %1 ").arg(num_xmltvid);
351  }
352  if (num_icon > 0)
353  {
354  msg += QString("icon: %1 ").arg(num_icon);
355  }
356  if (num_visible > 0)
357  {
358  msg += QString("visible: %1").arg(num_visible);
359  }
360  }
361  WaitFor(ShowOkPopup(msg));
362  }
363 }
364 
365 // Load value of selected video source (for display only)
367 {
369 }
370 
371 // Do the actual updating if "Save and Exit" is selected in the "Exit Settings?" dialog.
373 {
374  MSqlQuery query(MSqlQuery::InitCon());
375  for (auto & cd : m_ocd)
376  {
377  query.prepare(
378  "UPDATE channel "
379  "SET icon = :ICON, "
380  " xmltvid = :XMLTVID, "
381  " visible = :VISIBLE "
382  "WHERE chanid = :CHANID");
383  query.bindValue(":ICON", cd.icon);
384  query.bindValue(":XMLTVID", cd.xmltvid);
385  query.bindValue(":VISIBLE", cd.visible);
386  query.bindValue(":CHANID", cd.chanid);
387  if (!query.exec() || !query.isActive())
388  {
389  MythDB::DBError("RestoreData::Restore(5)", query);
390  return;
391  }
392  }
393  LOG(VB_GENERAL, LOG_INFO, LOC + QString("Restored data for %1 channels").arg(m_ocd.size()));
394 }
MSqlQuery::isActive
bool isActive(void) const
Definition: mythdbcon.h:216
MSqlQuery::next
bool next(void)
Wrap QSqlQuery::next() so we can display the query results.
Definition: mythdbcon.cpp:811
WaitFor
bool WaitFor(MythConfirmationDialog *dialog)
Blocks until confirmation dialog exits.
Definition: mythdialogbox.cpp:599
MSqlQuery
QSqlQuery wrapper that fetches a DB connection from the connection pool.
Definition: mythdbcon.h:128
RestoreData::m_videosource
VideoSourceShow * m_videosource
Definition: restoredata.h:94
TransMythUICheckBoxSetting
Definition: standardsettings.h:411
mythuitext.h
OldChannelData::found_visible
bool found_visible
Definition: restoredata.h:76
mythdb.h
OldChannelData::channum
QString channum
Definition: restoredata.h:68
ButtonStandardSetting
Definition: standardsettings.h:450
LOC
#define LOC
Definition: restoredata.cpp:56
RestoreData::m_sourceid
uint m_sourceid
Definition: restoredata.h:99
mythdialogbox.h
MSqlQuery::value
QVariant value(int i) const
Definition: mythdbcon.h:205
MSqlQuery::exec
bool exec(void)
Wrap QSqlQuery::exec() so we can display SQL.
Definition: mythdbcon.cpp:617
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
RestoreData::m_ocd
std::vector< OldChannelData > m_ocd
Definition: restoredata.h:101
mythuiimage.h
OldChannelData::visible
int visible
Definition: restoredata.h:72
ButtonStandardSetting::clicked
void clicked()
OldChannelData::icon
QString icon
Definition: restoredata.h:71
StandardSetting::addChild
virtual void addChild(StandardSetting *child)
Definition: standardsettings.cpp:71
sourceutil.h
RestoreXMLTVID::RestoreXMLTVID
RestoreXMLTVID()
Definition: restoredata.cpp:61
themeinfo.h
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:226
restoredata.h
StandardSetting::Load
virtual void Load(void)
Definition: standardsettings.cpp:214
OldChannelData::transportid
uint transportid
Definition: restoredata.h:66
StandardSetting::setHelpText
virtual void setHelpText(const QString &str)
Definition: standardsettings.h:37
OldChannelData::found_icon
bool found_icon
Definition: restoredata.h:75
RestoreData::m_restoreXMLTVID
RestoreXMLTVID * m_restoreXMLTVID
Definition: restoredata.h:95
OldChannelData::chanid
uint chanid
Definition: restoredata.h:63
uint
unsigned int uint
Definition: compat.h:81
OldChannelData::name
QString name
Definition: restoredata.h:69
RestoreData::m_restoreVisible
RestoreVisible * m_restoreVisible
Definition: restoredata.h:96
OldChannelData::found_xmltvid
bool found_xmltvid
Definition: restoredata.h:74
StandardSetting::setLabel
virtual void setLabel(QString str)
Definition: standardsettings.h:34
VideoSourceShow
Definition: videosource.h:76
RestoreData::Restore
void Restore(void)
Definition: restoredata.cpp:134
mythcorecontext.h
RestoreXMLTVID
Definition: restoredata.cpp:58
OldChannelData
Definition: restoredata.h:61
RestoreData::Load
void Load(void) override
Definition: restoredata.cpp:366
MSqlQuery::bindValue
void bindValue(const QString &placeholder, const QVariant &val)
Add a single binding.
Definition: mythdbcon.cpp:887
OldChannelData::serviceid
uint serviceid
Definition: restoredata.h:65
RestoreData::RestoreData
RestoreData(uint sourceid)
Definition: restoredata.cpp:102
RestoreData::Save
void Save(void) override
Definition: restoredata.cpp:372
RestoreIcon::RestoreIcon
RestoreIcon()
Definition: restoredata.cpp:90
OldChannelData::networkid
uint networkid
Definition: restoredata.h:67
OldChannelData::xmltvid
QString xmltvid
Definition: restoredata.h:70
RestoreIcon
Definition: restoredata.cpp:87
RestoreData::m_restoreIcon
RestoreIcon * m_restoreIcon
Definition: restoredata.h:97
RestoreVisible
Definition: restoredata.cpp:73
RestoreVisible::RestoreVisible
RestoreVisible()
Definition: restoredata.cpp:76
MythUICheckBoxSetting::setValue
void setValue(const QString &newValue) override
Definition: standardsettings.cpp:721
ShowOkPopup
MythConfirmationDialog * ShowOkPopup(const QString &message, bool showCancel)
Non-blocking version of MythPopupBox::showOkPopup()
Definition: mythdialogbox.cpp:562
videosource.h
MSqlQuery::prepare
bool prepare(const QString &query)
QSqlQuery::prepare() is not thread safe in Qt <= 3.3.2.
Definition: mythdbcon.cpp:836
MythUICheckBoxSetting::boolValue
bool boolValue()
Definition: standardsettings.h:403