Porting Applications to KDE 4.0

Note

All modules should build from /trunk/KDE/kdelibs; there is no snapshot branch any longer. Major changes happen on Mondays (in any timezone) so expect rapid changes during that time. Things should be generally stable otherwise, but this is the development area, so there are no guarantees.

This document contains the changes you have to apply to programs written for KDE 3.x when you want to port them to KDE 4.0.

Table of Contents

Getting started

As a start you should have a look at doc/html/porting4.html in the Qt package, or this page online.

To start with the porting, you can run "qt3to4 -strict list_of_files" followed by kdesdk/scripts/qt4/adapt-to-qt4-api.pl and kdesdk/scripts/qt4/adapt-to-kde4-api.pl

Return to the Table of Contents

Global Changes

Return to the Table of Contents

Changes in kab

Return to the Table of Contents

Changes in kdefx

KPixmap

Return to the Table of Contents

Changes in kdecore

Header file names

KAboutData

See also the I18N section below.

KAccel

KAccelAction

KAccelBase

KApplication

KAudioPlayer

Removed, use now Phonon::AudioPlayer

KCatalogue

KCharsets

KCodecs

KCompletion

KConfigBackend

KSimpleConfig

KConfig

KConfigBase

KConfigGroupSaver

The class KConfigGroupSaver has been removed, instead use KConfigGroup. The difference is a KConfigGroupSaver object directly affects the config object it is acting on, while a KConfigGroup object only ensures the entries read or written through it are in the correct group.

Instead of:
KConfig *config = KGlobal::config();
KConfigGroupSaver saver(config, "group name");
  ...
config->readXXX("entry name");
should be rewritten as:
KConfigGroup cg = KGlobal::config()->group("group name");
  ...
cg.readXXX("entry name");

KCmdLineArgs

KCmdLineOptions

Now a proper class, instead of statically initialized struct. Use add() method for adding options (option syntax same as before); add() returns object reference, so calls can be chained to avoid repeating variable name all the way.

KAboutData

I18N_NOOPs in general replaced with KLocalizedString, produced by ki18n()/ki18nc() calls, or just KLocalizedString() if wanted empty.

KDesktopFile

KExtendedSocket

KFilterDev

KGlobal

KGlobalAccel

KGlobalSettings

KIcon

KIconLoader

KInstance

KIPC

KKey

KKeyNative

KKeySequence

KLibFactory

KLibLoader

KLocale

See also the I18N section below.

KMacroExpander

KMD5

KMimeSourceFactory

KNetwork::KIpAddress

KNetwork::KResolver

KNetwork::KSocketAddress and KNetwork::KInetSocketAddress

KNetwork::KUnixSocketAddress

KNetwork::K*Socket

KNotifyClient

KPalette

KPixmapSplitter

KProcess

KProcCtrl

Deprecated and renamed to K3ProcessController, header is k3processcontroller.h

KProcIO

Deprecated and renamed to K3ProcIO, header is k3procio.h

KRegExp

This class has been removed. Use QRegExp instead

KRFCDate

This class has been deprecated by KDateTime. It has been renamed K3RFCDate and moved to the libkde3support. The following method changes make porting easy:

KSaveFile

KShortcut

KShortcutDialog

KShortcutList and derivatives

KSortableValueList

KStdAccel

KStandardDirs

KStringHandler

KStyle

KTempDir

KTempFile

KURL

KURLDrag

KVMAllocator

KWin

This class has been merged with KWinModule into class KWindowSystem.

KWinModule

This class has been merged with KWinModule into class KWindowSystem. Don't create instances, use static functions and KWindowSystem::self() for connecting to signals.

KWin::WindowInfo

This class has been renamed KWindowInfo and moved to its own header: KWindowInfo or kwindowinfo.h. This class is not to be confused with KWindowInfo in KDE3 which was a small utility class to display message in the window icon and title. KWindowInfo represents the windowing system information for a give window, such as its geometry, desktop positioning, type, name, etc.

locate

NETRootInfo

Return to the Table of Contents

Changes in kdeui

KAboutContainer

KAboutContributor

KAboutDialog

KAboutWidget

KAction

Making KAction a subclass of QAction (actually QWidgetAction) brings KDE more into line with the Qt way of creating user interfaces, improves accessibility, and removes code duplication.

