Re: patch: simple session management

Frank Osterfeld <[email protected]> Tue, 16 Mar 2004 11:33:58 +0100
Newsgroups gmane.comp.kde.devel.knowit
Message-ID <[email protected]>
--Boundary-00=_WgtVA2X1ClaaYMf
Content-Type: text/plain;
  charset="iso-8859-15"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

Here is a better patch, which shouldn't set knowit->filename to "" after 
restore anymore...
It took some time until I realized that "open(filename)" doesn't work because
the local fname is just a reference to filename then and following happens in 
open():

| reset(); // filename (_same_ object as fname) ist set to "" 
| ...
| filename = fname // filename = fname = "" 

I think open() should work with a copy of the passed KURL to prevent this. (I 
used open(KURL(filename)) as a workaround)

Other changes to last patch:

- reopening last file is disabled when a session is restored
- a hardcoded "untitled.kno" is replaced with Knowit::Untitled

Regards,

Frank (who should really learn now instead of playing with knowit 8-) )

--Boundary-00=_WgtVA2X1ClaaYMf
Content-Type: text/x-diff;
  charset="iso-8859-15";
  name="session2.diff"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment;
	filename="session2.diff"

diff -u knowit-0.10beta4-original/src/knowit.cpp knowit-0.10beta4/src/knowit.cpp
--- knowit-0.10beta4-original/src/knowit.cpp	2004-03-14 18:11:36.000000000 +0100
+++ knowit-0.10beta4/src/knowit.cpp	2004-03-16 11:10:44.000000000 +0100
@@ -270,7 +270,8 @@
           }
    args->clear();
 
-   if (!opened && Options.reopen && filename.path() != "")
+   // disable reopen when session was restored
+   if (!kapp->isRestored() && !opened && Options.reopen && filename.path() != "")
       open(KURL(filename));
 
    slotActionUpdate();
@@ -441,7 +442,8 @@
    if (!shuttingDown) {
       /* brutally closed by session manager */
       if (modified())
-         save(filename.isEmpty() ? i18n("untitled.kno") : filename);
+         // Untitled instead of "untitled.kno" (?)
+         save(filename.isEmpty() ? Untitled : filename);
       saveOptions();
    }
    return true;
@@ -500,8 +502,9 @@
    resize(config->readSizeEntry("Geometry", &defSize));
    actionCollection()->readShortcutSettings("Keys", config);
    actionRecent->loadEntries(config,"Recent Files");
-   if (!oldfile.isEmpty())
-      filename = KURL(oldfile);
+   // ignore oldfile, if session was restored
+   if (!kapp->isRestored() && !oldfile.isEmpty())
+     filename = KURL(oldfile);
    applyOptions(Options);
 }
 
@@ -1269,3 +1272,15 @@
    kapp->clipboard()->setText(link.link, QClipboard::Selection);
 }
 
+   
+void Knowit::saveProperties(KConfig* config)
+{
+  config->writeEntry("File", filename.path());
+}
+   
+void Knowit::readProperties(KConfig* config)
+{
+  filename = KURL(config->readEntry("File"));
+  if (!filename.isEmpty())
+    open(KURL(filename));               
+}
diff -u knowit-0.10beta4-original/src/knowit.h knowit-0.10beta4/src/knowit.h
--- knowit-0.10beta4-original/src/knowit.h	2004-03-14 18:11:34.000000000 +0100
+++ knowit-0.10beta4/src/knowit.h	2004-03-16 11:14:06.000000000 +0100
@@ -82,6 +82,13 @@
     KAction *actionCut, *actionCopy, *actionUndo, *actionRedo;
 
     enum {StatusText = 1, StatusOvr = 2};    
+    
+  protected:
+    /** session management: saves state of application */    
+    virtual void saveProperties(KConfig* config);
+    /** session management: reloads state */
+    virtual void readProperties(KConfig* config);
+   
   public:
     /** Constructor of main window */
     Knowit(QWidget* parent=0, const char *name=0);
diff -u knowit-0.10beta4-original/src/main.cpp knowit-0.10beta4/src/main.cpp
--- knowit-0.10beta4-original/src/main.cpp	2004-03-14 18:11:36.000000000 +0100
+++ knowit-0.10beta4/src/main.cpp	2004-03-16 09:14:48.000000000 +0100
@@ -44,8 +44,11 @@
   KCmdLineArgs::addCmdLineOptions( options ); 
 
   KnowitApplication a(true, true);
-
-  Knowit *knowit = new Knowit(0, "KnowIt");
-  a.setMainWidget(knowit);
+      
+  if (kapp->isRestored())
+    RESTORE(Knowit)
+  else
+    a.setMainWidget(new Knowit(0, "KnowIt"));
+    
   return a.exec();
 }

--Boundary-00=_WgtVA2X1ClaaYMf--


-------------------------------------------------------
This SF.Net email is sponsored by: IBM Linux Tutorials
Free Linux tutorial presented by Daniel Robbins, President and CEO of
GenToo technologies. Learn everything from fundamentals to system
administration.http://ads.osdn.com/?ad_id=1470&alloc_id=3638&op=click