~ubuntu-branches/debian/stretch/knewstuff/stretch

« back to all changes in this revision

Viewing changes to src/ui/entrydetailsdialog.cpp

  • Committer: Package Import Robot
  • Author(s): Scarlett Clark
  • Date: 2014-07-15 16:22:19 UTC
  • Revision ID: package-import@ubuntu.com-20140715162219-y23ejdtgskj0wwzq
Tags: upstream-5.0.0
ImportĀ upstreamĀ versionĀ 5.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    Copyright (C) 2009 Frederik Gladhorn <gladhorn@kde.org>
 
3
 
 
4
    This library is free software; you can redistribute it and/or
 
5
    modify it under the terms of the GNU Lesser General Public
 
6
    License as published by the Free Software Foundation; either
 
7
    version 2.1 of the License, or (at your option) any later version.
 
8
 
 
9
    This library 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
    Lesser General Public License for more details.
 
13
 
 
14
    You should have received a copy of the GNU Lesser General Public
 
15
    License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 
16
*/
 
17
 
 
18
#include "entrydetailsdialog_p.h"
 
19
 
 
20
#include <QMenu>
 
21
#include <QDebug>
 
22
#include <klocalizedstring.h>
 
23
 
 
24
#include "core/engine_p.h"
 
25
#include "ui/imageloader_p.h"
 
26
#include <attica/provider.h>
 
27
 
 
28
using namespace KNS3;
 
29
 
 
30
EntryDetails::EntryDetails(Engine *engine, Ui::DownloadWidget *widget)
 
31
    : QObject(widget->m_listView), m_engine(engine), ui(widget)
 
32
{
 
33
    init();
 
34
}
 
35
 
 
36
EntryDetails::~EntryDetails()
 
37
{
 
38
}
 
39
 
 
40
void EntryDetails::init()
 
41
{
 
42
    connect(ui->preview1, SIGNAL(clicked()), this, SLOT(preview1Selected()));
 
43
    connect(ui->preview2, SIGNAL(clicked()), this, SLOT(preview2Selected()));
 
44
    connect(ui->preview3, SIGNAL(clicked()), this, SLOT(preview3Selected()));
 
45
 
 
46
    ui->ratingWidget->setMaxRating(10);
 
47
    ui->ratingWidget->setHalfStepsEnabled(true);
 
48
 
 
49
    updateButtons();
 
50
    connect(ui->installButton, SIGNAL(clicked()), this, SLOT(install()));
 
51
    connect(ui->uninstallButton, SIGNAL(clicked()), this, SLOT(uninstall()));
 
52
    // updating is the same as installing
 
53
    connect(ui->updateButton, SIGNAL(clicked()), this, SLOT(install()));
 
54
    connect(ui->becomeFanButton, SIGNAL(clicked()), this, SLOT(becomeFan()));
 
55
 
 
56
    ui->installButton->setIcon(QIcon::fromTheme("dialog-ok"));
 
57
    ui->updateButton->setIcon(QIcon::fromTheme("system-software-update"));
 
58
    ui->uninstallButton->setIcon(QIcon::fromTheme("edit-delete"));
 
59
 
 
60
    connect(m_engine, SIGNAL(signalEntryDetailsLoaded(KNS3::EntryInternal)),
 
61
            this, SLOT(entryChanged(KNS3::EntryInternal)));
 
62
    connect(m_engine, SIGNAL(signalEntryChanged(KNS3::EntryInternal)),
 
63
            this, SLOT(entryStatusChanged(KNS3::EntryInternal)));
 
64
    connect(m_engine, SIGNAL(signalEntryPreviewLoaded(KNS3::EntryInternal,KNS3::EntryInternal::PreviewType)),
 
65
            this, SLOT(slotEntryPreviewLoaded(KNS3::EntryInternal,KNS3::EntryInternal::PreviewType)));
 
66
}
 
67
 
 
68
void EntryDetails::setEntry(const KNS3::EntryInternal &entry)
 
