File indexing completed on 2024-04-21 05:45:05

0001 /***************************************************************************
0002  *   Copyright (C) 2010 by Daniel Nicoletti                                *
0003  *   dantti12@gmail.com                                                    *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; see the file COPYING. If not, write to       *
0017  *   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,  *
0018  *   Boston, MA 02110-1301, USA.                                           *
0019  ***************************************************************************/
0020 
0021 #include "ApplicationLauncher.h"
0022 #include "ui_ApplicationLauncher.h"
0023 
0024 #include <QStandardItemModel>
0025 #include <QLoggingCategory>
0026 #include <KToolInvocation>
0027 #include <KLocalizedString>
0028 #include <KService>
0029 #include <KConfig>
0030 #include <KConfigGroup>
0031 
0032 Q_DECLARE_LOGGING_CATEGORY(APPER_LIB)
0033 
0034 ApplicationLauncher::ApplicationLauncher(QWidget *parent) :
0035     QDialog(parent),
0036     m_embed(false),
0037     ui(new Ui::ApplicationLauncher)
0038 {
0039     ui->setupUi(this);
0040     connect(ui->showCB, &QCheckBox::toggled, this, &ApplicationLauncher::on_showCB_toggled);
0041     setObjectName(QLatin1String("ApplicationLauncher"));
0042 
0043     connect(ui->kdialogbuttonbox, &QDialogButtonBox::rejected, this, &ApplicationLauncher::accept);
0044     setWindowIcon(QIcon::fromTheme(QLatin1String("task-complete")));
0045 
0046     connect(ui->applicationsView, &QListView::clicked, this, &ApplicationLauncher::itemClicked);
0047 }
0048 
0049 ApplicationLauncher::~ApplicationLauncher()
0050 {
0051     delete ui;
0052 }
0053 
0054 bool ApplicationLauncher::embedded() const
0055 {
0056     return m_embed;
0057 }
0058 
0059 void ApplicationLauncher::setEmbedded(bool embedded)
0060 {
0061     m_embed = embedded;
0062     ui->showCB->setVisible(!embedded);
0063     ui->kdialogbuttonbox->setVisible(!embedded);
0064     qCDebug(APPER_LIB) << embedded;
0065 }
0066 
0067 QStringList ApplicationLauncher::packages() const
0068 {
0069     return m_packages;
0070 }
0071 
0072 bool ApplicationLauncher::hasApplications()
0073 {
0074     QStandardItemModel *model = new QStandardItemModel(this);
0075     ui->applicationsView->setModel(model);
0076     m_files.removeDuplicates();
0077 
0078     QStandardItem *item;
0079     for (const QString &desktop : m_files) {
0080         // we use KService to parse the .desktop file because findByDestopPath
0081         // might fail because the Sycoca database is not up to date yet.
0082         KService service(desktop);
0083         if (service.isApplication() &&
0084            !service.noDisplay() &&
0085            !service.exec().isEmpty())
0086         {
0087             QString name = service.genericName().isEmpty() ? service.name()
0088                                                            : service.name() + QLatin1String(" - ") + service.genericName();
0089             item = new QStandardItem(name);
0090             item->setIcon(QIcon::fromTheme(service.icon()));
0091             item->setData(service.entryPath(), Qt::UserRole);
0092             item->setFlags(Qt::ItemIsEnabled);
0093             model->appendRow(item);
0094         }
0095     }
0096 
0097     setWindowTitle(i18np("New application available",
0098                          "New applications available",
0099                          model->rowCount()));
0100     ui->label->setText(i18np("The following application was just installed. Click on it to launch:",
0101                              "The following applications were just installed. Click on them to launch:",
0102                              model->rowCount()));
0103 
0104     return model->rowCount();
0105 }
0106 
0107 void ApplicationLauncher::addPackage(PackageKit::Transaction::Info info, const QString &packageID, const QString &summary)
0108 {
0109     Q_UNUSED(info)
0110     Q_UNUSED(summary)
0111     if (!m_packages.contains(packageID)) {
0112         m_packages << packageID;
0113     }
0114 }
0115 
0116 void ApplicationLauncher::files(const QString &packageID, const QStringList &files)
0117 {
0118     Q_UNUSED(packageID)
0119     m_files.append(files.filter(QLatin1String(".desktop")));
0120 }
0121 
0122 void ApplicationLauncher::itemClicked(const QModelIndex &index)
0123 {
0124 //     kDebug() << index.data(Qt::UserRole).toString();
0125     KToolInvocation::startServiceByDesktopPath(index.data(Qt::UserRole).toString());
0126 }
0127 
0128 void ApplicationLauncher::on_showCB_toggled(bool checked)
0129 {
0130     KConfig config(QLatin1String("apper"));
0131     KConfigGroup transactionGroup(&config, "Transaction");
0132     transactionGroup.writeEntry("ShowApplicationLauncher", !checked);
0133     config.sync();
0134 }
0135 
0136 #include "moc_ApplicationLauncher.cpp"