Jack Jansen | 310c657 | 1995-11-14 11:32:53 +0000 | [diff] [blame] | 1 | """Sample program handling InterSLIP control and showing off EasyDialogs, |
| 2 | Res and Dlg in the process""" |
| 3 | |
| 4 | import EasyDialogs |
| 5 | import Res |
| 6 | import Dlg |
| 7 | import sys |
| 8 | import interslip |
| 9 | # |
| 10 | # Definitions for our resources |
| 11 | ID_MAIN=512 |
| 12 | |
| 13 | ITEM_CONNECT=1 |
| 14 | ITEM_DISCONNECT=2 |
| 15 | ITEM_UPDATE=3 |
| 16 | ITEM_QUIT=4 |
| 17 | ITEM_STATUS=5 |
| 18 | ITEM_MESSAGE=6 |
| 19 | |
| 20 | status2text = ["<idle>", "<wait-modem>", "<dialling>", "<logging in>", |
| 21 | "<connected>", "<disconnecting>"] |
| 22 | |
| 23 | |
| 24 | def 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 | |
| 39 | def 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 | |
| 67 | def 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 | |
| 74 | def 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 | |
| 81 | def 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 | |
| 92 | main() |