Skip to content

Commit

Permalink
Generate channel search providers
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanmccall committed Sep 21, 2021
1 parent 8e0b21d commit 8741eab
Showing 1 changed file with 68 additions and 4 deletions.
72 changes: 68 additions & 4 deletions kolibri_app_desktop_xdg_plugin/channel_launchers.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,13 @@
LAUNCHER_CATEGORIES = ("Education", "X-Kolibri-Channel")

KOLIBRI_APP_ID = os.environ.get("FLATPAK_ID", "org.learningequality.Kolibri")
KOLIBRI_SEARCH_PROVIDER_BUS_NAME = KOLIBRI_APP_ID + ".SearchProvider"
KOLIBRI_SEARCH_PROVIDER_OBJECT_PATH = "/" + KOLIBRI_SEARCH_PROVIDER_BUS_NAME.replace(".", "/")

CHANNEL_DESKTOP_ID_FORMAT = KOLIBRI_APP_ID + ".channel_{}"
CHANNEL_SEARCH_PROVIDER_OBJECT_PATH_FORMAT = (
KOLIBRI_SEARCH_PROVIDER_OBJECT_PATH + "/channel_{}"
)


def update_channel_launchers(force=False):
Expand Down Expand Up @@ -83,6 +88,12 @@ class ChannelLaunchersContext(object):
def applications_dir(self):
return os.path.join(get_content_share_dir_path(), "applications")

@property
def search_providers_dir(self):
return os.path.join(
get_content_share_dir_path(), "gnome-shell", "search-providers"
)

@property
def icon_theme_dir(self):
return os.path.join(get_content_share_dir_path(), "icons", "hicolor")
Expand Down Expand Up @@ -114,8 +125,17 @@ def desktop_file_path(self):
@property
def desktop_file_name(self):
return "{}.desktop".format(self.desktop_id)

@property
def search_provider_file_path(self):
return os.path.join(
self.__context.search_providers_dir, self.search_provider_file_name
)

@property
def search_provider_file_name(self):
return "{}.ini".format(self.desktop_id)

def get_icon_file_path(self, file_name, size="256x256"):
return os.path.join(self.__context.icon_theme_dir, size, "apps", file_name)

Expand All @@ -127,7 +147,10 @@ def compare(self, other):
return (self_channel - other_channel) or (self_format - other_format)

def is_same_channel(self, other):
return self.channel_id == other.channel_id
return (
self.desktop_file_path == other.desktop_file_path
and self.channel_id == other.channel_id
)

def save(self):
try:
Expand All @@ -145,8 +168,18 @@ def save(self):
"Error writing desktop file for channel %s: %s", self.channel_id, error
)

try:
self.write_search_provider()
except Exception as error:
logger.warning(
"Error writing search provider for channel %s: %s",
self.channel_id,
error,
)

def delete(self):
self.delete_desktop_file()
self.delete_search_provider()
self.delete_channel_icon()

def write_desktop_file(self, icon_name):
Expand All @@ -155,6 +188,12 @@ def write_desktop_file(self, icon_name):
def delete_desktop_file(self):
os.remove(self.desktop_file_path)

def write_search_provider(self):
raise NotImplementedError()

def delete_search_provider(self):
os.remove(self.search_provider_file_path)

def write_channel_icon(self):
raise NotImplementedError()

Expand All @@ -163,7 +202,7 @@ def delete_channel_icon(self):


class ChannelLauncher_FromDatabase(ChannelLauncher):
FORMAT_VERSION = 5
FORMAT_VERSION = 6

def __init__(self, context, channelmetadata):
super().__init__(context)
Expand Down Expand Up @@ -202,7 +241,9 @@ def write_desktop_file(self, icon_name):
desktop_file_parser.set(
"Desktop Entry",
"Exec",
f"gio open kolibri-channel:{self.channel_id}",
"gio open x-kolibri-dispatch://{channel_id}".format(
channel_id=self.channel_id
),
)
desktop_file_parser.set("Desktop Entry", "X-Endless-LaunchMaximized", "True")
desktop_file_parser.set(
Expand All @@ -222,6 +263,29 @@ def write_desktop_file(self, icon_name):
with open(self.desktop_file_path, "w") as desktop_entry_file:
desktop_file_parser.write(desktop_entry_file, space_around_delimiters=False)

def write_search_provider(self):
search_provider_file_parser = configparser.ConfigParser()
search_provider_file_parser.optionxform = str
search_provider_file_parser.add_section("Shell Search Provider")
search_provider_file_parser.set(
"Shell Search Provider", "DesktopId", self.desktop_file_name
)
search_provider_file_parser.set(
"Shell Search Provider", "BusName", KOLIBRI_SEARCH_PROVIDER_BUS_NAME
)
search_provider_file_parser.set(
"Shell Search Provider",
"ObjectPath",
CHANNEL_SEARCH_PROVIDER_OBJECT_PATH_FORMAT.format(self.channel_id),
)
search_provider_file_parser.set("Shell Search Provider", "Version", "2")

ensure_dir(self.search_provider_file_path)
with open(self.search_provider_file_path, "w") as search_provider_file:
search_provider_file_parser.write(
search_provider_file, space_around_delimiters=False
)

def write_channel_icon(self):
if not self.__channel_icon:
return
Expand Down Expand Up @@ -289,7 +353,7 @@ def delete_channel_icon(self):
icon_file_path = self.get_icon_file_path(icon_name + ".png")

if icon_file_path and os.path.isfile(icon_file_path):
try_remove(self.icon_file_path)
try_remove(icon_file_path)


class ChannelIcon(object):
Expand Down

0 comments on commit 8741eab

Please sign in to comment.