2013-06-19 15:15:25 +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 "kcm.h"
|
|
|
|
|
2014-06-16 19:02:07 +01:00
|
|
|
#include <QLabel>
|
|
|
|
#include <QMenu>
|
|
|
|
#include <QMenuBar>
|
|
|
|
#include <QAction>
|
|
|
|
#include <QStackedLayout>
|
|
|
|
#include <QListView>
|
2013-06-25 17:08:34 +01:00
|
|
|
#include <QDBusConnection>
|
|
|
|
#include <QDBusInterface>
|
2013-08-15 23:25:13 +01:00
|
|
|
|
2013-08-13 04:07:32 +01:00
|
|
|
#include <KServiceTypeTrader>
|
|
|
|
#include <KPluginInfo>
|
2015-03-19 15:36:53 +00:00
|
|
|
#include <KPluginMetaData>
|
2013-09-09 17:29:24 +01:00
|
|
|
#include <KPluginFactory>
|
2014-06-16 19:02:07 +01:00
|
|
|
#include <KAboutData>
|
|
|
|
#include <KLocalizedString>
|
2013-09-09 17:29:24 +01:00
|
|
|
|
|
|
|
#include "ui_kcm.h"
|
2014-06-14 14:22:40 +01:00
|
|
|
#include "interfaces/dbusinterfaces.h"
|
|
|
|
#include "interfaces/devicesmodel.h"
|
2013-09-09 17:29:24 +01:00
|
|
|
#include "devicessortproxymodel.h"
|
2013-06-19 15:15:25 +01:00
|
|
|
|
|
|
|
K_PLUGIN_FACTORY(KdeConnectKcmFactory, registerPlugin<KdeConnectKcm>();)
|
|
|
|
|
|
|
|
KdeConnectKcm::KdeConnectKcm(QWidget *parent, const QVariantList&)
|
2014-06-16 19:02:07 +01:00
|
|
|
: KCModule(KAboutData::pluginData("kdeconnect-kcm"), parent)
|
2013-06-27 01:13:16 +01:00
|
|
|
, kcmUi(new Ui::KdeConnectKcmUi())
|
2015-03-02 05:23:05 +00:00
|
|
|
, daemon(new DaemonDbusInterface(this))
|
2013-08-15 23:25:13 +01:00
|
|
|
, devicesModel(new DevicesModel(this))
|
2013-08-13 04:07:32 +01:00
|
|
|
, currentDevice(0)
|
2013-06-19 15:15:25 +01:00
|
|
|
{
|
2013-06-27 01:13:16 +01:00
|
|
|
kcmUi->setupUi(this);
|
2013-06-19 15:15:25 +01:00
|
|
|
|
2015-03-02 05:23:05 +00:00
|
|
|
|
2013-06-27 01:13:16 +01:00
|
|
|
kcmUi->deviceList->setIconSize(QSize(32,32));
|
2013-08-15 23:25:13 +01:00
|
|
|
|
|
|
|
sortProxyModel = new DevicesSortProxyModel(devicesModel);
|
|
|
|
|
|
|
|
kcmUi->deviceList->setModel(sortProxyModel);
|
2013-06-19 15:15:25 +01:00
|
|
|
|
2013-07-03 02:52:44 +01:00
|
|
|
kcmUi->deviceInfo->setVisible(false);
|
2013-08-30 18:10:43 +01:00
|
|
|
kcmUi->progressBar->setVisible(false);
|
|
|
|
kcmUi->messages->setVisible(false);
|
2013-06-25 17:08:34 +01:00
|
|
|
|
2015-03-02 05:23:05 +00:00
|
|
|
kcmUi->rename_label->setText(daemon->announcedName());
|
|
|
|
kcmUi->rename_edit->setText(daemon->announcedName());
|
|
|
|
setRenameMode(false);
|
|
|
|
|
2013-08-13 04:07:32 +01:00
|
|
|
setButtons(KCModule::NoAdditionalButton);
|
|
|
|
|
2013-08-15 23:25:13 +01:00
|
|
|
connect(devicesModel, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
|
|
|
|
this, SLOT(resetSelection()));
|
|
|
|
connect(kcmUi->deviceList, SIGNAL(pressed(QModelIndex)),
|
|
|
|
this, SLOT(deviceSelected(QModelIndex)));
|
2015-03-01 21:58:40 +00:00
|
|
|
connect(kcmUi->pair_button, SIGNAL(clicked()),
|
2013-08-30 18:10:43 +01:00
|
|
|
this, SLOT(requestPair()));
|
2015-03-01 21:58:40 +00:00
|
|
|
connect(kcmUi->unpair_button, SIGNAL(clicked()),
|
2013-08-30 18:10:43 +01:00
|
|
|
this, SLOT(unpair()));
|
2015-03-01 21:58:40 +00:00
|
|
|
connect(kcmUi->ping_button, SIGNAL(clicked()),
|
2013-08-15 23:25:13 +01:00
|
|
|
this, SLOT(sendPing()));
|
2015-03-01 21:58:40 +00:00
|
|
|
connect(kcmUi->refresh_button,SIGNAL(clicked()),
|
2014-08-11 17:56:11 +01:00
|
|
|
this, SLOT(refresh()));
|
2015-03-02 05:23:05 +00:00
|
|
|
connect(kcmUi->rename_edit,SIGNAL(returnPressed()),
|
|
|
|
this, SLOT(renameDone()));
|
|
|
|
connect(kcmUi->renameDone_button,SIGNAL(clicked()),
|
|
|
|
this, SLOT(renameDone()));
|
|
|
|
connect(kcmUi->renameShow_button,SIGNAL(clicked()),
|
|
|
|
this, SLOT(renameShow()));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void KdeConnectKcm::renameShow()
|
|
|
|
{
|
|
|
|
setRenameMode(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void KdeConnectKcm::renameDone()
|
|
|
|
{
|
|
|
|
QString newName = kcmUi->rename_edit->text();
|
|
|
|
if (newName.isEmpty()) {
|
|
|
|
//Rollback changes
|
|
|
|
kcmUi->rename_edit->setText(kcmUi->rename_label->text());
|
|
|
|
} else {
|
|
|
|
kcmUi->rename_label->setText(newName);
|
|
|
|
daemon->setAnnouncedName(newName);
|
|
|
|
}
|
|
|
|
setRenameMode(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void KdeConnectKcm::setRenameMode(bool b) {
|
|
|
|
kcmUi->renameDone_button->setVisible(b);
|
|
|
|
kcmUi->rename_edit->setVisible(b);
|
|
|
|
kcmUi->renameShow_button->setVisible(!b);
|
|
|
|
kcmUi->rename_label->setVisible(!b);
|
2013-06-19 15:15:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
KdeConnectKcm::~KdeConnectKcm()
|
|
|
|
{
|
2014-11-10 19:36:12 +00:00
|
|
|
delete kcmUi;
|
2013-06-19 15:15:25 +01:00
|
|
|
}
|
|
|
|
|
2014-08-11 17:56:11 +01:00
|
|
|
void KdeConnectKcm::refresh()
|
|
|
|
{
|
2015-03-02 05:23:05 +00:00
|
|
|
daemon->forceOnNetworkChange();
|
2014-08-11 17:56:11 +01:00
|
|
|
}
|
|
|
|
|
2013-08-15 23:25:13 +01:00
|
|
|
void KdeConnectKcm::resetSelection()
|
|
|
|
{
|
|
|
|
kcmUi->deviceList->selectionModel()->setCurrentIndex(sortProxyModel->mapFromSource(currentIndex), QItemSelectionModel::ClearAndSelect);
|
|
|
|
}
|
|
|
|
|
2013-07-03 02:52:44 +01:00
|
|
|
void KdeConnectKcm::deviceSelected(const QModelIndex& current)
|
2013-06-19 15:15:25 +01:00
|
|
|
{
|
2013-08-15 23:25:13 +01:00
|
|
|
|
2013-09-16 15:52:40 +01:00
|
|
|
if (currentDevice) {
|
|
|
|
disconnect(currentDevice,SIGNAL(pairingSuccesful()),
|
|
|
|
this, SLOT(pairingSuccesful()));
|
|
|
|
disconnect(currentDevice,SIGNAL(pairingFailed(QString)),
|
|
|
|
this, SLOT(pairingFailed(QString)));
|
|
|
|
disconnect(currentDevice,SIGNAL(unpaired()),
|
|
|
|
this, SLOT(unpaired()));
|
|
|
|
}
|
|
|
|
|
2013-08-15 23:25:13 +01:00
|
|
|
//Store previous device config
|
2013-08-13 04:07:32 +01:00
|
|
|
pluginsConfigChanged();
|
|
|
|
|
2013-08-18 17:22:54 +01:00
|
|
|
if (!current.isValid()) {
|
|
|
|
kcmUi->deviceInfo->setVisible(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-08-15 23:25:13 +01:00
|
|
|
currentIndex = sortProxyModel->mapToSource(current);
|
2013-08-18 17:22:54 +01:00
|
|
|
currentDevice = devicesModel->getDevice(currentIndex);
|
2013-08-15 23:25:13 +01:00
|
|
|
|
2013-08-18 17:22:54 +01:00
|
|
|
bool valid = (currentDevice != NULL && currentDevice->isValid());
|
2013-08-15 23:25:13 +01:00
|
|
|
kcmUi->deviceInfo->setVisible(valid);
|
2013-08-18 17:22:54 +01:00
|
|
|
if (!valid) {
|
|
|
|
return;
|
|
|
|
}
|
2013-08-15 23:25:13 +01:00
|
|
|
|
2013-08-30 18:10:43 +01:00
|
|
|
kcmUi->messages->setVisible(false);
|
|
|
|
if (currentDevice->pairRequested()) {
|
|
|
|
kcmUi->progressBar->setVisible(true);
|
|
|
|
kcmUi->unpair_button->setVisible(false);
|
|
|
|
kcmUi->pair_button->setVisible(false);
|
|
|
|
kcmUi->ping_button->setVisible(false);
|
|
|
|
} else {
|
|
|
|
kcmUi->progressBar->setVisible(false);
|
|
|
|
if (currentDevice->isPaired()) {
|
|
|
|
kcmUi->unpair_button->setVisible(true);
|
|
|
|
kcmUi->pair_button->setVisible(false);
|
|
|
|
kcmUi->ping_button->setVisible(true);
|
|
|
|
} else {
|
|
|
|
kcmUi->unpair_button->setVisible(false);
|
|
|
|
kcmUi->pair_button->setVisible(true);
|
|
|
|
kcmUi->ping_button->setVisible(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-02 04:16:07 +00:00
|
|
|
//KPluginSelector has no way to remove a list of plugins and load another, so we need to destroy and recreate it each time
|
2013-08-13 04:07:32 +01:00
|
|
|
delete kcmUi->pluginSelector;
|
|
|
|
kcmUi->pluginSelector = new KPluginSelector(this);
|
|
|
|
kcmUi->verticalLayout_2->addWidget(kcmUi->pluginSelector);
|
|
|
|
|
2015-03-14 04:19:39 +00:00
|
|
|
kcmUi->pluginSelector->setConfigurationArguments(QStringList(currentDevice->id()));
|
|
|
|
|
2013-08-30 18:10:43 +01:00
|
|
|
kcmUi->name_label->setText(currentDevice->name());
|
2013-09-04 20:19:02 +01:00
|
|
|
kcmUi->status_label->setText(currentDevice->isPaired()? i18n("(paired)") : i18n("(unpaired)"));
|
2013-08-13 04:07:32 +01:00
|
|
|
|
2013-09-16 15:52:40 +01:00
|
|
|
connect(currentDevice,SIGNAL(pairingSuccesful()),
|
|
|
|
this, SLOT(pairingSuccesful()));
|
|
|
|
connect(currentDevice,SIGNAL(pairingFailed(QString)),
|
|
|
|
this, SLOT(pairingFailed(QString)));
|
|
|
|
connect(currentDevice,SIGNAL(unpaired()),
|
|
|
|
this, SLOT(unpaired()));
|
|
|
|
|
2015-03-19 15:36:53 +00:00
|
|
|
const QList<KPluginInfo> pluginInfo = KPluginInfo::fromMetaData(KPluginLoader::findPlugins("kdeconnect/"));
|
2015-03-02 04:16:07 +00:00
|
|
|
KSharedConfigPtr deviceConfig = KSharedConfig::openConfig(currentDevice->pluginsConfigFile());
|
2015-03-19 15:36:53 +00:00
|
|
|
kcmUi->pluginSelector->addPlugins(pluginInfo, KPluginSelector::ReadConfigFile, i18n("Plugins"), QString(), deviceConfig);
|
2013-08-13 04:07:32 +01:00
|
|
|
|
2013-08-15 23:25:13 +01:00
|
|
|
connect(kcmUi->pluginSelector, SIGNAL(changed(bool)),
|
|
|
|
this, SLOT(pluginsConfigChanged()));
|
2013-09-16 15:52:40 +01:00
|
|
|
|
2013-06-19 15:15:25 +01:00
|
|
|
}
|
|
|
|
|
2013-08-30 18:10:43 +01:00
|
|
|
void KdeConnectKcm::requestPair()
|
2013-06-19 15:15:25 +01:00
|
|
|
{
|
2013-09-16 15:52:40 +01:00
|
|
|
if (!currentDevice) {
|
|
|
|
return;
|
|
|
|
}
|
2013-08-30 18:10:43 +01:00
|
|
|
|
|
|
|
kcmUi->messages->hide();
|
|
|
|
|
|
|
|
kcmUi->pair_button->setVisible(false);
|
|
|
|
kcmUi->progressBar->setVisible(true);
|
|
|
|
|
|
|
|
currentDevice->requestPair();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void KdeConnectKcm::unpair()
|
|
|
|
{
|
2013-09-16 15:52:40 +01:00
|
|
|
if (!currentDevice) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
currentDevice->unpair();
|
|
|
|
}
|
|
|
|
|
|
|
|
void KdeConnectKcm::unpaired()
|
|
|
|
{
|
|
|
|
DeviceDbusInterface* senderDevice = (DeviceDbusInterface*) sender();
|
|
|
|
devicesModel->deviceStatusChanged(senderDevice->id());
|
|
|
|
|
|
|
|
if (senderDevice != currentDevice) return;
|
2013-08-30 18:10:43 +01:00
|
|
|
|
|
|
|
kcmUi->pair_button->setVisible(true);
|
|
|
|
kcmUi->unpair_button->setVisible(false);
|
|
|
|
kcmUi->progressBar->setVisible(false);
|
|
|
|
kcmUi->ping_button->setVisible(false);
|
2013-09-04 20:19:02 +01:00
|
|
|
kcmUi->status_label->setText(i18n("(unpaired)"));
|
2013-08-30 18:10:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void KdeConnectKcm::pairingFailed(const QString& error)
|
|
|
|
{
|
2013-09-16 14:46:28 +01:00
|
|
|
if (sender() != currentDevice) return;
|
|
|
|
|
2013-09-16 15:52:40 +01:00
|
|
|
kcmUi->pair_button->setVisible(true);
|
|
|
|
kcmUi->unpair_button->setVisible(false);
|
|
|
|
kcmUi->progressBar->setVisible(false);
|
|
|
|
kcmUi->ping_button->setVisible(false);
|
|
|
|
kcmUi->status_label->setText(i18n("(unpaired)"));
|
|
|
|
|
2013-09-04 20:19:02 +01:00
|
|
|
kcmUi->messages->setText(i18n("Error trying to pair: %1",error));
|
2013-08-30 18:10:43 +01:00
|
|
|
kcmUi->messages->animatedShow();
|
|
|
|
}
|
|
|
|
|
|
|
|
void KdeConnectKcm::pairingSuccesful()
|
|
|
|
{
|
2013-09-16 15:52:40 +01:00
|
|
|
DeviceDbusInterface* senderDevice = (DeviceDbusInterface*) sender();
|
|
|
|
devicesModel->deviceStatusChanged(senderDevice->id());
|
2013-09-16 14:46:28 +01:00
|
|
|
|
2013-09-16 15:52:40 +01:00
|
|
|
if (senderDevice != currentDevice) return;
|
2013-09-16 14:46:28 +01:00
|
|
|
|
2013-08-30 18:10:43 +01:00
|
|
|
kcmUi->pair_button->setVisible(false);
|
2013-09-16 15:52:40 +01:00
|
|
|
kcmUi->unpair_button->setVisible(true);
|
|
|
|
kcmUi->progressBar->setVisible(false);
|
2013-08-30 18:10:43 +01:00
|
|
|
kcmUi->ping_button->setVisible(true);
|
2013-09-04 20:19:02 +01:00
|
|
|
kcmUi->status_label->setText(i18n("(paired)"));
|
2013-08-13 04:07:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void KdeConnectKcm::pluginsConfigChanged()
|
|
|
|
{
|
|
|
|
//Store previous selection
|
|
|
|
if (!currentDevice) return;
|
|
|
|
|
2013-08-30 18:10:43 +01:00
|
|
|
DeviceDbusInterface* auxCurrentDevice = currentDevice;
|
|
|
|
currentDevice = 0; //HACK to avoid infinite recursion (for some reason calling save on pluginselector emits changed)
|
2013-08-13 04:07:32 +01:00
|
|
|
kcmUi->pluginSelector->save();
|
|
|
|
currentDevice = auxCurrentDevice;
|
|
|
|
|
|
|
|
currentDevice->reloadPlugins();
|
|
|
|
}
|
|
|
|
|
|
|
|
void KdeConnectKcm::save()
|
|
|
|
{
|
|
|
|
pluginsConfigChanged();
|
|
|
|
KCModule::save();
|
2013-06-19 15:15:25 +01:00
|
|
|
}
|
|
|
|
|
2013-07-03 02:52:44 +01:00
|
|
|
void KdeConnectKcm::sendPing()
|
2013-06-19 15:15:25 +01:00
|
|
|
{
|
2013-08-13 04:07:32 +01:00
|
|
|
if (!currentDevice) return;
|
2014-06-27 17:21:40 +01:00
|
|
|
QDBusMessage msg = QDBusMessage::createMethodCall("org.kde.kdeconnect", "/modules/kdeconnect/devices/"+currentDevice->id()+"/ping", "org.kde.kdeconnect.device.ping", "sendPing");
|
|
|
|
QDBusConnection::sessionBus().call(msg);
|
2013-06-19 15:15:25 +01:00
|
|
|
}
|
2014-06-16 19:02:07 +01:00
|
|
|
|
2015-03-01 21:59:28 +00:00
|
|
|
QSize KdeConnectKcm::sizeHint() const
|
|
|
|
{
|
|
|
|
return QSize(890,550); //Golden ratio :D
|
|
|
|
}
|
|
|
|
|
|
|
|
QSize KdeConnectKcm::minimumSizeHint() const
|
|
|
|
{
|
|
|
|
return QSize(500,300);
|
|
|
|
}
|
|
|
|
|
2014-06-16 19:02:07 +01:00
|
|
|
#include "kcm.moc"
|
|
|
|
#include "moc_kcm.cpp"
|