def mylib_create_banner(game_name):

    nebula_dir = sys.path[0]
    data_dir = os.getenv('HOME') + '/.games_nebula'

    banners_dir = data_dir + '/images/mylib/'
    banner_path = banners_dir + game_name + '.jpg'

    if not os.path.exists(banners_dir):
        os.makedirs(banners_dir)

    script_path_0 = data_dir + '/scripts/mylib/' + game_name + '/setup'
    script_path_1 = nebula_dir + '/scripts/mylib/' + game_name + '/setup'

    if os.path.exists(script_path_0):
        mylib_setup_script_path = script_path_0
    else:
        mylib_setup_script_path = script_path_1

    script_file = open(mylib_setup_script_path, 'r')
    file_content = script_file.readlines()
    raw_game_title = file_content[-1].replace('#', '').replace('\n', '')

    if sys.version_info[0] == 2:
        game_title = raw_game_title.decode('utf-8')
    elif sys.version_info[0] == 3:
        game_title = raw_game_title

    script_file.close()

    banner = Image.new('1', (518, 240), 0)
    draw = ImageDraw.Draw(banner)
    font = ImageFont.truetype(nebula_dir + '/fonts/DejaVuSans.ttf', 24)

    game_title_width = font.getsize(game_title)[0]
    game_title_height = font.getsize(game_title)[1]

    if game_title_width > 518:

        game_title_list = []
        for i in range(0, len(game_title), 28):
            game_title_list.append(game_title[i:i+28])

        all_lines_height = len(game_title_list) * game_title_height
        full_title_y = 120 - all_lines_height/2

        n = 0
        for line in game_title_list:
            line_width = font.getsize(line)[0]
            x = 259 - line_width/2
            y = full_title_y + (game_title_height * n)
            draw.text((x, y), line, fill=1, font=font)
            n += 1

    else:

        x = 259 - game_title_width/2
        y = 120 - game_title_height/2
        draw.text((x, y), game_title, fill=1, font=font)

    banner.save(banner_path, 'JPEG') def games_info(data_dir):
