File: DebViewer.cpp

package info (click to toggle)
libqapt 3.0.5-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,372 kB
  • sloc: cpp: 11,270; makefile: 13; sh: 11
file content (292 lines) | stat: -rw-r--r-- 10,668 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/***************************************************************************
 *   Copyright © 2011 Jonathan Thomas <echidnaman@kubuntu.org>             *
 *                                                                         *
 *   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) version 3 or any later version        *
 *   accepted by the membership of KDE e.V. (or its successor approved     *
 *   by the membership of KDE e.V.), which shall act as a proxy            *
 *   defined in Section 14 of version 3 of the license.                    *
 *                                                                         *
 *   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.  If not, see <http://www.gnu.org/licenses/>. *
 ***************************************************************************/

#include "DebViewer.h"

#include <QDebug>
#include <QDir>
#include <QFile>
#include <QIcon>
#include <QLabel>
#include <QPointer>
#include <QPushButton>
#include <QStringBuilder>
#include <QTabWidget>
#include <QTextBrowser>
#include <QVBoxLayout>

#include <KFormat>
#include <KLocalizedString>

#include <QApt/Backend>
#include <QApt/DebFile>

#include "ChangesDialog.h"

DebViewer::DebViewer(QWidget *parent)
    : QWidget(parent)
    , m_backend(nullptr)
    , m_debFile(nullptr)
{
    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    mainLayout->setMargin(0);
    setLayout(mainLayout);

    // Header
    QWidget *headerWidget = new QWidget(this);
    QHBoxLayout *headerLayout = new QHBoxLayout(headerWidget);
    headerLayout->setMargin(0);
    headerWidget->setLayout(headerLayout);

    m_iconLabel = new QLabel(headerWidget);

    QWidget *headerSpacer = new QWidget(headerWidget);
    headerSpacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);

    // Name/status box in header
    QWidget *nameStatusBox = new QWidget(headerWidget);
    QGridLayout *nameStatusGrid = new QGridLayout(nameStatusBox);
    nameStatusBox->setLayout(nameStatusGrid);

    // nameStatusGrid, row 0
    QLabel *namePrefixLabel = new QLabel(nameStatusBox);
    namePrefixLabel->setText(i18nc("@label Label preceding the package name",
                                   "Package:"));
    m_nameLabel = new QLabel(nameStatusBox);
    nameStatusGrid->addWidget(namePrefixLabel, 0, 0, Qt::AlignRight);
    nameStatusGrid->addWidget(m_nameLabel, 0, 1, Qt::AlignLeft);

    // nameStatusGrid, row 1
    QLabel *statusPrefixLabel = new QLabel(nameStatusBox);
    statusPrefixLabel->setText(i18nc("@label Label preceding the package status",
                                     "Status:"));
    m_statusLabel = new QLabel(nameStatusBox);
    m_detailsButton = new QPushButton(i18nc("@label", "Details..."), nameStatusBox);
    m_detailsButton->hide();
    connect(m_detailsButton, SIGNAL(clicked()), this, SLOT(detailsButtonClicked()));
    nameStatusGrid->addWidget(statusPrefixLabel, 1, 0, Qt::AlignRight);
    nameStatusGrid->addWidget(m_statusLabel, 1, 1, Qt::AlignLeft);
    nameStatusGrid->addWidget(m_detailsButton, 1, 2, Qt::AlignLeft);

    headerLayout->addWidget(m_iconLabel);
    headerLayout->addWidget(nameStatusBox);
    headerLayout->addWidget(headerSpacer);


    // Version info box
    m_versionInfoWidget = new QWidget(this);
    QHBoxLayout *versionInfoLayout = new QHBoxLayout(m_versionInfoWidget);
    m_versionInfoWidget->setLayout(versionInfoLayout);

    QLabel *infoIcon = new QLabel(m_versionInfoWidget);
    infoIcon->setPixmap(QIcon::fromTheme("dialog-information").pixmap(32, 32));

    QWidget *verInfoBox = new QWidget(m_versionInfoWidget);
    verInfoBox->setLayout(new QVBoxLayout);
    m_versionTitleLabel = new QLabel(verInfoBox);
    m_versionInfoLabel = new QLabel(verInfoBox);
    verInfoBox->layout()->addWidget(m_versionTitleLabel);
    verInfoBox->layout()->addWidget(m_versionInfoLabel);

    QWidget *versionSpacer = new QWidget(m_versionInfoWidget);
    versionSpacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Minimum);

    versionInfoLayout->addWidget(infoIcon);
    versionInfoLayout->addWidget(verInfoBox);
    versionInfoLayout->addWidget(versionSpacer);
    //


    // Details tab widget
    QTabWidget *detailsWidget = new QTabWidget(this);

    QWidget *descriptionTab = new QWidget(detailsWidget);
    descriptionTab->setLayout(new QVBoxLayout);
    m_descriptionWidget = new QTextBrowser(descriptionTab);
    descriptionTab->layout()->addWidget(m_descriptionWidget);

    QWidget *detailsTab = new QWidget(detailsWidget);
    QGridLayout *detailsGrid = new QGridLayout(detailsTab);
    detailsTab->setLayout(detailsGrid);

    // detailsGrid, row 1
    QLabel *versionPrefixLabel = new QLabel(detailsTab);
    versionPrefixLabel->setText(i18nc("@label Label preceding the package version",
                                      "Version:"));
    m_versionLabel = new QLabel(detailsTab);
    detailsGrid->addWidget(versionPrefixLabel, 0, 0, Qt::AlignRight);
    detailsGrid->addWidget(m_versionLabel, 0, 1, Qt::AlignLeft);
    // detailsGrid, row 2
    QLabel *sizePrefixLabel = new QLabel(detailsTab);
    sizePrefixLabel->setText(i18nc("@label Label preceding the package size",
                                   "Installed Size:"));
    m_sizeLabel = new QLabel(detailsTab);
    detailsGrid->addWidget(sizePrefixLabel, 1, 0, Qt::AlignRight);
    detailsGrid->addWidget(m_sizeLabel, 1, 1, Qt::AlignLeft);
    // detailsGrid, row 3
    QLabel *maintainerPrefixLabel = new QLabel(detailsTab);
    maintainerPrefixLabel->setText(i18nc("@label Label preceding the package maintainer",
                                         "Maintainer:"));
    m_maintainerLabel = new QLabel(detailsTab);
    detailsGrid->addWidget(maintainerPrefixLabel, 2, 0, Qt::AlignRight);
    detailsGrid->addWidget(m_maintainerLabel, 2, 1, Qt::AlignLeft);
    // detailsGrid, row 4
    QLabel *sectionPrefixLabel = new QLabel(detailsTab);
    sectionPrefixLabel->setText(i18nc("@label Label preceding the package category",
                                      "Category:"));
    m_sectionLabel = new QLabel(detailsTab);
    detailsGrid->addWidget(sectionPrefixLabel, 3, 0, Qt::AlignRight);
    detailsGrid->addWidget(m_sectionLabel, 3, 1, Qt::AlignLeft);
    // detailsGrid, row 4
    QLabel *homepagePrefixLabel = new QLabel(detailsTab);
    homepagePrefixLabel->setText(i18nc("@label Label preceding the package homepage",
                                       "Homepage:"));
    m_homepageLabel = new QLabel(detailsTab);
    detailsGrid->addWidget(homepagePrefixLabel, 4, 0, Qt::AlignRight);
    detailsGrid->addWidget(m_homepageLabel, 4, 1, Qt::AlignLeft);

    detailsGrid->setColumnStretch(1, 1);
    detailsGrid->setRowStretch(5, 1);

    QWidget *fileTab = new QWidget(detailsWidget);
    fileTab->setLayout(new QVBoxLayout);
    m_fileWidget = new QTextBrowser(fileTab);
    fileTab->layout()->addWidget(m_fileWidget);


    detailsWidget->addTab(descriptionTab, i18nc("@title:tab", "Description"));
    detailsWidget->addTab(detailsTab, i18nc("@title:tab", "Details"));
    detailsWidget->addTab(fileTab, i18nc("@title:tab", "Included Files"));

    mainLayout->addWidget(headerWidget);
    mainLayout->addWidget(m_versionInfoWidget);
    mainLayout->addWidget(detailsWidget);
}

