~bilalakhtar/update-manager/unity-crash-fix

« back to all changes in this revision

Viewing changes to UpdateManager/UnitySupport.py

  • Committer: Michael Vogt
  • Date: 2011-05-02 16:05:33 UTC
  • Revision ID: michael.vogt@ubuntu.com-20110502160533-0abf6dt8ravtvml7
move unity support into its own class so that its optional

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# UpdateManager.py 
 
2
#  
 
3
#  Copyright (c) 2011 Canonical
 
4
#  
 
5
#  Author: Michael Vogt <mvo@ubuntu.com>
 
6
 
7
#  This program is free software; you can redistribute it and/or 
 
8
#  modify it under the terms of the GNU General Public License as 
 
9
#  published by the Free Software Foundation; either version 2 of the
 
10
#  License, or (at your option) any later version.
 
11
 
12
#  This program is distributed in the hope that it will be useful,
 
13
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
#  GNU General Public License for more details.
 
16
 
17
#  You should have received a copy of the GNU General Public License
 
18
#  along with this program; if not, write to the Free Software
 
19
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 
20
#  USA
 
21
 
 
22
import logging
 
23
 
 
24
HAVE_UNITY_SUPPORT=False
 
25
try:
 
26
    from gi.repository import Dbusmenu, Unity
 
27
    HAVE_UNITY_SUPPORT=True
 
28
except ImportError as e:
 
29
    logging.warn("can not import unity GI %s" % e)
 
30
 
 
31
class IUnitySupport(object):
 
32
    """ interface for unity support """
 
33
    def __init__(self, parent): pass
 
34
    def set_updates_count(self, num_updates): pass
 
35
    def set_install_menuitem_visible(self, visible): pass
 
36
 
 
37
class UnitySupportImpl(IUnitySupport):
 
38
    """ implementation of unity support (if unity is available) """
 
39
 
 
40
    def __init__(self, parent):
 
41
        # create launcher and quicklist
 
42
        um_launcher_entry = Unity.LauncherEntry.get_for_desktop_id(
 
43
            "update-manager.desktop")
 
44
        self._unity = um_launcher_entry
 
45
        quicklist = Dbusmenu.Menuitem.new()
 
46
        # update
 
47
        update_dbusmenuitem = Dbusmenu.Menuitem.new()
 
48
        update_dbusmenuitem.property_set(
 
49
            Dbusmenu.MENUITEM_PROP_LABEL, _("Check for Updates"))
 
50
        update_dbusmenuitem.property_set_bool(
 
51
            Dbusmenu.MENUITEM_PROP_VISIBLE, True)
 
52
        update_dbusmenuitem.connect (
 
53
            "item-activated", parent.on_button_reload_clicked, None)
 
54
        quicklist.child_append(update_dbusmenuitem)
 
55
        # install 
 
56
        self.install_dbusmenuitem = Dbusmenu.Menuitem.new()
 
57
        self.install_dbusmenuitem.property_set (Dbusmenu.MENUITEM_PROP_LABEL,
 
58
                                                 _("Install All Available Updates"))
 
59
        self.install_dbusmenuitem.property_set_bool (Dbusmenu.MENUITEM_PROP_VISIBLE, True)
 
60
        self.install_dbusmenuitem.connect (
 
61
            "item-activated", parent.install_all_updates, None)
 
62
        quicklist.child_append (self.install_dbusmenuitem)
 
63
        # add it
 
64
        um_launcher_entry.set_property ("quicklist", quicklist)
 
65
    
 
66
    def set_updates_count(self, num_updates):
 
67
        self._unity.set_property("count", num_updates)
 
68
        self._unity.set_property("count-visible", True)
 
69
        # FIXME: setup emblem as well(?) and add urgency only for security
 
70
        if num_updates > 0:
 
71
            self._unity.set_property("urgent", True)
 
72
        else:
 
73
            self._unity.set_property("urgent", False)
 
74
 
 
75
    def set_install_menuitem_visible(self, visible):
 
76
        self.install_dbusmenuitem.property_set_bool(Dbusmenu.MENUITEM_PROP_VISIBLE, visible)
 
77
 
 
78
 
 
79
# check what to export to the clients
 
80
if HAVE_UNITY_SUPPORT:
 
81
    UnitySupport = UnitySupportImpl
 
82
else:
 
83
    # we just provide the empty interface 
 
84
    UnitySupport = IUnitySupport