def get_info(games_list, mylib_dir): def mylib_tags_create(game_name, tags, tags_file):

    parser = ConfigParser()
    parser.read(tags_file)
    if 'mylib tags' in parser.sections():
        exists = True
    else:
        exists = False

    file = open(tags_file, 'w')
    if exists == False:
        parser.add_section('mylib tags')
    if tags == '':
        parser.remove_option('mylib tags', game_name)
    else:
        parser.set('mylib tags', game_name, str(tags))
    parser.write(file)
    file.close() def mylib_tags_get_all(tags_file):

    all_tags = []

    parser = ConfigParser()
    parser.read(tags_file)

    if 'mylib tags' in parser.sections():

        for game_name in parser.options('mylib tags'):
            game_tags = parser.get('mylib tags', game_name).split(',')

            if len(game_tags) != 0:
                for tag in game_tags:
                    if tag not in all_tags:
                        all_tags.append(tag)

    return sorted(all_tags) def mylib_tags_get(game_name, tags_file):

    parser = ConfigParser()
    parser.read(tags_file)
    if 'mylib tags' in parser.sections():
        if game_name in parser.options('mylib tags'):
            tags = parser.get('mylib tags', game_name).split(',')
        else:
            tags = []
    else:
        tags = []

    return tags def goglib_authorized(): def goglib_available(): def games_info(data_dir): def goglib_get_games_list():

    proc = subprocess.Popen(['lgogdownloader', '--exclude', \
    'all', '--list-details'],stdout=subprocess.PIPE)
    games_detailed_list = proc.stdout.readlines()
    stdoutdata, stderrdata = proc.communicate()

    if proc.returncode == 0:

        file_path = os.getenv('HOME') + '/.games_nebula/config/games_list'

        games_list_file = open(file_path, 'w')

        for line in games_detailed_list:
            if 'Getting game info' not in line.decode('utf-8'):
                games_list_file.write(b2str(line))

        return 0

    else:

        return 1 def goglib_tags_create(game_name, tags, tags_file):

    parser = ConfigParser()
    parser.read(tags_file)
    if 'goglib tags' in parser.sections():
        exists = True
    else:
        exists = False

    file = open(tags_file, 'w')
    if exists == False:
        parser.add_section('goglib tags')
    if tags == '':
        parser.remove_option('goglib tags', game_name)
    else:
        parser.set('goglib tags', game_name, str(tags))
    parser.write(file)
    file.close() def goglib_tags_get_all(tags_file):

    all_tags = []

    parser = ConfigParser()
    parser.read(tags_file)

    if 'goglib tags' in parser.sections():

        for game_name in parser.options('goglib tags'):
            game_tags = parser.get('goglib tags', game_name).split(',')

            if len(game_tags) != 0:
                for tag in game_tags:
                    if tag not in all_tags:
                        all_tags.append(tag)

    return sorted(all_tags) def goglib_tags_get(game_name, tags_file):

    parser = ConfigParser()
    parser.read(tags_file)
    if 'goglib tags' in parser.sections():
        if game_name in parser.options('goglib tags'):
            tags = parser.get('goglib tags', game_name).split(',')
        else:
            tags = []
    else:
        tags = []

    return tags def autosetup(lib, install_dir, game_name):

    parser = ConfigParser()

    if lib == 'goglib':
        banners_dir = os.getenv('HOME') + '/.games_nebula/images/goglib/'
        path_0 = os.getenv('HOME') + '/.games_nebula/scripts/goglib/autosetup.ini'
        path_1 = nebula_dir + '/scripts/goglib/autosetup.ini'
    elif lib == 'mylib':
        banners_dir = os.getenv('HOME') + '/.games_nebula/images/mylib/'
        path_0 = os.getenv('HOME') + '/.games_nebula/scripts/mylib/autosetup.ini'
        path_1 = nebula_dir + '/scripts/mylib/autosetup.ini'

    if os.path.exists(path_0):
        parser.read(path_0)
    else:
        parser.read(path_1)

    if (os.path.exists(install_dir + '/' + game_name)) and (parser.has_section(game_name)):

        if parser.has_option(game_name, 'image'):
            image = parser.get(game_name, 'image')
            if image != '':
                print("Downloading image")
                get_banner.get_banner(game_name, image, banners_dir, lib)

        if parser.has_option(game_name, 'native_exe'):
            native_exe = parser.get(game_name, 'native_exe')
            if native_exe != '':
                print("Writing start.sh")

                start_file_path = install_dir + '/' + game_name + '/start.sh'
                start_gog_path = install_dir + '/' + game_name + '/start_gog.sh'

                if os.path.exists(start_file_path):
                    os.rename(start_file_path, start_gog_path)

                start_lines = ['#!/bin/bash\n',
                'python "$NEBULA_DIR/launcher_native.py" ' + game_name]

                start_file = open(start_file_path, 'w')
                for line in start_lines:
                    start_file.write(line)
                start_file.close()
                os.chmod(start_file_path, 0o711)

                if '/' in native_exe:
                    native_exe_dir = native_exe.split('/')[0]
                    native_exe = native_exe.split('/')[1]
                    start_gn_lines = ['#!/bin/bash\n',
                    'cd "$INSTALL_DIR/' + game_name + '/game/' + native_exe_dir + '"\n',
                    './' + native_exe]
                else:
                    start_gn_lines = ['#!/bin/bash\n',
                    'cd "$INSTALL_DIR/' + game_name + '/game"\n',
                    './' + native_exe]

                start_gn_file_path = install_dir + '/' + game_name + '/start_gn.sh'
                start_gn_file = open(start_gn_file_path, 'w')
                for line in start_gn_lines:
                    start_gn_file.write(line)
                start_gn_file.close()
                os.chmod(start_gn_file_path, 0o711)

        if parser.has_option(game_name, 'native_settings_exe'):
            native_settings_exe = parser.get(game_name, 'native_settings_exe')
            if native_settings_exe != '':
                print("Writing settings.sh")

                if '/' in native_settings_exe:
                    native_settings_exe_dir = native_settings_exe.split('/')[0]
                    native_settings_exe = native_settings_exe.split('/')[1]
                    start_gn_lines = ['#!/bin/bash\n',
                    'cd "$INSTALL_DIR/' + game_name + '/game/' + native_settings_exe_dir + '"\n',
                    './' + native_settings_exe]
                else:
                    start_gn_lines = ['#!/bin/bash\n',
                    'cd "$INSTALL_DIR/' + game_name + '/game"\n',
                    './' + native_settings_exe]

                start_gn_file_path = install_dir + '/' + game_name + '/settings.sh'
                start_gn_file = open(start_gn_file_path, 'w')
                for line in start_gn_lines:
                    start_gn_file.write(line)
                start_gn_file.close()
                os.chmod(start_gn_file_path, 0o711)

        if parser.has_option(game_name, 'win_exe'):
            win_exe = parser.get(game_name, 'win_exe')
            if win_exe != '':
                print("Writing start.sh")

                if parser.has_option(game_name, 'winedlloverrides'):
                    overrides = parser.get(game_name, 'winedlloverrides')
                    start_lines = ['#!/bin/bash\n',
                    'export WINEDLLOVERRIDES="' + overrides + '"\n',
                    'python "$NEBULA_DIR/launcher_wine.py" ' + game_name + ' "' + win_exe + '"']
                else:
                    start_lines = ['#!/bin/bash\n',
                    'python "$NEBULA_DIR/launcher_wine.py" ' + game_name + ' "' + win_exe + '"']

                start_file_path = install_dir + '/' + game_name + '/start.sh'
                start_file = open(start_file_path, 'w')
                for line in start_lines:
                    start_file.write(line)
                start_file.close()
                os.chmod(start_file_path, 0o711)

        if parser.has_option(game_name, 'win_settings_exe'):
            win_settings_exe = parser.get(game_name, 'win_settings_exe')
            if win_settings_exe != '':
                print("Writing settings.sh")

                settings_lines = ['#!/bin/bash\n',
                'cd "$INSTALL_DIR/' + game_name + '/game"\n',
                '"$WINELOADER" "' + win_settings_exe + '"']

                settings_file_path = install_dir + '/' + game_name + '/settings.sh'
                settings_file = open(settings_file_path, 'w')
                for line in settings_lines:
                    settings_file.write(line)
                settings_file.close()
                os.chmod(settings_file_path, 0o711)

        if parser.has_option(game_name, 'winetricks'):
            winetricks = parser.get(game_name, 'winetricks')
            if winetricks != '':
                print("Writing additions.sh")

                additions_lines = ['#!/bin/bash\n',
                'python "$NEBULA_DIR/dialogs.py" "progress" "' + 'winetricks --gui ' + winetricks + '"\n']

                additions_file_path = install_dir + '/' + game_name + '/additions.sh'
                additions_file = open(additions_file_path, 'w')
                for line in additions_lines:
                    additions_file.write(line)
                additions_file.close()
                os.chmod(additions_file_path, 0o711)

        if parser.has_option(game_name, 'dos_iso'):
            dos_iso = parser.get(game_name, 'dos_iso')
        else:
            dos_iso = ''

        if parser.has_option(game_name, 'dos_exe'):
            dos_exe = parser.get(game_name, 'dos_exe')
            if dos_exe != '':
                print("Writing start.sh")

                start_lines = ['#!/bin/bash\n',
                'python "$NEBULA_DIR/launcher_dosbox.py" ' + game_name]

                exe_dir = ''
                if '\\' in dos_exe:
                    exe = dos_exe.split('\\')[-1]
                    exe_dir = '\\'.join(dos_exe.split('\\')[:-1])
                    dos_exe = exe

                start_file_path = install_dir + '/' + game_name + '/start.sh'
                start_file = open(start_file_path, 'w')
                for line in start_lines:
                    start_file.write(line)
                start_file.close()
                os.chmod(start_file_path, 0o711)

                game_conf_lines = ['[autoexec]\n',
                '@echo off\n',
                'mount c ' + os.getenv('HOME') + '/.games_nebula/games/.dosbox/' + game_name + '\n',
                'c:\n',
                'imgmount d ' + os.getenv('HOME') + '/.games_nebula/games/.dosbox/' + game_name + '/' + dos_iso + ' -fs iso\n',
                'cls\n',
                'cd ' + exe_dir + '\n',
                dos_exe + '\n',
                'exit\n']

                if dos_iso == '':
                    del game_conf_lines[4]
                    if exe_dir == '':
                        del game_conf_lines[5]
                elif exe_dir == '':
                    del game_conf_lines[6]

                dosbox_game_conf_path = install_dir + '/' + game_name + '/dosbox_game.conf'
                dosbox_game_conf = open(dosbox_game_conf_path, 'w')
                for line in game_conf_lines:
                    dosbox_game_conf.write(line)
                dosbox_game_conf.close()

        if parser.has_option(game_name, 'dos_settings_exe'):
            dos_settings_exe = parser.get(game_name, 'dos_settings_exe')
            if dos_settings_exe != '':
                print("Writing dosbox_settings.conf")

                exe_dir = ''
                if '\\' in dos_settings_exe:
                    exe = dos_settings_exe.split('\\')[-1]
                    exe_dir = '\\'.join(dos_settings_exe.split('\\')[:-1])
                    dos_settings_exe = exe

                settings_conf_lines = ['[autoexec]\n',
                '@echo off\n',
                'mount c ' + os.getenv('HOME') + '/.games_nebula/games/.dosbox/' + game_name + '\n',
                'c:\n',
                'imgmount d ' + os.getenv('HOME') + '/.games_nebula/games/.dosbox/' + game_name + '/' + dos_iso + ' -fs iso\n',
                'cls\n',
                'cd ' + exe_dir + '\n',
                dos_settings_exe + '\n',
                'exit\n']

                if dos_iso == '':
                    del settings_conf_lines[4]
                    if exe_dir == '':
                        del settings_conf_lines[5]
                if exe_dir == '':
                    del settings_conf_lines[6]

                dosbox_settings_conf_path = install_dir + '/' + game_name + '/dosbox_settings.conf'
                dosbox_settings_conf = open(dosbox_settings_conf_path, 'w')
                for line in settings_conf_lines:
                    dosbox_settings_conf.write(line)
                dosbox_settings_conf.close()

        if parser.has_option(game_name, 'scummvm_name') and \
                parser.has_option(game_name, 'scummvm_id'):

            scummvm_name = parser.get(game_name, 'scummvm_name')
            scummvm_id = parser.get(game_name, 'scummvm_id')

            if scummvm_id != '':
                print("Writing start.sh")

                start_lines = ['#!/bin/bash\n',
                'python "$NEBULA_DIR/launcher_scummvm.py" ' + game_name + ' ' + scummvm_name]

                start_file_path = install_dir + '/' + game_name + '/start.sh'
                start_file = open(start_file_path, 'w')
                for line in start_lines:
                    start_file.write(line)
                start_file.close()
                os.chmod(start_file_path, 0o711)

                scummvmrc_lines = ['[' + scummvm_name + ']\n',
                'gameid=' + scummvm_id + '\n']

                scummvmrc_path = install_dir + '/' + game_name + '/scummvmrc'
                scummvmrc = open(scummvmrc_path, 'w')
                for line in scummvmrc_lines:
                    scummvmrc.write(line)
                scummvmrc.close()

        if parser.has_option(game_name, 'special'):
            commands_list = parser.get(game_name, 'special').split('; ')
            for command in commands_list:
                os.system(command)

        if parser.has_option(game_name, 'win_reg1'):
            win_reg1 = parser.get(game_name, 'win_reg1')
            if win_reg1 != '':
                print("Writing additions.sh, reg keys")

                options_list = parser.options(game_name)

                for i in range(len(options_list)):
                    if options_list[i] == 'win_reg1':
                        win_reg1_index = i

                reg_list = parser.options(game_name)[win_reg1_index:]

                additions_file_path = install_dir + '/' + game_name + '/additions.sh'

                if (parser.has_option(game_name, 'winetricks')) and (winetricks != ''):
                    additions_file = open(additions_file_path, 'a')
                else:
                    additions_file = open(additions_file_path, 'w')
                    additions_file.write('#!/bin/bash\n')

                for item in reg_list:

                    key = parser.get(game_name, item).split('; ')[0]
                    value = parser.get(game_name, item).split('; ')[1]
                    dtype = parser.get(game_name, item).split('; ')[2]
                    data = parser.get(game_name, item).split('; ')[3]

                    line = '"$WINELOADER" reg add "' + key + '" /v "' + value + '" /t ' + dtype + ' /d "' + data + '" /f\n'

                    additions_file.write(line)

                additions_file.close()
                os.chmod(additions_file_path, 0o711) def get_monitors(): def get_image_path(lib, game_name, *args):
