generate moc
[kdenetwork.git] / krdc / hostpreferences.cpp
blob215cef9fc40afb8c89ffc17f3ee5eaf60d6c8f04
1 /*
2 Copyright (C) 2003 Tim Jansen <tim@tjansen.de>
3 Copyright (C) 2004 Nadeem Hasan <nhasan@kde.org>
4 */
5 /***************************************************************************
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 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 ***************************************************************************/
14 #include "hostpreferences.h"
15 #include "vnc/vnchostpref.h"
16 #include "rdp/rdphostpref.h"
18 #include <kapplication.h>
19 #include <kconfig.h>
20 #include <kstaticdeleter.h>
22 #include <QRegExp>
23 #include <QMap>
24 #include <kglobal.h>
26 HostPreferences *HostPreferences::m_instance = 0;
27 static KStaticDeleter<HostPreferences> sd;
29 HostPreferences *HostPreferences::instance()
31 if ( m_instance == 0 )
32 sd.setObject( m_instance, new HostPreferences() );
34 return m_instance;
37 HostPref::HostPref(KConfig *conf, const QString &host, const QString &type) :
38 m_host(host),
39 m_type(type),
40 m_config(conf) {
43 HostPref::~HostPref() {
44 m_config->sync();
47 QString HostPref::host() const {
48 return m_host;
51 QString HostPref::type() const {
52 return m_type;
55 QString HostPref::prefix() const {
56 return prefix(m_host, m_type);
59 QString HostPref::prefix(const QString &host, const QString &type) {
60 return QString("PerHost-%1-%2-").arg(type).arg(host);
64 HostPreferences::HostPreferences() {
65 m_config = KGlobal::config();
68 HostPreferences::~HostPreferences() {
69 if ( m_instance == this )
70 sd.setObject( m_instance, 0, false );
73 HostPrefPtr HostPreferences::getHostPref(const QString &host, const QString &type) {
74 m_config->setGroup("PerHostSettings");
75 if (!m_config->readEntry(HostPref::prefix(host, type)+"exists",false))
76 return 0;
78 if (type == VncHostPref::VncType) {
79 HostPrefPtr hp = new VncHostPref(m_config, host, type);
80 hp->load();
81 return hp;
83 else if(type == RdpHostPref::RdpType) {
84 HostPrefPtr hp = new RdpHostPref(m_config, host, type);
85 hp->load();
86 return hp;
88 Q_ASSERT(true);
89 return 0;
92 HostPrefPtr HostPreferences::createHostPref(const QString &host, const QString &type) {
93 HostPrefPtr hp = getHostPref(host, type);
94 if (hp)
95 return hp;
97 if(type == VncHostPref::VncType)
99 hp = new VncHostPref(m_config, host, type);
101 else if(type == RdpHostPref::RdpType)
103 hp = new RdpHostPref(m_config, host, type);
105 hp->setDefaults();
106 return hp;
109 HostPrefPtr HostPreferences::vncDefaults()
111 HostPrefPtr hp = new VncHostPref( m_config );
112 hp->load();
114 return hp;
117 HostPrefPtr HostPreferences::rdpDefaults()
119 HostPrefPtr hp = new RdpHostPref( m_config );
120 hp->load();
122 return hp;
125 HostPrefPtrList HostPreferences::getAllHostPrefs() {
126 HostPrefPtrList r;
127 QMap<QString, QString> map = m_config->entryMap("PerHostSettings");
128 QStringList keys = map.keys();
129 QStringList::iterator it = keys.begin();
130 while (it != keys.end()) {
131 QString key = *it;
132 if (key.endsWith("-exists")) {
133 QRegExp re("PerHost-([^-]+)-(.*)-exists");
134 if (re.exactMatch(key)) {
135 HostPrefPtr hp = getHostPref(re.cap(2), re.cap(1));
136 if (hp)
137 r += hp;
141 it++;
143 return r;
146 void HostPreferences::removeHostPref(HostPref *hostPref) {
147 hostPref->remove();
150 void HostPreferences::setShowBrowsingPanel( bool b )
152 m_config->setGroup( QString::null );
153 m_config->writeEntry( "browsingPanel", b );
156 void HostPreferences::setServerCompletions( const QStringList &list )
158 m_config->setGroup( QString::null );
159 m_config->writeEntry( "serverCompletions", list );
162 void HostPreferences::setServerHistory( const QStringList &list )
164 m_config->setGroup( QString::null );
165 m_config->writeEntry( "serverHistory", list );
168 bool HostPreferences::showBrowsingPanel()
170 m_config->setGroup( QString::null );
171 return m_config->readEntry( "browsingPanel",false );
174 QStringList HostPreferences::serverCompletions()
176 m_config->setGroup( QString::null );
177 return m_config->readEntry( "serverCompletions", QStringList() );
180 QStringList HostPreferences::serverHistory()
182 m_config->setGroup( QString::null );
183 return m_config->readEntry( "serverHistory", QStringList() );
186 void HostPreferences::sync() {
187 m_config->sync();