File indexing completed on 2024-03-24 15:43:20

0001 // Copyright (c) 2015 Pino Toscano <pino@kde.org>
0002 //
0003 // This library is free software; you can redistribute it and/or
0004 // modify it under the terms of the GNU Lesser General Public
0005 // License version 2.1 as published by the Free Software Foundation.
0006 //
0007 // This library is distributed in the hope that it will be useful,
0008 // but WITHOUT ANY WARRANTY; without even the implied warranty of
0009 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0010 // Lesser General Public License for more details.
0011 //
0012 // You should have received a copy of the GNU Lesser General Public License
0013 // along with this library; see the file COPYING.LIB.  If not, write to
0014 // the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0015 // Boston, MA 02110-1301, USA.
0016 
0017 #include "tokentheme.h"
0018 
0019 #include <QDir>
0020 #include <QDirIterator>
0021 #include <QFileInfo>
0022 #include <QSet>
0023 #include <QStandardPaths>
0024 
0025 #include <functional>
0026 
0027 static void iterateTokenThemes(std::function<void(const QString &, const QString &)> fun)
0028 {
0029     foreach (const QString &dir, QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QLatin1String("atlantik/themes/"), QStandardPaths::LocateDirectory))
0030     {
0031         QDirIterator it(dir, QDir::Dirs | QDir::NoDotAndDotDot);
0032         while (it.hasNext())
0033         {
0034             QString d = it.next();
0035             const QString fn = it.fileName();
0036             d += QLatin1String("/tokens/");
0037             if (QFileInfo(d).isDir())
0038                 fun(d, fn);
0039         }
0040     }
0041 }
0042 
0043 TokenTheme::TokenTheme()
0044 {
0045 }
0046 
0047 TokenTheme::TokenTheme(const QString &name, const QString &dir)
0048     : m_name(name)
0049     , m_dir(dir)
0050 {
0051 }
0052 
0053 TokenTheme::TokenTheme(const TokenTheme &other)
0054     : m_name(other.m_name)
0055     , m_dir(other.m_dir)
0056 {
0057 }
0058 
0059 TokenTheme::~TokenTheme()
0060 {
0061 }
0062 
0063 TokenTheme &TokenTheme::operator=(const TokenTheme &other)
0064 {
0065     m_name = other.m_name;
0066     m_dir = other.m_dir;
0067     return *this;
0068 }
0069 
0070 bool TokenTheme::isValid() const
0071 {
0072     return !m_dir.isEmpty();
0073 }
0074 
0075 QString TokenTheme::name() const
0076 {
0077     return m_name;
0078 }
0079 
0080 QString TokenTheme::path() const
0081 {
0082     return m_dir;
0083 }
0084 
0085 QString TokenTheme::fallbackIcon() const
0086 {
0087     return QStringLiteral("hamburger.png");
0088 }
0089 
0090 QString TokenTheme::tokenPath(const QString &name) const
0091 {
0092     if (!isValid())
0093         return QString();
0094 
0095     const QString filename = m_dir + name;
0096     if (!QFile::exists(filename))
0097         return QString();
0098 
0099     return filename;
0100 }
0101 
0102 QPixmap TokenTheme::tokenPixmap(const QString &name) const
0103 {
0104     const QString path = tokenPath(name);
0105     return path.isEmpty() ? QPixmap() : QPixmap(path);
0106 }
0107 
0108 QStringList TokenTheme::themeNames()
0109 {
0110     QSet<QString> set;
0111 
0112     iterateTokenThemes(
0113         [&set](const QString &, const QString &name)
0114         {
0115             set.insert(name);
0116         }
0117     );
0118 
0119     return set.values();
0120 }
0121 
0122 QVector<TokenTheme> TokenTheme::themes()
0123 {
0124     QVector<TokenTheme> list;
0125 
0126     iterateTokenThemes(
0127         [&list](const QString &dir, const QString &name)
0128         {
0129             list.append(TokenTheme(name, dir));
0130         }
0131     );
0132 
0133     return list;
0134 }
0135 
0136 TokenTheme TokenTheme::theme(const QString &theme)
0137 {
0138     TokenTheme newTheme;
0139 
0140     iterateTokenThemes(
0141         [&newTheme, theme](const QString &dir, const QString &name)
0142         {
0143             if (name == theme)
0144                 newTheme = TokenTheme(name, dir);
0145         }
0146     );
0147 
0148     return newTheme;
0149 }