Пример #1
0
    def from_dotdesktop(cls, app_def: Path,
                        distro: WSLDistro) -> Optional[WSLApp]:
        """
        Return a WSLApp from a .desktop file.

        Args:
            app_def: .desktop file path
        """
        de = DesktopEntry(app_def)
        name = de.getName()
        generic_name = de.getGenericName()
        cmd = de.getExec()
        gui = not de.getTerminal()
        icon = de.getIcon()
        if name:
            return cls(name, generic_name, cmd, gui, icon)
        else:
            # This is a symlink
            linux_path = str(app_def)[str(app_def).index(r"\\wsl$") + 7:]
            linux_path = linux_path[linux_path.index("\\"):].replace("\\", "/")
            # Turn symlink target into Windows Path
            symlink = distro.get_cmd_output(f"readlink -f -e '{linux_path}'")
            app_def = distro._unc_path_from_cmd(symlink)

            de = DesktopEntry(app_def)
            name = de.getName()
            generic_name = de.getGenericName()
            cmd = de.getExec()
            gui = not de.getTerminal()
            icon = de.getIcon()
            if name:
                return cls(name, generic_name, cmd, gui, icon)
        return None
Пример #2
0
 def test_values(self):
     entry = DesktopEntry(self.test_file)
     self.assertEqual(entry.getName(), 'gedit')
     self.assertEqual(entry.getGenericName(), 'Text Editor')
     self.assertEqual(entry.getNoDisplay(), False)
     self.assertEqual(entry.getComment(), 'Edit text files')
     self.assertEqual(entry.getIcon(), 'accessories-text-editor')
     self.assertEqual(entry.getHidden(), False)
     self.assertEqual(entry.getOnlyShowIn(), [])
     self.assertEqual(entry.getExec(), 'gedit %U')
     self.assertEqual(entry.getTerminal(), False)
     self.assertEqual(entry.getMimeTypes(), ['text/plain'])
     self.assertEqual(entry.getCategories(), ['GNOME', 'GTK', 'Utility', 'TextEditor'])
     self.assertEqual(entry.getTerminal(), False)
Пример #3
0
 def test_values(self):
     entry = DesktopEntry(self.test_file)
     self.assertEqual(entry.getName(), 'gedit')
     self.assertEqual(entry.getGenericName(), 'Text Editor')
     self.assertEqual(entry.getNoDisplay(), False)
     self.assertEqual(entry.getComment(), 'Edit text files')
     self.assertEqual(entry.getIcon(), 'accessories-text-editor')
     self.assertEqual(entry.getHidden(), False)
     self.assertEqual(entry.getOnlyShowIn(), [])
     self.assertEqual(entry.getExec(), 'gedit %U')
     self.assertEqual(entry.getTerminal(), False)
     self.assertEqual(entry.getMimeTypes(), ['text/plain'])
     self.assertEqual(entry.getCategories(),
                      ['GNOME', 'GTK', 'Utility', 'TextEditor'])
     self.assertEqual(entry.getTerminal(), False)
Пример #4
0
    def provide(self):
        from fnmatch import fnmatch
        from xdg.DesktopEntry import DesktopEntry

        items = []

        for app_directory in map(os.path.expanduser, self.app_directories):
            for root, dirs, files in os.walk(app_directory):
                for filename in files:
                    if fnmatch(filename, "*.desktop"):
                        app_entry = DesktopEntry(os.path.join(root, filename))

                        icon_theme = Gtk.IconTheme.get_default()

                        if app_entry.getNoDisplay():
                            continue

                        if app_entry.getIcon() == "":
                            icon = Gtk.IconTheme.load_icon(icon_theme, "image-missing", self.icon_size, 0)
                        elif "/" in app_entry.getIcon():
                            try:
                                unscaled_icon = GdkPixbuf.Pixbuf.new_from_file(app_entry.getIcon())
                                icon = unscaled_icon.scale_simple(self.icon_size, self.icon_size, GdkPixbuf.InterpType.BILINEAR)
                            except:
                                icon = Gtk.IconTheme.load_icon(icon_theme, "image-missing", self.icon_size, 0)
                        else:
                            try:
                                unscaled_icon = Gtk.IconTheme.load_icon(icon_theme, app_entry.getIcon(), self.icon_size, 0)
                                icon = unscaled_icon.scale_simple(self.icon_size, self.icon_size, GdkPixbuf.InterpType.BILINEAR)
                            except:
                                icon = Gtk.IconTheme.load_icon(icon_theme, "image-missing", self.icon_size, 0)


                        words = app_entry.getName().split()
                        # words.append(app_entry.getExec())

                        command = self.escape_command(app_entry.getExec())


                        if app_entry.getTerminal():
                            command = "%s '%s'" % (self.terminal_emulator_command, command)

                        item = {
                            "indexer": self,

                            "name": app_entry.getName(),
                            "description": app_entry.getComment(),
                            "icon": icon,

                            "command": command,

                            "words": words,
                        }

                        items.append(item)

        items.sort(key=lambda i: i["name"])

        return items