69
{
 
70
    m_entry = entry;
 
71
    // immediately show something
 
72
    entryChanged(m_entry);
 
73
    // fetch more preview images
 
74
    m_engine->loadDetails(m_entry);
 
75
}
 
76
 
 
77
void EntryDetails::entryChanged(const KNS3::EntryInternal &entry)
 
78
{
 
79
    if (ui->detailsStack->currentIndex() == 0) {
 
80
        return;
 
81
    }
 
82
    m_entry = entry;
 
83
 
 
84
    // FIXME
 
85
    //ui->ratingWidget->setEditable(m_engine->userCanVote(m_entry));
 
86
 
 
87
    if (!m_engine->userCanBecomeFan(m_entry)) {
 
88
        ui->becomeFanButton->setEnabled(false);
 
89
    }
 
90
 
 
91
    ui->m_titleWidget->setText(i18n("Details for %1", m_entry.name()));
 
92
    if (!m_entry.author().homepage().isEmpty()) {
 
93
        ui->authorLabel->setText("<a href=\"" + m_entry.author().homepage() + "\">" + m_entry.author().name() + "</a>");
 
94
    } else if (!m_entry.author().email().isEmpty()) {
 
95
        ui->authorLabel->setText("<a href=\"mailto:" + m_entry.author().email() + "\">" + m_entry.author().name() + "</a>");
 
96
    } else {
 
97
        ui->authorLabel->setText(m_entry.author().name());
 
98
    }
 
99
 
 
100
    QString summary = replaceBBCode(m_entry.summary()).replace("\n", "<br/>");
 
101
    QString changelog = replaceBBCode(m_entry.changelog()).replace("\n", "<br/>");
 
102
 
 
103
    QString description = "<html><body>" + summary;
 
104
    if (!changelog.isEmpty()) {
 
105
        description += "<br/><p><b>" + i18n("Changelog:") + "</b><br/>" + changelog + "</p>";
 
106
    }
 
107
    description += "</body></html>";
 
108
    ui->descriptionLabel->setText(description);
 
109
 
 
110
    QString homepageText("<a href=\"" + m_entry.homepage().url() + "\">" +
 
111
                         i18nc("A link to the description of this Get Hot New Stuff item", "Homepage") + "</a>");
 
112
 
 
113
    if (!m_entry.donationLink().isEmpty()) {
 
114
        homepageText += "<br><a href=\"" + m_entry.donationLink() + "\">" + i18nc("A link to make a donation for a Get Hot New Stuff item (opens a web browser)", "Make a donation") + "</a>";
 
115
    }
 
116
    if (!m_entry.knowledgebaseLink().isEmpty()) {
 
117
        homepageText += "<br><a href=\"" + m_entry.knowledgebaseLink() + "\">"
 
118
                        + i18ncp("A link to the knowledgebase (like a forum) (opens a web browser)", "Knowledgebase (no entries)", "Knowledgebase (%1 entries)", m_entry.numberKnowledgebaseEntries()) + "</a>";
 
119
    }
 
120
    ui->homepageLabel->setText(homepageText);
 
121
    ui->homepageLabel->setToolTip(i18nc("Tooltip for a link in a dialog", "Opens in a browser window"));
 
122
 
 
123
    if (m_entry.rating() > 0) {
 
124
        ui->ratingWidget->setVisible(true);
 
125
        disconnect(ui->ratingWidget, SIGNAL(ratingChanged(uint)), this, SLOT(ratingChanged(uint)));
 
126
        // Most of the voting is 20 - 80, so rate 20 as 0 stars and 80 as 5 stars
 
127
        int rating = qMax(0, qMin(10, (m_entry.rating() - 20) / 6));
 
128
        ui->ratingWidget->setRating(rating);
 
129
        connect(ui->ratingWidget, SIGNAL(ratingChanged(uint)), this, SLOT(ratingChanged(uint)));
 
130
    } else {
 
131
        ui->ratingWidget->setVisible(false);
 
132
    }
 
133
 
 
134
    bool hideSmallPreviews = m_entry.previewUrl(EntryInternal::PreviewSmall2).isEmpty()
 
135
                             && m_entry.previewUrl(EntryInternal::PreviewSmall3).isEmpty();
 
136
 
 
137
    ui->preview1->setVisible(!hideSmallPreviews);
 
138
    ui->preview2->setVisible(!hideSmallPreviews);
 
139
    ui->preview3->setVisible(!hideSmallPreviews);
 
140
 
 
141
    // in static xml we often only get a small preview, use that in details
 
142
    if (m_entry.previewUrl(EntryInternal::PreviewBig1).isEmpty() && !m_entry.previewUrl(EntryInternal::PreviewSmall1).isEmpty()) {
 
143
        m_entry.setPreviewUrl(m_entry.previewUrl(EntryInternal::PreviewSmall1), EntryInternal::PreviewBig1);
 
144
        m_entry.setPreviewImage(m_entry.previewImage(EntryInternal::PreviewSmall1), EntryInternal::PreviewBig1);
 
145
    }
 
146
 
 
147
    for (int type = EntryInternal::PreviewSmall1; type <= EntryInternal::PreviewBig3; ++type) {
 
148
        if (m_entry.previewUrl(EntryInternal::PreviewSmall1).isEmpty()) {
 
149
            ui->previewBig->setVisible(false);
 
150
        } else
 
151
 
 
152
            if (!m_entry.previewUrl((EntryInternal::PreviewType)type).isEmpty()) {
 
153
                // qDebug() << "type: " << type << m_entry.previewUrl((EntryInternal::PreviewType)type);
 
154
                if (m_entry.previewImage((EntryInternal::PreviewType)type).isNull()) {
 
155
                    m_engine->loadPreview(m_entry, (EntryInternal::PreviewType)type);
 
156
                } else {
 
157
                    slotEntryPreviewLoaded(m_entry, (EntryInternal::PreviewType)type);
 
158
                }
 
159
            }
 
160
    }
 
161
 
 
162
    updateButtons();
 
163
}
 
