Exemple #1
0
    def __init__(self):
        super(Gtk.Window, self).__init__()
        self.connect('show', self.start)
        self.connect('delete-event', self.stop)

        self.css_provider = None

        self.channel_box = Gtk.Box(homogeneous=False, spacing=0)

        vbox = Gtk.VBox(homogeneous=False, spacing=0)
        vbox.pack_start(self.channel_box, expand=True, fill=True, padding=0)

        self.command_entry = Gtk.TextView()
        self.command_entry.set_property('wrap-mode', Gtk.WrapMode.WORD)
        #self.command_entry.connect('key-press-event', self.on_command_keydown)
        self.connect('key-press-event', self.on_command_keydown)
        vbox.pack_start(self.command_entry, expand=False, fill=True, padding=0)

        self.add(vbox)
        self.set_default_size(900, 400)
        self.show_all()

        self.command_entry.grab_focus()
        self.keymap = Keymap()

        self.update_theme()

        self.network_channels = {}
        self.current_channel = None
Exemple #2
0
class MainWindow(Gtk.Window):

    def __init__(self):
        super(Gtk.Window, self).__init__()
        self.connect('show', self.start)
        self.connect('delete-event', self.stop)

        self.css_provider = None

        self.channel_box = Gtk.Box(homogeneous=False, spacing=0)

        vbox = Gtk.VBox(homogeneous=False, spacing=0)
        vbox.pack_start(self.channel_box, expand=True, fill=True, padding=0)

        self.command_entry = Gtk.TextView()
        self.command_entry.set_property('wrap-mode', Gtk.WrapMode.WORD)
        #self.command_entry.connect('key-press-event', self.on_command_keydown)
        self.connect('key-press-event', self.on_command_keydown)
        vbox.pack_start(self.command_entry, expand=False, fill=True, padding=0)

        self.add(vbox)
        self.set_default_size(900, 400)
        self.show_all()

        self.command_entry.grab_focus()
        self.keymap = Keymap()

        self.update_theme()

        self.network_channels = {}
        self.current_channel = None

    def on_focus_out(self, widget, event):
        self.command_entry.grab_focus()

    def on_command_keydown(self, widget, event):
        command = self.keymap.gtk_event(event)

        if command is None:
            return False

        log.info('key command: {0}'.format(command))

        if command == 'send-buffer':
            text_buffer = self.command_entry.get_buffer()
            message = text_buffer.get_text(text_buffer.get_start_iter(), text_buffer.get_end_iter(), True).strip()
            log.info('message: {0}'.format(message))

            channel_window = self.current_channel
            async_engine_command('send', channel_window.network, (channel_window.channel, message))
            text_buffer.set_text('')
            return True

        elif command == 'window-next':
            l = []
            for n in self.network_channels:
                for c in self.network_channels[n]:
                    l.append(self.network_channels[n][c])

            p = l.index(self.current_channel) + 1
            if p == len(l):
                self.set_channel(l[0])
            else:
                self.set_channel(l[p])

        elif command == 'window-previous':
            l = []
            for n in self.network_channels:
                for c in self.network_channels[n]:
                    l.append(self.network_channels[n][c])

            p = l.index(self.current_channel) - 1
            if p < 0:
                self.set_channel(l[-1])
            else:
                self.set_channel(l[p])

        elif command == 'quit':
            self.stop()

        else:
            method = command.replace('-', '_')
            method = getattr(self, method, None)
            if method is not None:
                method()

        return True

    def start(self, widget):
        async_engine_command('connect')

    def stop(self, widget=None, event=None):
        async_engine_command('stop')

    def update_theme(self):
        load_themes()
        config = Dotfile('config')

        theme = config['theme']
        if theme not in themes:
            log.warning('configured theme "{0}" not found; falling back to native'.format(theme))
            theme = 'native'

        theme = themes[theme]
        css = theme.render()

        if self.css_provider is not None:
            Gtk.StyleContext.remove_provider_for_screen(Gdk.Screen.get_default(), self.css_provider)

        self.css_provider = Gtk.CssProvider()
        self.css_provider.load_from_data(css)
        Gtk.StyleContext.add_provider_for_screen(Gdk.Screen.get_default(), self.css_provider, 900)

    def set_channel(self, channel_window):
        log.debug('setting channel to {0}'.format(channel_window))
        if self.current_channel is not None:
            self.channel_box.remove(self.current_channel)

        self.current_channel = channel_window
        self.channel_box.pack_start(channel_window, expand=True, fill=True, padding=0)

    def on_new_channel(self, network, channel):
        log.debug('in on_new_channel')
        if network.name not in self.network_channels:
            self.network_channels[network.name] = {}

        if channel.name not in self.network_channels[network.name]:
            channel_window = ChannelWindow(network, channel)
            self.network_channels[network.name][channel.name] = channel_window

        if self.current_channel is None:
            self.set_channel(channel_window)

    def on_message(self, network, channel):
        log.debug('in on_message')
        if network.name in self.network_channels and channel.name in self.network_channels[network.name]:
            channel_window = self.network_channels[network.name][channel.name]
            channel_window.on_message(network, channel)

        else:
            log.warning('message for unknown channel {0}'.format(channel.name))