Source

controlcenter / user_add / users_manage.py

#!/usr/bin/env python

#    This file is part of controlcenter.
#
#    controlcenter is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License v2 as published by
#    the Free Software Foundation.
#
#    controlcenter is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with controlcenter.  If not, see <http://www.gnu.org/licenses/>.


__author__ = 'Moises Henriquez'
__author_email__ = 'moc.liamg]ta[xnl.E0M'.replace(']ta[', '@')[::-1]

import gtk
import user_add
import user_mod

def _(str): return str


class Win(gtk.Window):
    '''Main users management window'''
    def __init__(self):
        gtk.Window.__init__(self, gtk.WINDOW_TOPLEVEL)
        self.set_title(_('User accounts'))
        self.connect('destroy', gtk.main_quit)
        self.set_position(gtk.WIN_POS_CENTER)
        self.set_default_size(500, 300)
        self.body = gtk.VBox()
        self.add(self.body)
        pane = gtk.HBox()

        self.body.pack_start(pane, True, True, 2)
        sw = gtk.ScrolledWindow()
        sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
        
        tree = self._get_tree()
        sw.add(tree)
        pane.pack_start(sw, True, True, 2)
        right = gtk.VBox()
        bt_add = gtk.Button(_('Add User'))
        bt_add.connect('clicked', self.add_user)
        img1 = gtk.Image()
        img1.set_from_stock(gtk.STOCK_ADD, gtk.ICON_SIZE_BUTTON)
        bt_add.set_image(img1)
        
        bt_delete = gtk.Button(_('Delete User'))
        img2 = gtk.Image()
        img2.set_from_stock(gtk.STOCK_DELETE, gtk.ICON_SIZE_BUTTON)
        bt_delete.set_image(img2)
        
        bt_properties = gtk.Button(_('Properties'))
        bt_properties.connect('clicked', self.edit_user)
        img3 = gtk.Image()
        img3.set_from_stock(gtk.STOCK_PROPERTIES, gtk.ICON_SIZE_BUTTON)
        bt_properties.set_image(img3)

        spacer = gtk.Label()
        
        btHelp = gtk.Button(_('Help'), gtk.STOCK_HELP)
        btClose = gtk.Button(_('Close'), gtk.STOCK_CLOSE)
        btClose.connect('clicked', gtk.main_quit)

        right.pack_start(bt_add, False, False, 4)
        right.pack_start(bt_delete, False, False, 4)
        right.pack_start(bt_properties, False, False, 4)
        right.pack_start(spacer, True, True, 4)
        right.pack_start(btHelp, False, False, 4)
        right.pack_start(btClose, False, False, 4)

        pane.pack_start(right, False, False, 4)
        
        self.statusbar = gtk.Statusbar()
        self.body.pack_start(self.statusbar, False, False, 2)
        self.statusbar.push(0, _('Welcome to controlcenter'))
        
        self.__modwin = None
        
    def edit_user(self, widget):
        ''' Show properties for selected user from the treeview'''
        
        if not self._selected_user: return
        
        sel_user = user_add.Account(new = False)
        sel_user.load(self._selected_user)
        if not self.__modwin: self.__modwin = user_mod.Win(sel_user)
        
        self.__modwin = user_mod.Win(sel_user)
        self.__modwin.set_title(sel_user.login + ' '+ _('Account Properties'))
        self.__modwin.show_all()
        #propw = user_mod.Win(account = sel_user)
        #propw = user_add.MainWin(account=sel_user)
        #propw.set_modal(True)
        #propw.set_default_size(350, 450)
        #propw.set_title(sel_user.login + ' ' +  _('Account Properties'))
        #propw.show_all()
        
    def _get_user_lists(self):
        '''Returns a list of 3-tuples containing (user real name, login, homedir)'''
        ret = []
        f = open('/etc/passwd', 'r')
        data = f.readlines()
        f.close()
        
        for line in data:
            spl = line.split(':')
            if int(spl[2]) == 0 or int(spl[2]) >= 1000:
                ret.append((spl[4], spl[0], spl[-2]))
        return ret
    def _get_tree(self):
        lst = gtk.ListStore(str, str, str)
        users = self._get_user_lists()
        for user in users:
            lst.append([user[0],user[1], user[2]])
        cell = gtk.CellRendererText()
        self.tree = gtk.TreeView(lst)
        
        col0 = gtk.TreeViewColumn(_('Name'), cell)
        col0.add_attribute(cell, 'text', 0)
        
        col1 = gtk.TreeViewColumn(_('Login'), cell)
        col1.add_attribute(cell, 'text', 1)
        
        col2 = gtk.TreeViewColumn(_('Home Directory'), cell)
        col2.add_attribute(cell, 'text', 2)
        
        self.tree.append_column(col0)
        self.tree.append_column(col1)
        self.tree.append_column(col2)
        
        self.tree.connect('cursor-changed', self.tree_select)
        #self.tree.connect('row-activated', self.tree_select)
        return self.tree
    def tree_select(self, widget):
        sel_row = widget.get_cursor()[0][0]
        #print sel_row
        self._selected_user = widget.get_model()[sel_row][1]

    def add_user(self, widget):
        ''' Launch the add user dialog.'''
        acct = user_add.Account(new = True)
        if not self.__modwin: self.__modwin = user_mod.Win(acct)
        self.__modwin.set_title(_('Add new user'))
        self.__modwin.show_all()

        
        

if __name__ == '__main__':
    w = Win()
    w.show_all()
    gtk.main()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.