File: BrowseView.cpp

package info (click to toggle)
apper 1.0.0-5
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 10,244 kB
  • sloc: cpp: 14,383; xml: 970; makefile: 13; sh: 4
file content (338 lines) | stat: -rw-r--r-- 12,138 bytes parent folder | download | duplicates (4)
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/***************************************************************************
 *   Copyright (C) 2009-2010 by Daniel Nicoletti                           *
 *   dantti12@gmail.com                                                    *
 *                                                                         *
 *   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.                                           *
 ***************************************************************************/

#include "BrowseView.h"

#include "PackageDetails.h"
#include "CategoryModel.h"

#include <ApplicationsDelegate.h>
#include <ApplicationSortFilterModel.h>
#include <PackageModel.h>

#include <Daemon>

#include <QFileDialog>
#include <KPixmapSequence>
#include <QMenu>
#include <KIconLoader>
#include <KConfig>
#include <KConfigGroup>
#include <KLocalizedString>

#include <QDBusConnection>
#include <QDBusMessage>
#include <QAbstractItemView>
#include <QScrollBar>

#include <QLoggingCategory>

using namespace PackageKit;

BrowseView::BrowseView(QWidget *parent)
 : QWidget(parent)
{
    setupUi(this);
    connect(categoryView, &QListView::clicked, this, &BrowseView::categoryActivated);

    m_busySeq = new KPixmapSequenceOverlayPainter(this);
    m_busySeq->setSequence(KIconLoader::global()->loadPixmapSequence(QLatin1String("process-working"), KIconLoader::SizeSmallMedium));
    m_busySeq->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    m_busySeq->setWidget(packageView->viewport());

    m_model = new PackageModel(this);
    m_proxy = new ApplicationSortFilterModel(this);
    m_proxy->setSourceModel(m_model);

    packageView->setModel(m_proxy);
    packageView->sortByColumn(PackageModel::NameCol, Qt::AscendingOrder);
    packageView->header()->setDefaultAlignment(Qt::AlignCenter);
    packageView->header()->setStretchLastSection(false);
    packageView->header()->setSectionResizeMode(PackageModel::NameCol, QHeaderView::Stretch);
    packageView->header()->setSectionResizeMode(PackageModel::VersionCol, QHeaderView::ResizeToContents);
    packageView->header()->setSectionResizeMode(PackageModel::ArchCol, QHeaderView::ResizeToContents);
    packageView->header()->setSectionResizeMode(PackageModel::OriginCol, QHeaderView::ResizeToContents);
    packageView->header()->setSectionResizeMode(PackageModel::SizeCol, QHeaderView::ResizeToContents);
    packageView->header()->setSectionResizeMode(PackageModel::ActionCol, QHeaderView::ResizeToContents);

    // Hide current Version since it's useless for us
    packageView->header()->setSectionHidden(PackageModel::CurrentVersionCol, true);

    ApplicationsDelegate *delegate = new ApplicationsDelegate(packageView);
    packageView->setItemDelegate(delegate);

    exportInstalledPB->setIcon(QIcon::fromTheme(QLatin1String("document-export")));
    importInstalledPB->setIcon(QIcon::fromTheme(QLatin1String("document-import")));

    KConfig config(QLatin1String("apper"));
    KConfigGroup viewGroup(&config, "BrowseView");

    // Version
    packageView->header()->setSectionHidden(PackageModel::VersionCol, true);
    m_showPackageVersion = new QAction(i18n("Show Versions"), this);
    m_showPackageVersion->setCheckable(true);
    connect(m_showPackageVersion, &QAction::toggled, this, &BrowseView::showVersions);
    m_showPackageVersion->setChecked(viewGroup.readEntry("ShowApplicationVersions", true));

    // Arch
    packageView->header()->setSectionHidden(PackageModel::ArchCol, true);
    m_showPackageArch = new QAction(i18n("Show Architectures"), this);
    m_showPackageArch->setCheckable(true);
    connect(m_showPackageArch, &QAction::toggled, this, &BrowseView::showArchs);
    m_showPackageArch->setChecked(viewGroup.readEntry("ShowApplicationArchitectures", false));

    // Origin
    packageView->header()->setSectionHidden(PackageModel::OriginCol, true);
    m_showPackageOrigin = new QAction(i18n("Show Origins"), this);
    m_showPackageOrigin->setCheckable(true);
    connect(m_showPackageOrigin, &QAction::toggled, this, &BrowseView::showOrigins);
    m_showPackageOrigin->setChecked(viewGroup.readEntry("ShowApplicationOrigins", false));

    // Sizes
    packageView->header()->setSectionHidden(PackageModel::SizeCol, true);
    m_showPackageSizes = new QAction(i18n("Show Sizes"), this);
    m_showPackageSizes->setCheckable(true);
    connect(m_showPackageSizes, &QAction::toggled, this, &BrowseView::showSizes);
    m_showPackageSizes->setChecked(viewGroup.readEntry("ShowPackageSizes", false));


    // Ensure the index is visible when the packageDetails appears
    connect(packageDetails, &PackageDetails::ensureVisible, this, &BrowseView::ensureVisible);
}

