5 * th9x - http://code.google.com/p/th9x
6 * er9x - http://code.google.com/p/er9x
7 * gruvin9x - http://code.google.com/p/gruvin9x
9 * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
22 #include "helpers_html.h"
23 #include <QStringList>
26 #include <QTextStream>
28 QString
tdAlign(const QString
& s
, const QString
& align
, const QString
& color
, bool bold
)
31 if (bold
) str
= "<b>" + str
+ "</b>";
32 if (!color
.isEmpty()) str
= "<font color='" + color
+ "'>" + str
+ "</font>";
33 return "<td align='" + align
+ "'>" + str
+ "</td>";
36 QString
doTC(const QString
& s
, const QString
& color
, bool bold
)
38 return tdAlign(s
, "center", color
, bold
);
41 QString
doTR(const QString
& s
, const QString
& color
, bool bold
)
43 return tdAlign(s
, "right", color
, bold
);
46 QString
doTL(const QString
& s
, const QString
& color
, bool bold
)
48 return tdAlign(s
, "left", color
, bold
);
51 QString
fv(const QString
& name
, const QString
& value
, const QString
& color
)
53 return "<b>" + name
+ ": </b><font color='" + color
+ "'>" + value
+ "</font><br>";
56 QString
doTableCell(const QString
& s
, const unsigned int width
, const QString
& align
, const QString
& color
, bool bold
)
60 prfx
.append(QString(" width='%1%'").arg(QString::number(width
)));
62 prfx
.append(QString(" align='%1'").arg(align
));
67 str
= "<b>" + str
+ "</b>";
69 str
= QString("<font color='%1'>%2</font>").arg(color
).arg(str
);
71 str
= prfx
+ str
+ "</td>";
75 QString
doTableRow(const QStringList
& strl
, const unsigned int width
, const QString
& align
, const QString
& color
, bool bold
)
78 for (int i
=0; i
<strl
.count(); i
++) {
79 str
.append(doTableCell(strl
.at(i
), width
, align
, color
, bold
));
81 return "<tr>" + str
+ "</tr>";
84 QString
doTableBlankRow()
89 Stylesheet::Stylesheet(const QString
& name
):
95 Stylesheet::Stylesheet(const QString
& name
, const StyleType styleType
):
99 mResult
= load(styleType
);
102 Stylesheet::~Stylesheet()
106 void Stylesheet::init()
111 QStringList p
= QStandardPaths::standardLocations(QStandardPaths::AppDataLocation
);
113 mCustomFile
= p
[0] + "/" + mName
;
114 mDefaultFile
= THEMES_DEFAULT_PATH
+ mName
;
117 bool Stylesheet::load(const StyleType styleType
)
121 case STYLE_TYPE_NONE
:
122 case STYLE_TYPE_DEFAULT
:
123 mResult
= read(mDefaultFile
);
124 case STYLE_TYPE_CUSTOM
:
125 mResult
= read(mCustomFile
);
126 case STYLE_TYPE_EFFECTIVE
:
127 mResult
= read(mCustomFile
);
129 mResult
= read(mDefaultFile
);
134 bool Stylesheet::read(const QString
& path
)
141 if (file
.open(QFile::ReadOnly
| QFile::Text
)) {
142 QTextStream
in(&file
);
143 if (in
.status()==QTextStream::Ok
) {
144 mText
= in
.readAll();
145 if (in
.status()==QTextStream::Ok
)
154 mErrormsg
= tr("Style sheet data read from '%1'").arg(QDir::toNativeSeparators(path
));
156 mErrormsg
= tr("Style sheet data unable to be read from '%1'").arg(QDir::toNativeSeparators(path
));
157 qDebug() << mErrormsg
;
161 bool Stylesheet::update()
165 QDir
path(mCustomPath
);
166 if (!path
.exists()) {
168 if (!dir
.mkpath(mCustomPath
))
169 mErrormsg
= tr("Cannot create folder '%1'").arg(QDir::toNativeSeparators(mCustomPath
));
172 QFile
file(mCustomFile
);
173 if (!file
.open(QFile::WriteOnly
| QFile::Text
)) {
174 mErrormsg
= tr("Cannot open file for writing '%1': Error: %2").arg(QDir::toNativeSeparators(mCustomFile
), file
.errorString());
177 QTextStream
out(&file
);
178 if (out
.status()==QTextStream::Ok
) {
180 if (!(out
.status()==QTextStream::Ok
)) {
181 mErrormsg
= tr("Cannot write to file '%1': Error: %2").arg(QDir::toNativeSeparators(mCustomFile
), file
.errorString());
183 mErrormsg
= tr("Cannot flush buffer for file '%1': Error: %2").arg(QDir::toNativeSeparators(mCustomFile
), file
.errorString());
188 mErrormsg
= tr("Style sheet written to '%1'").arg(QDir::toNativeSeparators(mCustomFile
));
194 qDebug() << mErrormsg
;
198 bool Stylesheet::deleteCustom()
200 QFile
file(mCustomFile
);
201 mResult
= file
.remove();
203 mErrormsg
= tr("Custom style sheet deleted: '%1'").arg(QDir::toNativeSeparators(mCustomFile
));
205 mErrormsg
= tr("Unable to delete custom style sheet: '%1'").arg(QDir::toNativeSeparators(mCustomFile
));
206 qDebug() << mErrormsg
;
210 QString
Stylesheet::name()
215 QString
Stylesheet::text()
220 void Stylesheet::setText(const QString
& text
)
225 QString
Stylesheet::errormsg()