[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-devel
Subject:    [PATCH + FEATURE] Automatic notification of hotplugged devices :
From:       Edwin Schepers <yez () home ! nl>
Date:       2005-05-11 20:55:07
Message-ID: 200505112259.06028.yez () home ! nl
[Download RAW message or body]

Hi,
This is my draft version of the 'medianotifier'. The medianotifier program is 
started when KDE's Media Manager detects a new 
device. Then, one can choose which program should be started. 
Since KDE's Media Manager starts medianotifier, I have a patch. It currently 
only works with HAL, but I guess Media Manager can be adapted so that it also 
works without HAL.

Since the medianotifier tgz is a rough 700kb, I provide a link :
http://members.home.nl/edwin.schepers/medianotifier/medianotifier-0.1.tgz

When you install the program, unpack it in kdebase/kioslave/media, since it 
requires a media/libmediacommon headerfile and library that are not installed.

Eventually, I would like this program to be in kdebase (kioslave/media?). What 
are the prerequisites / steps to be taken for having it in kdebase ?
Is the first step to add it to playground/<something> ?  I think my program 
can reach relatively quick a mature state since it's a very small/simple 
program.

Regards,
Edwin

["media.patch" (text/x-diff)]

Index: halbackend.cpp
===================================================================
--- halbackend.cpp	(revision 410407)
+++ halbackend.cpp	(working copy)
@@ -22,6 +22,8 @@
 #include <klocale.h>
 #include <kurl.h>
 #include <kdebug.h>
+#include <kprocess.h>
+#include <kstandarddirs.h>
 
 #define MOUNT_SUFFIX	(libhal_volume_is_mounted(halVolume) ? QString("_mounted") : \
QString("_unmounted"))  
@@ -33,9 +35,17 @@
 {
 	char*   _ppt_string;
 	QString _ppt_QString;
+	#ifdef HAL_0_4
+	_ppt_string = hal_device_get_property_string(ctx, udi, key, NULL);
+	#else
 	_ppt_string = libhal_device_get_property_string(ctx, udi, key, NULL);
+	#endif
 	_ppt_QString = QString(_ppt_string ? _ppt_string : "");
+	#ifdef HAL_0_4
+	hal_free_string(_ppt_string);
+	#else
 	libhal_free_string(_ppt_string);
+	#endif
 	return _ppt_QString;
 }
 
@@ -110,8 +120,10 @@
 	DBusError error;
 	dbus_error_init(&error);
 	DBusConnection *dbus_connection = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
-	if (dbus_error_is_set(&error))
+	if (dbus_error_is_set(&error)) {
+		kdDebug() << "error in dbus_bus_get()"<< endl;
 		return false;
+	}
 	MainLoopIntegration(dbus_connection);
 	libhal_ctx_set_dbus_connection(m_halContext, dbus_connection);
 
@@ -153,49 +165,42 @@
 		return false;
 
 	kdDebug() << "HALBackend::ListDevices : " << numDevices << " devices found" << \
                endl;
-	for (int i = 0; i < numDevices; i++)
-		AddDevice(halDeviceList[i]);
+	DeviceType type;
+	for (int i = 0; i < numDevices; i++) {
+		type = TypeOfDevice(halDeviceList[i]);
+		if ( type != HALBackend::Invalid )
+			AddDevice(halDeviceList[i], type);
+	}
 
 	return true;
 }
 
 /* Create a media instance for the HAL device "udi".
 This functions checks whether the device is worth listing */
