/*
    <one line to give the program's name and a brief idea of what it does.>
    Copyright (C) <year>  <name of author>

    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 3 of the License, or
    (at your option) any later version.

    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 "createremotedevice.h"
#include <QDebug>
CreateRemoteDevice::CreateRemoteDevice(const QString& addr, QObject* parent):
    QThread(parent)
{
    m_addr = addr;
    Solid::Control::BluetoothManager &btmanager(Solid::Control::BluetoothManager::self());
    m_iface = new Solid::Control::BluetoothInterface(btmanager.defaultInterface());
}

void CreateRemoteDevice::run()
{
    const QString ubi = m_iface->getBluetoothRemoteDeviceUBI(m_addr);
    qDebug() << "Ubi: " << ubi;
    if(ubi != "") {
        qDebug() << "We've got a dev, calling removeDevice";
        Solid::Control::BluetoothRemoteDevice* dev = m_iface->findBluetoothRemoteDeviceUBI(ubi);
        emit deviceCreated(dev);
    } else {
        //Craeting the device so we can query it
        m_iface->createBluetoothRemoteDevice(m_addr);
        connect(m_iface, SIGNAL(deviceCreated(const QString &)), this, SLOT(slotDeviceCreated(const QString &)));
        exec();
    }
}

void CreateRemoteDevice::slotDeviceCreated(const QString& ubi)
{
    Solid::Control::BluetoothRemoteDevice* dev = m_iface->findBluetoothRemoteDeviceUBI(ubi);
    emit deviceCreated(dev);
}
