fix KSaveFile::open

Matthias Kretz kretz at kde.org
Mon Nov 19 10:31:56 GMT 2007


Hi,

as -Woverloaded-virtual hinted, KSaveFile hides the virtual open(OpenMode) 
function (from QIODevice) and "replaces" it with a open() function. As long 
as code is only using the KSaveFile interface everything's fine, but consider 
a place where code like this

  QFile *device = new QFile(filename);
  saveToDevice(device);
....
void saveToDevice(QIODevice *device)
{
  device->open(QIODevice::WriteOnly | QIODevice::Truncate);
  ...

is changed to

  QFile *device = new KSaveFile(filename);
  saveToDevice(device);

After all KSaveFile is advertised as a QFile drop-in replacement. The pasted 
patch makes sure that a call to the virtual open function will work with a 
KSaveFile object regardless of the interface you're looking at.

Ok, to commit? (And yes, I'll add dox)

Index: io/ksavefile.cpp
===================================================================
--- io/ksavefile.cpp    (revision 738067)
+++ io/ksavefile.cpp    (working copy)
@@ -78,6 +78,11 @@

 bool KSaveFile::open()
 {
+    return open(QIODevice::ReadWrite);
+}
+
+bool KSaveFile::open(OpenMode flags)
+{
     if ( d->realFileName.isNull() ) {
         d->error=QFile::OpenError;
         d->errorString=i18n("No target filename has been given.");
@@ -131,7 +136,7 @@

     //Open oursleves with the temporary file
     QFile::setFileName(tempFile.fileName());
-    if ( !QFile::open(QIODevice::ReadWrite) ) {
+    if (!QFile::open(flags)) {
         tempFile.setAutoRemove(true);
         return false;
     }
Index: io/ksavefile.h
===================================================================
--- io/ksavefile.h      (revision 738067)
+++ io/ksavefile.h      (working copy)
@@ -154,6 +154,7 @@
      * @return true if successful, or false if an error has occurred.
      */
     bool open();
+    bool open(OpenMode flags);

     /**
      * @brief Discard changes without affecting the target file.

-- 
________________________________________________________
Matthias Kretz (Germany)                            <><
http://Vir.homelinux.org/
MatthiasKretz at gmx.net, kretz at kde.org,
Matthias.Kretz at urz.uni-heidelberg.de
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part.
URL: <http://mail.kde.org/pipermail/kde-core-devel/attachments/20071119/0618818b/attachment.sig>


More information about the kde-core-devel mailing list