Diffstat (limited to 'noncore/apps/tinykate/libkate/kateconfig.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/apps/tinykate/libkate/kateconfig.cpp | 219 |
1 files changed, 112 insertions, 107 deletions
diff --git a/noncore/apps/tinykate/libkate/kateconfig.cpp b/noncore/apps/tinykate/libkate/kateconfig.cpp index 96f91fb..782b629 100644 --- a/noncore/apps/tinykate/libkate/kateconfig.cpp +++ b/noncore/apps/tinykate/libkate/kateconfig.cpp @@ -15,44 +15,49 @@ ** ** Contact info@trolltech.com if any conditions of this licensing are ** not clear to you. ** **********************************************************************/ +#define QTOPIA_INTERNAL_LANGLIST +#include "kateconfig.h" + +/* OPIE */ +#include <opie2/odebug.h> +#include <qpe/global.h> + +/* QT */ #include <qdir.h> #include <qmessagebox.h> #if QT_VERSION <= 230 && defined(QT_NO_CODECS) #include <qtextcodec.h> #endif #include <qtextstream.h> +/* STD */ #include <sys/stat.h> #include <sys/types.h> #include <fcntl.h> #include <stdlib.h> #include <unistd.h> -#define QTOPIA_INTERNAL_LANGLIST -#include "kateconfig.h" -#include <qpe/global.h> - /*! \internal */ QString KateConfig::configFilename(const QString& name, Domain d) { switch (d) { - case File: - return name; - case User: { - QDir dir = (QString(getenv("HOME")) + "/Settings"); - if ( !dir.exists() ) - mkdir(dir.path().local8Bit(),0700); - return dir.path() + "/" + name + ".conf"; - } + case File: + return name; + case User: { + QDir dir = (QString(getenv("HOME")) + "/Settings"); + if ( !dir.exists() ) + mkdir(dir.path().local8Bit(),0700); + return dir.path() + "/" + name + ".conf"; + } } return name; } /*! \class KateConfig config.h @@ -90,24 +95,24 @@ QString KateConfig::configFilename(const QString& name, Domain d) In the File Domain, \a name is an absolute filename. */ KateConfig::KateConfig( const QString &name, Domain domain ) : filename( configFilename(name,domain) ) { - qWarning("KateConfig constructor\n"); + owarn << "KateConfig constructor\n" << oendl; git = groups.end(); read(); QStringList l = Global::languageList(); lang = l[0]; glang = l[1]; } // Sharp ROM compatibility KateConfig::KateConfig ( const QString &name, bool what ) - : filename( configFilename(name,what ? User : File) ) + : filename( configFilename(name,what ? User : File) ) { git = groups.end(); read(); QStringList l = Global::languageList(); lang = l[0]; glang = l[1]; @@ -115,24 +120,24 @@ KateConfig::KateConfig ( const QString &name, bool what ) /*! Writes any changes to disk and destroys the in-memory object. */ KateConfig::~KateConfig() { - qWarning("KateConfig destructor\n"); + owarn << "KateConfig destructor\n" << oendl; if ( changed ) - write(); + write(); } /*! Returns whether the current group has an entry called \a key. */ bool KateConfig::hasKey( const QString &key ) const { if ( groups.end() == git ) - return FALSE; + return FALSE; KateConfigGroup::ConstIterator it = ( *git ).find( key ); return it != ( *git ).end(); } /*! Sets the current group for subsequent reading and writing of @@ -144,15 +149,15 @@ bool KateConfig::hasKey( const QString &key ) const The \a gname must not be empty. */ void KateConfig::setGroup( const QString &gname ) { QMap< QString, KateConfigGroup>::Iterator it = groups.find( gname ); if ( it == groups.end() ) { - git = groups.insert( gname, KateConfigGroup() ); - changed = TRUE; - return; + git = groups.insert( gname, KateConfigGroup() ); + changed = TRUE; + return; } git = it; } /*! Writes a (\a key, \a value) entry to the current group. @@ -169,18 +174,18 @@ void KateConfig::writeEntry( const QString &key, const char* value ) \sa readEntry() */ void KateConfig::writeEntry( const QString &key, const QString &value ) { if ( git == groups.end() ) { - qWarning( "no group set" ); - return; + owarn << "no group set" << oendl; + return; } if ( (*git)[key] != value ) { - ( *git ).insert( key, value ); - changed = TRUE; + ( *git ).insert( key, value ); + changed = TRUE; } } /* Note that the degree of protection offered by the encryption here is only sufficient to avoid the most casual observation of the configuration @@ -193,32 +198,32 @@ void KateConfig::writeEntry( const QString &key, const QString &value ) static QString encipher(const QString& plain) { // mainly, we make it long QString cipher; int mix=28730492; for (int i=0; i<(int)plain.length(); i++) { - int u = plain[i].unicode(); - int c = u ^ mix; - QString x = QString::number(c,36); - cipher.append(QChar('a'+x.length())); - cipher.append(x); - mix *= u; + int u = plain[i].unicode(); + int c = u ^ mix; + QString x = QString::number(c,36); + cipher.append(QChar('a'+x.length())); + cipher.append(x); + mix *= u; } return cipher; } static QString decipher(const QString& cipher) { QString plain; int mix=28730492; for (int i=0; i<(int)cipher.length();) { - int l = cipher[i].unicode()-'a'; - QString x = cipher.mid(i+1,l); i+=l+1; - int u = x.toInt(0,36) ^ mix; - plain.append(QChar(u)); - mix *= u; + int l = cipher[i].unicode()-'a'; + QString x = cipher.mid(i+1,l); i+=l+1; + int u = x.toInt(0,36) ^ mix; + plain.append(QChar(u)); + mix *= u; } return plain; } /*! Writes an encrypted (\a key, \a value) entry to the current group. @@ -229,19 +234,19 @@ static QString decipher(const QString& cipher) \sa readEntry() */ void KateConfig::writeEntryCrypt( const QString &key, const QString &value ) { if ( git == groups.end() ) { - qWarning( "no group set" ); - return; + owarn << "no group set" << oendl; + return; } QString evalue = encipher(value); if ( (*git)[key] != evalue ) { - ( *git ).insert( key, evalue ); - changed = TRUE; + ( *git ).insert( key, evalue ); + changed = TRUE; } } /*! Writes a (\a key, \a num) entry to the current group. @@ -284,48 +289,48 @@ void KateConfig::writeEntry( const QString &key, bool b ) */ void KateConfig::writeEntry( const QString &key, const QStringList &lst, const QChar &sep ) { QString s; QStringList::ConstIterator it = lst.begin(); for ( ; it != lst.end(); ++it ) - s += *it + sep; + s += *it + sep; writeEntry( key, s ); } void KateConfig::writeEntry( const QString &key, const QColor &val ) { QStringList l; l.append( QString().setNum(val.red()) ); l.append( QString().setNum(val.green()) ); l.append( QString().setNum(val.blue()) ); - + writeEntry( key, l, QChar(',') ); } void KateConfig::writeEntry( const QString &key, const QFont &val ) { QStringList l; l.append( val.family() ); l.append( QString().setNum(val.pointSize()) ); l.append( QString().setNum(val.weight()) ); l.append( QString().setNum((int)val.italic()) ); l.append( QString().setNum((int)val.charSet()) ); - + writeEntry( key, l, QChar(',') ); } /*! Removes the \a key entry from the current group. Does nothing if there is no such entry. */ void KateConfig::removeEntry( const QString &key ) { if ( git == groups.end() ) { - qWarning( "no group set" ); - return; + owarn << "no group set" << oendl; + return; } ( *git ).remove( key ); changed = TRUE; } /*! @@ -351,17 +356,17 @@ void KateConfig::removeEntry( const QString &key ) For compatibility, non-const version. */ QString KateConfig::readEntry( const QString &key, const QString &deflt ) { QString res = readEntryDirect( key+"["+lang+"]" ); if ( !res.isNull() ) - return res; + return res; if ( !glang.isEmpty() ) { - res = readEntryDirect( key+"["+glang+"]" ); - if ( !res.isNull() ) - return res; + res = readEntryDirect( key+"["+glang+"]" ); + if ( !res.isNull() ) + return res; } return readEntryDirect( key, deflt ); } /*! \fn QString KateConfig::readEntryCrypt( const QString &key, const QString &deflt ) const @@ -374,17 +379,17 @@ QString KateConfig::readEntry( const QString &key, const QString &deflt ) For compatibility, non-const version. */ QString KateConfig::readEntryCrypt( const QString &key, const QString &deflt ) { QString res = readEntryDirect( key+"["+lang+"]" ); if ( res.isNull() && glang.isEmpty() ) - res = readEntryDirect( key+"["+glang+"]" ); + res = readEntryDirect( key+"["+glang+"]" ); if ( res.isNull() ) - res = readEntryDirect( key, QString::null ); + res = readEntryDirect( key, QString::null ); if ( res.isNull() ) - return deflt; + return deflt; return decipher(res); } /*! \fn QString KateConfig::readEntryDirect( const QString &key, const QString &deflt ) const \internal @@ -394,20 +399,20 @@ QString KateConfig::readEntryCrypt( const QString &key, const QString &deflt ) \internal For compatibility, non-const version. */ QString KateConfig::readEntryDirect( const QString &key, const QString &deflt ) { if ( git == groups.end() ) { - //qWarning( "no group set" ); - return deflt; + //owarn << "no group set" << oendl; + return deflt; } KateConfigGroup::ConstIterator it = ( *git ).find( key ); if ( it != ( *git ).end() ) - return *it; + return *it; else - return deflt; + return deflt; } /*! \fn int KateConfig::readNumEntry( const QString &key, int deflt ) const Reads a numeric entry stored with \a key, defaulting to \a deflt if there is no entry. */ @@ -417,15 +422,15 @@ QString KateConfig::readEntryDirect( const QString &key, const QString &deflt ) For compatibility, non-const version. */ int KateConfig::readNumEntry( const QString &key, int deflt ) { QString s = readEntry( key ); if ( s.isEmpty() ) - return deflt; + return deflt; else - return s.toInt(); + return s.toInt(); } /*! \fn bool KateConfig::readBoolEntry( const QString &key, bool deflt ) const Reads a bool entry stored with \a key, defaulting to \a deflt if there is no entry. */ @@ -435,15 +440,15 @@ int KateConfig::readNumEntry( const QString &key, int deflt ) For compatibility, non-const version. */ bool KateConfig::readBoolEntry( const QString &key, bool deflt ) { QString s = readEntry( key ); if ( s.isEmpty() ) - return deflt; + return deflt; else - return (bool)s.toInt(); + return (bool)s.toInt(); } /*! \fn QStringList KateConfig::readListEntry( const QString &key, const QChar &sep ) const Reads a string list entry stored with \a key, and with \a sep as the separator. */ @@ -453,15 +458,15 @@ bool KateConfig::readBoolEntry( const QString &key, bool deflt ) For compatibility, non-const version. */ QStringList KateConfig::readListEntry( const QString &key, const QChar &sep ) { QString s = readEntry( key ); if ( s.isEmpty() ) - return QStringList(); + return QStringList(); else - return QStringList::split( sep, s ); + return QStringList::split( sep, s ); } QColor KateConfig::readColorEntry( const QString &key, const QColor &def ) const { QStringList list = readListEntry(key, QChar(',')); if( list.count() != 3 ) @@ -481,13 +486,13 @@ QFont KateConfig::readFontEntry( const QString &key, const QFont &def ) const QValueList<int> KateConfig::readIntListEntry( const QString &key ) const { QString s = readEntry( key ); QValueList<int> il; if ( s.isEmpty() ) - return il; + return il; QStringList l = QStringList::split( QChar(','), s ); QStringList::Iterator l_it; for( l_it = l.begin(); l_it != l.end(); ++l_it ) il.append( (*l_it).toInt() ); @@ -497,67 +502,67 @@ QValueList<int> KateConfig::readIntListEntry( const QString &key ) const /*! Removes all entries from the current group. */ void KateConfig::clearGroup() { if ( git == groups.end() ) { - qWarning( "no group set" ); - return; + owarn << "no group set" << oendl; + return; } if ( !(*git).isEmpty() ) { - ( *git ).clear(); - changed = TRUE; + ( *git ).clear(); + changed = TRUE; } } /*! \internal */ void KateConfig::write( const QString &fn ) { QString strNewFile; if ( !fn.isEmpty() ) - filename = fn; + filename = fn; strNewFile = filename + ".new"; QFile f( strNewFile ); if ( !f.open( IO_WriteOnly|IO_Raw ) ) { - qWarning( "could not open for writing `%s'", strNewFile.latin1() ); - git = groups.end(); - return; + owarn << "could not open for writing `" << strNewFile << "'" << oendl; + git = groups.end(); + return; } - + QString str; - QCString cstr; + QCString cstr; QMap< QString, KateConfigGroup >::Iterator g_it = groups.begin(); for ( ; g_it != groups.end(); ++g_it ) { - str += "[" + g_it.key() + "]\n"; - KateConfigGroup::Iterator e_it = ( *g_it ).begin(); - for ( ; e_it != ( *g_it ).end(); ++e_it ) - str += e_it.key() + " = " + *e_it + "\n"; + str += "[" + g_it.key() + "]\n"; + KateConfigGroup::Iterator e_it = ( *g_it ).begin(); + for ( ; e_it != ( *g_it ).end(); ++e_it ) + str += e_it.key() + " = " + *e_it + "\n"; } cstr = str.utf8(); - + int total_length; total_length = f.writeBlock( cstr.data(), cstr.length() ); if ( total_length != int(cstr.length()) ) { - QMessageBox::critical( 0, QObject::tr("Out of Space"), - QObject::tr("There was a problem creating\nKateConfiguration Information \nfor this program.\n\nPlease free up some space and\ntry again.") ); - f.close(); - QFile::remove( strNewFile ); - return; + QMessageBox::critical( 0, QObject::tr("Out of Space"), + QObject::tr("There was a problem creating\nKateConfiguration Information \nfor this program.\n\nPlease free up some space and\ntry again.") ); + f.close(); + QFile::remove( strNewFile ); + return; } - + f.close(); // now rename the file... if ( rename( strNewFile, filename ) < 0 ) { - qWarning( "problem renaming the file %s to %s", strNewFile.latin1(), - filename.latin1() ); - QFile::remove( strNewFile ); - } + qWarning( "problem renaming the file %s to %s", strNewFile.latin1(), + filename.latin1() ); + QFile::remove( strNewFile ); + } } /*! Returns whether the KateConfig is in a valid state. */ bool KateConfig::isValid() const @@ -570,20 +575,20 @@ bool KateConfig::isValid() const */ void KateConfig::read() { changed = FALSE; if ( !QFileInfo( filename ).exists() ) { - git = groups.end(); - return; + git = groups.end(); + return; } QFile f( filename ); if ( !f.open( IO_ReadOnly ) ) { - git = groups.end(); - return; + git = groups.end(); + return; } QTextStream s( &f ); #if QT_VERSION <= 230 && defined(QT_NO_CODECS) // The below should work, but doesn't in Qt 2.3.0 s.setCodec( QTextCodec::codecForMib( 106 ) ); @@ -605,28 +610,28 @@ void KateConfig::read() /*! \internal */ bool KateConfig::parse( const QString &l ) { QString line = l.stripWhiteSpace(); - + if ( line [0] == QChar ( '#' )) - return true; // ignore comments - + return true; // ignore comments + if ( line[ 0 ] == QChar( '[' ) ) { - QString gname = line; - gname = gname.remove( 0, 1 ); - if ( gname[ (int)gname.length() - 1 ] == QChar( ']' ) ) - gname = gname.remove( gname.length() - 1, 1 ); - git = groups.insert( gname, KateConfigGroup() ); + QString gname = line; + gname = gname.remove( 0, 1 ); + if ( gname[ (int)gname.length() - 1 ] == QChar( ']' ) ) + gname = gname.remove( gname.length() - 1, 1 ); + git = groups.insert( gname, KateConfigGroup() ); } else if ( !line.isEmpty() ) { - if ( git == groups.end() ) - return FALSE; - int eq = line.find( '=' ); - if ( eq == -1 ) - return FALSE; - QString key = line.left(eq).stripWhiteSpace(); - QString value = line.mid(eq+1).stripWhiteSpace(); - ( *git ).insert( key, value ); + if ( git == groups.end() ) + return FALSE; + int eq = line.find( '=' ); + if ( eq == -1 ) + return FALSE; + QString key = line.left(eq).stripWhiteSpace(); + QString value = line.mid(eq+1).stripWhiteSpace(); + ( *git ).insert( key, value ); } return TRUE; } |