1/* This file is part of the KDE Project
2 Copyright (c) 2005 Jean-Remy Falleri <jr.falleri@laposte.net>
3 Copyright (c) 2005-2007 Kevin Ottens <ervin@kde.org>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License version 2 as published by the Free Software Foundation.
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 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
18*/
19
20#include "deviceactionsdialog.h"
21#include <QLayout>
22
23#include <krun.h>
24#include <klocale.h>
25#include <kstandarddirs.h>
26#include <kio/global.h>
27#include <klistwidget.h>
28#include <kicon.h>
29#include <QCheckBox>
30
31
32#include "deviceaction.h"
33#include "ui_deviceactionsdialogview.h"
34
35DeviceActionsDialog::DeviceActionsDialog(QWidget *parent)
36 : KDialog(parent)
37{
38 setModal(false);
39 setButtons(Ok|Cancel);
40 setDefaultButton(Ok);
41
42 QWidget *page = new QWidget(this);
43 m_view.setupUi(page);
44 setMainWidget(page);
45 updateActionsListBox();
46
47 resize(QSize(400,400).expandedTo(minimumSizeHint()));
48
49 connect(this, SIGNAL(okClicked()),
50 this, SLOT(slotOk()));
51 connect(m_view.actionsList, SIGNAL(doubleClicked(QListWidgetItem *, const QPoint &)),
52 this, SLOT(slotOk()));
53
54 connect(this, SIGNAL(finished()),
55 this, SLOT(delayedDestruct()));
56}
57
58DeviceActionsDialog::~DeviceActionsDialog()
59{
60}
61
62void DeviceActionsDialog::setDevice(const Solid::Device &device)
63{
64 m_device = device;
65
66 QString label = device.vendor();
67 if (!label.isEmpty()) label+=' ';
68 label+= device.product();
69
70 setWindowTitle(label);
71
72 m_view.iconLabel->setPixmap(KIcon(device.icon()).pixmap(64));
73 m_view.descriptionLabel->setText(device.vendor()+' '+device.product());
74 setWindowIcon(KIcon(device.icon()));
75}
76
77Solid::Device DeviceActionsDialog::device() const
78{
79 return m_device;
80}
81
82void DeviceActionsDialog::setActions(const QList<DeviceAction*> &actions)
83{
84 qDeleteAll(m_actions);
85 m_actions.clear();
86
87 m_actions = actions;
88
89 updateActionsListBox();
90}
91
92QList<DeviceAction*> DeviceActionsDialog::actions() const
93{
94 return m_actions;
95}
96
97void DeviceActionsDialog::updateActionsListBox()
98{
99 m_view.actionsList->clear();
100
101 foreach (DeviceAction *action, m_actions) {
102 QListWidgetItem *item = new QListWidgetItem(KIcon(action->iconName()),
103 action->label());
104 item->setData(Qt::UserRole, action->id());
105 m_view.actionsList->addItem(item);
106 }
107
108 if (m_view.actionsList->count()>0)
109 m_view.actionsList->item(0)->setSelected(true);
110}
111
112void DeviceActionsDialog::slotOk()
113{
114 QListWidgetItem *item = m_view.actionsList->selectedItems().value(0);
115
116 if (item != 0L) {
117 QString id = item->data(Qt::UserRole).toString();
118
119 foreach (DeviceAction *action, m_actions) {
120 if (action->id()==id) {
121 launchAction(action);
122 return;
123 }
124 }
125 }
126}
127
128void DeviceActionsDialog::launchAction(DeviceAction *action)
129{
130 action->execute(m_device);
131 accept();
132}
133
134#include "deviceactionsdialog.moc"
135