Пример #5
0
def get_desktop_entries():

    starts = ((os.path.expanduser('~'), 'local'), (os.sep, 'usr'))
    end = ('share', 'applications', '*.desktop')

    for start in starts:
        pattern = os.path.join(*(start + end))

        for desktop_entry in glob.iglob(pattern):
            entry = DesktopEntry(desktop_entry)
            if entry.getNoDisplay():
                continue
            if entry.getOnlyShowIn():
                continue
            if entry.getTerminal():
                continue
            yield entry
Пример #6
0
 def add_desktop(self, filename):
     try:
         d = DesktopEntry(filename)
         if d.getHidden() or d.getNoDisplay() or d.getTerminal(
         ) or d.getType() != 'Application':
             return
         app = Application(name=d.getName(),
                           command_line=get_command(d),
                           mtime=datetime.fromtimestamp(
                               os.path.getmtime(filename)),
                           is_desktop=True)
         if d.getPath():
             app.path = d.getPath()
         self.add_app(app)
     except (xdg.Exceptions.ParsingError,
             xdg.Exceptions.DuplicateGroupError,
             xdg.Exceptions.DuplicateKeyError):
         pass
Пример #7
0
    def _generate_app_actions(self, path):
        app_actions = []
        filename_pattern = os.path.expanduser(os.path.join(
                path,
                AppsOperator.FILE_TYPE
        ))
        for filename in glob.glob(filename_pattern):
            app = DesktopEntry(filename)

            # TODO: Comply with  full DesktopEntries spec - OnlyShowIn, TryExec
            if app.getType() == "Application" and not app.getNoDisplay() and \
                    not app.getHidden() and not app.getTerminal():
                action = AppAction(
                    name=app.getName(),
                    description="application",
                    run=self._launch_application,
                    data={"desktop_entry": app, "cmd": self.get_cmd(app)},
                    icon=get_icon(app.getIcon()),
                )
                app_actions.append(action)
        return app_actions
Пример #8
0
    def from_dotdesktop(app_def):
        """
        Return a WSLApp from a .desktop file.

        Args:
            app_def: .desktop file path
        """
        de = DesktopEntry(app_def)
        name = de.getName()
        generic_name = de.getGenericName()
        cmd = de.getExec()
        gui = not de.getTerminal()
        icon = de.getIcon()

        return {
            "name": name,
            "generic_name": generic_name,
            "cmd": cmd,
            "gui": gui,
            "icon": icon
        }
withpty = which("withpty")

for dfile in itertools.chain.from_iterable(
        glob(os.path.join(path, '*.desktop'))
        for path in BaseDirectory.load_data_paths('applications')
):
    desktop = DesktopEntry(dfile)
    fname = os.path.splitext(os.path.basename(dfile))[0]

    try:
        cmd = FILTER.sub('', desktop.getExec()).split(' ', 1)
        cmd[0] = which(cmd[0])
    except ProgramNotFound:
        continue

    if desktop.getTerminal():
        cmd.insert(0, withpty)

    #service_path = os.path.join(generator_dir, fname)
    service_path = os.path.join(generator_dir, fname + "@.service")
    service = IniFile()
    service.addGroup('Unit')
    service.set('SourcePath', dfile, 'Unit')
    if not desktop.getHidden():
        service.set('X-ShowInMenu', "True", 'Unit')
    service.set('Description', desktop.getName(), 'Unit')
    service.addGroup('Service')
    service.set('EnvironmentFile', "%t/sessions/%i/environ", 'Service')
    service.set('ExecStart', " ".join(cmd), 'Service')

    service.defaultGroup = 'Unit'