Changeset View
Changeset View
Standalone View
Standalone View
src/kreadconfig/kreadconfig.cpp
Show First 20 Lines • Show All 52 Lines • ▼ Show 20 Line(s) | 52 | { | |||
---|---|---|---|---|---|
53 | QCoreApplication app(argc, argv); | 53 | QCoreApplication app(argc, argv); | ||
54 | 54 | | |||
55 | QCommandLineParser parser; | 55 | QCommandLineParser parser; | ||
56 | parser.addOption(QCommandLineOption(QStringLiteral("file"), QCoreApplication::translate("main", "Use <file> instead of global config"), QStringLiteral("file"))); | 56 | parser.addOption(QCommandLineOption(QStringLiteral("file"), QCoreApplication::translate("main", "Use <file> instead of global config"), QStringLiteral("file"))); | ||
57 | parser.addOption(QCommandLineOption(QStringLiteral("group"), QCoreApplication::translate("main", "Group to look in. Use repeatedly for nested groups."), QStringLiteral("group"), QStringLiteral("KDE"))); | 57 | parser.addOption(QCommandLineOption(QStringLiteral("group"), QCoreApplication::translate("main", "Group to look in. Use repeatedly for nested groups."), QStringLiteral("group"), QStringLiteral("KDE"))); | ||
58 | parser.addOption(QCommandLineOption(QStringLiteral("key"), QCoreApplication::translate("main", "Key to look for"), QStringLiteral("key"))); | 58 | parser.addOption(QCommandLineOption(QStringLiteral("key"), QCoreApplication::translate("main", "Key to look for"), QStringLiteral("key"))); | ||
59 | parser.addOption(QCommandLineOption(QStringLiteral("default"), QCoreApplication::translate("main", "Default value"), QStringLiteral("value"))); | 59 | parser.addOption(QCommandLineOption(QStringLiteral("default"), QCoreApplication::translate("main", "Default value"), QStringLiteral("value"))); | ||
60 | parser.addOption(QCommandLineOption(QStringLiteral("type"), QCoreApplication::translate("main", "Type of variable"), QStringLiteral("type"))); | 60 | parser.addOption(QCommandLineOption(QStringLiteral("type"), QCoreApplication::translate("main", "Type of variable"), QStringLiteral("type"))); | ||
61 | parser.addOption(QCommandLineOption(QStringLiteral("ignore-globals"), QCoreApplication::translate("main", "Ignore global files"))); | ||||
62 | parser.addOption(QCommandLineOption(QStringLiteral("ignore-cascading"), QCoreApplication::translate("main", "Ignore configuration file cascading"))); | ||||
63 | parser.addHelpOption(); | ||||
61 | 64 | | |||
62 | parser.process(app); | 65 | parser.process(app); | ||
63 | 66 | | |||
64 | QStringList groups=parser.values(QStringLiteral("group")); | 67 | QStringList groups=parser.values(QStringLiteral("group")); | ||
65 | QString key=parser.value(QStringLiteral("key")); | 68 | QString key=parser.value(QStringLiteral("key")); | ||
66 | QString file=parser.value(QStringLiteral("file")); | 69 | QString file=parser.value(QStringLiteral("file")); | ||
67 | QString dflt=parser.value(QStringLiteral("default")); | 70 | QString dflt=parser.value(QStringLiteral("default")); | ||
68 | QString type=parser.value(QStringLiteral("type")).toLower(); | 71 | QString type=parser.value(QStringLiteral("type")).toLower(); | ||
69 | 72 | | |||
70 | if (key.isNull() || !parser.positionalArguments().isEmpty()) { | 73 | if (key.isNull() || !parser.positionalArguments().isEmpty()) { | ||
71 | parser.showHelp(1); | 74 | parser.showHelp(1); | ||
72 | } | 75 | } | ||
73 | 76 | | |||
74 | KSharedConfig::openConfig(); | 77 | KConfig::OpenFlags flags = KConfig::FullConfig; | ||
78 | if (parser.isSet(QLatin1String("ignore-globals"))) | ||||
79 | flags &= ~KConfig::IncludeGlobals; | ||||
80 | if (parser.isSet(QLatin1String("ignore-cascading"))) | ||||
81 | flags &= ~KConfig::CascadeConfig; | ||||
82 | | ||||
83 | KSharedConfig::Ptr konfig = KSharedConfig::openConfig(file, flags); | ||||
75 | 84 | | |||
76 | KConfig *konfig; | | |||
77 | bool configMustDeleted = false; | | |||
78 | if (file.isEmpty()) | | |||
79 | konfig = KSharedConfig::openConfig().data(); | | |||
80 | else | | |||
81 | { | | |||
82 | konfig = new KConfig( file, KConfig::NoGlobals ); | | |||
83 | configMustDeleted=true; | | |||
84 | } | | |||
85 | KConfigGroup cfgGroup = konfig->group(QString()); | 85 | KConfigGroup cfgGroup = konfig->group(QString()); | ||
86 | foreach (const QString &grp, groups) | 86 | foreach (const QString &grp, groups) | ||
87 | cfgGroup = cfgGroup.group(grp); | 87 | cfgGroup = cfgGroup.group(grp); | ||
88 | if(type==QStringLiteral("bool")) { | 88 | if(type==QStringLiteral("bool")) { | ||
89 | dflt=dflt.toLower(); | 89 | dflt=dflt.toLower(); | ||
90 | bool def=(dflt==QStringLiteral("true") || dflt==QStringLiteral("on") || dflt==QStringLiteral("yes") || dflt==QStringLiteral("1")); | 90 | bool def=(dflt==QStringLiteral("true") || dflt==QStringLiteral("on") || dflt==QStringLiteral("yes") || dflt==QStringLiteral("1")); | ||
91 | bool retValue = !cfgGroup.readEntry(key, def); | 91 | bool retValue = !cfgGroup.readEntry(key, def); | ||
92 | if ( configMustDeleted ) | | |||
93 | delete konfig; | | |||
94 | return retValue; | 92 | return retValue; | ||
95 | } else if((type==QStringLiteral("num")) || (type==QStringLiteral("int"))) { | 93 | } else if((type==QStringLiteral("num")) || (type==QStringLiteral("int"))) { | ||
96 | int retValue = cfgGroup.readEntry(key, dflt.toInt()); | 94 | int retValue = cfgGroup.readEntry(key, dflt.toInt()); | ||
97 | if ( configMustDeleted ) | | |||
98 | delete konfig; | | |||
99 | return retValue; | 95 | return retValue; | ||
100 | } else if (type==QStringLiteral("path")){ | 96 | } else if (type==QStringLiteral("path")){ | ||
101 | fprintf(stdout, "%s\n", cfgGroup.readPathEntry(key, dflt).toLocal8Bit().data()); | 97 | fprintf(stdout, "%s\n", cfgGroup.readPathEntry(key, dflt).toLocal8Bit().data()); | ||
102 | if ( configMustDeleted ) | | |||
103 | delete konfig; | | |||
104 | return 0; | 98 | return 0; | ||
105 | } else { | 99 | } else { | ||
106 | /* Assume it's a string... */ | 100 | /* Assume it's a string... */ | ||
107 | fprintf(stdout, "%s\n", cfgGroup.readEntry(key, dflt).toLocal8Bit().data()); | 101 | fprintf(stdout, "%s\n", cfgGroup.readEntry(key, dflt).toLocal8Bit().data()); | ||
108 | if ( configMustDeleted ) | | |||
109 | delete konfig; | | |||
110 | return 0; | 102 | return 0; | ||
111 | } | 103 | } | ||
112 | } | 104 | } | ||
113 | 105 | |