qa: Avoid deprecation warnings for QList/QSet conversions
[quassel.git] / src / uisupport / icon.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2019 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 }  // namespace
40
41 QIcon get(const QString& iconName, const QString& fallbackPath)
42 {
43     return get(std::vector<QString>{iconName}, fallbackPath);
44 }
45
46 QIcon get(const std::vector<QString>& iconNames, const QString& fallbackPath)
47 {
48     for (auto&& iconName : iconNames) {
49         // Exact match
50         if (QIcon::hasThemeIcon(iconName)) {
51             return QIcon::fromTheme(iconName);
52         }
53     }
54
55     for (auto&& iconName : iconNames) {
56         // Try to get something from the theme anyway (i.e. a more generic fallback)
57         QIcon fallback = QIcon::fromTheme(iconName);
58         if (!fallback.availableSizes().isEmpty()) {
59             printWarning(iconName, QString{"(using fallback: \"%1\")"}.arg(fallback.name()));
60             return fallback;
61         }
62     }
63
64     // Build error string
65     QStringList requested;
66     for (auto&& iconName : iconNames) {
67         requested << iconName;
68     }
69     QString missing = "{" + requested.join(", ") + "}";
70
71     // Nothing from the theme, so try to load from path if given
72     if (!fallbackPath.isEmpty()) {
73         QIcon fallback{fallbackPath};
74         if (!fallback.availableSizes().isEmpty()) {
75             printWarning(missing, QString{"(using fallback: \"%1\")"}.arg(fallbackPath));
76             return fallback;
77         }
78     }
79
80     // Meh.
81     printWarning(missing);
82     return {};
83 }
84
85 }  // namespace icon