00001 #include <QCoreApplication>
00002 #include <QRegExp>
00003 #include <QBuffer>
00004 #include <QDir>
00005 #include <QFileInfo>
00006
00007 #include "mythdb.h"
00008 #include "mythdirs.h"
00009 #include "mythlogging.h"
00010 #include "httpcomms.h"
00011 #include "importicons.h"
00012 #include "mythmiscutil.h"
00013
00014
00015 #include "mythuitext.h"
00016 #include "mythuibutton.h"
00017 #include "mythuibuttonlist.h"
00018 #include "mythuitextedit.h"
00019 #include "mythdialogbox.h"
00020 #include "mythprogressdialog.h"
00021
00022 ImportIconsWizard::ImportIconsWizard(MythScreenStack *parent, bool fRefresh,
00023 const QString &channelname)
00024 :MythScreenType(parent, "ChannelIconImporter"),
00025 m_strChannelname(channelname), m_fRefresh(fRefresh),
00026 m_nMaxCount(0), m_nCount(0),
00027 m_missingMaxCount(0), m_missingCount(0),
00028 m_url("http://services.mythtv.org/channel-icon/"), m_progressDialog(NULL),
00029 m_iconsList(NULL), m_manualEdit(NULL),
00030 m_nameText(NULL), m_manualButton(NULL),
00031 m_skipButton(NULL), m_statusText(NULL),
00032 m_preview(NULL), m_previewtitle(NULL)
00033 {
00034 m_strChannelname.detach();
00035 LOG(VB_GENERAL, LOG_INFO,
00036 QString("Fetch Icons for channel %1").arg(m_strChannelname));
00037
00038 m_popupStack = GetMythMainWindow()->GetStack("popup stack");
00039
00040 m_tmpDir = QDir(QString("%1/icontmp").arg(GetConfDir()));
00041
00042 if (!m_tmpDir.exists())
00043 m_tmpDir.mkpath(m_tmpDir.absolutePath());
00044 }
00045
00046 ImportIconsWizard::~ImportIconsWizard()
00047 {
00048 if (m_tmpDir.exists())
00049 {
00050 QStringList files = m_tmpDir.entryList();
00051 for (int i = 0; i < files.size(); ++i)
00052 {
00053 m_tmpDir.remove(files.at(i));
00054 }
00055 m_tmpDir.rmpath(m_tmpDir.absolutePath());
00056 }
00057 }
00058
00059 bool ImportIconsWizard::Create()
00060 {
00061 if (!initialLoad(m_strChannelname))
00062 return false;
00063
00064
00065 bool foundtheme = false;
00066
00067
00068 foundtheme = LoadWindowFromXML("config-ui.xml", "iconimport", this);
00069
00070 if (!foundtheme)
00071 return false;
00072
00073 m_iconsList = dynamic_cast<MythUIButtonList *>(GetChild("icons"));
00074 m_manualEdit = dynamic_cast<MythUITextEdit *>(GetChild("manualsearch"));
00075 m_nameText = dynamic_cast<MythUIText *>(GetChild("name"));
00076 m_manualButton = dynamic_cast<MythUIButton *>(GetChild("search"));
00077 m_skipButton = dynamic_cast<MythUIButton *>(GetChild("skip"));
00078 m_statusText = dynamic_cast<MythUIText *>(GetChild("status"));
00079 m_preview = dynamic_cast<MythUIImage *>(GetChild("preview"));
00080 m_previewtitle = dynamic_cast<MythUIText *>(GetChild("previewtitle"));
00081
00082 if (!m_iconsList || !m_manualEdit || !m_nameText || !m_manualButton ||
00083 !m_skipButton || !m_statusText)
00084 {
00085 LOG(VB_GENERAL, LOG_ERR,
00086 "Unable to load window 'iconimport', missing required element(s)");
00087 return false;
00088 }
00089
00090 m_nameText->SetEnabled(false);
00091
00092 m_nameText->SetHelpText(tr("Name of the icon file"));
00093 m_iconsList->SetHelpText(tr("List of possible icon files"));
00094 m_manualEdit->SetHelpText(tr("Enter text here for the manual search"));
00095 m_manualButton->SetHelpText(tr("Manually search for the text"));
00096 m_skipButton->SetHelpText(tr("Skip this icon"));
00097
00098 connect(m_manualButton, SIGNAL(Clicked()), SLOT(manualSearch()));
00099 connect(m_skipButton, SIGNAL(Clicked()), SLOT(skip()));
00100 connect(m_iconsList, SIGNAL(itemClicked(MythUIButtonListItem *)),
00101 SLOT(menuSelection(MythUIButtonListItem *)));
00102 connect(m_iconsList, SIGNAL(itemSelected(MythUIButtonListItem *)),
00103 SLOT(itemChanged(MythUIButtonListItem *)));
00104
00105 BuildFocusList();
00106
00107 enableControls(STATE_NORMAL);
00108
00109 return true;
00110 }
00111
00112 void ImportIconsWizard::Init()
00113 {
00114 if (m_nMaxCount > 0)
00115 {
00116 m_missingIter = m_missingEntries.begin();
00117 if (!doLoad())
00118 {
00119 if (!m_strMatches.isEmpty())
00120 askSubmit(m_strMatches);
00121 }
00122 }
00123 }
00124
00125 void ImportIconsWizard::enableControls(dialogState state, bool selectEnabled)
00126 {
00127 switch (state)
00128 {
00129 case STATE_NORMAL:
00130 if (m_missingCount + 1 == m_missingMaxCount)
00131 m_skipButton->SetText(tr("Finish"));
00132 else
00133 m_skipButton->SetText(tr("Skip"));
00134 m_skipButton->SetEnabled(true);
00135 m_nameText->SetEnabled(true);
00136 m_iconsList->SetEnabled(true);
00137 m_manualEdit->SetEnabled(true);
00138 m_manualButton->SetEnabled(true);
00139 break;
00140 case STATE_SEARCHING:
00141 m_skipButton->SetEnabled(false);
00142 m_manualButton->SetEnabled(false);
00143 m_iconsList->SetEnabled(false);
00144 m_iconsList->Reset();
00145 m_manualEdit->Reset();
00146 break;
00147 case STATE_DISABLED:
00148 m_skipButton->SetEnabled(false);
00149 m_manualButton->SetEnabled(false);
00150 m_iconsList->SetEnabled(false);
00151 m_iconsList->Reset();
00152 m_nameText->SetEnabled(false);
00153 m_nameText->Reset();
00154 m_manualEdit->SetEnabled(false);
00155 m_manualEdit->Reset();
00156 SetFocusWidget(m_iconsList);
00157 break;
00158 }
00159 }
00160
00161 void ImportIconsWizard::manualSearch()
00162 {
00163 QString str = m_manualEdit->GetText();
00164 if (!search(escape_csv(str)))
00165 m_statusText->SetText(tr("No matches found for %1") .arg(str));
00166 else
00167 m_statusText->SetText("");
00168 }
00169
00170 void ImportIconsWizard::skip()
00171 {
00172 m_missingCount++;
00173 m_missingIter++;
00174
00175 if (!doLoad())
00176 {
00177 if (!m_strMatches.isEmpty())
00178 askSubmit(m_strMatches);
00179 else
00180 Close();
00181 }
00182 }
00183
00184 void ImportIconsWizard::menuSelection(MythUIButtonListItem *item)
00185 {
00186 if (!item)
00187 return;
00188
00189 SearchEntry entry = qVariantValue<SearchEntry>(item->GetData());
00190
00191 LOG(VB_GENERAL, LOG_INFO, QString("Menu Selection: %1 %2 %3")
00192 .arg(entry.strID) .arg(entry.strName) .arg(entry.strLogo));
00193
00194 enableControls(STATE_SEARCHING);
00195
00196 CSVEntry entry2 = (*m_missingIter);
00197 m_strMatches += QString("%1,%2,%3,%4,%5,%6,%7,%8,%9\n")
00198 .arg(escape_csv(entry.strID))
00199 .arg(escape_csv(entry2.strName))
00200 .arg(escape_csv(entry2.strXmlTvId))
00201 .arg(escape_csv(entry2.strCallsign))
00202 .arg(escape_csv(entry2.strTransportId))
00203 .arg(escape_csv(entry2.strAtscMajorChan))
00204 .arg(escape_csv(entry2.strAtscMinorChan))
00205 .arg(escape_csv(entry2.strNetworkId))
00206 .arg(escape_csv(entry2.strServiceId));
00207
00208 if (checkAndDownload(entry.strLogo, entry2.strChanId))
00209 {
00210 if (m_statusText)
00211 m_statusText->SetText(tr("Icon for %1 was downloaded successfully.")
00212 .arg(entry2.strName));
00213 }
00214 else
00215 {
00216 if (m_statusText)
00217 m_statusText->SetText(tr("Failed to download the icon for %1.")
00218 .arg(entry2.strName));
00219 }
00220
00221 if (m_missingMaxCount > 1)
00222 {
00223 m_missingCount++;
00224 m_missingIter++;
00225 if (!doLoad())
00226 {
00227 if (!m_strMatches.isEmpty())
00228 askSubmit(m_strMatches);
00229 else
00230 Close();
00231 }
00232 }
00233 else
00234 {
00235 enableControls(STATE_DISABLED);
00236
00237 SetFocusWidget(m_iconsList);
00238 if (!m_strMatches.isEmpty())
00239 askSubmit(m_strMatches);
00240 else
00241 Close();
00242 }
00243
00244 }
00245
00246 void ImportIconsWizard::itemChanged(MythUIButtonListItem *item)
00247 {
00248 if (!item)
00249 return;
00250
00251 if (m_preview)
00252 {
00253 m_preview->Reset();
00254 QString iconpath = item->GetImage("icon");
00255 if (!iconpath.isEmpty())
00256 {
00257 m_preview->SetFilename(iconpath);
00258 m_preview->Load();
00259 }
00260 }
00261
00262 if (m_previewtitle)
00263 m_previewtitle->SetText(item->GetText("iconname"));
00264 }
00265
00266 bool ImportIconsWizard::initialLoad(QString name)
00267 {
00268 QString dirpath = GetConfDir();
00269 QDir configDir(dirpath);
00270 if (!configDir.exists() && !configDir.mkdir(dirpath))
00271 {
00272 LOG(VB_GENERAL, LOG_ERR, QString("Could not create %1").arg(dirpath));
00273 }
00274
00275 m_strChannelDir = QString("%1/%2").arg(configDir.absolutePath())
00276 .arg("/channels");
00277 QDir strChannelDir(m_strChannelDir);
00278 if (!strChannelDir.exists() && !strChannelDir.mkdir(m_strChannelDir))
00279 {
00280 LOG(VB_GENERAL, LOG_ERR,
00281 QString("Could not create %1").arg(m_strChannelDir));
00282 }
00283 m_strChannelDir += "/";
00284
00285 bool closeDialog = false;
00286
00287 QString querystring("SELECT chanid, name, xmltvid, callsign,"
00288 "dtv_multiplex.transportid, atsc_major_chan, "
00289 "atsc_minor_chan, dtv_multiplex.networkid, "
00290 "channel.serviceid, channel.mplexid,"
00291 "dtv_multiplex.mplexid, channel.icon, channel.visible "
00292 "FROM channel LEFT JOIN dtv_multiplex "
00293 "ON channel.mplexid = dtv_multiplex.mplexid "
00294 "WHERE ");
00295 if (!name.isEmpty())
00296 querystring.append("name=\"" + name + "\"");
00297 else
00298 querystring.append("channel.visible");
00299 querystring.append(" ORDER BY name");
00300
00301 MSqlQuery query(MSqlQuery::InitCon());
00302 query.prepare(querystring);
00303
00304 m_listEntries.clear();
00305 m_nCount=0;
00306 m_nMaxCount=0;
00307 m_missingMaxCount=0;
00308
00309 if (query.exec() && query.size() > 0)
00310 {
00311 m_progressDialog =
00312 new MythUIProgressDialog(tr("Initializing, please wait..."),
00313 m_popupStack, "IconImportInitProgress");
00314
00315 if (m_progressDialog->Create())
00316 {
00317 m_popupStack->AddScreen(m_progressDialog);
00318 m_progressDialog->SetTotal(query.size());
00319 }
00320 else
00321 {
00322 delete m_progressDialog;
00323 m_progressDialog = NULL;
00324 }
00325
00326 while(query.next())
00327 {
00328 CSVEntry entry;
00329
00330 if (m_fRefresh)
00331 {
00332 if (QFile(query.value(11).toString()).exists())
00333 continue;
00334 }
00335
00336 entry.strChanId=query.value(0).toString();
00337 entry.strName=query.value(1).toString();
00338 entry.strXmlTvId=query.value(2).toString();
00339 entry.strCallsign=query.value(3).toString();
00340 entry.strTransportId=query.value(4).toString();
00341 entry.strAtscMajorChan=query.value(5).toString();
00342 entry.strAtscMinorChan=query.value(6).toString();
00343 entry.strNetworkId=query.value(7).toString();
00344 entry.strServiceId=query.value(8).toString();
00345 entry.strIconCSV= QString("%1,%2,%3,%4,%5,%6,%7,%8,%9\n").
00346 arg(escape_csv(entry.strChanId)).
00347 arg(escape_csv(entry.strName)).
00348 arg(escape_csv(entry.strXmlTvId)).
00349 arg(escape_csv(entry.strCallsign)).
00350 arg(escape_csv(entry.strTransportId)).
00351 arg(escape_csv(entry.strAtscMajorChan)).
00352 arg(escape_csv(entry.strAtscMinorChan)).
00353 arg(escape_csv(entry.strNetworkId)).
00354 arg(escape_csv(entry.strServiceId));
00355 entry.strNameCSV=escape_csv(entry.strName);
00356 LOG(VB_CHANNEL, LOG_INFO,
00357 QString("chanid %1").arg(entry.strIconCSV));
00358
00359 m_listEntries.append(entry);
00360 m_nMaxCount++;
00361 if (m_progressDialog)
00362 m_progressDialog->SetProgress(m_nMaxCount);
00363 }
00364
00365 if (m_progressDialog)
00366 {
00367 m_progressDialog->Close();
00368 m_progressDialog = NULL;
00369 }
00370 }
00371
00372 m_iter = m_listEntries.begin();
00373
00374 m_progressDialog = new MythUIProgressDialog(tr("Downloading, please wait..."),
00375 m_popupStack, "IconImportInitProgress");
00376
00377 if (m_progressDialog->Create())
00378 {
00379 m_popupStack->AddScreen(m_progressDialog);
00380 m_progressDialog->SetTotal(m_listEntries.size());
00381 }
00382 else
00383 {
00384 delete m_progressDialog;
00385 m_progressDialog = NULL;
00386 }
00387
00388 while (!closeDialog && (m_iter != m_listEntries.end()))
00389 {
00390 QString message = QString("Downloading %1 / %2 : ").arg(m_nCount+1)
00391 .arg(m_listEntries.size()) + (*m_iter).strName;
00392 if (m_missingEntries.size() > 0)
00393 message.append(QString("\nCould not find %1 icons.").arg(m_missingEntries.size()));
00394
00395 if (!findmissing((*m_iter).strIconCSV))
00396 {
00397 m_missingEntries.append((*m_iter));
00398 m_missingMaxCount++;
00399 }
00400
00401 m_nCount++;
00402 m_iter++;
00403 if (m_progressDialog)
00404 {
00405 m_progressDialog->SetMessage(message);
00406 m_progressDialog->SetProgress(m_nCount);
00407 }
00408 }
00409
00410 if (m_progressDialog)
00411 {
00412 m_progressDialog->Close();
00413 m_progressDialog = NULL;
00414 }
00415
00416 if (m_missingEntries.size() == 0 || closeDialog)
00417 return false;
00418
00419 if (m_nMaxCount <= 0)
00420 return false;
00421
00422 return true;
00423 }
00424
00425 bool ImportIconsWizard::doLoad()
00426 {
00427 LOG(VB_CHANNEL, LOG_INFO, QString("Icons: Found %1 / Missing %2")
00428 .arg(m_missingCount).arg(m_missingMaxCount));
00429
00430
00431 while (m_missingIter != m_missingEntries.end() &&
00432 (*m_missingIter).strName.isEmpty())
00433 {
00434 m_missingCount++;
00435 m_missingIter++;
00436 }
00437
00438 if (m_missingIter == m_missingEntries.end())
00439 {
00440 LOG(VB_CHANNEL, LOG_INFO, "doLoad Icon search complete");
00441 enableControls(STATE_DISABLED);
00442 return false;
00443 }
00444
00445
00446 m_nameText->SetText(tr("Choose icon for channel %1")
00447 .arg((*m_missingIter).strName));
00448 m_manualEdit->SetText((*m_missingIter).strName);
00449 if (!search((*m_missingIter).strNameCSV))
00450 m_statusText->SetText(tr("No matches found for %1")
00451 .arg((*m_missingIter).strName));
00452 else
00453 m_statusText->SetText("");
00454
00455 return true;
00456 }
00457
00458 QString ImportIconsWizard::escape_csv(const QString& str)
00459 {
00460 QRegExp rxDblForEscape("\"");
00461 QString str2 = str;
00462 str2.replace(rxDblForEscape,"\\\"");
00463 return "\""+str2+"\"";
00464 }
00465
00466 QStringList ImportIconsWizard::extract_csv(const QString &line)
00467 {
00468 QStringList ret;
00469 QString str;
00470 bool in_comment = false;
00471 bool in_escape = false;
00472 int comma_count = 0;
00473 for (int i = 0; i < line.length(); i++)
00474 {
00475 QChar cur = line[i];
00476 if (in_escape)
00477 {
00478 str += cur;
00479 in_escape = false;
00480 }
00481 else if (cur == '"')
00482 {
00483 in_comment = !in_comment;
00484 }
00485 else if (cur == '\\')
00486 {
00487 in_escape = true;
00488 }
00489 else if (!in_comment && (cur == ','))
00490 {
00491 ret += str;
00492 str.clear();
00493 ++comma_count;
00494 }
00495 else
00496 {
00497 str += cur;
00498 }
00499 }
00500 if (comma_count)
00501 ret += str;
00502
00503
00504 while (ret.size() < 5)
00505 ret.push_back("");
00506
00507 return ret;
00508 }
00509
00510 QString ImportIconsWizard::wget(QUrl& url,const QString& strParam )
00511 {
00512 QByteArray raw(strParam.toAscii());
00513 QBuffer data(&raw);
00514 QHttpRequestHeader header;
00515
00516 header.setContentType(QString("application/x-www-form-urlencoded"));
00517 header.setContentLength(raw.size());
00518
00519 header.setValue("User-Agent", "MythTV Channel Icon lookup bot");
00520
00521 QString str = HttpComms::postHttp(url,&header,&data);
00522
00523 return str;
00524 }
00525
00526 bool ImportIconsWizard::checkAndDownload(const QString& url, const QString& localChanId)
00527 {
00528 QString iconUrl = url;
00529 QString filename = url.section('/', -1);
00530 QFileInfo file(m_strChannelDir+filename);
00531
00532 bool fRet;
00533
00534 if (!file.exists())
00535 fRet = HttpComms::getHttpFile(file.absoluteFilePath(),iconUrl,20000);
00536 else
00537 fRet = true;
00538
00539 if (fRet)
00540 {
00541 MSqlQuery query(MSqlQuery::InitCon());
00542 QString qstr = "UPDATE channel SET icon = :ICON "
00543 "WHERE chanid = :CHANID";
00544
00545 query.prepare(qstr);
00546 query.bindValue(":ICON", file.absoluteFilePath());
00547 query.bindValue(":CHANID", localChanId);
00548
00549 if (!query.exec())
00550 {
00551 MythDB::DBError("Error inserting channel icon", query);
00552 return false;
00553 }
00554
00555 }
00556
00557 return fRet;
00558 }
00559
00560 bool ImportIconsWizard::lookup(const QString& strParam)
00561 {
00562 QString strParam1 = QUrl::toPercentEncoding("callsign="+strParam);
00563 QUrl url(m_url+"/lookup");
00564
00565 QString str = wget(url,strParam1);
00566 if (str.isEmpty() || str.startsWith("Error", Qt::CaseInsensitive))
00567 {
00568 LOG(VB_GENERAL, LOG_ERR,
00569 QString("Error from icon lookup : %1").arg(str));
00570 return true;
00571 }
00572 else
00573 {
00574 LOG(VB_CHANNEL, LOG_INFO,
00575 QString("Icon Import: Working lookup : %1").arg(str));
00576 return false;
00577 }
00578 }
00579
00580 bool ImportIconsWizard::search(const QString& strParam)
00581 {
00582
00583 QString strParam1 = QUrl::toPercentEncoding(strParam);
00584 bool retVal = false;
00585 enableControls(STATE_SEARCHING);
00586 QUrl url(m_url+"/search");
00587
00588 CSVEntry entry2 = (*m_missingIter);
00589 QString channelcsv = QString("%1,%2,%3,%4,%5,%6,%7,%8\n")
00590 .arg(escape_csv(entry2.strName))
00591 .arg(escape_csv(entry2.strXmlTvId))
00592 .arg(escape_csv(entry2.strCallsign))
00593 .arg(escape_csv(entry2.strTransportId))
00594 .arg(escape_csv(entry2.strAtscMajorChan))
00595 .arg(escape_csv(entry2.strAtscMinorChan))
00596 .arg(escape_csv(entry2.strNetworkId))
00597 .arg(escape_csv(entry2.strServiceId));
00598
00599 QString message = QObject::tr("Searching for icons for channel %1")
00600 .arg(entry2.strName);
00601
00602 OpenBusyPopup(message);
00603
00604 QString str = wget(url,"s="+strParam1+"&csv="+channelcsv);
00605 m_listSearch.clear();
00606 m_iconsList->Reset();
00607
00608 if (str.isEmpty() || str.startsWith("#") ||
00609 str.startsWith("Error", Qt::CaseInsensitive))
00610 {
00611 LOG(VB_GENERAL, LOG_ERR, QString("Error from search : %1").arg(str));
00612 retVal = false;
00613 }
00614 else
00615 {
00616 LOG(VB_CHANNEL, LOG_INFO,
00617 QString("Icon Import: Working search : %1").arg(str));
00618 QStringList strSplit = str.split("\n");
00619
00620
00621
00622
00623 if (strSplit.size() > 36*3)
00624 {
00625 LOG(VB_GENERAL, LOG_WARNING,
00626 QString("Warning: Result set contains %1 items, "
00627 "truncating to the first %2 results")
00628 .arg(strSplit.size()).arg(18*3));
00629 while (strSplit.size() > 18*3) strSplit.removeLast();
00630 }
00631
00632
00633 QString prevIconName;
00634 int namei = 1;
00635
00636 for (int x = 0; x < strSplit.size(); ++x)
00637 {
00638 QString row = strSplit[x];
00639 if (row != "#" )
00640 {
00641 QStringList ret = extract_csv(row);
00642 LOG(VB_CHANNEL, LOG_INFO,
00643 QString("Icon Import: search : %1 %2 %3")
00644 .arg(ret[0]).arg(ret[1]).arg(ret[2]));
00645 SearchEntry entry;
00646 entry.strID = ret[0];
00647 entry.strName = ret[1];
00648 entry.strLogo = ret[2];
00649 m_listSearch.append(entry);
00650
00651 MythUIButtonListItem *item;
00652 if (prevIconName == entry.strName)
00653 {
00654 QString newname = QString("%1 (%2)").arg(entry.strName)
00655 .arg(namei);
00656 item = new MythUIButtonListItem(m_iconsList, newname,
00657 qVariantFromValue(entry));
00658 namei++;
00659 }
00660 else
00661 {
00662 item = new MythUIButtonListItem(m_iconsList, entry.strName,
00663 qVariantFromValue(entry));
00664 namei=1;
00665 }
00666
00667 QString iconname = entry.strName;
00668
00669 item->SetImage(entry.strLogo, "icon", false);
00670 item->SetText(iconname, "iconname");
00671
00672 prevIconName = entry.strName;
00673 }
00674 }
00675
00676 retVal = true;
00677 }
00678 enableControls(STATE_NORMAL, retVal);
00679 CloseBusyPopup();
00680 return retVal;
00681 }
00682
00683 bool ImportIconsWizard::findmissing(const QString& strParam)
00684 {
00685 QString strParam1 = QUrl::toPercentEncoding(strParam);
00686 QUrl url(m_url+"/findmissing");
00687
00688 QString str = wget(url,"csv="+strParam1);
00689 LOG(VB_CHANNEL, LOG_INFO,
00690 QString("Icon Import: findmissing : strParam1 = %1. str = %2")
00691 .arg(strParam1).arg(str));
00692 if (str.isEmpty() || str.startsWith("#"))
00693 {
00694 return false;
00695 }
00696 else if (str.startsWith("Error", Qt::CaseInsensitive))
00697 {
00698 LOG(VB_GENERAL, LOG_ERR,
00699 QString("Error from findmissing : %1").arg(str));
00700 return false;
00701 }
00702 else
00703 {
00704 LOG(VB_CHANNEL, LOG_INFO,
00705 QString("Icon Import: Working findmissing : %1") .arg(str));
00706 QStringList strSplit = str.split("\n", QString::SkipEmptyParts);
00707 for (QStringList::const_iterator it = strSplit.begin();
00708 it != strSplit.end(); ++it)
00709 {
00710 if (*it != "#")
00711 {
00712 const QStringList ret = extract_csv(*it);
00713 LOG(VB_CHANNEL, LOG_INFO,
00714 QString("Icon Import: findmissing : %1 %2 %3 %4 %5")
00715 .arg(ret[0]).arg(ret[1]).arg(ret[2])
00716 .arg(ret[3]).arg(ret[4]));
00717 checkAndDownload(ret[4], (*m_iter).strChanId);
00718 }
00719 }
00720 return true;
00721 }
00722 }
00723
00724 void ImportIconsWizard::askSubmit(const QString& strParam)
00725 {
00726 m_strParam = strParam;
00727 QString message = tr("You now have the opportunity to transmit your "
00728 "choices back to mythtv.org so that others can "
00729 "benefit from your selections.");
00730
00731 MythConfirmationDialog *confirmPopup =
00732 new MythConfirmationDialog(m_popupStack, message);
00733
00734 confirmPopup->SetReturnEvent(this, "submitresults");
00735
00736 if (confirmPopup->Create())
00737 m_popupStack->AddScreen(confirmPopup, false);
00738 }
00739
00740 bool ImportIconsWizard::submit()
00741 {
00742 QString strParam1 = QUrl::toPercentEncoding(m_strParam);
00743 QUrl url(m_url+"/submit");
00744
00745 QString str = wget(url,"csv="+strParam1);
00746 if (str.isEmpty() || str.startsWith("#") ||
00747 str.startsWith("Error", Qt::CaseInsensitive))
00748 {
00749 LOG(VB_GENERAL, LOG_ERR, QString("Error from submit : %1").arg(str));
00750 if (m_statusText)
00751 m_statusText->SetText(tr("Failed to submit icon choices."));
00752 return false;
00753 }
00754 else
00755 {
00756 LOG(VB_CHANNEL, LOG_INFO, QString("Icon Import: Working submit : %1")
00757 .arg(str));
00758 QStringList strSplit = str.split("\n", QString::SkipEmptyParts);
00759 unsigned atsc = 0, dvb = 0, callsign = 0, tv = 0, xmltvid = 0;
00760 for (QStringList::const_iterator it = strSplit.begin();
00761 it != strSplit.end(); ++it)
00762 {
00763 if (*it == "#")
00764 continue;
00765
00766 QStringList strSplit2=(*it).split(":", QString::SkipEmptyParts);
00767 if (strSplit2.size() < 2)
00768 continue;
00769
00770 QString s = strSplit2[0].trimmed();
00771 if (s == "a")
00772 atsc = strSplit2[1].toUInt();
00773 else if (s == "c")
00774 callsign = strSplit2[1].toUInt();
00775 else if (s == "d")
00776 dvb = strSplit2[1].toUInt();
00777 else if (s == "t")
00778 tv = strSplit2[1].toUInt();
00779 else if (s == "x")
00780 xmltvid = strSplit2[1].toUInt();
00781 }
00782 LOG(VB_CHANNEL, LOG_INFO,
00783 QString("Icon Import: working submit : atsc=%1 callsign=%2 "
00784 "dvb=%3 tv=%4 xmltvid=%5")
00785 .arg(atsc).arg(callsign).arg(dvb).arg(tv).arg(xmltvid));
00786 if (m_statusText)
00787 m_statusText->SetText(tr("Icon choices submitted successfully."));
00788 return true;
00789 }
00790 }
00791
00792 void ImportIconsWizard::customEvent(QEvent *event)
00793 {
00794 if (event->type() == DialogCompletionEvent::kEventType)
00795 {
00796 DialogCompletionEvent *dce = (DialogCompletionEvent*)(event);
00797
00798 QString resultid = dce->GetId();
00799 int buttonnum = dce->GetResult();
00800
00801 if (resultid == "submitresults")
00802 {
00803 switch (buttonnum)
00804 {
00805 case 0 :
00806 Close();
00807 break;
00808 case 1 :
00809 submit();
00810 Close();
00811 break;
00812 }
00813 }
00814 }
00815 }
00816
00817 void ImportIconsWizard::Close()
00818 {
00819 MythScreenType::Close();
00820 }