📜 ⬆️ ⬇️

Writing for KDE4: Solid

Having read the habratopic pillars of KDE4. Solid. I decided to check whether this framework is so convenient for developers and what it actually knows how?

Theory


Most of the solid classes available to the programmer for development are contained in kdelibs and include:

Solid :: AcAdapter Work with AC adapters
Solid :: AudioInterface Working with sound cards
Solid :: Battery Working with batteries
Solid :: Block Working with block devices
Solid :: Button Working with specific buttons (Lid, Power, Sleep)
Solid :: Camera Working with digital cameras
Solid :: Device Allows applications to find out if a particular hardware is available in the system.
Solid :: DeviceInterface Base class for all device interfaces
Solid :: DeviceNotifier Alerting the system of all devices that are connected and connected during operation
Solid :: DvbInterface Work with DVB (Digital Video Broadcast) devices
Solid :: GenericInterface A common interface for working with devices directly
Solid :: NetworkInterface Working with network interfaces
Solid :: Networking :: Notifier Alerts the system about network status changes (Unknown, Unconnected, Disconnecting, Connecting, Connected)
Solid :: PowerManagement :: Notifier Power Alert System
Solid :: OpticalDisc Working with optical media
Solid :: OpticalDrive Working with optical drives
Solid :: PortableMediaPlayer Working with portable players
Solid :: Predicate Shows whether the device belongs to a particular type.
Solid :: Processor Work with a processor
Solid :: SerialInterface Working with Serial Interface Devices
Solid :: StorageAccess Working with sections of storage devices
Solid :: StorageDrive Working with storage devices
Solid :: StorageVolume Working with all devices that may contain information
Solid :: Video Working with video processing devices

Using a set of these classes, you can simply obtain information about any devices present in the system.
')

Practice


Let's try to write a simple example. in which we use a couple of Solid features.

To start the experiments, you need to install the necessary dev packages into your system. Commanding
$ sudo aptitude install automoc cmake kdelibs5-dev
and wait for about five minutes until everything needed is downloaded and installed.

The application will consist of three files and implement two functions:
1) Listing all devices available on the system at startup
2) Tracking all newly connected devices in the system.

app.h:
#include <solid/devicenotifier.h>
#include <solid/device.h>

#include <QApplication.h>
#include <QDebug.h>

class App : public QApplication
{
Q_OBJECT

public :
App( int args, char **argv);
virtual ~App();
private slots:
void deviceAdded( const QString &udi);
void deviceRemoved( const QString &udi);
};


* This source code was highlighted with Source Code Highlighter .

app.cpp:
#include "app.h"

App::App( int args, char **argv )
: QApplication(args, argv)
{
//
qDebug() << "===== All available devices =====" ;
foreach ( const Solid::Device &device, Solid::Device::allDevices())
{
qDebug() << device.udi().toLatin1().constData();
}

//
qDebug() << "\n===== Start monitoring for new =====" ;
Solid::DeviceNotifier *notifier = Solid::DeviceNotifier::instance ();
connect(notifier, SIGNAL(deviceAdded ( const QString &)), this , SLOT(deviceAdded(QString const &)));
connect(notifier, SIGNAL(deviceRemoved(QString const &)), this , SLOT(deviceRemoved(QString const &)));
}

void App::deviceAdded(QString const &udi)
{
qDebug() << "Added" << udi.toLatin1().constData();
}

void App::deviceRemoved(QString const &udi)
{
qDebug() << "Removed" << udi.toLatin1().constData();
}

App::~App()
{
//TODO
}


* This source code was highlighted with Source Code Highlighter .

main.cpp:
#include "app.h"

int main( int args, char **argv)
{

App app( args, argv );

return app.exec();
}


* This source code was highlighted with Source Code Highlighter .

CMakeLists.txt:
project(solid)

find_package(KDE4 REQUIRED)

include_directories( ${KDE4_INCLUDES} )

set (mySources main.cpp app.h app.cpp)

kde4_add_executable(solid ${mySources})

target_link_libraries(solid ${KDE4_KDEUI_LIBS} ${KDE4_KPARTS_LIBS} )

install(TARGETS solid DESTINATION bin)


* This source code was highlighted with Source Code Highlighter .

We save all this in a directory and execute in turn:
cmake .
make
./solid


* This source code was highlighted with Source Code Highlighter .

If everything is assembled correctly, after launch we will see a list of all devices in the system.

Now we will try to connect a USB flash drive or mp3 player to the usb connector and see something like:
Added /org/freedesktop/Hal/devices/usb_device_58f_6387_51ZT24OY_4
Added /org/freedesktop/Hal/devices/usb_device_58f_6387_51ZT24OY_4_if0
Added /org/freedesktop/Hal/devices/usb_device_58f_6387_51ZT24OY_4_usbraw
Added /org/freedesktop/Hal/devices/usb_device_58f_6387_51ZT24OY_4_if0_scsi_host
Added /org/freedesktop/Hal/devices/usb_device_58f_6387_51ZT24OY_4_if0_scsi_host_scsi_device_lun0
Added /org/freedesktop/Hal/devices/usb_device_58f_6387_51ZT24OY_4_if0_scsi_host_scsi_device_lun0_scsi_generic
Added /org/freedesktop/Hal/devices/storage_serial_JetFlash_TS2GJFV60_51ZT24OY_0_0
Added /org/freedesktop/Hal/devices/volume_uuid_4979_CC88


* This source code was highlighted with Source Code Highlighter .

Turning off our device and see:
Removed /org/freedesktop/Hal/devices/usb_device_58f_6387_51ZT24OY_4_if0_scsi_host_scsi_device_lun0_scsi_generic
Removed /org/freedesktop/Hal/devices/volume_uuid_4979_CC88
Removed /org/freedesktop/Hal/devices/storage_serial_JetFlash_TS2GJFV60_51ZT24OY_0_0
Removed /org/freedesktop/Hal/devices/usb_device_58f_6387_51ZT24OY_4_if0_scsi_host_scsi_device_lun0
Removed /org/freedesktop/Hal/devices/usb_device_58f_6387_51ZT24OY_4_if0_scsi_host
Removed /org/freedesktop/Hal/devices/usb_device_58f_6387_51ZT24OY_4_if0
Removed /org/freedesktop/Hal/devices/usb_device_58f_6387_51ZT24OY_4_usbraw


* This source code was highlighted with Source Code Highlighter .

All this means that everything worked out for us, solid works and uses HAL as its backend.

For the preparation of notes used material site api.kde.org. Going to which you can find an exhaustive amount of information on Solid, including tutorials, API documentation, etc.

Source: https://habr.com/ru/post/54616/


All Articles