This file is indexed.

/usr/lib/python3/dist-packages/sgtlauncher/SgtLauncher.py is in sgt-launcher 0.2.4-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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
#
#   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 warranties of
#   MERCHANTABILITY, SATISFACTORY QUALITY, 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 gi
import os

from locale import gettext as _

gi.require_version('Gtk', '3.0')
gi.require_version('Gdk', '3.0')

from gi.repository import Gdk  # nopep8
from gi.repository import GdkPixbuf  # nopep8
from gi.repository import Gtk  # nopep8
from gi.repository import Gio  # nopep8
from gi.repository import GLib  # nopep8
from gi.repository import Pango  # nopep8

from . import SgtSocketLauncher  # nopep8
import sgtlauncher_lib  # nopep8


class MyWindow(Gtk.ApplicationWindow):
    def __init__(self, app, appname, launchers):
        self.appname = appname
        self.title = _("SGT Puzzles Collection")

        Gtk.Window.__init__(self, title=self.title, application=app)
        self.set_title(self.title)
        self.set_role(self.title)
        self.set_startup_id("sgt-launcher")
        self.set_default_icon_name(appname)
        self.set_default_size(800, 600)
        self.set_position(Gtk.WindowPosition.CENTER)

        self.launcher = SgtSocketLauncher.SgtSocketLauncher()
        self.loading = False
        self.load_id = 0
        self.load_retry = 0
        self.load_title = ""

        self.socket = self.launcher.get_socket()
        self.socket.connect("plug_removed", self.socket_disconnect)
        self.socket.connect("plug_added", self.socket_connect)

        display = Gdk.Display.get_default()
        seat = display.get_default_seat()
        self.keyboard = seat.get_keyboard()

        self.setup_ui(launchers)

    def setup_ui(self, launchers):
        """Initialize the headerbar, actions, and individual views"""
        self.hb = Gtk.HeaderBar()
        self.hb.props.show_close_button = True
        self.hb.props.title = self.title
        self.set_titlebar(self.hb)

        self.stack = Gtk.Stack.new()
        self.add(self.stack)

        self.setup_action_buttons()
        self.setup_launcher_view(launchers)
        self.setup_loading_view()
        self.setup_game_view()

    def setup_action_buttons(self):
        """Initialize the in-game action buttons"""
        # Button definitions
        buttons = {
            "new-game": (_("New Game"), "document-new-symbolic", 57,
                         Gdk.KEY_n),
            "undo": (_("Undo"), "edit-undo-symbolic", 30, Gdk.KEY_u),
            "redo": (_("Redo"), "edit-redo-symbolic", 27, Gdk.KEY_r)
        }

        # Setup Action buttons
        self.action_buttons = {}
        for key in list(buttons.keys()):
            # Create the button
            self.action_buttons[key] = Gtk.Button.new_from_icon_name(
                buttons[key][1], Gtk.IconSize.LARGE_TOOLBAR)
            # Add a tooltip
            self.action_buttons[key].set_tooltip_text(buttons[key][0])
            # Enable hiding
            self.action_buttons[key].set_no_show_all(True)
            # Connect the clicked event
            self.action_buttons[key].connect("clicked",
                                             self.on_keyboard_button_click,
                                             buttons[key][2], buttons[key][3])

        # Add the buttons
        self.hb.pack_start(self.action_buttons["new-game"])

        buttonbox = Gtk.Box.new(Gtk.Orientation.HORIZONTAL, 0)
        ctx = buttonbox.get_style_context()
        ctx.add_class("linked")

        buttonbox.pack_start(self.action_buttons["undo"], False, False, 0)
        buttonbox.pack_start(self.action_buttons["redo"], False, False, 0)
        self.hb.pack_start(buttonbox)

    def setup_launcher_view(self, launchers):
        """Initialize the launcher view with the games"""
        # Populate the model (name, comment, icon_name, exe)
        listmodel = Gtk.ListStore(str, str, str, str)
        for launcher in launchers:
            listmodel.append(launcher)

        # Initialize the treeview
        view = Gtk.TreeView(model=listmodel)
        view.set_headers_visible(False)

        # Create and pack the custom column
        col = Gtk.TreeViewColumn.new()

        # Pixbuf: icon renderer
        pixbuf = Gtk.CellRendererPixbuf()
        pixbuf.props.stock_size = Gtk.IconSize.DIALOG
        col.pack_start(pixbuf, False)
        col.add_attribute(pixbuf, "icon-name", 2)

        # Text: label renderer
        text = Gtk.CellRendererText()
        text.props.wrap_mode = Pango.WrapMode.WORD
        col.pack_start(text, True)
        col.add_attribute(text, "text", 0)

        # Draw custom cell
        col.set_cell_data_func(text, self.treeview_cell_text_func, None)
        col.set_cell_data_func(pixbuf, self.treeview_cell_pixbuf_func, None)

        # Add the column and enable typeahead
        view.append_column(col)
        view.set_search_column(1)

        view.connect("row-activated", self.on_activated)

        # Add the treeview to a scrolled window
        scrolled = Gtk.ScrolledWindow.new()
        scrolled.add(view)

        self.stack.add_named(scrolled, "launcher")

    def setup_loading_view(self):
        """Initialize the loading view used to correctly embed the window"""
        parent_box = Gtk.Box.new(Gtk.Orientation.VERTICAL, 0)
        child_box = Gtk.Box.new(Gtk.Orientation.VERTICAL, 6)

        self.launching_image = Gtk.Image.new()
        self.launching_title = Gtk.Label.new()
        self.launching_label = Gtk.Label.new(_("Please wait..."))

        spinner = Gtk.Spinner.new()
        spinner.start()

        child_box.pack_start(self.launching_image, False, False, 0)
        child_box.pack_start(self.launching_title, False, False, 0)
        child_box.pack_start(self.launching_label, False, False, 0)
        child_box.pack_start(spinner, False, False, 0)

        parent_box.set_center_widget(child_box)

        self.stack.add_named(parent_box, "loading")

    def setup_game_view(self):
        """Initialize the game view where the game is embedded"""
        self.stack.add_named(self.socket, "game")

    # Callables
    def hide_actions(self):
        """Hide the in-game action buttons"""
        for key in list(self.action_buttons.keys()):
            self.action_buttons[key].hide()

    def show_actions(self):
        """Show the in-game action buttons"""
        for key in list(self.action_buttons.keys()):
            self.action_buttons[key].show()

    def set_subtitle(self, title):
        """Set the window subtitle"""
        self.hb.set_subtitle(title)

    def launch(self, title, icon_name, path):
        """Launch the specified application"""
        subtitle = _("Loading %s") % title

        if os.path.isfile(icon_name):
            self.launching_image.set_from_file(icon_name)
        else:
            self.launching_image.set_from_icon_name(icon_name,
                                                    Gtk.IconSize.DIALOG)
        self.launching_title.set_markup("<b>%s</b>" % title)
        self.set_view("loading", icon_name, subtitle)

        self.load_icon = icon_name
        self.load_title = title
        self.load_retry = 0
        self.loading_path = path
        self.threaded_launch()

    def threaded_launch(self):
        """
        Run the application launch in a separate thread, ensuring the window
        is correctly embedded
        """
        if self.loading:
            # Application is successfully launched, switch to the game view
            self.loading = False
            self.load_retry = 0

            # Stop the load thread
            GLib.source_remove(self.load_id)
            self.load_id = 0

            self.set_view("game", self.load_icon, self.load_title)
            self.grab_focus()
            return True
        if self.load_retry < 10:
            # Application is not fully launched yet
            self.loading = True
            self.launcher.launch(self.loading_path)
            self.load_id = GLib.timeout_add(1000, self.threaded_launch)
            self.grab_focus()
        else:
            # Application failed to load, return to the launcher
            self.loading = False
            self.load_retry = 0
            self.set_view("launcher")
            self.grab_focus()
            return True
        return False

    def set_view(self, name, icon_name=None, subtitle=None):
        """Change the view, setting the icon name and subtitle"""
        if name is "launcher":
            self.stack.set_transition_type(Gtk.StackTransitionType.OVER_RIGHT)
            self.hide_actions()
        if name is "loading":
            self.stack.set_transition_type(Gtk.StackTransitionType.OVER_LEFT)
            self.hide_actions()
        if name is "game":
            self.stack.set_transition_type(Gtk.StackTransitionType.OVER_LEFT)
            self.show_actions()

        title = self.title
        if icon_name is None:
            icon_name = self.appname
        if subtitle is None:
            subtitle = ""
        else:
            title = "%s - %s" % (title, subtitle)

        if os.path.isfile(icon_name):
            self.set_default_icon_from_file(icon_name)
        else:
            self.set_default_icon_name(icon_name)

        self.set_subtitle(subtitle)
        self.stack.set_visible_child_name(name)

        self.get_window().set_title(title)

        if name is "game":
            self.socket.grab_focus()

    # Events
    def socket_connect(self, socket):
        """Embedded window connected"""
        self.socket = self.launcher.get_socket()
        return True

    def socket_disconnect(self, socket):
        """Embedded window disconnected"""
        if self.loading:
            self.loading = False
            self.load_retry += 1
            GLib.source_remove(self.load_id)
            self.load_id = GLib.timeout_add(1000, self.threaded_launch)
        else:
            self.set_view("launcher")
        return True

    def on_keyboard_button_click(self, button, keycode, keyval):
        """Send keypress to embedded window on button click"""
        self.socket.grab_focus()
        event = Gdk.Event.new(Gdk.EventType.KEY_PRESS)
        event.keyval = keyval
        event.hardware_keycode = keycode
        event.window = self.get_window()
        event.set_device(self.keyboard)
        event.put()

    def treeview_cell_text_func(self, col, renderer, model, treeiter, data):
        """Render the treeview row"""
        name, comment, icon_name, exe = model[treeiter][:]
        markup = "<b>%s</b>\n%s" % (name, comment)
        renderer.set_property('markup', markup)
        return

    def treeview_cell_pixbuf_func(self, col, renderer, model, treeiter, data):
        """Render the treeview row"""
        name, comment, icon_name, exe = model[treeiter][:]
        if os.path.isfile(icon_name):
            pixbuf = GdkPixbuf.Pixbuf.new_from_file_at_size(icon_name, 48, 48)
            renderer.set_property("pixbuf", pixbuf)
        return

    def on_activated(self, widget, treeiter, col):
        """Launch the selected application"""
        model = widget.get_model()
        name, comment, icon_name, exe = model[treeiter][:]
        self.launch(name, icon_name, exe)


