VlibrasUpdateInstallPlayer.py 5.32 KB

# -*- coding: utf-8 -*-
 
import wx
import subprocess
import time
import os
from wx.lib.pubsub import pub
from DownloadFile import *
from VlibrasCheckVersion import *
import zipfile
import Util


class UpdateView(wx.Frame):

    def __init__(self,_listPlayerVersion):
        
        self.listPlayerVersion = _listPlayerVersion
        
        style = wx.SYSTEM_MENU | wx.SYSTEM_MENU | wx.CAPTION | wx.CLOSE_BOX | wx.MINIMIZE_BOX
        wx.Frame.__init__(self, None, title='Atualização do VLibras', style=style,size=(480,250))
        # Define o ícone para a janela
        self.SetIcon(wx.Icon("c:/vlibras-libs/icons/icon_vlibras.ico", wx.BITMAP_TYPE_ICO))
        self.myPanel = wx.Panel(self, -1)
        self.myPanel.SetBackgroundColour('#F5F5F5')
        btnCancel = wx.Button(self.myPanel, label="Cancelar",pos=(365, 180), size=(80, 25))
        self.myText = wx.StaticText(self.myPanel, -1, pos=(5,105))
        self.myText.SetForegroundColour('#000000')
        self.progress = wx.Gauge(self.myPanel, range=100,pos=(5,80),size=(463,25))
        self.Center() 
        
	# Anexa um evento ao botão
        self.Bind(wx.EVT_BUTTON, self.onBtnCancelClick, btnCancel)
        self.__close_callback = None
        self.Bind(wx.EVT_CLOSE, self._when_closed)

        #registra listeners na classe DownloadFile
        pub.subscribe(self.downloadSucess, "downloadSucess")
        pub.subscribe(self.erroDeConexao, "erroDeConexao")
        pub.subscribe(self.updateProgress, "updateProgress")
        self.percentDownload = 0;


           #inicia o download do arquivo
        self.downloadFile = DownloadFile2(self.listPlayerVersion[0], Util.INSTALL_PLAYER_PATH)
        self.downloadFile.start()
        self.myText.SetLabel('Baixando atualização do VLibras. Aguarde... ')
        
    def erroDeConexao(self, msg):
        wx.MessageBox('Ocorreu um erro ao tentar baixar atualização. \n Por favor verifique sua conexão e tente novamente.', 'Informação',wx.OK | wx.ICON_INFORMATION)
        self.Destroy()
        
    def register_close_callback(self, callback):
        self.__close_callback = callback

    def _when_closed(self, event):
            self.OnCloseFrame(event);


    def onBtnCancelClick(self, event):
        self.OnCloseFrame(event);   
        
        
    def downloadSucess(self, msg):     
        self.percentDownload = 100.0
        time.sleep( 5 )
        
        self.Hide()
        process = subprocess.call(Util.INSTALL_PLAYER_PATH)
        time.sleep( 3 )
        if os.path.isfile(Util.TEXT_CHECK_INSTALL):
            self.ShowMessage('Vlibras Atualizado com sucesso!') 
            Util.updateFilePlayerVersion(self.listPlayerVersion[1])
            subprocess.Popen(Util.PATH_PLAYER)
        else:
            self.ShowMessageError('O Vlibras não foi atualizado!')
            Util.updateFilePlayerVersion(self.listPlayerVersion[2])
            subprocess.Popen(Util.PATH_PLAYER)
        self.downloadFile.stop()
        self.Destroy()
        
           
    def updateProgress(self, msg):
        value = msg
        self.percentDownload = value
        self.progress.SetValue(value)

    def ShowMessage(self, mesagem):
        wx.MessageBox(mesagem, 'Informação',
                      wx.OK | wx.ICON_INFORMATION)
    def ShowMessageError(self, mesagem):
        wx.MessageBox(mesagem, 'Informação',
                      wx.OK | wx.ICON_ERROR)
        self.downloadFile.stop()
        self.Destroy()
            
            



    # monitora a interação do usuário no botão de fechar
    def OnCloseFrame(self, event):
        if(self.percentDownload>= 100):
            self.downloadFile.stop()
            self.Destroy()
        dialog = wx.MessageDialog(self, message = "Tem certeza que deseja cancelar a atualização?", caption = "Cancelar Atualização",
                                  style = wx.YES_NO | wx.ICON_INFORMATION)
        response = dialog.ShowModal()

        if (response == wx.ID_YES):
            self.downloadFile.stop()
            time.sleep( 0.5 )
            os.remove(Util.INSTALL_PLAYER_PATH)
            Util.updateFilePlayerVersion(self.listPlayerVersion[2])
            self.Destroy()
        else:
            event.StopPropagation()
            

class ShowInfo(wx.Frame):

    def __init__(self,_mesagem):
        self.mesagem = _mesagem
        style = wx.SYSTEM_MENU | wx.SYSTEM_MENU | wx.CAPTION | wx.CLOSE_BOX | wx.MINIMIZE_BOX
        wx.Frame.__init__(self, None, title='Atualização do VLibras', style=style,size=(480,250))
    def ShowMessage(self):
        wx.MessageBox(self.mesagem, 'Informação',
                      wx.OK | wx.ICON_INFORMATION)
    def ShowMessageError(self):
        wx.MessageBox(self.mesagem, 'Informação',
                      wx.OK | wx.ICON_ERROR)
    


if __name__ == '__main__':
    app = wx.App(False)
    listPlayerVersion = Util.getUrlDictPlayer()
    if os.path.isfile(Util.TEXT_CHECK_INSTALL):
        os.remove(Util.TEXT_CHECK_INSTALL)
    if len(listPlayerVersion) > 2:
        if(listPlayerVersion[0].endswith('.exe')): 
            frame = UpdateView(listPlayerVersion)
            frame.register_close_callback(lambda: True)
            frame.Show()
            app.MainLoop()
        else:
            ShowInfo('Ocorreu um erro ao tentar atualizar o VLibras!').ShowMessageError()
            
    else:
        ShowInfo('Atualização não disponível!').ShowMessageError()