File: ksolidnotify.cpp

package info (click to toggle)
kde-runtime 4%3A17.08.3-2.1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 25,204 kB
  • sloc: cpp: 111,675; ansic: 5,030; perl: 1,579; xml: 793; sh: 407; makefile: 42; python: 28
file content (214 lines) | stat: -rw-r--r-- 7,871 bytes parent folder | download | duplicates (3)
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
/*
   Copyright (C) 2010 by Jacopo De Simoi <wilderkde@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, 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; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

 */

#include "ksolidnotify.h"
#include "knotify.h"

//solid specific includes
#include <Solid/DeviceNotifier>
#include <Solid/Device>
#include <Solid/DeviceInterface>
#include <Solid/StorageDrive>
#include <Solid/StorageVolume>
#include <Solid/StorageAccess>
#include <Solid/OpticalDrive>
#include <Solid/OpticalDisc>
#include <Solid/PortableMediaPlayer>
#include <Solid/Predicate>

#include <QDBusConnection>
#include <QDBusConnectionInterface>
#include <QDBusServiceWatcher>

#include <KLocale>

static const char dbusDeviceNotificationsName[] = "org.kde.DeviceNotifications";
static const char dbusDeviceNotificationsPath[] = "/org/kde/DeviceNotifications";


KSolidNotify::KSolidNotify(KNotify* parent):
	QObject(parent),
	m_kNotify(parent),
	m_dbusServiceExists(false)
{
	Solid::Predicate p(Solid::DeviceInterface::StorageAccess);
	p |= Solid::Predicate(Solid::DeviceInterface::OpticalDrive);
    p |= Solid::Predicate(Solid::DeviceInterface::PortableMediaPlayer);
	QList<Solid::Device> devices = Solid::Device::listFromQuery(p);
	foreach (const Solid::Device &dev, devices)
	{
		m_devices.insert(dev.udi(), dev);
		connectSignals(&m_devices[dev.udi()]);
	}

	connect(Solid::DeviceNotifier::instance(), SIGNAL(deviceAdded(const QString &)),
			this, SLOT(onDeviceAdded(const QString &)));
	connect(Solid::DeviceNotifier::instance(), SIGNAL(deviceRemoved(const QString &)),
			this, SLOT(onDeviceRemoved(const QString &)));

	// check if service already exists on plugin instantiation
	QDBusConnectionInterface* interface = QDBusConnection::sessionBus().interface();
	m_dbusServiceExists = interface && interface->isServiceRegistered(dbusDeviceNotificationsName);

	if( m_dbusServiceExists )
		slotServiceOwnerChanged(dbusDeviceNotificationsName, QString(), "_"); //connect signals

	// to catch register/unregister events from service in runtime
	QDBusServiceWatcher *watcher = new QDBusServiceWatcher(this);
	watcher->setConnection(QDBusConnection::sessionBus());
	watcher->setWatchMode(QDBusServiceWatcher::WatchForOwnerChange);
	watcher->addWatchedService(dbusDeviceNotificationsName);
	connect(watcher, SIGNAL(serviceOwnerChanged(const QString&, const QString&, const QString&)),
			SLOT(slotServiceOwnerChanged(const QString&, const QString&, const QString&)));
}

void KSolidNotify::onDeviceAdded(const QString &udi)
{
	Solid::Device device(udi);
	m_devices.insert(udi, device);
	connectSignals(&m_devices[udi]);
}

void KSolidNotify::onDeviceRemoved(const QString &udi)
{
	if (m_devices[udi].is<Solid::StorageVolume>())
	{
		Solid::StorageAccess *access = m_devices[udi].as<Solid::StorageAccess>();
		if (access)
			disconnect(access, 0, this, 0);
	}
	m_devices.remove(udi);
}

bool KSolidNotify::isSafelyRemovable(const QString &udi)
{
	Solid::Device parent = m_devices[udi].parent();
	if (parent.is<Solid::StorageDrive>()) 
	{
		Solid::StorageDrive *drive = parent.as<Solid::StorageDrive>();
		return (!drive->isInUse() && (drive->isHotpluggable() || drive->isRemovable()));
	}
	Solid::StorageAccess* access = m_devices[udi].as<Solid::StorageAccess>();
	if (access) {
		return !m_devices[udi].as<Solid::StorageAccess>()->isAccessible();
	} else {
		// If this check fails, the device has been already physically 
		// ejected, so no need to say that it is safe to remove it
		return false;
	}
}

