-
Notifications
You must be signed in to change notification settings - Fork 3
/
Plugin.pm
343 lines (267 loc) · 9.55 KB
/
Plugin.pm
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# AutoRescan Plugin for Squeezebox Server
# Copyright © Stuart Hickinbottom 2007-2014
# This file is part of AutoRescan.
#
# AutoRescan 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.
#
# AutoRescan 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 AutoRescan; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
# This is a plugin to provide automatic rescanning of music files as they are
# changed within the filesystem. It depends on the 'inotify' kernel function
# within Linux and, therefore, currently only works when used on a Linux system
# where that kernel feature has been enabled. See the INSTALL file for further
# instructions on the kernel configuration.
#
# For further details see:
# http://www.hickinbottom.com
use strict;
use warnings;
package Plugins::AutoRescan::Plugin;
use base qw(Slim::Plugin::Base);
use utf8;
use Plugins::AutoRescan::Settings;
use Slim::Utils::Strings qw (string);
use Slim::Utils::Timers;
use Slim::Utils::Log;
use Slim::Utils::Prefs;
use Time::HiRes;
use Scalar::Util qw(blessed);
use File::Find;
use File::Basename;
use Slim::Utils::OSDetect;
# Name of this plugin - used for various global things to prevent clashes with
# other plugins.
use constant PLUGIN_NAME => 'PLUGIN_AUTORESCAN';
# Preference ranges and defaults.
use constant AUTORESCAN_DELAY_DEFAULT => 5;
use constant AUTORESCAN_SCRIPT_DEFAULT => '';
# Polling control.
use constant AUTORESCAN_POLL => 1;
# Export the version to the server (as a subversion keyword).
use vars qw($VERSION);
$VERSION = 'v@@VERSION@@ (trunk-7.x)';
# A logger we will use to write plugin-specific messages.
my $log = Slim::Utils::Log->addLogCategory(
{
'category' => 'plugin.autorescan',
'defaultLevel' => 'INFO',
'description' => 'PLUGIN_AUTORESCAN'
}
);
# Monitor object that contains the platform-specific functionality for
# monitoring directories for changes.
my $monitor;
# Hash so we can track directory monitors.
my %monitors;
# Access to preferences for this plugin and for server-wide settings.
my $myPrefs = preferences('plugin.autorescan');
my $serverPrefs = preferences('server');
# Flag to protect against multiple initialisation or shutdown
my $initialised = 0;
# Below are functions that are part of the standard Squeezebox Server plugin
# interface.
# Return the name of this plugin; this goes on the server setting plugin
# page, for example.
sub getDisplayName {
return PLUGIN_NAME;
}
# Set up this plugin when it's inserted or the server started.
sub initPlugin() {
my $class = shift;
return if $initialised; # don't need to do it twice
$log->info("Initialising $VERSION");
$class->SUPER::initPlugin(@_);
# Create the monitor interface, depending on our current platorm.
my $os = Slim::Utils::OSDetect::OS();
if ( $os eq 'unix' ) {
$log->debug('Linux monitoring method selected');
eval 'use Plugins::AutoRescan::Monitor_Linux';
$monitor = Plugins::AutoRescan::Monitor_Linux->new($class);
} elsif ( $os eq 'win' ) {
$log->debug('Windows monitoring method selected');
eval 'use Plugins::AutoRescan::Monitor_Windows';
$monitor = Plugins::AutoRescan::Monitor_Windows->new($class);
} else {
$log->warn(
"Unsupported operating system type '$os' - will not monitor for changes"
);
}
my $can_script = 0;
$can_script = $monitor->{can_script} if ($monitor);
# Initialise settings.
Plugins::AutoRescan::Settings->new( $class, $can_script );
# Remember we're now initialised. This prevents multiple-initialisation,
# which may otherwise cause trouble with duplicate hooks or modes.
$initialised = 1;
# Make sure the preferences are set to something sensible before we call
# on them later.
checkDefaults();
# If initialisation worked then add monitors.
addWatch() if ($monitor);
$log->debug("Initialisation complete");
}
# Called when the plugin is being disabled or Squeezebox Server shut down.
sub shutdownPlugin() {
my $class = shift;
return if !$initialised; # don't need to do it twice
$log->debug("Shutting down");
# If we've still got a pending callback timer then cancel it so we're
# not called back after shutdown.
killCallbackTimer();
# Shutdown the monitor.
$log->debug("Removing change monitor");
$monitor->delete if $monitor;
# We're no longer initialised.
$initialised = 0;
}
# Below are functions that are specific to this plugin.
# Called during initialisation, this makes sure that the plugin preferences
# stored are sensible. This has the effect of adding them the first time this
# plugin is activated and removing the need to check they're defined in each
# case of reading them.
sub checkDefaults {
if ( !defined( $myPrefs->get('delay') ) ) {
$myPrefs->set( 'delay', AUTORESCAN_DELAY_DEFAULT );
}
if ( !defined( $myPrefs->get('script') ) ) {
$myPrefs->set( 'script', AUTORESCAN_SCRIPT_DEFAULT );
}
# If the revision isn't yet in the preferences we set it to something
# that's guaranteed to be different so that we can detect the plugin
# is used for the first time.
if ( !defined( $myPrefs->get('revision') ) ) {
$myPrefs->set( 'revision', '-undefined-' );
}
}
# Add a watch to the music folder.
sub addWatch() {
# Filter media directories for those with audio - LMS7.7+ only.
my $audioDirs = Slim::Utils::Misc::getMediaDirs('audio');
for my $audioDir (@$audioDirs) {
if ( defined $audioDir && -d $audioDir ) {
$log->debug("Adding monitor to music directory: $audioDir");
# Add the watch callback. This will also watch all subordinate folders.
addNotifierRecursive($audioDir);
# Tell the monitor.
$monitor->addDone if $monitor;
# Add a poller callback timer. We need this to pump events.
Slim::Utils::Timers::setTimer( undef,
Time::HiRes::time() + AUTORESCAN_POLL, \&poller );
} else {
$log->info(
"Music folder is not defined - skipping add of change monitor");
}
}
}
# Recursively add notifiers for all folders under a given root.
sub addNotifierRecursive($) {
my $dir = shift;
if ( -d $dir ) {
find(
{
wanted => sub {
addNotifier($File::Find::name) if -d $File::Find::name;
},
follow => 1,
no_chdir => 1
},
$dir
);
}
}
# Add an monitor for a given directory.
sub addNotifier($) {
my $dir = shift;
# We prune the search from directories that start with a '.'
# (don't you also keep your music files in git-annex..?)
if (basename($dir) =~ m/^\./) {
$File::Find::prune = 1;
$log->debug("Not monitoring hidden directory tree: $dir");
return;
}
# Only add a monitor if we're not already monitoring this directory (and
# it is indeed a directory).
if (not exists $monitors{$dir}) {
# Remember the monitor object created - we do this so we can check if
# it's already being monitored later on.
$monitors{$dir} = $monitor->addWatch($dir);
}
}
# Called periodically so we can detect and dispatch any events.
sub poller() {
# Pump that poller - let the monitors decide how to do that. We support
# pumping for each monitored directory, or only once in total, depending
# on what the monitor type wants to do.
if ( $monitor && $monitor->{poll_each} ) {
# Loop through the monitored directories and poll each.
for my $dir ( keys %monitors ) {
$monitor->poll( $dir, $monitors{$dir} );
}
} else {
# Pump the poller once.
$monitor->poll if $monitor;
}
# Schedule another poll.
Slim::Utils::Timers::setTimer( undef, Time::HiRes::time() + AUTORESCAN_POLL,
\&poller );
}
# Note a directory as having been touched - we schedule a callback to see if
# it's convenient to perform a rescan.
sub noteTouch {
my $dir = shift;
# Schedule a callback to trigger a rescan in a short time.
setCallbackTimer();
# Make sure we are monitoring any new subdirectories under here.
addNotifierRecursive($dir);
}
# Remove any existing delayed callback timer. This is tolerant if there's
# currently no timer set.
sub killCallbackTimer {
$log->debug("Cancelling any pending change callback");
Slim::Utils::Timers::killOneTimer( undef, \&delayedChangeCallback );
}
# Add a new callback timer to call us back in a short while.
sub setCallbackTimer {
# Remove any existing timer.
killCallbackTimer();
# Schedule a callback.
$log->debug("Scheduling a delayed callback");
Slim::Utils::Timers::setTimer( undef,
Time::HiRes::time() + $myPrefs->get('delay'),
\&delayedChangeCallback );
}
# Called following a short delay following the most recently detected change.
# This is what ultimately triggers the database rescan.
sub delayedChangeCallback {
$log->debug("Delayed callback invoked");
# Check if there's a scan currently in progress.
if ( Slim::Music::Import->stillScanning() ) {
# If so then schedule another delayed callback - we'll try again in a short
# while.
$log->debug("Putting off rescan due to current scan being in progress");
setCallbackTimer();
} else {
# If not then we'll trigger the rescan now.
$log->info("Triggering database rescan following directory changes");
Slim::Control::Request::executeRequest( undef, ['rescan'] );
# If the monitor supports a rescan script then call it.
if ( $monitor->{can_script} ) {
$monitor->executeScript( $myPrefs->get('script') );
}
}
}
1;
__END__
# Local Variables:
# tab-width:4
# indent-tabs-mode:t
# End: