~ubuntu-branches/ubuntu/vivid/muon/vivid-proposed

1.4.38 by Jonathan Riddell
Import upstream version 5.2.2-1
1
/***************************************************************************
2
 *   Copyright © 2010 Jonathan Thomas <echidnaman@kubuntu.org>             *
3
 *   Copyright © 2012 Aleix Pol Gonzalez <aleixpol@blue-systems.com>       *
4
 *                                                                         *
5
 *   This program is free software; you can redistribute it and/or         *
6
 *   modify it under the terms of the GNU General Public License as        *
7
 *   published by the Free Software Foundation; either version 2 of        *
8
 *   the License or (at your option) version 3 or any later version        *
9
 *   accepted by the membership of KDE e.V. (or its successor approved     *
10
 *   by the membership of KDE e.V.), which shall act as a proxy            *
11
 *   defined in Section 14 of version 3 of the license.                    *
12
 *                                                                         *
13
 *   This program is distributed in the hope that it will be useful,       *
14
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
15
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
16
 *   GNU General Public License for more details.                          *
17
 *                                                                         *
18
 *   You should have received a copy of the GNU General Public License     *
19
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
20
 ***************************************************************************/
21
22
#include "LaunchListModel.h"
23
24
// Qt includes
25
#include <QStringBuilder>
26
27
// KDE includes
28
#include <KService>
29
#include <KToolInvocation>
30
31
// Libmuon includes
32
#include "Transaction/TransactionModel.h"
33
#include "resources/AbstractResource.h"
34
35
LaunchListModel::LaunchListModel(QObject* parent)
36
    : QStandardItemModel(parent)
37
{
38
    connect(TransactionModel::global(), SIGNAL(transactionAdded(Transaction*)),
39
            this, SLOT(watchTransaction(Transaction*)));
40
}
41
42
void LaunchListModel::watchTransaction(Transaction *trans)
43
{
44
    connect(trans, SIGNAL(statusChanged(Transaction::Status)),
45
            this, SLOT(transactionStatusChanged(Transaction::Status)));
46
}
47
48
void LaunchListModel::transactionStatusChanged(Transaction::Status status)
49
{
50
    Transaction *trans = qobject_cast<Transaction *>(sender());
51
52
    if (status == Transaction::DoneStatus)
53
        transactionFinished(trans);
54
}
55
56
void LaunchListModel::transactionFinished(Transaction* trans)
57
{
58
    bool doneInstall = trans->status() == Transaction::DoneStatus &&
59
                       trans->role() == Transaction::InstallRole;
60
61
    if(trans->resource()->canExecute() && doneInstall)
62
        addApplication(trans->resource());
63
}
64
65
void LaunchListModel::addApplication(AbstractResource* app)
66
{
67
    QList<QStandardItem*> items;
68
    QStringList execs = app->executables();
69
70
    for (const QString& exec : execs) {
71
        KService::Ptr service = KService::serviceByStorageId(exec);
72
73
        QString name = service->property("Name").toString();
74
        if (!service->genericName().isEmpty())
75
            name += QLatin1String(" - ") % service->genericName();
76
77
        QStandardItem *item = new QStandardItem(name);
78
        item->setIcon(QIcon::fromTheme(service->icon()));
79
        item->setData(service->desktopEntryPath(), Qt::UserRole);
80
        items += item;
81
    }
82
    if(!items.isEmpty())
83
        invisibleRootItem()->appendRows(items);
84
}
85
86
void LaunchListModel::invokeApplication(const QModelIndex &idx) const
87
{
88
    KToolInvocation::startServiceByDesktopPath(idx.data(Qt::UserRole).toString());
89
}
90
91
void LaunchListModel::invokeApplication(int row) const
92
{
93
    invokeApplication(index(row, 0));
94
}