(Add some implementation examples) |
Neverendingo (Talk | contribs) m (Text replace - "</code>" to "</syntaxhighlight>") |
||
| (3 intermediate revisions by 2 users not shown) | |||
| Line 1: | Line 1: | ||
| − | + | This tutorial informs about how to use Get Hot New Stuff collaborative data sharing features in your KDE application. | |
| + | Complementary to this tutorial, there is a [http://websvn.kde.org/*checkout*/trunk/KDE/kdelibs/knewstuff/doc/porting.txt porting guide] for KDE 3 developers and [http://websvn.kde.org/*checkout*/trunk/KDE/kdelibs/knewstuff/doc/tutorial.txt another tutorial] in SVN. | ||
| + | The [[Projects/KNS2/Users|KNS2-enabled application list]] is also a good reference to find similar applications. | ||
== Terminology == | == Terminology == | ||
| Line 9: | Line 11: | ||
=== First write a .knsrc file. === | === First write a .knsrc file. === | ||
A .knsrc file is just a file telling the library which options to use for a given application. It also lists where providers can be found, where uploads should be sent, etc. Anyway, the format of the file is an ini file with one group: | A .knsrc file is just a file telling the library which options to use for a given application. It also lists where providers can be found, where uploads should be sent, etc. Anyway, the format of the file is an ini file with one group: | ||
| − | < | + | <syntaxhighlight lang="text"> |
[KNewStuff2] | [KNewStuff2] | ||
ProvidersUrl= | ProvidersUrl= | ||
InstallationCommand= | InstallationCommand= | ||
Uncompress= | Uncompress= | ||
| − | </ | + | </syntaxhighlight> |
::'''NOTE''': InstallationCommand is optional, and if included will be invoked after each item is downloaded. | ::'''NOTE''': InstallationCommand is optional, and if included will be invoked after each item is downloaded. | ||
| Line 25: | Line 27: | ||
:One of the following is required to tell where downloads should go: | :One of the following is required to tell where downloads should go: | ||
| − | < | + | <syntaxhighlight lang="text"> |
TargetDir= | TargetDir= | ||
InstallPath= | InstallPath= | ||
StandardResource= | StandardResource= | ||
AbsoluteInstallPath= | AbsoluteInstallPath= | ||
| − | </ | + | </syntaxhighlight> |
:* TargetDir installs to KStandardDirs::locateLocal("data") + TargetDir + "/" | :* TargetDir installs to KStandardDirs::locateLocal("data") + TargetDir + "/" | ||
| Line 39: | Line 41: | ||
:And the following values are currently read, though not used yet: | :And the following values are currently read, though not used yet: | ||
| − | < | + | <syntaxhighlight lang="text"> |
CustomName= | CustomName= | ||
CachePolicy= | CachePolicy= | ||
| Line 45: | Line 47: | ||
SignaturePolicy= | SignaturePolicy= | ||
Scope= | Scope= | ||
| − | </ | + | </syntaxhighlight> |
:install the file using CMake install macro like this | :install the file using CMake install macro like this | ||
| − | < | + | <syntaxhighlight lang="text"> |
install( FILES yourdata.knsrc DESTINATION ${CONFIG_INSTALL_DIR} ) | install( FILES yourdata.knsrc DESTINATION ${CONFIG_INSTALL_DIR} ) | ||
| − | </ | + | </syntaxhighlight> |
:at this point, you can test your knsrc file with khotnewstuff4 like so: | :at this point, you can test your knsrc file with khotnewstuff4 like so: | ||
| − | < | + | <syntaxhighlight lang="text"> |
khotnewstuff4 yourdata.knsrc | khotnewstuff4 yourdata.knsrc | ||
| − | </ | + | </syntaxhighlight> |
:it should show you a download dialog of the data available on your provider(s). | :it should show you a download dialog of the data available on your provider(s). | ||
| Line 61: | Line 63: | ||
: Option 1. Use the static call. KNewStuff::Engine has a static method | : Option 1. Use the static call. KNewStuff::Engine has a static method | ||
| − | < | + | <syntaxhighlight lang="text"> |
static KNS::Entry::List download(); | static KNS::Entry::List download(); | ||
| − | </ | + | </syntaxhighlight> |
:if you use this method, your knsrc file must have the same name as your KGlobal::activeComponent().componentName(), it will create an Engine object, initialize it with the knsrc file, and call downloadDialogModal, then before returning it will copy the list of modified (installed, uninstalled, etc.) entries. Use is like this: | :if you use this method, your knsrc file must have the same name as your KGlobal::activeComponent().componentName(), it will create an Engine object, initialize it with the knsrc file, and call downloadDialogModal, then before returning it will copy the list of modified (installed, uninstalled, etc.) entries. Use is like this: | ||
| − | < | + | <syntaxhighlight lang="text"> |
KNS::Entry::List entries = KNS::Engine::download(); | KNS::Entry::List entries = KNS::Engine::download(); | ||
// list of changed entries | // list of changed entries | ||
| Line 78: | Line 80: | ||
} | } | ||
qDeleteAll(entries); | qDeleteAll(entries); | ||
| − | </ | + | </syntaxhighlight> |
:Taken from kdeedu/parley/src/parleydocument.cpp | :Taken from kdeedu/parley/src/parleydocument.cpp | ||
| Line 86: | Line 88: | ||
: Option 2. Allocate your own Engine object. This method is more flexible, in that you can name your knsrc file anything you want, since you call Engine::init yourself. You also don't have to worry about deleting the EntryList that is returned, since your Engine object owns it. This method is done like so: | : Option 2. Allocate your own Engine object. This method is more flexible, in that you can name your knsrc file anything you want, since you call Engine::init yourself. You also don't have to worry about deleting the EntryList that is returned, since your Engine object owns it. This method is done like so: | ||
| − | < | + | <syntaxhighlight lang="text"> |
KNS::Engine engine(0); | KNS::Engine engine(0); | ||
if (engine.init("wallpaper.knsrc")) { | if (engine.init("wallpaper.knsrc")) { | ||
| Line 98: | Line 100: | ||
} | } | ||
} | } | ||
| − | </ | + | </syntaxhighlight> |
:Taken from plasma/containments/desktop/backgrounddialog.cpp | :Taken from plasma/containments/desktop/backgrounddialog.cpp | ||
This tutorial informs about how to use Get Hot New Stuff collaborative data sharing features in your KDE application. Complementary to this tutorial, there is a porting guide for KDE 3 developers and another tutorial in SVN. The KNS2-enabled application list is also a good reference to find similar applications.
Contents |
First some basic terminology just to get us all on the same page. KHotNewStuff2 is the new library that implements the GHNS freedesktop.org specification for downloading and uploading user data. It will also support DXS (Desktop Exchange Service)
There are a many good examples for how to use khotnewstuff2 in the KDE-Edu module. Look at their source code for examples if needed, but it's basically a 2-part process to get download into your app.
A .knsrc file is just a file telling the library which options to use for a given application. It also lists where providers can be found, where uploads should be sent, etc. Anyway, the format of the file is an ini file with one group:
[KNewStuff2] ProvidersUrl= InstallationCommand= Uncompress=
TargetDir= InstallPath= StandardResource= AbsoluteInstallPath=
CustomName= CachePolicy= ChecksumPolicy= SignaturePolicy= Scope=
install( FILES yourdata.knsrc DESTINATION ${CONFIG_INSTALL_DIR} )khotnewstuff4 yourdata.knsrc
static KNS::Entry::List download();
KNS::Entry::List entries = KNS::Engine::download();
// list of changed entries
foreach(KNS::Entry* entry, entries) {
// care only about installed ones
if (entry->status() == KNS::Entry::Installed) {
// do something with the installed entries
}
}
}
qDeleteAll(entries);
KNS::Engine engine(0);
if (engine.init("wallpaper.knsrc")) {
KNS::Entry::List entries = engine.downloadDialogModal(this);
if (entries.size() > 0) {
// do something with the modified entries here if you want
// such as rescaning your data folder or whatnot
m_model->reload();
}
}