def get_setup_script_path(lib, game_name): def image_to_grayscale(input_path, output_path):
    """Convert image to grayscale"""
    def convert():
        img = Image.open(input_path)
        img = img.convert('L')
        img.save(output_path)
    if os.path.exists(input_path):
        convert()
    else:
        game_name = os.path.basename(input_path).split('.jpg')[0]
        goglib_recreate_banner.goglib_recreate_banner(game_name, input_path)
        convert()
import sys, os, subprocess, signal, re, shutil
import gi
import gettext
        import webbrowser
from os.path import isfile, join
from gi.repository import Gtk, Gdk, GLib, GObject, GdkPixbuf
from gi.repository.GdkPixbuf import Pixbuf, InterpType
    from ConfigParser import ConfigParser as ConfigParser
    from configparser import ConfigParser as ConfigParser
from modules import mylib_create_banner, mylib_get_data, mylib_tags_create, mylib_tags_get_all, \
mylib_tags_get, goglib_check_authorization, goglib_check_connection, goglib_get_data, \
goglib_get_games_list, goglib_tags_create, goglib_tags_get_all, goglib_tags_get, autosetup, \
monitors, paths, image_to_grayscale
        from gi.repository import WebKit2
                self.combobox_mylib_tags3.append_text(self.mylib_all_tags[i])
            if self.mylib_all_tags[i] == self.mylib_tags_filter3:
                self.combobox_mylib_tags3.set_active(i + 1)

        if self.mylib_tags_filter3 not in self.mylib_all_tags:
            self.combobox_mylib_tags3.set_active(0)

        self.combobox_mylib_tags3.append_text(_("No tags"))

        self.combobox_mylib_tags3.connect('changed', self.cb_combobox_mylib_tags3)
        self.combobox_mylib_tags3.connect('button-press-event', self.cb2_comboboxes_filters)

        if self.mylib_filter_color_indication == True:
            mylib_combobox_tags3_cell_view = self.combobox_mylib_tags3.get_child()
            if self.mylib_tags_filter3_type == 'include':
                mylib_combobox_tags3_cell_view.set_background_rgba(self.mylib_include_rgba)
            elif self.mylib_tags_filter3_type == 'exclude':
                mylib_combobox_tags3_cell_view.set_background_rgba(self.mylib_exclude_rgba)

        self.combobox_mylib_tags4 = Gtk.ComboBoxText(
            tooltip_text = _("Tags filter 4"),
            no_show_all = True,
            name = 'combobox_mylib_tags4'
            )
        self.combobox_mylib_tags4.append_text(_("No filter"))

        for i in range(len(self.mylib_all_tags)):
            if self.mylib_all_tags[i] != '':
                self.combobox_mylib_tags4.append_text(self.mylib_all_tags[i])
            if self.mylib_all_tags[i] == self.mylib_tags_filter4:
                self.combobox_mylib_tags4.set_active(i + 1)

        if self.mylib_tags_filter4 not in self.mylib_all_tags:
            self.combobox_mylib_tags4.set_active(0)

        self.combobox_mylib_tags4.append_text(_("No tags"))

        self.combobox_mylib_tags4.connect('changed', self.cb_combobox_mylib_tags4)
        self.combobox_mylib_tags4.connect('button-press-event', self.cb2_comboboxes_filters)

        if self.mylib_filter_color_indication == True:
            mylib_combobox_tags4_cell_view = self.combobox_mylib_tags4.get_child()
            if self.mylib_tags_filter4_type == 'include':
                mylib_combobox_tags4_cell_view.set_background_rgba(self.mylib_include_rgba)
            elif self.mylib_tags_filter4_type == 'exclude':
                mylib_combobox_tags4_cell_view.set_background_rgba(self.mylib_exclude_rgba)

        self.button_mylib_tagfilter_add = Gtk.Button(
            name = 'add',
            image = Gtk.Image(stock=Gtk.STOCK_ADD),
            no_show_all = True,
            tooltip_text = _("Add tags filter")
            )
        self.button_mylib_tagfilter_add.connect('clicked', self.mylib_tagfilters_number_changed)

        self.button_mylib_tagfilter_remove = Gtk.Button(
            name = 'remove',
            image = Gtk.Image(stock=Gtk.STOCK_REMOVE),
            tooltip_text = _("Remove tags filter"),
            no_show_all = True,
            )
        self.button_mylib_tagfilter_remove.connect('clicked', self.mylib_tagfilters_number_changed)

        self.adjustment_mylib_scale_banner = Gtk.Adjustment(self.mylib_scale_level, 0.4, 1, 0.1, 0.3)
        self.adjustment_mylib_scale_banner.connect('value-changed', self.cb_adjustment_mylib_scale_banner)
        self.mylib_scl_banners = Gtk.Scale(
            tooltip_text = _("Scale"),
            orientation = Gtk.Orientation.HORIZONTAL,
            halign = Gtk.Align.END,
            valign = Gtk.Align.CENTER,
            width_request = 150,
            draw_value = False,
            show_fill_level = True,
            adjustment = self.adjustment_mylib_scale_banner,
            )

        self.mylib_filters_box.pack_start(self.mylib_tab_search_entry, True, True, 0)
        self.mylib_filters_box.pack_start(self.combobox_mylib_status, False, False, 0)
        self.mylib_filters_box.pack_start(self.combobox_mylib_tags1, False, False, 0)
        self.mylib_filters_box.pack_start(self.combobox_mylib_tags2, False, False, 0)
        self.mylib_filters_box.pack_start(self.combobox_mylib_tags3, False, False, 0)
        self.mylib_filters_box.pack_start(self.combobox_mylib_tags4, False, False, 0)
        self.mylib_filters_box.pack_start(self.button_mylib_tagfilter_remove, False, False, 0)
        self.mylib_filters_box.pack_start(self.button_mylib_tagfilter_add, False, False, 0)
        self.mylib_filters_box.pack_start(self.mylib_scl_banners, False, False, 0)

        self.adjustment_mylib_scale_banner.set_value(self.mylib_scale_level)

        self.mylib_tab_scrolled_window = Gtk.ScrolledWindow()

        self.mylib_tab_grid = Gtk.Grid(
            halign = Gtk.Align.FILL,
            valign = Gtk.Align.START,
            hexpand = True,
            column_homogeneous = True,
            row_spacing = 30,
            margin_top = 30,
            margin_bottom = 30,
            )

        self.mylib_tab_scrolled_window.add(self.mylib_tab_grid)

        for i in range(0, self.mylib_number_of_games_to_show):

            mylib_game_grid = Gtk.Grid(
                name = self.mylib_shown_games_list[i],
                halign = Gtk.Align.CENTER,
                row_spacing = 1,
                column_spacing = 1,
                )
            mylib_game_grids_full_list.append(mylib_game_grid)
            mylib_game_grids_current_list.append(mylib_game_grid)

            mylib_setup_button = Gtk.Button(
                name = self.mylib_shown_games_list[i],
                )
            mylib_setup_button.set_label(_("Install"))
            mylib_setup_buttons_list.append(mylib_setup_button)
            mylib_setup_button.connect('clicked', self.mylib_setup_game)


            mylib_image_path = paths.get_image_path('mylib', self.mylib_shown_games_list[i])