[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-core-devel
Subject:    Re: KConfig object-orientification
From:       Waldo Bastian <bastian () kde ! org>
Date:       2001-11-03 8:00:59
[Download RAW message or body]

On Wednesday 31 October 2001 05:02 pm, Waldo Bastian wrote:
> On Wednesday 31 October 2001 03:21 pm, Charles Samuels wrote:
> > Well, I've started on the arduous task of making KConfig::group() return
> > a temporary class (KConfigGroup) that has all the accessors (like
> > readEntry, writeEntry, etc).
> >
> > I'm also breaking compatiblity big-time with older versions.  As in,
> > major. KConfigGroupSaver is departing, and, well, all sorts of good
> > things.
>
> I think the same can be achieved while keeping the current class-structure.
> E.g. you can make a KConfigGroup class that derives from KConfigBase but
> uses the  lookupData() / putData() methods of the original KConfig object.
>
> Cheers,
> Waldo

Something like this. (untested)

Cheers,
Waldo
-- 
After imposing conduct restrictions on Saddam Hussein for years,
the US government will now also impose conduct restrictions on Microsoft.

["kconfiggroup.diff" (text/x-diff)]

Index: kconfig.cpp
===================================================================
RCS file: /home/kde/kdelibs/kdecore/kconfig.cpp,v
retrieving revision 1.61
diff -u -3 -d -p -r1.61 kconfig.cpp
--- kconfig.cpp	2001/11/02 12:51:42	1.61
+++ kconfig.cpp	2001/11/03 06:46:30
@@ -125,32 +125,6 @@ QStringList KConfig::groupList() const
   return retList;
 }
 
-bool KConfig::hasKey(const QString &key) const
-{
-   return KConfig::hasKey(key.utf8().data());
-}
-
-bool KConfig::hasKey(const char *pKey) const
-{
-  KEntryKey aEntryKey(mGroup, 0);
-  aEntryKey.c_key = pKey;
-
-  KEntryMapConstIterator aIt;
-
-  if (!locale().isNull()) {
-    // try the localized key first
-    aEntryKey.bLocal = true;
-    aIt = aEntryMap.find(aEntryKey);
-    if (aIt != aEntryMap.end() && !(*aIt).mValue.isNull() && !(*aIt).bDeleted )
-      return true;
-    aEntryKey.bLocal = false;
-  }
-
-  // try the non-localized version
-  aIt = aEntryMap.find(aEntryKey);
-  return  (aIt != aEntryMap.end() && !(*aIt).mValue.isNull() && !(*aIt).bDeleted );
-}
-
 QMap<QString, QString> KConfig::entryMap(const QString &pGroup) const
 {
   QCString pGroup_utf = pGroup.utf8();
@@ -256,17 +230,7 @@ KEntry KConfig::lookupData(const KEntryK
   }
 }
 
