1# -*- coding: utf-8 -*-
2# Pitivi video editor
3# Copyright (c) 2011 Jean-François Fortin Tam <nekohayo@gmail.com>
4#
5# This program is free software; you can redistribute it and/or
6# modify it under the terms of the GNU Lesser General Public
7# License as published by the Free Software Foundation; either
8# version 2.1 of the License, or (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13# Lesser General Public License for more details.
14#
15# You should have received a copy of the GNU Lesser General Public
16# License along with this program; if not, write to the
17# Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18# Boston, MA 02110-1301, USA.
19"""Missing dependencies logic."""
20import os
21
22from gi.repository import Gtk
23
24from pitivi.check import missing_soft_deps
25from pitivi.configure import get_ui_dir
26
27
28class DepsManager(object):
29    """Manages a dialog listing missing soft dependencies."""
30
31    def __init__(self, app, parent_window=None):
32        self.app = app
33        self.builder = Gtk.Builder()
34        self.builder.add_from_file(
35            os.path.join(get_ui_dir(), "depsmanager.ui"))
36        self.builder.connect_signals(self)
37        self.window = self.builder.get_object("window1")
38        self.window.set_modal(True)
39        if parent_window:
40            self.window.set_transient_for(parent_window)
41        else:
42            self.window.set_transient_for(self.app.gui)
43        # Same hack as in the rendering progress dialog,
44        # to prevent GTK3 from eating a crazy amount of vertical space:
45        self.window.set_resizable(False)
46
47        # FIXME: autodetect if we can actually use PackageKit's
48        # "InstallResource" dbus method, and if yes, show this button.
49        self.builder.get_object("install_btn").hide()
50        self._setDepsLabel()
51        self.show()
52
53    def _onCloseButtonClickedCb(self, unused_button):
54        """Hides the dialog."""
55        self.hide()
56
57    def _onInstallButtonClickedCb(self, unused_button):
58        """Hides on install and tries to install dependencies."""
59        self.hide()
60        # FIXME: this is not implemented properly.
61        # Here is some partially working code:
62
63        # self.session_bus = dbus.SessionBus()
64        # self.dbus_path = "/org/freedesktop/PackageKit"
65        # self.dbus_name = "org.freedesktop.PackageKit"
66        # self.dbus_interface = "org.freedesktop.PackageKit.Modify"
67        # self.obj = self.session_bus.get_object(self.dbus_name, self.dbus_path)
68        # self.iface = dbus.Interface(self.obj, self.dbus_interface)
69
70        # soft_deps_list = missing_soft_deps.keys()
71
72        # This line works for testing, but InstallProvideFiles
73        # is not really what we want:
74        # self.iface.InstallProvideFiles(self.window.window_xid,
75        # soft_deps_list, "show-progress,show-finished")
76
77        # Instead, we should be using InstallResources(xid, type, resources)
78        # self.iface.InstallResources(self.window.window_xid,
79        # None, soft_deps_list)
80
81        # TODO: catch exceptions/create callbacks to _installFailedCb
82
83    def _setDepsLabel(self):
84        """Updates the UI to display the list of missing dependencies."""
85        label_contents = ""
86        for depname, dep in missing_soft_deps.items():
87            label_contents += "• %s (%s)\n" % (
88                dep.modulename, dep.additional_message)
89        self.builder.get_object("pkg_list").set_text(label_contents)
90
91    def show(self):
92        """Shows the dialog."""
93        self.window.show()
94
95    def hide(self):
96        """Hides the dialog."""
97        self.window.hide()
98
99    def _installFailedCb(self, unused_exception):
100        """Handles the failure of installing packages."""
101        self.show()
102