modernize: Prefer default member init over ctor init
[quassel.git] / src / uisupport / icon.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) version 3.                                           *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #include "icon.h"
22
23 #include <set>
24
25 #include <QDebug>
26
27 namespace icon {
28
29 namespace {
30
31 void printWarning(const QString &iconName, const QString &extra = {})
32 {
33     static std::set<QString> warnedAbout;
34     if (warnedAbout.insert(iconName).second) {
35         qWarning() << "Missing icon:" << iconName << qPrintable(extra);
36     }
37 }
38
39 }
40
41
42 QIcon get(const QString &iconName, const QString &fallbackPath)
43 {
44     return get(std::vector<QString>{iconName}, fallbackPath);
45 }
46
47
48 QIcon get(const std::vector<QString> &iconNames, const QString &fallbackPath)
49 {
50     for (auto &&iconName : iconNames) {
51         // Exact match
52         if (QIcon::hasThemeIcon(iconName)) {
53             return QIcon::fromTheme(iconName);
54         }
55     }
56
57     for (auto &&iconName : iconNames) {
58         // Try to get something from the theme anyway (i.e. a more generic fallback)
59         QIcon fallback = QIcon::fromTheme(iconName);
60         if (!fallback.availableSizes().isEmpty()) {
61             printWarning(iconName, QString{"(using fallback: \"%1\")"}.arg(fallback.name()));
62             return fallback;
63         }
64     }
65
66     // Build error string
67     QStringList requested;
68     for (auto &&iconName : iconNames) {
69         requested << iconName;
70     }
71     QString missing = "{" + requested.join(", ") + "}";
72
73     // Nothing from the theme, so try to load from path if given
74     if (!fallbackPath.isEmpty()) {
75         QIcon fallback{fallbackPath};
76         if (!fallback.availableSizes().isEmpty()) {
77             printWarning(missing, QString{"(using fallback: \"%1\")"}.arg(fallbackPath));
78             return fallback;
79         }
80     }
81
82     // Meh.
83     printWarning(missing);
84     return {};
85 }
86
87 }