forked from tecwindow/WikiSearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_dialog.py
98 lines (77 loc) · 2.93 KB
/
update_dialog.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#-*- coding: utf-8 -*-
# import project libraries.
import wx
import threading
import requests
import os
import shutil
import json
import subprocess
from settings import Settings
from functions import *
#Set language for update dialog
_ = SetLanguage(Settings().ReadSettings())
#Delete setup file if is found in temp.
temp = os.path.join(os.getenv("temp"), "WikiSearch")
if os.path.exists(temp):
shutil.rmtree(temp, ignore_errors=True)
#creating update dialog
class UpdateDialog(wx.Dialog):
def __init__(self, parent, RecentVersion, whatIsNew):
wx.Dialog.__init__(self, parent, title = _("Update found"), size=(500, 500))
self.Center()
#creating panel
Panel = wx.Panel(self)
#creating field to show what's new.
wx.StaticText(Panel, -1, _("Version {} is available.").format(RecentVersion), pos=(20,20), size=(170, 30))
self.WhatsNew = wx.TextCtrl(Panel, -1, value=whatIsNew, pos=(10,60), size=(450,320), style=wx.HSCROLL+wx.TE_MULTILINE+wx.TE_READONLY)
# Creating Buttons
self.Update = wx.Button(Panel, -1, _("&Update"), pos=(20,400), size=(60,30))
self.Update.SetDefault()
self.Close = wx.Button(Panel, wx.ID_CANCEL, _("&Cancel"), pos=(90,400), size=(60,30))
self.hotKeys = wx.AcceleratorTable([
(wx.ACCEL_ALT, ord("U"), self.Update.GetId()),
(wx.ACCEL_CTRL, ord("W"), self.Close.GetId()),
])
Panel.SetAcceleratorTable(self.hotKeys)
#show the dialog
self.Show()
#event for update button
self.Update.Bind(wx.EVT_BUTTON, self.OnDownloadUpdate)
#creating OnDownloadUpdate function to show progress dialog.
def OnDownloadUpdate(self, event):
ProgressDialog = progress_dialog(_("Downloading update"), _("Please wait."), maximum=100, parent=self, style=wx.PD_CAN_ABORT)
#creating progress dialog
class progress_dialog(wx.ProgressDialog):
def __init__(self, *args, **kwargs):
wx.ProgressDialog.__init__(self, *args, **kwargs)
self.handle = kwargs["parent"]
#making downloading function in thread to download update.
threading.Thread(target=self.downloading, daemon=True).start()
#creating downloading function
def downloading(self):
#geting download ling from online info file.
DownloadLink = GetOnlineInfo()["url"]
#extracting the name file and geting temp path.
if not os.path.exists(temp):
os.mkdir(temp)
file_name = DownloadLink.split('/')[-1]
path = os.path.join(temp, file_name)
f = open(path, 'wb')
with requests.get(DownloadLink, stream=True) as r:
downloaded=0
total = int(r.headers['content-length'])
for i in r.iter_content(chunk_size=1024):
if self.WasCancelled():
self.Destroy()
f.close()
os.remove(path)
self.handle.Destroy()
return None
downloaded+=len(i)
progress = int((downloaded/total)*100)
f.write(i)
if progress == 100:
f.close()
subprocess.run([path, "/SILENT", "/VERYSILENT", "/SUPPRESSMSGBOXES"])
self.Update(progress)