164
 
 
165
void EntryDetails::entryStatusChanged(const KNS3::EntryInternal &entry)
 
166
{
 
167
    Q_UNUSED(entry);
 
168
    updateButtons();
 
169
}
 
170
 
 
171
void EntryDetails::updateButtons()
 
172
{
 
173
    if (ui->detailsStack->currentIndex() == 0) {
 
174
        return;
 
175
    }
 
176
    // qDebug() << "update buttons: " << m_entry.status();
 
177
    ui->installButton->setVisible(false);
 
178
    ui->uninstallButton->setVisible(false);
 
179
    ui->updateButton->setVisible(false);
 
180
 
 
181
    switch (m_entry.status()) {
 
182
    case Entry::Installed:
 
183
        ui->uninstallButton->setVisible(true);
 
184
        ui->uninstallButton->setEnabled(true);
 
185
        break;
 
186
    case Entry::Updateable:
 
187
        ui->updateButton->setVisible(true);
 
188
        ui->updateButton->setEnabled(true);
 
189
        ui->uninstallButton->setVisible(true);
 
190
        ui->uninstallButton->setEnabled(true);
 
191
        break;
 
192
 
 
193
    case Entry::Invalid:
 
194
    case Entry::Downloadable:
 
195
        ui->installButton->setVisible(true);
 
196
        ui->installButton->setEnabled(true);
 
197
        break;
 
198
 
 
199
    case Entry::Installing:
 
200
        ui->installButton->setVisible(true);
 
201
        ui->installButton->setEnabled(false);
 
202
        break;
 
203
    case Entry::Updating:
 
204
        ui->updateButton->setVisible(true);
 
205
        ui->updateButton->setEnabled(false);
 
206
        ui->uninstallButton->setVisible(true);
 
207
        ui->uninstallButton->setEnabled(false);
 
208
        break;
 
209
    case Entry::Deleted:
 
210
        ui->installButton->setVisible(true);
 
211
        ui->installButton->setEnabled(true);
 
212
        break;
 
213
    }
 
214
 
 
215
    if (ui->installButton->menu()) {
 
216
        QMenu *buttonMenu = ui->installButton->menu();
 
217
        buttonMenu->clear();
 
218
        ui->installButton->setMenu(0);
 
219
        buttonMenu->deleteLater();
 
220
    }
 
221
    if (ui->installButton->isVisible() && m_entry.downloadLinkCount() > 1) {
 
222
        QMenu *installMenu = new QMenu(ui->installButton);
 
223
        foreach (EntryInternal::DownloadLinkInformation info, m_entry.downloadLinkInformationList()) {
 
224
            QString text = info.name;
 
225
            if (!info.distributionType.trimmed().isEmpty()) {
 
226
                text + " (" + info.distributionType.trimmed() + ")";
 
227
            }
 
228
            QAction *installAction = installMenu->addAction(QIcon::fromTheme("dialog-ok"), text);
 
229
            installAction->setData(info.id);
 
230
        }
 
231
        // qDebug() << "links: " << m_entry.downloadLinkInformationList().size();
 
232
        ui->installButton->setMenu(installMenu);
 
233
    }
 
234
}
 
