Development/Tutorials/Services/Traders (de)

From KDE TechBase
Revision as of 15:15, 20 November 2007 by DrSlowDecay (talk | contribs) (Seite angelegt und erste Teile übersetzt/Page created)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


Development/Tutorials/Services/Traders


Zusammenfassung

It is often desirable to be able to find specific types of services or services with specific features or designations. KDE provides a simple yet powerful query language to accomplish this called the KTrader Query Language.

Eine Geschichte von zwei Tradern

KDE provides two classes that act as "traders". A trader takes a query and returns a set of services that match those constraints. There is one trader for plugins and other add-ons: KServiceTypeTrader, and one for mimetypes: KMimetypeTrader. Characteristics for both are similar: they have same syntax for querying, offer a singleton pattern accessor, return KService::Ptrs, as well as other similarities. So while this tutorial concentrates primarily on the KServiceTypeTrader, much of the content is applicable to the mimetype trader as well.

Dienst Typen

KServiceTypeTrader is used to locate individual components such as application plugins, screensavers, and control panels that are registered with the system. The primary concept used here, is that of the "service type". Each set of services has a unique service type, which makes it very easy to locate the sort of component needed.

This means that, each kind of application plugin is uniquely namespaced within the set of all services. So, it is trivial to locate plugins for a given application, without having to worry about getting another application's plugin in the list.

Examples of service types include:

  • KParts/ReadWritePart
  • KTextEditor/Document
  • ScreenSaver
  • TerminalEmulator
  • UserAgentStrings
  • ThumbCreator
  • KDevelop/Plugin

There is no limit to the number of service types that a given application may use or register. Of course, service types are not limited to plugins and may be used for any sort of data component.

Creating new service types is covered in the next tutorial of this series: Creating and Loading Plugins Using KService

Benutzung von KServiceTypeTrader

The service trader is always accessed via the self() singleton accessor. With the KServiceTypeTrader in hand, we can then do one of three things:

  • query:query for a list of services ordered according to the user's preferences (if any)
  • defaultOffers: request all services, unsorted
  • preferredService: request the preferred service of a given type

The code to do this is very straight forward: KService::List services; KServiceTypeTrader* trader = KServiceTypeTrader::self();

services = trader->query("KParts/ReadWritePart"); foreach (KService::Ptr service, services) {

   kDebug() << "read write part" << service->name();

}

services = trader->defaultOffers("ThumbCreator"); if (services.isEmpty()) {

   kDebug() << "no services found for ThumbCreator!";

}

KService::Ptr service = trader->preferredService("KDevelop/Plugin"); if (!service) {

   kDebug() << "no preferred service found for KDevelop/Plugin";

}

In each case zero or more services are returned that are now usable for locating, describing and loading the item it represents.

Die KTrader Query Sprache

The above examples are quite simplistic and are not detailed enough for many application needs. For instance, we may only want to list plugins for a certain category, that are associated with a particular mimetype, or that have a specific plugin name. This is where the query language comes in.

The query language itself is designed to be human readable and flexible. KServiceTypeTrader::query optionally takes a query, in addition to the service type, and uses that to provide more fine-grained searches. So, for example, we'll modify the earlier example in the following way:

KService::List services ; KServiceTypeTrader* trader = KServiceTypeTrader::self();

QString constraint = "'text/plain' in MimeTypes and "

                    "('KOfficePart' in ServiceTypes or "
                    " 'oasis' ~ [X-KDE-ExtraNativeMimeTypes])";

services = trader->query("KParts/ReadWritePart", constraint);

foreach (KService::Ptr service, services) {

   kDebug() << "read write part" << service->name();

}

This code will look for a KPart that is both capable of reading/writing plain text files and is also a KOffice component, or can simply read ODF document formats.

Errors in queries are reported via debug output to console at runtime.

Reihenfolge der Operatoren

The query string is evaluated left to right, one section at a time. Sections are divided by boolean operators (see below) or by parentheses ('()'), which serve as grouping characters.

Basistypen

Drei Basistypen werden von der KTrader Query Sprache unterstützt:

  • strings: Zeichenketten, die einfachen Anführungsstrichen (') umschlossen sein müssen
  • booleans: Boolsche Werte (TRUE oder FALSE)
  • signed integers: vorzeichenbehaftete Ganzzahlen
  • signed floating point numbers: vorzeichenbehaftete Gleitkommazahlen

Kennungen

Identifiers in a query string are mapped to entries listed in the service's .desktop file. For example, "Name" is the name of the service, "ServiceTypes" is a list of the service types it supports.

Identifiers may only contain alphanumeric characters and the '-' character. Identifiers that do not start with an alphabetical character or that contain non-alphanumeric characters must be enclosed in brackets, e.g. [X-KDE-Init].

There are also three special identifiers:

  • DesktopEntryName stands for the filename of the service desktop entry without its extension. This can be useful to exclude some specific services.
  • DesktopEntryPath stands for the relative or full path to the .desktop file, see KService::desktopEntryPath.
  • Library: a synonym for [X-KDE-Library] in the .desktop file.

An identifier can be checked for existence with the exist operator, e.g. "exist [X-KDE-Library]". This is especially useful when checking for the value of an identifier with a default value. For example, if MyProp is a property with type boolean, one might write "not exist MyProp or MyProp" to match MyProp with a default of "true".

Vergleichsoperatoren

Die folgenden Vergleichoperatoren können benutzt werden, um Werte von allen unterstützten Daten miteinander zu vergleichen:

  • ==: gleichheit
  • !=: ungleichheit
  • <: kleiner als
  • <=: kleiner oder gleich
  • >: größer als
  • >=: größer oder gleich

Mathematische Operatoren

Folgende mathematische Operatoren können auf Zahlentypen angewandt werden

  • +
  • -
  • *
  • /

Boolsche Operatoren

Folgende boolsche Operatoren werden unterstützt:

  • and: logisches und
  • or: logisches oder
  • not: logisches nicht

Operatoren für Zeichenketten

Die Operatoren, die auf Gleichheit von Zeichenketten prüfen, geben einen boolschen Wert zurück, der anzeigt, ob erfolgreich oder nicht.

Zeichenkettenvergleiche werden mit folgenden Operatoren erledigt:

  • ==: Gleichheit, Groß- und Kleinschreibung wird beachtet
  • =~: Gleichheit, Groß- und Kleinschreibung wird nicht beachtet
  • !=: Ungleichheit, Groß- und Kleinschreibung wird beachtet
  • !~: Ungleichheit, Groß- und Kleinschreibung wird nicht beachtet

Sub-string matching can be accomplished by using the following operators:

  • ~: contains, e.g. "'Bar' ~ 'FooBarBaz'" is true
  • ~~: contains with case insensitive matching, e.g. "'Bar' ~~ 'FoobarBaz'" is true

Some properties, such as MimeTypes and ServiceTypes, are lists of strings. These lists can be searched using the following two operators:

  • in: the list contains the string, e.g. "'MyApp/Plugin' in ServiceTypes'"
  • ~in: the list contains the string, with case insensitive matching