~ubuntu-branches/ubuntu/maverick/kdeutils/maverick-proposed

« back to all changes in this revision

Viewing changes to kremotecontrol/libkremotecontrol/remotelist.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2010-05-28 09:49:30 UTC
  • mfrom: (1.2.44 upstream)
  • Revision ID: james.westby@ubuntu.com-20100528094930-jzynf0obv1n2v13a
Tags: 4:4.4.80-0ubuntu1~ppa1
New upstream beta release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 "remotelist.h"
 
21
#include "profileaction.h"
 
22
 
 
23
#include <kglobal.h>
 
24
#include <kconfig.h>
 
25
 
 
26
#include <QtCore/QList>
 
27
 
 
28
 
 
29
RemoteList::RemoteList() {
 
30
}
 
31
 
 
32
bool RemoteList::contains(const QString& remoteName) const {
 
33
    for(QVector<Remote*>::const_iterator i = constBegin(); i != constEnd(); ++i){
 
34
        if((*i)->name() == remoteName){
 
35
            return true;
 
36
        }
 
37
    }
 
38
    return false;
 
39
}
 
40
 
 
41
// Save everything to config File in the format [Remote][modeIndex][actionIndex]
 
42
void RemoteList::saveToConfig(const QString& configName) {
 
43
    KConfig config(configName);
 
44
    // Clear out all old remotes settings. We will sync the whole tree to disk now
 
45
    config.deleteGroup("Remotes");
 
46
    KConfigGroup remotesGroup(&config, "Remotes");
 
47
    for(QVector<Remote*>::const_iterator remoteIterator = constBegin(); remoteIterator != constEnd(); ++remoteIterator){
 
48
        KConfigGroup remoteGroup(&remotesGroup, (*remoteIterator)->name());
 
49
        // Save Remote properties here
 
50
        remoteGroup.writeEntry("DefaultMode", (*remoteIterator)->defaultMode()->name());
 
51
        remoteGroup.writeEntry("ModeChangeMode", (*remoteIterator)->modeChangeMode() == Remote::Group ? "Group" : "Cycle");
 
52
        remoteGroup.writeEntry("NextModeButton", (*remoteIterator)->nextModeButton());
 
53
        remoteGroup.writeEntry("PreviousModeButton", (*remoteIterator)->previousModeButton());
 
54
        
 
55
        int modeIndex = 0;
 
56
        foreach(const Mode *mode, (*remoteIterator)->allModes()){
 
57
            KConfigGroup modeGroup(&remoteGroup, QString::number(modeIndex++));
 
58
            // Save Mode properties here
 
59
            modeGroup.writeEntry("Name", mode->name());
 
60
            modeGroup.writeEntry("IconName", mode->iconName());
 
61
            modeGroup.writeEntry("Button", mode->button());
 
62
            
 
63
            int actionIndex = 0;
 
64
            foreach(Action *action, mode->actions()){
 
65
                KConfigGroup actionGroup(&modeGroup, QString::number(actionIndex++));
 
66
                // Actions need to save themselves...
 
67
                action->saveToConfig(actionGroup);
 
68
            }
 
69
        }
 
70
    }
 
71
}
 
72
 
 
73
void RemoteList::loadFromConfig(const QString& configName) {
 
74
    clear(); //Drop old entries
 
75
    KConfig config(configName, KConfig::NoGlobals);
 
76
    KConfigGroup remotesGroup(&config, "Remotes");
 
77
 
 
78
    foreach(const QString &remoteGroupName, remotesGroup.groupList()){
 
79
        Remote *remote = new Remote(remoteGroupName);
 
80
        KConfigGroup remoteGroup(&remotesGroup, remoteGroupName);
 
81
        QStringList modeGroupList = remoteGroup.groupList();
 
82
        modeGroupList.sort();
 
83
        foreach(const QString &modeIndex, modeGroupList){
 
84
            KConfigGroup modeGroup(&remoteGroup, modeIndex);
 
85
            Mode *mode;
 
86
            QString modeName = modeGroup.readEntry("Name");
 
87
            if(modeName == "Master") { // A Remote always has a Master Mode... Adding a second one will not work
 
88
                mode = remote->masterMode();
 
89
                mode->setIconName(modeGroup.readEntry("IconName"));
 
90
            } else {
 
91
                mode = new Mode(modeName, modeGroup.readEntry("IconName"));
 
92
            }
 
93
            QStringList actionGroupList = modeGroup.groupList();
 
94
            actionGroupList.sort();
 
95
            foreach(const QString &actionId, actionGroupList){
 
96
                KConfigGroup actionGroup(&modeGroup, actionId);
 
97
                // Read Action properties here
 
98
                Action *action = 0;
 
99
                Action::ActionType actionType = (Action::ActionType) actionGroup.readEntry("Type", 0);
 
100
                switch(actionType){
 
101
                    case Action::DBusAction:
 
102
                        action = new DBusAction();
 
103
                        break;
 
104
                    case Action::ProfileAction:
 
105
                        action = new ProfileAction();
 
106
                        break;
 
107
                }
 
108
                if(!action){
 
109
                    continue;
 
110
                }
 
111
                action->loadFromConfig(actionGroup);
 
112
                
 
113
                mode->addAction(action);
 
114
            }
 
115
            // Read Mode properties here
 
116
            mode->setIconName(modeGroup.readEntry("IconName", "infrared-remote"));
 
117
            mode->setButton(modeGroup.readEntry("Button"));
 
118
            
 
119
            remote->addMode(mode);
 
120
        }
 
121
        // Read Remote properties here
 
122
        remote->setDefaultMode(remoteGroup.readEntry("DefaultMode"));
 
123
        remote->setModeChangeMode(remoteGroup.readEntry("ModeChangeMode", "Group") == "Group" ? Remote::Group : Remote::Cycle);
 
124
        remote->setNextModeButton(remoteGroup.readEntry("NextModeButton"));
 
125
        remote->setPreviousModeButton(remoteGroup.readEntry("PreviousModeButton"));
 
126
        
 
127
        append(remote);
 
128
    }
 
129
 
 
130
}
 
131
Remote* RemoteList::remote ( const QString& remoteName ){
 
132
   for(QVector<Remote*>::const_iterator i = constBegin(); i != constEnd(); ++i){
 
133
        if((*i)->name() == remoteName){
 
134
            return *i;
 
135
        }
 
136
    }
 
137
    return 0;
 
138
}
 
139