MythTV  master
mythnewsconfig.cpp
Go to the documentation of this file.
1 // QT Headers
2 #include <QStringList>
3 #include <QFile>
4 
5 // MythTV headers
6 #include <mythuitext.h>
7 #include <mythuibuttonlist.h>
8 #include <mythcontext.h>
9 #include <mythdbcon.h>
10 #include <mythmainwindow.h>
11 #include <mythdirs.h>
12 
13 // MythNews headers
14 #include "mythnewsconfig.h"
15 #include "newsdbutil.h"
16 #include "newssite.h"
17 
18 #define LOC QString("MythNewsConfig: ")
19 #define LOC_WARN QString("MythNewsConfig, Warning: ")
20 #define LOC_ERR QString("MythNewsConfig, Error: ")
21 
22 // ---------------------------------------------------
23 
25 {
26  public:
28  QStringList m_selectedSitesList;
29 };
30 
31 // ---------------------------------------------------
32 
33 MythNewsConfig::MythNewsConfig(MythScreenStack *parent, const QString &name)
34  : MythScreenType(parent, name),
35  m_priv(new MythNewsConfigPriv),
36  m_updateFreq(gCoreContext->GetNumSetting("NewsUpdateFrequency", 30))
37 {
38  populateSites();
39 }
40 
42 {
43  delete m_priv;
44 }
45 
47 {
48  QMutexLocker locker(&m_lock);
49 
50  QString filename = QString("%1%2")
51  .arg(GetShareDir()).arg("mythnews/news-sites.xml");
52 
53  QFile xmlFile(filename);
54 
55  if (!xmlFile.exists() || !xmlFile.open(QIODevice::ReadOnly))
56  {
57  LOG(VB_GENERAL, LOG_ERR, LOC + "Cannot open news-sites.xml");
58  return;
59  }
60 
61  QString errorMsg;
62  int errorLine = 0;
63  int errorColumn = 0;
64 
65  QDomDocument domDoc;
66 
67  if (!domDoc.setContent(&xmlFile, false, &errorMsg,
68  &errorLine, &errorColumn))
69  {
70  LOG(VB_GENERAL, LOG_ERR, LOC +
71  "Could not read content of news-sites.xml" +
72  QString("\n\t\t\tError parsing %1").arg(filename) +
73  QString("\n\t\t\tat line: %1 column: %2 msg: %3")
74  .arg(errorLine).arg(errorColumn).arg(errorMsg));
75  return;
76  }
77 
78  m_priv->m_categoryList.clear();
79 
80  QDomNodeList catList =
81  domDoc.elementsByTagName(QString::fromUtf8("category"));
82 
83  QDomNode catNode;
84  QDomNode siteNode;
85  for (int i = 0; i < catList.count(); i++)
86  {
87  catNode = catList.item(i);
88 
90  cat.m_name = catNode.toElement().attribute("name");
91 
92  QDomNodeList siteList = catNode.childNodes();
93 
94  for (int j = 0; j < siteList.count(); j++)
95  {
96  siteNode = siteList.item(j);
97 
98  NewsSiteItem site = NewsSiteItem();
99  site.m_name = siteNode.namedItem(QString("title")).toElement().text();
100  site.m_category = cat.m_name;
101  site.m_url = siteNode.namedItem(QString("url")).toElement().text();
102  site.m_ico = siteNode.namedItem(QString("ico")).toElement().text();
103  site.m_podcast = false;
104  site.m_inDB = findInDB(site.m_name);
105 
106  cat.m_siteList.push_back(site);
107  }
108 
109  m_priv->m_categoryList.push_back(cat);
110  }
111 
112  xmlFile.close();
113 }
114 
116 {
117  QMutexLocker locker(&m_lock);
118 
119  // Load the theme for this screen
120  bool foundtheme = LoadWindowFromXML("news-ui.xml", "config", this);
121 
122  if (!foundtheme)
123  return false;
124 
125  bool err = false;
126  UIUtilE::Assign(this, m_categoriesList, "category", &err);
127  UIUtilE::Assign(this, m_siteList, "sites", &err);
128  UIUtilW::Assign(this, m_helpText, "help", &err);
129 
130  if (err)
131  {
132  LOG(VB_GENERAL, LOG_ERR, "Cannot load screen 'config'");
133  return false;
134  }
135 
140 
141  BuildFocusList();
142 
144 
145  loadData();
146 
147  return true;
148 }
149 
151 {
152  QMutexLocker locker(&m_lock);
153 
154  for (auto & category : m_priv->m_categoryList)
155  {
156  auto *item = new MythUIButtonListItem(m_categoriesList,category.m_name);
157  item->SetData(QVariant::fromValue(&category));
158  if (!category.m_siteList.empty())
159  item->setDrawArrow(true);
160  }
162 }
163 
165 {
166  QMutexLocker locker(&m_lock);
167 
168  if (!item )
169  return;
170 
171  auto *site = item->GetData().value<NewsSiteItem*>();
172  if (!site)
173  return;
174 
175  bool checked = (item->state() == MythUIButtonListItem::FullChecked);
176 
177  if (!checked)
178  {
179  if (insertInDB(site))
180  {
181  site->m_inDB = true;
183  }
184  }
185  else
186  {
187  if (removeFromDB(site))
188  {
189  site->m_inDB = false;
191  }
192  }
193 }
194 
196 {
197  QMutexLocker locker(&m_lock);
198 
199  if (!item)
200  return;
201 
202  m_siteList->Reset();
203 
204  auto *cat = item->GetData().value<NewsCategory*>();
205  if (!cat)
206  return;
207 
208  for (auto & site : cat->m_siteList)
209  {
210  auto *newitem =
211  new MythUIButtonListItem(m_siteList, site.m_name, nullptr, true,
212  site.m_inDB ?
215  newitem->SetData(QVariant::fromValue(&site));
216  }
217 }
218 
219 bool MythNewsConfig::keyPressEvent(QKeyEvent *event)
220 {
221  if (GetFocusWidget()->keyPressEvent(event))
222  return true;
223 
224  QStringList actions;
225  bool handled = GetMythMainWindow()->TranslateKeyPress("News", event, actions);
226 
227  if (!handled && MythScreenType::keyPressEvent(event))
228  handled = true;
229 
230  return handled;
231 }
newsdbutil.h
MythNewsConfig::loadData
void loadData(void)
Definition: mythnewsconfig.cpp:150
NewsSiteItem::m_inDB
bool m_inDB
Definition: newssite.h:33
NewsCategory
Definition: newssite.h:39
mythuitext.h
insertInDB
bool insertInDB(RSSSite *site)
Definition: netutils.cpp:644
xmlFile
QString xmlFile
Definition: mythplugins/mytharchive/mytharchivehelper/main.cpp:594
findInDB
bool findInDB(const QString &url, ArticleType type)
Definition: netutils.cpp:528
mythnewsconfig.h
NewsSiteItem::m_category
QString m_category
Definition: newssite.h:30
MythMainWindow::TranslateKeyPress
bool TranslateKeyPress(const QString &context, QKeyEvent *e, QStringList &actions, bool allowJumps=true)
Get a list of actions for a keypress in the given context.
Definition: mythmainwindow.cpp:1139
hardwareprofile.devicelist.cat
def cat(file_name)
Definition: devicelist.py:95
MythNewsConfigPriv::m_categoryList
NewsCategory::List m_categoryList
Definition: mythnewsconfig.cpp:27
MythUIButtonList::itemSelected
void itemSelected(MythUIButtonListItem *item)
MythScreenStack
Definition: mythscreenstack.h:16
arg
arg(title).arg(filename).arg(doDelete))
mythdbcon.h
MythUIButtonListItem::FullChecked
@ FullChecked
Definition: mythuibuttonlist.h:34
category
QString category
Definition: mythplugins/mytharchive/mytharchivehelper/main.cpp:1422
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23
MythScreenType
Screen in which all other widgets are contained and rendered.
Definition: mythscreentype.h:45
mythdirs.h
LOC
#define LOC
Definition: mythnewsconfig.cpp:18
NewsCategory::List
std::vector< NewsCategory > List
Definition: newssite.h:41
mythuibuttonlist.h
NewsSiteItem::m_podcast
bool m_podcast
Definition: newssite.h:34
MythScreenType::GetFocusWidget
MythUIType * GetFocusWidget(void) const
Definition: mythscreentype.cpp:112
NewsSiteItem
Definition: newssite.h:25
MythNewsConfigPriv::m_selectedSitesList
QStringList m_selectedSitesList
Definition: mythnewsconfig.cpp:28
MythUIButtonListItem
Definition: mythuibuttonlist.h:28
MythNewsConfig::m_lock
QMutex m_lock
Definition: mythnewsconfig.h:31
MythUIButtonList::itemClicked
void itemClicked(MythUIButtonListItem *item)
NewsSiteItem::m_url
QString m_url
Definition: newssite.h:31
MythScreenType::SetFocusWidget
bool SetFocusWidget(MythUIType *widget=nullptr)
Definition: mythscreentype.cpp:117
removeFromDB
bool removeFromDB(RSSSite *site)
Definition: netutils.cpp:686
GetShareDir
QString GetShareDir(void)
Definition: mythdirs.cpp:222
MythNewsConfigPriv
Definition: mythnewsconfig.cpp:25
MythScreenType::BuildFocusList
void BuildFocusList(void)
Definition: mythscreentype.cpp:222
MythNewsConfig::m_helpText
MythUIText * m_helpText
Definition: mythnewsconfig.h:37
filename
QString filename
Definition: mythplugins/mytharchive/mytharchivehelper/main.cpp:637
MythUIButtonListItem::GetData
QVariant GetData()
Definition: mythuibuttonlist.cpp:3580
gCoreContext
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
Definition: mythcorecontext.cpp:56
NewsSiteItem::m_name
QString m_name
Definition: newssite.h:29
UIUtilDisp::Assign
static bool Assign(ContainerType *container, UIType *&item, const QString &name, bool *err=nullptr)
Definition: mythuiutils.h:27
newssite.h
MythScreenType::keyPressEvent
bool keyPressEvent(QKeyEvent *event) override
Key event handler.
Definition: mythscreentype.cpp:414
MythNewsConfig::~MythNewsConfig
~MythNewsConfig() override
Definition: mythnewsconfig.cpp:41
MythNewsConfig::Create
bool Create(void) override
Definition: mythnewsconfig.cpp:115
XMLParseBase::LoadWindowFromXML
static bool LoadWindowFromXML(const QString &xmlfile, const QString &windowname, MythUIType *parent)
Definition: xmlparsebase.cpp:692
MythNewsConfig::m_priv
MythNewsConfigPriv * m_priv
Definition: mythnewsconfig.h:32
MythUIButtonListItem::state
CheckState state() const
Definition: mythuibuttonlist.cpp:3534
mythcontext.h
MythUIButtonList::Reset
void Reset() override
Reset the widget to it's original state, should not reset changes made by the theme.
Definition: mythuibuttonlist.cpp:114
GetMythMainWindow
MythMainWindow * GetMythMainWindow(void)
Definition: mythmainwindow.cpp:105
MythNewsConfig::keyPressEvent
bool keyPressEvent(QKeyEvent *event) override
Key event handler.
Definition: mythnewsconfig.cpp:219
MythNewsConfig::toggleItem
void toggleItem(MythUIButtonListItem *item)
Definition: mythnewsconfig.cpp:164
MythNewsConfig::MythNewsConfig
MythNewsConfig(MythScreenStack *parent, const QString &name)
Definition: mythnewsconfig.cpp:33
NewsSiteItem::m_ico
QString m_ico
Definition: newssite.h:32
MythNewsConfig::m_categoriesList
MythUIButtonList * m_categoriesList
Definition: mythnewsconfig.h:34
MythUIButtonList::GetItemFirst
MythUIButtonListItem * GetItemFirst() const
Definition: mythuibuttonlist.cpp:1636
MythNewsConfig::m_siteList
MythUIButtonList * m_siteList
Definition: mythnewsconfig.h:35
mythmainwindow.h
MythUIButtonListItem::setChecked
void setChecked(CheckState state)
Definition: mythuibuttonlist.cpp:3544
MythNewsConfig::slotCategoryChanged
void slotCategoryChanged(MythUIButtonListItem *item)
Definition: mythnewsconfig.cpp:195
MythUIButtonListItem::NotChecked
@ NotChecked
Definition: mythuibuttonlist.h:32
MythNewsConfig::populateSites
void populateSites(void)
Definition: mythnewsconfig.cpp:46