-bool KConfig::hasGroup(const QString &group) const
-{
-  return KConfig::hasGroup( group.utf8());
-}
-
-bool KConfig::hasGroup(const char *_pGroup) const
-{
-  return KConfig::hasGroup( QCString(_pGroup));
-}
-
-bool KConfig::hasGroup(const QCString &group) const
+bool KConfig::internalHasGroup(const QCString &group) const
 {
   KEntryKey groupKey( group, 0);
 
Index: kconfig.h
===================================================================
RCS file: /home/kde/kdelibs/kdecore/kconfig.h,v
retrieving revision 1.41
diff -u -3 -d -p -r1.41 kconfig.h
--- kconfig.h	2001/11/02 22:27:27	1.41
+++ kconfig.h	2001/11/03 06:46:30
@@ -81,29 +81,12 @@ public:
    */
   virtual void rollback(bool bDeep = true);
 
-  /**
-   * Returns true if the specified group is known.
-   *
-   * @param group The group to search for.
-   * @returns @p true if the group exists.
-   */
-  virtual bool hasGroup(const QString &group) const;
 
   /**
    * Returns a list of groups that are known.
    */
   virtual QStringList groupList() const;
 
-  /*
-   * Checks if the key has an entry in the currently active group. Use
-   * this to determine if a key is not specified for the current group
-   * (@p hasKey returns false). Keys with null data are considered
-   * nonexistent.
-   *
-   * @param key The key to search for.
-   */
-  virtual bool hasKey(const QString &key) const;
-
   /**
    * Returns a map (tree) of entries for all entries in a particular
    * group.
@@ -126,6 +109,14 @@ public:
 protected:
 
   /**
+   * Returns true if the specified group is known.
+   *
+   * @param group The group to search for.
+   * @returns @p true if the group exists.
+   */
+  virtual bool internalHasGroup(const QCString &group) const;
+
+  /**
    * Returns a map (tree) of the entries in the specified group.
    *
    * Do not use this function, the implementation / return type are
@@ -179,15 +170,6 @@ protected:
    * set to QString::null.
    */
   KEntryMap aEntryMap;
-
-
-public:
-  /**
-   * Overloaded public functions.
-   */
-  virtual bool hasGroup(const QCString &group) const;
-  virtual bool hasGroup(const char *_pGroup) const;
-  virtual bool hasKey(const char *pKey) const;
 
 private:
   /**
Index: kconfigbase.cpp
===================================================================
RCS file: /home/kde/kdelibs/kdecore/kconfigbase.cpp,v
retrieving revision 1.125
diff -u -3 -d -p -r1.125 kconfigbase.cpp
--- kconfigbase.cpp	2001/11/02 13:38:49	1.125
+++ kconfigbase.cpp	2001/11/03 06:46:31
@@ -151,7 +151,6 @@ void KConfigBase::setGroup( const QStrin
     mGroup = "<default>";
   else
     mGroup = group.utf8();
-  bCheckGroup = true;
 }
 
 void KConfigBase::setGroup( const char *pGroup )
@@ -165,7 +164,6 @@ void KConfigBase::setGroup( const QCStri
     mGroup = "<default>";
   else
     mGroup = group;
-  bCheckGroup = true;
 }
 
 QString KConfigBase::group() const {
@@ -177,6 +175,45 @@ void KConfigBase::setDesktopGroup()
   mGroup = "Desktop Entry";
 }
 
+bool KConfigBase::hasKey(const QString &key) const
+{
+   return hasKey(key.utf8().data());
+}
+
+bool KConfigBase::hasKey(const char *pKey) const
+{
+  KEntryKey aEntryKey(mGroup, 0);
+  aEntryKey.c_key = pKey;
+
+  if (!locale().isNull()) {
+    // try the localized key first
+    aEntryKey.bLocal = true;
+    KEntry entry = lookupData(aEntryKey);
+    if (!entry.mValue.isNull())
+       return true;
+    aEntryKey.bLocal = false;
+  }
+
+  // try the non-localized version
+  KEntry entry = lookupData(aEntryKey);
+  return !entry.mValue.isNull();
+}
+
+bool KConfigBase::hasGroup(const QString &group) const
+{
+  return internalHasGroup( group.utf8());
+}
+
+bool KConfigBase::hasGroup(const char *_pGroup) const
+{
+  return internalHasGroup( QCString(_pGroup));
+}
+
+bool KConfigBase::hasGroup(const QCString &_pGroup) const
+{
+  return internalHasGroup( _pGroup);
+}
+
 QString KConfigBase::readEntry( const QString& pKey,
                                 const QString& aDefault ) const
 {
@@ -927,7 +964,7 @@ void KConfigBase::writeEntry( const char
   // from under us before we read. A race condition is still
   // possible but minimized.
   if( bPersistent )
-    bDirty = true;
+    setDirty(true);
 
   if (!bLocaleInitialized && KGlobal::locale())
     setLocale();
@@ -944,8 +981,7 @@ void KConfigBase::writeEntry( const char
     aEntryData.bDirty = true;
 
   // rewrite the new value
-  putData(entryKey, aEntryData, bCheckGroup);
-  bCheckGroup = false;
+  putData(entryKey, aEntryData, true);
 }
 
 void KConfigBase::writePathEntry( const QString& pKey, const QString & path,
@@ -986,7 +1022,7 @@ void KConfigBase::deleteEntry( const cha
   // classes do caching, they won't try and flush the cache out
   // from under us before we read. A race condition is still
   // possible but minimized.
-  bDirty = true;
+  setDirty(true);
 
   if (!bLocaleInitialized && KGlobal::locale())
     setLocale();
@@ -1000,8 +1036,7 @@ void KConfigBase::deleteEntry( const cha
   aEntryData.bDeleted = true;
 
   // rewrite the new value
-  putData(entryKey, aEntryData, bCheckGroup);
-  bCheckGroup = false;
+  putData(entryKey, aEntryData, true);
 }
 
 bool KConfigBase::deleteGroup( const QString& group, bool bDeep )
@@ -1013,6 +1048,8 @@ bool KConfigBase::deleteGroup( const QSt
     return aEntryMap.isEmpty();
   }
 
+  bool dirty = false;
+  bool checkGroup = true;
   // we want to remove all entries in the group
   KEntryMapIterator aIt;
   for (aIt = aEntryMap.begin(); aIt != aEntryMap.end(); ++aIt)
@@ -1023,11 +1060,13 @@ qWarning("Deleting key = %s", aIt.key().
       (*aIt).bDeleted = true;
       (*aIt).bDirty = true;
       (*aIt).mValue = 0;
-      putData(aIt.key(), *aIt, bCheckGroup);
-      bCheckGroup = false;
-      bDirty = true;
+      putData(aIt.key(), *aIt, checkGroup);
+      checkGroup = false;
+      dirty = true;
     }
   }
+  if (dirty)
+     setDirty(true);
   return true;
 }
 
@@ -1464,6 +1503,38 @@ KConfigBase::ConfigState KConfigBase::ge
     if (backEnd)
        return backEnd->getConfigState();
     return ReadOnly;
+}
+
+KConfigGroup::KConfigGroup(KConfigBase *master, const QCString &group)
+{
+  mMaster = master;
+  backEnd = 0;
+  bLocaleInitialized = true;
+  bReadOnly = mMaster->bReadOnly;
+  bExpand = false;
+  bDirty = false; // Not used
+  mGroup = group;
+  aLocaleString = mMaster->aLocaleString;
+}
+
+void KConfigGroup::deleteGroup()
+{
+  mMaster->deleteGroup(KConfigBase::group());
+}
+
+void KConfigGroup::setDirty(bool b)
+{
+  mMaster->setDirty(b);
+}
+
+void KConfigGroup::putData(const KEntryKey &_key, const KEntry &_data, bool _checkGroup)
+{
+  mMaster->putData(_key, _data, _checkGroup);
+}
+
+KEntry KConfigGroup::lookupData(const KEntryKey &_key) const
+{
+  return mMaster->lookupData(_key);
 }
 
 #include "kconfigbase.moc"
Index: kconfigbase.h
===================================================================
RCS file: /home/kde/kdelibs/kdecore/kconfigbase.h,v
retrieving revision 1.66
diff -u -3 -d -p -r1.66 kconfigbase.h
--- kconfigbase.h	2001/11/02 13:38:49	1.66
+++ kconfigbase.h	2001/11/03 06:46:31
@@ -38,6 +38,7 @@
 
 class KConfigBackEnd;
 class KConfigBasePrivate;
+class KConfigGroup;
 
 /**
  * Abstract base class for KDE configuration entries.
@@ -69,6 +70,7 @@ class KConfigBase : public QObject
 
   friend class KConfigBackEnd;
   friend class KConfigINIBackEnd;
+  friend class KConfigGroup;
 
 public:
   /**
@@ -113,7 +115,7 @@ public:
    * @param group The group to search for.
    * @returns Whether the group exists.
    */
-  virtual bool hasGroup(const QString &group) const = 0;
+  bool hasGroup(const QString &group) const;
 
   /**
    * Returns a list of groups that are known about.
@@ -923,7 +925,7 @@ public:
    * @param pKey The key to search for.
    * @return If true, the key is available.
    */
-  virtual bool hasKey( const QString& key ) const = 0;
+  bool hasKey( const QString& key ) const;
 
   /**
    * Returns a map (tree) of entries for all entries in a particular
@@ -1057,6 +1059,8 @@ protected:
    */
   virtual KEntry lookupData(const KEntryKey &_key) const = 0;
 
+  virtual bool internalHasGroup(const QCString &group) const = 0;
+
   /**
    * A back end for loading/saving to disk in a particular format.
    */
@@ -1067,9 +1071,9 @@ public:
    */
   void setGroup( const QCString &pGroup );
   void setGroup( const char *pGroup );
-  virtual bool hasGroup(const QCString &_pGroup) const = 0;
-  virtual bool hasGroup(const char *_pGroup) const = 0;
-  virtual bool hasKey( const char *pKey ) const = 0;
+  bool hasGroup(const QCString &_pGroup) const;
+  bool hasGroup(const char *_pGroup) const;
+  bool hasKey( const char *pKey ) const;
 
 protected:
   QCString readEntryUtf8( const char *pKey) const;
@@ -1090,7 +1094,6 @@ protected:
   bool bLocaleInitialized;
   bool bReadOnly;           // currently only used by KSimpleConfig
   mutable bool bExpand;     // whether dollar expansion is used
-  bool bCheckGroup;         // Flag whether to check group status
 
   KConfigBasePrivate *d;
 };
