grantleeviewformatter.cpp 6.14 KB
Newer Older
1
/*
Laurent Montel's avatar
Laurent Montel committed
2
   Copyright (C) 2016-2019 Montel Laurent <montel@kde.org>
Laurent Montel's avatar
Laurent Montel committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
18
19
20
*/

#include "grantleeviewformatter.h"
Laurent Montel's avatar
Laurent Montel committed
21
#include "articlegrantleeobject.h"
Laurent Montel's avatar
Laurent Montel committed
22
#include "utils.h"
23
#include "akregatorconfig.h"
Laurent Montel's avatar
Laurent Montel committed
24
#include "grantleeutil.h"
Laurent Montel's avatar
Laurent Montel committed
25
#include <KLocalizedString>
26
27
28

#include <grantlee/engine.h>

Laurent Montel's avatar
Laurent Montel committed
29
30
#include <QVariantHash>
#include <QApplication>
Laurent Montel's avatar
Laurent Montel committed
31
#include <QVariantList>
Laurent Montel's avatar
Laurent Montel committed
32
#include <folder.h>
Laurent Montel's avatar
Laurent Montel committed
33
#include <feed.h>
Laurent Montel's avatar
Laurent Montel committed
34

35
36
using namespace Akregator;

37
GrantleeViewFormatter::GrantleeViewFormatter(const QString &htmlFileName, const QString &themePath, int deviceDpiY)
38
    : GrantleeTheme::GenericFormatter(htmlFileName, themePath)
Laurent Montel's avatar
Laurent Montel committed
39
40
41
    , mHtmlArticleFileName(htmlFileName)
    , mGrantleeThemePath(QStringLiteral("file://") + themePath + QLatin1Char('/'))
    , mDeviceDpiY(deviceDpiY)
42
{
Laurent Montel's avatar
Laurent Montel committed
43
    mDirectionString = QApplication::isRightToLeft() ? QStringLiteral("rtl") : QStringLiteral("ltr");
44
    setApplicationDomain("akregator");
45
46
47
48
49
50
}

GrantleeViewFormatter::~GrantleeViewFormatter()
{
}

Laurent Montel's avatar
Laurent Montel committed
51
52
53
54
55
int GrantleeViewFormatter::pointsToPixel(int pointSize) const
{
    return (pointSize * mDeviceDpiY + 36) / 72;
}

56
void GrantleeViewFormatter::addStandardObject(QVariantHash &grantleeObject)
Laurent Montel's avatar
Laurent Montel committed
57
{
58
59
60
    grantleeObject.insert(QStringLiteral("absoluteThemePath"), mGrantleeThemePath);
    grantleeObject.insert(QStringLiteral("applicationDir"), mDirectionString);
    grantleeObject.insert(QStringLiteral("standardFamilyFont"), Settings::standardFont());
Laurent Montel's avatar
Laurent Montel committed
61
    grantleeObject.insert(QStringLiteral("mediumFontSize"), pointsToPixel(Settings::mediumFontSize()));
Laurent Montel's avatar
Laurent Montel committed
62
63
}

Laurent Montel's avatar
Laurent Montel committed
64
QString GrantleeViewFormatter::formatFeed(Akregator::Feed *feed)
65
{
Laurent Montel's avatar
Laurent Montel committed
66
67
68
69
    setDefaultHtmlMainFile(QStringLiteral("defaultnormalvisitfeed.html"));
    if (!errorMessage().isEmpty()) {
        return errorMessage();
    }
Laurent Montel's avatar
Laurent Montel committed
70
    QVariantHash feedObject;
Laurent Montel's avatar
Laurent Montel committed
71
    addStandardObject(feedObject);
Laurent Montel's avatar
Laurent Montel committed
72
73
    feedObject.insert(QStringLiteral("strippedTitle"), Utils::stripTags(feed->title()));
    QString numberOfArticle;
Laurent Montel's avatar
Laurent Montel committed
74
    if (feed->unread() == 0) {
Laurent Montel's avatar
Laurent Montel committed
75
76
77
78
        numberOfArticle = i18n(" (no unread articles)");
    } else {
        numberOfArticle = i18np(" (1 unread article)", " (%1 unread articles)", feed->unread());
    }
Laurent Montel's avatar
Laurent Montel committed
79

Laurent Montel's avatar
Laurent Montel committed
80
    feedObject.insert(QStringLiteral("feedCount"), numberOfArticle);
Laurent Montel's avatar
Laurent Montel committed
81

Laurent Montel's avatar
Laurent Montel committed
82
    QString feedImage;
83
    if (!feed->logoInfo().imageUrl.isEmpty()) { // image
84
        feedImage = GrantleeUtil::imageFeed(feed);
Laurent Montel's avatar
Laurent Montel committed
85
86
87
88
89
    } else {
        feedImage = QStringLiteral("<div class=\"body\">");
    }
    feedObject.insert(QStringLiteral("feedImage"), feedImage);

Laurent Montel's avatar
Laurent Montel committed
90
    if (!feed->description().isEmpty()) {
91
        QString feedDescription = QStringLiteral("<div dir=\"%1\">").arg(mDirectionString);
Laurent Montel's avatar
Laurent Montel committed
92
        feedDescription += i18n("<b>Description:</b> %1<br />", feed->description());
Laurent Montel's avatar
Laurent Montel committed
93
        feedDescription += QStringLiteral("</div>"); // /description
Laurent Montel's avatar
Laurent Montel committed
94
95
96
        feedObject.insert(QStringLiteral("feedDescription"), feedDescription);
    }

Laurent Montel's avatar
Laurent Montel committed
97
    if (!feed->htmlUrl().isEmpty()) {
98
        QString feedHomePage = QStringLiteral("<div dir=\"%1\">").arg(mDirectionString);
Laurent Montel's avatar
Laurent Montel committed
99
        feedHomePage += i18n("<b>Homepage:</b> <a href=\"%1\">%1</a>", feed->htmlUrl());
Laurent Montel's avatar
Laurent Montel committed
100
        feedHomePage += QStringLiteral("</div>"); // / link
Laurent Montel's avatar
Laurent Montel committed
101
102
103
        feedObject.insert(QStringLiteral("feedHomePage"), feedHomePage);
    }

Laurent Montel's avatar
Laurent Montel committed
104
    if (!feed->copyright().isEmpty()) {
105
        QString feedCopyright = QStringLiteral("<div dir=\"%1\">").arg(mDirectionString);
Laurent Montel's avatar
Laurent Montel committed
106
107
108
109
110
        feedCopyright += i18n("<b>Copyright:</b> %1</a>", feed->copyright());
        feedCopyright += QStringLiteral("</div>");
        feedObject.insert(QStringLiteral("feedCopyright"), feedCopyright);
    }

Laurent Montel's avatar
Laurent Montel committed
111
    return render(feedObject);
Laurent Montel's avatar
Laurent Montel committed
112
113
114
115
116
117
118
119
120
}

