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 environmentYou can do this by opening your project in QtCreator and
- Click on SailfishOS on the side menu
- Under Targets click Manage Target on your desired environment
- In the search area, type in sparql
- Install all shown packages
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
https://github.com/nemomobile/libqtsparql/tree/master/src/sparql
Also in there is a bunch of examples which are fairly handy
https://github.com/nemomobile/libqtsparql/tree/master/examples/sparql
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 querySparqlListModel {
id: queryModel
objectName: queryModel
// create a new SparqlConnection for the queryModel
connection: SparqlConnection { id:sparqlConnection;
objectName:"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