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

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