1import os.path
2import asyncio
3import json
4
5from gi.repository import GLib, Gtk
6
7from pychess import VERSION
8from pychess.widgets import mainwindow
9from pychess.System import download_file_async, prefix
10
11URL = "https://api.github.com/repos/pychess/pychess/releases/latest"
12LINK = "https://github.com/pychess/pychess/releases"
13
14
15def isgit():
16    return os.path.isdir(prefix.addDataPrefix(".git"))
17
18
19@asyncio.coroutine
20def checkversion():
21    if isgit():
22        return
23
24    new_version = None
25
26    filename = yield from download_file_async(URL)
27
28    if filename is not None:
29        with open(filename, encoding="utf-8") as f:
30            new_version = json.loads(f.read())["name"]
31
32    def notify(new_version):
33        msg_dialog = Gtk.MessageDialog(mainwindow(), type=Gtk.MessageType.INFO,
34                                       buttons=Gtk.ButtonsType.OK)
35
36        msg = _("<b>New version %s is available!</b>" % new_version)
37        msg_dialog.set_markup(msg)
38        msg_dialog.format_secondary_markup('<a href="%s">%s</a>' % (LINK, LINK))
39
40        msg_dialog.connect("response", lambda msg_dialog, a: msg_dialog.hide())
41        msg_dialog.show()
42
43    if new_version is not None and VERSION.split(".") < new_version.split("."):
44        GLib.idle_add(notify, new_version)
45