1 /* PlaylistMode.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
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 "PlaylistMode.h"
22 #include "Utils/Logger/Logger.h"
23 #include <QStringList>
24 
25 using Playlist::Mode;
26 
27 struct Mode::Private
28 {
29 	Mode::State	rep1;
30 	Mode::State	repAll;
31 	Mode::State	append;
32 	Mode::State	shuffle;
33 	Mode::State	dynamic;
34 	Mode::State	gapless;
35 
PrivateMode::Private36 	Private() :
37 		rep1(Mode::Off),
38 		repAll(Mode::On),
39 		append(Mode::Off),
40 		shuffle(Mode::Off),
41 		dynamic(Mode::Off),
42 		gapless(Mode::Off)
43 	{}
44 
PrivateMode::Private45 	Private(const Private& other) :
46 		CASSIGN(rep1),
47 		CASSIGN(repAll),
48 		CASSIGN(append),
49 		CASSIGN(shuffle),
50 		CASSIGN(dynamic),
51 		CASSIGN(gapless)
52 	{}
53 
operator =Mode::Private54 	Private& operator=(const Private& other)
55 	{
56 		ASSIGN(rep1);
57 		ASSIGN(repAll);
58 		ASSIGN(append);
59 		ASSIGN(shuffle);
60 		ASSIGN(dynamic);
61 		ASSIGN(gapless);
62 
63 		return *this;
64 	}
65 
66 
67 	static
set_stateMode::Private68 	Mode::State set_state(bool active, bool enabled)
69 	{
70 		uint8_t ret = Mode::Off;
71 		if(active){
72 			ret |= Mode::On;
73 		}
74 
75 		if(!enabled){
76 			ret |= Mode::Disabled;
77 		}
78 
79 		return (Mode::State) ret;
80 	}
81 
82 };
83 
84 
Mode()85 Mode::Mode()
86 {
87 	m = Pimpl::make<Private>();
88 }
89 
90 Mode::~Mode() = default;
91 
Mode(const Playlist::Mode & other)92 Mode::Mode(const Playlist::Mode& other)
93 {
94 	m = Pimpl::make<Private>(*(other.m));
95 }
96 
operator =(const Playlist::Mode & other)97 Mode& Mode::operator=(const Playlist::Mode& other)
98 {
99 	*m = *(other.m);
100 	return *this;
101 }
102 
rep1() const103 Mode::State Mode::rep1() const { return m->rep1; }
repAll() const104 Mode::State Mode::repAll() const { return m->repAll; }
append() const105 Mode::State Mode::append() const { return m->append; }
shuffle() const106 Mode::State Mode::shuffle() const { return m->shuffle; }
dynamic() const107 Mode::State Mode::dynamic() const { return m->dynamic; }
gapless() const108 Mode::State Mode::gapless() const { return m->gapless; }
109 
setRep1(Mode::State state)110 void Mode::setRep1(Mode::State state){ m->rep1 = state; }
setRepAll(Mode::State state)111 void Mode::setRepAll(Mode::State state){ m->repAll = state; }
setAppend(Mode::State state)112 void Mode::setAppend(Mode::State state){ m->append = state; }
setShuffle(Mode::State state)113 void Mode::setShuffle(Mode::State state){ m->shuffle = state; }
setDynamic(Mode::State state)114 void Mode::setDynamic(Mode::State state){ m->dynamic = state; }
setGapless(Mode::State state)115 void Mode::setGapless(Mode::State state){ m->gapless = state; }
116 
setRep1(bool on,bool enabled)117 void Mode::setRep1(bool on, bool enabled){ m->rep1 = Private::set_state(on, enabled); }
setRepAll(bool on,bool enabled)118 void Mode::setRepAll(bool on, bool enabled){ m->repAll = Private::set_state(on, enabled); }
setAppend(bool on,bool enabled)119 void Mode::setAppend(bool on, bool enabled){ m->append = Private::set_state(on, enabled); }
setShuffle(bool on,bool enabled)120 void Mode::setShuffle(bool on, bool enabled){ m->shuffle = Private::set_state(on, enabled); }
setDynamic(bool on,bool enabled)121 void Mode::setDynamic(bool on, bool enabled){ m->dynamic = Private::set_state(on, enabled); }
setGapless(bool on,bool enabled)122 void Mode::setGapless(bool on, bool enabled){ m->gapless = Private::set_state(on, enabled); }
123 
isActive(Mode::State pl)124 bool Mode::isActive(Mode::State pl)
125 {
126 	uint8_t ipl = (uint8_t) pl;
127 	return ( ipl & Mode::On );
128 }
129 
isEnabled(Mode::State pl)130 bool Mode::isEnabled(Mode::State pl){
131 	uint8_t ipl = (uint8_t) pl;
132 	return (( ipl & Mode::Disabled ) == 0);
133 }
134 
isActiveAndEnabled(Mode::State pl)135 bool Mode::isActiveAndEnabled(Mode::State pl)
136 {
137 	return (isEnabled(pl) && isActive(pl));
138 }
139 
print()140 void Mode::print()
141 {
142 	spLog(Log::Debug, this) << "rep1 = "   << (int) m->rep1 << ", "
143 		<< "repAll = "  << (int) m->repAll << ", "
144 		<< "append = "  << (int) m->append <<", "
145 		<< "dynamic = " << (int) m->dynamic << ","
146 		<< "gapless = " << (int) m->gapless;
147 }
148 
toString() const149 QString Mode::toString() const
150 {
151 	QString str;
152 	str += QString::number((int) m->append)  + QString(",");
153 	str += QString::number((int) m->repAll)  + QString(",");
154 	str += QString::number((int) m->rep1)    + QString(",");
155 	str += "0,";
156 	str += QString::number((int) m->shuffle) + QString(",");
157 	str += QString::number((int) m->dynamic) + QString(",");
158 	str += QString::number((int) m->gapless);
159 
160 	return str;
161 }
162 
163 #define state_cast(x) static_cast<Mode::State>( x .toInt())
loadFromString(const QString & str)164 bool Mode::loadFromString(const QString& str)
165 {
166 	QStringList list = str.split(',');
167 
168 	if(list.size() < 6) {
169 		return false;
170 	}
171 
172 	this->setAppend( state_cast(list[0]) );
173 	this->setRepAll( state_cast(list[1]) );
174 	this->setRep1(   state_cast(list[2]) );
175 	this->setShuffle(state_cast(list[4]) );
176 	this->setDynamic(state_cast(list[5]) );
177 
178 	if(list.size() > 6){
179 		this->setGapless(state_cast(list[6]));
180 	}
181 
182 	return true;
183 }
184 
operator ==(const Mode & pm) const185 bool Mode::operator==(const Mode& pm) const
186 {
187 	if(pm.append() != m->append) return false;
188 	if(pm.repAll() != m->repAll) return false;
189 	if(pm.rep1() != m->rep1) return false;
190 	if(pm.shuffle() != m->shuffle) return false;
191 	if(pm.dynamic() != m->dynamic) return false;
192 	if(pm.gapless() != m->gapless) return false;
193 
194 	return true;
195 }
196