-
Notifications
You must be signed in to change notification settings - Fork 7
/
plugins.tcl
262 lines (215 loc) · 6.86 KB
/
plugins.tcl
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package provide de1_plugins 1.0
package require de1_logging 1.1
proc plugin_directory {} {
return "plugins"
}
proc plugin_settings_file {plugin} {
return "[homedir]/[plugin_directory]/${plugin}/settings.tdb"
}
proc plugin_settings {plugin} {
return "::plugins::${plugin}::settings"
}
proc load_plugin_settings {plugin} {
plugins load_settings $plugin
}
proc save_plugin_settings {plugin} {
plugins save_settings $plugin
}
proc plugin_preload {plugin} {
plugins preload $plugin
}
proc load_plugin {plugin} {
plugins load $plugin
}
proc plugin_enabled {plugin} {
plugins enabled $plugin
}
proc plugin_available {plugin} {
plugins available $plugin
}
proc plugin_preloaded {plugin} {
plugins loaded $plugin
}
proc plugin_loaded {plugin} {
plugins loaded $plugin
}
proc toggle_plugin {plugin} {
plugins toggle $plugin
}
proc available_plugins {} {
plugins list
}
proc load_plugins {} {
plugins init
}
proc enable_plugin {plugin} {
plugins enable $plugin
}
proc disable_plugin {plugin} {
plugins disable $plugin
}
namespace eval ::plugins {
namespace export load_settings save_settings peek load list toggle disable enable init available enabled loaded peeked read gui preload
namespace ensemble create
proc read {plugin} {
if {[file exists "[homedir]/[plugin_directory]/$plugin/plugin.tcl"] != 1} {
msg -ERROR [namespace current] "Plugin $plugin does not exist"
return 0
}
::source "[homedir]/[plugin_directory]/${plugin}/plugin.tcl"
return 1
}
# Keeping compatibilty to early 1.34 beta version
proc preload {plugin} {
load $plugin
}
proc peek {plugin} {
msg -INFO [namespace current] "peeking $plugin"
if { [peeked $plugin] } {
return
}
# Set metadata in case the plugin does not
namespace eval ::plugins::$plugin {
variable author {}
variable contact {}
variable version {}
variable name {}
variable description {}
variable plugin_peeked 0
variable plugin_loaded 0
}
array set ::plugins::${plugin}::settings {}
plugins load_settings $plugin
if {[catch {
if {$plugin == "D_Flow_Espresso_Profile"} {
if {[plugin_enabled $plugin] != true} {
# don't peek into the D-Flow plugin code at all if not enabled, because it overwrites other code in the de1app. Can undo this patch once D-Flow behaves like other extensions
return 0
}
}
msg -INFO [namespace current] "sourcing $plugin"
if {[::plugins::read $plugin] != 1} {
error "sourcing failed"
}
set ${plugin}::plugin_peeked 1
} err opts_dict] != 0} {
::logging::log_error_result_opts_dict $err $opts_dict
catch {
info_page [subst {${plugin}:[translate "The plugin could not be sourced for metadata"]\n\n$err}] [translate "Ok"]
}
}
}
proc gui {plugin context} {
set ${plugin}::ui_entry $context
}
proc load {plugin} {
if { [loaded $plugin] } {
return
}
if {[catch {
if {[info proc ${plugin}::preload] != ""} {
set ${plugin}::ui_entry [${plugin}::preload]
}
if {[info proc ${plugin}::main] != ""} {
${plugin}::main
} else {
borg toast "loaded empty plugin $plugin"
}
set ${plugin}::plugin_loaded 1
msg -NOTICE "loaded plugin" $plugin
} err opts_dict] != 0} {
::logging::log_error_result_opts_dict $err $opts_dict
catch {
if {!::debugging} {
# remove from enabled plugins
disable_plugin $plugin
}
}
catch {
info_page [subst {${plugin}:[translate "The plugin could not be loaded. Disabled"]\n\n$err}] [translate "Ok"]
}
}
}
proc list {} {
set plugin_sources [lsort -dictionary [glob -nocomplain -tails -type d -directory "[homedir]/[plugin_directory]" * ]]
set plugins {}
foreach p $plugin_sources {
set fbasename [file rootname [file tail $p]]
if {[file exists "[homedir]/[plugin_directory]/$fbasename/plugin.tcl"] == 1} {
lappend plugins $fbasename
}
}
return $plugins
}
proc toggle {plugin} {
if {[plugin_enabled $plugin]} {
disable_plugin $plugin
} else {
enable_plugin $plugin
}
return [plugin_enabled $plugin]
}
proc disable {plugin} {
if {[plugin_enabled $plugin] == 0} {
return 0;
}
set new [lsearch -inline -all -not -exact $::settings(enabled_plugins) $plugin]
set ::settings(enabled_plugins) $new
::save_settings
return 1;
}
proc enable {plugin} {
if {[plugin_enabled $plugin]} {
return 0;
}
lappend ::settings(enabled_plugins) $plugin
::save_settings
plugins peek $plugin
plugins load $plugin
return 1;
}
proc save_settings {plugin} {
save_array_to_file [plugin_settings $plugin] [plugin_settings_file $plugin]
}
proc load_settings {plugin} {
msg -NOTICE [namespace current] "loading settings for $plugin"
set fn [plugin_settings_file $plugin]
if { [file exists $fn] } {
set settings_file_contents [encoding convertfrom utf-8 [read_binary_file $fn]]
if {[string length $settings_file_contents] != 0} {
array set [plugin_settings $plugin] $settings_file_contents
return 1
}
}
msg -INFO [namespace current] "Settings file $fn not found"
return 0
}
proc init {} {
# Source all plugins
foreach plugin [plugins list] {
plugins peek $plugin
}
# start enabled plugins
foreach plugin $::settings(enabled_plugins) {
plugins load $plugin
}
}
#
# Status requests
#
proc available {plugin} {
return [expr {[lsearch [plugins list] $plugin] >= 0}]
}
proc peeked {plugin} {
return [expr {[info exists ::plugins::${plugin}::plugin_peeked] && [subst \$::plugins::${plugin}::plugin_peeked] == 1}]
}
proc loaded {plugin} {
return [expr {[info exists ::plugins::${plugin}::plugin_loaded] && [subst \$::plugins::${plugin}::plugin_loaded] == 1}]
}
proc enabled {plugin} {
if {[lsearch $::settings(enabled_plugins) $plugin] >= 0} {
return true
}
return false
}
}