QString GrantleeViewFormatter::formatFolder(Akregator::Folder *node)
{
    setDefaultHtmlMainFile(QStringLiteral("defaultnormalvisitfolder.html"));
    if (!errorMessage().isEmpty()) {
        return errorMessage();
    }
    QVariantHash folderObject;
Laurent Montel's avatar
Laurent Montel committed
121
122
    addStandardObject(folderObject);

Laurent Montel's avatar
Laurent Montel committed
123
124
    folderObject.insert(QStringLiteral("nodeTitle"), node->title());
    QString numberOfArticle;
Laurent Montel's avatar
Laurent Montel committed
125
    if (node->unread() == 0) {
Laurent Montel's avatar
Laurent Montel committed
126
127
128
129
130
131
132
133
134
        numberOfArticle = i18n(" (no unread articles)");
    } else {
        numberOfArticle = i18np(" (1 unread article)", " (%1 unread articles)", node->unread());
    }

    folderObject.insert(QStringLiteral("nodeCount"), numberOfArticle);
    return render(folderObject);
}

Laurent Montel's avatar
Laurent Montel committed
135
QString GrantleeViewFormatter::formatArticles(const QVector<Article> &article, ArticleFormatter::IconOption icon)
Laurent Montel's avatar
Laurent Montel committed
136
137
{
    setDefaultHtmlMainFile(mHtmlArticleFileName);
138
139
    if (!errorMessage().isEmpty()) {
        return errorMessage();
140
    }
Laurent Montel's avatar
Laurent Montel committed
141

Laurent Montel's avatar
Laurent Montel committed
142
    QVariantHash articleObject;
Laurent Montel's avatar
Laurent Montel committed
143
144
145
146

    QVariantList articlesList;
    const int nbArticles(article.count());
    articlesList.reserve(nbArticles);
Laurent Montel's avatar
Laurent Montel committed
147
    QList<ArticleGrantleeObject *> lstObj;
Laurent Montel's avatar
Laurent Montel committed
148
    lstObj.reserve(nbArticles);
Laurent Montel's avatar
Laurent Montel committed
149
    for (int i = 0; i < nbArticles; ++i) {
150
        ArticleGrantleeObject *articleObj = new ArticleGrantleeObject(article.at(i), icon);
Laurent Montel's avatar
Laurent Montel committed
151
        articlesList << QVariant::fromValue(static_cast<QObject *>(articleObj));
Laurent Montel's avatar
Laurent Montel committed
152
        lstObj.append(articleObj);
Laurent Montel's avatar
Laurent Montel committed
153
    }
Laurent Montel's avatar
Laurent Montel committed
154
    articleObject.insert(QStringLiteral("articles"), articlesList);
Laurent Montel's avatar
Laurent Montel committed
155

Laurent Montel's avatar
Laurent Montel committed
156
    addStandardObject(articleObject);
Laurent Montel's avatar
Laurent Montel committed
157
158
159
160

    articleObject.insert(QStringLiteral("dateI18n"), i18n("Date"));
    articleObject.insert(QStringLiteral("commentI18n"), i18n("Comment"));
    articleObject.insert(QStringLiteral("completeStoryI18n"), i18n("Complete Story"));
161
162
    articleObject.insert(QStringLiteral("authorI18n"), i18n("Author"));
    articleObject.insert(QStringLiteral("enclosureI18n"), i18n("Enclosure"));
163

Laurent Montel's avatar
Laurent Montel committed
164
165
166
    const QString str = render(articleObject);
    qDeleteAll(lstObj);
    return str;
167
}