-
Notifications
You must be signed in to change notification settings - Fork 0
/
default.py
109 lines (81 loc) · 3.77 KB
/
default.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
99
100
101
102
103
104
105
106
107
108
109
import xbmc,xbmcvfs,xbmcgui
import time
import resources.lib.utils as utils
from resources.lib.database import Database, WatchHistory,DBSettings
class HistoryService:
playerMonitor = None
updateMonitor = None
historyDB = None
settings = None
require_pin = False #need to store this as it might change
def __init__(self):
utils.log("Starting History Service")
self.playerMonitor = HistoryPlayerMonitor(on_play_started = self.playStarted)
self.updateMonitor = HistorySettingsMonitor(update_settings = self.onSettingsUpdate)
#setup the database connection
database = Database()
database.connect()
database.checkDBStructure()
self.historyDB = WatchHistory(database)
self.settings = DBSettings(database)
if(utils.getSetting('require_pin_on_change') == 'true'):
self.require_pin = True
#backup the settings file
self._settingsBackup()
def run(self):
while(not xbmc.abortRequested):
xbmc.sleep(500)
def playStarted(self):
if(self.playerMonitor.isPlayingVideo() and utils.getSetting('monitor_video') == 'true'):
videoTag = self.playerMonitor.getVideoInfoTag()
utils.log("Logging: " + videoTag.getTitle(),xbmc.LOGDEBUG)
self.historyDB.insert(("video",videoTag.getTitle(),self.playerMonitor.getPlayingFile(),int(time.time())))
elif(self.playerMonitor.isPlayingAudio() and utils.getSetting('monitor_music') == 'true'):
audioTag = self.playerMonitor.getMusicInfoTag()
utils.log("Logging: " + audioTag.getTitle(),xbmc.LOGDEBUG)
self.historyDB.insert(('audio',audioTag.getTitle(),self.playerMonitor.getPlayingFile(),int(time.time())))
def onSettingsUpdate(self):
#check if a pin is required to change settings
if(self.require_pin):
dialog = xbmcgui.Dialog()
#ask the user for their pin
user_try = dialog.numeric(0,'Pin required to change settings')
if(self.settings.checkPIN(user_try)):
#make backup of new settings
self._settingsBackup()
if(utils.getSetting('require_pin_on_change') == 'false'):
self.require_pin = False
else:
#restore the settings file
self._settingsRestore()
dialog.ok('Error','Incorrect PIN')
else:
#check this setting in case it's the one that changed
if(utils.getSetting('require_pin_on_change') == 'true'):
self.require_pin = True
def _settingsBackup(self):
#make a backup of the settings file (in case pin is incorrect)
settings_file = xbmc.translatePath(utils.data_dir() + "settings.xml")
if(xbmcvfs.exists(settings_file)):
xbmcvfs.copy(settings_file,settings_file + ".bak")
def _settingsRestore(self):
#restore the settings file (in case pin is incorrect)
settings_file = xbmc.translatePath(utils.data_dir() + "settings.xml")
if(xbmcvfs.exists(settings_file)):
xbmcvfs.copy(settings_file + ".bak",settings_file)
class HistoryPlayerMonitor(xbmc.Player):
onPlay = None
def __init__(self,*args,**kwargs):
xbmc.Player.__init__(self)
self.onPlay = kwargs['on_play_started']
def onPlayBackStarted(self):
self.onPlay()
class HistorySettingsMonitor(xbmc.Monitor):
update_settings = None
def __init__(self,*args,**kwargs):
xbmc.Monitor.__init__(self)
self.update_settings = kwargs['update_settings']
def onSettingsChanged(self):
self.update_settings()
historyService = HistoryService()
historyService.run()