def get_shortcuts():
    values = []
    dcm = DConfManager('org.gnome.desktop.wm.keybindings')
    for key in dcm.get_keys():
        for each_element in dcm.get_value(key):
            values.append(each_element)
    dcm = DConfManager('org.gnome.settings-daemon.plugins.media-keys')
    for key in dcm.get_keys():
        each_element = dcm.get_value(key)
        if type(each_element) == str:
            values.append(each_element)
    return values
def get_shortcuts():
    values = []
    de = get_desktop_environment()
    if de == 'gnome':
        dcm = DConfManager('org.gnome.desktop.wm.keybindings')
        for key in dcm.get_keys():
            for each_element in dcm.get_value(key):
                if type(each_element) == str:
                    values.append(each_element)
                elif type(each_element) == list:
                    values.extend(each_element)
        dcm = DConfManager('org.gnome.settings-daemon.plugins.media-keys')
        for key in dcm.get_keys():
            each_element = dcm.get_value(key)
            if type(each_element) == str:
                values.append(each_element)
            elif type(each_element) == list:
                values.extend(each_element)
    elif de == 'cinnamon':
        dcm = DConfManager('org.cinnamon.desktop.keybindings.media-keys')
        for key in dcm.get_keys():
            for each_element in dcm.get_value(key):
                if type(each_element) == str:
                    values.append(each_element)
                elif type(each_element) == list:
                    values.extend(each_element)
        dcm = DConfManager('org.cinnamon.desktop.keybindings.wm')
        for key in dcm.get_keys():
            for each_element in dcm.get_value(key):
                if type(each_element) == str:
                    values.append(each_element)
                elif type(each_element) == list:
                    values.extend(each_element)
    elif de == 'mate':
        dcm = DConfManager('org.mate.SettingsDaemon.plugins.media-keys')
        for key in dcm.get_keys():
            values.append(dcm.get_value(key))
    return values
    def save_preferences(self):
        configuration = Configuration()
        configuration.set('first-time', False)
        configuration.set('version', comun.VERSION)
        if self.radiobutton1.get_active() is True:
            configuration.set('theme', 'light')
        elif self.radiobutton2.get_active() is True:
            configuration.set('theme', 'dark')
        else:
            configuration.set('theme', 'normal')

        if self.on_start['none'].get_active() is True:
            configuration.set('on_start', 0)
        elif self.on_start['enable'].get_active() is True:
            configuration.set('on_start', 1)
        else:
            configuration.set('on_start', -1)

        if self.on_end['none'].get_active() is True:
            configuration.set('on_end', 0)
        elif self.on_end['enable'].get_active() is True:
            configuration.set('on_end', 1)
        else:
            configuration.set('on_end', -1)

        configuration.set('autostart', self.checkbutton1.get_active())
        set_autostart(self.checkbutton1.get_active())
        configuration.set('on_mouse_plugged', self.checkbutton2.get_active())

        configuration.set('start_hidden', self.checkbutton5.get_active())
        configuration.set('show_notifications', self.checkbutton6.get_active())

        configuration.set('disable_on_typing', self.checkbutton8.get_active())
        configuration.set('interval', self.interval.get_value())

        configuration.set('natural_scrolling', self.checkbutton46.get_active())
        tp = Touchpad()
        if tp.is_there_touchpad():
            tipo = tp._get_type(tp._get_ids()[0])
            if tipo == SYNAPTICS:
                configuration.set('two_finger_scrolling',
                                  self.two_finger_scrolling.get_active())
                configuration.set('edge_scrolling',
                                  self.edge_scrolling.get_active())
                configuration.set('cicular_scrolling',
                                  self.cicular_scrolling.get_active())

            elif tipo == LIBINPUT:
                if tp.can_two_finger_scrolling():
                    configuration.set('two_finger_scrolling',
                                      self.two_finger_scrolling.get_active())
                if tp.can_edge_scrolling():
                    configuration.set('edge_scrolling',
                                      self.edge_scrolling.get_active())
                if tp.has_tapping():
                    configuration.set('tapping', self.tapping.get_active())
                configuration.set('speed', self.speed.get_value())
            elif tipo == EVDEV:
                configuration.set('speed', self.speed.get_value())

        configuration.save()
        desktop_environment = get_desktop_environment()

        if desktop_environment in ['gnome', 'unity', 'cinnamon', 'mate']:
            self.ctrl.set_active(True)
            self.alt.set_active(True)

        print(desktop_environment)
        if desktop_environment in ['gnome', 'unity']:
            dcm = DConfManager('org.gnome.settings-daemon.plugins.media-keys.\
custom-keybindings.touchpad-indicator')
            if self.checkbutton0.get_active() and\
                    len(self.entry11.get_text()) > 0:
                key1 = ''
                key2 = None
                if self.ctrl.get_active() is True:
                    key1 += '<Control>'
                    key2 = '<Primary>'
                if self.alt.get_active() is True:
                    key1 += '<Alt>'
                    if key2 is not None:
                        key2 += '<Alt>'
                key1 += self.entry11.get_text().lower()
                if key2 is not None:
                    key2 += self.entry11.get_text().lower()
                if key1 not in get_shortcuts() and key2 not in get_shortcuts():
                    dcm = DConfManager('org.gnome.settings-daemon.plugins.\
media-keys')
                    shortcuts = dcm.get_value('custom-keybindings')
                    key = '/org/gnome/settings-daemon/plugins/media-keys/\
custom-keybindings/touchpad-indicator/'

                    if key in shortcuts:
                        shortcuts.pop(shortcuts.index(key))
                        dcm.set_value('custom-keybindings', shortcuts)
                    if key not in shortcuts:
                        shortcuts.append(key)
                        dcm.set_value('custom-keybindings', shortcuts)
                    dcm = DConfManager(
                        'org.gnome.settings-daemon.plugins.media-keys.\
custom-keybindings.touchpad-indicator')
                    dcm.set_value('name', 'Touchpad-Indicator')
                    dcm.set_value('binding', key1)
                    dcm.set_value(
                        'command', '/usr/bin/python3 \
/usr/share/touchpad-indicator/change_touchpad_state.py')
            else:
                dcm.set_value('binding', '')
                dcm = DConfManager('org.gnome.settings-daemon.plugins.\
media-keys')
                shortcuts = dcm.get_value('custom-keybindings')
                key = '/org/gnome/settings-daemon/plugins/media-keys/\
custom-keybindings/touchpad-indicator/'

                if key in shortcuts:
                    shortcuts.pop(shortcuts.index(key))
                    dcm.set_value('custom-keybindings', shortcuts)
        elif desktop_environment == 'cinnamon':
            dcm = DConfManager('org.cinnamon.desktop.keybindings.\
custom-keybindings.touchpad-indicator')
            if self.checkbutton0.get_active() and\
                    len(self.entry11.get_text()) > 0:
                key1 = ''
                key2 = None
                if self.ctrl.get_active() is True:
                    key1 += '<Control>'
                    key2 = '<Primary>'
                if self.alt.get_active() is True:
                    key1 += '<Alt>'
                    if key2 is not None:
                        key2 += '<Alt>'
                key1 += self.entry11.get_text().lower()
                if key2 is not None:
                    key2 += self.entry11.get_text().lower()
                if key1 not in get_shortcuts() and key2 not in get_shortcuts():
                    dcm.set_value('name', 'Touchpad-Indicator')
                    dcm.set_value('binding', [key1])
                    dcm.set_value(
                        'command', '/usr/bin/python3 \
/usr/share/touchpad-indicator/change_touchpad_state.py')
                dcm = DConfManager('org.cinnamon.desktop.keybindings')
                shortcuts = dcm.get_value('custom-list')
                if 'touchpad-indicator' in shortcuts:
                    shortcuts.pop(shortcuts.index('touchpad-indicator'))
                    dcm.set_value('custom-list', shortcuts)
                if 'touchpad-indicator' not in shortcuts:
                    shortcuts.append('touchpad-indicator')
                    dcm.set_value('custom-list', shortcuts)
            else:
                dcm.set_value('binding', [])
                dcm = DConfManager('org.cinnamon.desktop.keybindings')
                shortcuts = dcm.get_value('custom-list')
                if 'touchpad-indicator' in shortcuts:
                    shortcuts.pop(shortcuts.index('touchpad-indicator'))
                    dcm.set_value('custom-list', shortcuts)
        elif desktop_environment == 'mate':
            dcm = DConfManager('org.mate.desktop.keybindings.\
touchpad-indicator')
            if self.checkbutton0.get_active() and\
                    len(self.entry11.get_text()) > 0:
                key1 = ''
                key2 = None
                if self.ctrl.get_active() is True:
                    key1 += '<Control>'
                    key2 = '<Primary>'
                if self.alt.get_active() is True:
                    key1 += '<Alt>'
                    if key2 is not None:
                        key2 += '<Alt>'
                key1 += self.entry11.get_text().lower()
                if key2 is not None:
                    key2 += self.entry11.get_text().lower()
                if key1 not in get_shortcuts() and key2 not in get_shortcuts():
                    dcm.set_value('name', 'Touchpad-Indicator')
                    dcm.set_value('binding', key1)
                    dcm.set_value(
                        'action', '/usr/bin/python3 \
/usr/share/touchpad-indicator/change_touchpad_state.py')
            else:
                dcm.set_value('binding', '')
        elif desktop_environment == 'xfce':
            if xfconfquery_exists():
                xfceconf = XFCEConfiguration('xfce4-keyboard-shortcuts')
                keys = xfceconf.search_for_value_in_properties_startswith(
                    '/commands/custom/', '/usr/share/\
touchpad-indicator/change_touchpad_state.py')
                if keys:
                    for akey in keys:
                        xfceconf.reset_property(akey['key'])
                if self.checkbutton0.get_active():
                    key = key.replace('<Primary>', '<Control>')
                    xfceconf.set_property(
                        '/commands/custom/' + key, '/usr/share/\
touchpad-indicator/change_touchpad_state.py')
    def load_preferences(self):
        configuration = Configuration()
        first_time = configuration.get('first-time')
        version = configuration.get('version')
        if first_time or version != comun.VERSION:
            configuration.set_defaults()
            configuration.read()
        if os.path.exists(comun.FILE_AUTO_START) and\
                not os.path.islink(comun.FILE_AUTO_START):
            os.remove(comun.FILE_AUTO_START)
        self.checkbutton1.set_active(os.path.islink(comun.FILE_AUTO_START))
        print(comun.FILE_AUTO_START)
        print('====', os.path.exists(comun.FILE_AUTO_START))
        self.checkbutton2.set_active(configuration.get('on_mouse_plugged'))

        desktop_environment = get_desktop_environment()
        print(desktop_environment)

        if desktop_environment == 'gnome' or\
                desktop_environment == 'unity':
            dcm = DConfManager('org.gnome.settings-daemon.plugins.media-keys.\
custom-keybindings.touchpad-indicator')
            shortcut = dcm.get_value('binding')
            if shortcut is None or len(shortcut) == 0:
                self.checkbutton0.set_active(False)
                self.entry11.set_text('')
            else:
                self.checkbutton0.set_active(True)
                self.ctrl.set_active(shortcut.find('<Control>') > -1)
                self.alt.set_active(shortcut.find('<Alt>') > -1)
                self.entry11.set_text(shortcut[-1:])
        elif desktop_environment == 'cinnamon':
            dcm = DConfManager('org.cinnamon.desktop.keybindings.\
custom-keybindings.touchpad-indicator')
            shortcuts = dcm.get_value('binding')
            if shortcuts is None or len(shortcuts) == 0:
                self.checkbutton0.set_active(False)
                self.entry11.set_text('')
            else:
                shortcut = shortcuts[0]
                self.checkbutton0.set_active(True)
                self.ctrl.set_active(shortcut.find('<Control>') > -1)
                self.alt.set_active(shortcut.find('<Alt>') > -1)
                self.entry11.set_text(shortcut[-1:])
        elif desktop_environment == 'mate':
            dcm = DConfManager('org.mate.desktop.keybindings.\
touchpad-indicator')
            shortcut = dcm.get_value('binding')
            if shortcut is None or len(shortcut) == 0:
                self.checkbutton0.set_active(False)
                self.entry11.set_text('')
            else:
                self.checkbutton0.set_active(True)
                self.ctrl.set_active(shortcut.find('<Control>') > -1)
                self.alt.set_active(shortcut.find('<Alt>') > -1)
                self.entry11.set_text(shortcut[-1:])
        option = configuration.get('on_start')
        if option == 0:
            self.on_start['none'].set_active(True)
        if option == 1:
            self.on_start['enable'].set_active(True)
        elif option == -1:
            self.on_start['disable'].set_active(True)

        option = configuration.get('on_end')
        if option == 0:
            self.on_end['none'].set_active(True)
        elif option == 1:
            self.on_end['enable'].set_active(True)
        elif option == -1:
            self.on_end['disable'].set_active(True)

        self.checkbutton5.set_active(configuration.get('start_hidden'))
        self.checkbutton6.set_active(configuration.get('show_notifications'))

        self.checkbutton8.set_active(configuration.get('disable_on_typing'))
        self.interval.set_value(configuration.get('interval'))
        self.label_interval.set_sensitive(self.checkbutton8.get_active())
        self.interval.set_sensitive(self.checkbutton8.get_active())

        option = configuration.get('theme')
        if option == 'light':
            self.radiobutton1.set_active(True)
        elif option == 'dark':
            self.radiobutton2.set_active(True)
        elif option == 'normal':
            self.radiobutton3.set_active(True)

        self.checkbutton46.set_active(configuration.get('natural_scrolling'))
        tp = Touchpad()
        if tp.is_there_touchpad():
            tipo = tp._get_type(tp._get_ids()[0])
            if tipo == SYNAPTICS:
                self.two_finger_scrolling.set_active(
                    configuration.get('two_finger_scrolling'))
                self.edge_scrolling.set_active(
                    configuration.get('edge_scrolling'))
                self.cicular_scrolling.set_active(
                    configuration.get('cicular_scrolling'))
            elif tipo == LIBINPUT:
                if tp.can_two_finger_scrolling():
                    self.two_finger_scrolling.set_active(
                        configuration.get('two_finger_scrolling'))
                if tp.can_edge_scrolling():
                    self.edge_scrolling.set_active(
                        configuration.get('edge_scrolling'))
                if tp.has_tapping():
                    self.tapping.set_active(configuration.get('tapping'))
            self.speed.set_value(configuration.get('speed'))
    def save_preferences(self):
        configuration = Configuration()
        configuration.set('first-time', False)
        configuration.set('version', comun.VERSION)
        key = ''
        if self.ctrl.get_active() == True:
            key += '<Primary>'
        if self.alt.get_active() == True:
            key += '<Alt>'
        key += self.entry11.get_text()
        if self.radiobutton1.get_active() == True:
            theme = 'light'
        elif self.radiobutton2.get_active() == True:
            theme = 'dark'
        configuration.set('shortcut_enabled', self.checkbutton0.get_active())
        configuration.set('autostart', self.checkbutton1.get_active())
        create_or_remove_autostart(self.checkbutton1.get_active())
        configuration.set('on_mouse_plugged', self.checkbutton2.get_active())
        configuration.set('natural_scrolling', self.checkbutton46.get_active())
        configuration.set('enable_on_exit', self.checkbutton3.get_active())
        configuration.set('disable_on_exit', self.checkbutton4.get_active())
        configuration.set('start_hidden', self.checkbutton5.get_active())
        configuration.set('show_notifications', self.checkbutton6.get_active())
        configuration.set('disable_touchpad_on_start_indicator',
                          self.checkbutton7.get_active())
        configuration.set('disable_on_typing', self.checkbutton8.get_active())
        configuration.set('seconds', self.seconds.get_value() / 1000)
        configuration.set('shortcut', key)
        configuration.set('theme', theme)
        configuration.set('VertEdgeScroll', self.checkbutton41.get_active())
        configuration.set('HorizEdgeScroll', self.checkbutton42.get_active())
        configuration.set('CircularScrolling', self.checkbutton43.get_active())
        configuration.set('VertTwoFingerScroll',
                          self.checkbutton44.get_active())
        configuration.set('HorizTwoFingerScroll',
                          self.checkbutton45.get_active())
        configuration.set('TapButton1', self.combobox47.get_active())
        configuration.set('TapButton2', self.combobox48.get_active())
        configuration.set('TapButton3', self.combobox49.get_active())
        configuration.save()
        '''
		aclient = Synclient()
		aclient.set('VertEdgeScroll',1 if self.checkbutton41.get_active() else 0)
		aclient.set('HorizEdgeScroll',1 if self.checkbutton42.get_active() else 0)
		aclient.set('CircularScrolling',1 if self.checkbutton43.get_active() else 0)
		aclient.set('VertTwoFingerScroll',1 if self.checkbutton44.get_active() else 0)
		aclient.set('HorizTwoFingerScroll',1 if self.checkbutton45.get_active() else 0)
		aclient.set('TapButton1',self.combobox47.get_active())
		aclient.set('TapButton2',self.combobox48.get_active())
		aclient.set('TapButton3',self.combobox49.get_active())		
		if self.checkbutton46.get_active():
			aclient.set('VertScrollDelta',-abs(int(aclient.get('VertScrollDelta'))))
			aclient.set('HorizScrollDelta',-abs(int(aclient.get('HorizScrollDelta'))))
		else:
			aclient.set('VertScrollDelta',abs(int(aclient.get('VertScrollDelta'))))
			aclient.set('HorizScrollDelta',abs(int(aclient.get('HorizScrollDelta'))))
		'''
        desktop_environment = get_desktop_environment()
        if desktop_environment == 'gnome':
            print('gnom3')
            dcm = DConfManager('org.gnome.settings-daemon.plugins.media-keys')
            values = dcm.get_value('custom-keybindings')
            if self.checkbutton0.get_active():
                if '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/touchpad-indicator/' not in values:
                    values.append(
                        '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/touchpad-indicator/'
                    )
                    dcm.set_value('custom-keybindings', values)
                dcm = DConfManager(
                    'org.gnome.settings-daemon.plugins.media-keys.custom-keybindings.touchpad-indicator'
                )
                print(dcm.set_value('binding', key))
                print(
                    dcm.set_value(
                        'command',
                        '/usr/bin/python3 /opt/extras.ubuntu.com/touchpad-indicator/share/touchpad-indicator/change_touchpad_state.py'
                    ))
                print(dcm.set_value('name', 'Touchpad-Indicator key binding'))
            else:
                if '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/touchpad-indicator/' in values:
                    values.remove(
                        '/org/gnome/settings-daemon/plugins/media-keys/custom-keybindings/touchpad-indicator/'
                    )
                    dcm.set_value('custom-keybindings', values)
        elif desktop_environment == 'xfce':
            if xfconfquery_exists():
                xfceconf = XFCEConfiguration('xfce4-keyboard-shortcuts')
                keys = xfceconf.search_for_value_in_properties_startswith(
                    '/commands/custom/',
                    '/opt/extras.ubuntu.com/touchpad-indicator/share/touchpad-indicator/change_touchpad_state.py'
                )
                if keys:
                    for akey in keys:
                        xfceconf.reset_property(akey['key'])
                if self.checkbutton0.get_active():
                    key = key.replace('<Primary>', '<Control>')
                    xfceconf.set_property(
                        '/commands/custom/' + key,
                        '/opt/extras.ubuntu.com/touchpad-indicator/share/touchpad-indicator/change_touchpad_state.py'
                    )