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"
|
|
|
|
#include "ui_kcm.h"
|
2013-06-25 20:19:17 +01:00
|
|
|
#include "ui_wizard.h"
|
2013-08-21 17:25:44 +01:00
|
|
|
#include "libkdeconnect/dbusinterfaces.h"
|
2013-06-19 15:15:25 +01:00
|
|
|
|
|
|
|
#include <QtGui/QLabel>
|
|
|
|
#include <QtGui/QMenu>
|
|
|
|
#include <QtGui/QMenuBar>
|
|
|
|
#include <QtGui/QAction>
|
|
|
|
#include <QtGui/QStackedLayout>
|
|
|
|
#include <QtGui/QListView>
|
2013-06-25 17:08:34 +01:00
|
|
|
#include <QDBusConnection>
|
|
|
|
#include <QDBusInterface>
|
2013-06-19 15:15:25 +01:00
|
|
|
|
2013-08-15 23:25:13 +01:00
|
|
|
#include "devicessortproxymodel.h"
|
|
|
|
|
2013-08-13 04:07:32 +01:00
|
|
|
#include <KServiceTypeTrader>
|
|
|
|
#include <KPluginInfo>
|
2013-06-19 15:15:25 +01:00
|
|
|
#include <KDebug>
|
|
|
|
#include <kpluginfactory.h>
|
|
|
|
#include <kstandarddirs.h>
|
|
|
|
|
|
|
|
K_PLUGIN_FACTORY(KdeConnectKcmFactory, registerPlugin<KdeConnectKcm>();)
|
|
|
|
K_EXPORT_PLUGIN(KdeConnectKcmFactory("kdeconnect-kcm", "kdeconnect-kcm"))
|
|
|
|
|
|
|
|
KdeConnectKcm::KdeConnectKcm(QWidget *parent, const QVariantList&)
|
|
|
|
: KCModule(KdeConnectKcmFactory::componentData(), parent)
|
2013-06-27 01:13:16 +01:00
|
|
|
, kcmUi(new Ui::KdeConnectKcmUi())
|
2013-08-15 23:25:13 +01:00
|
|
|
, devicesModel(new DevicesModel(this))
|
2013-08-13 04:07:32 +01:00
|
|
|
, currentDevice(0)
|
|
|
|
//, config(KSharedConfig::openConfig("kdeconnectrc"))
|
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
|
|
|
|
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-06-25 17:08:34 +01:00
|
|
|
|
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)));
|
|
|
|
connect(kcmUi->ping_button, SIGNAL(pressed()),
|
|
|
|
this, SLOT(sendPing()));
|
|
|
|
connect(kcmUi->trust_checkbox, SIGNAL(toggled(bool)),
|
|
|
|
this, SLOT(trustedStateChanged(bool)));
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-06-19 15:15:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
KdeConnectKcm::~KdeConnectKcm()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
//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-13 04:07:32 +01:00
|
|
|
//FIXME: KPluginSelector has no way to remove a list of plugins and load another, so we need to destroy and recreate it each time
|
|
|
|
delete kcmUi->pluginSelector;
|
|
|
|
kcmUi->pluginSelector = new KPluginSelector(this);
|
|
|
|
kcmUi->verticalLayout_2->addWidget(kcmUi->pluginSelector);
|
|
|
|
|
|
|
|
kcmUi->deviceName->setText(currentDevice->name());
|
|
|
|
kcmUi->trust_checkbox->setChecked(currentDevice->paired());
|
|
|
|
|
|
|
|
KService::List offers = KServiceTypeTrader::self()->query("KdeConnect/Plugin");
|
|
|
|
QList<KPluginInfo> scriptinfos = KPluginInfo::fromServices(offers);
|
|
|
|
|
|
|
|
QString path = KStandardDirs().resourceDirs("config").first()+"kdeconnect/";
|
|
|
|
KSharedConfigPtr deviceConfig = KSharedConfig::openConfig(path + currentDevice->id());
|
|
|
|
kcmUi->pluginSelector->addPlugins(scriptinfos, KPluginSelector::ReadConfigFile, "Plugins", QString(), deviceConfig);
|
|
|
|
|
2013-08-15 23:25:13 +01:00
|
|
|
connect(kcmUi->pluginSelector, SIGNAL(changed(bool)),
|
|
|
|
this, SLOT(pluginsConfigChanged()));
|
2013-06-19 15:15:25 +01:00
|
|
|
}
|
|
|
|
|
2013-07-03 02:52:44 +01:00
|
|
|
void KdeConnectKcm::trustedStateChanged(bool b)
|
2013-06-19 15:15:25 +01:00
|
|
|
{
|
2013-08-13 04:07:32 +01:00
|
|
|
if (!currentDevice) return;
|
2013-08-16 08:27:32 +01:00
|
|
|
QDBusPendingReply<void> pendingReply = currentDevice->setPair(b);
|
|
|
|
pendingReply.waitForFinished();
|
|
|
|
if (pendingReply.isValid()) {
|
|
|
|
//If dbus was down, calling this would make kcm crash
|
|
|
|
devicesModel->deviceStatusChanged(currentDevice->id());
|
|
|
|
} else {
|
|
|
|
//Revert checkbox
|
|
|
|
disconnect(kcmUi->trust_checkbox, SIGNAL(toggled(bool)),
|
|
|
|
this, SLOT(trustedStateChanged(bool)));
|
|
|
|
kcmUi->trust_checkbox->setCheckState(b? Qt::Unchecked : Qt::Checked);
|
|
|
|
connect(kcmUi->trust_checkbox, SIGNAL(toggled(bool)),
|
|
|
|
this, SLOT(trustedStateChanged(bool)));
|
|
|
|
}
|
2013-08-13 04:07:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void KdeConnectKcm::pluginsConfigChanged()
|
|
|
|
{
|
|
|
|
//Store previous selection
|
|
|
|
if (!currentDevice) return;
|
|
|
|
|
|
|
|
DeviceDbusInterface* auxCurrentDevice = currentDevice; //HACK to avoid infinite recursion (for some reason calling save on pluginselector emits changed)
|
|
|
|
currentDevice = 0;
|
|
|
|
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;
|
|
|
|
currentDevice->sendPing();
|
2013-06-19 15:15:25 +01:00
|
|
|
}
|
|
|
|
|