class MyAboutDialog(Gtk.AboutDialog):
    def __init__(self, appname, title, parent):
        Gtk.AboutDialog.__init__(self)
        self.set_program_name(title)
        self.set_transient_for(parent)
        self.set_logo_icon_name(appname)
        self.set_modal(True)

        self.set_authors([
            "Sean Davis (SGT Puzzles)",
            "Simon Tatham (Simon Tatham's Portable Puzzle Collection)"
        ])
        self.set_copyright(
            "SGT Puzzles\n"
            "© 2016-2017 Sean Davis\n"
            "\n"
            "Simon Tatham's Portable Puzzle Collection\n"
            "© 2004-2012 Simon Tatham"
        )
        self.set_artists([
            "Pasi Lallinaho"
        ])
        self.set_website("https://launchpad.net/sgt-launcher")
        self.set_website_label("SGT Puzzle Launcher on Launchpad")
        self.set_license_type(Gtk.License.GPL_3_0)
        self.set_version(sgtlauncher_lib.get_version())

        self.connect("response", self.on_response)

    def on_response(self, dialog, response):
        self.hide()
        self.destroy()


class MyApplication(Gtk.Application):
    APPNAME = "sgt-launcher"
    TITLE = _("SGT Puzzles Collection")
    GAMES = [
        'blackbox',
        'bridges',
        'cube',
        'dominosa',
        'fifteen',
        'filling',
        'flip',
        'flood',
        'galaxies',
        'guess',
        'inertia',
        'keen',
        'lightup',
        'loopy',
        'magnets',
        'map',
        'mines',
        'net',
        'netslide',
        'palisade',
        'pattern',
        'pearl',
        'pegs',
        'range',
        'rect',
        'samegame',
        'signpost',
        'singles',
        'sixteen',
        'slant',
        'solo',
        'tents',
        'towers',
        'tracks',
        'twiddle',
        'undead',
        'unequal',
        'unruly',
        'untangle'
    ]

    def __init__(self):
        Gtk.Application.__init__(self)

    def do_activate(self):
        launchers = self.get_launchers()
        self.win = MyWindow(self, self.APPNAME, launchers)
        self.win.show_all()

    def do_startup(self):
        Gtk.Application.do_startup(self)

        menu = Gio.Menu()
        # menu.append(_("Preferences"), "app.show-preferences")
        menu.append(_("About"), "app.about")
        menu.append(_("Quit"), "app.quit")
        self.set_app_menu(menu)

        prefs_action = Gio.SimpleAction.new("show-preferences", None)
        prefs_action.connect("activate", self.prefs_cb)
        self.add_action(prefs_action)

        about_action = Gio.SimpleAction.new("about", None)
        about_action.connect("activate", self.about_cb)
        self.add_action(about_action)

        quit_action = Gio.SimpleAction.new("quit", None)
        quit_action.connect("activate", self.quit_cb)
        self.add_action(quit_action)

    def prefs_cb(self, action, parameter):
        print("Not implemented")

    def about_cb(self, action, parameter):
        """Show the about dialog"""
        about = MyAboutDialog(self.APPNAME, self.TITLE, self.win)
        about.run()

    def quit_cb(self, action, parameter):
        """Exit application"""
        self.quit()

    def exists_in_path(self, binary):
        realpath = os.path.realpath(binary)
        if os.path.exists(realpath):
            return True
        paths = (os.environ.get("PATH")).split(":")
        paths.reverse()
        for path in paths:
            fullpath = os.path.join(path, binary)
            if (os.path.exists(fullpath)):
                return True
        return False

    def get_launchers(self):
        """Get localized launcher contents"""
        flags = GLib.KeyFileFlags.NONE
        launchers = []
        for game in self.GAMES:
            for prefix in ["sgt", "puzzle"]:
                launcher = "applications/%s-%s.desktop" % (prefix, game)
                keyfile = GLib.KeyFile.new()
                try:
                    if (keyfile.load_from_data_dirs(launcher, flags)):
                        data = [
                            keyfile.get_value("Desktop Entry", "Name"),
                            keyfile.get_value("Desktop Entry", "Comment"),
                            keyfile.get_value("Desktop Entry", "Icon"),
                            keyfile.get_value("Desktop Entry", "Exec"),
                        ]
                        if self.exists_in_path(data[3]):
                            launchers.append(data)
                    break
                except GLib.Error:
                    pass
        launchers.sort()
        return launchers