#!/usr/bin/env python

from gi.repository import Gtk
from gi.repository import Peas, PeasGtk

class DemoWindow(Gtk.Window):
    def __init__(self, n_windows):
        Gtk.Window.__init__(self)
        box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6,
                      homogeneous=True)
        self.set_title("Peas Window %s" % n_windows)
        self.add(box)
        
        # this constructor results in TypeError: 
        # Item 0: Expected GObject.Parameter, but got StructMeta
        
        #extension_set = Peas.ExtensionSet.new(Peas.Engine.get_default(),
        #                                      Peas.Activatable.__gtype__,
        #                                      ["object", self])
        
        # this constructor results in TypeError:
        # could not convert value for property `extension_type' from gobject.GType to GType
        
        #extension_set = Peas.ExtensionSet(engine=Peas.Engine.get_default(),
        #                                  extension_type=Peas.Activatable.__gtype__,
        #                                  construct_properties=["object", self])
        
        extension_set = Peas.ExtensionSet.new(Peas.Engine.get_default(),
                                              Peas.Activatable.__gtype__,
                                              [])
        
        extension_set.foreach(self.on_extension_added, None)
        extension_set.connect("extension-added", self.on_extension_added)
        extension_set.connect("extension-removed", self.on_extension_removed)
        
    def on_extension_added(self, extension_set, info, extension, data=None):
        print "on_extension_added"
        extension.activate()
    
    def on_extension_removed(self, extension_set, info, extension, data=None):
        print "on_extension_removed"
        extension.deactivate()
    
class MainWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self)
        self.n_windows = 0
        self.set_title("Peas Demo")
        self.connect("delete-event", Gtk.main_quit)
        self.set_border_width(6)
        self.set_has_resize_grip(False)
        grid = Gtk.Grid(orientation=Gtk.Orientation.VERTICAL, row_spacing=6)

        self.add(grid)
        
        engine = Peas.Engine.get_default()
        manager = PeasGtk.PluginManager(engine=engine, vexpand=True, 
                                        hexpand=True, homogeneous=False, 
                                        spacing=6)
        grid.add(manager)
        
        button_box = Gtk.ButtonBox(orientation=Gtk.Orientation.HORIZONTAL)
        button_box.set_spacing(6)
        button_box.set_layout(Gtk.ButtonBoxStyle.END)
        grid.add(button_box)
        
        button = Gtk.Button(label="New Window")
        button.connect("clicked", self.on_create_window_clicked)
        button_box.add(button)
        
        button = Gtk.Button.new_from_stock(Gtk.STOCK_QUIT)
        button.connect("clicked", Gtk.main_quit)
        button_box.add(button)

        grid.show_all()
    
    def on_create_window_clicked(self, button, data=None):
        self.n_windows += 1
        window = DemoWindow(self.n_windows)
        window.show()

if __name__ == "__main__":
    engine = Peas.Engine.get_default()
    
    # set plugin_dir to location of peas-demo plugins
    plugin_dir = "/opt/gnome/lib64/peas-demo/plugins"
    engine.add_search_path(plugin_dir, plugin_dir)
    
    engine.enable_loader("gjs")
    engine.enable_loader("python")
    engine.enable_loader("seed")
    
    window = MainWindow()
    window.show()
    Gtk.main()