QAction in Qt4 is a true 1st class citizen - all QWidgets now have a list of associated actions, and QToolBar, QMenu etc. all use this list directly to show the graphical items (widgets, entries in the menu, etc). This has replaced all usage of integers as "id"s. There is also a new QEvent (QActionEvent), and the corresponding virtual protected function in QWidget, actionEvent().

KActionCollection

KActionSelector

KActionSeparator

KActiveLabel

KAnimWidget

KAuthIcon

KBugReport

KButtonBox

This class has been renamed to K3ButtonBox and moved to kde3support. Use KDialogButtonBox instead. Add the role parameter to the addButton function. Or check if it can't be achieved using KDialog.

KCharSelectTable

KCharSelect

KColorCells

KColorCollection

KColorDrag

KColorScheme

New class that provides access to system-wide, user configurable color roles. Like QPalette, the return type is now QBrush instead of QColor. As of 4.0 the brushes are solid colors, but this may change in the future.

KColorScheme

KComboBox

KCommand

KCommand has been renamed to K3Command and moved to kde3support. The header file was named k3command.h.

KCommandHistory

KCommandHistory has been renamed to K3CommandHistory and moved to kde3support. The header file was named k3command.h.

KConfigDialog

KContextMenuManager

KCursor

KDatePicker

KDateTable

KDateWidget

KDialog

KDialogBase

KDockWindow

KDualColorButton

KEdit

KEditListBox

KEditToolbar

KEditToolbarWidget

KFind

KFind

The constructors have been simplified to take an optional parent. Other parameters must be set by using the API directly, e.g. setHasSelection and setSearch

KFontChooser

KFontDialog

KGradientSelector

KGUIItem

KHistoryCombo

KIcon

KInputDialog

KIntNumInput, KDoubleNumInput, KDoubleSpinBox

KJanusWidget

KKeyButton

KKeyChooser

KKeyDialog

KLed

KLineEdit

KLineEditDlg

KListAction

KListBox

KMacroCommand

KMacroCommand has been renamed to K3MacroCommand and moved to kde3support. The header file was renamed k3command.h.

KMainWindow

KMainWindowInterface

KMessageBox

KNamedCommand

KNamedCommand has been renamed to K3NamedCommand and moved to kde3support. The header file was renamed k3command.h.

KPassivePopup

KPasswordEdit

KPasswordDialog

KPasswordDialog dlg(parent);
dlg.setPrompt( prompt );
dlg.setWindowTitle( caption );
if( dlg.exec() != QDialog::Accepted )
    return;
use( dlg.password() );

KPixmapIO

KPixmapRegionSelecorDialog

KPopupMenu

KPopupTitle

KProgress

KProgressDialog

KPushButton

KRadioAction

KRecentFilesAction

KRootPixmap

KSelectAction

KHSSelector

KSeparator

KSessionManaged

KSharedPixmap

KStatusBar

KStdAction

KStdGuiItem

KSyntaxHighlighter

KSystemTray

KTabBar

KTabCtl

KTextBrowser

KTextEdit

KTimeWidget

KTimezoneWidget

KToggleAction

KToggleFullScreenAction

KToolBar

KToolBarButton

KToolBarLabelAction

KToolBarRadioGroup

KToolBarSeparator

KUndoRedoAction

KUndoRedoAction has been renamed to K3UndoRedoAction and moved to kde3support. The header file was named k3command.h.

KURLLabel

KValueSelector

KWidgetAction

KWindowListMenu

KWizard

Return to the Table of Contents

Changes in kio

KArchive/KTar/KZip/KAr

KBookmark classes

KDEDesktopMimeType class

KDirLister class

KDiskFreeSp class

KDESasl class

KDirSelectDialog

KFileDialog,KDirSelectDialog,KUrlRequesterDlg,KCustomMenuEditor,KUrlBarItemDialog

KFileDialog, KFileView, KDirOperator

KFileDialog

KFileIconView, KFileDetailView, KFileTreeView, KFileTreeViewItem

KFileItemList

KFileMetaInfo

KFileMetaInfo has a new API and a new implementation. The change moves the emphasis away from mimetypes and groups of metadata to individual properties of files. These properties are still called KFileMetaInfoItem. The no longer belong to a group; they belong to the KFileMetaInfo object. The class KFileMetaInfoGroup is only there to group the items. Each KFileMetaInfoItem now has a member called PredicateProperties. This term comes from RDF. It determines the type of the item. This type encompasses the key (a URI), a name in the current locale, a description in the current locale, a type and a QValidator.

