Merge lp:~3v1n0/sni-qt/icons-user-cache-on-snap into lp:sni-qt

Proposed by Marco Trevisan (Treviño)
Status: Merged
Approved by: Lukáš Tinkl
Approved revision: 109
Merged at revision: 103
Proposed branch: lp:~3v1n0/sni-qt/icons-user-cache-on-snap
Merge into: lp:sni-qt
Prerequisite: lp:~3v1n0/sni-qt/x11-usertime-on-activate
Diff against target: 149 lines (+57/-10)
5 files modified
src/fsutils.cpp (+16/-4)
src/iconcache.cpp (+11/-2)
src/iconcache.h (+2/-2)
src/statusnotifieritem.cpp (+1/-1)
tests/auto/iconcachetest.cpp (+27/-1)
To merge this branch: bzr merge lp:~3v1n0/sni-qt/icons-user-cache-on-snap
Reviewer Review Type Date Requested Status
Lukáš Tinkl (community) Approve
Review via email: mp+311033@code.launchpad.net

Commit message

IconCache: get the proper theme path based on the fact we're using a themed icon or not

If an app uses QIcon::fromTheme then we only have the icon name of it,
and thus we need to return a valid icon theme path. Now, it's not possible
to return the whole list of dirs, so we just fallback to the home icons
older, so that users might in case add icons there.

In $SNAP world, when using desktop-launcher ~/.local/share/icons also
contains all the themes in from /usr/share/icons and potentially more
(depending on the wrapper used for launching) so, we can be quite flexible
with it.

Also when running in a snap, save icons in $XDG_CACHE_HOME or in the user
cache, but still in an indicator readable location

Description of the change

To post a comment you must log in.
Revision history for this message
Lukáš Tinkl (lukas-kde) wrote :

