Project

Profile

Help

Download (5.39 KB) Statistics View on GitHub Reload from mirrored respository
| Branch: | Tag: | Revision:

github / src / updater.py @ master

1 7efcc006 Vincent Le Goff
# Copyright (c) 2016-2020, LE GOFF Vincent
2 8ca46736 Vincent Le Goff
# All rights reserved.
3
4
# Redistribution and use in source and binary forms, with or without
5
# modification, are permitted provided that the following conditions are met:
6
7
# * Redistributions of source code must retain the above copyright notice, this
8
#   list of conditions and the following disclaimer.
9
10
# * Redistributions in binary form must reproduce the above copyright notice,
11
#   this list of conditions and the following disclaimer in the documentation
12
#   and/or other materials provided with the distribution.
13
14
# * Neither the name of ytranslate nor the names of its
15
#   contributors may be used to endorse or promote products derived from
16
#   this software without specific prior written permission.
17
18
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29
30
"""Auto-updater of the CocoMUD client."""
31
32 e97bfeca Vincent Le Goff
import os
33
34
from configobj import ConfigObj
35
from ytranslate import init, select, t
36 8ca46736 Vincent Le Goff
import wx
37 8f68a7b0 Vincent Le Goff
from wx.lib.pubsub import pub
38 8ca46736 Vincent Le Goff
39
from autoupdate import AutoUpdate
40 5148ac31 Vincent Le Goff
from version import BUILD
41 8ca46736 Vincent Le Goff
42 e97bfeca Vincent Le Goff
# Determines the user's language
43
AVAILABLE_LANGUAGES = ("en", "fr")
44
DEFAULT_LANGUAGE = "en"
45
path = os.path.join("settings", "options.conf")
46
config = ConfigObj(path)
47
try:
48
    lang = config["general"]["language"]
49
    assert lang in AVAILABLE_LANGUAGES
50
except (KeyError, AssertionError):
51
    lang = DEFAULT_LANGUAGE
52
53
# Translation
54
init(root_dir="translations")
55
select(lang)
56
57
# Classes
58
59 5148ac31 Vincent Le Goff
class DummyUpdater(wx.Frame):
60 8ca46736 Vincent Le Goff
61 5148ac31 Vincent Le Goff
    """Dummy updater, to which updaters should inherit."""
62
63
    def __init__(self, parent):
64
        wx.Frame.__init__(self, parent)
65
        self.autoupdater = None
66 e97bfeca Vincent Le Goff
        self.default_text = t("ui.message.update.loading")
67 5148ac31 Vincent Le Goff
        self.progress = 0
68 8ca46736 Vincent Le Goff
69 5148ac31 Vincent Le Goff
        # Event binding
70 367789e1 Vincent Le Goff
        pub.subscribe(self.OnGauge, "gauge")
71
        pub.subscribe(self.OnText, "text")
72
        pub.subscribe(self.OnForceDestroy, "forceDestroy")
73
        pub.subscribe(self.OnResponseUpdate, "responseUpdate")
74 5148ac31 Vincent Le Goff
75
    def create_updater(self, just_checking=False):
76
        """Create a new autoupdater instance."""
77
        self.autoupdate = AutoUpdate(BUILD, self, just_checking=just_checking)
78 8ca46736 Vincent Le Goff
        self.autoupdate.start()
79 5148ac31 Vincent Le Goff
80 367789e1 Vincent Le Goff
    def OnGauge(self, value=0):
81 5148ac31 Vincent Le Goff
        """The progress indicator changes."""
82
        pass
83
84 367789e1 Vincent Le Goff
    def OnText(self, text=""):
85 5148ac31 Vincent Le Goff
        """The text of the indicator changes."""
86
        pass
87
88 367789e1 Vincent Le Goff
    def OnForceDestroy(self):
89 5148ac31 Vincent Le Goff
        """Ask for the window's destruction."""
90
        pass
91
92 367789e1 Vincent Le Goff
    def OnResponseUpdate(self, build=None):
