aboutsummaryrefslogtreecommitdiff
path: root/ch23
diff options
context:
space:
mode:
authorJan Sucan <jan@jansucan.com>2025-09-14 16:17:03 +0200
committerJan Sucan <jan@jansucan.com>2025-09-14 16:17:03 +0200
commitad4cf7db4cd61d01f1e2191d1d69edd5916d6e88 (patch)
tree9497eb2ce10df6a17679f7110aadf159a566b7cb /ch23
parentcbb801e06adc03fe5baddb3288d070e39d2cf878 (diff)
23_a_1: Add solution
Diffstat (limited to 'ch23')
-rw-r--r--ch23/23_a_1/23_a_1.txt1
-rw-r--r--ch23/23_a_1/PodMainGUI.hs47
2 files changed, 30 insertions, 18 deletions
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"