You don't have to use QDir::separator(), it's even discouraged (http://doc.qt.io/qt-5/qdir.html#separator)

review: Needs Fixing
Revision history for this message
Lukáš Tinkl (lukas-kde) wrote :

Inline comment about the way you find the .cache directories

review: Needs Fixing
107. By Marco Trevisan (Treviño)

Don't use deprecated QDir::separator()

108. By Marco Trevisan (Treviño)

fsutils: add some comments about how we get the .cache

Revision history for this message
Marco Trevisan (Treviño) (3v1n0) wrote :

Lukáš Tinkl (lukas-kde):
> Trevinho, I still have a slight doubt about ~/.cache; what if it doesn't exist? what creates it?

The subsequent call to dir.mkpath(".").

109. By Marco Trevisan (Treviño)

IconCache: try to get $XDG_DATA_HOME as base icon folders

Revision history for this message
Lukáš Tinkl (lukas-kde) wrote :

LGTM, working fine

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'src/fsutils.cpp'
--- src/fsutils.cpp 2011-10-14 21:41:08 +0000
+++ src/fsutils.cpp 2016-11-16 23:16:14 +0000
@@ -32,16 +32,28 @@
3232
33QString generateTempDir(const QString& prefix)33QString generateTempDir(const QString& prefix)
34{34{
35 QDir dir = QDir::temp();35 QDir dir;
36
37 if (!getenv("SNAP")) {
38 dir = QDir::temp();
39 } else {
40 // Try to get the .cache from $XDG_CACHE_HOME, if it's not set,
41 // it has to be in ~/.cache as per XDG standard
42 QString path = QString::fromUtf8(getenv("XDG_CACHE_HOME"));
43 if (path.isEmpty()) {
44 path = QDir::cleanPath(QDir::homePath() + "/.cache");
45 }
46
47 dir.setPath(path);
48 }
49
36 if (!dir.mkpath(".")) {50 if (!dir.mkpath(".")) {
37 qCritical("Failed to generate temporary file for prefix %s: could not create %s",51 qCritical("Failed to generate temporary file for prefix %s: could not create %s",
38 qPrintable(prefix), qPrintable(dir.path()));52 qPrintable(prefix), qPrintable(dir.path()));
39 return QString();53 return QString();
40 }54 }
4155
42 QString tmpl = QString("%1/%2-XXXXXX")56 QString tmpl = QDir::cleanPath(dir.path() + '/' + prefix + "-XXXXXX");
43 .arg(dir.path())
44 .arg(prefix);
45 QByteArray ba = QFile::encodeName(tmpl);57 QByteArray ba = QFile::encodeName(tmpl);
46 const char* name = mkdtemp(ba.data());58 const char* name = mkdtemp(ba.data());
47 if (!name) {59 if (!name) {
4860
=== modified file 'src/iconcache.cpp'
--- src/iconcache.cpp 2011-10-10 16:37:00 +0000
+++ src/iconcache.cpp 2016-11-16 23:16:14 +0000
@@ -26,7 +26,6 @@
26#include <QDateTime>26#include <QDateTime>
27#include <QDebug>27#include <QDebug>
28#include <QDir>28#include <QDir>
29#include <QIcon>
30#include <QList>29#include <QList>
3130
32const int IconCache::MaxIconCount = 20;31const int IconCache::MaxIconCount = 20;
@@ -86,8 +85,18 @@
86 }85 }
87}86}
8887
89QString IconCache::themePath() const88QString IconCache::themePath(const QIcon& icon) const
90{89{
90 if (!icon.isNull() && !icon.name().isEmpty() && QIcon::hasThemeIcon(icon.name())) {
91 QString dataHome = QString::fromUtf8(getenv("XDG_DATA_HOME"));
92
93 if (dataHome.isEmpty()) {
94 dataHome = QDir::homePath() + "/.local/share";
95 }
96
97 return QDir::cleanPath(dataHome + "/icons");
98 }
99
91 return m_themePath;100 return m_themePath;
92}101}
93102
94103
=== modified file 'src/iconcache.h'
--- src/iconcache.h 2011-10-10 16:37:00 +0000
+++ src/iconcache.h 2016-11-16 23:16:14 +0000
@@ -19,6 +19,7 @@
1919
20// Qt20// Qt
21#include <QObject>21#include <QObject>
22#include <QIcon>
22#include <QStringList>23#include <QStringList>
2324
24class QIcon;25class QIcon;
@@ -35,8 +36,7 @@
3536
36 static const int MaxIconCount;37 static const int MaxIconCount;
3738
38 QString themePath() const;39 QString themePath(const QIcon& icon = QIcon()) const;
39
40 QString nameForIcon(const QIcon& icon) const;40 QString nameForIcon(const QIcon& icon) const;
4141
42 // Internal, testing only42 // Internal, testing only
4343
=== modified file 'src/statusnotifieritem.cpp'
--- src/statusnotifieritem.cpp 2016-11-16 23:16:14 +0000
+++ src/statusnotifieritem.cpp 2016-11-16 23:16:14 +0000
@@ -204,7 +204,7 @@
204204
205QString StatusNotifierItem::iconThemePath() const205QString StatusNotifierItem::iconThemePath() const
206{206{
207 return m_iconCache->themePath();207 return m_iconCache->themePath(trayIcon->icon());
208}208}
209209
210QString StatusNotifierItem::iconName() const210QString StatusNotifierItem::iconName() const
211211
=== modified file 'tests/auto/iconcachetest.cpp'
--- tests/auto/iconcachetest.cpp 2011-10-10 16:37:00 +0000
+++ tests/auto/iconcachetest.cpp 2016-11-16 23:16:14 +0000
@@ -73,6 +73,32 @@
73 QVERIFY(info.isDir());73 QVERIFY(info.isDir());
74 }74 }
7575
76 void testThemePathForIcon()
77 {
78 QList<int> sizes = QList<int>() << 16 << 22 << 32;
79 QIcon icon = createTestIcon(sizes, Qt::red);
80
81 IconCache cache(m_sandBoxDirName);
82 QString themePath = cache.themePath(icon);
83 QCOMPARE(themePath, m_sandBoxDirName + "/icons");
84
85 QFileInfo info(themePath);
86 QVERIFY(info.isDir());
87 }
88
89 void testThemePathForThemedIcons()
90 {
91 IconCache cache(m_sandBoxDirName);
92 QIcon icon = QIcon::fromTheme("indicator-messages");
93 QString themePath = cache.themePath(icon);
94
95 if (icon.name().isEmpty()) {
96 QSKIP("Icon has not been found in theme, so there's nothing to test here", SkipSingle);
97 }
98
99 QCOMPARE(themePath, QDir::homePath() + "/.local/share/icons");
100 }
101
76 void testPixmapIcon()102 void testPixmapIcon()
77 {103 {
78 IconCache cache(m_sandBoxDirName);104 IconCache cache(m_sandBoxDirName);
@@ -82,7 +108,7 @@
82108
83 QString name = cache.nameForIcon(icon);109 QString name = cache.nameForIcon(icon);
84 Q_FOREACH(int size, sizes) {110 Q_FOREACH(int size, sizes) {
85 QString dirName = cache.themePath() + QString("/hicolor/%1x%1/apps").arg(size);111 QString dirName = cache.themePath(icon) + QString("/hicolor/%1x%1/apps").arg(size);
86 QVERIFY(QFile::exists(dirName));112 QVERIFY(QFile::exists(dirName));
87 QImage image;113 QImage image;
88 QVERIFY(image.load(dirName + "/" + name + ".png"));114 QVERIFY(image.load(dirName + "/" + name + ".png"));

Subscribers

People subscribed via source and target branches