aboutsummaryrefslogtreecommitdiffstats
path: root/cad/qcad/files/patch-zzz-autosave
blob: 8f499b71b3542d3172dd9233cdf426ed7faaedd8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
diff -ru ../qcad-2.0.5.0-1-community.src.orig/qcad/src/qc_applicationwindow.cpp ./qcad/src/qc_applicationwindow.cpp
--- ../qcad-2.0.5.0-1-community.src.orig/qcad/src/qc_applicationwindow.cpp  Tue Nov 22 12:49:33 2005
+++ ./qcad/src/qc_applicationwindow.cpp Sat Aug 11 22:47:35 2007
@@ -162,6 +162,11 @@
    RS_DEBUG->print("QC_ApplicationWindow::QC_ApplicationWindow: init MDI");
     initMDI();
 
+    // Activate autosave timer
+    autosaveTimer = new QTimer(this, "autosave");
+    connect(autosaveTimer, SIGNAL(timeout()), this, SLOT(slotFileAutoSave()));
+    autosaveTimer->start(autosaveTime);
+
     // Disable menu and toolbar items
     emit windowsChanged(FALSE);
 
@@ -2133,6 +2138,9 @@
                name = w->getDocument()->getFilename();
                recentFiles->add(name);
                w->setCaption(name);
+       if (!autosaveTimer->isActive()) {
+           autosaveTimer->start(autosaveTime);
+       }
        }
         } else {
             // error
@@ -2148,6 +2156,37 @@
     QString message = tr("Saved drawing: %1").arg(name);
     statusBar()->message(message, 2000);
     commandWidget->appendHistory(message);
+}
+
+
+
+/**
+ * Autosave.
+ */
+void QC_ApplicationWindow::slotFileAutoSave() {
+    RS_DEBUG->print("QC_ApplicationWindow::slotFileAutoSave()");
+
+    statusBar()->message(tr("Auto-saving drawing..."));
+
+    QC_MDIWindow* w = getMDIWindow();
+    QString name;
+    if (w!=NULL) {
+   bool cancelled;
+   if (w->slotFileSave(cancelled, true)) {
+       // auto-save cannot be cancelled by user, so the
+       // "cancelled" parameter is a dummy
+       statusBar()->message(tr("Auto-saved drawing"), 2000);
+   } else {
+       // error
+       autosaveTimer->stop();
+       QMessageBox::information(this, QMessageBox::tr("Warning"),
+                    tr("Cannot auto-save the file\n%1\nPlease "
+                   "check the permissions.\n"
+                   "Auto-save disabled.")
+                    .arg(w->getDocument()->getAutoSaveFilename()),
+                    QMessageBox::Ok);
+   }
+    }
 }
 
 
diff -ru ../qcad-2.0.5.0-1-community.src.orig/qcad/src/qc_applicationwindow.h ./qcad/src/qc_applicationwindow.h
--- ../qcad-2.0.5.0-1-community.src.orig/qcad/src/qc_applicationwindow.h    Tue Nov 22 12:49:34 2005
+++ ./qcad/src/qc_applicationwindow.h   Sat Aug 11 22:47:35 2007
@@ -37,6 +37,7 @@
 #include <qsplitter.h>
 #include <qstatusbar.h>
 #include <qtable.h>
+#include <qtimer.h>
 #include <qtoolbar.h>
 #include <qtoolbutton.h>
 #include <qwhatsthis.h>
@@ -148,6 +149,8 @@
     void slotFileSave();
     /** saves a document under a different filename*/
     void slotFileSaveAs();
+    /** auto-save document */
+    void slotFileAutoSave();
    /** exports the document as bitmap */
    void slotFileExport();
    bool slotFileExport(const QString& name, const QString& format, 
@@ -456,6 +459,9 @@
     QAction *testResize800;
     QAction *testResize1024;
 
+    QTimer *autosaveTimer;
+
+    const static int autosaveTime = 60 * 1000; // 1 minute
 };
 
 
diff -ru ../qcad-2.0.5.0-1-community.src.orig/qcad/src/qc_mdiwindow.cpp ./qcad/src/qc_mdiwindow.cpp
--- ../qcad-2.0.5.0-1-community.src.orig/qcad/src/qc_mdiwindow.cpp  Tue Nov 22 12:49:37 2005
+++ ./qcad/src/qc_mdiwindow.cpp Sat Aug 11 22:47:35 2007
@@ -324,23 +324,32 @@
 /**
  * Saves the current file.
  *
+ * @param  isAutoSave true if this is an "autosave" operation.
+ *                    false if this is "Save" operation requested
+ *                    by the user.
  * @return true if the file was saved successfully.
  *         false if the file could not be saved or the document 
  *         is invalid.
  */
