00001 #include <iostream>
00002 #include <set>
00003 #include <map>
00004
00005 #include <QImageReader>
00006
00007
00008 #include <mythuibuttonlist.h>
00009 #include <mythmainwindow.h>
00010 #include <mythdialogbox.h>
00011 #include <mythcontext.h>
00012 #include <mythdbcon.h>
00013 #include <mythdirs.h>
00014 #include <netutils.h>
00015 #include <netgrabbermanager.h>
00016
00017
00018 #include "searcheditor.h"
00019 #include "netcommon.h"
00020
00021 #define LOC QString("SearchEditor: ")
00022 #define LOC_WARN QString("SearchEditor, Warning: ")
00023 #define LOC_ERR QString("SearchEditor, Error: ")
00024
00029 SearchEditor::SearchEditor(MythScreenStack *parent,
00030 const QString name) :
00031 MythScreenType(parent, name),
00032 m_grabbers(NULL),
00033 m_busyPopup(NULL),
00034 m_popupStack(),
00035 m_manager(NULL),
00036 m_reply(NULL),
00037 m_changed(false)
00038 {
00039 m_popupStack = GetMythMainWindow()->GetStack("popup stack");
00040 }
00041
00042 SearchEditor::~SearchEditor()
00043 {
00044 qDeleteAll(m_grabberList);
00045 m_grabberList.clear();
00046
00047 if (m_manager)
00048 {
00049 m_manager->disconnect();
00050 m_manager->deleteLater();
00051 m_manager = NULL;
00052 }
00053
00054 if (m_changed)
00055 emit itemsChanged();
00056 }
00057
00058 bool SearchEditor::Create(void)
00059 {
00060
00061 bool foundtheme = LoadWindowFromXML("netvision-ui.xml", "treeeditor", this);
00062
00063 if (!foundtheme)
00064 return false;
00065
00066 bool err = false;
00067 UIUtilE::Assign(this, m_grabbers, "grabbers", &err);
00068
00069 if (err)
00070 {
00071 LOG(VB_GENERAL, LOG_ERR, "Cannot load screen 'treeeditor'");
00072 return false;
00073 }
00074
00075 connect(m_grabbers, SIGNAL(itemClicked(MythUIButtonListItem*)),
00076 this, SLOT(toggleItem(MythUIButtonListItem*)));
00077
00078 BuildFocusList();
00079
00080 loadData();
00081
00082 return true;
00083 }
00084
00085 void SearchEditor::loadData()
00086 {
00087 QString msg = tr("Querying Backend for Internet Content Sources...");
00088 createBusyDialog(msg);
00089
00090 m_manager = new QNetworkAccessManager();
00091
00092 connect(m_manager, SIGNAL(finished(QNetworkReply*)),
00093 SLOT(slotLoadedData(void)));
00094
00095 QUrl url(GetMythXMLURL() + "GetInternetSources");
00096 m_reply = m_manager->get(QNetworkRequest(url));
00097 }
00098
00099 void SearchEditor::slotLoadedData()
00100 {
00101 QDomDocument doc;
00102 doc.setContent(m_reply->readAll());
00103 QDomElement root = doc.documentElement();
00104 QDomElement content = root.firstChildElement("InternetContent");
00105 QDomElement grabber = content.firstChildElement("grabber");
00106
00107 while (!grabber.isNull())
00108 {
00109 QString title, author, image, description, type, commandline;
00110 double version;
00111 bool search = false;
00112 bool tree = false;
00113
00114 title = grabber.firstChildElement("name").text();
00115 commandline = grabber.firstChildElement("command").text();
00116 author = grabber.firstChildElement("author").text();
00117 image = grabber.firstChildElement("thumbnail").text();
00118 type = grabber.firstChildElement("type").text();
00119 description = grabber.firstChildElement("description").text();
00120 version = grabber.firstChildElement("version").text().toDouble();
00121 QString treestring = grabber.firstChildElement("tree").text();
00122 if (!treestring.isEmpty() && (treestring.toLower() == "true" ||
00123 treestring.toLower() == "yes" || treestring == "1"))
00124 tree = true;
00125 QString searchstring = grabber.firstChildElement("search").text();
00126 if (!searchstring.isEmpty() && (searchstring.toLower() == "true" ||
00127 searchstring.toLower() == "yes" || searchstring == "1"))
00128 search = true;
00129
00130 if (type.toLower() == "video" && search)
00131 {
00132 LOG(VB_GENERAL, LOG_INFO,
00133 QString("Found Search Source %1...").arg(title));
00134 m_grabberList.append(new GrabberScript(title, image, VIDEO_FILE, author,
00135 search, tree, description, commandline, version));
00136 }
00137
00138 grabber = grabber.nextSiblingElement("grabber");
00139 }
00140
00141 parsedData();
00142 }
00143
00144 void SearchEditor::parsedData()
00145 {
00146 if (m_busyPopup)
00147 {
00148 m_busyPopup->Close();
00149 m_busyPopup = NULL;
00150 }
00151
00152 fillGrabberButtonList();
00153 }
00154
00155 void SearchEditor::createBusyDialog(QString title)
00156 {
00157 if (m_busyPopup)
00158 return;
00159
00160 QString message = title;
00161
00162 m_busyPopup = new MythUIBusyDialog(message, m_popupStack,
00163 "mythvideobusydialog");
00164
00165 if (m_busyPopup->Create())
00166 m_popupStack->AddScreen(m_busyPopup);
00167 else
00168 {
00169 delete m_busyPopup;
00170 m_busyPopup = NULL;
00171 }
00172 }
00173
00174 bool SearchEditor::keyPressEvent(QKeyEvent *event)
00175 {
00176 if (GetFocusWidget()->keyPressEvent(event))
00177 return true;
00178
00179 bool handled = false;
00180 QStringList actions;
00181 handled = GetMythMainWindow()->TranslateKeyPress("Internet Video", event, actions);
00182
00183 if (!handled && MythScreenType::keyPressEvent(event))
00184 handled = true;
00185
00186 return handled;
00187 }
00188
00189 void SearchEditor::fillGrabberButtonList()
00190 {
00191 for (GrabberScript::scriptList::iterator i = m_grabberList.begin();
00192 i != m_grabberList.end(); ++i)
00193 {
00194 MythUIButtonListItem *item =
00195 new MythUIButtonListItem(m_grabbers, (*i)->GetTitle());
00196 if (item)
00197 {
00198 item->SetText((*i)->GetTitle(), "title");
00199 item->SetData(qVariantFromValue(*i));
00200 QString img = (*i)->GetImage();
00201 QString thumb;
00202 if (!img.startsWith("/") && !img.isEmpty())
00203 thumb = QString("%1mythnetvision/icons/%2").arg(GetShareDir())
00204 .arg((*i)->GetImage());
00205 else
00206 thumb = img;
00207 item->SetImage(thumb);
00208 item->setCheckable(true);
00209 item->setChecked(MythUIButtonListItem::NotChecked);
00210 QFileInfo fi((*i)->GetCommandline());
00211 if (findSearchGrabberInDB(fi.fileName(), VIDEO_FILE))
00212 item->setChecked(MythUIButtonListItem::FullChecked);
00213 }
00214 else
00215 delete item;
00216 }
00217 }
00218
00219 void SearchEditor::toggleItem(MythUIButtonListItem *item)
00220 {
00221 if (!item)
00222 return;
00223
00224 GrabberScript *script = qVariantValue<GrabberScript *>(item->GetData());
00225
00226 if (!script)
00227 return;
00228
00229 bool checked = (item->state() == MythUIButtonListItem::FullChecked);
00230
00231 if (!checked)
00232 {
00233 if (insertSearchInDB(script, VIDEO_FILE))
00234 {
00235 m_changed = true;
00236 item->setChecked(MythUIButtonListItem::FullChecked);
00237 }
00238 }
00239 else
00240 {
00241 if (removeSearchFromDB(script))
00242 {
00243 m_changed = true;
00244 item->setChecked(MythUIButtonListItem::NotChecked);
00245 }
00246 }
00247 }
00248