Sayonara Player
PlaylistMode.h
1 /* PlaylistMode.h */
2 
3 /* Copyright (C) 2011 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 #ifndef PLAYLISTMODE_H_
22 #define PLAYLISTMODE_H_
23 
24 #include <iostream>
25 #include <QList>
26 #include <QVariant>
27 #include <QStringList>
28 
29 using namespace std;
30 
31 // this class has to be inline because of the settings registry
32 class PlaylistMode {
33 
34 public:
35  enum PlaylistModeState
36  {
37  Off = 0,
38  On = 1,
39  Disabled = 2 // this has to be because of consistence
40  };
41 
42 private:
43  PlaylistModeState _rep1;
44  PlaylistModeState _repAll;
45  PlaylistModeState _append;
46  PlaylistModeState _shuffle;
47  PlaylistModeState _dynamic;
48  PlaylistModeState _gapless;
49 
50  PlaylistModeState set_state(bool active, bool enabled){
51  quint8 ret = PlaylistMode::Off;
52  if(active){
53  ret |= PlaylistMode::On;
54  }
55 
56  if(!enabled){
57  ret |= PlaylistMode::Disabled;
58  }
59 
60  return (PlaylistModeState) ret;
61  }
62 
63 public:
64  PlaylistModeState rep1() const { return _rep1; }
65  PlaylistModeState repAll() const { return _repAll; }
66  PlaylistModeState append() const { return _append; }
67  PlaylistModeState shuffle() const { return _shuffle; }
68  PlaylistModeState dynamic() const { return _dynamic; }
69  PlaylistModeState gapless() const { return _gapless; }
70 
71  void setRep1(PlaylistModeState state){ _rep1 = state; }
72  void setRepAll(PlaylistModeState state){ _repAll = state; }
73  void setAppend(PlaylistModeState state){ _append = state; }
74  void setShuffle(PlaylistModeState state){ _shuffle = state; }
75  void setDynamic(PlaylistModeState state){ _dynamic = state; }
76  void setGapless(PlaylistModeState state){ _gapless = state; }
77 
78  void setRep1(bool on, bool enabled=true){ _rep1 = set_state(on, enabled); }
79  void setRepAll(bool on, bool enabled=true){ _repAll = set_state(on, enabled); }
80  void setAppend(bool on, bool enabled=true){ _append = set_state(on, enabled); }
81  void setShuffle(bool on, bool enabled=true){ _shuffle = set_state(on, enabled); }
82  void setDynamic(bool on, bool enabled=true){ _dynamic = set_state(on, enabled); }
83  void setGapless(bool on, bool enabled=true){ _gapless = set_state(on, enabled); }
84 
85  static bool isActive(PlaylistModeState pl)
86  {
87  quint8 ipl = (quint8) pl;
88  return ( ipl & PlaylistMode::On );
89  }
90 
91  static bool isEnabled(PlaylistModeState pl){
92  quint8 ipl = (quint8) pl;
93  return (( ipl & PlaylistMode::Disabled ) == 0);
94  }
95 
96  static bool isActiveAndEnabled(PlaylistModeState pl)
97  {
98  return (isEnabled(pl) && isActive(pl));
99  }
100 
101  PlaylistMode(){
102  _rep1 = PlaylistMode::Off;
103  _repAll = PlaylistMode::On;
104  _append = PlaylistMode::Off;
105 
106  _shuffle = PlaylistMode::Off;
107  _gapless = PlaylistMode::Off;
108  _dynamic = PlaylistMode::Off;
109  }
110 
111  void print(){
112  cout << "rep1 = " << (int) _rep1 << ", "
113  << "repAll = " << (int) _repAll << ", "
114  << "append = " << (int) _append <<", "
115  << "dynamic = " << (int) _dynamic << ","
116  << "gapless = " << (int) _gapless << endl;
117 
118  }
119 
120  QString toString() const {
121  QString str;
122  str += QString::number((int) _append) + QString(",");
123  str += QString::number((int) _repAll) + QString(",");
124  str += QString::number((int) _rep1) + QString(",");
125  str += "0,";
126  str += QString::number((int) _shuffle) + QString(",");
127  str += QString::number((int) _dynamic) + QString(",");
128  str += QString::number((int) _gapless);
129 
130  return str;
131  }
132 
133  static PlaylistMode fromString(QString str){
134 
135  PlaylistMode plm;
136  QStringList list = str.split(',');
137 
138  if(list.size() < 6) return plm;
139 
140  plm.setAppend((PlaylistModeState) list[0].toInt());
141  plm.setRepAll((PlaylistModeState) list[1].toInt());
142  plm.setRep1((PlaylistModeState) list[2].toInt());
143  plm.setShuffle((PlaylistModeState) list[4].toInt());
144  plm.setDynamic((PlaylistModeState) list[5].toInt());
145 
146  if(list.size() > 6){
147  plm.setGapless((PlaylistModeState) list[6].toInt());
148  }
149 
150  return plm;
151  }
152 
153  bool operator==(const PlaylistMode& pm) const {
154 
155  if(pm.append() != _append) return false;
156  if(pm.repAll() != _repAll) return false;
157  if(pm.rep1() != _rep1) return false;
158  if(pm.shuffle() != _shuffle) return false;
159  if(pm.dynamic() != _dynamic) return false;
160  if(pm.gapless() != _gapless) return false;
161 
162  return true;
163  }
164 };
165 
166 #endif
Definition: PlaylistMode.h:32