KFileOpenWithHandler

KFilePlugin

This class has been replaced by Strigi analyzers and KFileWritePlugin. The Strigi analyzer should be installed in the directory lib/strigi to be picked up. Introspection provided by the factories that are defined in the plugin helps in determining when a particular analyzer will be used.

Strigi analyzer have the advantage that they are faster then KFilePlugins. In addition, having one mechanism for extracting metadata from files saves code and more importantly ensures consistency between the display of metadata and the ablility to search in it. Another advantage is that the way the metadata are described matches well with the methods employed in the semantic desktop, in particular Nepomuk.

How to convert a KFilePlugin?

If your KFilePlugin only reads from files, it is easiest. Then you have to write only a Strigi analyzer. Depending on the way you read the data from the file it is best to write either a StreamThroughAnalyzer or a StreamEndAnalyzer. How to make the decision is explained in the link above. When your analyzer is ready you have to decide where to put it. You can put it in the same place as the KFilePlugin was or, if there are little or no dependencies in your analyzer, you can put it in kdesupport/strigi/src/streamindexer. The latter option has the advantage that you do not have to add the code for loading a plugin.

When you have write functionality in your plugin, you must implement KFileWritePlugin and place the code for writing the file in there. The details of how to do this are not entirely clear at the moment as is explained in the entry for KFileWritePlugin.

KFileWritePlugin

The write functionality of the class KFilePlugin is now handled by KFileWritePlugin. A global instance of this class that is attached to a KFileMetaInfoItem can be used to write back changes that were made to a files' metadata.

The mechanism by which these global instances are tied to KFileMetaDataItems is not yet finished. The implementation of will go into kio/kio/kfilemetainfo.cpp.

KIconButton

KIconDialog

KImageIO classes

KIO:: global methods

KIO::Job

KIO::UDSEntry

KIO::RenameDialog

KIO::RenameDialogPlugin

KIO::PasswordDialog

KIO::PreviewJob

KIO::SkipDialog

KIO::Observer

KIO::SlaveBase

KMimeMagic

KMimeType

KMimeTypeResolver

KOpenWithDlg

KPropertiesDialog

KPropertiesDialogPlugin

KProtocolInfo

KRun

KURLBar

KURLComboBox

KURLCompletion

KURLPixmapProvider

KURLRequester

KService

KServiceType

KServiceTypeProfile

KTrader

PasswordDialog

UIServer

Return to the Table of Contents

Changes in kparts

BrowserRun

ComponentFactory

Return to the Table of Contents

Changes in kded

Return to the Table of Contents

Changes in kspell

KSpell

Return to the Table of Contents

Changes in kdnssd

DomainBrowser

ServiceBase

ServiceBrowser

RemoteService

PublicService

Query

Return to the Table of Contents

API-cleanups in KHTML

Return to the Table of Contents

Changes in kfile

KUrlRequesterDialog

Return to the Table of Contents

Changes in KControl

Return to the Table of Contents

Panel Applets and Extensions

Return to the Table of Contents

libkmid

KScreensaver

libkscreensaver

Return to the Table of Contents

I18N

Messages.sh files

Instead of the "messages" target of the Makefile.am files, KDE4 uses now Bash scripts with Messages.sh file names. The Perl script branches/work/l10n-kde4/scripts/conversion/Makefile2Messages.pl can be used as an half-automatic conversion. (Please verify the result!)

rc.cpp file

The rc.cpp is now always generated. It is mandatory to have it as parameter of the $XGETTEXT if you are using the class KAboutData and if you are not explicitly setting KAboutData::setTranslator.

Gettext 0.16.1

Scripty uses xgettext 0.16.1 for extracting messages out of the source code. This is a huge step from the former xgettext 0.10.35-kde used in KDE3.

Important changes:

Translator Comments

Translator comments are a feature that already existed in KDE3, but which was seldom used.

// i18n: this is a test sentence
i18n("The quick brown fox jumps over the lazy dog");

i18n calls

The basic call, a message without arguments, context or plural, remains as is:

i18n("Just plain info");

If there are arguments, arg methods are no longer used to substitute the placeholders. Instead, arguments are added to the call:

