Development/Tutorials/Plasma4/Services: Difference between revisions

From KDE TechBase
No edit summary
 
(No difference)

Latest revision as of 23:27, 11 September 2014


Writing a Service
Tutorial Series   Plasma Tutorial
Previous   C++, Qt, KDE development environment
What's Next  
Further Reading   DataEngine API, CMake Tutorials, Writing a Plasmoid (Tutorial), Writing a Data Engine (Tutorial)

Abstract

cover services, .operations file, .operations dtd spec, perhaps make a service like the pastebin one or something. idk, something easy.

http://www.kde.org/standards/kcfg/1.0/kcfg.xsd

also mention how a service can access the dataengine via it's pointer(I think that's in public api), if it really really needs to(e.g. plasma-kdm can't live without that)

The Plasma Engine Explorer

A very useful tool for anyone writing services is the Plasma engine explorer. You can use it to see test services by launching them by running

plasmaengineexplorer

If the service you want to test is associated to a source, you can access a dialog to launch the service and set its parameters by using the contextual menu of the source and selecting "Get associated service"


The Code

This tutorial will cover adding a service associated with a source into an existing data engine. Depending on your requirements, you may want a service which is associated with a certain source, and will be able to perform actions based on that source, or you may want a service that simply performs global calls which are not tied to any one source. We will cover the first case, since it is the most prominent.

The Description of the Service and its parameters

The service you make yourself and can really have any parameters you want, it's quite flexible. The constructor can be e.g. MyService(QObject* parent, const QString& source), or it can create a custom class which is a custom data source.

Writing an Operations File

The operations file is quite core to the service, as it defines which operations are available. It is simply an XML file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE kcfg SYSTEM
    "http://www.kde.org/standards/kcfg/1.0/kcfg.xsd">
<kcfg>
    <group name="setIcon">
        <entry name="Icon" type="String">
            <label>Set the name of the icon for this somesource</label>
        </entry>
    </group>
</kcfg>

The Main Code

Responding to Request for the Service Associated to a Source

Writing the Code Executing the Action of the Service

Building it

As in the previous DataEngine tutorial, the CMakeLists.txt file is required to build your plugin. To create a service in addition to an engine, only a few lines of code need to be added. We need to add our service's C++ source file to our existing project, then install the operations file that was made.

set(myproject_SRCS 
    myengine.cpp
    myservice.cpp
)

install(FILES org.kde.myprogramsname.service.operations
        DESTINATION ${DATA_INSTALL_DIR}/plasma/services)

In the case of "myprogramsname", that is simply e.g. microblog, pastebin, activities or whatever your collection of components is going to be named. (the reverse-domain name is used to prevent conflictions, so try to use it as much as possible).

Testing