1 /* Settings.cpp */
2 
3 /* Copyright (C) 2011-2020 Michael Lugmair (Lucio Carreras)
4  *
5  * This file is part of sayonara player
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11 
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
ExpandDecider(long cPtr, boolean cMemoryOwn)15  * GNU General Public License for more details.
16 
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #include <iostream>
22 
23 #include "Utils/Library/LibraryNamespaces.h"
24 #include "Utils/Settings/Settings.h"
25 #include "Utils/Settings/SettingRegistry.h"
26 #include "Utils/typedefs.h"
27 
28 #include "Utils/Crypt.h"
29 #include "Utils/Utils.h"
30 #include "Utils/Language/Language.h"
31 #include "Utils/Language/LanguageUtils.h"
32 
33 #include <array>
34 #include <iterator>
35 
36 struct Settings::Private
37 {
38 	QString			version;
39 	std::array<AbstrSetting*, static_cast<int>(SettingKey::Num_Setting_Keys)> settings;
40 	bool			initialized;
41 
42 	Private()
43 	{
44 		std::fill(settings.begin(), settings.end(), nullptr);
45 		initialized = false;
46 	}
47 };
swigTakeOwnership()48 
49 Settings::Settings()
50 {
51 	m = Pimpl::make<Private>();
52 }
ExpandDecider()53 
54 Settings::~Settings ()
55 {
56 	for(size_t i=0; i<m->settings.size(); i++)
57 	{
58 		delete m->settings[i];
59 		m->settings[i] = nullptr;
60 	}
61 }
release()62 
63 AbstrSetting* Settings::setting(SettingKey key) const
64 {
65 	return m->settings[size_t(key)];
66 }
67 
68 const SettingArray& Settings::settings()
69 {
70 	return m->settings;
71 }
72 
73 void Settings::registerSetting(AbstrSetting* s)
74 {
75 	SettingKey key  = s->getKey();
76 	uint i = static_cast<uint>(key);
77 	m->settings[i] = s;
78 }
79 
80 
81 bool Settings::checkSettings()
82 {
83 	if(m->initialized){
84 		return true;
85 	}
86 
87 	SettingRegistry::init();
88 	bool has_empty = std::any_of(m->settings.begin(), m->settings.end(), [](AbstrSetting* s){
89 		return (s==nullptr);
90 	});
91 
92 	m->initialized = (!has_empty);
93 	return m->initialized;
94 }
95 
96 
97 void Settings::applyFixes()
98 {
99 	int settingsRevision = this->get<Set::Settings_Revision>();
100 	if(settingsRevision < 1)
101 	{
102 		// Create Crypt keys
103 		QByteArray priv_key = ::Util::randomString(32).toLocal8Bit();
104 		this->set<Set::Player_PrivId>(priv_key);
105 
106 		QByteArray pub_key = ::Util::randomString(32).toLocal8Bit();
107 		this->set<Set::Player_PublicId>(pub_key);
108 
109 		// Crypt Last FM password
110 		StringPair lfm_pw = this->get<Set::LFM_Login>();
111 		this->set<Set::LFM_Username>(lfm_pw.first);
112 		this->set<Set::LFM_Password>(Util::Crypt::encrypt(lfm_pw.second));
113 		this->set<Set::LFM_Login>(StringPair("", ""));
114 
115 		// Crypt Proxy Password
116 		QString proxy_pw = this->get<Set::Proxy_Password>();
117 		this->set<Set::Proxy_Password>(Util::Crypt::encrypt(proxy_pw));
118 
119 		this->set<Set::Settings_Revision>(1);
120 	}
121 
122 	if(settingsRevision < 2)
123 	{
124 		QString language = this->get<Set::Player_Language>();
125 		QString fourLetter = Util::Language::convertOldLanguage(language);
126 		this->set<Set::Player_Language>(fourLetter);
127 
128 		this->set<Set::Settings_Revision>(2);
129 	}
130 
131 	if(settingsRevision < 3)
132 	{
133 		bool b = this->get<Set::Lib_ShowAlbumCovers>();
134 		if(b) {
135 			this->set<Set::Lib_ViewType>(::Library::ViewType::CoverView);
136 		}
137 
138 		else {
139 			this->set<Set::Lib_ViewType>(::Library::ViewType::Standard);
140 		}
141 
142 		this->set<Set::Settings_Revision>(3);
143 	}
144 
145 	if(settingsRevision < 4)
146 	{
147 		QString path = this->get<Set::Cover_TemplatePath>();
148 		path.replace("jpg", "png", Qt::CaseInsensitive);
149 		this->set<Set::Cover_TemplatePath>(path);
150 
151 		this->set<Set::Settings_Revision>(4);
152 	}
153 
154 	if(settingsRevision < 5)
155 	{
156 		QString path = this->get<Set::Cover_TemplatePath>();
157 		path.replace(QRegExp("\\.[a-zA-Z]{3}$"), "");
158 		this->set<Set::Cover_TemplatePath>(path);
159 
160 		this->set<Set::Settings_Revision>(5);
161 	}
162 
163 	if(get<Set::Player_PrivId>().isEmpty())
164 	{
165 		QByteArray id = ::Util::randomString(32).toLocal8Bit();
166 		this->set<Set::Player_PrivId>(id);
167 	}
168 
169 	if(get<Set::Player_PublicId>().isEmpty())
170 	{
171 		QByteArray id = ::Util::randomString(32).toLocal8Bit();
172 		this->set<Set::Player_PublicId>(id);
173 	}
174 }
175 
176