summaryrefslogtreecommitdiffstats
path: root/krfb/srvloc/kserviceregistry.cpp
blob: 1a14f9b0796bb6d2d6e31af09ede57cdd8d700e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
 *  Interface to register SLP services.
 *  Copyright (C) 2002 Tim Jansen <[email protected]>
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public License
 *  along with this library; see the file COPYING.LIB.  If not, write to
 *  the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 *  Boston, MA 02111-1307, USA.
 */
/**
 * TODO: see below..
 */

#include "config.h"
#include "kserviceregistry.h"
#include <kdebug.h>


#ifdef HAVE_SLP
#include <slp.h>

class KServiceRegistryPrivate {
public:
	KServiceRegistryPrivate(const QString &lang) :
		m_opened(false),
		m_lang(lang) {
	}
	bool ensureOpen();
	
        bool m_opened;
	QString m_lang;

        SLPHandle m_handle;
	friend void KServiceRegistryRegReport(SLPHandle slp, 
					      SLPError errcode, 
					      void* cookie);
	bool m_cbSuccess;
};

void KServiceRegistryRegReport(SLPHandle, 
			       SLPError errcode, 
			       void* cookie) {
	KServiceRegistryPrivate *s = (KServiceRegistryPrivate*) cookie;
	s->m_cbSuccess = (errcode == SLP_OK);
	if (errcode < 0)
		kdDebug() << "KServiceRegistry: error in callback:" << errcode <<endl;
}


KServiceRegistry::KServiceRegistry(const QString &lang) {
	d = new KServiceRegistryPrivate(lang);
}

KServiceRegistry::~KServiceRegistry() {
	if (d->m_opened)
		SLPClose(d->m_handle);
	delete d;
}

bool KServiceRegistryPrivate::ensureOpen() {
	SLPError e;

	if (m_opened)
		return true;

	e = SLPOpen(m_lang.latin1(), SLP_FALSE, &m_handle);
	if (e != SLP_OK) {
		kdDebug() << "KServiceRegistry: error while opening:" << e <<endl;
		return false;
	}
	m_opened = true;
	return true;
}

bool KServiceRegistry::available() {
	return d->ensureOpen();
}

bool KServiceRegistry::registerService(const QString &serviceURL, 
				       QString attributes, 
				       unsigned short lifetime) {
	if (!d->ensureOpen())
		return false;

	d->m_cbSuccess = true;
	SLPError e = SLPReg(d->m_handle,
			    serviceURL.latin1(),
			    lifetime > 0 ? lifetime : SLP_LIFETIME_MAXIMUM,
			    0,
			    attributes.isNull() ? "" : attributes.latin1(),
			    SLP_TRUE,
			    KServiceRegistryRegReport,
			    d);
	if (e != SLP_OK) {
		kdDebug() << "KServiceRegistry: error in registerService:" << e <<endl;
		return false;
	}
	return d->m_cbSuccess;
}

bool KServiceRegistry::registerService(const QString &serviceURL, 
				       QMap<QString,QString> attributes, 
				       unsigned short lifetime) {
	if (!d->ensureOpen())
		return false;
// TODO: encode strings in map?
	QString s;
	QMap<QString,QString>::iterator it = attributes.begin();
	while (it != attributes.end()) {
		if (!s.isEmpty())
			s += ",";
		s += QString("(%1=%2)").arg(it.key()).arg(it.data());
		it++;
	}
	return registerService(serviceURL, s, lifetime);
}

void KServiceRegistry::unregisterService(const QString &serviceURL) {
	if (!d->m_opened)
		return;
	SLPDereg(d->m_handle, serviceURL.latin1(), 
		 KServiceRegistryRegReport,
		 d);		 
}

QString KServiceRegistry::encodeAttributeValue(const QString &value) {
	char *n;
	if (SLPEscape(value.latin1(), &n, SLP_TRUE) == SLP_OK) {
		QString r(n);
		SLPFree(n);
		return r;
	}
	return QString::null;
}

#else

KServiceRegistry::KServiceRegistry(const QString &lang) :
	d(0) {
}

KServiceRegistry::~KServiceRegistry() {
}

bool KServiceRegistry::available() {
	return false;
}

bool KServiceRegistry::registerService(const QString &, QString, unsigned short ) {
	return false;
}

bool KServiceRegistry::registerService(const QString &, QMap<QString,QString>, unsigned short) {
	return false;
}

void KServiceRegistry::unregisterService(const QString &) {
}

QString KServiceRegistry::encodeAttributeValue(const QString &value) {
	return value;
}

#endif

QString KServiceRegistry::createCommaList(const QStringList &values) {
	return values.join(",");
}