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 Copyright (c) 2007 Alexis Ménard <darktears31@gmail.com>
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License version 2 as published by the Free Software Foundation.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 #include "soliduiserver.h"
24 #include <QtDBus/QDBusInterface>
25 #include <QtDBus/QDBusReply>
26 #include <QtDBus/QDBusError>
27 #include <kjobuidelegate.h>
29 #include <kapplication.h>
34 #include <kmessagebox.h>
35 #include <kstandardguiitem.h>
36 #include <kstandarddirs.h>
37 #include <kdesktopfileactions.h>
38 #include <kwindowsystem.h>
39 #include <kpassworddialog.h>
41 #include "deviceactionsdialog.h"
42 #include "deviceaction.h"
43 #include "deviceserviceaction.h"
44 #include "devicenothingaction.h"
47 #include <kpluginfactory.h>
48 #include <kpluginloader.h>
50 K_PLUGIN_FACTORY(SolidUiServerFactory
,
51 registerPlugin
<SolidUiServer
>();
53 K_EXPORT_PLUGIN(SolidUiServerFactory("soliduiserver"))
56 SolidUiServer::SolidUiServer(QObject
* parent
, const QList
<QVariant
>&)
61 SolidUiServer::~SolidUiServer()
65 void SolidUiServer::showActionsDialog(const QString
&udi
,
66 const QStringList
&desktopFiles
)
68 if (m_udiToActionsDialog
.contains(udi
)) {
69 DeviceActionsDialog
*dialog
= m_udiToActionsDialog
[udi
];
70 dialog
->activateWindow();
75 QList
<DeviceAction
*> actions
;
77 foreach (const QString
&desktop
, desktopFiles
) {
78 QString filePath
= KStandardDirs::locate("data", "solid/actions/"+desktop
);
80 QList
<KServiceAction
> services
81 = KDesktopFileActions::userDefinedServices(filePath
, true);
83 foreach (const KServiceAction
&service
, services
) {
84 DeviceServiceAction
*action
= new DeviceServiceAction();
85 action
->setService(service
);
90 // Only one action, execute directly
91 if (actions
.size()==1) {
92 DeviceAction
*action
= actions
.takeFirst();
93 Solid::Device
device(udi
);
94 action
->execute(device
);
99 actions
<< new DeviceNothingAction();
101 DeviceActionsDialog
*dialog
= new DeviceActionsDialog();
102 dialog
->setDevice(Solid::Device(udi
));
103 dialog
->setActions(actions
);
105 connect(dialog
, SIGNAL(finished()),
106 this, SLOT(onActionDialogFinished()));
108 m_udiToActionsDialog
[udi
] = dialog
;
110 // Update user activity timestamp, otherwise the notification dialog will be shown
111 // in the background due to focus stealing prevention. Entering a new media can
112 // be seen as a kind of user activity after all. It'd be better to update the timestamp
113 // as soon as the media is entered, but it apparently takes some time to get here.
114 kapp
->updateUserTimestamp();
119 void SolidUiServer::onActionDialogFinished()
121 DeviceActionsDialog
*dialog
= qobject_cast
<DeviceActionsDialog
*>(sender());
124 QString udi
= dialog
->device().udi();
125 m_udiToActionsDialog
.remove(udi
);
130 void SolidUiServer::showPassphraseDialog(const QString
&udi
,
131 const QString
&returnService
, const QString
&returnObject
,
132 uint wId
, const QString
&appId
)
134 if (m_idToPassphraseDialog
.contains(returnService
+':'+udi
)) {
135 KPasswordDialog
*dialog
= m_idToPassphraseDialog
[returnService
+':'+udi
];
136 dialog
->activateWindow();
140 Solid::Device
device(udi
);
142 KPasswordDialog
*dialog
= new KPasswordDialog();
144 QString label
= device
.vendor();
145 if (!label
.isEmpty()) label
+=' ';
146 label
+= device
.product();
148 dialog
->setPrompt(i18n("'%1' needs a password to be accessed. Please enter a password.", label
));
149 dialog
->setPixmap(KIcon(device
.icon()).pixmap(64, 64));
150 dialog
->setProperty("soliduiserver.udi", udi
);
151 dialog
->setProperty("soliduiserver.returnService", returnService
);
152 dialog
->setProperty("soliduiserver.returnObject", returnObject
);
154 connect(dialog
, SIGNAL(gotPassword(const QString
&, bool)),
155 this, SLOT(onPassphraseDialogCompleted(const QString
&, bool)));
156 connect(dialog
, SIGNAL(rejected()),
157 this, SLOT(onPassphraseDialogRejected()));
159 m_idToPassphraseDialog
[returnService
+':'+udi
] = dialog
;
161 reparentDialog(dialog
, (WId
)wId
, appId
, true);
165 void SolidUiServer::onPassphraseDialogCompleted(const QString
&pass
, bool keep
)
167 KPasswordDialog
*dialog
= qobject_cast
<KPasswordDialog
*>(sender());
170 QString returnService
= dialog
->property("soliduiserver.returnService").toString();
171 QString returnObject
= dialog
->property("soliduiserver.returnObject").toString();
172 QDBusInterface
returnIface(returnService
, returnObject
);
174 QDBusReply
<void> reply
= returnIface
.call("passphraseReply", pass
);
176 QString udi
= dialog
->property("soliduiserver.udi").toString();
177 m_idToPassphraseDialog
.remove(returnService
+':'+udi
);
179 if (!reply
.isValid()) {
180 kWarning() << "Impossible to send the passphrase to the application, D-Bus said: "
181 << reply
.error().name() << ", " << reply
.error().message() << endl
;
186 void SolidUiServer::onPassphraseDialogRejected()
188 onPassphraseDialogCompleted(QString(), false);
191 void SolidUiServer::reparentDialog(QWidget
*dialog
, WId wId
, const QString
&appId
, bool modal
)
193 // Code borrowed from kwalletd
195 KWindowSystem::setMainWindow(dialog
, wId
); // correct, set dialog parent
199 KWindowSystem::setState(dialog
->winId(), NET::Modal
);
201 KWindowSystem::clearState(dialog
->winId(), NET::Modal
);
205 // allow dialog activation even if it interrupts, better than trying hacks
206 // with keeping the dialog on top or on all desktops
207 kapp
->updateUserTimestamp();
210 #include "soliduiserver.moc"