gnome-games r7646 - in branches/gnome-2-22/glchess: . src/lib



Author: rancell
Date: Sun May 11 08:54:15 2008
New Revision: 7646
URL: http://svn.gnome.org/viewvc/gnome-games?rev=7646&view=rev

Log:
Correctly move games from the history (~/.gnome2/glchess/history) when the user selects a file to save to.
	  


Modified:
   branches/gnome-2-22/glchess/ChangeLog
   branches/gnome-2-22/glchess/src/lib/display.py
   branches/gnome-2-22/glchess/src/lib/history.py
   branches/gnome-2-22/glchess/src/lib/main.py

Modified: branches/gnome-2-22/glchess/src/lib/display.py
==============================================================================
--- branches/gnome-2-22/glchess/src/lib/display.py	(original)
+++ branches/gnome-2-22/glchess/src/lib/display.py	Sun May 11 08:54:15 2008
@@ -520,30 +520,20 @@
 
     def save(self, fileName = None):
         """Called by ui.ViewFeedback"""
-        if fileName is None:
-            fileName = self.game.fileName
-            assert(fileName is not None)
+        # If filename supplied take out of the history
+        if fileName is not None:
+            if self.game.inHistory:
+                self.game.inHistory = False
+                if self.game.fileName is not None:
+                    self.game.application.history.rename(self.game.fileName, fileName)
+            self.game.fileName = fileName
+        return self.game.save()
 
-        try:
-            f = file(fileName, 'w')
-        except IOError, e:
-            return e.args[1]
-        
-        self.game.application.logger.addLine('Saving game %s to %s' % (repr(self.game.name), fileName))
-
-        pgnGame = chess.pgn.PGNGame()
-        self.game.toPGN(pgnGame)
-            
-        lines = pgnGame.getLines()
-        for line in lines:
-            f.write(line + '\n')
-        f.write('\n')
-        f.close()
-        
-        self.game.fileName = fileName
-        
     def getFileName(self):
         """Called by ui.ViewFeedback"""
+        # If in the history then prompt for a new name
+        if self.game.inHistory:
+            return None
         return self.game.fileName
         
     def resign(self):

Modified: branches/gnome-2-22/glchess/src/lib/history.py
==============================================================================
--- branches/gnome-2-22/glchess/src/lib/history.py	(original)
+++ branches/gnome-2-22/glchess/src/lib/history.py	Sun May 11 08:54:15 2008
@@ -93,6 +93,28 @@
         
         return fileName
     
+    def rename(self, oldName, newName):
+        try:
+            os.unlink(oldName)
+        except OSError:
+            print 'Failed to remove game from history'
+
+        try:
+            f = file(UNFINISHED_FILE, 'r')
+            lines = f.readlines()
+            f.close()
+            
+            f = file(UNFINISHED_FILE, 'w')
+            for line in lines:
+                l = line.strip()
+                if l == oldName:
+                    f.write(newName + '\n')
+                else:
+                    f.write(l + '\n')
+            f.close()
+        except IOError:
+            print 'Failed to update unfinished list'
+    
     def save(self, g, fileName):
         """Save a game in the history.
         
@@ -127,7 +149,7 @@
             f = file(UNFINISHED_FILE, 'w')
             for line in lines:
                 l = line.strip()
-                if l == fileName and result != chess.pgn.RESULT_INCOMPLETE:
+                if l == fileName and result == chess.pgn.RESULT_INCOMPLETE:
                     continue
                 f.write(l + '\n')
             if result == chess.pgn.RESULT_INCOMPLETE:

Modified: branches/gnome-2-22/glchess/src/lib/main.py
==============================================================================
--- branches/gnome-2-22/glchess/src/lib/main.py	(original)
+++ branches/gnome-2-22/glchess/src/lib/main.py	Sun May 11 08:54:15 2008
@@ -292,12 +292,26 @@
 
     def save(self):
         """Save this game"""
-        if len(self.getMoves()) < 2:
-            return
         pgnGame = chess.pgn.PGNGame()
         self.toPGN(pgnGame)
-        self.application.history.save(pgnGame, self.fileName)
+        if self.inHistory:
+            # Don't bother if haven't made any significant moves
+            if len(self.getMoves()) < 2:
+                return
+            self.application.history.save(pgnGame, self.fileName)
+        else:
+            try:
+                f = file(self.fileName, 'w')
+                lines = pgnGame.getLines()
+                for line in lines:
+                    f.write(line + '\n')
+                f.write('\n')
+                f.close()
+            except IOError, e:
+                return e.args[1]
+
         self.setNeedsSaving(False)
+        self.application.logger.addLine('Saved game %s to %s' % (repr(self.name), self.fileName))
         
 class UI(ui.UIFeedback):
     """



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]