=== modified file 'debian/changelog'
--- debian/changelog	2017-02-13 18:27:48 +0000
+++ debian/changelog	2017-02-16 22:56:28 +0000
@@ -1,3 +1,14 @@
+appmenu-qt5 (0.3.0+16.04.20151130-0ubuntu3) UNRELEASED; urgency=medium
+
+  * IconCache: get the proper theme path based on the fact we're using a
+    themed icon or not (LP: #1600136)
+  * IconCache: use $XDG_RUNTIME_DIR as preferred place where to save
+    icons
+  * AppMenuPlatformMenuBar: Don't initialize X11 related functions in
+    other environments (LP: #1606246)
+
+ -- Marco Trevisan (Treviño) <mail@3v1n0.net>  Thu, 16 Feb 2017 23:48:55 +0100
+
 appmenu-qt5 (0.3.0+16.04.20151130-0ubuntu2) xenial; urgency=medium
 
   * Fix for creating two or more system tray icons. (LP: #1573639, LP:

=== modified file 'src/appmenuplatformmenubar.cpp'
--- src/appmenuplatformmenubar.cpp	2014-12-13 15:38:51 +0000
+++ src/appmenuplatformmenubar.cpp	2017-02-16 22:56:28 +0000
@@ -229,6 +229,10 @@
 /* Helper function, as copy-pasted from Qt 5.2.1 gtk2 platformthemeplugin */
 static QString gtkSetting(const gchar *propertyName)
 {
+    if (qgetenv("DISPLAY").isEmpty()) {
+        return QString();
+    }
+
     GtkSettings *settings = gtk_settings_get_default();
     gchararray value;
     g_object_get(settings, propertyName, &value, NULL);
@@ -261,13 +265,19 @@
 GnomeAppMenuPlatformTheme::GnomeAppMenuPlatformTheme()
     : QGnomeTheme()
 {
-    int (*oldErrorHandler)(Display *, XErrorEvent *) = XSetErrorHandler(NULL);
-    gtk_init(0, 0);
-    XSetErrorHandler(oldErrorHandler);
+    if (!qgetenv("DISPLAY").isEmpty()) {
+        int (*oldErrorHandler)(Display *, XErrorEvent *) = XSetErrorHandler(NULL);
+        gtk_init(0, 0);
+        XSetErrorHandler(oldErrorHandler);
+    }
 }
 
 QVariant GnomeAppMenuPlatformTheme::themeHint(QPlatformTheme::ThemeHint hint) const
 {
+    if (qgetenv("DISPLAY").isEmpty()) {
+        return QVariant(QVariant::String);
+    }
+
     switch (hint) {
         case QPlatformTheme::SystemIconThemeName:
             return QVariant(gtkSetting("gtk-icon-theme-name"));

=== modified file 'src/appmenuplatformsystemtrayicon.cpp'
--- src/appmenuplatformsystemtrayicon.cpp	2016-05-11 14:54:49 +0000
+++ src/appmenuplatformsystemtrayicon.cpp	2017-02-16 22:56:28 +0000
@@ -180,7 +180,7 @@
 
 QString AppMenuPlatformSystemTrayIcon::iconThemePath() const
 {
-    return iconCache.themePath();
+    return iconCache.themePath(m_icon);
 }
 
 QString AppMenuPlatformSystemTrayIcon::iconName() const

=== modified file 'src/iconcache.cpp'
--- src/iconcache.cpp	2014-12-13 15:38:51 +0000
+++ src/iconcache.cpp	2017-02-16 22:56:28 +0000
@@ -29,8 +29,7 @@
 
 IconCache::IconCache(QObject *parent):
     QObject(parent),
-    m_temporaryDir(Q_NULLPTR),
-    m_initialized(false)
+    m_temporaryDir(Q_NULLPTR)
 {
 }
 
@@ -41,13 +40,48 @@
     }
 }
 
-QString IconCache::themePath()
+QString IconCache::themePath(const QIcon &icon)
 {
-    if (!m_initialized) {
-        QString path = QDir::tempPath() + QStringLiteral("/iconcache-XXXXXX");
+    if (!m_temporaryDir) {
+        QString dir;
+        QString runtimeDir = QString::fromUtf8(getenv("XDG_RUNTIME_DIR"));
+
+        if (!runtimeDir.isEmpty()) {
+            dir = runtimeDir;
+            QDir d(dir);
+            if (!d.exists()) {
+                d.mkpath(".");
+            }
+        } else if (!getenv("SNAP")) {
+            dir = QDir::tempPath();
+        } else {
+            // Try to get the .cache from $XDG_CACHE_HOME, if it's not set,
+            // it has to be in ~/.cache as per XDG standard
+            dir = QString::fromUtf8(getenv("XDG_CACHE_HOME"));
+            if (dir.isEmpty()) {
+                dir = QDir::cleanPath(QDir::homePath() + QStringLiteral("/.cache"));
+            }
+
+            QDir d(dir);
+            if (!d.exists()) {
+                d.mkpath(".");
+            }
+        }
+
+        QString path = dir + QStringLiteral("/qt-tray-iconcache-XXXXXX");
         m_temporaryDir = new QTemporaryDir(path);
-        m_initialized = true;
-    }
+    }
+
+    if (!icon.isNull() && !icon.name().isEmpty() && QIcon::hasThemeIcon(icon.name())) {
+        QString dataHome = QString::fromUtf8(getenv("XDG_DATA_HOME"));
+
+        if (dataHome.isEmpty()) {
+            dataHome = QDir::homePath() + "/.local/share";
+        }
+
+        return QDir::cleanPath(dataHome + "/icons");
+    }
+
     return m_temporaryDir->path();
 }
 

=== modified file 'src/iconcache.h'
--- src/iconcache.h	2014-12-13 15:38:51 +0000
+++ src/iconcache.h	2017-02-16 22:56:28 +0000
@@ -39,13 +39,12 @@
 
     static const int MaxIconCount;
 
-    QString themePath();
+    QString themePath(const QIcon &icon = QIcon());
     QString nameForIcon(const QIcon &icon);
 
 private:
     QTemporaryDir *m_temporaryDir;
     mutable QList<qint64> m_cacheKeys;
-    bool m_initialized;
 
     void cacheIcon(qint64 key, const QIcon &);
     void trimCache();