void BrowseView::init(Transaction::Roles roles)
{
    packageDetails->init(roles);
}

BrowseView::~BrowseView()
{
}

bool BrowseView::showPageHeader() const
{
    return false;
}

PackageModel* BrowseView::model() const
{
    return m_model;
}

void BrowseView::showVersions(bool enabled)
{
    KConfig config(QLatin1String("apper"));
    KConfigGroup viewGroup(&config, "BrowseView");
    viewGroup.writeEntry("ShowApplicationVersions", enabled);
    packageView->header()->setSectionHidden(PackageModel::VersionCol, !enabled);
    packageDetails->hidePackageVersion(enabled);
}

void BrowseView::showArchs(bool enabled)
{
    KConfig config(QLatin1String("apper"));
    KConfigGroup viewGroup(&config, "BrowseView");
    viewGroup.writeEntry("ShowApplicationArchitectures", enabled);
    packageView->header()->setSectionHidden(PackageModel::ArchCol, !enabled);
    packageDetails->hidePackageArch(enabled);
}

void BrowseView::showOrigins(bool enabled)
{
    KConfig config(QLatin1String("apper"));
    KConfigGroup viewGroup(&config, "BrowseView");
    viewGroup.writeEntry("ShowApplicationOrigins", enabled);
    packageView->header()->setSectionHidden(PackageModel::OriginCol, !enabled);
}

void BrowseView::showSizes(bool enabled)
{
    KConfig config(QLatin1String("apper"));
    KConfigGroup viewGroup(&config, "BrowseView");
    viewGroup.writeEntry("ShowPackageSizes", enabled);
    packageView->header()->setSectionHidden(PackageModel::SizeCol, !enabled);
    packageDetails->hidePackageArch(enabled);
    if (enabled) {
        m_model->fetchSizes();
    }
}

void BrowseView::on_packageView_customContextMenuRequested(const QPoint &pos)
{
    auto menu = new QMenu(this);
    menu->addAction(m_showPackageVersion);
    menu->addAction(m_showPackageArch);
    menu->addAction(m_showPackageOrigin);
    menu->addAction(m_showPackageSizes);
    menu->exec(packageView->viewport()->mapToGlobal(pos));
    menu->deleteLater();
}

void BrowseView::on_packageView_clicked(const QModelIndex &index)
{
    if (index.column() == PackageModel::ActionCol) {
        return;
    }

    QModelIndex origIndex = m_proxy->mapToSource(index);
    packageDetails->setPackage(origIndex);
}

void BrowseView::ensureVisible(const QModelIndex &index)
{
    QModelIndex proxIndex = m_proxy->mapFromSource(index);
    packageView->scrollTo(proxIndex);
}

