1#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3
4"""
5
6    Copyright (C) 2011-2013 Martijn Kaijser
7    Copyright (C) 2013-2014 Team-XBMC
8    Copyright (C) 2014-2019 Team Kodi
9
10    This file is part of service.xbmc.versioncheck
11
12    SPDX-License-Identifier: GPL-3.0-or-later
13    See LICENSES/GPL-3.0-or-later.txt for more information.
14
15"""
16
17from contextlib import closing
18import os
19import sys
20
21import xbmc  # pylint: disable=import-error
22import xbmcaddon  # pylint: disable=import-error
23import xbmcgui  # pylint: disable=import-error
24import xbmcvfs  # pylint: disable=import-error
25
26_ADDON = xbmcaddon.Addon('service.xbmc.versioncheck')
27_ADDON_NAME = _ADDON.getAddonInfo('name')
28if sys.version_info[0] >= 3:
29    _ADDON_PATH = _ADDON.getAddonInfo('path')
30else:
31    _ADDON_PATH = _ADDON.getAddonInfo('path').decode('utf-8')
32_ICON = _ADDON.getAddonInfo('icon')
33
34
35class Viewer:
36    """ Show user a text viewer (WINDOW_DIALOG_TEXT_VIEWER)
37    Include the text file for the viewers body in the resources/ directory
38
39    usage:
40        script_path = os.path.join(_ADDON_PATH, 'resources', 'lib', 'version_check', 'viewer.py')
41        xbmc.executebuiltin('RunScript(%s,%s,%s)' % (script_path, 'Heading', 'notice.txt'))
42
43    :param heading: text viewer heading
44    :type heading: str
45    :param filename: filename to use for text viewers body
46    :type filename: str
47    """
48    WINDOW = 10147
49    CONTROL_LABEL = 1
50    CONTROL_TEXTBOX = 5
51
52    def __init__(self, heading, filename):
53        self.heading = heading
54        self.filename = filename
55        # activate the text viewer window
56        xbmc.executebuiltin('ActivateWindow(%d)' % (self.WINDOW,))
57        # get window
58        self.window = xbmcgui.Window(self.WINDOW)
59        # give window time to initialize
60        xbmc.sleep(100)
61        # set controls
62        self.set_controls()
63
64    def set_controls(self):
65        """ Set the window controls
66        """
67        # get text viewer body text
68        text = self.get_text()
69        # set heading
70        self.window.getControl(self.CONTROL_LABEL).setLabel('%s : %s' % (_ADDON_NAME,
71                                                                         self.heading,))
72        # set text
73        self.window.getControl(self.CONTROL_TEXTBOX).setText(text)
74        xbmc.sleep(2000)
75
76    def get_text(self):
77        """ Get the text viewers body text from self.filename
78
79        :return: contents of self.filename
80        :rtype: str
81        """
82        try:
83            return self.read_file(self.filename)
84        except Exception as error:  # pylint: disable=broad-except
85            xbmc.log(_ADDON_NAME + ': ' + str(error), xbmc.LOGERROR)
86        return ''
87
88    @staticmethod
89    def read_file(filename):
90        """ Read the contents of the provided file, from
91        os.path.join(_ADDON_PATH, 'resources', filename)
92
93        :param filename: name of file to read
94        :type filename: str
95        :return: contents of the provided file
96        :rtype: str
97        """
98        filename = os.path.join(_ADDON_PATH, 'resources', filename)
99        with closing(xbmcvfs.File(filename)) as open_file:
100            contents = open_file.read()
101        return contents
102
103
104class WebBrowser:
105    """ Display url using the default browser
106
107    usage:
108        script_path = os.path.join(_ADDON_PATH, 'resources', 'lib', 'version_check', 'viewer.py')
109        xbmc.executebuiltin('RunScript(%s,%s,%s)' % (script_path, 'webbrowser', 'https://kodi.tv/'))
110
111    :param url: url to open
112    :type url: str
113    """
114
115    def __init__(self, url):
116        self.url = url
117        try:
118            # notify user
119            self.notification(_ADDON_NAME, self.url)
120            xbmc.sleep(100)
121            # launch url
122            self.launch_url()
123        except Exception as error:  # pylint: disable=broad-except
124            xbmc.log(_ADDON_NAME + ': ' + str(error), xbmc.LOGERROR)
125
126    @staticmethod
127    def notification(heading, message, icon=None, time=15000, sound=True):
128        """ Create a notification
129
130        :param heading: notification heading
131        :type heading: str
132        :param message: notification message
133        :type message: str
134        :param icon: path and filename for the notification icon
135        :type icon: str
136        :param time: time to display notification
137        :type time: int
138        :param sound: is notification audible
139        :type sound: bool
140        """
141        if not icon:
142            icon = _ICON
143        xbmcgui.Dialog().notification(heading, message, icon, time, sound)
144
145    def launch_url(self):
146        """ Open self.url in the default web browser
147        """
148        import webbrowser  # pylint: disable=import-outside-toplevel
149        webbrowser.open(self.url)
150
151
152if __name__ == '__main__':
153    try:
154        if sys.argv[1] == 'webbrowser':
155            WebBrowser(sys.argv[2])
156        else:
157            Viewer(sys.argv[1], sys.argv[2])
158    except Exception as err:  # pylint: disable=broad-except
159        xbmc.log(_ADDON_NAME + ': ' + str(err), xbmc.LOGERROR)
160