DebViewer::~DebViewer()
{
}

void DebViewer::setBackend(QApt::Backend *backend)
{
    m_backend = backend;
    m_oldCacheState = m_backend->currentCacheState();
}

void DebViewer::setDebFile(QApt::DebFile *debFile)
{
    m_debFile = debFile;

    QStringList iconList = m_debFile->iconList();
    qSort(iconList);

    // Try to get the biggest icon, which should be last
    QString iconPath;
    if (!iconList.isEmpty()) {
        iconPath = iconList.last();
    }

    QDir tempDir = QDir::temp();
    tempDir.mkdir(QLatin1String("qapt-deb-installer"));

    QString destPath = QDir::tempPath() + QLatin1String("/qapt-deb-installer/");
    m_debFile->extractFileFromArchive(iconPath, destPath);

    QIcon icon;

    QString finalPath = destPath + iconPath;
    if (QFile::exists(destPath + iconPath)) {
        icon = QIcon(finalPath);
    }

    if (iconPath.isEmpty()) {
        icon = QIcon::fromTheme("application-x-deb");
    }

    m_iconLabel->setPixmap(icon.pixmap(48,48));

    m_nameLabel->setText(debFile->packageName());

    // Details tab widgets
    QString shortDesc = debFile->shortDescription();
    shortDesc.prepend(QLatin1String("<b>"));
    shortDesc.append(QLatin1String("</b><br><br>"));
    QString longDesc = debFile->longDescription();
    longDesc.replace('\n', QLatin1String("<br>"));

    m_descriptionWidget->append(shortDesc + longDesc);

    m_versionLabel->setText(debFile->version());
    m_sizeLabel->setText(KFormat().formatByteSize(debFile->installedSize() * 1024));
    m_maintainerLabel->setText(debFile->maintainer());
    m_sectionLabel->setText(debFile->section());
    m_homepageLabel->setText(debFile->homepage());

    QStringList fileList = debFile->fileList();
    qSort(fileList);
    QString filesString;

    foreach (const QString &file, fileList) {
        if (!file.trimmed().isEmpty()) {
            filesString.append(file + '\n');
        }
    }

    m_fileWidget->setPlainText(filesString);
}

void DebViewer::setStatusText(const QString &text)
{
    m_statusLabel->setText(text);
}

void DebViewer::showDetailsButton(bool show)
{
    m_detailsButton->setVisible(show);
}

void DebViewer::hideVersionInfo()
{
    m_versionInfoWidget->hide();
}

void DebViewer::setVersionTitle(const QString &title)
{
    m_versionTitleLabel->setText(title);
}

void DebViewer::setVersionInfo(const QString &info)
{
    m_versionInfoLabel->setText(info);
}

void DebViewer::detailsButtonClicked()
{
    QList<QApt::Package *> excluded;
    excluded.append(m_backend->package(m_debFile->packageName()));
    auto changes = m_backend->stateChanges(m_oldCacheState, excluded);

    if (changes.isEmpty()) {
        return;
    }

    QPointer<ChangesDialog> dialog = new ChangesDialog(this, changes);
    dialog->exec();
}