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

List:       kde-core-devel
Subject:    Re: KWrite Patch for #14817
From:       Martijn Klingens <mklingens () yahoo ! com>
Date:       2001-01-17 23:23:40
[Download RAW message or body]

Hmm.. I seem to forget adding those diffs :(

Here it is (Unified, for those who remember my earlier stupidities :-) )

Martijn

["kwrite-patch.diff" (text/plain)]

? kwrite.kdevprj
Index: kwdoc.cpp
===================================================================
RCS file: /home/kde/kdebase/kwrite/kwdoc.cpp,v
retrieving revision 1.96
diff -b -u -p -r1.96 kwdoc.cpp
--- kwdoc.cpp	2001/01/16 00:15:46	1.96
+++ kwdoc.cpp	2001/01/17 23:07:59
@@ -404,6 +404,24 @@ bool KWriteDoc::isReadOnly() const {
   return readOnly;
 }
 
+void KWriteDoc::setNewDoc( bool m )
+{
+//  KTextEditor::View *view;
+
+  if ( m != newDoc )
+  {
+    newDoc = m;
+////    if (readOnly) recordReset();
+//    for (view = m_views.first(); view != 0L; view = m_views.next() ) {
+//      emit static_cast<KWrite *>( view )->newStatus();
+//    }
+  }
+}
+
+bool KWriteDoc::isNewDoc() const {
+  return newDoc;
+}
+
 void KWriteDoc::setModified(bool m) {
   KTextEditor::View *view;
 
Index: kwdoc.h
===================================================================
RCS file: /home/kde/kdebase/kwrite/kwdoc.h,v
retrieving revision 1.69
diff -b -u -p -r1.69 kwdoc.h
--- kwdoc.h	2001/01/16 00:15:47	1.69
+++ kwdoc.h	2001/01/17 23:08:02
@@ -150,6 +150,8 @@ class KWriteDoc : public KTextEditor::Do
     int tabWidth() {return tabChars;}
     void setReadOnly(bool);
     bool isReadOnly() const;
+    void setNewDoc( bool );
+    bool isNewDoc() const;
     virtual void setReadWrite( bool );
     virtual bool isReadWrite() const;
     virtual void setModified(bool);
@@ -356,6 +358,8 @@ class KWriteDoc : public KTextEditor::Do
     bool m_singleSelection; // false: windows-like, true: X11-like
 
     bool readOnly;
+    bool newDoc;          // True if the file is a new document (used to determine whether
+                          // to check for overwriting files on save)
     bool modified;
 
     int foundLine;
Index: kwview.cpp
===================================================================
RCS file: /home/kde/kdebase/kwrite/kwview.cpp,v
retrieving revision 1.150
diff -b -u -p -r1.150 kwview.cpp
--- kwview.cpp	2001/01/16 00:15:47	1.150
+++ kwview.cpp	2001/01/17 23:08:21
@@ -2184,6 +2184,10 @@ bool KWrite::writeFile(const QString &na
 
 
 void KWrite::loadURL(const KURL &url, int flags) {
+/*
+    TODO: Add newDoc code for non-local files. Currently this is not supported there
+          - Martijn Klingens
+*/
   KURL u(url);
 
   if (u.isMalformed()) {
@@ -2224,6 +2228,7 @@ void KWrite::loadURL(const KURL &url, in
         kWriteDoc->setURL( url, !(flags & KWriteView::lfNoAutoHl ) );
         kWriteDoc->updateViews();
         emit statusMsg( i18n( "New File : %1" ).arg( url.fileName() ) );
+        kWriteDoc->setNewDoc( true ); // File is new, check for overwrite!
       }
     }
   }
@@ -2231,6 +2236,10 @@ void KWrite::loadURL(const KURL &url, in
 
 
 void KWrite::writeURL(const KURL &url, int ) {
+/*
+    TODO: Add newDoc code for non-local files. Currently this is not supported there
+          - Martijn Klingens
+*/
 
     // url
     emit statusMsg(i18n("Saving..."));
@@ -2292,6 +2301,7 @@ void KWrite::writeURL(const KURL &url, i
   {
       kWriteDoc->setModified( false );
       emit statusMsg( i18n( "Wrote %1" ).arg( url.fileName() ) );
+      kWriteDoc->setNewDoc( false ); // File is not new anymore
   }
 }
 
@@ -2407,14 +2417,51 @@ void KWrite::insertFile() {
 }
 
 KWrite::fileResult KWrite::save() {
+  int query = KMessageBox::Yes;
   if (isModified()) {
     if (!kWriteDoc->url().fileName().isEmpty() && ! isReadOnly()) {
+      // If document is new but has a name, check if saving it would
+      // overwrite a file that has been created since the new doc
+      // was created:
+      if( kWriteDoc->isNewDoc() )
+      {
+        query = checkOverwrite( kWriteDoc->url() );
+        if( query == KMessageBox::Cancel )
+          return CANCEL;
+      }
+      if( query == KMessageBox::Yes )
       writeURL(kWriteDoc->url(),!(KWriteView::lfNoAutoHl));
-    } else return saveAs();
+      else  // Do not overwrite already existing document:
+        return saveAs();
+    } // New, unnamed document:
+    else
+      return saveAs();
   } else emit statusMsg(i18n("No changes need to be saved"));
   return OK;
 }
 
+/*
+ * Check if the given URL already exists. Currently used by both save() and saveAs()
+ *
+ * Asks the user for permission and returns the message box result and defaults to
+ * KMessageBox::Yes in case of doubt
+ */
+int KWrite::checkOverwrite( KURL u )
+{
+  int query = KMessageBox::Yes;
+
+  if( u.isLocalFile() )
+  {
+    QFileInfo info;
+    QString name( u.path() );
+    info.setFile( name );
+    if( info.exists() )
+      query = KMessageBox::warningYesNoCancel( this,
+        i18n( "A Document with this Name already exists.\nDo you want to overwrite it?" ) );
+  }
+  return query;
+}
+
 KWrite::fileResult KWrite::saveAs() {
   KURL url;
   int query;
@@ -2425,7 +2472,7 @@ KWrite::fileResult KWrite::saveAs() {
     url = KFileDialog::getSaveURL(kWriteDoc->url().url(), QString::null,this);
     if (url.isEmpty()) return CANCEL;
 
-    KURL u(url);
+/*    KURL u(url);
     if (u.isLocalFile()) {
       QFileInfo info;
       QString name(u.path());
@@ -2433,9 +2480,12 @@ KWrite::fileResult KWrite::saveAs() {
       if (info.exists()) {
         query = KMessageBox::warningYesNo(this,
           i18n("A Document with this Name already exists.\nDo you want to overwrite it?"));
-      }
     }
-  } while (query == KMessageBox::No);
+    }*/
+    query = checkOverwrite( url );
+  } while (query != KMessageBox::Yes);
+  if( query == KMessageBox::Cancel )
+    return CANCEL;
 
   writeURL(url);
   kWriteDoc->setURL( url, false );
Index: kwview.h
===================================================================
RCS file: /home/kde/kdebase/kwrite/kwview.h,v
retrieving revision 1.90
diff -b -u -p -r1.90 kwview.h
--- kwview.h	2001/01/16 21:39:15	1.90
+++ kwview.h	2001/01/17 23:08:26
@@ -540,6 +540,14 @@ class KWrite : public KTextEditor::View,
 
     KTempFile *m_tempSaveFile;
 
+    /*
+     * Check if the given URL already exists. Currently used by both save() and saveAs()
+     *
+     * Asks the user for permission and returns the message box result and defaults to
+     * KMessageBox::Yes in case of doubt
+     */
+    int checkOverwrite( KURL u );
+
 //text access
   public:
      /**


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

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