This file is indexed.

/usr/share/virt-manager/virtManager/systray.py is in virt-manager 1:1.5.1-0ubuntu1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#
# Copyright (C) 2009, 2013 Red Hat, Inc.
# Copyright (C) 2009 Cole Robinson <crobinso@redhat.com>
#
# 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 2 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301 USA.
#

import logging

from gi.repository import GObject
from gi.repository import Gtk

from . import vmmenu
from .baseclass import vmmGObject
from .error import vmmErrorDialog

try:
    # pylint: disable=no-name-in-module
    # pylint: disable=wrong-import-order
    from gi.repository import AppIndicator3
except Exception:
    AppIndicator3 = None


class vmmSystray(vmmGObject):
    __gsignals__ = {
        "action-toggle-manager": (GObject.SignalFlags.RUN_FIRST, None, []),
        "action-view-manager": (GObject.SignalFlags.RUN_FIRST, None, []),
        "action-suspend-domain": (GObject.SignalFlags.RUN_FIRST, None, [str, str]),
        "action-resume-domain": (GObject.SignalFlags.RUN_FIRST, None, [str, str]),
        "action-run-domain": (GObject.SignalFlags.RUN_FIRST, None, [str, str]),
        "action-shutdown-domain": (GObject.SignalFlags.RUN_FIRST, None, [str, str]),
        "action-reset-domain": (GObject.SignalFlags.RUN_FIRST, None, [str, str]),
        "action-reboot-domain": (GObject.SignalFlags.RUN_FIRST, None, [str, str]),
        "action-destroy-domain": (GObject.SignalFlags.RUN_FIRST, None, [str, str]),
        "action-save-domain": (GObject.SignalFlags.RUN_FIRST, None, [str, str]),
        "action-migrate-domain": (GObject.SignalFlags.RUN_FIRST, None, [str, str]),
        "action-delete-domain": (GObject.SignalFlags.RUN_FIRST, None, [str, str]),
        "action-clone-domain": (GObject.SignalFlags.RUN_FIRST, None, [str, str]),
        "action-show-host": (GObject.SignalFlags.RUN_FIRST, None, [str]),
        "action-show-domain": (GObject.SignalFlags.RUN_FIRST, None, [str, str]),
        "action-exit-app": (GObject.SignalFlags.RUN_FIRST, None, []),
    }

    def __init__(self):
        vmmGObject.__init__(self)

        self.topwin = None
        self.err = vmmErrorDialog()

        self.conn_menuitems = {}
        self.conn_vm_menuitems = {}
        self.vm_action_dict = {}
        self.systray_menu = None
        self.systray_icon = None
        self.systray_indicator = False

        # Are we using Application Indicators?
        if AppIndicator3 is not None:
            self.systray_indicator = True
            logging.debug("Using AppIndicator3 for systray")

        self.init_systray_menu()

        self.add_gsettings_handle(
            self.config.on_view_system_tray_changed(self.show_systray))

        self.show_systray()

    def is_visible(self):
        if self.systray_indicator:
            return (self.config.get_view_system_tray() and
                    self.systray_icon)
        else:
            return (self.config.get_view_system_tray() and
                    self.systray_icon and
                    self.systray_icon.is_embedded())

    def _cleanup(self):
        self.err = None

        if self.systray_menu:
            self.systray_menu.destroy()
            self.systray_menu = None

        self.systray_icon = None
        self.conn_menuitems = None
        self.conn_vm_menuitems = None
        self.vm_action_dict = None


    ###########################
    # Initialization routines #
    ###########################

    def init_systray_menu(self):
        """
        Do we want notifications?

        Close App
        Hide app? As in, only have systray active? is that possible?
            Have one of those 'minimize to tray' notifications?

        """
        self.systray_menu = Gtk.Menu()

        self.systray_menu.add(Gtk.SeparatorMenuItem())

        if self.systray_indicator:
            hide_item = Gtk.MenuItem.new_with_mnemonic(
                    _("_Show Virtual Machine Manager"))
            hide_item.connect("activate", self.systray_activate)
            self.systray_menu.add(hide_item)

        exit_item = Gtk.ImageMenuItem.new_from_stock(Gtk.STOCK_QUIT, None)
        exit_item.connect("activate", self.exit_app)
        self.systray_menu.add(exit_item)
        self.systray_menu.show_all()

    def init_systray(self):
        # Build the systray icon
        if self.systray_icon:
            return

        if self.systray_indicator:
            # pylint: disable=maybe-no-member
            self.systray_icon = AppIndicator3.Indicator.new("virt-manager",
                                "virt-manager",
                                AppIndicator3.IndicatorCategory.APPLICATION_STATUS)
            self.systray_icon.set_status(AppIndicator3.IndicatorStatus.ACTIVE)
            self.systray_icon.set_menu(self.systray_menu)

        else:
            self.systray_icon = Gtk.StatusIcon()
            self.systray_icon.set_visible(True)
            self.systray_icon.set_property("icon-name", "virt-manager")
            self.systray_icon.connect("activate", self.systray_activate)
            self.systray_icon.connect("popup-menu", self.systray_popup)
            self.systray_icon.set_tooltip_text(_("Virtual Machine Manager"))

    def show_systray(self):
        do_show = self.config.get_view_system_tray()
        logging.debug("Showing systray: %s", do_show)

        if not self.systray_icon:
            if do_show:
                self.init_systray()
        else:
            if self.systray_indicator:
                # pylint: disable=maybe-no-member
                status = AppIndicator3.IndicatorStatus.PASSIVE
                if do_show:
                    status = AppIndicator3.IndicatorStatus.ACTIVE
                self.systray_icon.set_status(status)
            else:
                self.systray_icon.set_visible(do_show)

    # Helper functions
    def _get_vm_menu_item(self, vm):
        connkey = vm.get_connkey()
        uri = vm.conn.get_uri()

        if uri in self.conn_vm_menuitems:
            if connkey in self.conn_vm_menuitems[uri]:
                return self.conn_vm_menuitems[uri][connkey]
        return None

    def _set_vm_status_icon(self, vm, menu_item):
        image = Gtk.Image()
        image.set_from_icon_name(vm.run_status_icon_name(),
                                 Gtk.IconSize.MENU)
        image.set_sensitive(vm.is_active())
        menu_item.set_image(image)

    # Listeners

    def systray_activate(self, widget_ignore):
        self.emit("action-toggle-manager")

    def systray_popup(self, widget_ignore, button, event_time):
        if button != 3:
            return

        self.systray_menu.popup(None, None, Gtk.StatusIcon.position_menu,
                                self.systray_icon, 0, event_time)

    def repopulate_menu_list(self):
        # Build sorted connection list
        connsort = self.conn_menuitems.keys()
        connsort.sort()
        connsort.reverse()

        # Empty conn list
        for child in self.systray_menu.get_children():
            if child in self.conn_menuitems.values():
                self.systray_menu.remove(child)

        # Build sorted conn list
        for uri in connsort:
            self.systray_menu.insert(self.conn_menuitems[uri], 0)


    def conn_added(self, engine_ignore, conn):
        conn.connect("vm-added", self.vm_added)
        conn.connect("vm-removed", self.vm_removed)
        conn.connect("state-changed", self.conn_state_changed)

        if conn.get_uri() in self.conn_menuitems:
            return

        menu_item = Gtk.MenuItem.new_with_label(conn.get_pretty_desc())
        menu_item.show()
        vm_submenu = Gtk.Menu()
        vm_submenu.show()
        menu_item.set_submenu(vm_submenu)

        self.conn_menuitems[conn.get_uri()] = menu_item
        self.conn_vm_menuitems[conn.get_uri()] = {}

        self.repopulate_menu_list()

        self.conn_state_changed(conn)
        self.populate_vm_list(conn)

    def conn_removed(self, engine_ignore, uri):
        if uri not in self.conn_menuitems:
            return

        menu_item = self.conn_menuitems[uri]
        self.systray_menu.remove(menu_item)
        menu_item.destroy()
        del(self.conn_menuitems[uri])
        self.conn_vm_menuitems[uri] = {}

        self.repopulate_menu_list()

    def conn_state_changed(self, conn):
        sensitive = conn.is_active()
        menu_item = self.conn_menuitems[conn.get_uri()]
        menu_item.set_sensitive(sensitive)

    def populate_vm_list(self, conn):
        uri = conn.get_uri()
        conn_menu_item = self.conn_menuitems[uri]
        vm_submenu = conn_menu_item.get_submenu()

        # Empty conn menu
        for c in vm_submenu.get_children():
            vm_submenu.remove(c)

        vm_mappings = {}
        for vm in conn.list_vms():
            vm_mappings[vm.get_name()] = vm.get_connkey()

        vm_names = vm_mappings.keys()
        vm_names.sort()

        if len(vm_names) == 0:
            menu_item = Gtk.MenuItem.new_with_label(_("No virtual machines"))
            menu_item.set_sensitive(False)
            vm_submenu.insert(menu_item, 0)
            return

        for i, name in enumerate(vm_names):
            connkey = vm_mappings[name]
            if connkey in self.conn_vm_menuitems[uri]:
                vm_item = self.conn_vm_menuitems[uri][connkey]
                vm_submenu.insert(vm_item, i)

    def vm_added(self, conn, connkey):
        uri = conn.get_uri()
        vm = conn.get_vm(connkey)
        if not vm:
            return
        vm.connect("state-changed", self.vm_state_changed)

        vm_mappings = self.conn_vm_menuitems[uri]
        if connkey in vm_mappings:
            return

        # Build VM list entry
        menu_item = Gtk.ImageMenuItem.new_with_label(vm.get_name())
        menu_item.set_use_underline(False)

        vm_mappings[connkey] = menu_item
        vm_action_menu = vmmenu.VMActionMenu(self, lambda: vm)
        menu_item.set_submenu(vm_action_menu)
        self.vm_action_dict[connkey] = vm_action_menu

        # Add VM to menu list
        self.populate_vm_list(conn)

        # Update state
        self.vm_state_changed(vm)
        menu_item.show()

    def vm_removed(self, conn, connkey):
        uri = conn.get_uri()
        vm_mappings = self.conn_vm_menuitems[uri]
        if not vm_mappings:
            return

        if connkey not in vm_mappings:
            return

        conn_item = self.conn_menuitems[uri]
        vm_menu_item = vm_mappings[connkey]
        vm_menu = conn_item.get_submenu()
        vm_menu.remove(vm_menu_item)
        vm_menu_item.destroy()
        del(vm_mappings[connkey])

        if len(vm_menu.get_children()) == 0:
            placeholder = Gtk.MenuItem.new_with_label(
                _("No virtual machines"))
            placeholder.show()
            placeholder.set_sensitive(False)
            vm_menu.add(placeholder)

    def vm_state_changed(self, vm):
        menu_item = self._get_vm_menu_item(vm)
        if not menu_item:
            return

        self._set_vm_status_icon(vm, menu_item)

        # Update action widget states
        menu = self.vm_action_dict[vm.get_connkey()]
        menu.update_widget_states(vm)

    def exit_app(self, ignore):
        self.emit("action-exit-app")