From ad4cf7db4cd61d01f1e2191d1d69edd5916d6e88 Mon Sep 17 00:00:00 2001 From: Jan Sucan Date: Sun, 14 Sep 2025 16:17:03 +0200 Subject: 23_a_1: Add solution --- ch23/23_a_1/23_a_1.txt | 1 + ch23/23_a_1/PodMainGUI.hs | 47 +++++++++++++++++++++++++++++------------------ 2 files changed, 30 insertions(+), 18 deletions(-) create mode 100644 ch23/23_a_1/23_a_1.txt (limited to 'ch23/23_a_1') diff --git a/ch23/23_a_1/23_a_1.txt b/ch23/23_a_1/23_a_1.txt new file mode 100644 index 0000000..7b594a0 --- /dev/null +++ b/ch23/23_a_1/23_a_1.txt @@ -0,0 +1 @@ +Present a helpful GUI error message if the call to xmlNew returns Nothing. diff --git a/ch23/23_a_1/PodMainGUI.hs b/ch23/23_a_1/PodMainGUI.hs index e557d8b..6c7190d 100644 --- a/ch23/23_a_1/PodMainGUI.hs +++ b/ch23/23_a_1/PodMainGUI.hs @@ -47,28 +47,39 @@ main gladepath = withSocketsDo $ handleSqlError $ timeoutAddFull (yield >> return True) priorityDefaultIdle 100 - -- Load the GUI from the Glade file - gui <- loadGlade gladepath - - -- Connect to the database - dbh <- connect "pod.db" - - -- Set up our events - connectGui gui dbh - - -- Run the GTK+ main loop; exits after GUI is done - mainGUI + -- Load XML from glade path + xmlGlade <- xmlNew gladepath + case xmlGlade of + Nothing -> do + let errorMsg = "Cannot load glade file " ++ gladepath + d <- messageDialogNew Nothing -- No parent window + [] -- No dialog flags + MessageError + ButtonsClose + errorMsg + windowSetTitle d "Error" + dialogRun d + return () + Just xml -> do + -- Load the GUI from the Glade file + gui <- loadGlade xml + + -- Connect to the database + dbh <- connect "pod.db" + + -- Set up our events + connectGui gui dbh + + -- Run the GTK+ main loop; exits after GUI is done + mainGUI - -- Disconnect from the database at the end - disconnect dbh + -- Disconnect from the database at the end + disconnect dbh {-- /snippet main --} {-- snippet loadGlade --} -loadGlade gladepath = - do -- Load XML from glade path. - -- Note: crashes with a runtime error on console if fails! - Just xml <- xmlNew gladepath - +loadGlade xml = + do -- Load main window mw <- xmlGetWidget xml castToWindow "mainWindow" -- cgit v1.2.3