Development/Tutorials/Plasma4/Services: Difference between revisions

From KDE TechBase
m (Try to keep it the same than others)
No edit summary
(4 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{Template:I18n/Language Navigation Bar|Development/Tutorials/Plasma/DataEngines}}
 


{{TutorialBrowser|
{{TutorialBrowser|
Line 7: Line 7:
name=Writing a Service|
name=Writing a Service|


pre=[http://mindview.net/Books/TICPP/ThinkingInCPP2e.html C++], [http://www.trolltech.com/products/qt/ Qt], [[Getting_Started/Build/KDE4|KDE4 development environment]]|
pre=[http://mindview.net/Books/TICPP/ThinkingInCPP2e.html C++], [http://www.trolltech.com/products/qt/ Qt], [[Getting_Started/Build|KDE development environment]]|


next=|  
next=|  
Line 26: Line 26:
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
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


<code bash>
<syntaxhighlight lang="bash">
plasmaengineexplorer
plasmaengineexplorer
</code>
</syntaxhighlight>


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"
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"
Line 37: Line 37:
==The Code==
==The Code==


This tutorial will cover adding a service associated with a source into an existing data engine.
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 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====
====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:
<syntaxhighlight lang="xml">
<?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>
</syntaxhighlight>


===The Main Code===
===The Main Code===
Line 50: Line 63:


====Building it====
====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.
<syntaxhighlight lang="bash">
set(myproject_SRCS
    myengine.cpp
    myservice.cpp
)
install(FILES org.kde.myprogramsname.service.operations
        DESTINATION ${DATA_INSTALL_DIR}/plasma/services)
</syntaxhighlight>
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==
==Testing==

Revision as of 16:42, 19 July 2012


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