kcm.cpp 6.92 KB
Newer Older
Albert Vaca Cintora's avatar
Albert Vaca Cintora committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/**
 * 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 <QtGui/QLabel>
#include <QtGui/QMenu>
#include <QtGui/QMenuBar>
#include <QtGui/QAction>
#include <QtGui/QStackedLayout>
#include <QtGui/QListView>
Albert Vaca Cintora's avatar
Ooops  
Albert Vaca Cintora committed
29 30
#include <QDBusConnection>
#include <QDBusInterface>
Albert Vaca Cintora's avatar
Albert Vaca Cintora committed
31
#include <QDebug>
32

33 34
#include <KServiceTypeTrader>
#include <KPluginInfo>
Albert Vaca Cintora's avatar
Albert Vaca Cintora committed
35 36 37 38 39 40
#include <KPluginFactory>
#include <KStandardDirs>

#include "ui_kcm.h"
#include "libkdeconnect/dbusinterfaces.h"
#include "devicessortproxymodel.h"
Albert Vaca Cintora's avatar
Albert Vaca Cintora committed
41 42 43 44 45 46

K_PLUGIN_FACTORY(KdeConnectKcmFactory, registerPlugin<KdeConnectKcm>();)
K_EXPORT_PLUGIN(KdeConnectKcmFactory("kdeconnect-kcm", "kdeconnect-kcm"))

KdeConnectKcm::KdeConnectKcm(QWidget *parent, const QVariantList&)
    : KCModule(KdeConnectKcmFactory::componentData(), parent)
47
    , kcmUi(new Ui::KdeConnectKcmUi())
48
    , devicesModel(new DevicesModel(this))
49 50
    , currentDevice(0)
    //, config(KSharedConfig::openConfig("kdeconnectrc"))
Albert Vaca Cintora's avatar
Albert Vaca Cintora committed
51
{
52
    kcmUi->setupUi(this);
Albert Vaca Cintora's avatar
Albert Vaca Cintora committed
53

54
    kcmUi->deviceList->setIconSize(QSize(32,32));
55 56 57 58

    sortProxyModel = new DevicesSortProxyModel(devicesModel);

    kcmUi->deviceList->setModel(sortProxyModel);
Albert Vaca Cintora's avatar
Albert Vaca Cintora committed
59

60
    kcmUi->deviceInfo->setVisible(false);
Albert Vaca Cintora's avatar
Albert Vaca Cintora committed
61 62
    kcmUi->progressBar->setVisible(false);
    kcmUi->messages->setVisible(false);
Albert Vaca Cintora's avatar
Ooops  
Albert Vaca Cintora committed
63

64 65
    setButtons(KCModule::NoAdditionalButton);

66 67 68 69
    connect(devicesModel, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
            this, SLOT(resetSelection()));
    connect(kcmUi->deviceList, SIGNAL(pressed(QModelIndex)),
            this, SLOT(deviceSelected(QModelIndex)));
Albert Vaca Cintora's avatar
Albert Vaca Cintora committed
70 71 72 73
    connect(kcmUi->pair_button, SIGNAL(pressed()),
            this, SLOT(requestPair()));
    connect(kcmUi->unpair_button, SIGNAL(pressed()),
            this, SLOT(unpair()));
74 75 76
    connect(kcmUi->ping_button, SIGNAL(pressed()),
            this, SLOT(sendPing()));

Albert Vaca Cintora's avatar
Albert Vaca Cintora committed
77 78 79 80 81 82 83
}

KdeConnectKcm::~KdeConnectKcm()
{

}

84 85 86 87 88
void KdeConnectKcm::resetSelection()
{
    kcmUi->deviceList->selectionModel()->setCurrentIndex(sortProxyModel->mapFromSource(currentIndex), QItemSelectionModel::ClearAndSelect);
}

Albert Vaca Cintora's avatar
Albert Vaca Cintora committed
89

90
void KdeConnectKcm::deviceSelected(const QModelIndex& current)
Albert Vaca Cintora's avatar
Albert Vaca Cintora committed
91
{
92 93

    //Store previous device config
94 95
    pluginsConfigChanged();

96 97 98 99 100
    if (!current.isValid()) {
        kcmUi->deviceInfo->setVisible(false);
        return;
    }

101
    currentIndex = sortProxyModel->mapToSource(current);
102
    currentDevice = devicesModel->getDevice(currentIndex);
103

104
    bool valid = (currentDevice != NULL && currentDevice->isValid());
105
    kcmUi->deviceInfo->setVisible(valid);
106 107 108
    if (!valid) {
        return;
    }
109

Albert Vaca Cintora's avatar
Albert Vaca Cintora committed
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
    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);
        }
    }


130 131 132 133 134
    //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);

Albert Vaca Cintora's avatar
Albert Vaca Cintora committed
135
    kcmUi->name_label->setText(currentDevice->name());
Albert Vaca Cintora's avatar
i18n  
Albert Vaca Cintora committed
136
    kcmUi->status_label->setText(currentDevice->isPaired()? i18n("(paired)") : i18n("(unpaired)"));
137 138 139 140 141 142

    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());
Albert Vaca Cintora's avatar
i18n  
Albert Vaca Cintora committed
143
    kcmUi->pluginSelector->addPlugins(scriptinfos, KPluginSelector::ReadConfigFile, i18n("Plugins"), QString(), deviceConfig);
144

145 146
    connect(kcmUi->pluginSelector, SIGNAL(changed(bool)),
            this, SLOT(pluginsConfigChanged()));
Albert Vaca Cintora's avatar
Albert Vaca Cintora committed
147 148
}

Albert Vaca Cintora's avatar
Albert Vaca Cintora committed
149
void KdeConnectKcm::requestPair()
Albert Vaca Cintora's avatar
Albert Vaca Cintora committed
150
{
151
    if (!currentDevice) return;
Albert Vaca Cintora's avatar
Albert Vaca Cintora committed
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177

    kcmUi->messages->hide();

    kcmUi->pair_button->setVisible(false);
    kcmUi->progressBar->setVisible(true);

    connect(currentDevice,SIGNAL(pairingSuccesful()),
            this, SLOT(pairingSuccesful()));
    connect(currentDevice,SIGNAL(pairingFailed(QString)),
            this, SLOT(pairingFailed(QString)));

    currentDevice->requestPair();

}

void KdeConnectKcm::unpair()
{
    if (!currentDevice) return;

    kcmUi->pair_button->setVisible(true);
    kcmUi->unpair_button->setVisible(false);
    kcmUi->progressBar->setVisible(false);
    kcmUi->ping_button->setVisible(false);

    currentDevice->unpair();

Albert Vaca Cintora's avatar
i18n  
Albert Vaca Cintora committed
178
    kcmUi->status_label->setText(i18n("(unpaired)"));
Albert Vaca Cintora's avatar
Albert Vaca Cintora committed
179 180 181 182 183 184

    devicesModel->deviceStatusChanged(currentDevice->id());
}

void KdeConnectKcm::pairingFailed(const QString& error)
{
Albert Vaca Cintora's avatar
i18n  
Albert Vaca Cintora committed
185
    kcmUi->messages->setText(i18n("Error trying to pair: %1",error));
Albert Vaca Cintora's avatar
Albert Vaca Cintora committed
186 187 188 189 190 191 192 193 194 195 196 197
    kcmUi->messages->animatedShow();
    kcmUi->progressBar->setVisible(false);
    kcmUi->pair_button->setVisible(true);
}

void KdeConnectKcm::pairingSuccesful()
{
    kcmUi->progressBar->setVisible(false);
    kcmUi->unpair_button->setVisible(true);
    kcmUi->pair_button->setVisible(false);
    kcmUi->ping_button->setVisible(true);

Albert Vaca Cintora's avatar
i18n  
Albert Vaca Cintora committed
198
    kcmUi->status_label->setText(i18n("(paired)"));
Albert Vaca Cintora's avatar
Albert Vaca Cintora committed
199 200

    devicesModel->deviceStatusChanged(currentDevice->id());
201 202 203 204 205 206 207
}

void KdeConnectKcm::pluginsConfigChanged()
{
    //Store previous selection
    if (!currentDevice) return;

Albert Vaca Cintora's avatar
Albert Vaca Cintora committed
208 209
    DeviceDbusInterface* auxCurrentDevice = currentDevice;
    currentDevice = 0; //HACK to avoid infinite recursion (for some reason calling save on pluginselector emits changed)
210 211 212 213 214 215 216 217 218 219
    kcmUi->pluginSelector->save();
    currentDevice = auxCurrentDevice;

    currentDevice->reloadPlugins();
}

void KdeConnectKcm::save()
{
    pluginsConfigChanged();
    KCModule::save();
Albert Vaca Cintora's avatar
Albert Vaca Cintora committed
220 221
}

222
void KdeConnectKcm::sendPing()
Albert Vaca Cintora's avatar
Albert Vaca Cintora committed
223
{
224 225
    if (!currentDevice) return;
    currentDevice->sendPing();
Albert Vaca Cintora's avatar
Albert Vaca Cintora committed
226 227
}