File indexing completed on 2025-01-05 04:54:23
0001 /* 0002 SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "knoteprintselectthemecombobox.h" 0008 #include "knotesglobalconfig.h" 0009 0010 #include <KConfig> 0011 #include <KConfigGroup> 0012 #include <QDirIterator> 0013 #include <QStandardPaths> 0014 0015 KNotePrintSelectThemeComboBox::KNotePrintSelectThemeComboBox(QWidget *parent) 0016 : QComboBox(parent) 0017 { 0018 loadThemes(); 0019 } 0020 0021 KNotePrintSelectThemeComboBox::~KNotePrintSelectThemeComboBox() = default; 0022 0023 void KNotePrintSelectThemeComboBox::loadThemes() 0024 { 0025 clear(); 0026 const QString defaultTheme = KNotesGlobalConfig::self()->theme(); 0027 0028 const QString relativePath = QStringLiteral("knotes/print/themes/"); 0029 QStringList themesDirectories = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, relativePath, QStandardPaths::LocateDirectory); 0030 if (themesDirectories.count() < 2) { 0031 // Make sure to add local directory 0032 const QString localDirectory = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1Char('/') + relativePath; 0033 if (!themesDirectories.contains(localDirectory)) { 0034 themesDirectories.append(localDirectory); 0035 } 0036 } 0037 0038 for (const QString &directory : std::as_const(themesDirectories)) { 0039 QDirIterator dirIt(directory, QStringList(), QDir::AllDirs | QDir::NoDotAndDotDot); 0040 QStringList alreadyLoadedThemeName; 0041 while (dirIt.hasNext()) { 0042 dirIt.next(); 0043 const QString themeInfoFile = dirIt.filePath() + QLatin1StringView("/theme.desktop"); 0044 KConfig config(themeInfoFile); 0045 KConfigGroup group(&config, QStringLiteral("Desktop Entry")); 0046 QString name = group.readEntry("Name", QString()); 0047 if (name.isEmpty()) { 0048 continue; 0049 } 0050 if (alreadyLoadedThemeName.contains(name)) { 0051 int i = 2; 0052 const QString originalName(name); 0053 while (alreadyLoadedThemeName.contains(name)) { 0054 name = originalName + QStringLiteral(" (%1)").arg(i); 0055 ++i; 0056 } 0057 } 0058 const QString printThemePath(dirIt.filePath() + QLatin1Char('/')); 0059 if (!printThemePath.isEmpty()) { 0060 alreadyLoadedThemeName << name; 0061 addItem(name, printThemePath); 0062 } 0063 } 0064 } 0065 model()->sort(0); 0066 const int index = findData(defaultTheme); 0067 setCurrentIndex(index == -1 ? 0 : index); 0068 } 0069 0070 QString KNotePrintSelectThemeComboBox::selectedTheme() const 0071 { 0072 return itemData(currentIndex()).toString(); 0073 } 0074 0075 void KNotePrintSelectThemeComboBox::selectDefaultTheme() 0076 { 0077 const bool bUseDefaults = KNotesGlobalConfig::self()->useDefaults(true); 0078 const QString defaultTheme = KNotesGlobalConfig::self()->theme(); 0079 const int index = findData(defaultTheme); 0080 setCurrentIndex(index == -1 ? 0 : index); 0081 KNotesGlobalConfig::self()->useDefaults(bUseDefaults); 0082 } 0083 0084 #include "moc_knoteprintselectthemecombobox.cpp"