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
|
|
|
|
2009-09-03 01:49:58 +02:00
|
|
|
#include "authorizationmanager.h"
|
2011-04-29 15:18:35 +02:00
|
|
|
#include "authorizationmanager_p.h"
|
2009-09-02 04:21:41 +02:00
|
|
|
#include "service.h"
|
|
|
|
#include "serviceaccessjob.h"
|
|
|
|
|
2009-09-09 17:37:03 +02:00
|
|
|
#include "config-plasma.h"
|
2009-09-09 15:40:16 +02:00
|
|
|
|
2009-09-02 04:21:41 +02:00
|
|
|
#include <QtCore/QMap>
|
|
|
|
#include <QtCore/QTimer>
|
|
|
|
|
2009-09-03 01:49:46 +02:00
|
|
|
#include <dnssd/remoteservice.h>
|
|
|
|
#include <dnssd/servicebrowser.h>
|
2009-09-02 08:17:35 +02:00
|
|
|
#include <kdebug.h>
|
2009-09-02 04:27:16 +02:00
|
|
|
#include <kglobal.h>
|
|
|
|
#include <kstandarddirs.h>
|
2009-09-03 01:49:46 +02:00
|
|
|
#include <ktemporaryfile.h>
|
|
|
|
#include <kurl.h>
|
|
|
|
|
|
|
|
#include <QtJolie/Message>
|
2009-09-02 04:21:41 +02:00
|
|
|
|
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
2011-05-31 01:00:03 +02:00
|
|
|
class RemoteObjectDescription::Private
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
QString name;
|
|
|
|
QString description;
|
|
|
|
QString icon;
|
|
|
|
KUrl url;
|
|
|
|
};
|
|
|
|
|
|
|
|
RemoteObjectDescription::RemoteObjectDescription()
|
|
|
|
: d(new Private)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
RemoteObjectDescription::RemoteObjectDescription(const RemoteObjectDescription &other)
|
|
|
|
: d(new Private(*other.d))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
RemoteObjectDescription &RemoteObjectDescription::operator=(const RemoteObjectDescription &other)
|
|
|
|
{
|
|
|
|
*d = *other.d;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void RemoteObjectDescription::setName(const QString &name)
|
|
|
|
{
|
|
|
|
d->name = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString RemoteObjectDescription::name() const
|
|
|
|
{
|
|
|
|
return d->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void RemoteObjectDescription::setUrl(const KUrl &url)
|
|
|
|
{
|
|
|
|
d->url = url;
|
|
|
|
}
|
|
|
|
|
|
|
|
KUrl RemoteObjectDescription::url() const
|
|
|
|
{
|
|
|
|
return d->url;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RemoteObjectDescription::setDescription(const QString &description)
|
|
|
|
{
|
|
|
|
d->description = description;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString RemoteObjectDescription::description() const
|
|
|
|
{
|
|
|
|
return d->description;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RemoteObjectDescription::setIcon(const QString &icon)
|
|
|
|
{
|
|
|
|
d->icon = icon;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString RemoteObjectDescription::icon() const
|
|
|
|
{
|
|
|
|
return d->icon;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-09-02 04:21:41 +02:00
|
|
|
class AccessManagerSingleton
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AccessManager self;
|
|
|
|
};
|
|
|
|
|
|
|
|
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-03 01:49:58 +02:00
|
|
|
AuthorizationManager::self()->d->prepareForServiceAccess();
|
|
|
|
|
2009-09-02 04:27:16 +02:00
|
|
|
KUrl resolvedLocation;
|
2009-09-22 17:29:08 +02:00
|
|
|
if (location.protocol() == "plasma+zeroconf") {
|
2009-09-02 04:27:16 +02:00
|
|
|
if (d->zeroconfServices.contains(location.host())) {
|
2011-05-31 01:00:03 +02:00
|
|
|
resolvedLocation = d->services[location.host()].url();
|
2009-09-02 04:27:16 +02:00
|
|
|
} else {
|
2011-07-29 15:46:52 +02:00
|
|
|
#ifndef NDEBUG
|
2009-09-03 01:49:46 +02:00
|
|
|
kDebug() << "There's no zeroconf service with this name.";
|
2011-07-29 15:46:52 +02:00
|
|
|
#endif
|
2009-09-02 04:27:16 +02:00
|
|
|
}
|
|
|
|
} 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
|
|
|
}
|
|
|
|
|
2011-05-31 01:00:03 +02:00
|
|
|
QList<RemoteObjectDescription> 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-22 17:29:08 +02:00
|
|
|
QStringList AccessManager::supportedProtocols()
|
|
|
|
{
|
|
|
|
QStringList list;
|
|
|
|
list << "plasma" << "plasma+zeroconf";
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
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-09 15:40:16 +02:00
|
|
|
#ifdef ENABLE_REMOTE_WIDGETS
|
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();
|
2009-09-09 15:40:16 +02:00
|
|
|
#else
|
|
|
|
kWarning() << "libplasma is compiled without support for remote widgets. not monitoring remote widgets on the network";
|
|
|
|
#endif
|
2009-09-02 04:27:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
AccessManagerPrivate::~AccessManagerPrivate()
|
|
|
|
{
|
|
|
|
delete browser;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AccessManagerPrivate::slotJobFinished(KJob *job)
|
|
|
|
{
|
|
|
|
emit q->finished(static_cast<AccessAppletJob*>(job));
|
|
|
|
}
|
|
|
|
|
|
|
|
void AccessManagerPrivate::slotAddService(DNSSD::RemoteService::Ptr service)
|
|
|
|
{
|
2011-07-29 15:46:52 +02:00
|
|
|
#ifndef NDEBUG
|
2009-09-02 04:27:16 +02:00
|
|
|
kDebug();
|
2011-07-29 15:46:52 +02:00
|
|
|
#endif
|
2009-09-02 04:27:16 +02:00
|
|
|
if (!service->resolve()) {
|
2011-07-29 15:46:52 +02:00
|
|
|
#ifndef NDEBUG
|
2009-09-03 01:49:46 +02:00
|
|
|
kDebug() << "Zeroconf service can't be resolved";
|
2011-07-29 15:46:52 +02:00
|
|
|
#endif
|
2009-09-02 04:27:16 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!services.contains(service->serviceName())) {
|
2011-05-31 01:00:03 +02:00
|
|
|
RemoteObjectDescription metadata;
|
2011-07-29 15:46:52 +02:00
|
|
|
#ifndef NDEBUG
|
2009-09-02 04:27:16 +02:00
|
|
|
kDebug() << "textdata = " << service->textData();
|
2011-07-29 15:46:52 +02:00
|
|
|
#endif
|
|
|
|
#ifndef NDEBUG
|
2009-09-02 04:27:16 +02:00
|
|
|
kDebug() << "hostname: " << service->hostName();
|
2011-07-29 15:46:52 +02:00
|
|
|
#endif
|
2009-09-02 04:27:16 +02:00
|
|
|
QHostAddress address = DNSSD::ServiceBrowser::resolveHostName(service->hostName());
|
|
|
|
QString ip = address.toString();
|
2011-07-29 15:46:52 +02:00
|
|
|
#ifndef NDEBUG
|
2009-09-02 04:27:16 +02:00
|
|
|
kDebug() << "result for resolve = " << ip;
|
2011-07-29 15:46:52 +02:00
|
|
|
#endif
|
2009-09-02 04:27:16 +02:00
|
|
|
|
|
|
|
KUrl url(QString("plasma://%1:%2/%3").arg(ip)
|
|
|
|
.arg(service->port())
|
|
|
|
.arg(service->serviceName()));
|
|
|
|
|
2011-05-31 01:00:03 +02:00
|
|
|
if (service->textData().isEmpty()) {
|
2011-07-29 15:46:52 +02:00
|
|
|
#ifndef NDEBUG
|
2011-05-31 01:00:03 +02:00
|
|
|
kDebug() << "no textdata?";
|
2011-07-29 15:46:52 +02:00
|
|
|
#endif
|
2011-05-31 01:00:03 +02:00
|
|
|
metadata.setName(service->serviceName());
|
|
|
|
metadata.setUrl(url);
|
|
|
|
} else {
|
2011-07-29 15:46:52 +02:00
|
|
|
#ifndef NDEBUG
|
2009-09-02 04:27:16 +02:00
|
|
|
kDebug() << "service has got textdata";
|
2011-07-29 15:46:52 +02:00
|
|
|
#endif
|
2009-09-02 04:27:16 +02:00
|
|
|
QMap<QString, QByteArray> textData = service->textData();
|
|
|
|
metadata.setName(textData["name"]);
|
|
|
|
metadata.setDescription(textData["description"]);
|
2011-04-28 23:48:55 +02:00
|
|
|
metadata.setIcon(textData["icon"]);
|
2011-05-31 01:00:03 +02:00
|
|
|
metadata.setUrl(url);
|
2009-09-02 04:27:16 +02:00
|
|
|
}
|
|
|
|
|
2011-07-29 15:46:52 +02:00
|
|
|
#ifndef NDEBUG
|
2011-05-31 01:00:03 +02:00
|
|
|
kDebug() << "location = " << metadata.url();
|
2011-07-29 15:46:52 +02:00
|
|
|
#endif
|
|
|
|
#ifndef NDEBUG
|
2009-09-02 04:27:16 +02:00
|
|
|
kDebug() << "name = " << metadata.name();
|
2011-07-29 15:46:52 +02:00
|
|
|
#endif
|
|
|
|
#ifndef NDEBUG
|
2009-09-02 04:27:16 +02:00
|
|
|
kDebug() << "description = " << metadata.name();
|
2011-07-29 15:46:52 +02:00
|
|
|
#endif
|
2009-09-02 04:27:16 +02:00
|
|
|
|
|
|
|
services[service->serviceName()] = metadata;
|
|
|
|
zeroconfServices[service->serviceName()] = service;
|
|
|
|
emit q->remoteAppletAnnounced(metadata);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AccessManagerPrivate::slotRemoveService(DNSSD::RemoteService::Ptr service)
|
|
|
|
{
|
2011-07-29 15:46:52 +02:00
|
|
|
#ifndef NDEBUG
|
2009-09-02 04:27:16 +02:00
|
|
|
kDebug();
|
2011-07-29 15:46:52 +02:00
|
|
|
#endif
|
2009-09-02 04:27:16 +02:00
|
|
|
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"
|