-
Notifications
You must be signed in to change notification settings - Fork 2
/
Plugin.pm
3022 lines (2553 loc) · 97.1 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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# LazySearch2 Plugin for Squeezebox Server
# Copyright © Stuart Hickinbottom 2004-2021
# This file is part of LazySearch2.
#
# LazySearch2 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.
#
# LazySearch2 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 LazySearch2; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
# This is a plugin to implement lazy searching using the Squeezebox/Transporter
# IR remote control.
#
# For further details see:
# http://www.hickinbottom.com/lazysearch
use strict;
use warnings;
package Plugins::LazySearch2::Plugin;
use base qw(Slim::Plugin::Base);
use utf8;
use Plugins::LazySearch2::Settings;
use Slim::Utils::Strings qw (string);
use Slim::Utils::Misc;
use Slim::Utils::Text;
use Slim::Utils::Timers;
use Slim::Utils::Log;
use Slim::Utils::Prefs;
use Slim::Utils::OSDetect;
use Slim::Control::Request;
use Time::HiRes;
use Text::Unidecode;
use Scalar::Util qw(blessed);
# Name of this plugin - used for various global things to prevent clashes with
# other plugins.
use constant PLUGIN_NAME => 'PLUGIN_LAZYSEARCH2';
# Name of the menu item that is expected to be added to the home menu.
use constant LAZYSEARCH_HOME_MENUITEM => 'LazySearch2';
# A special search_type value that indicates that it's actually for a keyword
# search rather than a normal database entity search.
use constant SEARCH_TYPE_KEYWORD => 'Keyword';
# Mode for main lazy search mode and lazy search menu.
use constant LAZYSEARCH_TOP_MODE => 'PLUGIN_LAZYSEARCH2.topmode';
use constant LAZYSEARCH_CATEGORY_MENU_MODE => 'PLUGIN_LAZYSEARCH2.categorymenu';
use constant LAZYBROWSE_MODE => 'PLUGIN_LAZYSEARCH2.browsemode';
use constant LAZYBROWSE_RESULTS_MODE => 'PLUGIN_LAZYSEARCH2.browseresults';
use constant LAZYBROWSE_KEYWORD_MODE => 'PLUGIN_LAZYSEARCH2.keywordbrowse';
# Search button behaviour options.
use constant LAZYSEARCH_SEARCHBUTTON_STANDARD => 0;
use constant LAZYSEARCH_SEARCHBUTTON_TOGGLE => 1;
use constant LAZYSEARCH_SEARCHBUTTON_ARTIST => 2;
use constant LAZYSEARCH_SEARCHBUTTON_ALBUM => 3;
use constant LAZYSEARCH_SEARCHBUTTON_GENRE => 4;
use constant LAZYSEARCH_SEARCHBUTTON_TRACK => 5;
use constant LAZYSEARCH_SEARCHBUTTON_KEYWORD => 6;
use constant LAZYSEARCH_SEARCHBUTTON_MENU => 7;
# Preference ranges and defaults.
use constant LAZYSEARCH_SHOWHELP_DEFAULT => 0;
use constant LAZYSEARCH_MINLENGTH_MIN => 2;
use constant LAZYSEARCH_MINLENGTH_MAX => 9;
use constant LAZYSEARCH_MINLENGTH_ARTIST_DEFAULT => 3;
use constant LAZYSEARCH_MINLENGTH_ALBUM_DEFAULT => 3;
use constant LAZYSEARCH_MINLENGTH_GENRE_DEFAULT => 3;
use constant LAZYSEARCH_MINLENGTH_TRACK_DEFAULT => 4;
use constant LAZYSEARCH_MINLENGTH_KEYWORD_DEFAULT => 4;
use constant LAZYSEARCH_LEFTDELETES_DEFAULT => 1;
use constant LAZYSEARCH_HOOKSEARCHBUTTON_DEFAULT =>
LAZYSEARCH_SEARCHBUTTON_TOGGLE;
use constant LAZYSEARCH_ALLENTRIES_DEFAULT => 1;
use constant LAZYSEARCH_KEYWORD_ARTISTS_DEFAULT => 1;
use constant LAZYSEARCH_KEYWORD_ALBUMS_DEFAULT => 1;
use constant LAZYSEARCH_KEYWORD_TRACKS_DEFAULT => 1;
use constant LAZYSEARCH_KEYWORD_ALBUMARTISTS_DEFAULT => 0;
# Constants that control the background lazy search database encoding.
use constant LAZYSEARCH_ENCODE_MAX_QUANTA => 0.4;
use constant LAZYSEARCH_INITIAL_LAZIFY_DELAY => 5;
# Special item IDs that are used to recognise non-result items in the
# search results list.
use constant RESULT_ENTRY_ID_ALL => -1;
# The character used to separate individual words of a keyword search
# string.
use constant KEYWORD_SEPARATOR_CHARACTER => ',';
# The root of our web pages.
use constant URL_BASE => 'plugins/LazySearch2';
# Maximum WoL keep-awake interval (seconds).
use constant WOL_TICKLE_INTERVAL => 60;
# 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.lazysearch2',
'defaultLevel' => 'INFO',
'description' => 'PLUGIN_LAZYSEARCH2'
}
);
# Access to preferences for this plugin and for server-wide settings.
my $myPrefs = preferences('plugin.lazysearch2');
my $serverPrefs = preferences('server');
# This hash-of-hashes contains state information on the current lazy search for
# each player. The first hash index is the player (eg $clientMode{$client}),
# and the second a 'parameter' for that player.
# The elements of the second hash are as follows:
# search_type: This is the item type being searched for, and can be one
# of Track, Contributor, Album, Genre or Keyword.
# search_text: The current search text (ie the number keys on the remote
# control).
# side: Allows the search to be constrained to one side of
# the pipe in the customsearch column or the other.
# side=1 searches left, side=2 searches right, anything
# else isn't specific.
# perform_search: Function reference to a function that will perform
# the actual search. The search text is passed to this
# function.
# search_performed: Indicates whether a search has yet been performed
# (and hence whether search_items has the search
# results). It contains the search text at the time the
# search was performed.
# search_forced: Whether the results are for a forced search (where
# the user pressed SEARCH before a minimum-length
# string was entered).
# search_pending: A Boolean flag indicating whether there is a search
# scheduled to happen in a short time following the
# change of the search_text.
# hierarchy: Hierarchy definition that is passed to the mixer
# function when invoking a fix (this is a hack to
# allow reuse of BrowseDB's mix invocation function).
# level: Indication of the depth into the current 'hierarchy'
# (this is also part of the hack to reuse BrowseDB's
# mix function).
# all_entry: This is the string to be used for the 'all' entry at
# the end of the list. If this isn't defined then there
# won't be an 'all' entry added.
# select_col: This is the 'field' that the find returns.
# player_title: Start of line1 player text when there are search
# results.
# player_title_empty: The line1 text when no search has yet been performed.
# enter_more_prompt: The line2 prompt shown when there is insufficient
# search text entered to perform the search.
# further_help_prompt: Extra help available to the user by pressing DOWN
# when viewing the prompt to enter characters for that
# search mode.
# min_search_length: The minimum number of characters that must be entered
# before the lazy search is performed.
# onright: Function reference to a method that enters a browse
# mode on the item being displayed.
# search_tracks: Function reference to a method that will return all
# the tracks corresponding to the found item (the item
# is passed as a parameter to this method). This is used
# to find the tracks that will be added/replaced in the
# playlist when ADD/INSERT/PLAY is pressed.
# mix_type: The mix type CLI argument for MusicIP mixes created
# from this search type.
my %clientMode = ();
# Hash of outstanding database objects that are to be lazy-encoded. This is
# present to allow a background task to work on them in chunks, preventing
# performance problems caused by the server going busy for a long time.
# The structure of the hash is as follows:
# type => { rs, source_attr, keyword_artist, keyword_album, keyword_track,
# ids }
# Where:
# type is 'album', 'artist', 'genre' or 'track'.
# rs is the DBIx ResultSet containing the items of that type that need to
# be lazified.
# source_attr is the column name in the ResultSet that has the source
# attribute that will be lazified into the custom search.
# keyword_artist, keyword_album, keyword_track are flags (0/1) that indicate
# whether those items are lazified into the customsearch field to support
# custom searches.
# ids is a list (array) of IDs to process.
my %encodeQueues = ();
# Flag to protect against multiple initialisation or shutdown
my $initialised = 0;
# Flag to indicate whether we're currently applying 'lazification' to the
# database. Used to detect and warn the user of this when entering
# lazy search mode while this is in progress.
my $lazifyingDatabase = 0;
# Flag to indicate whether the plugin is running on Windows - if we are then we
# will stop Windows form sleeping during the lazification process.
my $onWindows = 0;
# Time that the last WoL tickle occurred; used to control frequency that the
# tickle API is called.
my $lastWoLTickle = 0;
# The cookie returned from the Server Power Control plugin when Lazy Search has
# set a block on powering down or suspending the server.
my $spcBlockCode = undef;
# Map which is used to quickly translate the button pushes captured by our
# mode back into the numbers on those keys.
my %numberScrollMap = (
'numberScroll_0' => '0',
'numberScroll_1' => '1',
'numberScroll_2' => '2',
'numberScroll_3' => '3',
'numberScroll_4' => '4',
'numberScroll_5' => '5',
'numberScroll_6' => '6',
'numberScroll_7' => '7',
'numberScroll_8' => '8',
'numberScroll_9' => '9',
);
# Below are functions that are part of the standard Squeezebox Server plugin
# interface.
# Main mode of this plugin; offers the artist/album/genre/song browse options
sub setMode {
my $class = shift;
my $client = shift;
my $method = shift || '';
# Handle request to exit our mode.
if ( $method eq 'pop' ) {
leaveMode($client);
# Pop the current mode off the mode stack and restore the previous one
Slim::Buttons::Common::popMode($client);
return;
}
# The menu items shown depend on whether keyword search is enabled or
# not.
my @topMenuItems = (qw({ARTISTS} {ALBUMS} {SONGS} {GENRES}));
if ( keywordSearchEnabled() ) {
push @topMenuItems, '{PLUGIN_LAZYSEARCH2_KEYWORD_MENU_ITEM}';
}
# Use INPUT.Choice to display the top-level search menu choices.
my %params = (
# The header (first line) to display whilst in this mode.
header => '{PLUGIN_LAZYSEARCH2_LINE1_BROWSE} {count}',
# A reference to the list of items to display.
listRef => \@topMenuItems,
# A unique name for this mode that won't actually get displayed
# anywhere.
modeName => LAZYSEARCH_CATEGORY_MENU_MODE,
# An anonymous function that is called every time the user presses the
# RIGHT button.
onRight => sub {
my ( $client, $item ) = @_;
# Push into a sub-mode for the selected category.
enterCategoryItem( $client, $item );
# If rescan is in progress then warn the user.
if ( $lazifyingDatabase || Slim::Music::Import->stillScanning() ) {
$log->info("Entering search while scan in progress");
if ( $client->linesPerScreen == 1 ) {
$client->showBriefly(
{
'line1' => $client->doubleString(
'PLUGIN_LAZYSEARCH2_SCAN_IN_PROGRESS')
}
);
} else {
$client->showBriefly(
{
'line1' =>
string('PLUGIN_LAZYSEARCH2_SCAN_IN_PROGRESS')
}
);
}
}
},
# These are all menu items and so have a right-arrow overlay
overlayRef => sub {
my $client = shift;
return [ undef, $client->symbols('rightarrow') ];
},
);
# Use our INPUT.Choice-derived mode to show the menu and let it do all the
# hard work of displaying the list, moving it up and down, etc, etc.
if ( $method eq 'push' ) {
Slim::Buttons::Common::pushModeLeft( $client,
LAZYSEARCH_CATEGORY_MENU_MODE, \%params );
} else {
Slim::Buttons::Common::pushMode( $client, LAZYSEARCH_CATEGORY_MENU_MODE,
\%params );
$client->update();
}
}
# Enter the correct search category item.
sub enterCategoryItem($$) {
my $client = shift;
my $item = shift;
# Search term initially empty.
$clientMode{$client}{search_text} = '';
$clientMode{$client}{search_items} = ();
$clientMode{$client}{search_performed} = '';
$clientMode{$client}{search_pending} = 0;
# Dispatch to the correct method.
if ( $item eq '{ARTISTS}' ) {
enterArtistSearch( $client, $item );
} elsif ( $item eq '{ALBUMS}' ) {
enterAlbumSearch( $client, $item );
} elsif ( $item eq '{GENRES}' ) {
enterGenreSearch( $client, $item );
} elsif ( $item eq '{SONGS}' ) {
enterTrackSearch( $client, $item );
} elsif ( $item eq '{PLUGIN_LAZYSEARCH2_KEYWORD_MENU_ITEM}' ) {
enterKeywordSearch( $client, $item );
}
}
# Used when the user starts an artist search from the main category menu.
sub enterArtistSearch($$) {
my $client = shift;
my $item = shift;
$clientMode{$client}{search_type} = 'Contributor';
$clientMode{$client}{side} = 0;
$clientMode{$client}{hierarchy} = 'contributor,album,track';
$clientMode{$client}{level} = 0;
$clientMode{$client}{all_entry} = '{ALL_ARTISTS}';
$clientMode{$client}{player_title} =
'{PLUGIN_LAZYSEARCH2_LINE1_BROWSE_ARTISTS}';
$clientMode{$client}{player_title_empty} =
'{PLUGIN_LAZYSEARCH2_LINE1_BROWSE_ARTISTS_EMPTY}';
$clientMode{$client}{enter_more_prompt} =
'PLUGIN_LAZYSEARCH2_LINE2_ENTER_MORE_ARTISTS';
$clientMode{$client}{further_help_prompt} =
'PLUGIN_LAZYSEARCH2_LINE2_BRIEF_HELP';
$clientMode{$client}{min_search_length} =
$myPrefs->get('pref_minlength_artist');
$clientMode{$client}{perform_search} = \&performArtistSearch;
$clientMode{$client}{onright} = \&rightIntoArtist;
$clientMode{$client}{search_tracks} = \&searchTracksForArtist;
$clientMode{$client}{mix_type} = 'artist';
setSearchBrowseMode( $client, $item, 0 );
}
# Used when the user starts an album search from the main category menu.
sub enterAlbumSearch($$) {
my $client = shift;
my $item = shift;
$clientMode{$client}{search_type} = 'Album';
$clientMode{$client}{side} = 0;
$clientMode{$client}{hierarchy} = 'album,track';
$clientMode{$client}{level} = 0;
$clientMode{$client}{all_entry} = '{ALL_ALBUMS}';
$clientMode{$client}{player_title} =
'{PLUGIN_LAZYSEARCH2_LINE1_BROWSE_ALBUMS}';
$clientMode{$client}{player_title_empty} =
'{PLUGIN_LAZYSEARCH2_LINE1_BROWSE_ALBUMS_EMPTY}';
$clientMode{$client}{enter_more_prompt} =
'PLUGIN_LAZYSEARCH2_LINE2_ENTER_MORE_ALBUMS';
$clientMode{$client}{further_help_prompt} =
'PLUGIN_LAZYSEARCH2_LINE2_BRIEF_HELP';
$clientMode{$client}{min_search_length} =
$myPrefs->get('pref_minlength_album');
$clientMode{$client}{perform_search} = \&performAlbumSearch;
$clientMode{$client}{onright} = \&rightIntoAlbum;
$clientMode{$client}{search_tracks} = \&searchTracksForAlbum;
$clientMode{$client}{mix_type} = 'album';
setSearchBrowseMode( $client, $item, 0 );
}
# Used when the user starts a genre search from the main category menu.
sub enterGenreSearch($$) {
my $client = shift;
my $item = shift;
$clientMode{$client}{search_type} = 'Genre';
$clientMode{$client}{side} = 0;
$clientMode{$client}{hierarchy} = 'genre,track';
$clientMode{$client}{level} = 0;
$clientMode{$client}{all_entry} = undef;
$clientMode{$client}{player_title} =
'{PLUGIN_LAZYSEARCH2_LINE1_BROWSE_GENRES}';
$clientMode{$client}{player_title_empty} =
'{PLUGIN_LAZYSEARCH2_LINE1_BROWSE_GENRES_EMPTY}';
$clientMode{$client}{enter_more_prompt} =
'PLUGIN_LAZYSEARCH2_LINE2_ENTER_MORE_GENRES';
$clientMode{$client}{further_help_prompt} =
'PLUGIN_LAZYSEARCH2_LINE2_BRIEF_HELP';
$clientMode{$client}{min_search_length} =
$myPrefs->get('pref_minlength_genre');
$clientMode{$client}{perform_search} = \&performGenreSearch;
$clientMode{$client}{onright} = \&rightIntoGenre;
$clientMode{$client}{search_tracks} = \&searchTracksForGenre;
$clientMode{$client}{mix_type} = 'genre';
setSearchBrowseMode( $client, $item, 0 );
}
# Used when the user starts a track search from the main category menu.
sub enterTrackSearch($$) {
my $client = shift;
my $item = shift;
$clientMode{$client}{search_type} = 'Track';
$clientMode{$client}{side} = 1;
$clientMode{$client}{hierarchy} = 'track';
$clientMode{$client}{level} = 0;
$clientMode{$client}{all_entry} = '{ALL_SONGS}';
$clientMode{$client}{player_title} =
'{PLUGIN_LAZYSEARCH2_LINE1_BROWSE_TRACKS}';
$clientMode{$client}{player_title_empty} =
'{PLUGIN_LAZYSEARCH2_LINE1_BROWSE_TRACKS_EMPTY}';
$clientMode{$client}{enter_more_prompt} =
'PLUGIN_LAZYSEARCH2_LINE2_ENTER_MORE_TRACKS';
$clientMode{$client}{further_help_prompt} =
'PLUGIN_LAZYSEARCH2_LINE2_BRIEF_HELP';
$clientMode{$client}{min_search_length} =
$myPrefs->get('pref_minlength_track');
$clientMode{$client}{perform_search} = \&performTrackSearch;
$clientMode{$client}{onright} = \&rightIntoTrack;
$clientMode{$client}{search_tracks} = \&searchTracksForTrack;
$clientMode{$client}{mix_type} = 'song';
setSearchBrowseMode( $client, $item, 0 );
}
# Used when the user starts a keyword search from the main category menu.
sub enterKeywordSearch($$) {
my $client = shift;
my $item = shift;
$clientMode{$client}{search_type} = SEARCH_TYPE_KEYWORD;
$clientMode{$client}{side} = 2;
$clientMode{$client}{all_entry} = undef;
$clientMode{$client}{hierarchy} = 'contributor,album,track';
$clientMode{$client}{level} = 0;
$clientMode{$client}{player_title} =
'{PLUGIN_LAZYSEARCH2_LINE1_BROWSE_ARTISTS}';
$clientMode{$client}{player_title_empty} =
'{PLUGIN_LAZYSEARCH2_LINE1_BROWSE_KEYWORDS_EMPTY}';
$clientMode{$client}{enter_more_prompt} =
'PLUGIN_LAZYSEARCH2_LINE2_ENTER_MORE_KEYWORDS';
$clientMode{$client}{further_help_prompt} =
'PLUGIN_LAZYSEARCH2_LINE2_BRIEF_HELP';
$clientMode{$client}{min_search_length} =
$myPrefs->get('pref_minlength_keyword');
$clientMode{$client}{onright} = \&keywordOnRightHandler;
$clientMode{$client}{search_tracks} = undef;
$clientMode{$client}{mix_type} = 'artist';
setSearchBrowseMode( $client, $item, 0 );
}
# Return a result set that contains all tracks for a given artist, for when
# PLAY/INSERT/ADD is pressed on one of those items.
sub searchTracksForArtist($$$$) {
my $client = shift;
my $id = shift;
my $jumpTrackIdRef = shift;
my $jumpIndexRef = shift;
my $condition = undef;
# We restrict the search to include artists related in the roles the
# user wants (set through Squeezebox Server preferences).
my $roles = Slim::Schema->artistOnlyRoles('TRACKARTIST');
if ($roles) {
$condition->{'role'} = { 'in' => $roles };
}
return Slim::Schema->search( 'ContributorTrack',
{ 'me.contributor' => $id } )->search_related(
'track',
$condition,
{
'order_by' =>
'track.album, track.disc, track.tracknum, track.titlesort'
}
)->distinct->all;
}
# Return a result set that contains all tracks for a given album, for when
# PLAY/INSERT/ADD is pressed on one of those items.
sub searchTracksForAlbum($$$$) {
my $client = shift;
my $id = shift;
my $jumpTrackIdRef = shift;
my $jumpIndexRef = shift;
return Slim::Schema->search(
'track',
{ 'album' => $id },
{ 'order_by' => 'me.disc, me.tracknum, me.titlesort' }
)->all;
}
# Return a result set that contains all tracks for a given genre, for when
# PLAY/INSERT/ADD is pressed on one of those items.
sub searchTracksForGenre($$$$) {
my $client = shift;
my $id = shift;
my $jumpTrackIdRef = shift;
my $jumpIndexRef = shift;
return Slim::Schema->search( 'GenreTrack', { 'me.genre' => $id } )
->search_related(
'track', undef,
{
'order_by' =>
'track.album, track.disc, track.tracknum, track.titlesort'
}
)->all;
}
# Return a result set that contain the given track, for when PLAY/INSERT/ADD is
# pressed on one of those items.
sub searchTracksForTrack($$$$) {
my $client = shift;
my $id = shift;
my $jumpTrackIdRef = shift;
my $jumpIndexRef = shift;
$log->debug("Playing/adding/inserting only this one track");
return Slim::Schema->find( 'Track', $id );
}
# Return a result set that contain the given track, for when PLAY/INSERT/ADD is
# pressed on one of those items. This also can return the entire album's items
# as a playlist if the appropriate server/player preference is set.
sub searchTracksForTrackOrAlbum($$$$) {
my $client = shift;
my $id = shift;
my $jumpTrackIdRef = shift;
my $jumpIndexRef = shift;
# Try to look up whether this client wants to play other tracks
# in the same album. This code shamelessly pinched from
# XMLBrowser::playItem.
my $playAlbum = $serverPrefs->client($client)->get('playtrackalbum');
# If player pref for playtrack album is not set, get the old server pref.
if ( !defined $playAlbum ) {
$playAlbum = $serverPrefs->get('playtrackalbum');
}
# If we're supposed to do the whole album then we'll use the album
# track search method instead.
my $track = Slim::Schema->find( 'Track', $id );
if ($playAlbum) {
# Find the album this track lives in.
my $album = $track->album;
# Find all the tracks in this album.
my @albumTracks = searchTracksForAlbum( $client, $album->id, undef, $jumpIndexRef);
# Now loop over this result set and find which entry has the track we started
# with. This becomes the 'jumpIndex' - ie the entry at which the playlist
# begins playing even though it is loaded with all tracks in the album.
my $index = 0;
my $jumpIndex = undef;
my $track = undef;
foreach $track (@albumTracks) {
# If this track is the one we started our search for then it
# is the one that we should jump to in the playlist.
if ($track->id == $id) {
$jumpIndex = $index;
}
$index++;
}
# Return the jump index.
$$jumpIndexRef = $jumpIndex;
$log->debug("Playing/adding/inserting other tracks in album; jumpIndex=" . $jumpIndex);
# The playlist is all of the tracks in the album.
return @albumTracks;
} else {
# Otherwise, it's just the one track you get.
$log->debug("Playing/adding/inserting only this one track");
return $track;
}
}
# Browse into a particular artist.
sub rightIntoArtist($$) {
my $client = shift;
my $item = shift;
# Browse albums for this artist
if ( blessed($item) ) {
# A result set of the items we're going to show.
my $childrenRS = Slim::Schema->search(
'track',
{ 'contributorTracks.contributor' => $item->id },
{
order_by => 'album.titlesort',
distinct => 1,
join => 'contributorTracks'
}
)->search_related('album')->distinct;
# Each element of the listRef will be a hash with keys name and value.
# This is true for artists, albums and tracks.
my @items = ();
while ( my $childItem = $childrenRS->next ) {
push @items, $childItem if ( blessed($childItem) );
}
# Show the browse results and let the user interact with them.
browseLazyResults( $client, $item, \@items, 'album', 'ALBUMS',
$item->name, \&searchTracksForAlbum, \&rightIntoAlbum );
} else {
$log->info("Avoiding entering non-object menu");
}
}
# Browse into a particular album.
sub rightIntoAlbum($$) {
my $client = shift;
my $item = shift;
# Browse tracks for this album.
if ( blessed($item) ) {
# A result set of the items we're going to show.
my $childrenRS = Slim::Schema->search(
'track',
{ 'album' => $item->id },
{ 'order_by' => 'me.disc, me.tracknum, me.titlesort' }
);
# Each element of the listRef will be a hash with keys name and value.
# This is true for artists, albums and tracks.
my @items = ();
while ( my $childItem = $childrenRS->next ) {
push @items, $childItem if ( blessed($childItem) );
}
# Show the browse results and let the user interact with them.
browseLazyResults( $client, $item, \@items, 'track', 'TRACKS',
$item->name, \&searchTracksForTrackOrAlbum, \&rightIntoTrack );
} else {
$log->info("Avoiding entering non-object menu");
}
}
# Browse into a particular genre.
sub rightIntoGenre($$) {
my $client = shift;
my $item = shift;
# Browse tracks for this album.
if ( blessed($item) ) {
# We restrict the search to include artists related in the roles the
# user wants (set through Squeezebox Server preferences).
my $artistOnlyRoles = Slim::Schema->artistOnlyRoles('TRACKARTIST');
if ( !defined($artistOnlyRoles) ) {
my @emptyArtists;
$artistOnlyRoles = \@emptyArtists;
}
my @roles = @{$artistOnlyRoles};
# If the user wants, remove the ALBUMARTIST role (ticket:42)
if ( !$myPrefs->get('pref_keyword_return_albumartists') ) {
my $albumArtistRole =
Slim::Schema::Contributor->typeToRole('ALBUMARTIST');
@roles = grep { !/^$albumArtistRole$/ } @roles;
}
my $condition = undef;
if ( scalar(@roles) > 0 ) {
$condition->{'role'} = { 'in' => \@roles };
}
# Browse artists for this genre.
my $childrenRS =
Slim::Schema->search( 'GenreTrack', { 'me.genre' => $item->id } )
->search_related('track')
->search_related( 'contributorTracks', $condition )
->search_related('contributor')->distinct;
# Each element of the listRef will be a hash with keys name and value.
# This is true for artists, albums and tracks.
my @items = ();
while ( my $childItem = $childrenRS->next ) {
push @items, $childItem if ( blessed($childItem) );
}
# Show the browse results and let the user interact with them.
browseLazyResults( $client, $item, \@items, 'genre', 'GENRES',
$item->name, \&searchTracksForArtist, \&rightIntoArtist );
} else {
$log->info("Avoiding entering non-object menu");
}
}
# Browse into a particular track.
sub rightIntoTrack($$) {
my $client = shift;
my $item = shift;
$log->debug( "Pushing right into TRACK " . $item->id );
# Push into the trackinfo mode for this one track.
my $track = Slim::Schema->rs('Track')->find( $item->id );
Slim::Buttons::Common::pushModeLeft( $client, 'trackinfo',
{ 'track' => $track } );
}
# Generalised lazy search result browser. This supports all the browse result types
# and pushing into a hierarchy of search results.
sub browseLazyResults($$$$$) {
my $client = shift;
my $item = shift;
my $itemArray = shift;
my $mixType = shift;
my $browseType = shift;
my $headerName = shift;
my $trackSearchMethod = shift;
my $pushRightMethod = shift;
# The current unique text to make the mode unique, and other browse constants.
my $searchText = $clientMode{$client}{search_text};
my $forceSearch = $clientMode{$client}{search_forced};
my $browseResultType = $browseType;
# Use INPUT.Choice to display the results for this browse-into mode.
my %params = (
# The header (first line) to display whilst in this mode.
header => $headerName . ' {count}',
# A reference to the list of items to display.
listRef => $itemArray,
# The function to extract the title of each item.
name => \&lazyGetText,
# A unique name for this mode that won't actually get displayed
# anywhere.
modeName => 'LAZYBROWSE_RESULTS_'
. $browseResultType
. "_MODE:$searchText",
# An anonymous function that is called every time the user presses the
# RIGHT button.
onRight => $pushRightMethod,
onLeft => sub {
$log->debug("LEFT");
},
# A handler that manages play/add/insert (differentiated by the
# last parameter).
onPlay => sub {
my ( $client, $item, $addMode ) = @_;
# Start playing the item selected (in the correct mode - play, add
# or insert).
lazyPlayOrAddResults( $client, $item, $addMode,
$trackSearchMethod );
},
# What overlays are shown on lines 1 and 2.
overlayRef => \&lazyOverlay,
# Our mix type that will be used if the user tries to
# create a MusicIP mix here. The type depends on the
# current position in the browsing hierarchy and so is
# determined above.
mixType => $mixType,
);
# Use our INPUT.Choice-derived mode to show the menu and let it do all the
# hard work of displaying the list, moving it up and down, etc, etc.
$log->debug( 'Pushing right into '
. $browseResultType
. ' from higher item '
. $item->id );
Slim::Buttons::Common::pushModeLeft( $client, LAZYBROWSE_RESULTS_MODE,
\%params );
}
# Function called when leaving our top-level lazy search menu mode. We use this
# to track whether or not the user is within the lazy search mode for a
# particular player.
sub leaveMode {
my $client = shift;
# Clear the search results to save a little memory.
$clientMode{$client}{search_items} = ();
}
# There are no functions in this mode as the main mode (the top-level menu) is
# all handled by the INPUT.Choice mode.
sub getFunctions {
return {};
}
# 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. Adds our hooks
# for database encoding and makes our customised mode that lets us grab and
# process extra buttons.
sub initPlugin() {
my $class = shift;
return if $initialised; # don't need to do it twice
$log->info("Initialising $VERSION");
$class->SUPER::initPlugin(@_);
# Initialise settings.
Plugins::LazySearch2::Settings->new($class);
# Remember we're now initialised. This prevents multiple-initialisation,
# which may otherwise cause trouble with duplicate hooks or modes.
$initialised = 1;
# Detect whether we're on Windows (we support the wake-on-LAN keep-awake
# tickle if we are)
my $os = Slim::Utils::OSDetect::OS();
if ( $os eq 'win' ) {
eval 'use Win32::API';
$log->debug('LazySearch2 is running on Windows');
$onWindows = 1;
}
# Make sure the preferences are set to something sensible before we call
# on them later.
checkDefaults();
# Subscribe so that we are notified when the database has been rescanned;
# we use this so that we can apply lazification.
Slim::Control::Request::subscribe(
\&Plugins::LazySearch2::Plugin::scanDoneCallback,
[ ['rescan'], ['done'] ] );
# Top-level menu mode. We register a custom INPUT.Choice mode so that
# we can detect when we're in it (for SEARCH button toggle).
$log->debug("Making custom INPUT.Choice-derived modes");
Slim::Buttons::Common::addMode( LAZYSEARCH_TOP_MODE, undef, \&setMode );
Slim::Buttons::Common::addMode(
LAZYSEARCH_CATEGORY_MENU_MODE,
Slim::Buttons::Input::Choice::getFunctions(),
\&Slim::Buttons::Input::Choice::setMode
);
# Out input map for the new categories menu mode, based on the default map
# contents for INPUT.Choice.
my %categoryInputMap = (
'arrow_left' => 'exit_left',
'arrow_right' => 'exit_right',
'play' => 'play',
'add' => 'add',
'stop' => 'passback',
'pause' => 'passback',
);
for my $buttonPressMode (qw{repeat hold hold_release single double}) {
$categoryInputMap{ 'play.' . $buttonPressMode } = 'dead';
$categoryInputMap{ 'add.' . $buttonPressMode } = 'dead';
$categoryInputMap{ 'search.' . $buttonPressMode } = 'dead';
$categoryInputMap{ 'stop.' . $buttonPressMode } = 'passback';
$categoryInputMap{ 'pause.' . $buttonPressMode } = 'passback';
}
Slim::Hardware::IR::addModeDefaultMapping( LAZYSEARCH_CATEGORY_MENU_MODE,
\%categoryInputMap );
# Make a customised version of the INPUT.Choice mode so that we can grab
# the numbers (INPUT.Choice will normally use these to scroll through
# 'numberScroll').
my %chFunctions = %{ Slim::Buttons::Input::Choice::getFunctions() };
$chFunctions{'numberScroll'} = \&lazyKeyHandler;
$chFunctions{'playSingle'} = \&onPlayHandler;
$chFunctions{'playHold'} = \&onCreateMixHandler;
$chFunctions{'addSingle'} = \&onAddHandler;
$chFunctions{'addHold'} = \&onInsertHandler;
$chFunctions{'leftSingle'} = \&onDelCharHandler;
$chFunctions{'leftHold'} = \&onDelAllHandler;
$chFunctions{'forceSearch'} = \&lazyForceSearch;
$chFunctions{'zeroButton'} = \&zeroButtonHandler;
$chFunctions{'keywordSep'} = \&keywordSepHandler;
Slim::Buttons::Common::addMode( LAZYBROWSE_MODE, \%chFunctions,
\&Slim::Buttons::Input::Choice::setMode );
# Our input map for the new lazy browse mode, based on the default map
# contents for INPUT.Choice.
my %lazyInputMap = (
'arrow_left' => 'leftSingle',
'arrow_left.hold' => 'leftHold',
'arrow_right' => 'exit_right',
'play.single' => 'playSingle',
'play.hold' => 'playHold',
'play' => 'dead',
'play.repeat' => 'dead',
'play.hold_release' => 'dead',
'play.double' => 'dead',
'pause.single' => 'pause',
'pause.hold' => 'stop',
'add.single' => 'addSingle',
'add.hold' => 'addHold',
'search' => 'forceSearch',
'0.single' => 'zeroButton',
'0.hold' => 'keywordSep',
'0' => 'dead',
'0.repeat' => 'dead',
'0.hold_release' => 'dead',
'0.double' => 'dead',
);
for my $buttonPressMode (qw{repeat hold hold_release single double}) {
$lazyInputMap{ 'search.' . $buttonPressMode } = 'dead';
}
Slim::Hardware::IR::addModeDefaultMapping( LAZYBROWSE_MODE,
\%lazyInputMap );
# The mode that is used to show keyword results once the user has entered
# one of the returned categories.
my %chFunctions2 = %{ Slim::Buttons::Input::Choice::getFunctions() };
$chFunctions2{'playSingle'} = \&onPlayHandler;
$chFunctions2{'playHold'} = \&onCreateMixHandler;
$chFunctions2{'addSingle'} = \&onAddHandler;
$chFunctions2{'addHold'} = \&onInsertHandler;
$chFunctions2{'forceSearch'} = \&lazyForceSearch;
Slim::Buttons::Common::addMode( LAZYBROWSE_KEYWORD_MODE, \%chFunctions2,
\&Slim::Buttons::Input::Choice::setMode );
# Our input map for the new keyword browse mode, based on the default map
# contents for INPUT.Choice.
my %keywordInputMap = (
'arrow_left' => 'exit_left',
'arrow_right' => 'exit_right',
'play.single' => 'playSingle',
'play.hold' => 'playHold',
'play' => 'dead',
'play.repeat' => 'dead',
'play.hold_release' => 'dead',
'play.double' => 'dead',
'pause.single' => 'pause',
'pause.hold' => 'stop',
'add.single' => 'addSingle',
'add.hold' => 'addHold',
'search' => 'forceSearch',
);
for my $buttonPressMode (qw{repeat hold hold_release single double}) {
$keywordInputMap{ 'search.' . $buttonPressMode } = 'dead';
}
Slim::Hardware::IR::addModeDefaultMapping( LAZYBROWSE_KEYWORD_MODE,
\%keywordInputMap );
# The mode that is used to browse search results. This is quite similar to the keyword results browse mode.
my %chFunctions3 = %{ Slim::Buttons::Input::Choice::getFunctions() };
$chFunctions3{'playSingle'} = \&onPlayHandler;
$chFunctions3{'playHold'} = \&onCreateMixHandler;
$chFunctions3{'addSingle'} = \&onAddHandler;
$chFunctions3{'addHold'} = \&onInsertHandler;
Slim::Buttons::Common::addMode( LAZYBROWSE_RESULTS_MODE, \%chFunctions3,
\&Slim::Buttons::Input::Choice::setMode );