i18n("%1 has scored %2", playerName, score);

Arguments can be of several integer types, double, QString, QChar (and perhaps more in the future).

Context call now has a different name, i18nc, and takes arguments in the same way:

i18nc("Player name - score", "%1 - %2", playerName, score);

Plural call is renamed to i18np and does away with %n placeholder, all being numbered instead:

i18np("One image in album %2", "%1 images in album %2", n, albumName);

The plural form is decided by first integer-valued argument.

There is one new call variant, the context-plural call:

i18ncp("Personal file", "One file", "%1 files", n);

i18n* calls are realized as templates, and so will take at most some finite number of arguments.

Notes on placeholder substitution:

Sometimes you might need old-style syntax for argument substitution. For example, when there are many arguments, or, more importantly, when it is convenient to defer substitution of arguments. This is possible using new ki18n call, with subs methods for argument substitution and toString method for finalization:

KLocalizedString ks = ki18n("The Foo-machine reported: %1");
case (fooErrCode) {
    ERR_OXI: ks.subs(i18n("Out of oxidizer")); break;
    ERR_HYD: ks.subs(i18n("Out of hydrazine")); break;
    ERR_PIL: ks.subs(i18n("Out of pilots")); break;
    default: ks.subs(i18n("Unknown catastrophe"));
}
QString scream = ks.toString();

Note that ki18n returns object of type KLocalizedString, hence the toString method for conversion to plain QString. The ki18n call should be used rarely; the previous example can as well be rewritten to:

QString problem;
case (fooErrCode) {
    ERR_OXI: problem = i18n("Out of oxidizer"); break;
    ...
}
QString scream = i18n("The Foo-machine reported: %1", problem);

Another case when you might want to use ki18n is if you want to format arguments. subs methods can take formatting arguments, similar to those of arg methods in QString. You should never use methods other than subs to format numbers in localized messages:

i18n("Rounds: %1", myNumberFormat(n, 8)); // bad, number not localized
ki18n("Rounds: %1").subs(n, 8).toString(); // good, number is localized

There is a context, plural and context-plural variant of ki18n as well:

ki18nc("No function", "None").toString();
ki18np("One file found", "%1 files found").subs(n).toString();
ki18ncp("Personal file", "One file", "%1 files").subs(n).toString();

If you need translation using locale (KLocale object) other than the default, you can use overloaded toString method which takes pointer to locale object (this replaces KLocale::translate(...) from KDE3):

KLocale *myLocale;
...
ki18n("Welcome").toString(myLocale);

There is a script for semi-automatic conversion of KDE 3 code, i18nk34conv.pl in branches/work/kde4-l10n/scripts/conversion/. It will convert most of the calls automatically, and mark the places that need manual review. There is also the script i18ncheckarg.pl in branches/work/kde4-l10n/scripts, which will check whether placeholders and arguments match. Use it to catch and correct deferred substitutions (like in the examples above). Both these scripts you can just run without any arguments in top directory of your sources (but you can also specify paths or filenames as arguments). Do not run conversion script twice on same sources!

Return to the Table of Contents

Changes in KSSL

KSSLCertDialog

KSSLInfoDialog

Return to the Table of Contents

Changes in mime types names

KDE 4 switched to the XDG mime types, thus the names for some mime types have changed.

The following list contains the renamings from the KDE mime types to the XDG ones.

Notes:

