Monday, 11 August 2014

Qt5Sparql and Tracker

Tracker is a service that runs in the background and collects information about things that are on the phone and stores it all into a Sparql database. On the Jolla, Tracker is used for things like the Gallery App and the Media App, so if you wanted to create a gallery or a music player app half the work is already done for you.

You can query the Tracker database from the command line using the tracker-sparql command
eg. tracker-sparql -q "SELECT
?title ?artist ?length ?album
WHERE { ?song a nmm:MusicPiece .
    ?song nie:title ?title .
    ?song nfo:duration ?length .
        ?song nie:url ?url .
    ?song nmm:performer ?aName .
    ?aName nmm:artistName ?artist .
    ?song nmm:musicAlbum ?malbum .
    ?malbum nmm:albumTitle ?album
    FILTER regex(?title, '^A', 'i')

This is a select query that will return info about the music files indexed by tracker, notice the FILTER line is a regex that will only return items that start with 'A'.

Another cool type of query is the ASK query, which will return true or false depending on whether any results exist
eg. tracker-sparql -q "ASK {
    ?song a nmm:MusicPiece .
    ?song nie:title ?title
    FILTER regex(?title, '^A', 'i') 

Sound great! But how do you use it?

First thing is you need to install the Qt5Sparql libraries into your development environment

You can do this by opening your project in QtCreator and
  1. Click on SailfishOS on the side menu
  2. Under Targets click Manage Target on your desired environment
  3. In the search area, type in sparql
  4. Install all shown packages
Once you've done that you need to add QtSparql to the CONFIG area in your .pro file

In your QML file you can now write import QtSparql 1.0, but it will still have a red line underneath it with a "QML Module does not contain information about components contained in plugins" error. This error can be ignored and will still compile and run, but the QtCreator IDE will not autocomplete for you. If you want to fix the error then you need to use a qmlplugdump program to generate the required file, but it doesn't come with the SailfishOS SDK and I couldn't be bothered trying to compile it myself from source.

Instead you can use the git repository as documentation on the objects and what signals/properties/methods they have

Also in there is a bunch of examples which are fairly handy

Example Code

The easiest way to get going is with something like the following, its a ListModel QML object that gets its ListItems from Tracker using a SPARQL query

SparqlListModel {
   id: queryModel
   objectName: queryModel
   // create a new SparqlConnection for the queryModel
   connection: SparqlConnection { id:sparqlConnection;  
                                  driver:"QTRACKER_DIRECT" }

   // This is the query for the model

   query: "SELECT ?title ?artist ?length ?album ?url "+
      "WHERE { ?song a nmm:MusicPiece . "+
      "?song nie:title ?title . "+
      "?song nfo:duration ?length . "+
      "?song nie:url ?url ." +
      "?song nmm:performer ?aName . "+
      "?aName nmm:artistName ?artist . "+
      "?song nmm:musicAlbum ?malbum . "+
      "?malbum nmm:albumTitle ?album "+
      "} "

No comments:

Post a comment