#!/usr/bin/python3.8
#   Copyright 2020 Aragubas
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
#
#
import pygame, os
from System import Core
from Library import UI
from System.Core import CntMng
from Library import CoreUtils
from Library import CorePaths

class Process(Core.Process):
    def Initialize(self):
        # Initialize Content Manager
        self.SetVideoMode(False, (400, 340))
        self.CenterWindow()
        self.SetTitle("Packpage Installer")

        self.ContentManager = CntMng.ContentManager()
        self.ContentManager.SetSourceFolder("PackpageInstaller/")
        self.ContentManager.SetFontPath("fonts/")
        self.ContentManager.SetImageFolder("img/")
        self.ContentManager.SetRegKeysPath("reg/")

        self.ContentManager.LoadImagesInFolder()
        self.ContentManager.LoadRegKeysInFolder()

        self.PackpageVerticalList = UI.VerticalListWithDescription(pygame.Rect(0, 0, self.DISPLAY.get_width(), self.DISPLAY.get_height() - 40), self.ContentManager)

        self.PackpageInstalationEnabled = False
        self.PackpageInstalationStep = 0
        self.PackpageInstalationStepMax = 8
        self.PackpageInstalationStepNextDelay = 0
        self.PackpageReplaceWarning = False
        self.BackFromPackpageReplaceWarning = False
        self.InstalationCompleteDeletePackpage = False
        self.NoPackpagesFoundMode = False
        self.IgnorePackpageReplace = False
        self.SelectedPackpage = ""
        self.ExtractPath = ""
        self.AppFolderPath = ""
        self.DataFolderPath = ""
        self.LastSelectedPackpage = ""
        self.DownToolbar = UI.Widget.Widget_Controller(self.ContentManager, pygame.Rect(0, self.DISPLAY.get_height() - 40, self.DISPLAY.get_width(), 40))

        self.DownToolbar.Append(UI.Widget.Widget_Label(self.ContentManager, "/Ubuntu_Bold.ttf", self.ContentManager.Get_RegKey("/strings/select_app_list"), 12, (255, 255, 255), 2, 2, 0))
        self.DownToolbar.Append(UI.Widget.Widget_Button(self.ContentManager, self.ContentManager.Get_RegKey("/strings/install_button"), 14, 4, 20, 1))
        self.DownToolbar.Append(UI.Widget.Widget_ProgressBar(self.ContentManager, 0, self.PackpageInstalationStepMax, (5, 25, 200, 15), 2))
        self.DownToolbar.Append(UI.Widget.Widget_Button(self.ContentManager, self.ContentManager.Get_RegKey("/strings/no_button"), 14, 34, 20, 3))
        self.DownToolbar.Append(UI.Widget.Widget_Button(self.ContentManager, self.ContentManager.Get_RegKey("/strings/yes_button"), 14, 4, 20, 4))

        # Set Invisible Objects
        self.DownToolbar.GetWidget(2).IsVisible = False
        self.DownToolbar.GetWidget(3).IsVisible = False
        self.DownToolbar.GetWidget(4).IsVisible = False

        self.ICON = self.ContentManager.GetImage("/app_icon.png")

        self.UpdatePackpageList()

    def Draw(self):
        self.DISPLAY.fill(UI.ThemesManager_GetProperty("DefaultBackground"))

        BGIconOpacity = 100
        if self.NoPackpagesFoundMode:
            BGIconOpacity = 190

        self.ContentManager.ImageRender(self.DISPLAY, "/app_icon.png", self.DISPLAY.get_width() / 2 - self.ContentManager.GetImage("/app_icon.png").get_width() / 2, self.DISPLAY.get_height() / 2 - self.ContentManager.GetImage("/app_icon.png").get_height() / 2, Opacity=BGIconOpacity)

        if not self.NoPackpagesFoundMode:
            self.PackpageVerticalList.Render(self.DISPLAY)

        self.DownToolbar.Draw(self.DISPLAY)

    def Update(self):
        self.UpdateDownToolbar()

        if self.NoPackpagesFoundMode:
            self.DownToolbar.GetWidget(1).IsVisible = True
            self.DownToolbar.GetWidget(2).IsVisible = False
            self.DownToolbar.GetWidget(3).IsVisible = False
            self.DownToolbar.GetWidget(4).IsVisible = False
            return

        if self.PackpageInstalationEnabled and not self.InstalationCompleteDeletePackpage:
            self.PackpageInstalationStepNextDelay += 1

            if self.PackpageReplaceWarning:
                self.SetStatusText(self.ContentManager.Get_RegKey("/strings/application_already_installed_warning"))
                # Set progress bar Invisible
                self.DownToolbar.GetWidget(2).IsVisible = False

                # Set Yes Button Visible
                self.DownToolbar.GetWidget(4).IsVisible = True

                # Set No Button Visible
                self.DownToolbar.GetWidget(3).IsVisible = True

            if self.PackpageInstalationStepNextDelay >= 5 and not self.PackpageReplaceWarning:
                self.PackpageInstalationStepNextDelay = 0
                if not self.BackFromPackpageReplaceWarning:
                    self.PackpageInstalationStep += 1
                else:
                    self.BackFromPackpageReplaceWarning = False

                self.ApplicationInstallingUpdateProgress()

            # Set the Progress Bar Value and Status Text
            if self.PackpageInstalationEnabled and not self.PackpageReplaceWarning:
                self.SetStatusText(self.ContentManager.Get_RegKey("/strings/progress_text").format(self.SelectedPackpage, str(self.PackpageInstalationStep), str(self.PackpageInstalationStepMax)))
                self.DownToolbar.GetWidget(2).Progress = self.PackpageInstalationStep

                if self.PackpageInstalationStep >= self.PackpageInstalationStepMax + 1:
                    print("Instalation steps has been reached its limit")
                    self.PackpageInstalationStep = self.PackpageInstalationStepMax
                    self.FinishInstalation(False)

        elif not self.InstalationCompleteDeletePackpage:
            self.PackpageVerticalList.ColisionXOffset = self.POSITION[0]
            self.PackpageVerticalList.ColisionYOffset = self.POSITION[1] + self.TITLEBAR_RECTANGLE[3]

            if self.PackpageVerticalList.LastItemClicked != "null":
                self.SetSelectedPackpage(self.PackpageVerticalList.LastItemClicked)
        else:
            self.SetStatusText(self.ContentManager.Get_RegKey("/strings/packpage_installed_want_remove"))

            # Set progress bar Visible
            self.DownToolbar.GetWidget(2).IsVisible = False

            # Set Yes Button Invisible
            self.DownToolbar.GetWidget(4).IsVisible = True

            # Set No Button Invisible
            self.DownToolbar.GetWidget(3).IsVisible = True

    def UpdateDownToolbar(self):
        self.DownToolbar.UpdateOffsetToWindow(self)
        self.DownToolbar.Update()

        if self.DownToolbar.LastInteractionID == 1 and self.DownToolbar.LastInteractionType is True:
            if not self.PackpageInstalationEnabled and not self.NoPackpagesFoundMode:
                if self.SelectedPackpage != "":
                    self.DownToolbar.GetWidget(1).IsVisible = False
                    self.DownToolbar.GetWidget(2).IsVisible = True
                    self.PackpageInstalationEnabled = True
                else:
                    self.ContentManager.Get_RegKey("/strings/select_app_list")

            if self.NoPackpagesFoundMode:
                print("Refreshing Packpage Lists...")
                self.UpdatePackpageList()

        if self.NoPackpagesFoundMode:
            return

        # Yes Button when Replace Warning
        if self.DownToolbar.LastInteractionID == 4 and self.DownToolbar.LastInteractionType is True and self.PackpageInstalationEnabled:
            if self.PackpageReplaceWarning:
                self.PackpageReplaceWarning = False
                # Set progress bar Visible
                self.DownToolbar.GetWidget(2).IsVisible = True

                # Set Yes Button Invisible
                self.DownToolbar.GetWidget(4).IsVisible = False

                # Set No Button Invisible
                self.DownToolbar.GetWidget(3).IsVisible = False

                # Set Back from Replace Warning
                self.BackFromPackpageReplaceWarning = True

                # Set IgnorePackpageReplace
                self.IgnorePackpageReplace = True

            if self.InstalationCompleteDeletePackpage:
                self.DeletePackpage()
                self.FinishInstalation(True)

        # No Button when Replace Warning
        if self.DownToolbar.LastInteractionID == 3 and self.DownToolbar.LastInteractionType is True and self.PackpageInstalationEnabled:
            if self.PackpageReplaceWarning:
                self.FinishInstalation(True)
                self.SetStatusText(self.ContentManager.Get_RegKey("/strings/packpage_instalation_aborted"))

            if self.InstalationCompleteDeletePackpage:
                self.FinishInstalation(True)

    def DeletePackpage(self):
        PackpagePath = CorePaths.TaiyouPath_UserPackpagesPath + self.SelectedPackpage + self.ContentManager.Get_RegKey("/packpage_file_format")

        if CoreUtils.File_Exists(PackpagePath):
            CoreUtils.File_Delete(PackpagePath)

    def ApplicationInstallingUpdateProgress(self):
        print("Instalation Progress Update\nStep: {0}".format(str(self.PackpageInstalationStep)))
        PackpagePath = CorePaths.TaiyouPath_UserPackpagesPath + self.SelectedPackpage + self.ContentManager.Get_RegKey("/packpage_file_format")

        # Check if File exists
        if self.PackpageInstalationStep == 1:
            if self.SelectedPackpage == "" or not CoreUtils.File_Exists(PackpagePath):
                self.FinishInstalation()

        elif self.PackpageInstalationStep == 2:  # Check if Extract Path exists and clean it
            print("Packpage : {0}".format(self.SelectedPackpage))
            self.ExtractPath = CorePaths.TaiyouPath_UserTempFolder + self.SelectedPackpage
            print("ExtractPath : " + self.ExtractPath)

            # Check if Extract Path Exists
            if CoreUtils.Directory_Exists(self.ExtractPath):
                print("Extraction Path Exists, Directory Removed and Re-Created")
                CoreUtils.Directory_Remove(self.ExtractPath)
                CoreUtils.Directory_MakeDir(self.ExtractPath)

        elif self.PackpageInstalationStep == 3:  # Extract the Packpage
            # Extract the Zip File
            print("Un-Zipping packpage....")
            CoreUtils.Unzip_File(PackpagePath, self.ExtractPath)

        elif self.PackpageInstalationStep == 4:  # Check if Packpage is Valid
            print("Check for app and data folder on extracted path")
            self.AppFolderPath = self.ExtractPath + CorePaths.TaiyouPath_CorrectSlash + "app" + CorePaths.TaiyouPath_CorrectSlash
            self.DataFolderPath = self.ExtractPath + CorePaths.TaiyouPath_CorrectSlash + "data" + CorePaths.TaiyouPath_CorrectSlash
            AppFolderExists = CoreUtils.Directory_Exists(self.AppFolderPath)
            DataFolderExists = CoreUtils.Directory_Exists(self.DataFolderPath)

            if not AppFolderExists or not DataFolderExists:
                self.FinishInstalation(True)
                self.SetStatusText(self.ContentManager.Get_RegKey("/strings/invalid_taiyou_packpage"))

        elif self.PackpageInstalationStep == 5:  # Check if Application Already Exists
            AppFolderInsideIt = os.listdir(self.AppFolderPath)[0]
            CheckPath = CorePaths.TaiyouPath_ApplicationsFolder + AppFolderInsideIt
            if CoreUtils.Directory_Exists(CheckPath) and not self.IgnorePackpageReplace:
                self.PackpageReplaceWarning = True
                return

            if self.IgnorePackpageReplace:
                print("Package will replace, check for conflicting directories...")
                FolderInAppPath = os.listdir(self.AppFolderPath)
                for folder in FolderInAppPath:
                    # Check if folder exists
                    CheckPath = ''.join((CorePaths.TaiyouPath_ApplicationsFolder, folder))
                    if CheckPath == CorePaths.TaiyouPath_ApplicationsFolder:
                        print("WARNING : This function almost deleted all of your installed applications.\nWe have an protection to prevent this.")
                        continue

                    print("CheckPath : " + CheckPath)
                    if CoreUtils.Directory_Exists(CheckPath):
                        print("Detected conflict between installed application folder.\nDeleting current existing folder.")
                        CoreUtils.Directory_Remove(CheckPath)

                FolderInDataPath = os.listdir(self.DataFolderPath)
                for folder in FolderInDataPath:
                    # Check if folder exists
                    CheckPath = ''.join((CorePaths.TaiyouPath_ApplicationsDataPath, folder))
                    if CheckPath == CorePaths.TaiyouPath_ApplicationsDataPath:
                        print("WARNING : This function almost deleted all of your installed applications data.\nWe have an protection to prevent this.")
                        continue

                    print("CheckPath : " + CheckPath)
                    if CoreUtils.Directory_Exists(CheckPath):
                        print("Detected conflict between installed application data folder.\nDeleting current existing folder.")
                        CoreUtils.Directory_Remove(CheckPath)

            print("Copy Application to Application Folder...")
            CoreUtils.Directory_Copy(self.AppFolderPath[:-1], CorePaths.TaiyouPath_ApplicationsFolder[:-1])

        elif self.PackpageInstalationStep == 6:  # Copy the Application Folder
            print("Copy Application to Application Folder...")
            CoreUtils.Directory_Copy(self.AppFolderPath, CorePaths.TaiyouPath_ApplicationsFolder[:-1])

        elif self.PackpageInstalationStep == 7:
            print("Copy Application Data to Application Data Folder...")
            CoreUtils.Directory_Copy(self.DataFolderPath, CorePaths.TaiyouPath_ApplicationsDataFolder)

        elif self.PackpageInstalationStep == 8:
            self.InstalationCompleteDeletePackpage = True

    def SetStatusText(self, text):
        self.DownToolbar.GetWidget(0).Text = text

    def UpdatePackpageList(self):
        print("Updating Packpages lists...")
        self.PackpageVerticalList.ClearItems()
        self.PackpageVerticalList.ResetSelectedItem()

        PackpagesList = CoreUtils.Directory_FilesList(CorePaths.TaiyouPath_UserPackpagesPath)

        for Packpage in PackpagesList:
            if Packpage.endswith(self.ContentManager.Get_RegKey("packpage_file_format")):
                Name = Packpage.rstrip().replace(CorePaths.TaiyouPath_UserPackpagesPath, "").replace(self.ContentManager.Get_RegKey("packpage_file_format"), "")
                print("Found Packpage {0}".format(Name))
                self.PackpageVerticalList.AddItem(Name, self.ContentManager.Get_RegKey("packpage_description"))

        if len(self.PackpageVerticalList.ItemsName) == 0:
            print("No packpages has been found.")
            self.NoPackpagesFoundMode = True
            self.SetStatusText(self.ContentManager.Get_RegKey("/strings/no_packpage_found_mode_title"))
            self.DownToolbar.GetWidget(1).SetText(self.ContentManager.Get_RegKey("/strings/no_packpage_found_mode_refresh"))
        else:
            self.NoPackpagesFoundMode = False

            print("Some packpage has been found.")
            self.DownToolbar.GetWidget(1).SetText(self.ContentManager.Get_RegKey("/strings/install_button"))
            self.SetStatusText(self.ContentManager.Get_RegKey("/strings/select_app_list"))

    def FinishInstalation(self, DeleteExtractPath=False):
        print("Instalation has been completed.")
        self.PackpageInstalationEnabled = False
        self.PackpageInstalationStepNextDelay = 0
        self.IgnorePackpageReplace = False
        self.PackpageReplaceWarning = False
        self.BackFromPackpageReplaceWarning = False
        self.InstalationCompleteDeletePackpage = False

        if DeleteExtractPath:
            CoreUtils.Directory_Remove(self.ExtractPath)

        if self.PackpageInstalationStep >= self.PackpageInstalationStepMax:
            self.SetStatusText(self.ContentManager.Get_RegKey("/strings/packpage_install_complete").format(self.SelectedPackpage))

        else:
            self.SetStatusText(self.ContentManager.Get_RegKey("/strings/packpage_install_error").format(self.SelectedPackpage))

        self.PackpageInstalationStep = 0

        # Re-Enable the Install Button
        self.DownToolbar.GetWidget(1).IsVisible = True
        self.DownToolbar.GetWidget(2).IsVisible = False
        self.DownToolbar.GetWidget(3).IsVisible = False
        self.DownToolbar.GetWidget(4).IsVisible = False

        self.SelectedPackpage = ""
        self.UpdatePackpageList()

    def SetSelectedPackpage(self, pkg_path):
        self.SelectedPackpage = pkg_path.rstrip().replace(CorePaths.TaiyouPath_UserPackpagesPath, "").replace(".zip", "")

        if self.LastSelectedPackpage != self.SelectedPackpage:
            self.LastSelectedPackpage = self.SelectedPackpage
            self.SetStatusText("Selected {0}.".format(self.SelectedPackpage))

        self.PackpageVerticalList.ResetSelectedItem()

    def EventUpdate(self, event):
        self.DownToolbar.EventUpdate(event)

        if self.NoPackpagesFoundMode:
            return

        if not self.PackpageInstalationEnabled:
            self.PackpageVerticalList.Update(event)