void KSolidNotify::connectSignals(Solid::Device* device)
{
	Solid::StorageAccess *access = device->as<Solid::StorageAccess>();
	if (access)
	{
		connect(access, SIGNAL(teardownDone(Solid::ErrorType, QVariant, const QString &)),
					this, SLOT(storageTeardownDone(Solid::ErrorType, QVariant , const QString &)));
		connect(access, SIGNAL(setupDone(Solid::ErrorType, QVariant, const QString &)),
					this, SLOT(storageSetupDone(Solid::ErrorType, QVariant , const QString &)));
	}	
	if (device->is<Solid::OpticalDisc>())
	{
		Solid::OpticalDrive *drive = device->parent().as<Solid::OpticalDrive>();
		connect(drive, SIGNAL(ejectDone(Solid::ErrorType, QVariant, const QString &)),
				this, SLOT(storageEjectDone(Solid::ErrorType, QVariant , const QString &)));
	}
}

void KSolidNotify::notifySolidEvent(QString event, Solid::ErrorType error, QVariant errorData, const QString & udi, const QString & errorMessage)
{
	ContextList context;
	if (m_dbusServiceExists)
	{
		KNotifyConfig mountConfig("hardwarenotifications", ContextList(), event);
		if (mountConfig.readEntry("Action").split('|').contains("Popup"))
		{
			QDBusMessage m = QDBusMessage::createMethodCall( dbusDeviceNotificationsName, dbusDeviceNotificationsPath, dbusDeviceNotificationsName, "notify" );
			m << error << errorMessage << errorData.toString().simplified() << udi;
			QDBusConnection::sessionBus().call(m);
		}
	context << QPair<QString, QString>("devnotifier", "present");
	}

	m_kNotify->event(event, "hardwarenotifications", context, i18n("Devices notification"), errorMessage, KNotifyImage(), QStringList(), -1);

}

void KSolidNotify::storageSetupDone(Solid::ErrorType error, QVariant errorData, const QString &udi)
{
	if (error)
	{
		Solid::Device device(udi);
		QString errorMessage = i18n("Could not mount the following device: %1", device.description());
		notifySolidEvent("mounterror", error, errorData, udi, errorMessage);
	}
}

void KSolidNotify::storageTeardownDone(Solid::ErrorType error, QVariant errorData, const QString &udi)
{
	if (error)
	{
		Solid::Device device(udi);
		QString errorMessage = i18n("Could not unmount the following device: %1\nOne or more files on this device are open within an application ", device.description());
		notifySolidEvent("mounterror", error, errorData, udi, errorMessage);
	} else if (isSafelyRemovable(udi))
	{
		Solid::Device device(udi);
		notifySolidEvent("safetoremove", error, errorData, udi, i18nc("The term \"remove\" here means \"physically disconnect the device from the computer\", whereas \"safely\" means \"without risk of data loss\"", "The following device can now be safely removed: %1", device.description()));
	}
}

void KSolidNotify::storageEjectDone(Solid::ErrorType error, QVariant errorData, const QString &udi)
{
	if (error)
	{
		QString discUdi;
		foreach (Solid::Device device, m_devices) {
		if (device.parentUdi() == udi) {
			discUdi = device.udi();
			}
		}

		if (discUdi.isNull()) {
			//This should not happen, bail out
			return;
		}

		Solid::Device discDevice(discUdi);
		QString errorMessage = i18n("Could not eject the following device: %1\nOne or more files on this device are open within an application ", discDevice.description());
		notifySolidEvent("mounterror", error, errorData, udi, errorMessage);
	} else if (isSafelyRemovable(udi))
	{
		Solid::Device device(udi);
		notifySolidEvent("safetoremove", error, errorData, udi, i18n("The following device can now be safely removed: %1", device.description()));
	}
}

void KSolidNotify::slotServiceOwnerChanged( const QString & serviceName, const QString & oldOwner, const QString & newOwner )
{
        Q_UNUSED(serviceName);
	if (newOwner.isEmpty())
		m_dbusServiceExists = false;
	else if (oldOwner.isEmpty())
		m_dbusServiceExists = true;
}


#include "ksolidnotify.moc"