-void HALBackend::AddDevice(const char *udi)
+HALBackend::DeviceType HALBackend::TypeOfDevice(const char *udi)
 {
 	/* We don't deal with devices that do not expose their capabilities.
 	If we don't check this, we will get a lot of warning messages from libhal */
 	if (!libhal_device_property_exists(m_halContext, udi, "info.capabilities", NULL))
-		return;
+		return Invalid;
 
-	/* If the device is already listed, do not process.
-	This should not happen, but who knows... */
-	/** @todo : refresh properties instead ? */
-	if (m_mediaList.findById(udi))
-		return;
-
 	/* Add volume block devices */
 	if (libhal_device_query_capability(m_halContext, udi, "volume", NULL))
 	{
 		/* We only list volume that have a filesystem or volume that have an audio track*/
 		if ( (hal_device_get_property_QString(m_halContext, udi, "volume.fsusage") != \
                "filesystem") &&
 		     (!libhal_device_get_property_bool(m_halContext, udi, "volume.disc.has_audio", \
                NULL)) )
-			return;
+			return Invalid;
 		/* Query drive udi */
 		QString driveUdi = hal_device_get_property_QString(m_halContext, udi, \
"block.storage_device");  /* We don't list floppy volumes because we list floppy \
drives */  if ((hal_device_get_property_QString(m_halContext, driveUdi.ascii(), \
                "storage.drive_type") == "floppy") ||
 		    (hal_device_get_property_QString(m_halContext, driveUdi.ascii(), \
                "storage.drive_type") == "zip"))
-			return;
+			return Invalid;
 
 		/** @todo check exclusion list **/
 
-		/* Create medium */
-		Medium* medium = new Medium(udi, "");
-		setVolumeProperties(medium);
-		m_mediaList.addMedium(medium);
-
-		return;
+		return Volume;
 	}
 
 	/* Floppy & zip drives */
@@ -203,27 +208,49 @@
 		if ((hal_device_get_property_QString(m_halContext, udi, "storage.drive_type") == \
                "floppy") ||
 		    (hal_device_get_property_QString(m_halContext, udi, "storage.drive_type") == \
"zip"))  {
-			/* Create medium */
-			Medium* medium = new Medium(udi, "");
-			setFloppyProperties(medium);
-			m_mediaList.addMedium(medium);
-			return;
+			return Floppy;
 		}
 
 	/* Camera handled by gphoto2*/
 	if (libhal_device_query_capability(m_halContext, udi, "camera", NULL))
 
 		{
-			/* Create medium */
-			Medium* medium = new Medium(udi, "");
-			setCameraProperties(medium);
-			m_mediaList.addMedium(medium);
-			return;
+			return Camera;
 		}
+	return Invalid;
 }
 
+void HALBackend::AddDevice(const char *udi, DeviceType type)
+{
+	/* If the device is already listed, do not process.
+	This should not happen, but who knows... */
+	/** @todo : refresh properties instead ? */
+	if (m_mediaList.findById(udi))
+		return;
+
+	/* Create medium */
+	Medium* medium = new Medium(udi, "");
+	switch ( type )
+	{
+		case Volume :
+					setVolumeProperties(medium);
+					break;
+		case Floppy:
+					setFloppyProperties(medium);
+					break;
+		case Camera:
+					setCameraProperties(medium);
+					break;
+		case Invalid:
+					return;
+	}
+
+	m_mediaList.addMedium(medium);
+}
+
 void HALBackend::RemoveDevice(const char *udi)
 {
+kdDebug() << "HALBackend::RemoveDevice" << endl;
 	m_mediaList.removeMedium(udi);
 }
 
@@ -541,9 +568,18 @@
 
 void HALBackend::hal_device_added(LibHalContext *ctx, const char *udi)
 {
+	DeviceType type;
 	kdDebug() << "HALBackend::hal_device_added " << udi <<  endl;
 	Q_UNUSED(ctx);
-	s_HALBackend->AddDevice(udi);
+	type = s_HALBackend->TypeOfDevice(udi);
+	if ( type != Invalid ) {
+		s_HALBackend->AddDevice(udi, type);
+                if ( KStandardDirs::findExe("medianotifier") != NULL )
+		kdDebug() << "run " << KStandardDirs::findExe("medianotifier") << endl;
+		KProcess *proc = new KProcess;
+		*proc << "medianotifier" << "--udi" << udi ;
+		proc->start(KProcess::DontCare);
+	}
 }
 
 void HALBackend::hal_device_removed(LibHalContext *ctx, const char *udi)
Index: halbackend.h
===================================================================
--- halbackend.h	(revision 410407)
+++ halbackend.h	(working copy)
@@ -119,6 +119,11 @@
 Q_OBJECT
 
 public:
+	enum DeviceType {	Invalid,
+				Volume,
+				Floppy,
+				Camera
+			};
 	/**
 	* Constructor
 	*/
@@ -145,12 +150,21 @@
 
 private:
 	/**
+	* 
+	* Determine if the device is a valid one
+	*
+	*  @param udi                 Universal Device Id
+	*/
+	DeviceType TypeOfDevice(const char *udi);
+
+	/**
 	* Append a device in the media list. This function will check if the device
 	* is worth listing.
 	*
 	*  @param udi                 Universal Device Id
+	*  @param type                The type of the device
 	*/
-	void AddDevice(const char* udi);
+	void AddDevice(const char* udi, DeviceType type);
 
 	/**
 	* Remove a device from the device list


["screenshot.png" (image/png)]

>> Visit http://mail.kde.org/mailman/listinfo/kde-devel#unsub to unsubscribe <<


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic