File indexing completed on 2024-03-03 05:45:11

0001 /*
0002     SPDX-FileCopyrightText: 2021 Michail Vourlakos <mvourlakos@gmail.com>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "schemesmodel.h"
0007 
0008 // local
0009 #include "../../data/layoutdata.h"
0010 #include "../../layouts/importer.h"
0011 #include "../../wm/schemecolors.h"
0012 
0013 // Qt
0014 #include <QDebug>
0015 #include <QDir>
0016 
0017 // KDE
0018 #include <KLocalizedString>
0019 
0020 namespace Latte {
0021 namespace Settings {
0022 namespace Model {
0023 
0024 Schemes::Schemes(QObject *parent)
0025     : QAbstractListModel(parent)
0026 {
0027     initSchemes();
0028 }
0029 
0030 Schemes::~Schemes()
0031 {
0032     qDeleteAll(m_schemes);
0033 }
0034 
0035 int Schemes::rowCount(const QModelIndex &parent) const
0036 {
0037     if (parent.isValid()) {
0038         return 0;
0039     }
0040 
0041     return m_schemes.count();
0042 }
0043 
0044 void Schemes::initSchemes()
0045 {
0046     qDeleteAll(m_schemes);
0047     m_schemes.clear();
0048 
0049     QString currentSchemePath = WindowSystem::SchemeColors::possibleSchemeFile(Data::Layout::DEFAULTSCHEMEFILE);
0050     insertSchemeInList(currentSchemePath);
0051 
0052     QStringList standardPaths = Latte::Layouts::Importer::standardPathsFor("color-schemes");
0053 
0054     QStringList registeredSchemes;
0055 
0056     for(auto path : standardPaths) {
0057         QDir directory(path);
0058         QStringList tempSchemes = directory.entryList(QStringList() << "*.colors" << "*.COLORS", QDir::Files);
0059 
0060         foreach (QString filename, tempSchemes) {
0061             if (!registeredSchemes.contains(filename)) {
0062                 QString fullPath = path + "/" + filename;
0063                 insertSchemeInList(fullPath);
0064                 registeredSchemes << filename;
0065             }
0066         }
0067     }
0068 }
0069 
0070 void Schemes::insertSchemeInList(QString file)
0071 {
0072     WindowSystem::SchemeColors *tempScheme = new WindowSystem::SchemeColors(this, file);
0073 
0074     int atPos{0};
0075 
0076     for (int i = 0; i < m_schemes.count(); i++) {
0077         WindowSystem::SchemeColors *s = m_schemes[i];
0078 
0079         int result = QString::compare(tempScheme->schemeName(), s->schemeName(), Qt::CaseInsensitive);
0080 
0081         if (result < 0) {
0082             atPos = i;
0083             break;
0084         } else {
0085             atPos = i + 1;
0086         }
0087 
0088     }
0089 
0090     m_schemes.insert(atPos, tempScheme);
0091 }
0092 
0093 int Schemes::row(const QString &id)
0094 {
0095     if (id.isEmpty() || id == Data::Layout::DEFAULTSCHEMEFILE) {
0096         return 0;
0097     }
0098 
0099     for (int i = 0; i < m_schemes.count(); i++) {
0100         if (m_schemes[i]->schemeFile() == id) {
0101             return i;
0102         }
0103     }
0104 
0105     return -1;
0106 }
0107 
0108 QVariant Schemes::data(const QModelIndex &index, int role) const
0109 {
0110     if (!index.isValid() || index.column() != 0 || index.row() < 0 || index.row() >= m_schemes.count()) {
0111         return QVariant();
0112     }
0113 
0114     const WindowSystem::SchemeColors *d = m_schemes[index.row()];
0115 
0116     switch (role) {
0117     case IDROLE:
0118         return index.row() == 0 ? Data::Layout::DEFAULTSCHEMEFILE : d->schemeFile();
0119         break;
0120 
0121     case Qt::DisplayRole:
0122     case NAMEROLE:
0123         return index.row() == 0 ? i18n("System Colors") : d->schemeName();
0124         break;
0125 
0126     case TEXTCOLORROLE:
0127         return d->textColor();
0128         break;
0129 
0130     case BACKGROUNDCOLORROLE:
0131         return d->backgroundColor();
0132         break;
0133     }
0134 
0135     return QVariant();
0136 }
0137 
0138 }
0139 }
0140 }
0141