Debug-- Remove includes
[kdepim.git] / akregator / src / formatter / grantleeviewformatter.cpp
blob9abb77d3fcad6dbf5a9dcfb1798974f9ccb1dcad
1 /*
2 Copyright (C) 2016 Montel Laurent <montel@kde.org>
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; see the file COPYING. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
20 #include "grantleeviewformatter.h"
21 #include "articlegrantleeobject.h"
22 #include "utils.h"
23 #include "akregatorconfig.h"
24 #include <KLocalizedString>
26 #include <grantlee/engine.h>
28 #include <QVariantHash>
29 #include <QApplication>
30 #include <QDateTime>
31 #include <QVariantList>
32 #include <folder.h>
33 #include <feed.h>
35 using namespace Akregator;
37 GrantleeViewFormatter::GrantleeViewFormatter(const QString &htmlFileName, const QString &themePath, const QUrl &imageDir, int deviceDpiY, QObject *parent)
38 : PimCommon::GenericGrantleeFormatter(htmlFileName, themePath, parent),
39 mImageDir(imageDir),
40 mHtmlArticleFileName(htmlFileName),
41 mGrantleeThemePath(QStringLiteral("file://") + themePath + QLatin1Char('/')),
42 mDeviceDpiY(deviceDpiY)
44 mDirectionString = QApplication::isRightToLeft() ? QStringLiteral("rtl") : QStringLiteral("ltr");
47 GrantleeViewFormatter::~GrantleeViewFormatter()
52 int GrantleeViewFormatter::pointsToPixel(int pointSize) const
54 return (pointSize * mDeviceDpiY + 36) / 72;
57 void GrantleeViewFormatter::addStandardObject(QVariantHash &grantleeObject)
59 grantleeObject.insert(QStringLiteral("absoluteThemePath"), mGrantleeThemePath);
60 grantleeObject.insert(QStringLiteral("applicationDir"), mDirectionString);
61 grantleeObject.insert(QStringLiteral("standardFamilyFont"), Settings::standardFont());
62 grantleeObject.insert(QStringLiteral("mediumFontSize"), pointsToPixel(Settings::mediumFontSize()));
65 QString GrantleeViewFormatter::formatFeed(Akregator::Feed *feed)
67 setDefaultHtmlMainFile(QStringLiteral("defaultnormalvisitfeed.html"));
68 if (!errorMessage().isEmpty()) {
69 return errorMessage();
71 QVariantHash feedObject;
72 addStandardObject(feedObject);
73 feedObject.insert(QStringLiteral("strippedTitle"), Utils::stripTags(feed->title()));
74 QString numberOfArticle;
75 if (feed->unread() == 0) {
76 numberOfArticle = i18n(" (no unread articles)");
77 } else {
78 numberOfArticle = i18np(" (1 unread article)", " (%1 unread articles)", feed->unread());
81 feedObject.insert(QStringLiteral("feedCount"), numberOfArticle);
83 QString feedImage;
84 if (!feed->image().isNull()) { // image
85 feedImage = QLatin1String("<div class=\"body\">");
86 QString file = Utils::fileNameForUrl(feed->xmlUrl());
87 QUrl u(mImageDir);
88 u = u.adjusted(QUrl::RemoveFilename);
89 u.setPath(u.path() + file);
90 feedImage = QStringLiteral("<a href=\"%1\"><img class=\"headimage\" src=\"%2.png\"></a>\n").arg(feed->htmlUrl(), u.url());
91 } else {
92 feedImage = QStringLiteral("<div class=\"body\">");
94 feedObject.insert(QStringLiteral("feedImage"), feedImage);
96 if (!feed->description().isEmpty()) {
97 QString feedDescription;
98 feedDescription = QStringLiteral("<div dir=\"%1\">").arg(mDirectionString);
99 feedDescription += i18n("<b>Description:</b> %1<br />", feed->description());
100 feedDescription += QStringLiteral("</div>"); // /description
101 feedObject.insert(QStringLiteral("feedDescription"), feedDescription);
104 if (!feed->htmlUrl().isEmpty()) {
105 QString feedHomePage;
106 feedHomePage = QStringLiteral("<div dir=\"%1\">").arg(mDirectionString);
107 feedHomePage += i18n("<b>Homepage:</b> <a href=\"%1\">%2</a>", feed->htmlUrl(), feed->htmlUrl());
108 feedHomePage += QStringLiteral("</div>"); // / link
109 feedObject.insert(QStringLiteral("feedHomePage"), feedHomePage);
112 return render(feedObject);
115 QString GrantleeViewFormatter::formatFolder(Akregator::Folder *node)
117 setDefaultHtmlMainFile(QStringLiteral("defaultnormalvisitfolder.html"));
118 if (!errorMessage().isEmpty()) {
119 return errorMessage();
121 QVariantHash folderObject;
122 addStandardObject(folderObject);
124 folderObject.insert(QStringLiteral("nodeTitle"), node->title());
125 QString numberOfArticle;
126 if (node->unread() == 0) {
127 numberOfArticle = i18n(" (no unread articles)");
128 } else {
129 numberOfArticle = i18np(" (1 unread article)", " (%1 unread articles)", node->unread());
132 folderObject.insert(QStringLiteral("nodeCount"), numberOfArticle);
133 return render(folderObject);
136 QString GrantleeViewFormatter::formatArticles(const QVector<Article> &article, ArticleFormatter::IconOption icon)
138 setDefaultHtmlMainFile(mHtmlArticleFileName);
139 if (!errorMessage().isEmpty()) {
140 return errorMessage();
143 QVariantHash articleObject;
145 QVariantList articlesList;
146 const int nbArticles(article.count());
147 articlesList.reserve(nbArticles);
148 QList<ArticleGrantleeObject *> lstObj;
149 lstObj.reserve(nbArticles);
150 for (int i = 0; i < nbArticles; ++i) {
151 ArticleGrantleeObject *articleObj = new ArticleGrantleeObject(mImageDir, article.at(i), icon);
152 articlesList << QVariant::fromValue(static_cast<QObject *>(articleObj));
153 lstObj.append(articleObj);
155 articleObject.insert(QStringLiteral("articles"), articlesList);
157 addStandardObject(articleObject);
159 articleObject.insert(QStringLiteral("dateI18n"), i18n("Date"));
160 articleObject.insert(QStringLiteral("commentI18n"), i18n("Comment"));
161 articleObject.insert(QStringLiteral("completeStoryI18n"), i18n("Complete Story"));
162 articleObject.insert(QStringLiteral("authorI18n"), i18n("Author"));
163 articleObject.insert(QStringLiteral("enclosureI18n"), i18n("Enclosure"));
165 const QString str = render(articleObject);
166 qDeleteAll(lstObj);
167 return str;