void BrowseView::showInstalledPanel(bool visible)
{
    installedF->setVisible(visible);
}

ApplicationSortFilterModel* BrowseView::proxy() const
{
    return m_proxy;
}

KPixmapSequenceOverlayPainter* BrowseView::busyCursor() const
{
    return m_busySeq;
}

void BrowseView::setCategoryModel(QAbstractItemModel *model)
{
    categoryView->setModel(model);
}

void BrowseView::setParentCategory(const QModelIndex &index)
{
    categoryView->setRootIndex(index);
    // Make sure the last item is not selected
    categoryView->selectionModel()->clearSelection();
    categoryView->horizontalScrollBar()->setValue(0);

    // Display the category view if the index has child items
    categoryF->setVisible(categoryView->model()->rowCount(index));
}

bool BrowseView::goBack()
{
    packageDetails->hide();
    QModelIndex index = categoryView->rootIndex();
    if (index.parent().isValid()) {
        index = index.parent();
        // if it's valid we need to know if it wasn't a  PK root category
        if (index.data(CategoryModel::GroupRole).type() == QVariant::String) {
            QString category = index.data(CategoryModel::GroupRole).toString();
            if (!category.startsWith(QLatin1Char('@'))) {
                return true;
            }
        }
        setParentCategory(index);
        emit categoryActivated(index);
        return false;
    }
    return true;
}

void BrowseView::on_categoryMvLeft_clicked()
{
    categoryView->horizontalScrollBar()->setValue(categoryView->horizontalScrollBar()->value() - 1);
}

void BrowseView::on_categoryMvRight_clicked()
{
    categoryView->horizontalScrollBar()->setValue(categoryView->horizontalScrollBar()->value() + 1);
}

void BrowseView::cleanUi()
{
    packageDetails->hide();
    categoryF->setVisible(false);
}

bool BrowseView::isShowingSizes() const
{
    return m_showPackageSizes->isChecked();
}

void BrowseView::on_exportInstalledPB_clicked()
{
    // We will assume the installed model
    // is populated since the user is seeing it.
    QString fileName;
    fileName = QFileDialog::getSaveFileName(this,
                                            i18n("Export installed packages"),
                                            QString(),
                                            QStringLiteral("*.catalog"));
    if (fileName.isEmpty()) {
        return;
    }

    QFile file(fileName);
    file.open(QIODevice::WriteOnly);
    QTextStream out(&file);
    out << "[PackageKit Catalog]\n\n";
    out << "InstallPackages(" << Daemon::global()->distroID() << ")=";
    QStringList packages;
    for (int i = 0; i < m_model->rowCount(); i++) {
        packages << m_model->data(m_model->index(i, 0),
                                  PackageModel::PackageName).toString();
    }
    out << packages.join(QLatin1Char(';'));
}

void BrowseView::on_importInstalledPB_clicked()
{
    QString fileName;
    fileName = QFileDialog::getOpenFileName(this,
                                            i18n("Install packages from catalog"),
                                            QString(),
                                            QStringLiteral("*.catalog"));
    if (fileName.isEmpty()) {
        return;
    }

    // send a DBus message to install this catalog
    QDBusMessage message;
    message = QDBusMessage::createMethodCall(QLatin1String("org.freedesktop.PackageKit"),
                                             QLatin1String("/org/freedesktop/PackageKit"),
                                             QLatin1String("org.freedesktop.PackageKit.Modify"),
                                             QLatin1String("InstallCatalogs"));
    message << static_cast<uint>(effectiveWinId());
    message << (QStringList() << fileName);
    message << QString();

    // This call must block otherwise this application closes before
    // smarticon is activated
    QDBusMessage reply = QDBusConnection::sessionBus().call(message, QDBus::Block);
}

void BrowseView::disableExportInstalledPB()
{
    exportInstalledPB->setEnabled(false);
}

void BrowseView::enableExportInstalledPB()
{
    exportInstalledPB->setEnabled(true);
}

#include "moc_BrowseView.cpp"