kdeconnect-kde/daemon/daemon.cpp

202 lines
5.6 KiB
C++
Raw Normal View History

2013-06-06 04:57:06 +01:00
/**
* Copyright 2013 Albert Vaca <albertvaka@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License or (at your option) version 3 or any later version
* accepted by the membership of KDE e.V. (or its successor approved
* by the membership of KDE e.V.), which shall act as a proxy
* defined in Section 14 of version 3 of the license.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "daemon.h"
#include "networkpackage.h"
2013-06-19 12:29:24 +01:00
#include "packagereceivers/notificationpackagereceiver.h"
#include "packagereceivers/pausemusicpackagereceiver.h"
#include "announcers/avahiannouncer.h"
#include "announcers/fakeannouncer.h"
2013-06-25 20:19:17 +01:00
#include "devicelinks/echodevicelink.h"
2013-06-06 04:57:06 +01:00
#include <QtNetwork/QUdpSocket>
#include <QFile>
2013-06-25 17:08:34 +01:00
#include <QDBusConnection>
2013-07-02 01:46:41 +01:00
#include <KIcon>
#include <KConfigGroup>
2013-06-06 04:57:06 +01:00
2013-06-17 11:23:08 +01:00
#include <sstream>
#include <iomanip>
2013-06-06 04:57:06 +01:00
#include <iostream>
K_PLUGIN_FACTORY(KdeConnectFactory, registerPlugin<Daemon>();)
K_EXPORT_PLUGIN(KdeConnectFactory("kdeconnect", "kdeconnect"))
2013-06-06 04:57:06 +01:00
2013-06-25 20:19:17 +01:00
void Daemon::linkTo(DeviceLink* dl)
{
2013-06-17 11:23:08 +01:00
linkedDevices.append(dl);
2013-06-17 11:23:08 +01:00
Q_FOREACH (PackageReceiver* pr, packageReceivers) {
QObject::connect(dl,SIGNAL(receivedPackage(const NetworkPackage&)),
pr,SLOT(receivePackage(const NetworkPackage&)));
2013-06-17 11:23:08 +01:00
}
2013-07-02 14:22:05 +01:00
KNotification* notification = new KNotification("pingReceived"); //KNotification::Persistent
notification->setPixmap(KIcon("dialog-ok").pixmap(48, 48));
notification->setComponentData(KComponentData("kdeconnect", "kdeconnect"));
notification->setTitle(dl->device()->name());
notification->setText("Succesfully connected");
2013-06-17 11:23:08 +01:00
}
2013-06-06 04:57:06 +01:00
Daemon::Daemon(QObject *parent, const QList<QVariant>&)
: KDEDModule(parent)
2013-07-02 01:46:41 +01:00
, config(KSharedConfig::openConfig("kdeconnectrc"))
2013-06-06 04:57:06 +01:00
{
qDebug() << "GO GO GO!";
//TODO: Do not hardcode the load of the package receivers
//use: https://techbase.kde.org/Development/Tutorials/Services/Plugins
2013-06-06 04:57:06 +01:00
packageReceivers.push_back(new NotificationPackageReceiver());
packageReceivers.push_back(new PauseMusicPackageReceiver());
//TODO: Do not hardcode the load of the device locators
//use: https://techbase.kde.org/Development/Tutorials/Services/Plugins
announcers.insert(new AvahiAnnouncer());
announcers.insert(new FakeAnnouncer());
//TODO: Add package emitters
2013-07-02 01:46:41 +01:00
//Read remebered paired devices
const KConfigGroup& known = config->group("devices").group("paired");
const QStringList& list = known.groupList();
const QString defaultName("unnamed");
Q_FOREACH(QString id, list) {
const KConfigGroup& data = known.group(id);
const QString& name = data.readEntry<QString>("name",defaultName);
pairedDevices.push_back(new Device(id,name));
}
QDBusConnection::sessionBus().registerService("org.kde.kdeconnect");
//Listen to incomming connections
Q_FOREACH (Announcer* a, announcers) {
QObject::connect(a,SIGNAL(deviceConnection(DeviceLink*)),
this,SLOT(deviceConnection(DeviceLink*)));
}
2013-06-06 04:57:06 +01:00
2013-06-17 11:23:08 +01:00
}
2013-06-06 04:57:06 +01:00
2013-06-17 11:23:08 +01:00
QString Daemon::listVisibleDevices()
{
2013-06-06 04:57:06 +01:00
2013-06-17 11:23:08 +01:00
std::stringstream ret;
2013-06-06 04:57:06 +01:00
2013-06-17 11:23:08 +01:00
ret << std::setw(20) << "ID";
ret << std::setw(20) << "Name";
ret << std::endl;
2013-06-06 04:57:06 +01:00
Q_FOREACH (DeviceLink* d, visibleDevices) {
ret << std::setw(20) << d->device()->id().toStdString();
ret << std::setw(20) << d->device()->name().toStdString();
ret << std::endl;
2013-06-06 04:57:06 +01:00
}
2013-06-17 11:23:08 +01:00
return QString::fromStdString(ret.str());
}
void Daemon::startDiscovery(int timeOut)
{
2013-06-27 13:33:51 +01:00
qDebug() << "Start discovery";
//Listen to incomming connections
Q_FOREACH (Announcer* a, announcers) {
a->setDiscoverable(true);
}
}
bool Daemon::pairDevice(QString id)
2013-06-17 11:23:08 +01:00
{
2013-07-02 14:22:05 +01:00
if (!visibleDevices.contains(id)) {
return false;
}
2013-07-02 01:46:41 +01:00
config->group("devices").group("paired").group(id).writeEntry("name",visibleDevices[id]->device()->name());
linkTo(visibleDevices[id]);
return true;
2013-07-02 14:22:05 +01:00
}
2013-07-02 14:22:05 +01:00
bool Daemon::unpairDevice(QString id)
{
/*qDebug() << "M'han passat" << id;
foreach(QString c, config->group("devices").group("paired").groupList()) {
qDebug() << "Tinc" << c;
}*/
if (!config->group("devices").group("paired").hasGroup(id)) {
return false;
}
config->group("devices").group("paired").deleteGroup(id);
return true;
2013-06-06 04:57:06 +01:00
}
2013-07-02 14:22:05 +01:00
2013-06-18 02:05:32 +01:00
QString Daemon::listLinkedDevices()
{
QString ret;
Q_FOREACH (DeviceLink* dl, linkedDevices) {
2013-06-25 20:19:17 +01:00
if (!ret.isEmpty()) ret += "\n";
//ret += dl->device()->name() + "(" + dl->device()->id() + ")";
ret += dl->device()->id();
}
return ret;
}
void Daemon::deviceConnection(DeviceLink* dl)
{
2013-07-02 01:46:41 +01:00
qDebug() << "deviceConnection";
QString id = dl->device()->id();
bool paired = false;
Q_FOREACH (Device* d, pairedDevices) {
if (id == d->id()) {
paired = true;
break;
}
}
visibleDevices[dl->device()->id()] = dl;
if (paired) {
2013-07-02 01:46:41 +01:00
qDebug() << "Known device connected" << dl->device()->name();
linkTo(dl);
}
else {
2013-07-02 01:46:41 +01:00
qDebug() << "Unknown device connected" << dl->device()->name();
emit deviceDiscovered(dl->device()->id(), dl->device()->name());
}
}
2013-06-06 04:57:06 +01:00
Daemon::~Daemon()
{
qDebug() << "SAYONARA BABY";
}
2013-06-17 11:23:08 +01:00