aboutsummaryrefslogtreecommitdiffstats
path: root/lvc/windows/autoupdate.py
blob: ba2e2719a55779ae4fe718a1f315618ff37de1f0 (plain)
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
99
100
101
102
103
104
105
106
107
# @Base: Miro - an RSS based video player application
# Copyright (C) 2017
# Jesus E.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
#
# In addition, as a special exception, the copyright holders give
# permission to link the code of portions of this program with the OpenSSL
# library.
#
# You must obey the GNU General Public License in all respects for all of
# the code used other than OpenSSL. If you modify file(s) with this
# exception, you may extend this exception to your version of the file(s),
# but you are not obligated to do so. If you do not wish to do so, delete
# this exception statement from your version. If you delete this exception
# statement from all source files in the program, then also delete it here.

"""Autoupdate functionality """

import ctypes
import _winreg as winreg
import logging

winsparkle = ctypes.cdll.WinSparkle

APPCAST_URL = 'http://miro-updates.participatoryculture.org/lvc-appcast.xml'


def startup():
    enable_automatic_checks()
    winsparkle.win_sparkle_set_appcast_url(APPCAST_URL)
    winsparkle.win_sparkle_init()


def shutdown():
    winsparkle.win_sparkle_cleanup()


def enable_automatic_checks():
    # We should be able to use win_sparkle_set_automatic_check_for_updates,
    # but that's only available after version 0.4 and the current release
    # version is 0.4
    with open_winsparkle_key() as winsparkle_key:
        if not check_for_updates_set(winsparkle_key):
            set_default_check_for_updates(winsparkle_key)


def open_winsparkle_key():
    """Open the MVC WinSparkle registry key

    If any components are not created yet, we will try to create them
    """
    with open_or_create_key(winreg.HKEY_CURRENT_USER, "Software") as software:
        with open_or_create_key(software,
                                "Participatory Culture Foundation") as pcf:
            with open_or_create_key(pcf, "Libre Video Converter") as lvc:
                return open_or_create_key(lvc, "WinSparkle",
                                          write_access=True)


def open_or_create_key(key, subkey, write_access=False):
    if write_access:
        sam = winreg.KEY_READ | winreg.KEY_WRITE
    else:
        sam = winreg.KEY_READ
    try:
        return winreg.OpenKey(key, subkey, 0, sam)
    except OSError as e:
        if e.errno == 2:
            # Not Found error.  We should create the key
            return winreg.CreateKey(key, subkey)
        else:
            raise


def check_for_updates_set(winsparkle_key):
    try:
        winreg.QueryValueEx(winsparkle_key, "CheckForUpdates")
    except OSError as e:
        if e.errno == 2:
            # not found error.
            return False
        else:
            raise
    else:
        return True


def set_default_check_for_updates(winsparkle_key):
    """Initialize the WinSparkle regstry values with our defaults.

    :param lvc_key winreg.HKey object for to the MVC registry
    """
    logging.info("Writing WinSparkle keys")
    winreg.SetValueEx(winsparkle_key, "CheckForUpdates", 0, winreg.REG_SZ, "1")