00001
00002
00003 #include "themeinfo.h"
00004
00005
00006 #include <QCoreApplication>
00007 #include <QFile>
00008 #include <QDir>
00009 #include <QDomElement>
00010
00011
00012 #include "mythlogging.h"
00013 #include "mythdownloadmanager.h"
00014
00015 #define LOC QString("ThemeInfo: ")
00016
00017 ThemeInfo::ThemeInfo(QString theme)
00018 :XMLParseBase()
00019 {
00020 m_theme = QFileInfo(theme);
00021 m_type = THEME_UNKN;
00022 m_baseres = QSize(800, 600);
00023 m_majorver = m_minorver = 0;
00024
00025 if (m_theme.exists())
00026 m_themeurl = m_theme.absoluteFilePath();
00027 else
00028 m_themeurl = theme;
00029
00030 if (!parseThemeInfo())
00031 {
00032 LOG(VB_GENERAL, LOG_ERR, LOC +
00033 QString("The theme (%1) is missing a themeinfo.xml file.")
00034 .arg(m_themeurl));
00035 }
00036 }
00037
00038 ThemeInfo::~ThemeInfo()
00039 {
00040 }
00041
00042 bool ThemeInfo::parseThemeInfo()
00043 {
00044
00045 QDomDocument doc;
00046
00047 if ((m_themeurl.startsWith("http://")) ||
00048 (m_themeurl.startsWith("https://")) ||
00049 (m_themeurl.startsWith("ftp://")) ||
00050 (m_themeurl.startsWith("myth://")))
00051 {
00052 QByteArray data;
00053 bool ok = GetMythDownloadManager()->download(m_themeurl +
00054 "/themeinfo.xml", &data);
00055 if (!ok)
00056 return false;
00057
00058 if (!doc.setContent(data))
00059 {
00060 LOG(VB_GENERAL, LOG_ERR, LOC +
00061 QString("Unable to parse themeinfo.xml for %1")
00062 .arg(m_themeurl));
00063 return false;
00064 }
00065 }
00066 else
00067 {
00068 QFile f(m_themeurl + "/themeinfo.xml");
00069
00070 if (!f.open(QIODevice::ReadOnly))
00071 {
00072 LOG(VB_GENERAL, LOG_WARNING, LOC +
00073 QString("Unable to open themeinfo.xml for %1")
00074 .arg(f.fileName()));
00075 return false;
00076 }
00077
00078 if (!doc.setContent(&f))
00079 {
00080 LOG(VB_GENERAL, LOG_ERR, LOC +
00081 QString("Unable to parse themeinfo.xml for %1")
00082 .arg(f.fileName()));
00083
00084 f.close();
00085 return false;
00086 }
00087 f.close();
00088 }
00089
00090 QDomElement docElem = doc.documentElement();
00091
00092 for (QDomNode n = docElem.firstChild(); !n.isNull();
00093 n = n.nextSibling())
00094 {
00095 QDomElement e = n.toElement();
00096 if (!e.isNull())
00097 {
00098 if (e.tagName() == "name")
00099 {
00100 m_name = getFirstText(e);
00101 }
00102 else if (e.tagName() == "aspect")
00103 {
00104 m_aspect = getFirstText(e);
00105 }
00106 else if (e.tagName() == "baseres")
00107 {
00108 QString size = getFirstText(e);
00109 m_baseres = QSize(size.section('x', 0, 0).toInt(),
00110 size.section('x', 1, 1).toInt());
00111 }
00112 else if (e.tagName() == "types")
00113 {
00114 for (QDomNode child = e.firstChild(); !child.isNull();
00115 child = child.nextSibling())
00116 {
00117 QDomElement ce = child.toElement();
00118 if (!ce.isNull())
00119 {
00120 if (ce.tagName() == "type")
00121 {
00122 QString type = getFirstText(ce);
00123
00124 if (type == "UI")
00125 {
00126 m_type |= THEME_UI;
00127 }
00128 else if (type == "OSD")
00129 {
00130 m_type |= THEME_OSD;
00131 }
00132 else if (type == "Menu")
00133 {
00134 m_type |= THEME_MENU;
00135 }
00136 else
00137 {
00138 VERBOSE_XML(VB_GENERAL, LOG_ERR,
00139 m_theme.fileName(), ce,
00140 "Invalid theme type");
00141 }
00142 }
00143 }
00144 }
00145 }
00146 else if (e.tagName() == "version")
00147 {
00148 for (QDomNode child = e.firstChild(); !child.isNull();
00149 child = child.nextSibling())
00150 {
00151 QDomElement ce = child.toElement();
00152 if (!ce.isNull())
00153 {
00154 if (ce.tagName() == "major")
00155 {
00156 m_majorver = getFirstText(ce).toInt();
00157 }
00158 else if (ce.tagName() == "minor")
00159 {
00160 m_minorver = getFirstText(ce).toInt();
00161 }
00162 }
00163 }
00164 }
00165 else if (e.tagName() == "author")
00166 {
00167 for (QDomNode child = e.firstChild(); !child.isNull();
00168 child = child.nextSibling())
00169 {
00170 QDomElement ce = child.toElement();
00171 if (!ce.isNull())
00172 {
00173 if (ce.tagName() == "name")
00174 {
00175 m_authorName = getFirstText(ce);
00176 }
00177 else if (ce.tagName() == "email")
00178 {
00179 m_authorEmail = getFirstText(ce);
00180 }
00181 }
00182 }
00183 }
00184 else if (e.tagName() == "detail")
00185 {
00186 for (QDomNode child = e.firstChild(); !child.isNull();
00187 child = child.nextSibling())
00188 {
00189 QDomElement ce = child.toElement();
00190 if (!ce.isNull())
00191 {
00192 if (ce.tagName() == "thumbnail")
00193 {
00194 if (ce.attribute("name") == "preview")
00195 {
00196 QString thumbnail = getFirstText(ce);
00197 m_previewpath = m_themeurl + '/' + thumbnail;
00198 }
00199 }
00200 else if (ce.tagName() == "description")
00201 {
00202 m_description = qApp->translate("ThemeUI",
00203 parseText(ce).toUtf8(), NULL,
00204 QCoreApplication::UnicodeUTF8);
00205 }
00206 else if (ce.tagName() == "errata")
00207 {
00208 m_errata = qApp->translate("ThemeUI",
00209 parseText(ce).toUtf8(), NULL,
00210 QCoreApplication::UnicodeUTF8);
00211 }
00212 }
00213 }
00214 }
00215 else if (e.tagName() == "downloadinfo")
00216 {
00217 for (QDomNode child = e.firstChild(); !child.isNull();
00218 child = child.nextSibling())
00219 {
00220 QDomElement ce = child.toElement();
00221 if (!ce.isNull())
00222 {
00223 if (ce.tagName() == "url")
00224 {
00225 m_downloadurl = getFirstText(ce);
00226 }
00227 }
00228 }
00229 }
00230 }
00231 }
00232
00233 return true;
00234 }
00235
00236 bool ThemeInfo::IsWide() const
00237 {
00238 if (m_aspect == "16:9" || m_aspect == "16:10")
00239 return true;
00240
00241 return false;
00242 }
00243
00244 void ThemeInfo::ToMap(QHash<QString, QString> &infoMap) const
00245 {
00246 infoMap["description"] = m_description;
00247 infoMap["name"] = m_name;
00248 infoMap["aspect"] = m_aspect;
00249 infoMap["resolution"] = QString("%1x%2").arg(m_baseres.width())
00250 .arg(m_baseres.height());
00251 infoMap["errata"] = m_errata;
00252 infoMap["majorversion"] = m_majorver;
00253 infoMap["minorversion"] = m_minorver;
00254 infoMap["version"] = QString("%1.%2").arg(m_majorver).arg(m_minorver);
00255
00256 infoMap["authorname"] = m_authorName;
00257 infoMap["authoremail"] = m_authorEmail;
00258 }