source: trunk/debathena/debathena/moira-gui/xmoira @ 25552

Revision 25552, 7.4 KB checked in by jdreed, 12 years ago (diff)
In moira-gui: * Fail if we can't auth to moira.
  • Property svn:executable set to *
Line 
1#!/usr/bin/python
2
3
4import gtk
5import gtk.glade
6import os
7import sys
8import pwd
9import re
10
11moiraClientName = "xmoira"
12if os.path.exists("xmoira.glade"):
13    gladeFile = "xmoira.glade"
14else:
15    gladeFile = "/usr/share/debathena-moira-gui/xmoira.glade"
16
17import moira
18from _moira import MoiraException
19import mrclient
20
21class XMoira():
22    def __init__(self):
23        self.running=False
24        self.widgets = gtk.glade.XML(gladeFile)
25        self.window = self.widgets.get_widget("mainWindow")
26        self.widgets.signal_autoconnect(self)
27        self.clientName = moiraClientName
28        self.connected = False
29        try:
30            self.currentUser = mrclient.krb_user()
31        except mrclient.MoiraClientException:
32            self.errorDialog("Unable to obtain username from Kerberos.\nDo you have valid tickets?", True)
33            sys.exit(255) # We're not in a main loop, don't call gtk.main_quit()
34        self.window.show()
35        self.widgets.get_widget("lblUserName").set_text("User: " + self.currentUser)
36        self.staleTabs = {"tabChpobox": True,
37                          "tabChfn": True,
38                          "tabChsh": True}
39        self.tabCallbacks = {"tabChpobox": self.refreshChpoboxTab,
40                             "tabChfn": self.refreshChfnTab,
41                             "tabChsh": self.refreshChshTab}
42
43        self.radioBtnQueries = {"rbChpoboxPobox": ('spop', None),
44                                "rbChpoboxSplit": ('spob', 'SPLIT'),
45                                "rbChpoboxSmtp": ('spob', 'SMTP')}
46        self.pageChanged(self.widgets.get_widget("nbMain"), None, 0)
47
48
49    def errorDialog(self, errstr, fatal=False):
50        button = gtk.BUTTONS_OK
51        if fatal:
52            button = gtk.BUTTONS_CLOSE
53        msg = gtk.MessageDialog(self.window, gtk.DIALOG_MODAL,
54                                gtk.MESSAGE_ERROR, button,
55                                "Error: %s" % errstr)
56        msg.set_title("Error")
57        msg.run()
58        msg.destroy()
59
60    def validateEmail(self, str):
61        try:
62            currUser = mrclient.krb_user()
63        except mrclient.MoiraClientException, e:
64            self.errorDialog(e.args[1])
65            return None
66        try:
67            validatedStr = mrclient.validate_pobox_smtp(currUser, str)
68        except mrclient.MoiraClientException, e:
69            self.errorDialog(e.args[1])
70            return None
71        return validatedStr
72
73    def applyChsh(self, button):
74        print self.widgets.get_widget('cbChshUnixShell').child.get_text()
75        print self.widgets.get_widget('cbChshWindowsShell').child.get_text()
76
77
78    def applyChfn(self, button):
79        argList = {}
80        for textEntry in self.widgets.get_widget_prefix("entChfn"):
81            mrfield = textEntry.name.replace('entChfn_', '')
82            argList[mrfield] = textEntry.get_text()
83        argList['login'] = self.currentUser
84        result = None
85        try:
86            result = moira.query('ufbl', None, **argList)
87        except MoiraException, e:
88            self.errorDialog(e.args[1])
89        if result == []:
90            msg = gtk.MessageDialog(self.window, gtk.DIALOG_MODAL,
91                                    gtk.MESSAGE_INFO, gtk.BUTTONS_OK,
92                                    "Your finger information has been updated.")
93            msg.set_title("Confirmation")
94            msg.run()
95            msg.destroy()
96           
97        self.refreshChfnTab()
98       
99    def applyChpobox(self, button):
100        for radioButton in self.widgets.get_widget_prefix("rbChpobox"):
101            if radioButton.get_active():
102                (query, type) = self.radioBtnQueries[radioButton.name]
103                textEntry = self.widgets.get_widget(radioButton.name.replace("rb", "ent"))
104                if type == None:
105                    self.query(query, mrclient.krb_user())
106                else:
107                    if textEntry and not textEntry.get_text():
108                        self.errorDialog("You did not enter an e-mail address")
109                        return
110                    validated = self.validateEmail(textEntry.get_text())
111                    if not validated:
112                        return
113                    self.query(query, self.currentUser, type, validated)
114                self.refreshChpoboxTab()
115
116
117    def chpoboxToggleHandler(self, button):
118        textEntry = self.widgets.get_widget(button.name.replace("rb", "ent"))
119        if textEntry != None:
120            textEntry.set_property("visible", button.get_active())
121
122    def refreshChfnTab(self):
123        gfbl = self.query('gfbl', self.currentUser)[0]
124        for textEntry in self.widgets.get_widget_prefix("entChfn"):
125            textEntry.set_text(gfbl[textEntry.name.replace('entChfn_', '')])
126        self.widgets.get_widget('lblChfnLastUpdated').set_text("Last modified %s by %s" % (gfbl['modtime'], gfbl['modby'].replace('@ATHENA.MIT.EDU', '')))
127
128    def refreshChshTab(self):
129        gual = self.query('gual', self.currentUser)[0]
130        self.widgets.get_widget('cbChshUnixShell').child.set_text(gual['shell'])
131        self.widgets.get_widget('cbChshWindowsShell').child.set_text(gual['winconsoleshell'])
132
133
134    def refreshChpoboxTab(self):
135        gpob = self.query('gpob', self.currentUser)[0]
136        english = " ".join((gpob['type'], gpob['address']))
137        if gpob['type'] == "IMAP" or gpob['type'] == "EXCHANGE":
138            english = "Your mail is not forwarded."
139            self.widgets.get_widget("rbChpoboxPobox").set_active(True)
140        if gpob['type'] == "SMTP":
141            english = "Your mail is being forwarded to %s." % gpob['box']
142            self.widgets.get_widget("rbChpoboxSmtp").set_active(True)
143            self.widgets.get_widget("entChpoboxSmtp").set_text(gpob['box'])
144        if gpob['type'] == "SPLIT":
145            english = "Your mail is being split between your MIT account\nand %s." % gpob['box']
146            self.widgets.get_widget("rbChpoboxSplit").set_active(True)
147            self.widgets.get_widget("entChpoboxSplit").set_text(gpob['box'])
148        self.widgets.get_widget("lblChpoboxCurrentSetting").set_text(english)
149
150    def pageChanged(self, notebook, page, page_num):
151        tabName = notebook.get_nth_page(page_num).name
152        if not self.staleTabs[tabName]:
153            return
154        self.tabCallbacks[tabName]()
155        self.staleTabs[tabName] = False
156
157    def connect(self):
158        if self.connected:
159            return
160        moira.connect()
161        try:
162            moira.auth(self.clientName)
163        except moira.MoiraException, e:
164            self.errorDialog("Unable to authenticate to Moira: %s" % (e.args[1]), True)
165            if self.running:
166                self.quit()
167            else:
168                sys.exit(255)
169        self.connected = True
170
171    def query(self, query, *argList):
172        if not self.connected:
173            self.connect()
174        result = []
175        try:
176            result = moira.query(query, *argList)
177        except MoiraException, e:
178            msg = gtk.MessageDialog(self.window, gtk.DIALOG_MODAL,
179                                    gtk.MESSAGE_ERROR, gtk.BUTTONS_OK,
180                                    "Error: %s" % e.args[1])
181            msg.set_title("Error")
182            msg.run()
183            msg.destroy()
184        return result
185           
186
187    def run(self):
188        self.running=True
189        gtk.main()
190
191    def quit(self, widget=None, event=None):
192        gtk.main_quit()
193
194if __name__ == "__main__":
195    if not os.path.exists(gladeFile):
196        print "Unable to load Glade file " + gladeFile
197        sys.exit(255)
198    xmoira = XMoira()
199    xmoira.run()
Note: See TracBrowser for help on using the repository browser.