# Path: kano_apps/Media.py
# def media_dir():
#     for path in MEDIA_LOCS:
#         if os.path.exists(path):
#             return os.path.abspath(path) + '/'
# 
#     raise Exception('Media directory not found.')


import os
import json
import re
from gi import require_version
from gi.repository import Gtk, Gdk
from kano_apps.Media import media_dir
KDESK_EXEC = '/usr/bin/kdesk'


def _get_kdesk_icon_path(app):
    kdesk_dir = os.path.expanduser(KDESK_DIR)
    return kdesk_dir + re.sub(' ', '-', app["title"]) + ".lnk"


def _create_kdesk_icon(app):
    icon_theme = Gtk.IconTheme.get_default()
    icon_info = icon_theme.lookup_icon(app["icon"], 66, 0)

    icon = app["icon"]
    if icon_info is not None:
        icon = icon_info.get_filename()

    cmd = app["launch_command"]
    if type(app["launch_command"]) is dict:
        args = map(lambda s: "\"{}\"".format(s) if s.find(" ") >= 0 else s,
                   app["launch_command"]["args"])
        cmd = app["launch_command"]["cmd"]
        if len(args) > 0:
            cmd += " " + " ".join(args)

    kdesk_entry = 'table Icon\n'
    kdesk_entry += '  Caption:\n'
    kdesk_entry += '  AppID:\n'
    kdesk_entry += '  Command: {}\n'.format(cmd)
    kdesk_entry += '  Singleton: true\n'
    kdesk_entry += '  Icon: {}\n'.format(icon)

# Path: kano_apps/Media.py
# def media_dir():
#     for path in MEDIA_LOCS:
#         if os.path.exists(path):
#             return os.path.abspath(path) + '/'
# 
#     raise Exception('Media directory not found.')


# Path: kano_apps/DesktopManage.py
import os
import json
import re
from gi import require_version
from gi.repository import Gtk, Gdk
from kano_apps.Media import media_dir

KDESK_EXEC = '/usr/bin/kdesk'


def _get_kdesk_icon_path(app):
    kdesk_dir = os.path.expanduser(KDESK_DIR)
    return kdesk_dir + re.sub(' ', '-', app["title"]) + ".lnk"


def _create_kdesk_icon(app):
    icon_theme = Gtk.IconTheme.get_default()
    icon_info = icon_theme.lookup_icon(app["icon"], 66, 0)

    icon = app["icon"]
    if icon_info is not None:
        icon = icon_info.get_filename()

    cmd = app["launch_command"]
    if type(app["launch_command"]) is dict:
        args = map(lambda s: "\"{}\"".format(s) if s.find(" ") >= 0 else s,
                   app["launch_command"]["args"])
        cmd = app["launch_command"]["cmd"]
        if len(args) > 0:
            cmd += " " + " ".join(args)

    kdesk_entry = 'table Icon\n'
    kdesk_entry += '  Caption:\n'
    kdesk_entry += '  AppID:\n'
    kdesk_entry += '  Command: {}\n'.format(cmd)
    kdesk_entry += '  Singleton: true\n'
    kdesk_entry += '  Icon: {}\n'.format(icon)

    kdesk_entry += '  IconHover: {}\n'.format(media_dir() +