Add QGuiApplication::setDesktopFileName()

This property might be set by applications whose desktop entry file name
cannot be determined by heuristics already in place.

It is particularly useful for QtWayland as it can be used to determine
the app_id simply by stripping the ".desktop" suffix from this property.
Without a correct app_id, Wayland compositors won't be able to e.g.
show the application icon on task managers.

This property is also very interesting for X11 as there are various
desktop environments trying to map windows to launchers.
It will be possible to export desktopFileName as a xproperty, making
such mapping less error prone.

Change-Id: I0fef23f28f383639e625379ab46e36aecb338ac4
Reviewed-by: Martin Gräßlin <mgraesslin@kde.org>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: David Faure <david.faure@kdab.com>
bb10
Pier Luigi Fiorini 2015-08-13 16:37:07 +02:00
parent 3b1cbc4753
commit 61ad604ad4
4 changed files with 52 additions and 0 deletions

View File

@ -143,6 +143,7 @@ QIcon *QGuiApplicationPrivate::app_icon = 0;
QString *QGuiApplicationPrivate::platform_name = 0;
QString *QGuiApplicationPrivate::displayName = 0;
QString *QGuiApplicationPrivate::desktopFileName = 0;
QPalette *QGuiApplicationPrivate::app_pal = 0; // default application palette
@ -604,6 +605,8 @@ QGuiApplication::~QGuiApplication()
QGuiApplicationPrivate::platform_name = 0;
delete QGuiApplicationPrivate::displayName;
QGuiApplicationPrivate::displayName = 0;
delete QGuiApplicationPrivate::desktopFileName;
QGuiApplicationPrivate::desktopFileName = 0;
}
QGuiApplicationPrivate::QGuiApplicationPrivate(int &argc, char **argv, int flags)
@ -644,6 +647,34 @@ QString QGuiApplication::applicationDisplayName()
return QGuiApplicationPrivate::displayName ? *QGuiApplicationPrivate::displayName : applicationName();
}
/*!
\property QGuiApplication::desktopFileName
\brief the base name of the desktop entry for this application
\since 5.7
This is the file name, without the full path, of the desktop entry
that represents this application according to the freedesktop desktop
entry specification.
This property gives a precise indication of what desktop entry represents
the application and it is needed by the windowing system to retrieve
such information without resorting to imprecise heuristics.
The latest version of the freedesktop desktop entry specification can be obtained
\l{http://standards.freedesktop.org/desktop-entry-spec/latest/}{here}.
*/
void QGuiApplication::setDesktopFileName(const QString &name)
{
if (!QGuiApplicationPrivate::desktopFileName)
QGuiApplicationPrivate::desktopFileName = new QString;
*QGuiApplicationPrivate::desktopFileName = name;
}
QString QGuiApplication::desktopFileName()
{
return QGuiApplicationPrivate::desktopFileName ? *QGuiApplicationPrivate::desktopFileName : QString();
}
/*!
Returns the most recently shown modal window. If no modal windows are
visible, this function returns zero.

View File

@ -67,6 +67,7 @@ class Q_GUI_EXPORT QGuiApplication : public QCoreApplication
Q_OBJECT
Q_PROPERTY(QIcon windowIcon READ windowIcon WRITE setWindowIcon)
Q_PROPERTY(QString applicationDisplayName READ applicationDisplayName WRITE setApplicationDisplayName)
Q_PROPERTY(QString desktopFileName READ desktopFileName WRITE setDesktopFileName)
Q_PROPERTY(Qt::LayoutDirection layoutDirection READ layoutDirection WRITE setLayoutDirection NOTIFY layoutDirectionChanged)
Q_PROPERTY(QString platformName READ platformName STORED false)
Q_PROPERTY(bool quitOnLastWindowClosed READ quitOnLastWindowClosed WRITE setQuitOnLastWindowClosed)
@ -82,6 +83,9 @@ public:
static void setApplicationDisplayName(const QString &name);
static QString applicationDisplayName();
static void setDesktopFileName(const QString &name);
static QString desktopFileName();
static QWindowList allWindows();
static QWindowList topLevelWindows();
static QWindow *topLevelAt(const QPoint &pos);

View File

@ -183,6 +183,7 @@ public:
static QIcon *app_icon;
static QString *platform_name;
static QString *displayName;
static QString *desktopFileName;
QWindowList modalWindowList;
static void showModalWindow(QWindow *window);

View File

@ -62,6 +62,7 @@ class tst_QGuiApplication: public tst_QCoreApplication
private slots:
void cleanup();
void displayName();
void desktopFileName();
void firstWindowTitle();
void windowIcon();
void focusObject();
@ -101,6 +102,21 @@ void tst_QGuiApplication::displayName()
QCOMPARE(QGuiApplication::applicationDisplayName(), QString::fromLatin1("The GUI Application"));
}
void tst_QGuiApplication::desktopFileName()
{
int argc = 1;
char *argv[] = { const_cast<char*>("tst_qguiapplication") };
QGuiApplication app(argc, argv);
QCOMPARE(QGuiApplication::desktopFileName(), QString());
QGuiApplication::setDesktopFileName("io.qt.QGuiApplication.desktop");
QCOMPARE(QGuiApplication::desktopFileName(), QString::fromLatin1("io.qt.QGuiApplication.desktop"));
QGuiApplication::setDesktopFileName(QString());
QCOMPARE(QGuiApplication::desktopFileName(), QString());
}
void tst_QGuiApplication::firstWindowTitle()
{
int argc = 3;