1#!/usr/bin/env python
2# Terminator by Chris Jones <cmsj@tenshu.net>
3# GPL v2 only
4"""layoutlauncher.py - class for the Layout Launcher window"""
5
6import os
7from gi.repository import Gtk
8from gi.repository import GObject
9
10from .util import dbg, err, spawn_new_terminator
11from . import config
12from .translation import _
13from .terminator import Terminator
14from .plugin import PluginRegistry
15
16class LayoutLauncher:
17    """Class implementing the various parts of the preferences editor"""
18    terminator = None
19    config = None
20    registry = None
21    plugins = None
22    keybindings = None
23    window = None
24    builder = None
25    layouttreeview = None
26    layouttreestore = None
27
28    def __init__ (self):
29        self.terminator = Terminator()
30        self.terminator.register_launcher_window(self)
31
32        self.config = config.Config()
33        self.config.base.reload()
34        self.builder = Gtk.Builder()
35        try:
36            # Figure out where our library is on-disk so we can open our UI
37            (head, _tail) = os.path.split(config.__file__)
38            librarypath = os.path.join(head, 'layoutlauncher.glade')
39            gladefile = open(librarypath, 'r')
40            gladedata = gladefile.read()
41        except Exception as ex:
42            print("Failed to find layoutlauncher.glade")
43            print(ex)
44            return
45
46        self.builder.add_from_string(gladedata)
47        self.window = self.builder.get_object('layoutlauncherwin')
48
49        icon_theme = Gtk.IconTheme.get_default()
50        if icon_theme.lookup_icon('terminator-layout', 48, 0):
51            self.window.set_icon_name('terminator-layout')
52        else:
53            dbg('Unable to load Terminator layout launcher icon')
54            icon = self.window.render_icon(Gtk.STOCK_DIALOG_INFO, Gtk.IconSize.BUTTON)
55            self.window.set_icon(icon)
56
57        self.window.set_size_request(250, 300)
58        self.builder.connect_signals(self)
59        self.window.connect('destroy', self.on_destroy_event)
60        self.window.show_all()
61        self.layouttreeview = self.builder.get_object('layoutlist')
62        self.layouttreestore = self.builder.get_object('layoutstore')
63        self.update_layouts()
64
65    def on_destroy_event(self, widget, data=None):
66        """Handle window destruction"""
67        dbg('destroying self')
68        self.terminator.deregister_launcher_window(self)
69        self.window.destroy()
70        del(self.window)
71
72    def update_layouts(self):
73        """Update the contents of the layout"""
74        self.layouttreestore.clear()
75        layouts = self.config.list_layouts()
76        for layout in sorted(layouts, key=str.lower):
77            if layout != "default":
78                self.layouttreestore.append([layout])
79            else:
80                self.layouttreestore.prepend([layout])
81
82    def on_launchbutton_clicked(self, widget):
83        """Handle button click"""
84        self.launch_layout()
85
86    def on_row_activated(self, widget,  path,  view_column):
87        """Handle item double-click and return"""
88        self.launch_layout()
89
90    def launch_layout(self):
91        """Launch the selected layout as new instance"""
92        dbg('We have takeoff!')
93        selection=self.layouttreeview.get_selection()
94        (listmodel, rowiter) = selection.get_selected()
95        if not rowiter:
96            # Something is wrong, just jump to the first item in the list
97            selection.select_iter(self.layouttreestore.get_iter_first())
98            (listmodel, rowiter) = selection.get_selected()
99        layout = listmodel.get_value(rowiter, 0)
100        dbg('Clicked for %s' % layout)
101        spawn_new_terminator(self.terminator.origcwd, ['-u', '-l', layout])
102
103if __name__ == '__main__':
104    from . import util
105    util.DEBUG = True
106    from . import terminal
107    LAYOUTLAUNCHER = LayoutLauncher()
108
109    Gtk.main()
110