forked from Zomboided/service.vpn.manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
windowfilter.py
150 lines (128 loc) · 5.58 KB
/
windowfilter.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2016 Zomboided
#
# 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 3 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, see <http://www.gnu.org/licenses/>.
#
# This module deals with the setting, editing and removal of window filters
import xbmc
import xbmcaddon
import xbmcgui
from libs.utility import debugTrace, errorTrace, infoTrace, newPrint, getID, getName
def editRange(default):
# Ask user for range of window IDs and return "low-high" if value or "" if invalid
default_low = ""
default_high = ""
if not default == "":
default_low, default_high = default.split("-")
new_filter_low = xbmcgui.Dialog().numeric(0, "Enter first window ID of range", default_low)
new_filter_high = xbmcgui.Dialog().numeric(0, "Enter last window ID of range", default_high)
if (not(new_filter_low == "" or new_filter_high == "")) and (int(new_filter_low) < int(new_filter_high)) and int(new_filter_low) > 9999 and int(new_filter_high) < 100000:
return new_filter_low + "-" + new_filter_high
else:
xbmcgui.Dialog().ok(addon_name, "Range is invalid. IDs should be 5 characters and the first ID should be less than the second ID.")
return ""
def editSingle(default):
new_filter = xbmcgui.Dialog().numeric(0, "Enter window ID", default)
if (not new_filter == "") and int(new_filter) > 9999 and int(new_filter) < 100000:
return new_filter
else:
xbmcgui.Dialog().ok(addon_name, "ID is invalid. IDs should be 5 characters")
return ""
vpn = sys.argv[1]
debugTrace("-- Entered windowfilter.py with parameter " + vpn + " --")
if not getID() == "":
id_range = "[I]Add range of IDs[/I]"
id_single = "[I]Add single ID[/I]"
id_reset = "[I]Delete all[/I]"
id_cancel = "[I]Cancel changes[/I]"
id_done = "[I]Done[/I]"
addon = xbmcaddon.Addon(getID())
addon_name = getName()
show_filters = True
# Build the list of filters
if vpn == "0":
filter_string = addon.getSetting("vpn_excluded_windows")
else:
filter_string = addon.getSetting(vpn + "_vpn_windows")
if not filter_string == "":
filters = filter_string.split(",")
else:
filters = []
# And add options to the filters. Cancel is last as pressing escape is like selecting the last option
filters.append(id_single)
filters.append(id_range)
filters.append(id_reset)
filters.append(id_done)
filters.append(id_cancel)
if vpn == "1" : vpnth = " first"
if vpn == "2" : vpnth = " second"
if vpn == "3" : vpnth = " third"
if vpn == "4" : vpnth = " fourth"
if vpn == "5" : vpnth = " fifth"
if vpn == "6" : vpnth = " sixth"
if vpn == "7" : vpnth = " seventh"
if vpn == "8" : vpnth = " eighth"
if vpn == "9" : vpnth = " ninth"
if vpn == "10" : vpnth = " tenth"
if vpn == "0":
title = "Windows not using a VPN"
else:
title = "Windows using " + vpnth + " VPN"
# Loop around edits until done or cancel are selected
while show_filters:
i = xbmcgui.Dialog().select(title, filters)
if filters[i] == id_done:
# Make the filters into a single string and store it
output_filter = ""
if len(filters) > 5:
for i in range (0, (len(filters)-5)):
if i > 0 : output_filter = output_filter + ","
output_filter = output_filter + filters[i]
if vpn == "0":
addon.setSetting("vpn_excluded_windows", output_filter)
else:
addon.setSetting(vpn + "_vpn_windows", output_filter)
show_filters = False
elif filters[i] == id_cancel:
# Don't commit the changes, just exit loop and return to settings
show_filters = False
elif filters[i] == id_reset:
# Delete all of the filters
if len(filters) > 5:
del filters[0:len(filters)-5]
elif filters[i] == id_single:
# Ask user for window ID
new_filter = editSingle("")
if not new_filter == "": filters.insert(len(filters)-5, new_filter)
elif filters[i] == id_range:
# Ask user for range of window IDs
new_filter = editRange("")
if not new_filter == "": filters.insert(len(filters)-5, new_filter)
else:
# Edit or delete an existing filter
if not xbmcgui.Dialog().yesno(addon_name, "Edit or delete window ID filter " + filters[i] + "?", nolabel="Edit", yeslabel="Delete"):
if "-" in filters[i]:
new_filter = editRange(filters[i])
else:
new_filter = editSingle(filters[i])
if not new_filter == "": filters.insert(len(filters)-5, new_filter)
else:
del filters[i]
command = "Addon.OpenSettings(" + getID() + ")"
xbmc.executebuiltin(command)
else:
errorTrace("windowfilter.py", "VPN service is not ready")
debugTrace("-- Exit windowfilter.py --")