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