[gnome-chess] Fix bad pgn load behaviour - invalid moves in pgn



commit 16037c46f80bf765958515c4b9812d3a9da1616a
Author: Sahil Sareen <sahil sareen hotmail com>
Date:   Sun May 3 22:56:48 2015 +0530

    Fix bad pgn load behaviour - invalid moves in pgn
    
    Don't load a pgn with invalid moves,
    prompt a dialog to the player, and
    start a new game.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=746218

 lib/chess-game.vala  |    7 +++++--
 src/gnome-chess.vala |   25 ++++++++++++++++++++++++-
 2 files changed, 29 insertions(+), 3 deletions(-)
---
diff --git a/lib/chess-game.vala b/lib/chess-game.vala
index b72cfff..aceb92e 100644
--- a/lib/chess-game.vala
+++ b/lib/chess-game.vala
@@ -87,7 +87,7 @@ public class ChessGame : Object
         }
     }
 
-    public ChessGame (string fen = STANDARD_SETUP, string[]? moves = null)
+    public ChessGame (string fen = STANDARD_SETUP, string[]? moves = null) throws PGNError
     {
         is_started = false;
         move_stack.prepend (new ChessState (fen));
@@ -98,7 +98,10 @@ public class ChessGame : Object
             for (var i = 0; i < moves.length; i++)
             {
                 if (!do_move (current_player, moves[i], true))
-                    warning ("Invalid move %s", moves[i]);
+                {
+                    /* Message when the game cannot be loaded due to an invalid move in the file. */
+                    throw new PGNError.LOAD_ERROR (_("Failed to load PGN: move %s is invalid."), moves[i]);
+                }
             }
         }
 
diff --git a/src/gnome-chess.vala b/src/gnome-chess.vala
index fd81274..1c2a6e6 100644
--- a/src/gnome-chess.vala
+++ b/src/gnome-chess.vala
@@ -450,7 +450,17 @@ public class ChessApplication : Gtk.Application
             else
                 warning ("Chess game has SetUp tag but no FEN tag");
         }
-        game = new ChessGame (fen, moves);
+
+        try
+        {
+            game = new ChessGame (fen, moves);
+        }
+        catch (Error e)
+        {
+            run_invalid_move_dialog (e.message);
+            start_new_game ();
+            return;
+        }
 
         /*
          * We only support simple timeouts
@@ -2155,6 +2165,19 @@ public class ChessApplication : Gtk.Application
         invalid_pgn_dialog.destroy ();
     }
 
+    private void run_invalid_move_dialog (string error_message)
+    {
+        var invalid_move_dialog = new Gtk.MessageDialog (window,
+                                                        Gtk.DialogFlags.MODAL,
+                                                        Gtk.MessageType.ERROR,
+                                                        Gtk.ButtonsType.NONE,
+                                                        error_message);
+        invalid_move_dialog.add_button (_("_OK"), Gtk.ResponseType.OK);
+
+        invalid_move_dialog.run ();
+        invalid_move_dialog.destroy ();
+    }
+
     private void about_response_cb (int response_id)
     {
         about_dialog.destroy ();


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