2009-09-02 04:21:41 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2009 by Rob Scheepmaker <r.scheepmaker@student.utwente.nl>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "accessmanager.h"
|
2009-09-02 04:27:16 +02:00
|
|
|
#include "private/accessmanager_p.h"
|
2009-09-02 04:21:41 +02:00
|
|
|
|
|
|
|
#include "service.h"
|
|
|
|
#include "serviceaccessjob.h"
|
|
|
|
|
|
|
|
#include <QtCore/QMap>
|
|
|
|
#include <QtCore/QTimer>
|
|
|
|
|
|
|
|
#include <QtJolie/Message>
|
|
|
|
|
2009-09-02 08:17:35 +02:00
|
|
|
#include <kdebug.h>
|
|
|
|
#include <kurl.h>
|
2009-09-02 04:27:16 +02:00
|
|
|
#include <kglobal.h>
|
|
|
|
#include <dnssd/servicebrowser.h>
|
|
|
|
#include <dnssd/remoteservice.h>
|
|
|
|
#include <ktemporaryfile.h>
|
|
|
|
#include <k3resolver.h>
|
|
|
|
#include <kstandarddirs.h>
|
|
|
|
#include <qhostinfo.h>
|
2009-09-02 04:21:41 +02:00
|
|
|
|
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
|
|
|
class AccessManagerSingleton
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AccessManager self;
|
|
|
|
};
|
|
|
|
|
2009-09-02 04:27:16 +02:00
|
|
|
//FIXME: AccessManagerSignleton could be removed to remove a friend.
|
|
|
|
//see kdelibs/kutils/kidletime/xsyncbasedpoller.cpp
|
2009-09-02 04:21:41 +02:00
|
|
|
K_GLOBAL_STATIC(AccessManagerSingleton, privateAccessManagerSelf)
|
|
|
|
|
|
|
|
AccessManager *AccessManager::self()
|
|
|
|
{
|
|
|
|
return &privateAccessManagerSelf->self;
|
|
|
|
}
|
|
|
|
|
|
|
|
AccessManager::AccessManager()
|
2009-09-02 04:27:16 +02:00
|
|
|
: QObject(),
|
|
|
|
d(new AccessManagerPrivate(this))
|
2009-09-02 04:21:41 +02:00
|
|
|
{
|
2009-09-02 04:27:16 +02:00
|
|
|
KGlobal::dirs()->addResourceType("trustedkeys", "config", "trustedkeys/");
|
2009-09-02 04:21:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
AccessManager::~AccessManager()
|
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
2009-09-02 04:27:16 +02:00
|
|
|
AccessAppletJob *AccessManager::accessRemoteApplet(const KUrl &location) const
|
2009-09-02 04:21:41 +02:00
|
|
|
{
|
2009-09-02 04:27:16 +02:00
|
|
|
kDebug() << "unresolved location = " << location.prettyUrl();
|
|
|
|
|
|
|
|
KUrl resolvedLocation;
|
|
|
|
if (location.protocol() == "zeroconf") {
|
|
|
|
if (d->zeroconfServices.contains(location.host())) {
|
|
|
|
resolvedLocation = d->services[location.host()].remoteLocation();
|
|
|
|
} else {
|
|
|
|
kDebug() << "not in the map";
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
resolvedLocation = location;
|
|
|
|
}
|
|
|
|
|
|
|
|
AccessAppletJob *job = new AccessAppletJob(resolvedLocation);
|
|
|
|
connect(job, SIGNAL(finished(KJob*)), this, SLOT(slotJobFinished(KJob*)));
|
|
|
|
QTimer::singleShot(0, job, SLOT(slotStart()));
|
|
|
|
return job;
|
2009-09-02 04:21:41 +02:00
|
|
|
}
|
|
|
|
|
2009-09-02 04:27:16 +02:00
|
|
|
QList<PackageMetadata> AccessManager::remoteApplets() const
|
2009-09-02 04:21:41 +02:00
|
|
|
{
|
2009-09-02 04:27:16 +02:00
|
|
|
return d->services.values();
|
2009-09-02 04:21:41 +02:00
|
|
|
}
|
|
|
|
|
2009-09-02 04:27:16 +02:00
|
|
|
AccessManagerPrivate::AccessManagerPrivate(AccessManager *manager)
|
|
|
|
: q(manager),
|
|
|
|
browser(new DNSSD::ServiceBrowser("_plasma._tcp"))
|
2009-09-02 04:21:41 +02:00
|
|
|
{
|
2009-09-02 04:27:16 +02:00
|
|
|
q->connect(browser, SIGNAL(serviceAdded(DNSSD::RemoteService::Ptr)),
|
|
|
|
q, SLOT(slotAddService(DNSSD::RemoteService::Ptr)));
|
|
|
|
q->connect(browser, SIGNAL(serviceRemoved(DNSSD::RemoteService::Ptr)),
|
|
|
|
q, SLOT(slotRemoveService(DNSSD::RemoteService::Ptr)));
|
|
|
|
browser->startBrowse();
|
|
|
|
}
|
|
|
|
|
|
|
|
AccessManagerPrivate::~AccessManagerPrivate()
|
|
|
|
{
|
|
|
|
delete browser;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AccessManagerPrivate::slotJobFinished(KJob *job)
|
|
|
|
{
|
|
|
|
emit q->finished(static_cast<AccessAppletJob*>(job));
|
|
|
|
}
|
|
|
|
|
|
|
|
void AccessManagerPrivate::slotAddService(DNSSD::RemoteService::Ptr service)
|
|
|
|
{
|
|
|
|
kDebug();
|
|
|
|
if (!service->resolve()) {
|
|
|
|
kDebug() << "can't be resolved";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!services.contains(service->serviceName())) {
|
|
|
|
PackageMetadata metadata;
|
|
|
|
kDebug() << "textdata = " << service->textData();
|
|
|
|
kDebug() << "hostname: " << service->hostName();
|
|
|
|
QHostAddress address = DNSSD::ServiceBrowser::resolveHostName(service->hostName());
|
|
|
|
QString ip = address.toString();
|
|
|
|
kDebug() << "result for resolve = " << ip;
|
|
|
|
|
|
|
|
KUrl url(QString("plasma://%1:%2/%3").arg(ip)
|
|
|
|
.arg(service->port())
|
|
|
|
.arg(service->serviceName()));
|
|
|
|
|
|
|
|
if (!service->textData().isEmpty()) {
|
|
|
|
kDebug() << "service has got textdata";
|
|
|
|
QMap<QString, QByteArray> textData = service->textData();
|
|
|
|
metadata.setName(textData["name"]);
|
|
|
|
metadata.setDescription(textData["description"]);
|
|
|
|
metadata.setRemoteLocation(url.prettyUrl());
|
|
|
|
} else {
|
|
|
|
kDebug() << "no textdata?";
|
|
|
|
metadata.setName(service->serviceName());
|
|
|
|
metadata.setRemoteLocation(url.prettyUrl());
|
|
|
|
}
|
|
|
|
|
|
|
|
kDebug() << "location = " << metadata.remoteLocation();
|
|
|
|
kDebug() << "name = " << metadata.name();
|
|
|
|
kDebug() << "description = " << metadata.name();
|
|
|
|
|
|
|
|
services[service->serviceName()] = metadata;
|
|
|
|
zeroconfServices[service->serviceName()] = service;
|
|
|
|
emit q->remoteAppletAnnounced(metadata);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AccessManagerPrivate::slotRemoveService(DNSSD::RemoteService::Ptr service)
|
|
|
|
{
|
|
|
|
kDebug();
|
|
|
|
emit q->remoteAppletUnannounced(services[service->serviceName()]);
|
|
|
|
services.remove(service->serviceName());
|
|
|
|
zeroconfServices.remove(service->serviceName());
|
2009-09-02 04:21:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} // Plasma namespace
|
|
|
|
|
|
|
|
#include "accessmanager.moc"
|