93 50456a44 Vincent Le Goff
        """The check for updates is complete."""
94 5148ac31 Vincent Le Goff
        pass
95
96
    def UpdateGauge(self, value):
97
        """Change the level indicator."""
98 367789e1 Vincent Le Goff
        wx.CallAfter(pub.sendMessage, "gauge", value=value)
99 5148ac31 Vincent Le Goff
100
    def UpdateText(self, text):
101
        """Change the text."""
102 367789e1 Vincent Le Goff
        wx.CallAfter(pub.sendMessage, "text", text=text)
103 5148ac31 Vincent Le Goff
104
    def AskDestroy(self):
105 367789e1 Vincent Le Goff
        wx.CallAfter(pub.sendMessage, "forceDestroy")
106 5148ac31 Vincent Le Goff
107 50456a44 Vincent Le Goff
    def ResponseUpdate(self, build):
108
        """The check for updates has responded.
109

110
        Note: the build parameter may be None (no update is available)
111
        or a number (updates are available).
112

113
        """
114 367789e1 Vincent Le Goff
        wx.CallAfter(pub.sendMessage, "responseUpdate", build=build)
115 5148ac31 Vincent Le Goff
116
117
class Updater(DummyUpdater):
118
119
    """Graphical updater with a gauge."""
120
121
    def __init__(self, parent, just_checking=False):
122
        DummyUpdater.__init__(self, parent)
123
        self.create_updater(just_checking)
124 8ca46736 Vincent Le Goff
        self.InitUI()
125 935d7e8f Vincent Le Goff
        self.SetTitle(t("ui.message.update.updating"))
126 8ca46736 Vincent Le Goff
        self.Show()
127
        self.Center()
128
129
    def InitUI(self):
130
        panel = wx.Panel(self)
131
        sizer = wx.BoxSizer(wx.VERTICAL)
132
        panel.SetSizer(sizer)
133 e97bfeca Vincent Le Goff
        self.text = wx.TextCtrl(panel, value=self.default_text,
134 935d7e8f Vincent Le Goff
                size=(600, 100), style=wx.TE_MULTILINE | wx.TE_READONLY)
135 8ca46736 Vincent Le Goff
        self.gauge = wx.Gauge(panel, range=100, size=(250, 25))
136
        self.cancel = wx.Button(panel, wx.ID_CANCEL)
137
138
        # Window design
139
        sizer.Add(self.text)
140
        sizer.Add(self.gauge)
141
        sizer.Add(self.cancel)
142
143
        # Event binding
144
        self.Bind(wx.EVT_BUTTON, self.OnCancel, self.cancel)
145
146 367789e1 Vincent Le Goff
    def OnGauge(self, value=0):
147
        self.gauge.SetValue(value)
148 8ca46736 Vincent Le Goff
        text = self.default_text
149 367789e1 Vincent Le Goff
        text += " ({}%)".format(value)
150 8ca46736 Vincent Le Goff
        self.text.SetValue(text)
151
152 367789e1 Vincent Le Goff
    def OnText(self, text):
153
        self.default_text = t(text)
154
        self.text.SetValue(self.default_text)
155 8ca46736 Vincent Le Goff
156 367789e1 Vincent Le Goff
    def OnForceDestroy(self):
157 0df353d7 Vincent Le Goff
        self.Destroy()
158
159 8ca46736 Vincent Le Goff
    def OnCancel(self, e):
160
        """The user clicks on 'cancel'."""
161 e97bfeca Vincent Le Goff
        value = wx.MessageBox(t("ui.message.update.confirm_cancel"),
162
                t("ui.dialog.confirm"), wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION)
163 8ca46736 Vincent Le Goff
164
        if value == wx.YES:
165
            self.Destroy()
166
167
168
# AppMainLoop
169 5148ac31 Vincent Le Goff
if __name__ == "__main__":
170
    app = wx.App()
171
    frame = Updater(None)
172
    app.MainLoop()