diff options
Diffstat (limited to 'kreadconfig')
-rw-r--r-- | kreadconfig/CMakeLists.txt | 37 | ||||
-rw-r--r-- | kreadconfig/Makefile.am | 13 | ||||
-rw-r--r-- | kreadconfig/kreadconfig.cpp | 105 | ||||
-rw-r--r-- | kreadconfig/kwriteconfig.cpp | 75 |
4 files changed, 230 insertions, 0 deletions
diff --git a/kreadconfig/CMakeLists.txt b/kreadconfig/CMakeLists.txt new file mode 100644 index 000000000..a8b2a668b --- /dev/null +++ b/kreadconfig/CMakeLists.txt @@ -0,0 +1,37 @@ +################################################# +# +# (C) 2010-2011 Serghei Amelian +# serghei (DOT) amelian (AT) gmail.com +# +# Improvements and feedback are welcome +# +# This file is released under GPL >= 2 +# +################################################# + +include_directories( + ${TDE_INCLUDE_DIR} + ${TQT_INCLUDE_DIRS} +) + +link_directories( + ${TQT_LIBRARY_DIRS} +) + + +##### kreadconfig (executable) ################## + +tde_add_executable( kreadconfig + SOURCES kreadconfig.cpp + LINK tdecore-shared + DESTINATION ${BIN_INSTALL_DIR} +) + + +##### kwriteconfig (executable) ################# + +tde_add_executable( kwriteconfig + SOURCES kwriteconfig.cpp + LINK tdecore-shared + DESTINATION ${BIN_INSTALL_DIR} +) diff --git a/kreadconfig/Makefile.am b/kreadconfig/Makefile.am new file mode 100644 index 000000000..0a4e56935 --- /dev/null +++ b/kreadconfig/Makefile.am @@ -0,0 +1,13 @@ +AM_CPPFLAGS = -DQT_NO_CAST_ASCII + +INCLUDES = $(all_includes) +AM_LDFLAGS = $(all_libraries) $(KDE_RPATH) $(LIB_QT) -lDCOP $(LIB_TDECORE) $(LIB_TDEUI) -ltdefx $(LIB_TDEIO) -ltdetexteditor +LDADD = $(LIB_TDECORE) + +bin_PROGRAMS = kreadconfig kwriteconfig +kreadconfig_SOURCES = kreadconfig.cpp +kwriteconfig_SOURCES = kwriteconfig.cpp + +messages: + $(XGETTEXT) $(kreadconfig_SOURCES) -o $(podir)/kreadconfig.pot + $(XGETTEXT) $(kwriteconfig_SOURCES) -o $(podir)/kwriteconfig.pot diff --git a/kreadconfig/kreadconfig.cpp b/kreadconfig/kreadconfig.cpp new file mode 100644 index 000000000..08e3dd430 --- /dev/null +++ b/kreadconfig/kreadconfig.cpp @@ -0,0 +1,105 @@ +/* Read TDEConfig() entries - for use in shell scripts. + * (c) 2001 Red Hat, Inc. + * Programmed by Bernhard Rosenkraenzer <bero@redhat.com> + * + * If --type is specified as bool, the return value is 0 if the value + * is set, 1 if it isn't set. There is no output. + * + * If --type is specified as num, the return value matches the value + * of the key. There is no output. + * + * If --type is not set, the value of the key is simply printed to stdout. + * + * Usage examples: + * if kreadconfig --group TDE --key macStyle --type bool; then + * echo "We're using Mac-Style menus." + * else + * echo "We're using normal menus." + * fi + * + * TRASH=`kreadconfig --group Paths --key Trash` + * if test -n "$TRASH"; then + * mv someFile "$TRASH" + * else + * rm someFile + * fi + */ +#include <tdeconfig.h> +#include <tdeglobal.h> +#include <tdeapplication.h> +#include <tdecmdlineargs.h> +#include <tdelocale.h> +#include <tdeaboutdata.h> +#include <stdio.h> + +static TDECmdLineOptions options[] = +{ + { "file <file>", I18N_NOOP("Use <file> instead of global config"), 0 }, + { "group <group>", I18N_NOOP("Group to look in"), "TDE" }, + { "key <key>", I18N_NOOP("Key to look for"), 0 }, + { "default <default>", I18N_NOOP("Default value"), 0 }, + { "type <type>", I18N_NOOP("Type of variable"), 0 }, + TDECmdLineLastOption +}; +int main(int argc, char **argv) +{ + TDEAboutData aboutData("kreadconfig", I18N_NOOP("KReadConfig"), + "1.0.1", + I18N_NOOP("Read TDEConfig entries - for use in shell scripts"), + TDEAboutData::License_GPL, + "(c) 2001 Red Hat, Inc."); + aboutData.addAuthor("Bernhard Rosenkraenzer", 0, "bero@redhat.com"); + TDECmdLineArgs::init(argc, argv, &aboutData); + TDECmdLineArgs::addCmdLineOptions(options); + TDECmdLineArgs *args=TDECmdLineArgs::parsedArgs(); + + TQString group=TQString::fromLocal8Bit(args->getOption("group")); + TQString key=TQString::fromLocal8Bit(args->getOption("key")); + TQString file=TQString::fromLocal8Bit(args->getOption("file")); + TQCString dflt=args->getOption("default"); + TQCString type=args->getOption("type").lower(); + + if (key.isNull()) { + TDECmdLineArgs::usage(); + return 1; + } + + TDEInstance inst(&aboutData); + TDEGlobal::config(); + + TDEConfig *konfig; + bool configMustDeleted = false; + if (file.isEmpty()) + konfig = TDEGlobal::config(); + else + { + konfig = new TDEConfig(file, true, false); + configMustDeleted=true; + } + konfig->setGroup(group); + if(type=="bool") { + dflt=dflt.lower(); + bool def=(dflt=="true" || dflt=="on" || dflt=="yes" || dflt=="1"); + bool retValue = !konfig->readBoolEntry(key, def); + if ( configMustDeleted ) + delete konfig; + return retValue; + } else if((type=="num") || (type=="int")) { + long retValue = konfig->readLongNumEntry(key, dflt.toLong()); + if ( configMustDeleted ) + delete konfig; + return retValue; + } else if (type=="path"){ + fprintf(stdout, "%s\n", konfig->readPathEntry(key, dflt).local8Bit().data()); + if ( configMustDeleted ) + delete konfig; + return 0; + } else { + /* Assume it's a string... */ + fprintf(stdout, "%s\n", konfig->readEntry(key, dflt).local8Bit().data()); + if ( configMustDeleted ) + delete konfig; + return 0; + } +} + diff --git a/kreadconfig/kwriteconfig.cpp b/kreadconfig/kwriteconfig.cpp new file mode 100644 index 000000000..5d705641d --- /dev/null +++ b/kreadconfig/kwriteconfig.cpp @@ -0,0 +1,75 @@ +/* Write TDEConfig() entries - for use in shell scripts. + * (c) 2001 Red Hat, Inc. & Lu�s Pedro Coelho + * Programmed by Lu�s Pedro Coelho <luis_pedro@netcabo.pt> + * based on kreadconfig by Bernhard Rosenkraenzer <bero@redhat.com> + * + * License: GPL + * + */ +#include <tdeconfig.h> +#include <tdeglobal.h> +#include <tdeapplication.h> +#include <tdecmdlineargs.h> +#include <tdelocale.h> +#include <tdeaboutdata.h> +#include <stdio.h> + +static TDECmdLineOptions options[] = +{ + { "file <file>", I18N_NOOP("Use <file> instead of global config"), 0 }, + { "group <group>", I18N_NOOP("Group to look in"), "TDE" }, + { "key <key>", I18N_NOOP("Key to look for"), 0 }, + { "type <type>", I18N_NOOP("Type of variable. Use \"bool\" for a boolean, otherwise it is treated as a string"), 0 }, + { "+value", I18N_NOOP( "The value to write. Mandatory, on a shell use '' for empty" ), 0 }, + TDECmdLineLastOption +}; +int main(int argc, char **argv) +{ + TDEAboutData aboutData("kwriteconfig", I18N_NOOP("KWriteConfig"), + "1.0.0", + I18N_NOOP("Write TDEConfig entries - for use in shell scripts"), + TDEAboutData::License_GPL, + "(c) 2001 Red Hat, Inc. & Lu�s Pedro Coelho"); + aboutData.addAuthor("Lu�s Pedro Coelho", 0, "luis_pedro@netcabo.pt"); + aboutData.addAuthor("Bernhard Rosenkraenzer", "Wrote kreadconfig on which this is based", "bero@redhat.com"); + TDECmdLineArgs::init(argc, argv, &aboutData); + TDECmdLineArgs::addCmdLineOptions(options); + TDECmdLineArgs *args=TDECmdLineArgs::parsedArgs(); + + TQString group=TQString::fromLocal8Bit(args->getOption("group")); + TQString key=TQString::fromLocal8Bit(args->getOption("key")); + TQString file=TQString::fromLocal8Bit(args->getOption("file")); + TQCString type=args->getOption("type").lower(); + + + if (key.isNull() || !args->count()) { + TDECmdLineArgs::usage(); + return 1; + } + TQCString value = args->arg( 0 ); + + TDEInstance inst(&aboutData); + + TDEConfig *konfig; + if (file.isEmpty()) + konfig = new TDEConfig(TQString::fromLatin1("kdeglobals"), false, false); + else + konfig = new TDEConfig(file, false, false); + + konfig->setGroup(group); + if ( konfig->getConfigState() != TDEConfig::ReadWrite || konfig->entryIsImmutable( key ) ) return 2; + + if(type=="bool") { + // For symmetry with kreadconfig we accept a wider range of values as true than Qt + bool boolvalue=(value=="true" || value=="on" || value=="yes" || value=="1"); + konfig->writeEntry( key, boolvalue ); + } else if (type=="path") { + konfig->writePathEntry( key, TQString::fromLocal8Bit( value ) ); + } else { + konfig->writeEntry( key, TQString(TQString::fromLocal8Bit( value )) ); + } + konfig->sync(); + delete konfig; + return 0; +} + |