#!/usr/bin/python3.8
#   Copyright 2020 Aragubas
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
#
#
import pygame, os
from System import Core
from Library import UI
from System.Core import CntMng
from Library import CoreUtils
from Library import CorePaths

class Process(Core.Process):
    def Initialize(self):
        # Initialize Content Manager
        self.SetVideoMode(False, (400, 340))
        self.CenterWindow()
        self.SetTitle("Packpage Installer")

        self.ContentManager = CntMng.ContentManager()
        self.ContentManager.SetSourceFolder("PackpageInstaller/")
        self.ContentManager.SetFontPath("fonts/")
        self.ContentManager.SetImageFolder("img/")
        self.ContentManager.SetRegKeysPath("reg/")

        self.ContentManager.LoadImagesInFolder()
        self.ContentManager.LoadRegKeysInFolder()

        self.PackpageVerticalList = UI.VerticalListWithDescription(pygame.Rect(0, 0, self.DISPLAY.get_width(), self.DISPLAY.get_height() - 40), self.ContentManager)

        self.PackpageInstalationEnabled = False
        self.PackpageInstalationStep = 0
        self.PackpageInstalationStepMax = 8
        self.PackpageInstalationStepNextDelay = 0
        self.PackpageReplaceWarning = False
        self.BackFromPackpageReplaceWarning = False
        self.InstalationCompleteDeletePackpage = False
        self.NoPackpagesFoundMode = False
        self.IgnorePackpageReplace = False
        self.SelectedPackpage = ""
        self.ExtractPath = ""
        self.AppFolderPath = ""
        self.DataFolderPath = ""
        self.LastSelectedPackpage = ""
        self.DownToolbar = UI.Widget.Widget_Controller(self.ContentManager, pygame.Rect(0, self.DISPLAY.get_height() - 40, self.DISPLAY.get_width(), 40))

        self.DownToolbar.Append(UI.Widget.Widget_Label(self.ContentManager, "/Ubuntu_Bold.ttf", self.ContentManager.Get_RegKey("/strings/select_app_list"), 12, (255, 255, 255), 2, 2, 0))
        self.DownToolbar.Append(UI.Widget.Widget_Button(self.ContentManager, self.ContentManager.Get_RegKey("/strings/install_button"), 14, 4, 20, 1))
        self.DownToolbar.Append(UI.Widget.Widget_ProgressBar(self.ContentManager, 0, self.PackpageInstalationStepMax, (5, 25, 200, 15), 2))
        self.DownToolbar.Append(UI.Widget.Widget_Button(self.ContentManager, self.ContentManager.Get_RegKey("/strings/no_button"), 14, 34, 20, 3))
        self.DownToolbar.Append(UI.Widget.Widget_Button(self.ContentManager, self.ContentManager.Get_RegKey("/strings/yes_button"), 14, 4, 20, 4))

        # Set Invisible Objects
        self.DownToolbar.GetWidget(2).IsVisible = False
        self.DownToolbar.GetWidget(3).IsVisible = False
        self.DownToolbar.GetWidget(4).IsVisible = False

        self.ICON = self.ContentManager.GetImage("/app_icon.png")

        self.UpdatePackpageList()

    def Draw(self):
        self.DISPLAY.fill(UI.ThemesManager_GetProperty("DefaultBackground"))

        BGIconOpacity = 100
        if self.NoPackpagesFoundMode:
            BGIconOpacity = 190

        self.ContentManager.ImageRender(self.DISPLAY, "/app_icon.png", self.DISPLAY.get_width() / 2 - self.ContentManager.GetImage("/app_icon.png").get_width() / 2, self.DISPLAY.get_height() / 2 - self.ContentManager.GetImage("/app_icon.png").get_height() / 2, Opacity=BGIconOpacity)

        if not self.NoPackpagesFoundMode:
            self.PackpageVerticalList.Render(self.DISPLAY)

        self.DownToolbar.Draw(self.DISPLAY)

    def Update(self):
        self.UpdateDownToolbar()

        if self.NoPackpagesFoundMode:
            self.DownToolbar.GetWidget(1).IsVisible = True
            self.DownToolbar.GetWidget(2).IsVisible = False
            self.DownToolbar.GetWidget(3).IsVisible = False
            self.DownToolbar.GetWidget(4).IsVisible = False
            return

        if self.PackpageInstalationEnabled and not self.InstalationCompleteDeletePackpage:
            self.PackpageInstalationStepNextDelay += 1

            if self.PackpageReplaceWarning:
                self.SetStatusText(self.ContentManager.Get_RegKey("/strings/application_already_installed_warning"))
                # Set progress bar Invisible
                self.DownToolbar.GetWidget(2).IsVisible = False

                # Set Yes Button Visible
                self.DownToolbar.GetWidget(4).IsVisible = True

                # Set No Button Visible
                self.DownToolbar.GetWidget(3).IsVisible = True

            if self.PackpageInstalationStepNextDelay >= 5 and not self.PackpageReplaceWarning:
                self.PackpageInstalationStepNextDelay = 0
                if not self.BackFromPackpageReplaceWarning:
                    self.PackpageInstalationStep += 1
                else:
                    self.BackFromPackpageReplaceWarning = False

                self.ApplicationInstallingUpdateProgress()

            # Set the Progress Bar Value and Status Text
            if self.PackpageInstalationEnabled and not self.PackpageReplaceWarning:
                self.SetStatusText(self.ContentManager.Get_RegKey("/strings/progress_text").format(self.SelectedPackpage, str(self.PackpageInstalationStep), str(self.PackpageInstalationStepMax)))
                self.DownToolbar.GetWidget(2).Progress = self.PackpageInstalationStep

                if self.PackpageInstalationStep >= self.PackpageInstalationStepMax + 1:
                    print("Instalation steps has been reached its limit")
                    self.PackpageInstalationStep = self.PackpageInstalationStepMax
                    self.FinishInstalation(False)

        elif not self.InstalationCompleteDeletePackpage:
            self.PackpageVerticalList.ColisionXOffset = self.POSITION[0]
            self.PackpageVerticalList.ColisionYOffset = self.POSITION[1] + self.TITLEBAR_RECTANGLE[3]

            if self.PackpageVerticalList.LastItemClicked != "null":
                self.SetSelectedPackpage(self.PackpageVerticalList.LastItemClicked)
        else:
            self.SetStatusText(self.ContentManager.Get_RegKey("/strings/packpage_installed_want_remove"))

            # Set progress bar Visible
            self.DownToolbar.GetWidget(2).IsVisible = False

            # Set Yes Button Invisible
            self.DownToolbar.GetWidget(4).IsVisible = True

            # Set No Button Invisible
            self.DownToolbar.GetWidget(3).IsVisible = True

    def UpdateDownToolbar(self):
        self.DownToolbar.UpdateOffsetToWindow(self)
        self.DownToolbar.Update()

        if self.DownToolbar.LastInteractionID == 1 and self.DownToolbar.LastInteractionType is True:
            if not self.PackpageInstalationEnabled and not self.NoPackpagesFoundMode:
                if self.SelectedPackpage != "":
                    self.DownToolbar.GetWidget(1).IsVisible = False
                    self.DownToolbar.GetWidget(2).IsVisible = True
                    self.PackpageInstalationEnabled = True
                else:
                    self.ContentManager.Get_RegKey("/strings/select_app_list")

            if self.NoPackpagesFoundMode:
                print("Refreshing Packpage Lists...")
                self.UpdatePackpageList()

        if self.NoPackpagesFoundMode:
            return

        # Yes Button when Replace Warning
        if self.DownToolbar.LastInteractionID == 4 and self.DownToolbar.LastInteractionType is True and self.PackpageInstalationEnabled:
            if self.PackpageReplaceWarning:
                self.PackpageReplaceWarning = False
                # Set progress bar Visible
                self.DownToolbar.GetWidget(2).IsVisible = True

                # Set Yes Button Invisible
                self.DownToolbar.GetWidget(4).IsVisible = False

                # Set No Button Invisible
                self.DownToolbar.GetWidget(3).IsVisible = False

                # Set Back from Replace Warning
                self.BackFromPackpageReplaceWarning = True

                # Set IgnorePackpageReplace
                self.IgnorePackpageReplace = True

            if self.InstalationCompleteDeletePackpage:
                self.DeletePackpage()
                self.FinishInstalation(True)

        # No Button when Replace Warning
        if self.DownToolbar.LastInteractionID == 3 and self.DownToolbar.LastInteractionType is True and self.PackpageInstalationEnabled:
            if self.PackpageReplaceWarning:
                self.FinishInstalation(True)
                self.SetStatusText(self.ContentManager.Get_RegKey("/strings/packpage_instalation_aborted"))

            if self.InstalationCompleteDeletePackpage:
                self.FinishInstalation(True)

    def DeletePackpage(self):
        PackpagePath = CorePaths.TaiyouPath_UserPackpagesPath + self.SelectedPackpage + self.ContentManager.Get_RegKey("/packpage_file_format")

        if CoreUtils.File_Exists(PackpagePath):
            CoreUtils.File_Delete(PackpagePath)

    def ApplicationInstallingUpdateProgress(self):
        print("Instalation Progress Update\nStep: {0}".format(str(self.PackpageInstalationStep)))
        PackpagePath = CorePaths.TaiyouPath_UserPackpagesPath + self.SelectedPackpage + self.ContentManager.Get_RegKey("/packpage_file_format")

        # Check if File exists
        if self.PackpageInstalationStep == 1:
            if self.SelectedPackpage == "" or not CoreUtils.File_Exists(PackpagePath):
                self.FinishInstalation()

        elif self.PackpageInstalationStep == 2:  # Check if Extract Path exists and clean it
            print("Packpage : {0}".format(self.SelectedPackpage))
            self.ExtractPath = CorePaths.TaiyouPath_UserTempFolder + self.SelectedPackpage
            print("ExtractPath : " + self.ExtractPath)

            # Check if Extract Path Exists
            if CoreUtils.Directory_Exists(self.ExtractPath):
                print("Extraction Path Exists, Directory Removed and Re-Created")
                CoreUtils.Directory_Remove(self.ExtractPath)
                CoreUtils.Directory_MakeDir(self.ExtractPath)

        elif self.PackpageInstalationStep == 3:  # Extract the Packpage
            # Extract the Zip File
            print("Un-Zipping packpage....")
            CoreUtils.Unzip_File(PackpagePath, self.ExtractPath)

        elif self.PackpageInstalationStep == 4:  # Check if Packpage is Valid
            print("Check for app and data folder on extracted path")
            self.AppFolderPath = self.ExtractPath + CorePaths.TaiyouPath_CorrectSlash + "app" + CorePaths.TaiyouPath_CorrectSlash
            self.DataFolderPath = self.ExtractPath + CorePaths.TaiyouPath_CorrectSlash + "data" + CorePaths.TaiyouPath_CorrectSlash
            AppFolderExists = CoreUtils.Directory_Exists(self.AppFolderPath)
            DataFolderExists = CoreUtils.Directory_Exists(self.DataFolderPath)

            if not AppFolderExists or not DataFolderExists:
                self.FinishInstalation(True)
                self.SetStatusText(self.ContentManager.Get_RegKey("/strings/invalid_taiyou_packpage"))

        elif self.PackpageInstalationStep == 5:  # Check if Application Already Exists
            AppFolderInsideIt = os.listdir(self.AppFolderPath)[0]
            CheckPath = CorePaths.TaiyouPath_ApplicationsFolder + AppFolderInsideIt
            if CoreUtils.Directory_Exists(CheckPath) and not self.IgnorePackpageReplace:
                self.PackpageReplaceWarning = True
                return

            if self.IgnorePackpageReplace:
                print("Package will replace, check for conflicting directories...")
                FolderInAppPath = os.listdir(self.AppFolderPath)
                for folder in FolderInAppPath:
                    # Check if folder exists
                    CheckPath = ''.join((CorePaths.TaiyouPath_ApplicationsFolder, folder))
                    if CheckPath == CorePaths.TaiyouPath_ApplicationsFolder:
                        print("WARNING : This function almost deleted all of your installed applications.\nWe have an protection to prevent this.")
                        continue

                    print("CheckPath : " + CheckPath)
                    if CoreUtils.Directory_Exists(CheckPath):
                        print("Detected conflict between installed application folder.\nDeleting current existing folder.")
                        CoreUtils.Directory_Remove(CheckPath)

                FolderInDataPath = os.listdir(self.DataFolderPath)
                for folder in FolderInDataPath:
                    # Check if folder exists
                    CheckPath = ''.join((CorePaths.TaiyouPath_ApplicationsDataPath, folder))
                    if CheckPath == CorePaths.TaiyouPath_ApplicationsDataPath:
                        print("WARNING : This function almost deleted all of your installed applications data.\nWe have an protection to prevent this.")
                        continue

                    print("CheckPath : " + CheckPath)
                    if CoreUtils.Directory_Exists(CheckPath):
                        print("Detected conflict between installed application data folder.\nDeleting current existing folder.")
                        CoreUtils.Directory_Remove(CheckPath)

            print("Copy Application to Application Folder...")
            CoreUtils.Directory_Copy(self.AppFolderPath[:-1], CorePaths.TaiyouPath_ApplicationsFolder[:-1])

        elif self.PackpageInstalationStep == 6:  # Copy the Application Folder
            print("Copy Application to Application Folder...")
            CoreUtils.Directory_Copy(self.AppFolderPath, CorePaths.TaiyouPath_ApplicationsFolder[:-1])

        elif self.PackpageInstalationStep == 7:
            print("Copy Application Data to Application Data Folder...")
            CoreUtils.Directory_Copy(self.DataFolderPath, CorePaths.TaiyouPath_ApplicationsDataFolder)

        elif self.PackpageInstalationStep == 8:
            self.InstalationCompleteDeletePackpage = True

    def SetStatusText(self, text):
        self.DownToolbar.GetWidget(0).Text = text

    def UpdatePackpageList(self):
        print("Updating Packpages lists...")
        self.PackpageVerticalList.ClearItems()
        self.PackpageVerticalList.ResetSelectedItem()

        PackpagesList = CoreUtils.Directory_FilesList(CorePaths.TaiyouPath_UserPackpagesPath)

        for Packpage in PackpagesList:
            if Packpage.endswith(self.ContentManager.Get_RegKey("packpage_file_format")):
                Name = Packpage.rstrip().replace(CorePaths.TaiyouPath_UserPackpagesPath, "").replace(self.ContentManager.Get_RegKey("packpage_file_format"), "")
                print("Found Packpage {0}".format(Name))
                self.PackpageVerticalList.AddItem(Name, self.ContentManager.Get_RegKey("packpage_description"))

        if len(self.PackpageVerticalList.ItemsName) == 0:
            print("No packpages has been found.")
            self.NoPackpagesFoundMode = True
            self.SetStatusText(self.ContentManager.Get_RegKey("/strings/no_packpage_found_mode_title"))
            self.DownToolbar.GetWidget(1).SetText(self.ContentManager.Get_RegKey("/strings/no_packpage_found_mode_refresh"))
        else:
            self.NoPackpagesFoundMode = False

            print("Some packpage has been found.")
            self.DownToolbar.GetWidget(1).SetText(self.ContentManager.Get_RegKey("/strings/install_button"))
            self.SetStatusText(self.ContentManager.Get_RegKey("/strings/select_app_list"))

    def FinishInstalation(self, DeleteExtractPath=False):
        print("Instalation has been completed.")
        self.PackpageInstalationEnabled = False
        self.PackpageInstalationStepNextDelay = 0
        self.IgnorePackpageReplace = False
        self.PackpageReplaceWarning = False
        self.BackFromPackpageReplaceWarning = False
        self.InstalationCompleteDeletePackpage = False

        if DeleteExtractPath:
            CoreUtils.Directory_Remove(self.ExtractPath)

        if self.PackpageInstalationStep >= self.PackpageInstalationStepMax:
            self.SetStatusText(self.ContentManager.Get_RegKey("/strings/packpage_install_complete").format(self.SelectedPackpage))

        else:
            self.SetStatusText(self.ContentManager.Get_RegKey("/strings/packpage_install_error").format(self.SelectedPackpage))

        self.PackpageInstalationStep = 0

        # Re-Enable the Install Button
        self.DownToolbar.GetWidget(1).IsVisible = True
        self.DownToolbar.GetWidget(2).IsVisible = False
        self.DownToolbar.GetWidget(3).IsVisible = False
        self.DownToolbar.GetWidget(4).IsVisible = False

        self.SelectedPackpage = ""
        self.UpdatePackpageList()

    def SetSelectedPackpage(self, pkg_path):
        self.SelectedPackpage = pkg_path.rstrip().replace(CorePaths.TaiyouPath_UserPackpagesPath, "").replace(".zip", "")

        if self.LastSelectedPackpage != self.SelectedPackpage:
            self.LastSelectedPackpage = self.SelectedPackpage
            self.SetStatusText("Selected {0}.".format(self.SelectedPackpage))

        self.PackpageVerticalList.ResetSelectedItem()

    def EventUpdate(self, event):
        self.DownToolbar.EventUpdate(event)

        if self.NoPackpagesFoundMode:
            return

        if not self.PackpageInstalationEnabled:
            self.PackpageVerticalList.Update(event)

#!/usr/bin/python3.8 #   Copyright 2020 Aragubas # #   Licensed under the Apache License, Version 2.0 (the "License"); #   you may not use this file except in compliance with the License. #   You may obtain a copy of the License at # #       http://www.apache.org/licenses/LICENSE-2.0 # #   Unless required by applicable law or agreed to in writing, software #   distributed under the License is distributed on an "AS IS" BASIS, #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #   See the License for the specific language governing permissions and #   limitations under the License. # # # Initialize Content Manager # Set Invisible Objects # Set progress bar Invisible # Set Yes Button Visible # Set No Button Visible # Set the Progress Bar Value and Status Text # Set progress bar Visible # Set Yes Button Invisible # Set No Button Invisible # Yes Button when Replace Warning # Set progress bar Visible # Set Yes Button Invisible # Set No Button Invisible # Set Back from Replace Warning # Set IgnorePackpageReplace # No Button when Replace Warning # Check if File exists # Check if Extract Path exists and clean it # Check if Extract Path Exists # Extract the Packpage # Extract the Zip File # Check if Packpage is Valid # Check if Application Already Exists # Check if folder exists # Check if folder exists # Copy the Application Folder # Re-Enable the Install Button