@@ -1172,6 +1175,49 @@ private:
   KConfigGroupSaver& operator=(const KConfigGroupSaver&);
 
   KConfigGroupSaverPrivate *d;
+};
+
+class KConfigGroup: public KConfigBase
+{
+public:
+   KConfigGroup(KConfigBase *master, const QCString &group);
+   
+   /**
+    * Delete all entries in the entire group
+    */
+   void deleteGroup();
+
+   // The following functions are reimplemented:
+   virtual void setDirty(bool b);
+   virtual void putData(const KEntryKey &_key, const KEntry &_data, bool _checkGroup = true);
+   virtual KEntry lookupData(const KEntryKey &_key) const;
+   
+private:
+   // Hide the following members: 
+   void setGroup() { }
+   void setDesktopGroup() { }
+   void group() { }
+   void hasGroup() { }
+   void setReadOnly(bool) { }
+   void isDirty() { }
+
+   // The following members are not used.
+   virtual QStringList groupList() const { return QStringList(); }
+   virtual void rollback(bool) { } 
+   virtual void sync() { }
+   virtual void reparseConfiguration() { }
+   virtual QMap<QString, QString> entryMap(const QString &) const 
+    { return QMap<QString,QString>(); }
+   virtual KEntryMap internalEntryMap( const QString&) const 
+    { return KEntryMap(); }
+   virtual KEntryMap internalEntryMap() const 
+    { return KEntryMap(); }
+   virtual bool internalHasGroup(const QCString &) const
+    { return false; }
+
+   void getConfigState() { }
+   
+   KConfigBase *mMaster;
 };
 
 #endif


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic