Development/Tutorials/K Hot New Stuff2: Difference between revisions

From KDE TechBase
No edit summary
(Mark for archiving)
 
(10 intermediate revisions by 4 users not shown)
Line 1: Line 1:
The page that used to be here was all KDE3 specific information, so I figured it was about time to start a new one.
{{Archived}}
 
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 7: Line 11:
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.
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.


:1. 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:
=== First write a .knsrc file. ===
<code>
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=
</code>
</syntaxhighlight>
 
::'''NOTE''': InstallationCommand is optional, and if included will be invoked after each item is downloaded.
::'''NOTE''': Uncompress is optional, and if included will try to uncompress all downloads into the target folder according to the mime-type of the file.
:* Uncompress possible values as of r794016 are
:* "always" - always attempt to uncompress (old values of "true" are seen as "always"
:* "archive" - uncompress if it's an archive but copy otherwise
:* "never" - (default) never attempt to uncompress


:'''NOTE''': InstallationCommand is optional, and if included will be invoked after each item is downloaded.
:'''NOTE''': Uncompress is optional, and if included will try to uncompress all downloads into the target folder according to the mime-type of the file.


One of the following to tell where downloads should go:
:One of the following is required to tell where downloads should go:
<code>
<syntaxhighlight lang="text">
TargetDir=
TargetDir=
InstallPath=
InstallPath=
StandardResource=
StandardResource=
</code>
AbsoluteInstallPath=
 
</syntaxhighlight>
TargetDir installs to KStandardDirs::locateLocal("data") + TargetDir + "/"


StandardResource installs to KStandardDirs::locateLocal(StandardResource)
:* TargetDir installs to KStandardDirs::locateLocal("data") + TargetDir + "/"
:* StandardResource installs to KStandardDirs::locateLocal(StandardResource)
:* InstallPath installs to QDir::home().path() + "/" + InstallPath
:* AbsoluteInstallPath installs to AbsoluteInstallPath + "/"
::'''NOTE''': AbsoluteInstallPath is not portable (/boot/grub is probably not a valid path except on linux), so should not be used in anything that is meant to run on all our platforms.


InstallPath installs to QDir::home().path() + "/" + InstallPath
:And the following values are currently read, though not used yet:
 
<syntaxhighlight lang="text">
And the following values are currently read, though not used yet:
<code>
CustomName=
CustomName=
CachePolicy=
CachePolicy=
Line 38: Line 49:
SignaturePolicy=
SignaturePolicy=
Scope=
Scope=
</code>
</syntaxhighlight>
:install the file using CMake install macro like this
<syntaxhighlight lang="text">
install( FILES yourdata.knsrc  DESTINATION  ${CONFIG_INSTALL_DIR} )
</syntaxhighlight>
:at this point, you can test your knsrc file with khotnewstuff4 like so:
<syntaxhighlight lang="text">
khotnewstuff4 yourdata.knsrc
</syntaxhighlight>
:it should show you a download dialog of the data available on your provider(s).


install the file using CMake install macro like this
=== Make your application launch the KNS ui with your knsrc file. ===
 
:There are many examples of using KNS in code, kde-edu apps, kdegames apps, plasma, some kcm modules, etc.  Basically there are two options at this time for invoking the download dialog.
 
: Option 1. Use the static call. KNewStuff::Engine has a static method
<syntaxhighlight lang="text">
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:
 
<syntaxhighlight lang="text">
    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);
</syntaxhighlight>
:Taken from kdeedu/parley/src/parleydocument.cpp
 
::'''NOTE''': Since the Ently::List is a copy of entries from the engine that has now been deleted, you are responsible for cleanup of the allocated Entry(s).


<code>
install( FILES yourdata.knsrc  DESTINATION  ${CONFIG_INSTALL_DIR} )
</code>


at this point, you can test your knsrc file with khotnewstuff4 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:


<code>
<syntaxhighlight lang="text">
khotnewstuff4 yourdata.knsrc
    KNS::Engine engine(0);
</code>
    if (engine.init("wallpaper.knsrc")) {
        KNS::Entry::List entries = engine.downloadDialogModal(this);


it should show you a download dialog of the data available on your provider(s).
        if (entries.size() > 0) {
    // do something with the modified entries here if you want


:2. Make your application launch the KNS ui with your knsrc file.
            // such as rescaning your data folder or whatnot
            m_model->reload();
        }
    }
</syntaxhighlight>
:Taken from plasma/containments/desktop/backgrounddialog.cpp

Latest revision as of 13:22, 31 May 2019


This page has been archived
The information on this page is outdated or no longer in use but is kept for historical purposes. Please see the Category:Archives for similar pages.

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.

Terminology

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)

Use it in your application

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.

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:

[KNewStuff2]
ProvidersUrl=
InstallationCommand=
Uncompress=
NOTE: InstallationCommand is optional, and if included will be invoked after each item is downloaded.
NOTE: Uncompress is optional, and if included will try to uncompress all downloads into the target folder according to the mime-type of the file.
  • Uncompress possible values as of r794016 are
  • "always" - always attempt to uncompress (old values of "true" are seen as "always"
  • "archive" - uncompress if it's an archive but copy otherwise
  • "never" - (default) never attempt to uncompress


One of the following is required to tell where downloads should go:
TargetDir=
InstallPath=
StandardResource=
AbsoluteInstallPath=
  • TargetDir installs to KStandardDirs::locateLocal("data") + TargetDir + "/"
  • StandardResource installs to KStandardDirs::locateLocal(StandardResource)
  • InstallPath installs to QDir::home().path() + "/" + InstallPath
  • AbsoluteInstallPath installs to AbsoluteInstallPath + "/"
NOTE: AbsoluteInstallPath is not portable (/boot/grub is probably not a valid path except on linux), so should not be used in anything that is meant to run on all our platforms.
And the following values are currently read, though not used yet:
CustomName=
CachePolicy=
ChecksumPolicy=
SignaturePolicy=
Scope=
install the file using CMake install macro like this
install( FILES yourdata.knsrc  DESTINATION  ${CONFIG_INSTALL_DIR} )
at this point, you can test your knsrc file with khotnewstuff4 like so:
khotnewstuff4 yourdata.knsrc
it should show you a download dialog of the data available on your provider(s).

Make your application launch the KNS ui with your knsrc file.

There are many examples of using KNS in code, kde-edu apps, kdegames apps, plasma, some kcm modules, etc. Basically there are two options at this time for invoking the download dialog.
Option 1. Use the static call. KNewStuff::Engine has a static method
static KNS::Entry::List download();
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:
    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);
Taken from kdeedu/parley/src/parleydocument.cpp
NOTE: Since the Ently::List is a copy of entries from the engine that has now been deleted, you are responsible for cleanup of the allocated Entry(s).


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:
    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();
        }
    }
Taken from plasma/containments/desktop/backgrounddialog.cpp