Old KDE mime type XDG mime type
application/chm application/x-chm
application/fits image/x-fits
application/java application/x-java
application/mathml+xml text/mathml
application/msexcel application/vnd.ms-excel (alias)
application/mspowerpoint application/vnd.ms-powerpoint (alias)
application/ms-tnef application/vnd.ms-tnef
application/pgp application/pgp-encrypted (alias)
application/vnd.ms-asf video/x-ms-asf
application/vnd.ms-word application/msword (alias)
application/vnd.palm application/x-palm-database (alias)
application/vnd.stardivision.writer-global application/vnd.stardivision.writer
application/vnd.sun.xml.base application/vnd.oasis.opendocument.database
application/vnd.sun.xml.writer.master application/vnd.sun.xml.writer.global
application/wordperfect application/vnd.wordperfect (alias)
application/x-7z application/x-7z-compressed
application/x-afm application/x-font-afm
application/x-applixgraphics image/x-applix-graphics
application/x-applixspread application/x-applix-spreadsheet
application/x-applixword application/x-applix-word
application/x-bz2dvi application/x-bzdvi
application/x-bzip2 application/x-bzip (alias)
application/x-dbase application/x-dbf (alias)
application/x-font-ghostscript application/x-font-type1
application/x-font-ttc application/x-font-ttf
application/x-gettext text/x-gettext-translation (*.po)
text/x-gettext-translation-template (*.pot)
application/x-hancomword application/x-hwp
application/x-iso application/x-cd-image
application/x-jar application/x-java-archive (alias)
application/x-javascript application/javascript (alias)
application/x-msaccess application/vnd.ms-access (alias)
application/x-msdos-program application/x-ms-dos-executable
application/x-msmetafile image/x-wmf
application/x-ogg application/ogg
application/x-perl-module application/x-perl
application/x-python text/x-python
application/x-rar-compressed application/x-rar
application/x-tbz application/x-bzip-compressed-tar
application/x-tgz application/x-compressed-tar
application/x-troff text/troff (alias)
application/x-zip application/zip
application/x-zip-compressed application/zip (alias)
application/xml-dtd text/x-dtd
audio/mpegurl audio/x-mpegurl (alias)
audio/x-midi audio/midi (alias)
audio/x-mp3 audio/mpeg (alias)
audio/x-oggflac audio/x-flac+ogg
audio/x-pn-realaudio audio/vnd.rn-realaudio (alias)
audio/x-speex audio/x-speex+ogg
audio/x-vorbis audio/x-vorbis+ogg
audio/vorbis audio/x-vorbis+ogg
image/fits image/x-fits
image/jp2 image/jpeg2000
image/jpg image/jpeg
image/pjpeg image/jpeg (alias)
image/svg-xml image/svg+xml
image/svg+xml image/svg+xml (*.svg)
image/svg+xml-compressed (*.svgz)
image/x-bmp image/bmp
image/x-djvu image/vnd.djvu (alias)
image/x-portable-greymap image/x-portable-graymap
image/x-raw image/x-dcraw (general mime type)

Specific mime types (all subclasses of image/x-dcraw):
image/x-panasonic-raw (*.raw)
image/x-kodak-dcr (*.dcr)
image/x-adobe-dng (*.dng)
image/x-canon-crw (*.crw *.cr2)
image/x-nikon-nef (*.nef)
image/x-minolta-mrw (*.mrw)
image/x-kodak-k25 (*.k25)
image/x-kodak-kdc (*.kdc)
image/x-olympus-orf (*.orf)
image/x-pentax-pef (*.pef)
image/x-fuji-raf (*.raf)
image/x-sony-sr2 (*.sr2)
image/x-sony-srf (*.srf)
image/x-sigma-x3f (*.x3f)
other extensions that were part of image/x-raw: image/x-kde-raw
image/x-targa image/x-tga
image/x-vnd.adobe.photoshop image/x-psd
image/x-xbm image/x-xbitmap
image/x-xcf-gimp image/x-xcf
image/x-xpm image/x-xpixmap
text/docbook application/docbook+xml
text/javascript application/javascript (alias)
text/rss application/rss+xml (alias)
text/rtf application/rtf
text/x-csv text/csv
text/x-diff text/x-patch
text/x-latex text/x-tex
text/xml application/xml (alias)
text/x-mswinurl application/x-mswinurl
text/x-vcalendar text/calendar (alias)
text/x-vcard text/directory (alias)
text/x-xslt application/xslt+xml
video/avi video/x-msvideo
video/x-ogm video/x-ogm+ogg
video/x-theora video/x-theora+ogg

Return to the Table of Contents

Changes in KNewStuff

General

KNewStuff is replaced with KNewStuff2. The API for applications has changed completely, as the library is a rewrite and not an update. Do not inherit from any class, use only KNS::Engine. All application-specific properties especially about what to do with downloaded files are configured in *.knsrc files; have a look at knewstuff2/doc/porting.txt and knewstuff2/doc/tutorial.txt.

Upload

Download

Return to the Table of Contents

DCOP

DCOP has been replaced with D-Bus.
Help with porting from DCOP to D-Bus can be found at http://techbase.kde.org/Development/Tutorials/Porting_to_D-Bus

Return to the Table of Contents