blob: 14fc59c86c94f90f9ac335314540c336af0227e6 [file] [log] [blame]
Jack Jansen310c6571995-11-14 11:32:53 +00001"""Sample program handling InterSLIP control and showing off EasyDialogs,
2Res and Dlg in the process"""
3
4import EasyDialogs
5import Res
6import Dlg
7import sys
8import interslip
9#
10# Definitions for our resources
11ID_MAIN=512
12
13ITEM_CONNECT=1
14ITEM_DISCONNECT=2
15ITEM_UPDATE=3
16ITEM_QUIT=4
17ITEM_STATUS=5
18ITEM_MESSAGE=6
19
20status2text = ["<idle>", "<wait-modem>", "<dialling>", "<logging in>",
21 "<connected>", "<disconnecting>"]
22
23
24def main():
25 """Main routine: open resourcefile, open interslip, call dialog handler"""
26 try:
27 Res.OpenResFile("InterslipControl-1.rsrc")
28 except Res.Error, arg:
29 EasyDialogs.Message("Cannot open resource file InterslipControl-1.rsrc: "+
30 arg[1])
31 sys.exit(1)
32 try:
33 interslip.open()
34 except interslip.error, arg:
35 EasyDialogs.Message("Cannot open interslip: "+arg[1])
36 sys.exit(1)
37 do_dialog()
38
39def do_dialog():
40 """Post dialog and handle user interaction until quit"""
41 my_dlg = Dlg.GetNewDialog(ID_MAIN, -1)
42 while 1:
43 n = Dlg.ModalDialog(None)
44 if n == ITEM_CONNECT:
45 do_connect()
46 elif n == ITEM_DISCONNECT:
47 do_disconnect()
48 elif n == ITEM_UPDATE:
49 status, msg = do_status()
50
51 # Convert status number to a text string
52 try:
53 txt = status2text[status]
54 except IndexError:
55 txt = "<unknown state %d>"%status
56
57 # Set the status text field
58 tp, h, rect = my_dlg.GetDialogItem(ITEM_STATUS)
59 Dlg.SetDialogItemText(h, txt)
60
61 # Set the message text field
62 tp, h, rect = my_dlg.GetDialogItem(ITEM_MESSAGE)
63 Dlg.SetDialogItemText(h, msg)
64 elif n == ITEM_QUIT:
65 break
66
67def do_connect():
68 """Connect, posting error message in case of failure"""
69 try:
70 interslip.connect()
71 except interslip.error, arg:
72 EasyDialogs.Message("Cannot connect: "+arg[1])
73
74def do_disconnect():
75 """Disconnect, posting error message in case of failure"""
76 try:
77 interslip.disconnect()
78 except interslip.error, arg:
79 EasyDialogs.Message("Cannot disconnect: "+arg[1])
80
81def do_status():
82 """Get status as (state_index, message),
83 posting error message in case of failure"""
84 try:
85 status, msgnum, msg = interslip.status()
86 except interslip.error, arg:
87 EasyDialogs.Message("Cannot get status: "+arg[1])
88 return 0, ''
89 return status, msg
90
91
92main()