Source

rfremix firstboot module / rfremix.py

Full commit
# -*- coding: utf-8 -*-
#
# This copyrighted material is made available to anyone wishing to use, modify,
# copy, or redistribute it subject to the terms and conditions of the GNU
# General Public License v.2.  This program is distributed in the hope that it
# will be useful, but WITHOUT ANY WARRANTY expressed or implied, including the
# implied warranties of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# Authors: Russian Fedora Team. http://russianfedora.ru
#
# This is a good example of impossible programing on python

import gtk

from firstboot.config import *
from firstboot.constants import *
from firstboot.functions import *
from firstboot.module import *

import gettext
_ = lambda x: gettext.ldgettext("firstboot", x)
N_ = lambda x: x

class moduleClass(Module):
    def __init__(self):
        Module.__init__(self)
        self.priority = 101
        # self.priority = 99
        self.sidebarTitle = N_("RFRemix configure")
        self.title = N_("RFRemix configure")
        self.icon = "workstation.png"

    def apply(self, interface, testing=False):
        print '======================================='
        for key, val in self.ACTIONS_MAP.iteritems():
            print "%s -> %s" % (key, val)
        print '======================================='
        if self.ch1.get_property('sensitive') and self.ch1.state == 1:
            os.system('/usr/bin/gconftool-2 --direct --config-source=xml:readwrite:/etc/gconf/gconf.xml.defaults -s -t bool /apps/nautilus/preferences/always_use_browser true')
        if self.ch2.get_property('sensitive') and self.ch2.state == 1:
            os.system('/usr/bin/gconftool-2 --direct --config-source=xml:readwrite:/etc/gconf/gconf.xml.defaults -s -t bool /desktop/gnome/interface/menus_have_icons true')
        if self.ch3.get_property('sensitive') and self.ch3.state == 1:
            os.system('/usr/bin/gconftool-2 --direct --config-source=xml:readwrite:/etc/gconf/gconf.xml.defaults -s -t bool /desktop/gnome/interface/buttons_have_icons true')
        if self.ch5.get_property('sensitive') and self.ch5.state == 1:
            os.system('/usr/bin/gconftool-2 --direct --config-source=xml:readwrite:/etc/gconf/gconf.xml.defaults -s -t bool /apps/gnome-terminal/global/use_menu_accelerators false')
        if self.ch6.get_property('sensitive') and self.ch6.state == 1:
            os.system('/usr/bin/gconftool-2 --direct --config-source=xml:readwrite:/etc/gconf/gconf.xml.defaults -s -t list --list-type=string /apps/gedit-2/preferences/encodings/auto_detected "[UTF-8,CURRENT,WINDOWS-1251,KOI8R,ISO-8859-5]"')
        if self.ch7.get_property('sensitive') and self.ch7.state == 1:
            os.system('/usr/bin/gconftool-2 --direct --config-source=xml:readwrite:/etc/gconf/gconf.xml.defaults -s -t int /apps/eog/ui/image_collection_position 3')
        if self.ch8.get_property('sensitive') and self.ch8.state == 1:
            os.system('/usr/bin/gconftool-2 --direct --config-source=xml:readwrite:/etc/gconf/gconf.xml.defaults -s -t int /apps/panel/toplevels/top_panel/padding 0; /usr/bin/gconftool-2 --direct --config-source=xml:readwrite:/etc/gconf/gconf.xml.defaults -s -t int /apps/panel/toplevels/bottom_panel/padding 0; /usr/bin/gconftool-2 --direct --config-source=xml:readwrite:/etc/gconf/gconf.xml.defaults -s -t int /apps/panel/applets/systray/prefs/padding 0')
        if self.ch15.get_property('sensitive') and self.ch15.state == 1:
            os.system('sed -i "s!UseNetworkManager=true!UseNetworkManager=false!g" /etc/PackageKit/PackageKit.conf; sed -i "s!UseNetworkHeuristic=true!UseNetworkHeuristic=false!g" /etc/PackageKit/PackageKit.conf')
        if self.button.get_property('sensitive'):
            if self.button.state == 0: # 0 is for rf-fonts
                os.system('/usr/sbin/freetype-backend rfremix')
            else: # 1 or else is for original fonts
                os.system(' /usr/sbin/freetype-backend fedora')
        print '======================================='
        for key, val in self.ACTIONS_MAP.iteritems():
            print "%s -> %s" % (key, val)
        print '======================================='
        
        if 'kdm_gdm' in self.ACTIONS_MAP:
            if self.ACTIONS_MAP['kdm_gdm'] == 'gdm':
                os.system(r"""echo "DISPLAYMANAGER=\"GNOME\"" > /etc/sysconfig/desktop""")
            else:
                os.system(r"""echo "DISPLAYMANAGER=\"KDE\"" > /etc/sysconfig/desktop""")
        
        if 'sudo_users' in self.ACTIONS_MAP:
            for user in self.ACTIONS_MAP['sudo_users']:
                os.system("cat >> /etc/sudoers.d/%(username)s << FOE\n"
                          "%(username)s ALL=(ALL) ALL\n"
                          "FOE" % dict(username=user))
                os.system("chmod 0440 /etc/sudoers.d/%(username)s" % dict(username=user))
        
        if 'autologin_user' in self.ACTIONS_MAP:
            # let's say that there is a value for kdm_gdm
            if 'kdm_gdm' in self.ACTIONS_MAP:
                if self.ACTIONS_MAP['kdm_gdm'] == 'gdm':
                    user = self.ACTIONS_MAP['autologin_user']
                    os.system("cat >> /etc/gdm/custom.conf << FOE\n"
                              "[daemon]\n"
                              "AutomaticLoginEnable=true\n"
                              "AutomaticLogin=%(username)s\n"
                              "TimedLoginEnable=true\n"
                              "TimedLogin=%(username)s\n"
                              "TimedLoginDelay=10\n"
                              "FOE" % dict(username=user))
                elif self.ACTIONS_MAP['kdm_gdm'] == 'kdm':
                    user = self.ACTIONS_MAP['autologin_user']
                    os.system(r"sed -i 's/#AutoLoginEnable=true/AutoLoginEnable=true/' /etc/kde/kdm/kdmrc")
                    os.system(r"sed -i 's/#AutoLoginUser=fred/AutoLoginUser=%(username)s/' /etc/kde/kdm/kdmrc" % dict(username=user))
        
        if 'c_m_backspace' in self.ACTIONS_MAP:
            os.system("grep 'terminate:ctrl_alt_bksp' /etc/sysconfig/keyboard > /dev/null || sed -i 's!OPTIONS=\"!OPTIONS=\"terminate:ctrl_alt_bksp,!g' /etc/sysconfig/keyboard")

        if 'disable_ipv6' in self.ACTIONS_MAP:
            os.system("echo \"install ipv6 /bin/true blacklist ipv6\" > /etc/modprobe.d/blacklist-ipv6.conf")
            os.system("/sbin/service ip6tables stop && /sbin/chkconfig ip6tables off")
            os.system("grep 'NETWORKING_IPV6=no' /etc/sysconfig/network || echo \"NETWORKING_IPV6=no\" >> /etc/sysconfig/network")

        if 'permissive_selinux' in self.ACTIONS_MAP:
            os.system("sed -i 's!SELINUX=enforcing!SELINUX=permissive!g' /etc/selinux/config")

        return RESULT_SUCCESS
    
    def callb_radio(self, widget, data=None):
        if widget.get_active():
            self.ACTIONS_MAP[data['key']] = data['val']

    def callb_checkbox(self, widget, data=None):
        if widget.get_active():
            self.ACTIONS_MAP[data['key']] = data['val']
        else:
            if data['key'] in self.ACTIONS_MAP:
                del self.ACTIONS_MAP[data['key']]
    
    def createScreen(self):
        self.ACTIONS_MAP = {}
        self.vbox = gtk.VBox()
        
        label = gtk.Label(_("Now your can enable some parameters, which are disabled in original "
                            "Fedora, such as fonts style, icons, encoding autodetection and others. "
                            "These are global parameter, they will be enabled for all users."))
        label.set_line_wrap(True)
        label.set_alignment(0.0, 0.5)
        label.set_size_request(500, -1)

        self.vbox.pack_start(label, False, False, 10)
        
        notebook = gtk.Notebook()
        
        ## Here goes first tab of notebook
        first_tab_vbox = gtk.VBox()
        
        label = gtk.Label(_("<b>GNOME Preferences (if GConf installed)</b>"))
        label.set_use_markup(True)
        label.set_line_wrap(True)
        label.set_alignment(0.0, 0.5)
        label.set_size_request(500, -1)
        
        first_tab_vbox.pack_start(label, False, True, 10)

        self.ch1 = gtk.CheckButton(label=_('Use single window mode in Nautilus'))
        first_tab_vbox.pack_start(self.ch1, False, True, 1)
        self.ch2 = gtk.CheckButton(label=_('Turn on icons in menus'))
        first_tab_vbox.pack_start(self.ch2, False, True, 1)
        self.ch3 = gtk.CheckButton(label=_('Turn on icons on buttons'))
        first_tab_vbox.pack_start(self.ch3, False, True, 1)
        self.ch5 = gtk.CheckButton(label=_('Disable menu accelerators in GNOME Terminal'))
        first_tab_vbox.pack_start(self.ch5, False, True, 1)
        self.ch6 = gtk.CheckButton(label=_('Autodetec windows-1251 encoding in Gedit'))
        first_tab_vbox.pack_start(self.ch6, False, True, 1)
        self.ch7 = gtk.CheckButton(label=_('Place collection right in the Eye of GNOME'))
        first_tab_vbox.pack_start(self.ch7, False, True, 1)
        self.ch8 = gtk.CheckButton(label=_('No padding between icons and applets in panels'))
        first_tab_vbox.pack_start(self.ch8, False, True, 1)

        label = gtk.Label(_("<b>System Preferences</b>"))
        label.set_use_markup(True)
        label.set_line_wrap(True)
        label.set_alignment(0.0, 0.5)
        label.set_size_request(500, -1)
        
        first_tab_vbox.pack_start(label, False, True, 10)

        self.ch15 = gtk.CheckButton(label=_('Let PackageKit work without network'))
        first_tab_vbox.pack_start(self.ch15, False, True, 1)

        self.ch1.set_active(True)
        self.ch2.set_active(True)
        self.ch5.set_active(True)
        self.ch6.set_active(True)
        self.ch8.set_active(True)
        self.ch15.set_active(True)
        if not os.path.exists('/usr/bin/gconftool-2'):
            self.ch1.set_sensitive(False)
            self.ch2.set_sensitive(False)
            self.ch3.set_sensitive(False)
            self.ch5.set_sensitive(False)
            self.ch6.set_sensitive(False)
            self.ch7.set_sensitive(False)
            self.ch8.set_sensitive(False)
        if not os.path.exists('/etc/PackageKit/PackageKit.conf'):
            self.ch15.set_sensitive(False)

        box2 = gtk.VBox()
        self.button = gtk.RadioButton(None, (_("Use Freetype built with BCL and SPR")))
        self.button.set_active(True)
        if not os.path.exists('/usr/sbin/freetype-backend'):
            self.button.set_sensitive(False)
        box2.pack_start(self.button)
        self.button = gtk.RadioButton(self.button, (_("Use Freetype as in Fedora")))
        if not os.path.exists('/usr/sbin/freetype-backend'):
            self.button.set_sensitive(False)
        box2.pack_start(self.button)
        
        first_tab_vbox.pack_start(box2, False, True, 0)
        
        notebook.append_page(first_tab_vbox, gtk.Label(_("Desktop Preferences")))
        
        ## Here goes second tab of notebook
        second_tab_vbox = gtk.VBox()
        notebook.append_page(second_tab_vbox, gtk.Label(_("System Preferences")))
        
        label = gtk.Label(_("<b>Select login manager:</b>"))
        label.set_use_markup(True)
        label.set_line_wrap(True)
        label.set_alignment(0.0, 0.5)
        label.set_size_request(500, -1)
        second_tab_vbox.pack_start(label, False, True, 10)
        
        box = gtk.VBox()
        self.wmanager_button = gtk.RadioButton(None, _("KDE Display Manager"))
        self.wmanager_button.set_active(True)
        if not os.path.exists('/usr/bin/kdm'):
            self.wmanager_button.set_sensitive(False)
        else:
            self.ACTIONS_MAP['kdm_gdm'] = 'kdm'
        
        self.wmanager_button.connect("toggled", self.callb_radio, dict(key='kdm_gdm', val='kdm'))
        
        box.pack_start(self.wmanager_button)
        self.wmanager_button = gtk.RadioButton(self.wmanager_button, _("GNOME Display Manager"))
        self.wmanager_button.set_active(True)
        if not os.path.exists('/usr/sbin/gdm'):
            self.wmanager_button.set_sensitive(False)
        else:
            self.ACTIONS_MAP['kdm_gdm'] = 'gdm'
        
        self.wmanager_button.connect("toggled", self.callb_radio, dict(key='kdm_gdm', val='gdm'))
        
        box.pack_start(self.wmanager_button)
        
        second_tab_vbox.pack_start(box, False, True, 0)    

        label = gtk.Label(_("<b>Advanced system settings</b>"))
        label.set_use_markup(True)
        label.set_line_wrap(True)
        label.set_alignment(0.0, 0.5)
        label.set_size_request(500, -1)
        label.set_alignment(0.0, 0.5)
        second_tab_vbox.pack_start(label, False, True, 10)

        #
        # C-M-Backspace option
        #
        box = gtk.VBox()
        c_m_checkbox = gtk.CheckButton(label=_('Enable Ctrl+Alt+Backspace combination to restart X-server'))
        c_m_checkbox.connect("toggled", self.callb_checkbox, dict(key='c_m_backspace', val=True))
        box.pack_start(c_m_checkbox, False, True, 0)
        second_tab_vbox.pack_start(box, False, True, 0)

        #
        # IPv6 settings
        #
        box = gtk.VBox()
        ipv6_checkbox = gtk.CheckButton(label=_("Disable IPv6 support"))
        ipv6_checkbox.connect("toggled", self.callb_checkbox, dict(key="disable_ipv6", val=True))
        box.pack_start(ipv6_checkbox, False, True, 0)
        second_tab_vbox.pack_start(box, False, True, 0)

        #
        # SELinux switcher
        #
        box = gtk.VBox()
        selinux_checkbox = gtk.CheckButton(label=_("Switch SELinux to Permissive (Reboot required)"))
        selinux_checkbox.connect("toggled", self.callb_checkbox, dict(key="permissive_selinux", val=True))
        box.pack_start(selinux_checkbox, False, True, 0)
        second_tab_vbox.pack_start(box, False, True, 0)
        
        self.vbox.pack_start(notebook)
        
        # user_opt = gtk.combo_box_new_text()
        # user_opt.append_text(_('Select user:'))
        # user_opt.append_text('kost')
        # user_opt.append_text('most')
        # user_opt.append_text('dost')
        # user_opt.connect('changed', changed_user_opt)
        # user_opt.set_active(0)
        # hbox.pack_start(user_opt)
        # add_sudo_checkbox = gtk.CheckButton(label=_('use SUDO for this user'))

        ##import grp, pwd
        ##users = [grp.getgrgid(user[3])[0] for user in pwd.getpwall() if grp.getgrgid(user[3])[2] <= 65535 and grp.getgrgid(user[3])[2] >= 500 ]
        
        ##box = gtk.VBox()
        ##hbox = gtk.HBox()
        ###
        ### Autologin
        ###
        ###self.ACTIONS_MAP['autologin_user'] = None
        
        ### def autologin_callb(widget, data=None):
        ###     return autologin_and_sudo_callb(self.ACTIONS_MAP['autologin_user'], widget, data)
        
        ##autologin_box = gtk.VBox()
        ### autologin_box.pack_start(gtk.Label(_("Autologin user:")))
        
        ##def change_autologin_user(widget):
        ##    def get_active_text(combobox):
        ##        model = combobox.get_model()
        ##        active = combobox.get_active()
        ##        if active < 0:
        ##            return None
        ##        return model[active][0]
            
        ##    if widget.get_active() > 0:
        ##        self.ACTIONS_MAP['autologin_user'] = get_active_text(widget)
        ##    else:
        ##        if 'autologin_user' in self.ACTIONS_MAP:
        ##            del self.ACTIONS_MAP['autologin_user']

        ##label = gtk.Label(_("<b>Select user for autologin:</b>"))
        ##label.set_use_markup(True)
        ##label.set_line_wrap(True)
        ##label.set_alignment(0.0, 0.5)
        ##label.set_size_request(500, -1)
        ##label.set_alignment(0.0, 0.5)
        ##second_tab_vbox.pack_start(label, False, True, 10)
        
        ##alog_opt = gtk.combo_box_new_text()
        ##alog_opt.append_text(_(' '))
        ##for user in users:
        ##    alog_opt.append_text(user)
        ##alog_opt.connect('changed', change_autologin_user)
        ##alog_opt.set_active(0)
        
        ##autologin_box.pack_start(alog_opt)
        ##hbox.pack_start(autologin_box)

        ##box.pack_start(hbox, False, True, 0)
        ##second_tab_vbox.pack_start(box, False, True, 0)
        
        ###
        ### Sudo
        ###
        ##self.ACTIONS_MAP['sudo_users'] = []
        
        ##def autologin_and_sudo_callb(var, widget, data=None):
        ##    if widget.get_active():
        ##        if data['user'] not in var:
        ##            var.append(data['user'])
        ##    else:
        ##        if data['user'] in var:
        ##            del var[var.index(data['user'])]
        
        ##def sudo_callb(widget, data=None):
        ##    return autologin_and_sudo_callb(self.ACTIONS_MAP['sudo_users'], widget, data)


        ##label = gtk.Label(_("<b>Allow sudo for users:</b>"))
        ##label.set_use_markup(True)
        ##label.set_line_wrap(True)
        ##label.set_alignment(0.0, 0.5)
        ##label.set_size_request(500, -1)
        ##label.set_alignment(0.0, 0.5)
        ##second_tab_vbox.pack_start(label, False, True, 10)

     
        ##box = gtk.VBox()
        ##hbox = gtk.HBox()

        ##sudo_box = gtk.VBox()

        ##for user in users:
        ##    checkbox = gtk.CheckButton(label=user)
        ##    sudo_box.pack_start(checkbox)
        ##    checkbox.connect("toggled", sudo_callb, {'user': user})
        ##hbox.pack_start(sudo_box)
        
        ##box.pack_start(hbox, False, True, 0)
        ##second_tab_vbox.pack_start(box, False, True, 0)
        
        
    def initializeUI(self):
        pass