• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeutils API Reference
  • KDE Home
  • Contact Us
 

kremotecontrol

  • sources
  • kde-4.12
  • kdeutils
  • kremotecontrol
  • libkremotecontrol
remote.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) <2010> Michael Zanetti <michael_zanetti@gmx.net>
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License along
15  with this program; if not, write to the Free Software Foundation, Inc.,
16  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 
18 */
19 
20 #include "remote.h"
21 
22 #include <KLocale>
23 #include <kdebug.h>
24 
25 class ModeChangeHandler
26 {
27  public:
28  ModeChangeHandler(Remote *remote) {
29  m_remote = remote;
30  }
31  virtual ~ModeChangeHandler(){}
32 
33  virtual bool handleModeButton(const QString &button) = 0;
34  virtual Remote::ModeChangeMode type() const = 0;
35  virtual QStringList availableModeSwitchButtons() const = 0;
36  virtual void addMode(Mode *mode) const = 0;
37  virtual void handleModeButtonAssignment(const QString &button){
38  Q_UNUSED(button);
39  };
40  virtual void handleModeButtonAssignments(){};
41 
42  protected:
43  Remote *m_remote;
44 };
45 
46 
47 class GroupModeChangeHandler : public ModeChangeHandler
48 {
49 
50  public:
51  GroupModeChangeHandler(Remote* remote) : ModeChangeHandler(remote) {}
52 
53 
69  bool handleModeButton(const QString& button) {
70 
71  int size = m_remote->m_modeList.size();
72  int currentModeHasButton = m_remote->currentMode()->button() == button;
73 
74  int index= currentModeHasButton ? m_remote->m_modeList.indexOf(m_remote->currentMode()) +1 : 0;
75 
76  for(; index < size ; ++index) {
77  if (m_remote->m_modeList.at(index)->button() == button) {
78 
79  m_remote->setCurrentMode(m_remote->m_modeList.at(index));
80  return true;
81  }
82  }
83 
84  if (currentModeHasButton) {
85  //Current mode has the same button as the pressed, but no mode after the current mode which has the same button was found.
86  // Switch to master mode.
87  m_remote->setCurrentMode(m_remote->masterMode());
88  return true;
89  }
90 
91  //mode with this button is not available, this should usually not happen.In this case the mode is not changed
92  kDebug() << "Mode with button " << button << " not available. Mode is not changed.";
93  return false;
94  }
95 
96  Remote::ModeChangeMode type() const {
97  return Remote::Group;
98  }
99 
100  QStringList availableModeSwitchButtons() const {
101  // GroupModeHandler allows to re-use all buttons
102  QStringList retList;
103  foreach(const RemoteControlButton &button, RemoteControl(m_remote->name()).buttons()) {
104  retList.append(button.name());
105  }
106  return retList;
107  }
108 
109  void addMode(Mode* mode) const {
110  m_remote->m_modeList.append(mode);
111  };
112 };
113 
114 class CycleModeChangeHandler : public ModeChangeHandler
115 {
116 
117  public:
118  CycleModeChangeHandler(Remote* remote) : ModeChangeHandler(remote) {}
119 
120  bool handleModeButton(const QString& button) {
121  int index = -1;
122  if(!m_remote->previousModeButton().isEmpty() && m_remote->previousModeButton() == button){
123  index = m_remote->m_modeList.indexOf(m_remote->currentMode());
124  index = index > 0 ? index-1 : m_remote->m_modeList.size()-1;
125  } else if (! m_remote->nextModeButton().isEmpty() && m_remote->nextModeButton() == button){
126  index = m_remote->m_modeList.indexOf(m_remote->currentMode());
127  index = index < m_remote->m_modeList.size() -1 ? index+1 : 0;
128  } else if((m_remote->currentMode()->button().isEmpty() || m_remote->currentMode()->button() != button )){
129  index = indexOfModeButton(button);
130  }
131  if(index > -1){
132  m_remote->setCurrentMode(m_remote->allModes().at(index));
133  return true;
134  }
135  return false;
136  }
137 
138  Remote::ModeChangeMode type() const {
139  return Remote::Cycle;
140  }
141 
142  QStringList availableModeSwitchButtons() const {
143  QStringList retList;
144  foreach(const RemoteControlButton &button, RemoteControl(m_remote->name()).buttons()) {
145  retList << button.name();
146  }
147  retList.removeAll(m_remote->nextModeButton());
148  retList.removeAll(m_remote->previousModeButton());
149  foreach(const Mode *mode, m_remote->m_modeList){
150  retList.removeAll(mode->button());
151  }
152  return retList;
153  }
154 
155  void addMode(Mode* mode) const {
156  foreach(Mode *m, m_remote->m_modeList){
157  if(! mode->button().isEmpty() && m->button() == mode->button()){
158  kDebug() << "mode "<< m->name() << " has already assigned the button " << mode->button();
159  return;
160  }
161  }
162  m_remote->m_modeList.append(mode);
163  };
164 
165  virtual void handleModeButtonAssignment(const QString &button) {
166  if(button.isEmpty()){
167  return;
168  }
169  foreach(Mode* mode, m_remote->allModes()){
170  if(mode->button() == button){
171  mode->setButton(QString());
172  }
173  }
174  };
175 
176  int indexOfModeButton(const QString &button){
177  if(button.isEmpty()){
178  return -1;
179  }
180  for (int index=0; index < m_remote->m_modeList.size() ; ++index) {
181  kDebug()<< "index " << index << " size " << m_remote->m_modeList.size();
182  if (m_remote->m_modeList.at(index)->button() == button) {
183  return index;
184  }
185  }
186  return -1;
187  }
188 
189  virtual void handleModeButtonAssignments(){
190  handleModeButtonAssignment(m_remote->nextModeButton());
191  handleModeButtonAssignment(m_remote->previousModeButton());
192  int size = m_remote->m_modeList.size();
193  for(int index=0; index < size; ++index){
194  QString buttonToValidate = m_remote->m_modeList.at(index)->button();
195  if(! buttonToValidate.isEmpty()){
196  for(int counter=index+1; counter< size; ++counter){
197  Mode *mode = m_remote->m_modeList.at(counter);
198  if(!mode->button().isEmpty() && mode->button() == buttonToValidate){
199  mode->setButton(QString());
200  }
201  }
202  }
203  }
204  };
205 
206 };
207 
208 Remote::Remote() {
209  // Always create the Master Mode and set it default
210  Mode *masterMode = new Mode(QLatin1String( "Master" ));
211  addMode(masterMode);
212  setDefaultMode(masterMode);
213  setCurrentMode(masterMode);
214  m_modechangeHandler = new GroupModeChangeHandler(this);
215 }
216 
217 Remote::Remote(const QString &remote, ModeChangeMode changeMode) {
218  m_remoteName = remote;
219  m_modechangeHandler = 0;
220 
221  // Always create the Master Mode and set it default
222  bool hasMaster = false;
223  setModeChangeMode(changeMode);
224  foreach(Mode *mode, m_modeList) {
225  if (mode->name() == QLatin1String( "Master" )) {
226  hasMaster = true;
227  setCurrentMode(mode);
228  }
229  }
230 
231  if (!hasMaster) {
232  Mode *masterMode = new Mode(QLatin1String( "Master" ));
233  addMode(masterMode);
234  setDefaultMode(masterMode);
235  setCurrentMode(masterMode);
236  }
237 }
238 
239 Remote::~Remote() {
240  while (!m_modeList.isEmpty()) {
241  Mode *mode = m_modeList.first();
242  m_modeList.remove(0);
243  delete mode;
244  }
245 }
246 
247 QString Remote::name() const {
248  return m_remoteName;
249 }
250 
251 void Remote::moveModeUp(Mode* mode) {
252  int oldPos = m_modeList.indexOf(mode);
253 
254  if (oldPos > 1) {
255  m_modeList.remove(oldPos);
256  m_modeList.insert(oldPos - 1, mode);
257  }
258 }
259 
260 void Remote::moveModeDown(Mode* mode) {
261  int oldPos = m_modeList.indexOf(mode);
262 
263  if (oldPos < (m_modeList.count() - 1)) {
264  m_modeList.remove(oldPos);
265  m_modeList.insert(oldPos + 1, mode);
266  }
267 }
268 
269 QVector<Mode*> Remote::allModes() const {
270  return m_modeList;
271 }
272 
273 void Remote::addMode(Mode* mode) {
274  // Don't add a second master mode
275  if(mode != masterMode() && mode->name() != QLatin1String( "Master" )){
276  m_modechangeHandler->addMode(mode);
277  }
278 }
279 
280 void Remote::removeMode(Mode *mode) {
281  if (mode->name() == QLatin1String( "Master" )) {
282  kDebug() << "cannot delete the Master mode";
283  return;
284  }
285 
286  if (m_defaultMode == mode) {
287  // Deleting the Default Mode... Setting Master Mode to default
288  foreach(Mode *tmp, m_modeList) {
289  if (tmp->name() == QLatin1String( "Master" )) {
290  m_defaultMode = tmp;
291  break;
292  }
293  }
294  }
295 
296  m_modeList.remove(m_modeList.indexOf(mode));
297  delete mode;
298 }
299 
300 Mode* Remote::masterMode() const {
301  foreach(Mode *mode, m_modeList) {
302 // kDebug() << "checking Mode" << mode->name();
303  if (mode->name() == QLatin1String( "Master" )) {
304  return mode;
305  }
306  }
307  kDebug() << "Master mode not found";
308  // Huh??? No Master Mode? Should never happen as removeMode() doesn't delete the Master mode
309  // and all ctors create a Master Mode...
310  return 0;
311 }
312 
313 Mode *Remote::defaultMode() const {
314  return m_defaultMode;
315 }
316 
317 void Remote::setDefaultMode(Mode *mode) {
318  if (!m_modeList.contains(mode)) {
319  m_modeList.append(mode);
320  }
321 
322  m_defaultMode = mode;
323 }
324 
325 void Remote::setDefaultMode(const QString &modeName) {
326  foreach(Mode *mode, m_modeList) {
327  if (mode->name() == modeName) {
328  setDefaultMode(mode);
329  return;
330  }
331  }
332 }
333 
334 Mode* Remote::modeByName(const QString& modeName) const {
335  foreach(Mode *mode, m_modeList) {
336  if (mode->name() == modeName) {
337  return mode;
338  }
339  }
340  return 0;
341 }
342 
343 Mode* Remote::currentMode() const {
344  if (m_currentMode != 0) {
345  return m_currentMode;
346  }
347 
348  return m_defaultMode;
349 }
350 
351 void Remote::setCurrentMode(Mode* mode) {
352  m_currentMode = mode;
353 }
354 
355 bool Remote::isAvailable() const {
356  return RemoteControl::allRemoteNames().contains(m_remoteName);
357 }
358 
359 bool Remote::nextMode(const QString& button) {
360  return m_modechangeHandler->handleModeButton(button);
361 }
362 
363 Remote::ModeChangeMode Remote::modeChangeMode() const {
364  return m_modechangeHandler->type();
365 }
366 
367 void Remote::setModeChangeMode(Remote::ModeChangeMode modeChangeMode) {
368  delete m_modechangeHandler;
369  if(modeChangeMode == Remote::Group){
370  m_modechangeHandler = new GroupModeChangeHandler(this);
371  } else {
372  m_modechangeHandler = new CycleModeChangeHandler(this);
373  }
374  m_modechangeHandler->handleModeButtonAssignments();
375 }
376 
377 QStringList Remote::availableModeSwitchButtons(const Mode *mode) const {
378  QStringList buttonList = m_modechangeHandler->availableModeSwitchButtons();
379  if(mode && !mode->button().isEmpty() && !buttonList.contains(mode->button())){
380  buttonList.append(mode->button());
381  }
382  return buttonList;
383 }
384 
385 QStringList Remote::availableModeCycleButtons() const {
386  QStringList buttonList = m_modechangeHandler->availableModeSwitchButtons();
387  if(!nextModeButton().isEmpty() && !buttonList.contains(nextModeButton())){
388  buttonList.append(nextModeButton());
389  }
390  if(!previousModeButton().isEmpty() && !buttonList.contains(previousModeButton())){
391  buttonList.append(previousModeButton());
392  }
393  return buttonList;
394 }
395 
396 QString Remote::nextModeButton() const {
397  return m_nextModeButton;
398 }
399 
400 void Remote::setNextModeButton(const QString& button) {
401  m_nextModeButton = button;
402  m_modechangeHandler->handleModeButtonAssignment(button);
403 }
404 
405 QString Remote::previousModeButton() const {
406  return m_previousModeButton;
407 }
408 
409 void Remote::setPreviousModeButton(const QString& button) {
410  m_previousModeButton = button;
411  m_modechangeHandler->handleModeButtonAssignment(button);
412 }
413 
Mode::setButton
void setButton(const QString &button)
Definition: mode.cpp:56
Remote::setNextModeButton
void setNextModeButton(const QString &button)
Definition: remote.cpp:400
Remote::name
QString name() const
Definition: remote.cpp:247
RemoteControl::allRemoteNames
static QStringList allRemoteNames()
Get the Names of the available remotes in the system.
Definition: remotecontrol.cpp:37
Remote::ModeChangeMode
ModeChangeMode
Definition: remote.h:39
Remote::removeMode
void removeMode(Mode *mode)
Remove the given Mode from this Remote and delete it.
Definition: remote.cpp:280
Remote::modeByName
Mode * modeByName(const QString &name) const
Definition: remote.cpp:334
Mode
Definition: mode.h:31
QVector
Remote::allModes
QVector< Mode * > allModes() const
Definition: remote.cpp:269
Remote::CycleModeChangeHandler
friend class CycleModeChangeHandler
Definition: remote.h:36
Remote::Cycle
Definition: remote.h:39
Mode::button
QString button() const
Definition: mode.cpp:44
Remote::modeChangeMode
ModeChangeMode modeChangeMode() const
Definition: remote.cpp:363
RemoteControl
Definition: remotecontrol.h:35
Remote::Remote
Remote()
Definition: remote.cpp:208
RemoteControlButton::name
QString name() const
Retrieves the name of the Button.
Definition: remotecontrolbutton.cpp:315
Remote::setPreviousModeButton
void setPreviousModeButton(const QString &button)
Definition: remote.cpp:409
Remote::currentMode
Mode * currentMode() const
Definition: remote.cpp:343
Remote::GroupModeChangeHandler
friend class GroupModeChangeHandler
Definition: remote.h:35
Remote::previousModeButton
QString previousModeButton() const
Definition: remote.cpp:405
Remote::nextModeButton
QString nextModeButton() const
Definition: remote.cpp:396
Remote::addMode
void addMode(Mode *mode)
Add the given Mode to this Remote.
Definition: remote.cpp:273
Remote::moveModeUp
void moveModeUp(Mode *mode)
Definition: remote.cpp:251
remote.h
Remote::~Remote
~Remote()
Destrys the Remote and all of its Modes.
Definition: remote.cpp:239
Remote::masterMode
Mode * masterMode() const
Definition: remote.cpp:300
Remote::setDefaultMode
void setDefaultMode(Mode *mode)
Definition: remote.cpp:317
Remote::defaultMode
Mode * defaultMode() const
Definition: remote.cpp:313
Remote::setModeChangeMode
void setModeChangeMode(ModeChangeMode modeChangeMode)
Definition: remote.cpp:367
Remote::availableModeSwitchButtons
QStringList availableModeSwitchButtons(const Mode *mode=0) const
Get buttons available for this mode (Free buttons + the current button for this mode) ...
Definition: remote.cpp:377
Mode::name
QString name() const
Definition: mode.cpp:36
Remote::Group
Definition: remote.h:39
RemoteControlButton
Definition: remotecontrolbutton.h:30
Remote::isAvailable
bool isAvailable() const
Definition: remote.cpp:355
Remote::setCurrentMode
void setCurrentMode(Mode *mode)
Definition: remote.cpp:351
Remote
Definition: remote.h:32
Remote::nextMode
bool nextMode(const QString &button)
Definition: remote.cpp:359
Remote::availableModeCycleButtons
QStringList availableModeCycleButtons() const
Get buttons available for switching to the next or previous mode (Free buttons + the current nextMode...
Definition: remote.cpp:385
Remote::moveModeDown
void moveModeDown(Mode *mode)
Definition: remote.cpp:260
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:07:43 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kremotecontrol

Skip menu "kremotecontrol"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdeutils API Reference

Skip menu "kdeutils API Reference"
  • ark
  • filelight
  • kcalc
  • kcharselect
  • kdf
  • kfloppy
  • kgpg
  • kremotecontrol
  • ktimer
  • kwallet
  • superkaramba
  • sweeper

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal