-rw-r--r-- | qmake/tools/qsettings.cpp | 365 |
1 files changed, 188 insertions, 177 deletions
diff --git a/qmake/tools/qsettings.cpp b/qmake/tools/qsettings.cpp index 35fc039..598e94b 100644 --- a/qmake/tools/qsettings.cpp +++ b/qmake/tools/qsettings.cpp @@ -1,12 +1,12 @@ /**************************************************************************** -** $Id$ +** ** ** Implementation of QSettings class ** ** Created : 000626 ** -** Copyright (C) 2000-2002 Trolltech AS. All rights reserved. +** Copyright (C) 2000-2003 Trolltech AS. All rights reserved. ** ** This file is part of the tools module of the Qt GUI Toolkit. ** ** This file may be distributed under the terms of the Q Public License @@ -59,9 +59,11 @@ static inline int qt_open( const char *pathname, int flags, mode_t mode ) #include "qmap.h" #include "qtextstream.h" #include "qregexp.h" #include <private/qsettings_p.h> +#ifndef NO_ERRNO_H #include <errno.h> +#endif /*! \class QSettings \brief The QSettings class provides persistent platform-independent application settings. @@ -100,9 +102,10 @@ static inline int qt_open( const char *pathname, int flags, mode_t mode ) /MyCompany/MyApplication/recent files/3 \endcode Each line above is a complete key, made up of subkeys. - A typical usage pattern for reading application startup: + A typical usage pattern for reading settings at application + startup: \code QSettings settings; settings.setPath( "MyCompany.com", "MyApplication" ); @@ -110,9 +113,10 @@ static inline int qt_open( const char *pathname, int flags, mode_t mode ) int width = settings.readNumEntry( "/geometry/width", 640 ); // ... \endcode - A typical usage pattern for application exit or 'save preferences': + A typical usage pattern for saving settings at application exit or + 'save preferences': \code QSettings settings; settings.setPath( "MyCompany.com", "MyApplication" ); @@ -120,10 +124,11 @@ static inline int qt_open( const char *pathname, int flags, mode_t mode ) settings.writeEntry( "/geometry/width", width ); // ... \endcode - QSettings can build a key prefix that is prepended to all keys. To - build the key prefix, use beginGroup() and endGroup(). + A key prefix can be prepended to all keys using beginGroup(). The + application of the prefix is stopped using endGroup(). For + example: \code QSettings settings; settings.beginGroup( "/MainWindow" ); @@ -153,10 +158,10 @@ static inline int qt_open( const char *pathname, int flags, mode_t mode ) QStringList subkeys = subkeyList( "/MyApplication/recent files" ); // subkeys is empty. \endcode - Since settings for Windows are stored in the registry there are size - limits as follows: + Since settings for Windows are stored in the registry there are + some size limitations as follows: \list \i A subkey may not exceed 255 characters. \i An entry's value may not exceed 16,300 characters. \i All the values of a key (for example, all the 'recent files' @@ -164,36 +169,37 @@ static inline int qt_open( const char *pathname, int flags, mode_t mode ) \endlist These limitations are not enforced on Unix or Mac OS X. - If you wish to use a different search path call insertSearchPath() - as often as necessary to add your preferred paths. Call - removeSearchPath() to remove any unwanted paths. + \warning Creating multiple, simultaneous instances of QSettings writing + to a text file may lead to data loss! This is a known issue which will + be fixed in a future release of Qt. \section1 Notes for Mac OS X Applications - Internal to the CFPreferences API it is not defined (for Mac OS 9 - support) where the settings will ultimitely be stored. However, at the - time of this writing the settings will be stored (either on a global or - user basis, preferring locally) into a plist file in - $ROOT/System/Library/Preferences (in XML format). QSettings will create - an appropriate plist file (com.<first group name>.plist) out of the - full path to a key. + The location where settings are stored is not formally defined by + the CFPreferences API. + + At the time of writing settings are stored (either on a global or + user basis, preferring locally) into a plist file in \c + $ROOT/System/Library/Preferences (in XML format). QSettings will + create an appropriate plist file (\c{com.<first group name>.plist}) + out of the full path to a key. - For further information on CFPreferences see also - \link http://developer.apple.com/techpubs/macosx/CoreFoundation/PreferenceServices/preferenceservices_carbon.html + For further information on CFPreferences see + \link http://developer.apple.com/documentation/CoreFoundation/Conceptual/CFPreferences/index.html Apple's Specifications\endlink \section1 Notes for Unix Applications There is no universally accepted place for storing application settings under Unix. In the examples the settings file will be searched for in the following directories: \list 1 - \i INSTALL/etc/settings - \i /opt/MyCompany/share/etc - \i /opt/MyCompany/share/MyApplication/etc - \i $HOME/.qt + \i \c SYSCONF - the default value is \c INSTALL/etc/settings + \i \c /opt/MyCompany/share/etc + \i \c /opt/MyCompany/share/MyApplication/etc + \i \c $HOME/.qt \endlist When reading settings the files are searched in the order shown above, with later settings overriding earlier settings. Files for which the user doesn't have read permission are ignored. When saving @@ -216,10 +222,10 @@ static inline int qt_open( const char *pathname, int flags, mode_t mode ) will end up writing the "geometry/width" setting to the file \c{$HOME/.qt/myapplicationrc} (assuming that the application is being run by an ordinary user, i.e. not by root). - For cross-platform applications you should ensure that the Windows - size limitations are not exceeded. + For cross-platform applications you should ensure that the + \link #sizelimit Windows size limitations \endlink are not exceeded. */ /*! \enum QSettings::System @@ -309,47 +315,8 @@ static void closelock( HANDLE fd ) (void) fcntl( fd, F_SETLKW, &fl ); close( fd ); } -#elif defined(Q_WS_WIN) -#define Q_LOCKREAD 1 -#define Q_LOCKWRITE 2 - -static HANDLE openlock( const QString &name, int /*type*/ ) -{ - if ( !QFile::exists( name ) ) - return 0; - - return 0; - - HANDLE fd = 0; - - QT_WA( { - fd = CreateFileW( (TCHAR*)name.ucs2(), GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL ); - } , { - fd = CreateFileA( name.local8Bit(), GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL ); - } ); - - if ( !LockFile( fd, 0, 0, (DWORD)-1, (DWORD)-1 ) ) { // ### (DWORD)-1 ??? -#ifdef QT_CHECK_STATE - qWarning( "QSettings: openlock failed!" ); -#endif - } - return fd; -} - -static void closelock( HANDLE fd ) -{ - if ( !fd ) - return; - - if ( !UnlockFile( fd, 0, 0, (DWORD)-1, (DWORD)-1 ) ) { // ### (DWORD)-1 ??? -#ifdef QT_CHECK_STATE - qWarning( "QSettings: closelock failed!"); -#endif - } - CloseHandle( fd ); -} #endif QSettingsGroup::QSettingsGroup() @@ -364,9 +331,11 @@ void QSettingsHeading::read(const QString &filename) { if (! QFileInfo(filename).exists()) return; +#ifndef Q_WS_WIN HANDLE lockfd = openlock( filename, Q_LOCKREAD ); +#endif QFile file(filename); if (! file.open(IO_ReadOnly)) { #if defined(QT_CHECK_STATE) @@ -385,9 +354,11 @@ void QSettingsHeading::read(const QString &filename) git = end(); file.close(); +#ifndef Q_WS_WIN closelock( lockfd ); +#endif } void QSettingsHeading::parseLine(QTextStream &stream) @@ -403,9 +374,9 @@ void QSettingsHeading::parseLine(QTextStream &stream) if (line[0] == QChar('[')) { QString gname = line; - gname = gname.remove(0, 1); + gname = gname.remove((uint)0, 1); if (gname[(int)gname.length() - 1] == QChar(']')) gname = gname.remove(gname.length() - 1, 1); git = find(gname); @@ -525,13 +496,9 @@ QSettingsPrivate::QSettingsPrivate( QSettings::Format format ) } } ); #endif // Q_OS_TEMP #else -// for now -#define QSETTINGS_DEFAULT_PATH_SUFFIX "/etc/settings" - - defPath = qInstallPath(); - defPath += QSETTINGS_DEFAULT_PATH_SUFFIX; + defPath = qInstallPathSysconf(); #endif QDir dir(appSettings); if (! dir.exists()) { if (! dir.mkdir(dir.path())) @@ -700,29 +667,34 @@ QDateTime QSettingsPrivate::modificationTime() } bool qt_verify_key( const QString &key ) { - if ( key.isEmpty() || key[0] != '/' || key.contains( QRegExp("[=\\\\r\\\\n" ) ) ) + if ( key.isEmpty() || key[0] != '/' || key.contains( QRegExp("[=\\r\\n]" ) ) ) return FALSE; return TRUE; } -static inline QString groupKey( const QString &group, const QString &key ) +static QString groupKey( const QString &group, const QString &key ) { + QString grp_key; if ( group.isEmpty() || ( group.length() == 1 && group[0] == '/' ) ) { // group is empty, or it contains a single '/', so we just return the key if ( key.startsWith( "/" ) ) - return key; - return "/" + key; + grp_key = key; + else + grp_key = "/" + key; } else if ( group.endsWith( "/" ) || key.startsWith( "/" ) ) { - return group + key; + grp_key = group + key; + } else { + grp_key = group + "/" + key; } - return group + "/" + key; + return grp_key; } /*! Inserts \a path into the settings search path. The semantics of \a - path depends on the system \a s. + path depends on the system \a s. It is usually easier and better to + use setPath() instead of this function. When \a s is \e Windows and the execution environment is \e not Windows the function does nothing. Similarly when \a s is \e Unix and the execution environment is \e not Unix the function does nothing. @@ -765,10 +737,11 @@ static inline QString groupKey( const QString &group, const QString &key ) filename for reading and writing settings files. By default, there are two entries in the search path: \list 1 - \i INSTALL/etc - where \c INSTALL is the directory where Qt was installed. - \i $HOME/.qt/ - where \c $HOME is the user's home directory. + \i \c SYSCONF - where \c SYSCONF is a directory specified when + configuring Qt; by default it is INSTALL/etc/settings. + \i \c $HOME/.qt/ - where \c $HOME is the user's home directory. \endlist All insertions into the search path will go before $HOME/.qt/. For example: @@ -779,9 +752,9 @@ static inline QString groupKey( const QString &group, const QString &key ) // ... \endcode Will result in a search path of: \list 1 - \i INSTALL/etc + \i SYSCONF \i /opt/MyCompany/share/etc \i /opt/MyCompany/share/MyApplication/etc \i $HOME/.qt \endlist @@ -790,8 +763,11 @@ static inline QString groupKey( const QString &group, const QString &key ) which the user doesn't have read permission are ignored. When saving settings QSettings works in the order shown above, writing to the first settings file for which the user has write permission. + Note that paths in the file system are not created by this + function, so they must already exist to be useful. + Settings under Unix are stored in files whose names are based on the first subkey of the key (not including the search path). The algorithm for creating names is essentially: lowercase the first subkey, replace spaces with underscores and add 'rc', e.g. @@ -814,9 +790,9 @@ void QSettings::insertSearchPath( System s, const QString &path) #if !defined(Q_WS_WIN) if ( s == Windows ) return; #endif -#if !defined(Q_WS_WIN) +#if !defined(Q_OS_MAC) if ( s == Mac ) return; #endif @@ -963,28 +939,39 @@ bool QSettings::sync() while (it != d->headings.end()) { // determine filename QSettingsHeading hd(*it); QSettingsHeading::Iterator hdit = hd.begin(); - QFile file; + QString filename; QStringList::Iterator pit = d->searchPaths.begin(); if ( !d->globalScope ) ++pit; while (pit != d->searchPaths.end()) { QString filebase = it.key().lower().replace(QRegExp("\\s+"), "_"); QFileInfo di(*pit); + if ( !di.exists() ) { + QDir dir; + dir.mkdir( *pit ); + } + QFileInfo fi((*pit++) + "/" + filebase + "rc"); if ((fi.exists() && fi.isFile() && fi.isWritable()) || - (! fi.exists() && di.isDir() && di.isWritable())) { - file.setName(fi.filePath()); + (! fi.exists() && di.isDir() +#ifndef Q_WS_WIN + && di.isWritable() +#else + && ((qWinVersion()&Qt::WV_NT_based) > Qt::WV_2000 || di.isWritable()) +#endif + )) { + filename = fi.filePath(); break; } } ++it; - if ( file.name().isEmpty() ) { + if ( filename.isEmpty() ) { #ifdef QT_CHECK_STATE qWarning("QSettings::sync: filename is null/empty"); #endif // QT_CHECK_STATE @@ -992,10 +979,13 @@ bool QSettings::sync() success = FALSE; continue; } - HANDLE lockfd = openlock( file.name(), Q_LOCKWRITE ); +#ifndef Q_WS_WIN + HANDLE lockfd = openlock( filename, Q_LOCKWRITE ); +#endif + QFile file( filename + ".tmp" ); if (! file.open(IO_WriteOnly)) { #ifdef QT_CHECK_STATE qWarning("QSettings::sync: failed to open '%s' for writing", @@ -1046,9 +1036,28 @@ bool QSettings::sync() } file.close(); + if ( success ) { + QDir dir( QFileInfo( file ).dir( TRUE ) ); + if ( dir.exists( filename ) && !dir.remove( filename ) || + !dir.rename( file.name(), filename, TRUE ) ) { + +#ifdef QT_CHECK_STATE + qWarning( "QSettings::sync: error writing file '%s'", + QFile::encodeName( filename ).data() ); +#endif // QT_CHECK_STATE + + success = FALSE; + } + } + + // remove temporary file + file.remove(); + +#ifndef Q_WS_WIN closelock( lockfd ); +#endif } d->modified = FALSE; @@ -1071,11 +1080,12 @@ bool QSettings::sync() \internal */ bool QSettings::readBoolEntry(const QString &key, bool def, bool *ok ) { - if ( !qt_verify_key( key ) ) { + QString grp_key( groupKey( group(), key ) ); + if ( !qt_verify_key( grp_key ) ) { #if defined(QT_CHECK_STATE) - qWarning( "QSettings::readBoolEntry: Invalid key: '%s'", key.isNull() ? "(null)" : key.latin1() ); + qWarning( "QSettings::readBoolEntry: Invalid key: '%s'", grp_key.isNull() ? "(null)" : grp_key.latin1() ); #endif if ( ok ) *ok = FALSE; @@ -1083,9 +1093,9 @@ bool QSettings::readBoolEntry(const QString &key, bool def, bool *ok ) } #if !defined(QWS) && (defined(Q_WS_WIN) || defined(Q_OS_MAC)) if ( d->sysd ) - return d->sysReadBoolEntry( groupKey( group(), key ), def, ok ); + return d->sysReadBoolEntry( grp_key, def, ok ); #endif QString value = readEntry( key, ( def ? "true" : "false" ), ok ); @@ -1122,11 +1132,12 @@ bool QSettings::readBoolEntry(const QString &key, bool def, bool *ok ) \internal */ double QSettings::readDoubleEntry(const QString &key, double def, bool *ok ) { - if ( !qt_verify_key( key ) ) { + QString grp_key( groupKey( group(), key ) ); + if ( !qt_verify_key( grp_key ) ) { #if defined(QT_CHECK_STATE) - qWarning( "QSettings::readDoubleEntry: Invalid key: '%s'", key.isNull() ? "(null)" : key.latin1() ); + qWarning( "QSettings::readDoubleEntry: Invalid key: '%s'", grp_key.isNull() ? "(null)" : grp_key.latin1() ); #endif if ( ok ) *ok = FALSE; @@ -1134,9 +1145,9 @@ double QSettings::readDoubleEntry(const QString &key, double def, bool *ok ) } #if !defined(QWS) && (defined(Q_WS_WIN) || defined(Q_OS_MAC)) if ( d->sysd ) - return d->sysReadDoubleEntry( groupKey( group(), key ), def, ok ); + return d->sysReadDoubleEntry( grp_key, def, ok ); #endif QString value = readEntry( key, QString::number(def), ok ); bool conv_ok; @@ -1167,20 +1178,21 @@ double QSettings::readDoubleEntry(const QString &key, double def, bool *ok ) \internal */ int QSettings::readNumEntry(const QString &key, int def, bool *ok ) { - if ( !qt_verify_key( key ) ) { + QString grp_key( groupKey( group(), key ) ); + if ( !qt_verify_key( grp_key ) ) { #if defined(QT_CHECK_STATE) - qWarning( "QSettings::readNumEntry: Invalid key: '%s'", key.isNull() ? "(null)" : key.latin1() ); + qWarning( "QSettings::readNumEntry: Invalid key: '%s'", grp_key.isNull() ? "(null)" : grp_key.latin1() ); #endif if ( ok ) *ok = FALSE; return def; } #if !defined(QWS) && (defined(Q_WS_WIN) || defined(Q_OS_MAC)) if ( d->sysd ) - return d->sysReadNumEntry( groupKey( group(), key ), def, ok ); + return d->sysReadNumEntry( grp_key, def, ok ); #endif QString value = readEntry( key, QString::number( def ), ok ); bool conv_ok; @@ -1211,37 +1223,36 @@ int QSettings::readNumEntry(const QString &key, int def, bool *ok ) \internal */ QString QSettings::readEntry(const QString &key, const QString &def, bool *ok ) { - if ( !qt_verify_key( key ) ) { + QString grp_key( groupKey( group(), key ) ); + if ( !qt_verify_key( grp_key ) ) { #if defined(QT_CHECK_STATE) - qWarning( "QSettings::readEntry: Invalid key: '%s'", key.isNull() ? "(null)" : key.latin1() ); + qWarning( "QSettings::readEntry: Invalid key: '%s'", grp_key.isNull() ? "(null)" : grp_key.latin1() ); #endif if ( ok ) *ok = FALSE; return def; } - QString theKey = groupKey( group(), key ); - #if !defined(QWS) && (defined(Q_WS_WIN) || defined(Q_OS_MAC)) if ( d->sysd ) - return d->sysReadEntry( theKey, def, ok ); + return d->sysReadEntry( grp_key, def, ok ); #endif if ( ok ) // no, everything is not ok *ok = FALSE; QString realkey; - if (theKey[0] == '/') { + if (grp_key[0] == '/') { // parse our key - QStringList list(QStringList::split('/', theKey)); + QStringList list(QStringList::split('/', grp_key)); if (list.count() < 2) { #ifdef QT_CHECK_STATE - qWarning("QSettings::readEntry: invalid key '%s'", theKey.latin1()); + qWarning("QSettings::readEntry: invalid key '%s'", grp_key.latin1()); #endif // QT_CHECK_STATE if ( ok ) *ok = FALSE; return def; @@ -1261,10 +1272,11 @@ QString QSettings::readEntry(const QString &key, const QString &def, bool *ok ) list.remove(list.at(0)); realkey = list.join("/"); } - } else - realkey = theKey; + } else { + realkey = grp_key; + } QSettingsGroup grp = d->readGroup(); QSettingsGroup::const_iterator it = grp.find( realkey ), end = grp.end(); QString retval = def; @@ -1289,18 +1301,19 @@ QString QSettings::readEntry(const QString &key, const QString &def, bool *ok ) \sa readListEntry(), readNumEntry(), readDoubleEntry(), readBoolEntry(), removeEntry() */ bool QSettings::writeEntry(const QString &key, bool value) { - if ( !qt_verify_key( key ) ) { + QString grp_key( groupKey( group(), key ) ); + if ( !qt_verify_key( grp_key ) ) { #if defined(QT_CHECK_STATE) - qWarning( "QSettings::writeEntry: Invalid key: '%s'", key.isNull() ? "(null)" : key.latin1() ); + qWarning( "QSettings::writeEntry: Invalid key: '%s'", grp_key.isNull() ? "(null)" : grp_key.latin1() ); #endif return FALSE; } #if !defined(QWS) && (defined(Q_WS_WIN) || defined(Q_OS_MAC)) if ( d->sysd ) - return d->sysWriteEntry( groupKey( group(), key ), value ); + return d->sysWriteEntry( grp_key, value ); #endif QString s(value ? "true" : "false"); return writeEntry(key, s); } @@ -1319,18 +1332,19 @@ bool QSettings::writeEntry(const QString &key, bool value) \sa readListEntry(), readNumEntry(), readDoubleEntry(), readBoolEntry(), removeEntry() */ bool QSettings::writeEntry(const QString &key, double value) { - if ( !qt_verify_key( key ) ) { + QString grp_key( groupKey( group(), key ) ); + if ( !qt_verify_key( grp_key ) ) { #if defined(QT_CHECK_STATE) - qWarning( "QSettings::writeEntry: Invalid key: '%s'", key.isNull() ? "(null)" : key.latin1() ); + qWarning( "QSettings::writeEntry: Invalid key: '%s'", grp_key.isNull() ? "(null)" : grp_key.latin1() ); #endif return FALSE; } #if !defined(QWS) && (defined(Q_WS_WIN) || defined(Q_OS_MAC)) if ( d->sysd ) - return d->sysWriteEntry( groupKey( group(), key ), value ); + return d->sysWriteEntry( grp_key, value ); #endif QString s(QString::number(value)); return writeEntry(key, s); } @@ -1348,18 +1362,19 @@ bool QSettings::writeEntry(const QString &key, double value) \sa readListEntry(), readNumEntry(), readDoubleEntry(), readBoolEntry(), removeEntry() */ bool QSettings::writeEntry(const QString &key, int value) { - if ( !qt_verify_key( key ) ) { + QString grp_key( groupKey( group(), key ) ); + if ( !qt_verify_key( grp_key ) ) { #if defined(QT_CHECK_STATE) - qWarning( "QSettings::writeEntry: Invalid key: '%s'", key.isNull() ? "(null)" : key.latin1() ); + qWarning( "QSettings::writeEntry: Invalid key: '%s'", grp_key.isNull() ? "(null)" : grp_key.latin1() ); #endif return FALSE; } #if !defined(QWS) && (defined(Q_WS_WIN) || defined(Q_OS_MAC)) if ( d->sysd ) - return d->sysWriteEntry( groupKey( group(), key ), value ); + return d->sysWriteEntry( grp_key, value ); #endif QString s(QString::number(value)); return writeEntry(key, s); } @@ -1382,15 +1397,8 @@ bool QSettings::writeEntry(const QString &key, int value) \sa readEntry(), removeEntry() */ bool QSettings::writeEntry(const QString &key, const char *value) { - if ( !qt_verify_key( key ) ) { -#if defined(QT_CHECK_STATE) - qWarning( "QSettings::writeEntry: Invalid key: '%s'", key.isNull() ? "(null)" : key.latin1() ); -#endif - return FALSE; - } - return writeEntry(key, QString(value)); } @@ -1407,32 +1415,31 @@ bool QSettings::writeEntry(const QString &key, const char *value) \sa readListEntry(), readNumEntry(), readDoubleEntry(), readBoolEntry(), removeEntry() */ bool QSettings::writeEntry(const QString &key, const QString &value) { - if ( !qt_verify_key( key ) ) { + QString grp_key( groupKey( group(), key ) ); + if ( !qt_verify_key( grp_key ) ) { #if defined(QT_CHECK_STATE) - qWarning( "QSettings::writeEntry: Invalid key: '%s'", key.isNull() ? "(null)" : key.latin1() ); + qWarning( "QSettings::writeEntry: Invalid key: '%s'", grp_key.isNull() ? "(null)" : grp_key.latin1() ); #endif return FALSE; } - QString theKey = groupKey( group(), key ); - #if !defined(QWS) && (defined(Q_WS_WIN) || defined(Q_OS_MAC)) if ( d->sysd ) - return d->sysWriteEntry( theKey, value ); + return d->sysWriteEntry( grp_key, value ); #endif // NOTE: we *do* allow value to be a null/empty string QString realkey; - if (theKey[0] == '/') { + if (grp_key[0] == '/') { // parse our key - QStringList list(QStringList::split('/', theKey)); + QStringList list(QStringList::split('/', grp_key)); if (list.count() < 2) { #ifdef QT_CHECK_STATE - qWarning("QSettings::writeEntry: invalid key '%s'", theKey.latin1()); + qWarning("QSettings::writeEntry: invalid key '%s'", grp_key.latin1()); #endif // QT_CHECK_STATE return FALSE; } @@ -1451,10 +1458,11 @@ bool QSettings::writeEntry(const QString &key, const QString &value) list.remove(list.at(0)); realkey = list.join("/"); } - } else - realkey = theKey; + } else { + realkey = grp_key; + } d->writeGroup(realkey, value); return TRUE; } @@ -1468,31 +1476,29 @@ bool QSettings::writeEntry(const QString &key, const QString &value) \sa readEntry(), writeEntry() */ bool QSettings::removeEntry(const QString &key) { - if ( !qt_verify_key( key ) ) { + QString grp_key( groupKey( group(), key ) ); + if ( !qt_verify_key( grp_key ) ) { #if defined(QT_CHECK_STATE) - qWarning( "QSettings::removeEntry: Invalid key: '%s'", key.isNull() ? "(null)" : key.latin1() ); + qWarning( "QSettings::removeEntry: Invalid key: '%s'", grp_key.isNull() ? "(null)" : grp_key.latin1() ); #endif return FALSE; } - QString theKey = groupKey( group(), key ); - #if !defined(QWS) && (defined(Q_WS_WIN) || defined(Q_OS_MAC)) if ( d->sysd ) - return d->sysRemoveEntry( theKey ); + return d->sysRemoveEntry( grp_key ); #endif QString realkey; - - if (theKey[0] == '/') { + if (grp_key[0] == '/') { // parse our key - QStringList list(QStringList::split('/', theKey)); + QStringList list(QStringList::split('/', grp_key)); if (list.count() < 2) { #ifdef QT_CHECK_STATE - qWarning("QSettings::removeEntry: invalid key '%s'", theKey.latin1()); + qWarning("QSettings::removeEntry: invalid key '%s'", grp_key.latin1()); #endif // QT_CHECK_STATE return FALSE; } @@ -1511,10 +1517,11 @@ bool QSettings::removeEntry(const QString &key) list.remove(list.at(0)); realkey = list.join("/"); } - } else - realkey = theKey; + } else { + realkey = grp_key; + } d->removeGroup(realkey); return TRUE; } @@ -1547,30 +1554,29 @@ bool QSettings::removeEntry(const QString &key) \sa subkeyList() */ QStringList QSettings::entryList(const QString &key) const { - if ( !qt_verify_key( key ) ) { + QString grp_key( groupKey( group(), key ) ); + if ( !qt_verify_key( grp_key ) ) { #if defined(QT_CHECK_STATE) - qWarning( "QSettings::entryList: Invalid key: %s", key.isNull() ? "(null)" : key.latin1() ); + qWarning( "QSettings::entryList: Invalid key: %s", grp_key.isNull() ? "(null)" : grp_key.latin1() ); #endif return QStringList(); } - QString theKey = groupKey( group(), key ); - #if !defined(QWS) && (defined(Q_WS_WIN) || defined(Q_OS_MAC)) if ( d->sysd ) - return d->sysEntryList( theKey ); + return d->sysEntryList( grp_key ); #endif QString realkey; - if (theKey[0] == '/') { + if (grp_key[0] == '/') { // parse our key - QStringList list(QStringList::split('/', theKey)); + QStringList list(QStringList::split('/', grp_key)); if (list.count() < 1) { #ifdef QT_CHECK_STATE - qWarning("QSettings::listEntries: invalid key '%s'", theKey.latin1()); + qWarning("QSettings::listEntries: invalid key '%s'", grp_key.latin1()); #endif // QT_CHECK_STATE return QStringList(); } @@ -1589,9 +1595,9 @@ QStringList QSettings::entryList(const QString &key) const realkey = list.join("/"); } } else - realkey = theKey; + realkey = grp_key; QSettingsGroup grp = d->readGroup(); QSettingsGroup::Iterator it = grp.begin(); QStringList ret; @@ -1644,31 +1650,30 @@ QStringList QSettings::entryList(const QString &key) const \sa entryList() */ QStringList QSettings::subkeyList(const QString &key) const { - if ( !qt_verify_key( key ) ) { + QString grp_key( groupKey( group(), key ) ); + if ( !qt_verify_key( grp_key ) ) { #if defined(QT_CHECK_STATE) - qWarning( "QSettings::subkeyList: Invalid key: %s", key.isNull() ? "(null)" : key.latin1() ); + qWarning( "QSettings::subkeyList: Invalid key: %s", grp_key.isNull() ? "(null)" : grp_key.latin1() ); #endif return QStringList(); } - QString theKey = groupKey( group(), key ); - #if !defined(QWS) && (defined(Q_WS_WIN) || defined(Q_OS_MAC)) if ( d->sysd ) - return d->sysSubkeyList( theKey ); + return d->sysSubkeyList( grp_key ); #endif QString realkey; int subkeycount = 2; - if (theKey[0] == '/') { + if (grp_key[0] == '/') { // parse our key - QStringList list(QStringList::split('/', theKey)); + QStringList list(QStringList::split('/', grp_key)); if (list.count() < 1) { #ifdef QT_CHECK_STATE - qWarning("QSettings::subkeyList: invalid key '%s'", theKey.latin1()); + qWarning("QSettings::subkeyList: invalid key '%s'", grp_key.latin1()); #endif // QT_CHECK_STATE return QStringList(); } @@ -1690,9 +1695,9 @@ QStringList QSettings::subkeyList(const QString &key) const realkey = list.join("/"); } } else - realkey = theKey; + realkey = grp_key; QStringList ret; if ( subkeycount == 1 ) { QMap<QString,QSettingsHeading>::Iterator it = d->headings.begin(); @@ -1736,31 +1741,30 @@ QStringList QSettings::subkeyList(const QString &key) const \internal This function returns the time of last modification for \a key. */ -QDateTime QSettings::lastModficationTime(const QString &key) +QDateTime QSettings::lastModificationTime( const QString &key ) { - if ( !qt_verify_key( key ) ) { + QString grp_key( groupKey( group(), key ) ); + if ( !qt_verify_key( grp_key ) ) { #if defined(QT_CHECK_STATE) - qWarning( "QSettings::lastModficationTime: Invalid key: '%s'", key.isNull() ? "(null)" : key.latin1() ); + qWarning( "QSettings::lastModificationTime: Invalid key '%s'", grp_key.isNull() ? "(null)" : grp_key.latin1() ); #endif return QDateTime(); } - QString theKey = groupKey( group(), key ); - #if !defined(QWS) && (defined(Q_WS_WIN) || defined(Q_OS_MAC)) if ( d->sysd ) return QDateTime(); #endif - if (theKey[0] == '/') { + if (grp_key[0] == '/') { // parse our key - QStringList list(QStringList::split('/', theKey)); + QStringList list(QStringList::split('/', grp_key)); if (list.count() < 2) { #ifdef QT_CHECK_STATE - qWarning("QSettings::lastModficationTime: invalid key '%s'", theKey.latin1()); + qWarning("QSettings::lastModificationTime: Invalid key '%s'", grp_key.latin1()); #endif // QT_CHECK_STATE return QDateTime(); } @@ -1779,8 +1783,9 @@ QDateTime QSettings::lastModficationTime(const QString &key) /*! \overload + \obsolete Writes the string list entry \a value into key \a key. The \a key is created if it doesn't exist. Any previous value is overwritten by \a value. The list is stored as a sequence of strings separated @@ -1795,12 +1800,13 @@ QDateTime QSettings::lastModficationTime(const QString &key) retrieve identical list data that is stored with this function. We recommend using the writeEntry() and readListEntry() overloads that do not take a \a separator argument. + If an error occurs the settings are left unchanged and FALSE is returned; otherwise returns TRUE. - \sa readListEntry(), readNumEntry(), readDoubleEntry(), readBoolEntry(), removeEntry() + \sa readListEntry(), readNumEntry(), readDoubleEntry(), readBoolEntry(), removeEntry(), QStringList::join() */ bool QSettings::writeEntry(const QString &key, const QStringList &value, const QChar &separator) { @@ -1838,21 +1844,21 @@ bool QSettings::writeEntry(const QString &key, const QStringList &value) /*! \overload QStringList QSettings::readListEntry(const QString &key, const QChar &separator, bool *ok ) const + \obsolete Reads the entry specified by \a key as a string. The \a separator is used to create a QStringList by calling QStringList::split(\a - separator, entry). If \a ok is not 0: \a *ok is set to TRUE if the - key was read, otherwise \a *ok is set to FALSE. + separator, entry). If \a ok is not 0: \a *ok is set to TRUE + if the key was read, otherwise \a *ok is set to FALSE. \warning As the documentation states, QStringList::split() will omit empty strings from the list. Because of this, it is impossible to retrieve identical list data with this function. We recommend using the readListEntry() and writeEntry() overloads that do not take a \a separator argument. - Note that if you want to iterate over the list, you should iterate over a copy, e.g. \code QStringList list = mySettings.readListEntry( "size", " " ); @@ -1948,10 +1954,9 @@ void qt_setSettingsBasePath(const QString &); //qsettings_mac.cpp QSettings::User for user-specific settings, or QSettings::Global for system-wide settings (generally these will be read-only to many users). - Not all information is relevant on all systems (e.g. scoping is - currently used only if QSettings accesses the Windows registry). + Not all information is relevant on all systems. */ void QSettings::setPath( const QString &domain, const QString &product, Scope scope ) { @@ -1979,15 +1984,21 @@ void QSettings::setPath( const QString &domain, const QString &product, Scope sc #if defined(Q_WS_WIN) actualSearchPath = "/" + domain.mid( 0, lastDot ) + "/" + product; insertSearchPath( Windows, actualSearchPath ); #elif !defined(QWS) && defined(Q_OS_MAC) + if(lastDot != -1) { QString topLevelDomain = domain.right( domain.length() - lastDot - 1 ) + "."; if ( !topLevelDomain.isEmpty() ) qt_setSettingsBasePath( topLevelDomain ); - actualSearchPath = "/" + domain.left( lastDot ) + product; + } + actualSearchPath = "/" + domain.left( lastDot ) + "." + product; insertSearchPath( Mac, actualSearchPath ); #else - actualSearchPath = "/" + domain.mid( 0, lastDot ) + "/" + product; + if (scope == User) + actualSearchPath = QDir::homeDirPath() + "/."; + else + actualSearchPath = QString(qInstallPathSysconf()) + "/"; + actualSearchPath += domain.mid( 0, lastDot ) + "/" + product; insertSearchPath( Unix, actualSearchPath ); #endif } |