235
 
 
236
void EntryDetails::install()
 
237
{
 
238
    m_engine->install(m_entry);
 
239
}
 
240
 
 
241
void EntryDetails::uninstall()
 
242
{
 
243
    m_engine->uninstall(m_entry);
 
244
}
 
245
 
 
246
void EntryDetails::slotEntryPreviewLoaded(const KNS3::EntryInternal &entry, KNS3::EntryInternal::PreviewType type)
 
247
{
 
248
    if (!(entry == m_entry)) {
 
249
        return;
 
250
    }
 
251
 
 
252
    switch (type) {
 
253
    case EntryInternal::PreviewSmall1:
 
254
        ui->preview1->setImage(entry.previewImage(EntryInternal::PreviewSmall1));
 
255
        break;
 
256
    case EntryInternal::PreviewSmall2:
 
257
        ui->preview2->setImage(entry.previewImage(EntryInternal::PreviewSmall2));
 
258
        break;
 
259
    case EntryInternal::PreviewSmall3:
 
260
        ui->preview3->setImage(entry.previewImage(EntryInternal::PreviewSmall3));
 
261
        break;
 
262
    case EntryInternal::PreviewBig1:
 
263
        m_currentPreview = entry.previewImage(EntryInternal::PreviewBig1);
 
264
        ui->previewBig->setImage(m_currentPreview);
 
265
        break;
 
266
    default:
 
267
        break;
 
268
    }
 
269
}
 
270
 
 
271
void EntryDetails::preview1Selected()
 
272
{
 
273
    previewSelected(0);
 
274
}
 
275
 
 
276
void EntryDetails::preview2Selected()
 
277
{
 
278
    previewSelected(1);
 
279
}
 
280
 
 
281
void EntryDetails::preview3Selected()
 
282
{
 
283
    previewSelected(2);
 
284
}
 
285
 
 
286
void EntryDetails::previewSelected(int current)
 
287
{
 
288
    EntryInternal::PreviewType type = static_cast<EntryInternal::PreviewType>(EntryInternal::PreviewBig1 + current);
 
289
    m_currentPreview = m_entry.previewImage(type);
 
290
    ui->previewBig->setImage(m_currentPreview);
 
291
}
 
292
 
 
293
void EntryDetails::ratingChanged(uint rating)
 
294
{
 
295
    // engine expects values from 0..100
 
296
    // qDebug() << "rating: " << rating << " -> " << rating*10;
 
297
    m_engine->vote(m_entry, rating * 10);
 
298
}
 
299
 
 
300
void EntryDetails::becomeFan()
 
301
{
 
302
    m_engine->becomeFan(m_entry);
 
303
}
 
304