#!/usr/bin/env python
# -*- coding: UTF-8 -*-

# <one line to give the program's name and a brief idea of what it does.>
# Copyright (C) <year>  <name of author>

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import os

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

class DemoWindow(Gtk.Window):
    __gtype_name__ = "DemoWindow"

    def __init__(self):
        global engine

        Gtk.Window.__init__(self)

        self.n_windows = 0

        self.box = Gtk.Box(orientation = Gtk.Orientation.VERTICAL, spacing = 6)
        self.box.set_homogeneous(True)
        self.add(self.box)

        label = "Peas Window %d" % ++self.n_windows
        self.set_title(label)

        self.exten_set = Peas.ExtensionSet(
            engine = engine.get_default(),
            extension_type = Peas.Activatable.__gtype__,
            construct_properties = ["object", self])

        self.exten_set.set_foreach(self.on_extension_added, self)

        self.exten_set.connect("extension-added", self.on_extension_added)
        self.exten_set.connect("extension-removed", self.on_extension_removed)

    def on_extension_added(self, *args):#(extset, info, exten, dw):
        print args

    def on_extension_removed(self, *args):#(extset, info, exten, dw):
        print args

def create_new_window(widget):
    window = DemoWindow()
    window.show_all()

def create_main_window():
    global engine

    window = Gtk.Window(type = Gtk.WindowType.TOPLEVEL)
    window.connect("delete-event", Gtk.main_quit)
    window.set_border_width(6)
    window.set_title("Peas Demo")

    window.set_has_resize_grip(False)

    box = Gtk.Box(orientation = Gtk.Orientation.VERTICAL, spacing = 6)
    window.add(box)

    manager = PeasGtk.PluginManager(engine = engine.get_default())
    box.pack_start(manager, True, True, 0)

    button_box = Gtk.ButtonBox(orientation = Gtk.Orientation.HORIZONTAL)
    button_box.set_spacing(6)
    button_box.set_layout(Gtk.ButtonBoxStyle.END)
    box.pack_start(button_box, False, False, 0)

    button = Gtk.Button(label = "New Window")
    button.connect("clicked", create_new_window)
    button_box.add(button)

    button = Gtk.Button(label = "gtk-quit")
    button.set_use_stock(True)
    button.connect("clicked", Gtk.main_quit)
    button_box.add(button)

    return window

def main():
    global engine

    engine = Peas.Engine.get_default()
    plugin_dir = os.path.join(GLib.get_user_config_dir(), "pead-demo/plugins")
    engine.add_search_path(plugin_dir, plugin_dir)

    engine.enable_loader("gjs")
    engine.enable_loader("python")
    engine.enable_loader("seed")

    engine.add_search_path("/usr/lib/peas-demo/plugins/", "/usr/share/peas-demo/plugins")

    n_windows = 0
    main_window = create_main_window()
    main_window.show_all()

    Gtk.main()

    main_window.destroy()

    return 0

if __name__ == "__main__":
    main()
