MythTV  0.26-pre
mythnewsconfig.cpp
Go to the documentation of this file.
00001 // QT Headers
00002 #include <QApplication>
00003 #include <QStringList>
00004 #include <QFile>
00005 
00006 // MythTV headers
00007 #include <mythuitext.h>
00008 #include <mythuibuttonlist.h>
00009 #include <mythcontext.h>
00010 #include <mythdbcon.h>
00011 #include <mythmainwindow.h>
00012 #include <mythdirs.h>
00013 
00014 // MythNews headers
00015 #include "mythnewsconfig.h"
00016 #include "newsdbutil.h"
00017 #include "newssite.h"
00018 
00019 #define LOC      QString("MythNewsConfig: ")
00020 #define LOC_WARN QString("MythNewsConfig, Warning: ")
00021 #define LOC_ERR  QString("MythNewsConfig, Error: ")
00022 
00023 // ---------------------------------------------------
00024 
00025 class MythNewsConfigPriv
00026 {
00027   public:
00028     NewsCategory::List categoryList;
00029     QStringList selectedSitesList;
00030 };
00031 
00032 // ---------------------------------------------------
00033 
00034 MythNewsConfig::MythNewsConfig(MythScreenStack *parent, const QString &name)
00035     : MythScreenType(parent, name),
00036       m_lock(QMutex::Recursive),
00037       m_priv(new MythNewsConfigPriv), m_categoriesList(NULL),
00038       m_siteList(NULL),               m_helpText(NULL),
00039       m_contextText(NULL),
00040       m_updateFreq(gCoreContext->GetNumSetting("NewsUpdateFrequency", 30))
00041 {
00042     populateSites();
00043 }
00044 
00045 MythNewsConfig::~MythNewsConfig()
00046 {
00047     delete m_priv;
00048 }
00049 
00050 void MythNewsConfig::populateSites(void)
00051 {
00052     QMutexLocker locker(&m_lock);
00053 
00054     QString filename = QString("%1%2")
00055         .arg(GetShareDir()).arg("mythnews/news-sites.xml");
00056 
00057     QFile xmlFile(filename);
00058 
00059     if (!xmlFile.exists() || !xmlFile.open(QIODevice::ReadOnly))
00060     {
00061         LOG(VB_GENERAL, LOG_ERR, LOC + "Cannot open news-sites.xml");
00062         return;
00063     }
00064 
00065     QString errorMsg;
00066     int errorLine = 0;
00067     int errorColumn = 0;
00068 
00069     QDomDocument domDoc;
00070 
00071     if (!domDoc.setContent(&xmlFile, false, &errorMsg,
00072                            &errorLine, &errorColumn))
00073     {
00074         LOG(VB_GENERAL, LOG_ERR, LOC +
00075             "Could not read content of news-sites.xml" +
00076                 QString("\n\t\t\tError parsing %1").arg(filename) +
00077                 QString("\n\t\t\tat line: %1  column: %2 msg: %3")
00078                 .arg(errorLine).arg(errorColumn).arg(errorMsg));
00079         return;
00080     }
00081 
00082     m_priv->categoryList.clear();
00083 
00084     QDomNodeList catList =
00085         domDoc.elementsByTagName(QString::fromUtf8("category"));
00086 
00087     QDomNode catNode;
00088     QDomNode siteNode;
00089     for (int i = 0; i < catList.count(); i++)
00090     {
00091         catNode = catList.item(i);
00092 
00093         NewsCategory cat;
00094         cat.name = catNode.toElement().attribute("name");
00095 
00096         QDomNodeList siteList = catNode.childNodes();
00097 
00098         for (int j = 0; j < siteList.count(); j++)
00099         {
00100             siteNode = siteList.item(j);
00101 
00102             NewsSiteItem site = NewsSiteItem();
00103             site.name = siteNode.namedItem(QString("title")).toElement().text();
00104             site.category = cat.name;
00105             site.url = siteNode.namedItem(QString("url")).toElement().text();
00106             site.ico = siteNode.namedItem(QString("ico")).toElement().text();
00107             site.podcast = false;
00108             site.inDB = findInDB(site.name);
00109 
00110             cat.siteList.push_back(site);
00111         }
00112 
00113         m_priv->categoryList.push_back(cat);
00114     }
00115 
00116     xmlFile.close();
00117 }
00118 
00119 bool MythNewsConfig::Create(void)
00120 {
00121     QMutexLocker locker(&m_lock);
00122 
00123     // Load the theme for this screen
00124     bool foundtheme = LoadWindowFromXML("news-ui.xml", "config", this);
00125 
00126     if (!foundtheme)
00127         return false;
00128 
00129     bool err = false;
00130     UIUtilE::Assign(this, m_categoriesList, "category", &err);
00131     UIUtilE::Assign(this, m_siteList, "sites", &err);
00132     UIUtilW::Assign(this, m_helpText, "help", &err);
00133 
00134     if (err)
00135     {
00136         LOG(VB_GENERAL, LOG_ERR, "Cannot load screen 'config'");
00137         return false;
00138     }
00139 
00140     connect(m_categoriesList, SIGNAL(itemSelected(MythUIButtonListItem*)),
00141             this, SLOT(slotCategoryChanged(MythUIButtonListItem*)));
00142     connect(m_siteList, SIGNAL(itemClicked(MythUIButtonListItem*)),
00143             this, SLOT(toggleItem(MythUIButtonListItem*)));
00144 
00145     BuildFocusList();
00146 
00147     SetFocusWidget(m_categoriesList);
00148 
00149     loadData();
00150 
00151     return true;
00152 }
00153 
00154 void MythNewsConfig::loadData(void)
00155 {
00156     QMutexLocker locker(&m_lock);
00157 
00158     NewsCategory::List::iterator it = m_priv->categoryList.begin();
00159     for (; it != m_priv->categoryList.end(); ++it)
00160     {
00161         MythUIButtonListItem *item =
00162             new MythUIButtonListItem(m_categoriesList, (*it).name);
00163         item->SetData(qVariantFromValue(&(*it)));
00164         if (!(*it).siteList.empty())
00165             item->setDrawArrow(true);
00166     }
00167     slotCategoryChanged(m_categoriesList->GetItemFirst());
00168 }
00169 
00170 void MythNewsConfig::toggleItem(MythUIButtonListItem *item)
00171 {
00172     QMutexLocker locker(&m_lock);
00173 
00174     if (!item )
00175         return;
00176 
00177     NewsSiteItem *site = qVariantValue<NewsSiteItem*>(item->GetData());
00178     if (!site)
00179         return;
00180 
00181     bool checked = (item->state() == MythUIButtonListItem::FullChecked);
00182 
00183     if (!checked)
00184     {
00185         if (insertInDB(site))
00186         {
00187             site->inDB = true;
00188             item->setChecked(MythUIButtonListItem::FullChecked);
00189         }
00190     }
00191     else
00192     {
00193         if (removeFromDB(site))
00194         {
00195             site->inDB = false;
00196             item->setChecked(MythUIButtonListItem::NotChecked);
00197         }
00198     }
00199 }
00200 
00201 void MythNewsConfig::slotCategoryChanged(MythUIButtonListItem *item)
00202 {
00203     QMutexLocker locker(&m_lock);
00204 
00205     if (!item)
00206         return;
00207 
00208     m_siteList->Reset();
00209 
00210     NewsCategory *cat = qVariantValue<NewsCategory*>(item->GetData());
00211     if (!cat)
00212         return;
00213 
00214     NewsSiteItem::List::iterator it = cat->siteList.begin();
00215     for (; it != cat->siteList.end(); ++it)
00216     {
00217         MythUIButtonListItem *newitem =
00218             new MythUIButtonListItem(m_siteList, (*it).name, 0, true,
00219                                      (*it).inDB ?
00220                                      MythUIButtonListItem::FullChecked :
00221                                      MythUIButtonListItem::NotChecked);
00222         newitem->SetData(qVariantFromValue(&(*it)));
00223     }
00224 }
00225 
00226 bool MythNewsConfig::keyPressEvent(QKeyEvent *event)
00227 {
00228     if (GetFocusWidget()->keyPressEvent(event))
00229         return true;
00230 
00231     bool handled = false;
00232     QStringList actions;
00233     handled = GetMythMainWindow()->TranslateKeyPress("News", event, actions);
00234 
00235     if (!handled && MythScreenType::keyPressEvent(event))
00236         handled = true;
00237 
00238     return handled;
00239 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends