-
Notifications
You must be signed in to change notification settings - Fork 24
/
Settings.pm
178 lines (134 loc) · 6.1 KB
/
Settings.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
package Plugins::Spotty::Settings;
use strict;
use base qw(Slim::Web::Settings);
use HTTP::Status qw(RC_MOVED_TEMPORARILY);
use Slim::Utils::Prefs;
use Slim::Utils::Strings qw(string);
use Plugins::Spotty::Plugin;
use Plugins::Spotty::AccountHelper;
use Plugins::Spotty::Settings::Auth;
use Plugins::Spotty::Settings::Player;
use Plugins::Spotty::Settings::PlaylistFolders;
use constant SETTINGS_URL => 'plugins/Spotty/settings/basic.html';
my $prefs = preferences('plugin.spotty');
sub new {
my $class = shift;
Plugins::Spotty::Settings::Auth->new();
Plugins::Spotty::Settings::Player->new();
Plugins::Spotty::Settings::PlaylistFolders->new();
if (!Slim::Networking::Async::HTTP->hasSSL()) {
Slim::Web::Pages->addPageFunction(SETTINGS_URL, $class);
}
return $class->SUPER::new(@_);
}
sub name {
return Slim::Web::HTTP::CSRF->protectName('PLUGIN_SPOTTY');
}
sub page {
return Slim::Web::HTTP::CSRF->protectURI(
Slim::Networking::Async::HTTP->hasSSL()
? SETTINGS_URL
: 'plugins/Spotty/settings/noSSL.html'
);
}
sub prefs {
my @prefs = qw(myAlbumsOnly cleanupTags bitrate iconCode accountSwitcherMenu helper optimizePreBuffer sortAlbumsAlphabetically sortArtistsAlphabetically sortPlaylisttracksByAddition);
push @prefs, 'disableDiscovery', 'checkDaemonConnected' if Plugins::Spotty::Plugin->canDiscovery();
push @prefs, 'disableAsyncTokenRefresh' if Plugins::Spotty::Helper->getCapability('save-token');
push @prefs, 'sortSongsAlphabetically' if !Plugins::Spotty::Plugin->hasDefaultIcon();
push @prefs, 'forceFallbackAP' if !Plugins::Spotty::Helper->getCapability('no-ap-port');
return ($prefs, @prefs);
}
sub handler {
my ($class, $client, $paramRef, $callback, $httpClient, $response) = @_;
my ($helperPath, $helperVersion) = Plugins::Spotty::Helper->get();
# rename temporary authentication cache folder (if existing)
Plugins::Spotty::Settings::Auth->cleanup();
# don't even continue if we're missing the helper application
$paramRef->{helperMissing} = _getHelperMissingMessage() unless $helperPath;
if ( my ($deleteAccount) = map { /delete_(.*)/; $1 } grep /^delete_/, keys %$paramRef ) {
Plugins::Spotty::AccountHelper->deleteCacheFolder($deleteAccount);
}
if ($paramRef->{saveSettings}) {
$paramRef->{pref_iconCode} ||= Plugins::Spotty::Plugin->_initIcon();
foreach my $client ( Slim::Player::Client::clients() ) {
$prefs->client($client)->set('enableSpotifyConnect', $paramRef->{'connect_' . $client->id} ? 1 : 0);
}
if ($paramRef->{clearPlaylistFolderCache}) {
Plugins::Spotty::PlaylistFolders->purgeCache(1);
}
if ($paramRef->{clearSearchHistory}) {
$prefs->set('spotify_recent_search', []);
}
my $dontImportAccounts = $prefs->get('dontImportAccounts') || {};
foreach my $prefName (keys %$paramRef) {
if ($prefName =~ /^pref_dontimport_(.*)/) {
$dontImportAccounts->{$1} = $paramRef->{$prefName};
}
}
$prefs->set('dontImportAccounts', $dontImportAccounts);
# make sure value is not undefined, or it might get re-initialized
$paramRef->{pref_cleanupTags} ||= 0;
}
if ( !$paramRef->{helperMissing} && ($paramRef->{addAccount} || !Plugins::Spotty::AccountHelper->hasCredentials()) ) {
$response->code(RC_MOVED_TEMPORARILY);
$response->header('Location' => 'authentication.html?ajaxUpdate=' . $paramRef->{ajaxUpdate});
return Slim::Web::HTTP::filltemplatefile($class->page, $paramRef);
}
# make sure our authentication helper isn't running
Plugins::Spotty::Settings::Auth->shutdownHelper();
$paramRef->{credentials} = Plugins::Spotty::AccountHelper->getSortedCredentialTupels();
$paramRef->{displayNames} = { map {
my ($id) = each %$_;
$id => Plugins::Spotty::AccountHelper->getDisplayName($id);
} @{$paramRef->{credentials}} };
$paramRef->{canDiscovery} = Plugins::Spotty::Plugin->canDiscovery();
$paramRef->{error429} = Plugins::Spotty::API->hasError429();
$paramRef->{isLowCaloriesPi} = Plugins::Spotty::Helper->isLowCaloriesPi();
$paramRef->{players} = [ sort {
lc($a->{name}) cmp lc($b->{name})
} map {
{
name => $_->name,
id => $_->id,
enabled => $prefs->client($_)->get('enableSpotifyConnect')
}
} Slim::Player::Client::clients() ];
return $class->SUPER::handler($client, $paramRef);
}
sub _getHelperMissingMessage {
my $osDetails = Slim::Utils::OSDetect::details();
# Windows should just work - except if the MSVC 2015 runtime was missing
return string('PLUGIN_SPOTTY_MISSING_HELPER_WINDOWS') if main::ISWINDOWS;
my $helperMissingMessage = string('PLUGIN_SPOTTY_MISSING_HELPER');
my $knownIncompatible = $osDetails->{osName} =~ /Mac.?OS .*10\.(?:1|2|3|4|5|6)\./i
|| ($osDetails->{osArch} && $osDetails->{osArch} =~ /\b(?:powerpc)\b/i);
if ($knownIncompatible) {
$helperMissingMessage = string('PLUGIN_SPOTTY_SYSTEM_INCOMPATIBLE');
}
return $helperMissingMessage . sprintf('<br><br>%s %s / %s<br><br>%s<br>%s<br>%s',
string('INFORMATION_OPERATINGSYSTEM') . string('COLON'),
$osDetails->{'osName'},
($osDetails->{'osArch'} ? $osDetails->{'osArch'} : 'unknown'),
string('PLUGIN_SPOTTY_INFORMATION_BINDIRS') . string('COLON'),
eval{ join("<br>", Slim::Utils::Misc::getBinPaths()) } || string('PLUGIN_SPOTTY_PLEASE_UPDATE'),
Slim::Utils::OSDetect::isLinux() ? `ldd --version 2>&1 | head -n1` : ''
);
}
sub beforeRender {
my ($class, $paramRef) = @_;
my $helpers = Plugins::Spotty::Helper->getAll();
if ($helpers && scalar keys %$helpers > 1) {
$paramRef->{helpers} = $helpers;
}
my ($helperPath, $helperVersion) = Plugins::Spotty::Helper->get();
$paramRef->{helperPath} = $helperPath;
$paramRef->{helperVersion} = $helperVersion ? "v$helperVersion" : string('PLUGIN_SPOTTY_HELPER_ERROR');
$paramRef->{canConnect} = Plugins::Spotty::Connect->canSpotifyConnect();
$paramRef->{canAsyncTokenRefresh} = Plugins::Spotty::API::Token::CAN_ASYNC_GET_TOKEN || Plugins::Spotty::Helper->getCapability('save-token');
$paramRef->{canApPort} = !Plugins::Spotty::Helper->getCapability('no-ap-port');
$paramRef->{hasDefaultIcon} = Plugins::Spotty::Plugin->hasDefaultIcon();
$paramRef->{dontImportAccounts} = $prefs->get('dontImportAccounts') || {};
$paramRef->{warning} && $paramRef->{warning} =~ s/iconCode/Client ID/i;
}
1;