00001
00002 #include <stdint.h>
00003
00004
00005 #include <QString>
00006
00007
00008 #include "mythdb.h"
00009 #include "scaninfo.h"
00010 #include "mythdbcon.h"
00011 #include "mythlogging.h"
00012
00013 ScanInfo::ScanInfo() : scanid(0), cardid(0), sourceid(0), processed(false) { }
00014
00015 ScanInfo::ScanInfo(uint _scanid, uint _cardid, uint _sourceid,
00016 bool _processed, const QDateTime &_scandate) :
00017 scanid(_scanid), cardid(_cardid), sourceid(_sourceid),
00018 processed(_processed), scandate(_scandate)
00019 {
00020 }
00021
00022 uint SaveScan(const ScanDTVTransportList &scan)
00023 {
00024 LOG(VB_CHANSCAN, LOG_INFO, QString("SaveScan() scan.size(): %1")
00025 .arg(scan.size()));
00026
00027 uint scanid = 0;
00028 if (scan.empty() || scan[0].channels.empty())
00029 return scanid;
00030
00031 uint sourceid = scan[0].channels[0].source_id;
00032 uint cardid = scan[0].cardid;
00033
00034
00035 const vector<ScanInfo> list = LoadScanList();
00036 for (uint i = 0; i < list.size(); i++)
00037 {
00038 if (list[i].scandate > QDateTime::currentDateTime().addDays(-14))
00039 continue;
00040 if ((list[i].cardid == cardid) && (list[i].sourceid == sourceid))
00041 ScanInfo::DeleteScan(list[i].scanid);
00042 }
00043
00044 MSqlQuery query(MSqlQuery::InitCon());
00045 query.prepare(
00046 "INSERT INTO channelscan ( cardid, sourceid, scandate) "
00047 "VALUES (:CARDID, :SOURCEID, :SCANDATE) ");
00048 query.bindValue(":CARDID", cardid);
00049 query.bindValue(":SOURCEID", sourceid);
00050 query.bindValue(":SCANDATE", QDateTime::currentDateTime());
00051
00052 if (!query.exec())
00053 {
00054 MythDB::DBError("SaveScan 1", query);
00055 return scanid;
00056 }
00057
00058 query.prepare("SELECT MAX(scanid) FROM channelscan");
00059 if (!query.exec())
00060 MythDB::DBError("SaveScan 2", query);
00061 else if (query.next())
00062 scanid = query.value(0).toUInt();
00063
00064 if (!scanid)
00065 return scanid;
00066
00067 for (uint i = 0; i < scan.size(); i++)
00068 scan[i].SaveScan(scanid);
00069
00070 return scanid;
00071 }
00072
00073 ScanDTVTransportList LoadScan(uint scanid)
00074 {
00075 ScanDTVTransportList list;
00076 MSqlQuery query(MSqlQuery::InitCon());
00077 MSqlQuery query2(MSqlQuery::InitCon());
00078 query.prepare(
00079 "SELECT frequency, inversion, symbolrate, "
00080 " fec, polarity, "
00081 " hp_code_rate, lp_code_rate, modulation, "
00082 " transmission_mode, guard_interval, hierarchy, "
00083 " modulation, bandwidth, sistandard, "
00084 " tuner_type, transportid, mod_sys, "
00085 " rolloff "
00086 "FROM channelscan_dtv_multiplex "
00087 "WHERE scanid = :SCANID");
00088 query.bindValue(":SCANID", scanid);
00089 if (!query.exec())
00090 {
00091 MythDB::DBError("LoadScan 1", query);
00092 return list;
00093 }
00094
00095 while (query.next())
00096 {
00097 ScanDTVTransport mux;
00098 mux.ParseTuningParams(
00099 (DTVTunerType) query.value(14).toUInt(),
00100 query.value(0).toString(), query.value(1).toString(),
00101 query.value(2).toString(), query.value(3).toString(),
00102 query.value(4).toString(), query.value(5).toString(),
00103 query.value(6).toString(), query.value(7).toString(),
00104 query.value(8).toString(), query.value(9).toString(),
00105 query.value(10).toString(), query.value(11).toString(),
00106 query.value(12).toString(), query.value(13).toString(),
00107 query.value(14).toString());
00108
00109 query2.prepare(
00110 "SELECT "
00111 " mplex_id, source_id, channel_id, "
00112 " callsign, service_name, chan_num, "
00113 " service_id, atsc_major_channel, atsc_minor_channel, "
00114 " use_on_air_guide, hidden, hidden_in_guide, "
00115 " freqid, icon, tvformat, "
00116 " xmltvid, pat_tsid, vct_tsid, "
00117 " vct_chan_tsid, sdt_tsid, orig_netid, "
00118 " netid, si_standard, in_channels_conf, "
00119 " in_pat, in_pmt, in_vct, "
00120 " in_nit, in_sdt, is_encrypted, "
00121 " is_data_service, is_audio_service, is_opencable, "
00122 " could_be_opencable, decryption_status, default_authority "
00123 "FROM channelscan_channel "
00124 "WHERE transportid = :TRANSPORTID");
00125 query2.bindValue(":TRANSPORTID", query.value(15).toUInt());
00126
00127 if (!query2.exec())
00128 {
00129 MythDB::DBError("LoadScan 2", query2);
00130 continue;
00131 }
00132
00133 while (query2.next())
00134 {
00135 QString si_standard = query2.value(22).toString();
00136 si_standard = (si_standard.isEmpty()) ?
00137 query.value(13).toString() : si_standard;
00138
00139 ChannelInsertInfo chan(
00140 query2.value(0).toUInt(),
00141 query2.value(1).toUInt(),
00142 query2.value(2).toUInt(),
00143 query2.value(3).toString(),
00144 query2.value(4).toString(),
00145 query2.value(5).toString(),
00146 query2.value(6).toUInt(),
00147
00148 query2.value(7).toUInt(),
00149 query2.value(8).toUInt(),
00150 query2.value(9).toBool(),
00151 query2.value(10).toBool(),
00152 query2.value(11).toBool(),
00153
00154 query2.value(12).toString(),
00155 query2.value(13).toString(),
00156 query2.value(14).toString(),
00157 query2.value(15).toString(),
00158
00159 query2.value(16).toUInt(),
00160 query2.value(17).toUInt(),
00161 query2.value(18).toUInt(),
00162 query2.value(19).toUInt(),
00163
00164 query2.value(20).toUInt(),
00165 query2.value(21).toUInt(),
00166
00167 si_standard,
00168
00169 query2.value(23).toBool(),
00170 query2.value(24).toBool(),
00171 query2.value(25).toBool(),
00172 query2.value(26).toBool(),
00173 query2.value(27).toBool(),
00174 query2.value(28).toBool(),
00175
00176 query2.value(29).toBool(),
00177 query2.value(30).toBool(),
00178 query2.value(31).toBool(),
00179 query2.value(32).toBool(),
00180 query2.value(33).toBool(),
00181 query2.value(34).toInt(),
00182 query2.value(35).toString());
00183
00184 mux.channels.push_back(chan);
00185 }
00186
00187 list.push_back(mux);
00188 }
00189
00190 return list;
00191 }
00192
00193 bool ScanInfo::MarkProcessed(uint scanid)
00194 {
00195 MSqlQuery query(MSqlQuery::InitCon());
00196 query.prepare(
00197 "UPDATE channelscan "
00198 "SET processed = 1 "
00199 "WHERE scanid = :SCANID");
00200 query.bindValue(":SCANID", scanid);
00201
00202 if (!query.exec())
00203 {
00204 MythDB::DBError("MarkProcessed", query);
00205 return false;
00206 }
00207
00208 return true;
00209 }
00210
00211 bool ScanInfo::DeleteScan(uint scanid)
00212 {
00213 MSqlQuery query(MSqlQuery::InitCon());
00214 query.prepare(
00215 "DELETE FROM channelscan_channel "
00216 "WHERE scanid = :SCANID");
00217 query.bindValue(":SCANID", scanid);
00218
00219 if (!query.exec())
00220 {
00221 MythDB::DBError("DeleteScan", query);
00222 return false;
00223 }
00224
00225 query.prepare(
00226 "DELETE FROM channelscan_dtv_multiplex "
00227 "WHERE scanid = :SCANID");
00228 query.bindValue(":SCANID", scanid);
00229
00230 if (!query.exec())
00231 {
00232 MythDB::DBError("DeleteScan", query);
00233 return false;
00234 }
00235
00236 query.prepare(
00237 "DELETE FROM channelscan "
00238 "WHERE scanid = :SCANID");
00239 query.bindValue(":SCANID", scanid);
00240
00241 if (!query.exec())
00242 {
00243 MythDB::DBError("DeleteScan", query);
00244 return false;
00245 }
00246
00247 return true;
00248 }
00249
00250 vector<ScanInfo> LoadScanList(void)
00251 {
00252 vector<ScanInfo> list;
00253
00254 MSqlQuery query(MSqlQuery::InitCon());
00255 query.prepare(
00256 "SELECT scanid, cardid, sourceid, processed, scandate "
00257 "FROM channelscan "
00258 "ORDER BY scanid, sourceid, cardid, scandate");
00259
00260 if (!query.exec())
00261 {
00262 MythDB::DBError("LoadScanList", query);
00263 return list;
00264 }
00265
00266 while (query.next())
00267 {
00268 list.push_back(
00269 ScanInfo(query.value(0).toUInt(),
00270 query.value(1).toUInt(),
00271 query.value(2).toUInt(),
00272 (bool) query.value(3).toUInt(),
00273 query.value(4).toDateTime()));
00274 }
00275
00276 return list;
00277 }