File indexing completed on 2024-04-28 04:44:17
0001 /* 0002 SnoreNotify is a Notification Framework based on Qt 0003 Copyright (C) 2015 Hannah von Reth <vonreth@kde.org> 0004 0005 SnoreNotify is free software: you can redistribute it and/or modify 0006 it under the terms of the GNU Lesser General Public License as published by 0007 the Free Software Foundation, either version 3 of the License, or 0008 (at your option) any later version. 0009 0010 SnoreNotify is distributed in the hope that it will be useful, 0011 but WITHOUT ANY WARRANTY; without even the implied warranty of 0012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0013 GNU Lesser General Public License for more details. 0014 0015 You should have received a copy of the GNU Lesser General Public License 0016 along with SnoreNotify. If not, see <http://www.gnu.org/licenses/>. 0017 */ 0018 #include "libsnore/version.h" 0019 #include "libsnore/snoreglobals.h" 0020 #include "libsnore/utils.h" 0021 0022 #include "libsnore/snore_static_plugins.h" 0023 0024 #include "settingsutils.h" 0025 0026 #include <QGuiApplication> 0027 #include <QCommandLineParser> 0028 #include <QTimer> 0029 0030 #include <iostream> 0031 0032 using namespace Snore; 0033 using namespace std; 0034 0035 static void listApps() 0036 { 0037 foreach(const QString & app, SettingsUtils::knownApps()) { 0038 cout << qPrintable(app) << endl; 0039 } 0040 } 0041 0042 static bool setSetting(const QString &appName, SettingsType type, const QString &_key, const QString &value) 0043 { 0044 QSettings &settings = SettingsUtils::settings(); 0045 QString key = Utils::normalizeSettingsKey(_key, type, appName); 0046 QVariant oldValue = settings.value(key); 0047 0048 //TODO: make sure that the values are valid qvariant.canConvert doesn't work. 0049 if (!oldValue.isValid()) { 0050 cout << "Invalid key: " << qPrintable(key) << endl; 0051 return false; 0052 } 0053 settings.setValue(key, value); 0054 cout << "Set: " << qPrintable(key) << " to " << qPrintable(settings.value(key).toString()) << endl; 0055 return true; 0056 } 0057 0058 static void listSettings(SettingsType type, const QString &application) 0059 { 0060 QSettings &settings = SettingsUtils::settings(); 0061 auto getAllKeys = [](QSettings & settings) { 0062 return settings.allKeys(); 0063 }; 0064 0065 QString prefix = application; 0066 if (application == QLatin1String("global")) { 0067 prefix = QString(); 0068 } 0069 cout << qPrintable(application) << endl; 0070 foreach(const QString & key, SettingsUtils::allSettingsKeysWithPrefix( 0071 Utils::normalizeSettingsKey(QLatin1String(""), type, prefix), settings, getAllKeys)) { 0072 cout << " " << qPrintable(key) << ": " << qPrintable(settings.value(Utils::normalizeSettingsKey(key, type, prefix)).toString()) << endl; 0073 } 0074 } 0075 0076 int main(int argc, char *argv[]) 0077 { 0078 QGuiApplication app(argc, argv); 0079 0080 app.setApplicationName(QStringLiteral("SnoreSettings-cli")); 0081 app.setOrganizationName(QStringLiteral("SnoreNotify")); 0082 app.setApplicationVersion(Snore::Version::version()); 0083 0084 QCommandLineParser parser; 0085 parser.setApplicationDescription(QStringLiteral("A settings interface for Snorenotify.")); 0086 parser.addHelpOption(); 0087 parser.addVersionOption(); 0088 0089 //TODO: merge with appNameCommand ? 0090 QCommandLineOption listAppsCommand(QStringLiteral("apps"), QStringLiteral("List possible application.")); 0091 parser.addOption(listAppsCommand); 0092 0093 QCommandLineOption listSettingsCommand({QStringLiteral("l"), QStringLiteral("list") } , QStringLiteral("List settings for the given --appName or the global settings.")); 0094 parser.addOption(listSettingsCommand); 0095 0096 QCommandLineOption appNameCommand({QStringLiteral("a"), QStringLiteral("appName") } , QStringLiteral("Set the Name of the app <app> or global."), QStringLiteral("app"), QStringLiteral("global")); 0097 parser.addOption(appNameCommand); 0098 0099 parser.addPositionalArgument(QStringLiteral("key"), QStringLiteral("The settings Key.")); 0100 parser.addPositionalArgument(QStringLiteral("value"), QStringLiteral("The new settings Value")); 0101 0102 parser.process(app); 0103 0104 QString appName = parser.value(appNameCommand); 0105 0106 SettingsType type = GlobalSetting; 0107 if (appName != QLatin1String("global")) { 0108 type = LocalSetting; 0109 } 0110 0111 if (parser.isSet(listAppsCommand)) { 0112 listApps(); 0113 } else if (parser.isSet(listSettingsCommand)) { 0114 listSettings(type, appName); 0115 } else { 0116 QStringList posArgs = parser.positionalArguments(); 0117 if (posArgs.size() != 2) { 0118 parser.showHelp(1); 0119 } 0120 if (!setSetting(appName, type, posArgs.at(0), posArgs.at(1))) { 0121 return 1; 0122 } 0123 } 0124 app.processEvents(); 0125 return 0; 0126 }