-bool QC_MDIWindow::slotFileSave(bool &cancelled) {
+bool QC_MDIWindow::slotFileSave(bool &cancelled, bool isAutoSave) {
     RS_DEBUG->print("QC_MDIWindow::slotFileSave()");
     bool ret = false;
     cancelled = false;
 
     if (document!=NULL) {
-        if (document->getFilename().isEmpty()) {
-            ret = slotFileSaveAs(cancelled);
+   if (isAutoSave) {
+       // Autosave filename is always supposed to be present.
+       // Autosave does not change the cursor.
+            ret = document->save(true);
         } else {
-            QApplication::setOverrideCursor( QCursor(Qt::WaitCursor) );
-            ret = document->save();
-            QApplication::restoreOverrideCursor();
-        }
+       if (document->getFilename().isEmpty()) {
+       ret = slotFileSaveAs(cancelled);
+       } else {
+       QApplication::setOverrideCursor( QCursor(Qt::WaitCursor) );
+       ret = document->save();
+       QApplication::restoreOverrideCursor();
+       }
+   }
     }
 
     return ret;
diff -ru ../qcad-2.0.5.0-1-community.src.orig/qcad/src/qc_mdiwindow.h ./qcad/src/qc_mdiwindow.h
--- ../qcad-2.0.5.0-1-community.src.orig/qcad/src/qc_mdiwindow.h    Tue Nov 22 12:49:37 2005
+++ ./qcad/src/qc_mdiwindow.h   Sat Aug 11 22:47:35 2007
@@ -69,7 +69,7 @@
 
     void slotFileNew();
     bool slotFileOpen(const QString& fileName, RS2::FormatType type);
-    bool slotFileSave(bool &cancelled);
+    bool slotFileSave(bool &cancelled, bool isAutoSave=false);
     bool slotFileSaveAs(bool &cancelled);
     bool slotFileClose(bool force);
     void slotFilePrint();
diff -ru ../qcad-2.0.5.0-1-community.src.orig/qcadlib/src/engine/rs_block.cpp ./qcadlib/src/engine/rs_block.cpp
--- ../qcad-2.0.5.0-1-community.src.orig/qcadlib/src/engine/rs_block.cpp    Tue Nov 22 12:52:35 2005
+++ ./qcadlib/src/engine/rs_block.cpp   Sat Aug 11 22:47:35 2007
@@ -78,10 +78,10 @@
 }
 
 
-bool RS_Block::save() {
+bool RS_Block::save(bool isAutoSave) {
     RS_Graphic* g = getGraphic();
     if (g!=NULL) {
-        return g->save();
+        return g->save(isAutoSave);
     } else {
         return false;
     }
diff -ru ../qcad-2.0.5.0-1-community.src.orig/qcadlib/src/engine/rs_block.h ./qcadlib/src/engine/rs_block.h
--- ../qcad-2.0.5.0-1-community.src.orig/qcadlib/src/engine/rs_block.h  Tue Nov 22 12:52:38 2005
+++ ./qcadlib/src/engine/rs_block.h Sat Aug 11 22:47:35 2007
@@ -128,7 +128,7 @@
     /**
      * Reimplementation from RS_Document. Saves the parent graphic document.
      */
-    virtual bool save();
+    virtual bool save(bool isAutoSave = false);
 
     /**
      * Reimplementation from RS_Document. Does nothing.
diff -ru ../qcad-2.0.5.0-1-community.src.orig/qcadlib/src/engine/rs_document.cpp ./qcadlib/src/engine/rs_document.cpp
--- ../qcad-2.0.5.0-1-community.src.orig/qcadlib/src/engine/rs_document.cpp Tue Nov 22 12:52:38 2005
+++ ./qcadlib/src/engine/rs_document.cpp    Sat Aug 11 22:47:35 2007
@@ -40,6 +40,7 @@
     RS_DEBUG->print("RS_Document::RS_Document() ");
 
     filename = "";
+    autosaveFilename = "Unnamed";
    formatType = RS2::FormatUnknown;
     setModified(false);
     RS_Color col(RS2::FlagByLayer);
diff -ru ../qcad-2.0.5.0-1-community.src.orig/qcadlib/src/engine/rs_document.h ./qcadlib/src/engine/rs_document.h
--- ../qcad-2.0.5.0-1-community.src.orig/qcadlib/src/engine/rs_document.h   Tue Nov 22 12:52:32 2005
+++ ./qcadlib/src/engine/rs_document.h  Sat Aug 11 22:47:35 2007
@@ -53,7 +53,7 @@
     virtual RS_BlockList* getBlockList() = 0;
 
     virtual void newDoc() = 0;
-    virtual bool save() = 0;
+    virtual bool save(bool isAutoSave = false) = 0;
     virtual bool saveAs(const RS_String &filename, RS2::FormatType type) = 0;
     virtual bool open(const RS_String &filename, RS2::FormatType type) = 0;
    
@@ -98,6 +98,13 @@
     }
    
     /**
+     * @return Auto-save file name of the document currently loaded.
+     */
+    RS_String getAutoSaveFilename() const {
+        return autosaveFilename;
+    }
+   
+    /**
      * Sets file name for the document currently loaded.
      */
     void setFilename(const RS_String& fn) {
@@ -136,6 +143,8 @@
     RS_Pen activePen;
     /** File name of the document or empty for a new document. */
     RS_String filename;
+   /** Auto-save file name of document. */
+   RS_String autosaveFilename;
    /** Format type */
    RS2::FormatType formatType;
 };
diff -ru ../qcad-2.0.5.0-1-community.src.orig/qcadlib/src/engine/rs_graphic.cpp ./qcadlib/src/engine/rs_graphic.cpp
--- ../qcad-2.0.5.0-1-community.src.orig/qcadlib/src/engine/rs_graphic.cpp  Tue Nov 22 12:52:32 2005
+++ ./qcadlib/src/engine/rs_graphic.cpp Sat Aug 11 22:47:57 2007
@@ -24,6 +24,8 @@
 **
 **********************************************************************/
 
+#include <qfile.h>
+#include <qfileinfo.h>
 
 #include "rs_graphic.h"
 
@@ -178,21 +180,46 @@
 /**
  * Saves this graphic with the current filename and settings.
  */
-bool RS_Graphic::save() {
+bool RS_Graphic::save(bool isAutoSave) {
 
    bool ret = false;
+
    
     RS_DEBUG->print("RS_Graphic::save");
-    RS_DEBUG->print("  file: %s", filename.latin1());
-    RS_DEBUG->print("  format: %d", (int)formatType);
-
-    RS_DEBUG->print("  export...");
-    ret = RS_FILEIO->fileExport(*this, filename, formatType);
-
-   if (ret) {
-       setModified(false);
-       layerList.setModified(false);
-       blockList.setModified(false);
+   if (isAutoSave && !isModified()) {
+       RS_DEBUG->print("  autsave and not modified => not saved");
+       ret = true;
+   } else {
+       const RS_String *actualName;
+       RS2::FormatType actualType;
+
+       actualType = formatType;
+       if (isAutoSave) {
+           actualName = new QString(autosaveFilename);
+           if (formatType == RS2::FormatUnknown) {
+               actualType = RS2::FormatDXF;
+           }
+       } else {
+           actualName = new QString(filename);
+       }
+       RS_DEBUG->print("  file: %s", actualName->latin1());
+       RS_DEBUG->print("  format: %d", (int)actualType);
+       RS_DEBUG->print("  export...");
+       ret = RS_FILEIO->fileExport(*this, *actualName, actualType);
+       delete actualName;
+
+       if (ret && !isAutoSave) {
+           setModified(false);
+           layerList.setModified(false);
+           blockList.setModified(false);
+           // Remove old autosave file
+           QFile f(autosaveFilename);
+           if (f.exists()) {
+               RS_DEBUG->print("  removing old autosave file %s",
+                               autosaveFilename.latin1());
+               f.remove();
+           }
+       }
    }
 
     RS_DEBUG->print("RS_Graphic::save ok");
@@ -210,9 +237,28 @@
     RS_DEBUG->print("RS_Graphic::saveAs");
 
     this->filename = filename;
+   RS_String *oldAutosaveName = new RS_String(autosaveFilename);
+   QFileInfo finfo(filename);
+   // Construct new autosave filename by prepending # to the filename
+   // part, using the same directory as the destination file.
+   this->autosaveFilename = finfo.dirPath() + "/#" + finfo.fileName();
    this->formatType = type;
 
-    return save();
+    bool ret = save();
+
+   if (ret) {
+       // save was successful, remove old autosave file
+       QFile f(*oldAutosaveName);
+       if (f.exists()) {
+           RS_DEBUG->print("removing old autosave file %s",
+                           oldAutosaveName->latin1());
+           f.remove();
+       }
+   }
+
+   delete oldAutosaveName;
+
+   return ret;
 }
 
 
@@ -226,6 +272,10 @@
    bool ret = false;
 
     this->filename = filename;
+   QFileInfo finfo(filename);
+   // Construct new autosave filename by prepending # to the filename
+   // part, using the same directory as the destination file.
+   this->autosaveFilename = finfo.dirPath() + "/#" + finfo.fileName();
 
     // clean all:
     newDoc();
diff -ru ../qcad-2.0.5.0-1-community.src.orig/qcadlib/src/engine/rs_graphic.h ./qcadlib/src/engine/rs_graphic.h
--- ../qcad-2.0.5.0-1-community.src.orig/qcadlib/src/engine/rs_graphic.h    Tue Nov 22 12:52:38 2005
+++ ./qcadlib/src/engine/rs_graphic.h   Sat Aug 11 22:47:35 2007
@@ -69,7 +69,7 @@
     }
 
     virtual void newDoc();
-    virtual bool save();
+    virtual bool save(bool isAutoSave = false);
     virtual bool saveAs(const RS_String& filename, RS2::FormatType type);
     virtual bool open(const RS_String& filename, RS2::FormatType type);