Add removal of game shortcuts (if exist) in context menu

This commit is contained in:
Xodetaetl 2015-08-02 14:35:34 +02:00
parent 3c2c5b526a
commit 3b5bf2fe47
3 changed files with 76 additions and 21 deletions

View file

@ -5,9 +5,8 @@ import time
from gi.repository import Gtk, Gdk, GLib
from lutris import api, pga, settings
from lutris import api, pga, settings, shortcuts
from lutris.game import Game, get_game_list
from lutris.shortcuts import create_launcher
from lutris.sync import Sync
from lutris.util import runtime
@ -130,7 +129,9 @@ class LutrisWindow(object):
('configure', "Configure", self.edit_game_configuration),
('browse', "Browse files", self.on_browse_files),
('desktop-shortcut', "Create desktop shortcut", self.create_desktop_shortcut),
('rm-desktop-shortcut', "Remove desktop shortcut", self.remove_desktop_shortcut),
('menu-shortcut', "Create application menu shortcut", self.create_menu_shortcut),
('rm-menu-shortcut', "Remove application menu shortcut", self.remove_menu_shortcut),
('remove', "Remove", self.on_remove_game),
]
self.menu = ContextualMenu(main_entries)
@ -538,15 +539,24 @@ class LutrisWindow(object):
def create_menu_shortcut(self, *args):
"""Add the game to the system's Games menu."""
game_slug = slugify(self.view.selected_game)
create_launcher(game_slug, menu=True)
game_name = Game(game_slug).name
shortcuts.create_launcher(game_slug, game_name, menu=True)
dialogs.NoticeDialog(
"Shortcut added to the Games category of the global menu.")
def create_desktop_shortcut(self, *args):
"""Add the game to the system's Games menu."""
game_slug = slugify(self.view.selected_game)
create_launcher(game_slug, desktop=True)
dialogs.NoticeDialog('Shortcut created on your desktop.')
game_name = Game(game_slug).name
shortcuts.create_launcher(game_slug, game_name, desktop=True)
def remove_menu_shortcut(self, *args):
game_slug = slugify(self.view.selected_game)
shortcuts.remove_launcher(game_slug, menu=True)
def remove_desktop_shortcut(self, *args):
game_slug = slugify(self.view.selected_game)
shortcuts.remove_launcher(game_slug, desktop=True)
def toggle_sidebar(self):
if self.sidebar_viewport.is_visible():

View file

@ -10,6 +10,7 @@ from lutris.config import LutrisConfig
from lutris.gui.cellrenderers import GridViewCellRendererText
from lutris.downloader import Downloader
from lutris.runners import import_runner
from lutris.shortcuts import desktop_launcher_exists, menu_launcher_exists
from lutris.util import datapath
# from lutris.util.log import logger
from lutris.util.system import reverse_expanduser
@ -107,6 +108,9 @@ class ContextualMenu(Gtk.Menu):
self.append(menuitem)
def popup(self, event, game_row):
game_slug = game_row[COL_ID]
runner_slug = game_row[COL_RUNNER]
# Clear existing menu
for item in self.get_children():
self.remove(item)
@ -115,10 +119,9 @@ class ContextualMenu(Gtk.Menu):
self.add_menuitems(self.main_entries)
# Runner specific items
runner_entries = None
runner_slug = game_row[COL_RUNNER]
if runner_slug:
game_config = LutrisConfig(runner_slug=runner_slug,
game_slug=game_row[COL_ID])
game_slug=game_slug)
runner = import_runner(runner_slug)(game_config)
runner_entries = runner.context_menu_entries
if runner_entries:
@ -132,8 +135,10 @@ class ContextualMenu(Gtk.Menu):
'add': is_installed,
'play': not is_installed,
'configure': not is_installed,
'desktop-shortcut': not is_installed,
'menu-shortcut': not is_installed,
'desktop-shortcut': not is_installed or desktop_launcher_exists(game_slug),
'menu-shortcut': not is_installed or menu_launcher_exists(game_slug),
'rm-desktop-shortcut': not is_installed or not desktop_launcher_exists(game_slug),
'rm-menu-shortcut': not is_installed or not menu_launcher_exists(game_slug),
'browse': not is_installed or game_row[COL_RUNNER] == 'browser',
}
for menuitem in self.get_children():

View file

@ -4,26 +4,26 @@ import stat
import shutil
import subprocess
from textwrap import dedent
from xdg import BaseDirectory
from lutris.game import Game
from lutris.settings import CACHE_DIR
def create_launcher(game_slug, desktop=False, menu=False):
""" Create desktop file """
game = Game(game_slug)
def create_launcher(game_slug, game_name, desktop=False, menu=False):
"""Create .desktop file."""
desktop_dir = subprocess.Popen(['xdg-user-dir', 'DESKTOP'],
stdout=subprocess.PIPE).communicate()[0]
desktop_dir = desktop_dir.strip()
launcher_content = """[Desktop Entry]
Type=Application
Name=%s
Icon=%s
Exec=lutris lutris:%s
Categories=Game
""" % (game.name, 'lutris_' + game_slug, game_slug)
launcher_content = dedent(
"""
[Desktop Entry]
Type=Application
Name=%s
Icon=%s
Exec=lutris lutris:%s
Categories=Game
""" % (game_name, 'lutris_' + game_slug, game_slug))
launcher_filename = "%s.desktop" % game_slug
tmp_launcher_path = os.path.join(CACHE_DIR, launcher_filename)
@ -41,3 +41,43 @@ Categories=Game
shutil.copy(tmp_launcher_path,
os.path.join(menu_path, launcher_filename))
os.remove(tmp_launcher_path)
def desktop_launcher_exists(game_slug):
filename = "%s.desktop" % game_slug
desktop_dir = subprocess.Popen(['xdg-user-dir', 'DESKTOP'],
stdout=subprocess.PIPE).communicate()[0]
desktop_dir = desktop_dir.strip()
file_path = os.path.join(desktop_dir, filename)
if os.path.exists(file_path):
return True
return False
def menu_launcher_exists(game_slug):
filename = "%s.desktop" % game_slug
menu_path = os.path.join(BaseDirectory.xdg_data_home, 'applications')
file_path = os.path.join(menu_path, filename)
if os.path.exists(file_path):
return True
return False
def remove_launcher(game_slug, desktop=False, menu=False):
"""Remove existing .desktop file."""
filename = "%s.desktop" % game_slug
if desktop:
desktop_dir = subprocess.Popen(['xdg-user-dir', 'DESKTOP'],
stdout=subprocess.PIPE).communicate()[0]
desktop_dir = desktop_dir.strip()
file_path = os.path.join(desktop_dir, filename)
if os.path.exists(file_path):
os.remove(file_path)
if menu:
menu_path = os.path.join(BaseDirectory.xdg_data_home, 'applications')
file_path = os.path.join(menu_path, filename)
if os.path.exists(file_path):
os.remove(file_path)