#include "bluetoothmain.h"

bool BlueToothMain::m_device_operating = false;
QString BlueToothMain::m_device_operating_address = "";
bool BlueToothMain::m_device_pin_flag = false;

typedef QPair<QString,qint16> fruit;
//设备显示排序
QVector <fruit> devShowListVec;

//dev-3.1 界面服务-分离
BlueToothMain::BlueToothMain(QWidget *parent)
    : QMainWindow(parent)
{
    if(QGSettings::isSchemaInstalled("org.ukui.bluetooth"))
    {
        m_settings = new QGSettings("org.ukui.bluetooth");
        QStringList btGsettingStrList = m_settings->keys();
        qDebug() << Q_FUNC_INFO << btGsettingStrList <<__LINE__;
        if (btGsettingStrList.contains(GSETTING_STR_ACTIVE_CONNECTION_TRAN))
            m_current_active_connection_status = m_settings->get(GSETTING_STR_ACTIVE_CONNECTION).toBool();
        qDebug() << Q_FUNC_INFO << "m_current_active_connection_status:" << m_current_active_connection_status << __LINE__;
        connect(m_settings,&QGSettings::changed,this,&BlueToothMain::gSettingsBluetoothChanged);
    }

    if(QGSettings::isSchemaInstalled("org.ukui.style")){
        styleSettings = new QGSettings("org.ukui.style");
        if(styleSettings->get("style-name").toString() == "ukui-black" ||
           styleSettings->get("style-name").toString() == "ukui-dark")
            isblack = true;
        else
            isblack = false;
        connect(styleSettings,&QGSettings::changed,this,&BlueToothMain::gSettingsChanged);
    }

    bool registerRes = registerClient();
    qInfo() << Q_FUNC_INFO << "test ===========================" << registerRes << __LINE__;

    initTimerLoadDevFunction();
    getAllAdapterData();

    _MCentralWidget = new QStackedWidget(this);
    this->setCentralWidget(_MCentralWidget);
    showBluetoothNormalMainWindow();
    showBluetoothErrorMainWindow();

    qDebug() << "adapter (addresss/name) size:" << m_adapter_address_list.size() << m_adapter_name_list.size();

    if (((Environment::HUAWEI == envPC) && !m_default_adapter_address.isEmpty()) ||
         (m_adapter_address_list.size() > 0 && m_adapter_name_list.size() == m_adapter_address_list.size()))
    {
        _MCentralWidget->setCurrentWidget(m_normal_main_widget);
        refreshUIWhenAdapterChanged();
    }
    else
    {
        _MCentralWidget->setCurrentWidget(m_error_main_widget);
    }
    monitorBluetoothDbusConnection();
}

BlueToothMain::~BlueToothMain()
{
    qDebug() << Q_FUNC_INFO << __LINE__;

    //注销控制面板事件
    unregisterClient();
    setDefaultAdapterScanOn(false);

    delete m_settings;
    m_settings = nullptr;
    delete styleSettings;
    styleSettings = nullptr;

    delete device_list;
    device_list = nullptr;
}

void BlueToothMain::leaveEvent(QEvent *event)
{
    //qDebug() << Q_FUNC_INFO;
}

void BlueToothMain::initTimerLoadDevFunction()
{
    devPreShow_timer = new QTimer(this);
    devPreShow_timer->setInterval(500);
    connect(devPreShow_timer,&QTimer::timeout,this,[=]
    {
        qDebug() << Q_FUNC_INFO << "++++++++++++++++++++++++++++++++\n"<< m_load_device_address_list << __LINE__;
        devPreShow_timer->stop();

        if (m_load_device_address_list.size() > 0)
        {
            bluetoothdevice * dev = createOneBluetoothDeviceFromMacAddress(m_load_device_address_list.at(0));
            if (nullptr == dev)
            {
                qDebug() << Q_FUNC_INFO <<__LINE__;
                m_load_device_address_list.removeAt(0);

            }
            else
            {
                if (dev->isPaired())
                    addMyDeviceItemUI(dev);
                else
                {
                    if(this->whetherToAddCurrentInterface(dev))
                        addOneBluetoothDeviceItemUi(dev);
                }
                m_load_device_address_list.removeAt(0);
                if (!m_default_bluetooth_adapter->m_bluetooth_device_list.contains(dev))
                    m_default_bluetooth_adapter->m_bluetooth_device_list.append(dev);
            }
            devPreShow_timer->start();
        }
        else
        {
            if (devPreShow_timer->isActive())
                devPreShow_timer->stop();
        }
    });
}

void BlueToothMain::monitorBluetoothDbusConnection()
{
    qDebug() << Q_FUNC_INFO << __LINE__;
    //    bool connect(const QString &service, const QString &path, const QString &interface,
    //                                         const QString &name, QObject *receiver, const char *slot);
    //1.适配器数据上报

    QDBusConnection::sessionBus().connect(SERVICE,
                                          PATH,
                                          INTERFACE,
                                          "defaultAdapterChanged",this, SLOT(reportDefaultAdapterChanged(QString)));

    QDBusConnection::sessionBus().connect(SERVICE,
                                          PATH,
                                          INTERFACE,
                                          "defaultAdapterNameChanged",this, SLOT(reportDefaultAdapterNameChanged(QString)));

    QDBusConnection::sessionBus().connect(SERVICE,
                                          PATH,
                                          INTERFACE,
                                          "adapterAddSignal",this, SLOT(reportAdapterAddSignal(QString)));
    QDBusConnection::sessionBus().connect(SERVICE,
                                          PATH,
                                          INTERFACE,
                                          "adapterRemoveSignal",this, SLOT(reportAdapterRemoveSignal(QString)));

    QDBusConnection::sessionBus().connect(SERVICE,
                                          PATH,
                                          INTERFACE,
                                          "defaultAdapterPowerChanged",this, SLOT(reportDefaultAdapterPowerChanged(bool)));
    QDBusConnection::sessionBus().connect(SERVICE,
                                          PATH,
                                          INTERFACE,
                                          "powerProgress",this, SLOT(reportPowerProgress(bool)));
    QDBusConnection::sessionBus().connect(SERVICE,
                                          PATH,
                                          INTERFACE,
                                          "defaultDiscoverableChanged",this, SLOT(reportDefaultDiscoverableChanged(bool)));

    QDBusConnection::sessionBus().connect(SERVICE,
                                          PATH,
                                          INTERFACE,
                                          "defaultScanStatusChanged",this, SLOT(reportDefaultScanStatusChanged(bool)));

    //2.蓝牙设备数据上报
    QDBusConnection::sessionBus().connect(SERVICE,
                                          PATH,
                                          INTERFACE,
                                          "deviceScanResult",this, SLOT(reportDeviceScanResult(QString,QString,QString,bool,qint16)));

    QDBusConnection::sessionBus().connect(SERVICE,
                                          PATH,
                                          INTERFACE,
                                          "devPairSignal",this, SLOT(reportDevPairSignal(QString,bool)));

    QDBusConnection::sessionBus().connect(SERVICE,
                                          PATH,
                                          INTERFACE,
                                          "devConnectStatusSignal",this, SLOT(reportDevConnectStatusSignal(QString,bool)));

    QDBusConnection::sessionBus().connect(SERVICE,
                                          PATH,
                                          INTERFACE,
                                          "devRemoveSignal",this, SLOT(reportDevRemoveSignal(QString)));

    QDBusConnection::sessionBus().connect(SERVICE,
                                          PATH,
                                          INTERFACE,
                                          "devMacAddressChangedSignal",this, SLOT(reportDevMacAddressChangedSignal(QString,QString)));

    QDBusConnection::sessionBus().connect(SERVICE,
                                          PATH,
                                          INTERFACE,
                                          "devNameChangedSignal",this, SLOT(reportDevNameChangedSignal(QString,QString)));

    QDBusConnection::sessionBus().connect(SERVICE,
                                          PATH,
                                          INTERFACE,
                                          "devTypeChangedSignal",this, SLOT(reportDevTypeChangedSignal(QString,QString)));

    QDBusConnection::sessionBus().connect(SERVICE,
                                          PATH,
                                          INTERFACE,
                                          "requestConfirmation",this, SLOT(reportRequestConfirmation(QString,QString)));

    //PIN窗口是否操作
    QDBusConnection::sessionBus().connect(SERVICE,
                                          PATH,
                                          INTERFACE,
                                          "replyRequestConfirmation",this, SLOT(reportReplyRequestConfirmation(bool)));



    QDBusConnection::sessionBus().connect(SERVICE,
                                          PATH,
                                          INTERFACE,
                                          "devOperateErrorSignal",this, SLOT(reportDevOperateErrorSignal(QString,int,QString)));


    QDBusConnection::sessionBus().connect(SERVICE,
                                          PATH,
                                          INTERFACE,
                                          "clearBluetoothDev",this, SLOT(reportClearBluetoothDev(QStringList)));

    QDBusConnection::sessionBus().connect(SERVICE,
                                          PATH,
                                          INTERFACE,
                                          "devRssiChangedSignal",this, SLOT(reportDevRssiChangedSignal(QString,qint16)));

    QDBusConnection::sessionBus().connect(SERVICE,
                                          PATH,
                                          INTERFACE,
                                          "devLaunchConnecting",this, SLOT(reportDevLaunchConnecting(QString)));



}

//adapter report start
void BlueToothMain::reportDefaultAdapterChanged(QString adapterAddress)
{
    qDebug () << Q_FUNC_INFO << "changed default:" << adapterAddress << __LINE__;
    if (adapterAddress.isEmpty() || adapterAddress.isNull())
    {
        qWarning() << Q_FUNC_INFO << "report adapter address is error!" << __LINE__;
        return ;
    }
    adapterListComboxEnabledState(true);
    qDebug () << Q_FUNC_INFO << "adapter name list:" << m_adapter_name_list << __LINE__;
    qDebug () << Q_FUNC_INFO << "adapter address list:" << m_adapter_address_list << __LINE__;

    int adapter_index = m_adapter_address_list.indexOf(adapterAddress);
    if (-1 != adapter_index)
    {
        if (nullptr != m_adapter_list_cmbox && m_adapter_list_cmbox->count() > adapter_index)
            m_adapter_list_cmbox->setCurrentIndex(adapter_index);
    }
}

void BlueToothMain::reportDefaultAdapterNameChanged(QString adapter_name)
{
    qDebug () << Q_FUNC_INFO << __LINE__;
    if(nullptr != m_default_bluetooth_adapter)
    {
        m_default_bluetooth_adapter->resetDeviceName(adapter_name);
        for(bluetoothadapter * adapter_dev : m_bluetooth_adapter_list)
        {
            if (adapter_dev->getDevAddress() == m_default_bluetooth_adapter->getDevAddress())
            {
                adapter_dev->resetDeviceName(adapter_name);
                break;
            }
        }
        emit defaultAdapterNameChanged(adapter_name);
    }
}

void BlueToothMain::reportDefaultDiscoverableChanged(bool discoverable)
{
    qInfo() << Q_FUNC_INFO
            << "m_current_adapter_disconvery_swtich:" << m_current_adapter_disconvery_swtich
            << "reportDefaultDiscoverableChanged:" << discoverable
            << "m_service_dbus_adapter_discovery_change_flag:" << m_service_dbus_adapter_discovery_change_flag
            << "m_discover_switch_btn->isChecked()" << m_discover_switch_btn->isChecked()
            << __LINE__;
    m_current_adapter_disconvery_swtich = discoverable;
    if (discoverable != m_discover_switch_btn->isChecked())
    {
        m_service_dbus_adapter_discovery_change_flag = true;
        m_discover_switch_btn->setChecked(m_current_adapter_disconvery_swtich);
    }
}

void BlueToothMain::reportDefaultScanStatusChanged(bool isScan)
{
    qDebug () << Q_FUNC_INFO << isScan << __LINE__;
    m_current_adapter_scan_status = isScan;

    if (isScan)
    {
        if(!loadLabel->isVisible())
            loadLabel->setVisible(true);
        if (!m_timer->isActive())
            m_timer->start();

        if(delayStartDiscover_timer->isActive())
            delayStartDiscover_timer->stop();
    }
    else
    {
        //if(loadLabel->isVisible())//设置为隐藏时无需判断，若夫窗口设置为false后界面下所有控件均为false
        loadLabel->setVisible(false);

        if (m_timer->isActive())
            m_timer->stop();
    }
}

bluetoothadapter * BlueToothMain::createOneBluetoothAdapter(QString adapter_address)
{
    qDebug () << Q_FUNC_INFO << adapter_address << __LINE__;
    bluetoothadapter * adapter_dev = nullptr ;

    //将等待值修改为300ms
    //建议基于事件定时获取，防止出现异常
    QString adapter_dev_name = getAdapterName(adapter_address);
    qDebug() << Q_FUNC_INFO << adapter_dev_name << __LINE__;
    if (adapter_dev_name.isEmpty())
    {
        if (adapter_dev_name.isEmpty())
        {
            qDebug() << Q_FUNC_INFO << "name is null!" << __LINE__;
            adapter_dev->deleteLater();
            return nullptr;
        }
    }

    //设备列表已经存在该设备
    for (bluetoothadapter * tmpDev : m_bluetooth_adapter_list)
    {
        if (adapter_address == tmpDev->getDevAddress())
        {
            qDebug() << Q_FUNC_INFO << "device existing!" << __LINE__;
            adapter_dev->deleteLater();
            return nullptr;
        }
    }

    adapter_dev = new bluetoothadapter(adapter_dev_name,
                                       adapter_address,
                                       false,
                                       false,
                                       false);

    qDebug () << Q_FUNC_INFO << adapter_address << "create ok!" << __LINE__;
    return adapter_dev;
}


void BlueToothMain::refreshBluetoothAdapterInterfaceUI()
{
    qInfo () << Q_FUNC_INFO << __LINE__;
    qInfo () << Q_FUNC_INFO << m_adapter_address_list << m_adapter_name_list <<  __LINE__;

    if (1 == m_adapter_address_list.size())
    {
        if("normalWidget" != _MCentralWidget->currentWidget()->objectName())
        {
            _MCentralWidget->setCurrentWidget(m_normal_main_widget);
        }
        if(frame_2->isVisible()) {
            frame_2->setVisible(false);
            line_frame2->setVisible(false);
        }
        frame_top->setMinimumSize(582,(Environment::LAIKA == envPC || Environment::MAVIS == envPC)?231:300);
    }
    else if (m_adapter_address_list.size() > 1)
    {
        if("normalWidget" != _MCentralWidget->currentWidget()->objectName())
        {
            _MCentralWidget->setCurrentWidget(m_normal_main_widget);
        }
        if(!frame_2->isVisible())
            frame_2->setVisible(true);
        if(!line_frame2->isVisible())
            line_frame2->setVisible(true);
        frame_top->setMinimumSize(582,(Environment::LAIKA == envPC || Environment::MAVIS == envPC)?300:369);

    }
    else
    {
        if("errorWidget" != _MCentralWidget->currentWidget()->objectName())
        {
            _MCentralWidget->setCurrentWidget(m_error_main_widget);
        }
    }

    refreshUIWhenAdapterChanged();
}

//void BlueToothMain::addBluetoothAdapterComboBox(QString adapterName)
//{

//}

//void BlueToothMain::removeBluetoothAdapterComboBox(int index)
//{

//}

void BlueToothMain::addAdapterDataList(QString adapterAddress)
{
    qDebug () << Q_FUNC_INFO  << adapterAddress << __LINE__;

    bluetoothadapter * adapter_dev = createOneBluetoothAdapter(adapterAddress);
    if (nullptr == adapter_dev)
    {
        qDebug () << Q_FUNC_INFO  << "adapter dev not add!" << __LINE__;
        return;
    }
    qDebug () << Q_FUNC_INFO  << "what's =======?" << __LINE__;
    qDebug () << Q_FUNC_INFO  << m_adapter_address_list << m_adapter_name_list << __LINE__;

    if (m_adapter_address_list.indexOf(adapterAddress) == -1)
    {
        m_adapter_address_list << adapterAddress;
        m_adapter_name_list << getAdapterName(adapterAddress);

        if(m_adapter_list_cmbox)
            m_adapter_list_cmbox->addItem(m_adapter_name_list.last());
    }
    qDebug () << Q_FUNC_INFO  << m_adapter_address_list << m_adapter_name_list << __LINE__;


    m_bluetooth_adapter_list.append(adapter_dev);
}

void BlueToothMain::removeAdapterDataList(QString adapterAddress)
{
    qDebug () << Q_FUNC_INFO  << adapterAddress << __LINE__;
    int i = m_adapter_address_list.indexOf(adapterAddress);
    if (-1 == i)
    {
        qDebug () << Q_FUNC_INFO  << "Not device :" << adapterAddress << __LINE__;
        return;
    }
    qDebug() << Q_FUNC_INFO << __LINE__ << m_adapter_address_list << m_adapter_name_list << i;
    m_adapter_name_list.removeAt(i);
    m_adapter_address_list.removeAll(adapterAddress);
    m_bluetooth_adapter_list.removeAt(i);

    disconnect(m_adapter_list_cmbox,SIGNAL(currentIndexChanged(int)),NULL,NULL);
    if(m_adapter_list_cmbox != nullptr)
    {
        m_adapter_list_cmbox->clear();
        m_adapter_list_cmbox->addItems(m_adapter_name_list);
        connect(m_adapter_list_cmbox,SIGNAL(currentIndexChanged(int)),this,SLOT(adapterComboxChanged(int)));
    }
}
void BlueToothMain::reportAdapterAddSignal(QString adapterAddress)
{
    qDebug () << Q_FUNC_INFO  << adapterAddress << __LINE__;

    if (!m_adapter_address_list.contains(adapterAddress))
    {
        addAdapterDataList(adapterAddress);
        refreshBluetoothAdapterInterfaceUI();
    }
}

void BlueToothMain::reportAdapterRemoveSignal(QString adapterAddress)
{
    qInfo () << Q_FUNC_INFO << adapterAddress << __LINE__;
    if (m_adapter_address_list.contains(adapterAddress))
    {
        qInfo () << Q_FUNC_INFO << adapterAddress<< "is exist! removing ..."<< __LINE__;
        removeAdapterDataList(adapterAddress);
        refreshBluetoothAdapterInterfaceUI();
    }
}

void BlueToothMain::reportPowerProgress(bool inProgress) {
    m_open_bluetooth_btn->setVisible(!inProgress);
    waitLabel->setVisible(inProgress);
    if (inProgress && !w_timer->isActive())
        w_timer->start();
    if (!inProgress && w_timer->isActive())
        w_timer->stop();
}

void BlueToothMain::reportDefaultAdapterPowerChanged(bool isPower)
{
    qDebug () << Q_FUNC_INFO << isPower << __LINE__;
    if("normalWidget" != _MCentralWidget->currentWidget()->objectName())
    {
        qDebug () << Q_FUNC_INFO << "is not normalWidget" << __LINE__;
        return;
    }

    m_current_adapter_power_swtich = isPower;

    if (w_timer->isActive())
        w_timer->stop();
    m_open_bluetooth_btn->show();
    waitLabel->hide();

    if (isPower && !m_open_bluetooth_btn->isChecked())
    {
        m_service_dbus_adapter_power_change_flag = true;
        m_open_bluetooth_btn->setChecked(true);
    }
    if (!isPower && m_open_bluetooth_btn->isChecked())
    {
        m_service_dbus_adapter_power_change_flag = true;
        m_open_bluetooth_btn->setChecked(false);
    }
}


//device report start
void BlueToothMain::reportDevPairSignal(QString deviceAddress,bool isPaired)
{
    qDebug() << Q_FUNC_INFO << deviceAddress << "isPaired:"<< isPaired << __LINE__;
    bool isNotExist = true ;
    for (bluetoothdevice * dev :m_default_bluetooth_adapter->m_bluetooth_device_list)
    {
        qDebug() << Q_FUNC_INFO << dev->getDevAddress() << __LINE__;
        if (deviceAddress == dev->getDevAddress())
        {
            dev->devPairedChanged(isPaired);
            isNotExist = false;
            break;
        }
    }
    qDebug() << Q_FUNC_INFO << deviceAddress << "isPaired:"<< isPaired << isNotExist << __LINE__;

    if (isPaired && isNotExist)
    {
        bluetoothdevice * dev = createOneBluetoothDeviceFromMacAddress(deviceAddress);

        if (nullptr != dev)
        {
            m_default_bluetooth_adapter->m_bluetooth_device_list.append(dev);
            addMyDeviceItemUI(dev);
        }
    }
}

void BlueToothMain::reportDevConnectStatusSignal(QString deviceAddress,bool isConnected)
{
    qDebug() << Q_FUNC_INFO << deviceAddress << "isConnected:"<< isConnected << __LINE__;
    for (bluetoothdevice * dev :m_default_bluetooth_adapter->m_bluetooth_device_list)
    {
        if (deviceAddress == dev->getDevAddress())
        {
            qDebug() << Q_FUNC_INFO << "find deviceAddress ok!" << __LINE__;
            dev->devConnectedChanged(isConnected);
            return;
        }
    }
}

void BlueToothMain::reportDevRemoveSignal(QString deviceAddress)
{
    qDebug () << Q_FUNC_INFO << deviceAddress << __LINE__;

    if(!m_load_device_address_list.isEmpty())
        m_load_device_address_list.removeAll(deviceAddress);

    if(m_device_operating && (m_device_operating_address == deviceAddress))
    {
        qDebug () << Q_FUNC_INFO << "dev is operating，no't remove" << __LINE__;
        return;
    }

    removeDeviceItemUI(deviceAddress);

    for (int i = 0 ; i < m_default_bluetooth_adapter->m_bluetooth_device_list.size() ; i++)
    {
        if(deviceAddress == m_default_bluetooth_adapter->m_bluetooth_device_list.at(i)->getDevAddress())
        {
            //m_default_bluetooth_adapter->m_bluetooth_device_list.removeAll(m_default_bluetooth_adapter->m_bluetooth_device_list.at(i));
            m_default_bluetooth_adapter->m_bluetooth_device_list.removeAt(i);
            break ;
        }
    }
}

void BlueToothMain::reportDevMacAddressChangedSignal(QString sourceMacAddress ,QString newMacAddress)
{
    qDebug() << Q_FUNC_INFO << "sourceMacAddress:" << sourceMacAddress << "newMacAddress:"<< newMacAddress << __LINE__;
    for (bluetoothdevice * dev :m_default_bluetooth_adapter->m_bluetooth_device_list)
    {
        if (sourceMacAddress == dev->getDevAddress() && !dev->isConnected())
        {
            dev->devMacAddressChanged(newMacAddress);
            qDebug() << Q_FUNC_INFO << "dev->getDevAddress:" << dev->getDevAddress() << __LINE__;
            DeviceInfoItem *item = frame_middle->findChild<DeviceInfoItem *>(sourceMacAddress);
            qDebug() << Q_FUNC_INFO << "new item ok!"  << __LINE__;
            if (item)
            {
                qDebug() << Q_FUNC_INFO << "item->setObjectName:" << item->objectName() << __LINE__;
                item->setObjectName(newMacAddress);
                qDebug() << Q_FUNC_INFO << "item->setObjectName:" << item->objectName() << __LINE__;
            }
            QFrame * line_item = frame_middle->findChild<QFrame *>("line-"+sourceMacAddress);
            qDebug() << Q_FUNC_INFO << "new line_item ok!"  << __LINE__;

            if (line_item)
            {
                qDebug() << Q_FUNC_INFO << "line_item->setObjectName:" << line_item->objectName() << __LINE__;
                line_item->setObjectName("line-"+newMacAddress);
                qDebug() << Q_FUNC_INFO << "line_item->setObjectName:" << line_item->objectName() << __LINE__;
            }

            //delete item;
            //delete line_item;
            return;
        }
    }
}

void BlueToothMain::reportDevNameChangedSignal(QString deviceAddress,QString changedName)
{
    qDebug() << Q_FUNC_INFO << deviceAddress << "changedName:"<< changedName << __LINE__;
    for (bluetoothdevice * dev :m_default_bluetooth_adapter->m_bluetooth_device_list)
    {
        if (deviceAddress == dev->getDevAddress())
        {
            dev->resetDeviceName(changedName);
            return;
        }
    }
}

void BlueToothMain::reportDevTypeChangedSignal(QString deviceAddress,QString changedType)
{
    qDebug() << Q_FUNC_INFO << deviceAddress << "changedType:"<< changedType << __LINE__;
    for (bluetoothdevice * dev :m_default_bluetooth_adapter->m_bluetooth_device_list)
    {
        if (deviceAddress == dev->getDevAddress())
        {
            bluetoothdevice::DEVICE_TYPE devType = getDeviceType(deviceAddress,changedType);
            dev->setDevType(devType);
            return;
        }
    }
}


void BlueToothMain::reportRequestConfirmation(QString deviceName,QString disPinCode)
{
    qDebug() << Q_FUNC_INFO << deviceName << disPinCode << __LINE__;
    m_device_pin_flag = true;
}
void BlueToothMain::reportReplyRequestConfirmation(bool userCheck)
{
    qDebug() << Q_FUNC_INFO << "User check：" << userCheck << __LINE__;
    m_device_pin_flag = false;
}

void BlueToothMain::reportDevOperateErrorSignal(QString deviceAddress , int ErrorId , QString ErrorText)
{
    qDebug() << Q_FUNC_INFO << deviceAddress << ErrorId << ErrorText << __LINE__;
    //m_device_pin_flag = false;
    for (bluetoothdevice * dev :m_default_bluetooth_adapter->m_bluetooth_device_list)
    {
        if (deviceAddress == dev->getDevAddress())
        {
            dev->setErrorInfo(ErrorId,ErrorText);
            return;
        }
    }
}

void BlueToothMain::reportClearBluetoothDev(QStringList devAddrList)
{
    qDebug() << Q_FUNC_INFO << devAddrList << __LINE__;
    QStringList rmDevAddrList;
    rmDevAddrList.clear();
    foreach (auto devAddr, devAddrList) {
        if(m_device_operating && (m_device_operating_address == devAddr))
            continue;
        rmDevAddrList.append(devAddr);
        reportDevRemoveSignal(devAddr);
    }

    qDebug() << Q_FUNC_INFO << rmDevAddrList << __LINE__;
    if (!rmDevAddrList.isEmpty())
        clearNonViableDevice(devAddrList);
}

void BlueToothMain::reportDevRssiChangedSignal(QString devAddr,qint16 rssi)
{
    qDebug() << Q_FUNC_INFO << devAddr << rssi << __LINE__;

    for (bluetoothdevice * dev :m_default_bluetooth_adapter->m_bluetooth_device_list)
    {
        if (devAddr == dev->getDevAddress())
        {
            if (dev->isPaired() || (m_device_operating && (devAddr == m_device_operating_address)))//|| dev->isConnected()
            {
                dev->setDevRssi(rssi);
            }
            else
            {
                if (rssi != dev->getDevRssi())
                {
                    //获取当前设备的显示位置
                    int DevAddrItemIndex =  getDevAddressItemIndex(dev->getDevAddress());
                    //获取当前设备的Rssi值需要显示的位置
                    int DevRssiInsertItemIndex =  getDevRssiItemInsertIndex(rssi);

                    //若当前显示的位置和需要加载的位置相同则不进行处理
                    if (DevAddrItemIndex == DevRssiInsertItemIndex)
                    {
                        dev->setDevRssi(rssi);
                    }
                    else
                    {
                        //删除原始位置
                        removeDeviceItemUI(devAddr);
                        dev->setDevRssi(rssi);
                        //重新加载设备显示位置
                        if(this->whetherToAddCurrentInterface(dev))
                            addOneBluetoothDeviceItemUi(dev);
                    }
                }
            }

            //找到设备跳出
            break;
        }
    }
}

void BlueToothMain::reportDevLaunchConnecting(QString devAddress)
{
    qDebug() << Q_FUNC_INFO << "Connecting dev address:" << devAddress << __LINE__;
    cacheDevTypeList->setEnabled(false);
    adapterListComboxEnabledState(false);
    for (bluetoothdevice * dev :m_default_bluetooth_adapter->m_bluetooth_device_list)
    {
        if (devAddress == dev->getDevAddress())
        {
            emit dev->devConnectingSignal();
            return;
        }
    }
}

bluetoothdevice * BlueToothMain::createOneBluetoothDeviceFromBluetoothService(
        QString devAddress,
        QString devName,
        QString type ,
        bool isSupportFileSend,
        qint16 devRssi)
{
    qDebug() << Q_FUNC_INFO << devName << devAddress << type << isSupportFileSend << __LINE__;
    bluetoothdevice * dev = nullptr ;
    if (devAddress.isEmpty() || devName.isEmpty())
    {
        qDebug() << Q_FUNC_INFO << "address or name is null!" << __LINE__;
        dev->deleteLater();
        return nullptr;
    }

    bluetoothdevice::DEVICE_TYPE dev_type = getDeviceType(devAddress,type);
    bool isPaired = getDevPairStatus(devAddress);
    bool isConnected = getDevConnectStatus(devAddress);
    if(isInvalidDevice(devName,dev_type))
    {
        qDebug() << Q_FUNC_INFO << "isInvalidDevice!" << __LINE__;
        dev->deleteLater();
        return nullptr;
    }
    //设备列表已经存在该设备
    for (bluetoothdevice * tmpDev : m_default_bluetooth_adapter->m_bluetooth_device_list)
    {
        if (devAddress == tmpDev->getDevAddress())
        {
            qDebug() << Q_FUNC_INFO << "device existing!" << __LINE__;
            dev->deleteLater();
            return nullptr;
        }
    }

    dev = new bluetoothdevice(devName,
                              devAddress,
                              dev_type,
                              isPaired,
                              isConnected,
                              isPaired,
                              devRssi);

    dev->setDevSendFileMark(isSupportFileSend);

    return dev;
}


bluetoothdevice * BlueToothMain::createOneBluetoothDeviceFromMacAddress(QString devAddress)
{
    //qInfo() << Q_FUNC_INFO  << __LINE__;
    bluetoothdevice * dev = nullptr ;

    QString dev_name = getDevName(devAddress);
    if (dev_name.isEmpty())
    {
        //dev_name = "Bluez 5.53";
        dev->deleteLater();
        return nullptr;
    }

    qInfo() << Q_FUNC_INFO << devAddress << dev_name << __LINE__;
    if (dev_name.isEmpty())
    {
        qDebug() << Q_FUNC_INFO << "name is null!" << __LINE__;
        dev->deleteLater();
        return nullptr;
    }

    bluetoothdevice::DEVICE_TYPE dev_type = getDeviceType(devAddress,"");
    bool isPaired = getDevPairStatus(devAddress);
    bool isConnected = getDevConnectStatus(devAddress);
    qint16 devRssi = getDevRssi(devAddress);
    //qInfo() << Q_FUNC_INFO << devAddress << isPaired << isConnected << __LINE__;

    if(isInvalidDevice(dev_name,dev_type))
    {
        qInfo() << Q_FUNC_INFO << "isInvalidDevice!" << __LINE__;
        dev->deleteLater();
        return nullptr;
    }

    //设备列表已经存在该设备
    for (bluetoothdevice * tmpDev : m_default_bluetooth_adapter->m_bluetooth_device_list)
    {
        if (devAddress == tmpDev->getDevAddress())
        {
            qInfo() << Q_FUNC_INFO << "device existing!" << __LINE__;
            dev->deleteLater();
            return nullptr;
        }
    }

    dev = new bluetoothdevice(dev_name,
                              devAddress,
                              dev_type,
                              isPaired,
                              isConnected,
                              isPaired,
                              devRssi);

    if (bluetoothdevice::computer == dev->getDevType() ||
        bluetoothdevice::phone == dev->getDevType())
    {
         bool isSupportFileSend = getDevSupportFileSend(devAddress);
         dev->setDevSendFileMark(isSupportFileSend);
    }
    else
    {
        dev->setDevSendFileMark(false);
    }
    return dev;
}

void BlueToothMain::reportDeviceScanResult(QString devAddress,QString devName,QString type ,bool isSupportFileSend , qint16 devRssi)
{
    qDebug () << Q_FUNC_INFO << devName << devAddress << type <<isSupportFileSend << devRssi << __LINE__;

    if ("normalWidget" != _MCentralWidget->currentWidget()->objectName())
    {
        qDebug() << Q_FUNC_INFO << "errorWidget not add device!!" << __LINE__;
        return;
    }
    else
    {
        if (nullptr != longTimeNoDevAddSignal_timer)
        {
            longTimeNoDevAddSignal_timer->stop();
            longTimeNoDevAddSignal_timer->start();
        }
    }

    qDebug () << Q_FUNC_INFO << devName << "m_current_adapter_scan_status =========" << m_current_adapter_scan_status << __LINE__;

    //当前是扫描状态，且扫描延时也在计时
    if (m_current_adapter_scan_status && delayStartDiscover_timer && delayStartDiscover_timer->isActive())
    {
        delayStartDiscover_timer->stop();
    }
    //设备列表已经存在该设备,添加到界面中去
    for (bluetoothdevice * tmpDev : m_default_bluetooth_adapter->m_bluetooth_device_list)
    {
        if (devAddress == tmpDev->getDevAddress())
        {
            if(!tmpDev->isPaired() && whetherToAddCurrentInterface(tmpDev))
            {
                addOneBluetoothDeviceItemUi(tmpDev);
                qDebug () << Q_FUNC_INFO << "device list is exist,add item ui!";
                return;
            }
        }
    }

    bluetoothdevice * device = createOneBluetoothDeviceFromBluetoothService(devAddress,devName,type,isSupportFileSend,devRssi);
    if (device == nullptr)
    {
        qDebug () << Q_FUNC_INFO << "device not add!";
        return;
    }

    qDebug() << "effective device";
    device->setObjectName(devAddress);

    m_default_bluetooth_adapter->m_bluetooth_device_list.append(device);
    if(device->isPaired())
    {
        this->addMyDeviceItemUI(device);
    }
    else
    {
        if(this->whetherToAddCurrentInterface(device))
            this->addOneBluetoothDeviceItemUi(device);
    }


}

void BlueToothMain::addMyDeviceItemUI(bluetoothdevice * device)
{
    qInfo() << __FUNCTION__
            << "adapter_address:" << m_default_adapter_address
            << "device_name:" << device->getDevName()
            << "device_address:" << device->getDevAddress()
            << "device_type:" << device->getDevType() << __LINE__;

    DeviceInfoItem *item  = frame_middle->findChild<DeviceInfoItem *>(device->getDevAddress());
    if (item)
    {
        m_myDev_show_flag = true;
        qInfo () << Q_FUNC_INFO << "device is exist" << __LINE__;
        return;
    }

    if (device && device->isPaired()) {

        mDevFrameAddLineFrame(0,"paired",device->getDevAddress());

        DeviceInfoItem *item = new DeviceInfoItem(mDev_frame,device);
        item->setObjectName(device->getDevAddress());
        connect(item,SIGNAL(devPaired(QString)),this,SLOT(changeDeviceParentWindow(QString)));
        connect(item,SIGNAL(devConnect(QString)),this,SLOT(receiveConnectsignal(QString)));
        connect(item,SIGNAL(devDisconnect(QString)),this,SLOT(receiveDisConnectSignal(QString)));
        connect(item,SIGNAL(devRemove(QString)),this,SLOT(receiveRemoveSignal(QString)));
        connect(item,SIGNAL(devSendFiles(QString)),this,SLOT(receiveSendFileSignal(QString)));
        connect(item,&DeviceInfoItem::devConnectionComplete,this,[=]
        {
            m_device_operating = false;
            m_device_operating_address.clear();
            cacheDevTypeList->setEnabled(true);
            adapterListComboxEnabledState(true);
        });
        m_myDev_show_flag = true;

        paired_dev_layout->addWidget(item,Qt::AlignTop);
    }
}
bool BlueToothMain::whetherToAddCurrentInterface(bluetoothdevice * device)
{
    switch (discoverDevFlag) {
    case DevTypeShow::All:
        return true;
    case DevTypeShow::Audio:
        if (device->getDevType() == bluetoothdevice::DEVICE_TYPE::headset ||
            device->getDevType() == bluetoothdevice::DEVICE_TYPE::headphones ||
            device->getDevType() == bluetoothdevice::DEVICE_TYPE::audiovideo) {
            return true;
        } else {
            return false;
        }
        break;
    case DevTypeShow::Peripherals:
        if (device->getDevType() == bluetoothdevice::DEVICE_TYPE::mouse ||
            device->getDevType() == bluetoothdevice::DEVICE_TYPE::keyboard) {
            return true;
        } else {
            return false;
        }
        break;
    case DevTypeShow::PC:
        if (device->getDevType() == bluetoothdevice::DEVICE_TYPE::computer) {
            return true;
        } else {
            return false;
        }
        break;
    case DevTypeShow::Phone:
        if (device->getDevType() == bluetoothdevice::DEVICE_TYPE::phone) {
            return true;
        } else {
            return false;
        }
        break;
    default:
        if (device->getDevType() == bluetoothdevice::DEVICE_TYPE::headset ||
            device->getDevType() == bluetoothdevice::DEVICE_TYPE::headphones ||
            device->getDevType() == bluetoothdevice::DEVICE_TYPE::audiovideo ||
            device->getDevType() == bluetoothdevice::DEVICE_TYPE::phone ||
            device->getDevType() == bluetoothdevice::DEVICE_TYPE::mouse ||
            device->getDevType() == bluetoothdevice::DEVICE_TYPE::keyboard ||
            device->getDevType() == bluetoothdevice::DEVICE_TYPE::computer) {
            return false;
        }
        return true;
    }
}

void BlueToothMain::addOneBluetoothDeviceItemUi(bluetoothdevice * device)
{
    qInfo() << Q_FUNC_INFO << __LINE__;
    qInfo() << Q_FUNC_INFO
            << "adapter_address:" << m_default_adapter_address
            << "device_name:" << device->getDevName()
            << "device_address:" << device->getDevAddress()
            << "device list:" << device_list
            << __LINE__;

    DeviceInfoItem * item  = frame_bottom->findChild<DeviceInfoItem *>(device->getDevAddress());
    if (item)
    {
        qInfo () << Q_FUNC_INFO << "device is exist" << __LINE__;
        return;
    }

    item = new DeviceInfoItem(device_list,device);
    item->setObjectName(device->getDevAddress());
    connect(item,SIGNAL(devPaired(QString)),this,SLOT(changeDeviceParentWindow(QString)));
    connect(item,SIGNAL(devConnect(QString)),this,SLOT(receiveConnectsignal(QString)));
    connect(item,SIGNAL(devDisconnect(QString)),this,SLOT(receiveDisConnectSignal(QString)));
    connect(item,SIGNAL(devRemove(QString)),this,SLOT(receiveRemoveSignal(QString)));
    connect(item,SIGNAL(devSendFiles(QString)),this,SLOT(receiveSendFileSignal(QString)));
    connect(item,&DeviceInfoItem::devConnectionComplete,this,[=]
    {
        m_device_operating = false;
        m_device_operating_address.clear();
        cacheDevTypeList->setEnabled(true);
        adapterListComboxEnabledState(true);
    });

    if (!device_list->isVisible())
        device_list->setVisible(true);

    int insert_index = getDevRssiItemInsertIndex(device->getDevRssi());
    //qInfo () << Q_FUNC_INFO << "insert_index:"<< insert_index << __LINE__;

    if (insert_index == -1)
    {
        devShowListVec.append(fruit(device->getDevAddress(),device->getDevRssi()));
        insert_index = device_list_layout->count();
    }
    else
    {
        devShowListVec.insert(insert_index,fruit(device->getDevAddress(),device->getDevRssi()));
        insert_index = insert_index*2;
        if (insert_index > device_list_layout->count())
            insert_index = device_list_layout->count();
    }

    mDevFrameAddLineFrame(insert_index,"other",device->getDevAddress());
    device_list_layout->insertWidget(insert_index,item,Qt::AlignTop);
}

int BlueToothMain::getDevAddressItemIndex(QString devAddress)
{
    qDebug () << Q_FUNC_INFO << "devAddress:"<< devAddress << __LINE__;

    for(auto it = devShowListVec.begin() ; it != devShowListVec.end() ; ++it)
    {
        //qDebug () << Q_FUNC_INFO << "it->first(address):"<< it->first << "it->second(rssi):"<< it->second << __LINE__;
        if (it->first == devAddress)
        {
            //qDebug () << Q_FUNC_INFO << "========index:"<< devShowListVec.indexOf(fruit(it->first,it->second)) << __LINE__;
            return (devShowListVec.indexOf(fruit(it->first,it->second)));
        }
    }
    return -1;
}


int BlueToothMain::getDevRssiItemInsertIndex(qint16 currDevRssi)
{
    qDebug () << Q_FUNC_INFO << "currDevRssi："<< currDevRssi << __LINE__;
    //qDebug () << Q_FUNC_INFO << "devShowListVec:"<< devShowListVec << __LINE__;

    for(auto it = devShowListVec.begin() ; it != devShowListVec.end() ; ++it)
    {
        //qDebug () << Q_FUNC_INFO << "it->first(address):"<< it->first << "it->second(rssi):"<< it->second << __LINE__;
        if (it->second <= currDevRssi)
        {
            //qDebug () << Q_FUNC_INFO << "========index:"<< devShowListVec.indexOf(fruit(it->first,it->second)) << __LINE__;
            return (devShowListVec.indexOf(fruit(it->first,it->second)));
        }
    }
    return -1;
}

void BlueToothMain::getDefaultAdapterData(QString adapter_address)
{
    qDebug () << Q_FUNC_INFO << __LINE__;
    //m_current_bluetooth_block_status    = getBluetoothBlock();
    m_default_adapter_name              = getAdapterName(adapter_address);
    m_current_adapter_power_swtich      = getDefaultAdapterPower();
    m_current_adapter_scan_status       = getDefaultAdapterScanStatus();
    m_current_adapter_disconvery_swtich = getDefaultAdapterDiscoverable();

    m_default_bluetooth_adapter         = new bluetoothadapter(m_default_adapter_name,
                                                               m_default_adapter_address,
                                                               m_current_adapter_power_swtich,
                                                               m_current_adapter_scan_status,
                                                               m_current_adapter_disconvery_swtich);
    m_default_bluetooth_adapter->m_bluetooth_device_list.clear();

    //加载已配对列表
    m_default_adapter_paired_device_address_list = getDefaultAdapterPairedDevAddress();
    for (auto deviceAddress: m_default_adapter_paired_device_address_list)
    {
        bluetoothdevice * dev = createOneBluetoothDeviceFromMacAddress(deviceAddress);
        if (nullptr == dev)
        {
            qDebug() << Q_FUNC_INFO << __LINE__;
            continue ;
        }
        m_default_bluetooth_adapter->m_bluetooth_device_list.append(dev);
    }

    //加载蓝牙列表
    m_discovery_device_address_all_list = getDefaultAdapterCacheDevAddress();
    int firstStartCount = 0;
    m_load_device_address_list.clear();

    for (auto deviceAddress: m_discovery_device_address_all_list)
    {
        firstStartCount++;
        if (firstStartCount < 5)
        {
            bluetoothdevice * dev = createOneBluetoothDeviceFromMacAddress(deviceAddress);
            if (nullptr == dev)
            {
                qDebug() << Q_FUNC_INFO <<__LINE__;
                continue ;
                qDebug() << Q_FUNC_INFO <<__LINE__;
            }
            qDebug() << Q_FUNC_INFO <<__LINE__;
            m_default_bluetooth_adapter->m_bluetooth_device_list.append(dev);
            qDebug() << Q_FUNC_INFO <<__LINE__;

        }
        else
        {
            m_load_device_address_list.append(deviceAddress);
        }
        qDebug() << Q_FUNC_INFO <<__LINE__;
    }

    //测试代码
    if (m_load_device_address_list.size() > 0)
    {
        if(devPreShow_timer->isActive())
            devPreShow_timer->stop();

        devPreShow_timer->start();
    }
    qDebug () << Q_FUNC_INFO << "end" << __LINE__;

}

void BlueToothMain::connectBluetoothServiceSignal()
{
    qDebug () << Q_FUNC_INFO << __LINE__;
    QDBusInterface iface(SERVICE,
                         PATH,
                         INTERFACE,
                         QDBusConnection::sessionBus());
    iface.setTimeout(300);
    iface.call("connectSignal");
}

void BlueToothMain::getAllAdapterData()
{
    qDebug () << Q_FUNC_INFO << __LINE__;
    m_default_bluetooth_adapter = nullptr;

    m_default_adapter_address           = getDefaultAdapterAddress();
    if(m_default_adapter_address.isEmpty())
    {
        m_default_bluetooth_adapter = nullptr;
        return;
    }

    getDefaultAdapterData(m_default_adapter_address);

    qDebug () << Q_FUNC_INFO << "m_default_bluetooth_adapter->m_bluetooth_device_list.size():" << m_default_bluetooth_adapter->m_bluetooth_device_list.size();

    m_adapter_address_list              = getAdapterDevAddressList();
    m_adapter_name_list                 = getAdapterDevNameList();

    qDebug () << Q_FUNC_INFO << "m_adapter_address_list size :" << m_adapter_address_list.size() << __LINE__;
    for(QString adapterAddress:m_adapter_address_list)
    {
        QString adapterName = getAdapterName(adapterAddress);
        if(adapterAddress == m_default_adapter_address)
            m_bluetooth_adapter_list.append(m_default_bluetooth_adapter);
        else
        {
            bluetoothadapter * adapterdev = new bluetoothadapter(adapterName,adapterAddress,false,false,false);
            m_bluetooth_adapter_list.append(adapterdev);
        }
    }

    qDebug () << Q_FUNC_INFO << "m_current_bluetooth_block_status: " << m_current_bluetooth_block_status << __LINE__;
    qDebug () << Q_FUNC_INFO << "m_default_adapter_address: " << m_default_adapter_address << __LINE__;
    qDebug () << Q_FUNC_INFO << "m_adapter_address_list: " << m_adapter_address_list << __LINE__;
    qDebug () << Q_FUNC_INFO << "m_default_adapter_name: " << m_default_adapter_name << __LINE__;
    qDebug () << Q_FUNC_INFO << "m_adapter_name_list: " << m_adapter_name_list << __LINE__;
    qDebug () << Q_FUNC_INFO << "m_current_adapter_power_swtich: " << m_current_adapter_power_swtich << __LINE__;
    qDebug () << Q_FUNC_INFO << "m_current_adapter_disconvery_swtich: " << m_current_adapter_disconvery_swtich << __LINE__;
    qDebug () << Q_FUNC_INFO << "m_current_adapter_scan_status: " << m_current_adapter_scan_status << __LINE__;
}

bool BlueToothMain::getBluetoothBlock()
{
    qDebug () << Q_FUNC_INFO << __LINE__;
    QDBusInterface iface(SERVICE,
                         PATH,
                         INTERFACE,
                         QDBusConnection::sessionBus());
    //设置超时时间
    iface.setTimeout(300);
    QDBusReply<bool> reply=iface.call("getBluetoothBlock");
    //qDebug () << Q_FUNC_INFO << "==========================" << reply << __LINE__;
    return reply;
}

QString BlueToothMain::getDefaultAdapterAddress()
{
    qDebug () << Q_FUNC_INFO << __LINE__;

    QDBusInterface iface(SERVICE,
                         PATH,
                         INTERFACE,
                         QDBusConnection::sessionBus());
    //设置超时时间
    iface.setTimeout(300);
    QDBusReply<QString> reply=iface.call("getDefaultAdapterAddress");
    //qDebug () << Q_FUNC_INFO << "==========================" << reply << __LINE__;
    return reply;
}

QStringList BlueToothMain::getAdapterDevAddressList()
{
    qDebug () << Q_FUNC_INFO << __LINE__;

    QDBusInterface iface(SERVICE,
                         PATH,
                         INTERFACE,
                         QDBusConnection::sessionBus());
    //设置超时时间
    iface.setTimeout(300);
    QDBusReply<QStringList> reply=iface.call("getAdapterDevAddressList");
    //qDebug () << Q_FUNC_INFO << "==========================" << reply << __LINE__;
    return reply;
}

QStringList BlueToothMain::getAdapterDevNameList()
{
    qDebug () << Q_FUNC_INFO << __LINE__;

    QStringList adapterNameList ;
    adapterNameList.clear();
    for (QString adapterAddress : m_adapter_address_list)
    {
        adapterNameList << getAdapterName(adapterAddress);
    }
    qDebug () << Q_FUNC_INFO << adapterNameList << __LINE__;
    return adapterNameList;
}

QString BlueToothMain::getAdapterName(QString adapterAddress)
{
    qDebug () << Q_FUNC_INFO << "get adapter name address:" << adapterAddress << __LINE__;

    QDBusInterface iface(SERVICE,
                         PATH,
                         INTERFACE,
                         QDBusConnection::sessionBus());
    //设置超时时间, 从20ms增加300ms,减少出现异常的几率
    iface.setTimeout(300);
    QDBusReply<QString> reply=iface.call("getAdapterNameByAddr", adapterAddress);
    //qDebug () << Q_FUNC_INFO << "==========================" << reply << __LINE__;
    return reply;
}

bool BlueToothMain::getDefaultAdapterPower()
{
    qDebug () << Q_FUNC_INFO << __LINE__;

    QDBusInterface iface(SERVICE,
                         PATH,
                         INTERFACE,
                         QDBusConnection::sessionBus());
    //设置超时时间 ms
    iface.setTimeout(300);
    QDBusReply<bool> reply=iface.call("getDefaultAdapterPower");
    //qDebug () << Q_FUNC_INFO << "==========================" << reply << __LINE__;
    return reply;
}

QStringList BlueToothMain::getDefaultAdapterPairedDevAddress()
{
    qDebug () << Q_FUNC_INFO << __LINE__;

    QDBusInterface iface(SERVICE,
                         PATH,
                         INTERFACE,
                         QDBusConnection::sessionBus());
    //设置超时时间, ms
    iface.setTimeout(300);
    QDBusReply<QStringList> reply=iface.call("getDefaultAdapterPairedDevAddress");
    //qDebug () << Q_FUNC_INFO << "==========================" << reply << __LINE__;
    return reply;
}

QStringList BlueToothMain::getDefaultAdapterCacheDevAddress()
{
    qDebug () << Q_FUNC_INFO << __LINE__;

    QDBusInterface iface(SERVICE,
                         PATH,
                         INTERFACE,
                         QDBusConnection::sessionBus());
    //设置超时时间, ms
    iface.setTimeout(300);
    QDBusReply<QStringList> reply=iface.call("getDefaultAdapterCacheDevAddress");
    //qDebug () << Q_FUNC_INFO << "==========================" << reply << __LINE__;
    return reply;
}

bool BlueToothMain::getDefaultAdapterDiscoverable()
{
    qDebug () << Q_FUNC_INFO << __LINE__;

    QDBusInterface iface(SERVICE,
                         PATH,
                         INTERFACE,
                         QDBusConnection::sessionBus());
    //设置超时时间
    iface.setTimeout(300);
    QDBusReply<bool> reply=iface.call("getDefaultAdapterDiscoverable");
    //qDebug () << Q_FUNC_INFO << "==========================" << reply << __LINE__;
    return reply;
}

bool BlueToothMain::getDefaultAdapterScanStatus()
{
    qDebug () << Q_FUNC_INFO << __LINE__;
    QDBusInterface iface(SERVICE,
                         PATH,
                         INTERFACE,
                         QDBusConnection::sessionBus());
    //设置超时时间,从20ms延迟至300ms
    iface.setTimeout(300);
    QDBusReply<bool> reply=iface.call("getDefaultAdapterScanStatus");
    //qDebug () << Q_FUNC_INFO << "==========================" << reply << __LINE__;
    return reply;
}


QString BlueToothMain::getDevName(QString deviceAddress)
{
    qDebug () << Q_FUNC_INFO << __LINE__;
    QDBusInterface iface(SERVICE,
                         PATH,
                         INTERFACE,
                         QDBusConnection::sessionBus());
    //设置超时时间,从20ms延长至300ms
    iface.setTimeout(300);
    QDBusReply<QString> reply=iface.call("getDevName",deviceAddress);
    //qDebug () << Q_FUNC_INFO << "==========================" << reply << __LINE__;
    return reply;
}

bool BlueToothMain::getDevPairStatus(QString deviceAddress)
{
    qDebug () << Q_FUNC_INFO << __LINE__;
    QDBusInterface iface(SERVICE,
                         PATH,
                         INTERFACE,
                         QDBusConnection::sessionBus());
    //设置超时时间
    iface.setTimeout(300);
    QDBusReply<bool> reply=iface.call("getDevPairStatus",deviceAddress);
    //qDebug () << Q_FUNC_INFO << "==========================" << reply << __LINE__;
    return reply;
}
bool BlueToothMain::getDevConnectStatus(QString deviceAddress)
{
    qDebug () << Q_FUNC_INFO << __LINE__;
    QDBusInterface iface(SERVICE,
                         PATH,
                         INTERFACE,
                         QDBusConnection::sessionBus());
    //设置超时时间
    iface.setTimeout(300);
    QDBusReply<bool> reply=iface.call("getDevConnectStatus",deviceAddress);
    //qDebug () << Q_FUNC_INFO << "==========================" << reply << __LINE__;
    return reply;
}

bool BlueToothMain::getDevSupportFileSend(QString deviceAddress)
{
    qDebug () << Q_FUNC_INFO << __LINE__;
    QDBusInterface iface(SERVICE,
                         PATH,
                         INTERFACE,
                         QDBusConnection::sessionBus());
    //设置超时时间
    iface.setTimeout(300);
    QDBusReply<bool> reply=iface.call("getDevSupportFileSend",deviceAddress);
    //qDebug () << Q_FUNC_INFO << "==========================" << reply << __LINE__;
    return reply;
}

qint16 BlueToothMain::getDevRssi(QString deviceAddress)
{
    qDebug () << Q_FUNC_INFO << __LINE__;
    QDBusInterface iface(SERVICE,
                         PATH,
                         INTERFACE,
                         QDBusConnection::sessionBus());
    //设置超时时间
    iface.setTimeout(300);
    QDBusReply<qint16> reply=iface.call("getDevRssi",deviceAddress);
    //qDebug () << Q_FUNC_INFO << "==========================" << reply << __LINE__;
    return reply;
}

bluetoothdevice::DEVICE_TYPE BlueToothMain::getDeviceType(QString deviceAddress , QString devType)
{
    qDebug () << Q_FUNC_INFO << __LINE__;
    QString dev_type_str = devType ;
    if (dev_type_str.isEmpty())
        dev_type_str = getDevType(deviceAddress);

    //qDebug() << Q_FUNC_INFO << "Type:" << dev_type_str << __LINE__;
    QByteArray enum_str_to_char = dev_type_str.toLatin1();
    const char * enum_char = enum_str_to_char.data();
    QMetaEnum metaEnum = QMetaEnum::fromType<bluetoothdevice::DEVICE_TYPE>();
    //qDebug() << Q_FUNC_INFO << "enum index:" << metaEnum.keysToValue(enum_char);

    return (metaEnum.keysToValue(enum_char) == -1 ? bluetoothdevice::DEVICE_TYPE::uncategorized : bluetoothdevice::DEVICE_TYPE(metaEnum.keysToValue(enum_char)));
}
QString BlueToothMain::getDevType(QString deviceAddress)
{
    qDebug () << Q_FUNC_INFO << __LINE__;
    QDBusInterface iface(SERVICE,
                         PATH,
                         INTERFACE,
                         QDBusConnection::sessionBus());
    //设置超时时间
    iface.setTimeout(300);
    QDBusReply<QString> reply=iface.call("getDevType",deviceAddress);
    //qDebug () << Q_FUNC_INFO << "==========================" << reply << __LINE__;

    return reply;
}

void BlueToothMain::clearAllUnPairedDevicelist()
{
    qDebug () << Q_FUNC_INFO << __LINE__;

    QDBusInterface iface(SERVICE,
                         PATH,
                         INTERFACE,
                         QDBusConnection::sessionBus());
    //设置超时时间
    iface.call(QDBus::NoBlock, "clearAllUnPairedDevicelist");
    //qDebug () << Q_FUNC_INFO << "==========================" << reply << __LINE__;
    //return reply;
}

void BlueToothMain::clearNonViableDevice(QStringList devAddrList)
{
    qDebug () << Q_FUNC_INFO << __LINE__;

    QDBusMessage m = QDBusMessage::createMethodCall(SERVICE,
                                                    PATH,
                                                    INTERFACE,
                                                    "clearNonViableDevice");
    m << devAddrList;
    qDebug() << Q_FUNC_INFO << m.arguments().at(0).value<QStringList>() <<__LINE__;
    QDBusConnection::sessionBus().call(m, QDBus::NoBlock);
}

void BlueToothMain::setTrayVisible(bool value)
{
    qDebug() << Q_FUNC_INFO << "user check: " << value << __LINE__;
    m_settings->set("tray-show",QVariant::fromValue(value));
}

void BlueToothMain::setBluetoothBlock(bool value)
{
    qDebug() << Q_FUNC_INFO << __LINE__;
    QDBusMessage m = QDBusMessage::createMethodCall(SERVICE,
                                                    PATH,
                                                    INTERFACE,
                                                    "setBluetoothBlock");
    m << value;
    qDebug() << Q_FUNC_INFO << m.arguments().at(0).value<bool>() <<__LINE__;
    QDBusConnection::sessionBus().call(m, QDBus::NoBlock);
}

void BlueToothMain::setDefaultAdapterName(QString adapterName)
{
    qDebug() << Q_FUNC_INFO << __LINE__;
    QDBusMessage m = QDBusMessage::createMethodCall(SERVICE,
                                                    PATH,
                                                    INTERFACE,
                                                    "setDefaultAdapterName");//setBluetoothName
    m << adapterName;
    qDebug() << Q_FUNC_INFO << m.arguments().at(0).value<QString>() <<__LINE__;
    // 发送Message
    QDBusConnection::sessionBus().call(m, QDBus::NoBlock);

}

void BlueToothMain::setDefaultAdapterPower(bool value)
{
    qDebug() << Q_FUNC_INFO << __LINE__;
    QDBusMessage m = QDBusMessage::createMethodCall(SERVICE,
                                                    PATH,
                                                    INTERFACE,
                                                    "setDefaultAdapterPower");
    m << value;
    qDebug() << Q_FUNC_INFO << m.arguments().at(0).value<bool>() <<__LINE__;
    // 发送Message
    QDBusConnection::sessionBus().call(m, QDBus::NoBlock);
}

void BlueToothMain::setDefaultAdapter(QString adapterAddress)
{
    
    qDebug() << Q_FUNC_INFO << __LINE__;
    QDBusMessage m = QDBusMessage::createMethodCall(SERVICE,
                                                    PATH,
                                                    INTERFACE,
                                                    "setDefaultAdapter");
    m << adapterAddress;
    qDebug() << Q_FUNC_INFO << m.arguments().at(0).value<QString>() <<__LINE__;
    // 发送Message
    QDBusConnection::sessionBus().call(m, QDBus::NoBlock);
}

void BlueToothMain::setDefaultAdapterDiscoverable(bool value)
{
    qInfo() << Q_FUNC_INFO
            << "setDefaultAdapterDiscoverable:" << value
            <<"m_service_dbus_adapter_discovery_change_flag:" << m_service_dbus_adapter_discovery_change_flag
           << __LINE__;

    //同步可被发现开关的状态
    //m_current_adapter_disconvery_swtich = value;

    if (m_service_dbus_adapter_discovery_change_flag)
    {
        qInfo() << Q_FUNC_INFO << "service report,don't Sending!" << __LINE__;
        m_service_dbus_adapter_discovery_change_flag = false;
        return ;
    }
    QDBusMessage m = QDBusMessage::createMethodCall(SERVICE,
                                                    PATH,
                                                    INTERFACE,
                                                    "setDefaultAdapterDiscoverable");
    m << value;
    qDebug() << Q_FUNC_INFO << m.arguments().at(0).value<bool>() <<__LINE__;
    // 发送Message
    QDBusConnection::sessionBus().call(m, QDBus::NoBlock);
}
//控制面板切换界面时调用
void BlueToothMain::setbluetoothAdapterDiscoveringStatus(bool isScan)
{
    qDebug() << Q_FUNC_INFO << __LINE__;

    if ("normalWidget" != _MCentralWidget->currentWidget()->objectName())
    {
        qDebug() << Q_FUNC_INFO << "errorWidget not open scan!!" << __LINE__;
        return;
    }
    setDefaultAdapterScanOn(isScan);
}

void BlueToothMain::setDefaultAdapterScanOn(bool value)
{
    qDebug() << Q_FUNC_INFO << "set adapter state:" << value << __LINE__;
    if(m_current_active_connection_status && !value)
    {
        qDebug() << Q_FUNC_INFO << "open active connection ，can't off scan" << __LINE__;
        return;
    }

    if (nullptr != longTimeNoDevAddSignal_timer)
    {
        if (value)
        {
            longTimeNoDevAddSignal_timer->stop();
            longTimeNoDevAddSignal_timer->start();
        }
        else
            longTimeNoDevAddSignal_timer->stop();
    }
    else
    {
        qDebug() << Q_FUNC_INFO << "longTimeNoDevAddSignal_timer is nullptr!" << __LINE__;
    }

    QDBusMessage m = QDBusMessage::createMethodCall(SERVICE,
                                                    PATH,
                                                    INTERFACE,
                                                    "setDefaultAdapterScanOn");
    m << value;
    qDebug() << Q_FUNC_INFO << m.arguments().at(0).value<bool>() <<__LINE__;
    // 发送Message
    QDBusConnection::sessionBus().call(m, QDBus::NoBlock);
}

void BlueToothMain::receiveConnectsignal(QString address)
{
    cacheDevTypeList->setEnabled(false);
    adapterListComboxEnabledState(false);
    qDebug() << Q_FUNC_INFO << __LINE__;
    QDBusMessage m = QDBusMessage::createMethodCall(SERVICE,
                                                    PATH,
                                                    INTERFACE,
                                                    "devConnect");
    m << address;
    qDebug() << Q_FUNC_INFO << m.arguments().at(0).value<QString>() <<__LINE__;
    // 发送Message
    QDBusConnection::sessionBus().call(m, QDBus::NoBlock);
}

void BlueToothMain::receiveDisConnectSignal(QString address)
{
    qDebug() << Q_FUNC_INFO << __LINE__;
    QDBusMessage m = QDBusMessage::createMethodCall(SERVICE,
                                                    PATH,
                                                    INTERFACE,
                                                    "devDisconnect");
    m << address;
    qDebug() << Q_FUNC_INFO << m.arguments().at(0).value<QString>() <<__LINE__;
    // 发送Message
    QDBusConnection::sessionBus().call(m, QDBus::NoBlock);
}

void BlueToothMain::receiveRemoveSignal(QString address)
{
    qDebug() << Q_FUNC_INFO << __LINE__;
    QDBusMessage m = QDBusMessage::createMethodCall(SERVICE,
                                                    PATH,
                                                    INTERFACE,
                                                    "devRemove");
    m << address;
    qDebug() << Q_FUNC_INFO << m.arguments().at(0).value<QString>() <<__LINE__;
    // 发送Message
    QDBusConnection::sessionBus().call(m, QDBus::NoBlock);

    removeDeviceItemUI(address);
}

void BlueToothMain::receiveSendFileSignal(QString address)
{
    qDebug() << Q_FUNC_INFO << address;
    QDBusMessage m = QDBusMessage::createMethodCall(SERVICE,
                                                    PATH,
                                                    INTERFACE,
                                                    "setSendTransferDeviceMesg");
    m << address;
    qDebug() << Q_FUNC_INFO << m.arguments().at(0).value<QString>() <<__LINE__;
    // 发送Message
    QDBusConnection::sessionBus().call(m, QDBus::NoBlock);

}

void BlueToothMain::showBluetoothNormalMainWindow()
{
    qDebug() << Q_FUNC_INFO << __LINE__;
    m_normal_main_widget = new QWidget(_MCentralWidget);
    m_normal_main_widget->setObjectName("normalWidget");
    //this->setCentralWidget(m_normal_main_widget);

    main_layout = new QVBoxLayout(m_normal_main_widget);
    main_layout->setSpacing(40);
    main_layout->setContentsMargins(0,0,0,10);
    frame_top    = new QWidget(m_normal_main_widget);
    frame_top->setObjectName("frame_top");
    if ((m_adapter_address_list.size() > 1) &&
        (m_adapter_address_list.size() == m_adapter_name_list.size()))//用什么作为适配数量的判断
    {
        frame_top->setMinimumSize(582,(Environment::LAIKA == envPC || Environment::MAVIS == envPC)?300:369);
    }
    else
    {
        frame_top->setMinimumSize(582,(Environment::LAIKA == envPC || Environment::MAVIS == envPC)?231:300);

    }

    frame_middle = new QWidget(m_normal_main_widget);
    frame_middle->setObjectName("frame_middle");
    frame_bottom = new QWidget(m_normal_main_widget);
    frame_bottom->setObjectName("frame_bottom");
    frame_bottom->setMinimumWidth(582);

    main_layout->addWidget(frame_top,1,Qt::AlignTop);
    main_layout->addWidget(frame_middle,1,Qt::AlignTop);
    main_layout->addWidget(frame_bottom,1,Qt::AlignTop);
    main_layout->addStretch(10);

    initMainWindowTopUI();
    initMainWindowMiddleUI();
    initMainWindowbottomUI();
    this->setLayout(main_layout);
    _MCentralWidget->addWidget(m_normal_main_widget);

    initMainWindowParameters();
}

void BlueToothMain::initMainWindowParameters()
{
    qDebug() << Q_FUNC_INFO << __LINE__;
    longTimeNoDevAddSignal_timer = new QTimer(this);
    longTimeNoDevAddSignal_timer->setInterval(NO_DEV_ADD_SIGNAL_DETECTION_INTERVAL);
    connect(longTimeNoDevAddSignal_timer,&QTimer::timeout,this,&BlueToothMain::longTimeNoDevAddSlots);
    longTimeNoDevAddSignal_timer->start();

    //m_adapter_list_cmbox显示计时器
    adapterListCmbox_timer = new QTimer(this);
    adapterListCmbox_timer->setInterval(30*1000);
    connect(adapterListCmbox_timer,&QTimer::timeout,this,[=]
    {
        qDebug() << "adapterListCmbox_timer out:" << __LINE__;
        adapterListComboxEnabledState(true);
    });
}

void BlueToothMain::longTimeNoDevAddSlots()
{
    qDebug() << Q_FUNC_INFO << __LINE__;
    if ("errorWidget" == _MCentralWidget->currentWidget()->objectName())
    {
        qDebug() << Q_FUNC_INFO << "errorWidget" << __LINE__;
        longTimeNoDevAddSignal_timer->stop();
        return;
    }

    m_current_adapter_scan_status = getDefaultAdapterScanStatus();
    qDebug() << Q_FUNC_INFO << "bluetoothsevice scan status:" << m_current_adapter_scan_status << __LINE__;
    if (!m_current_adapter_scan_status && m_current_adapter_power_swtich)
    {
        if (m_device_operating)
        {
            //nothing to do;
        }
        else
        {
            setDefaultAdapterScanOn(true);
        }
    }

    if (longTimeNoDevAddSignal_timer->isActive())
    {
        longTimeNoDevAddSignal_timer->stop();
    }
    longTimeNoDevAddSignal_timer->start();

}

void BlueToothMain::initMainWindowTopUI()
{
    qDebug() << Q_FUNC_INFO << __LINE__;
    QVBoxLayout *top_layout = new QVBoxLayout(frame_top);
    top_layout->setSpacing(8);
    top_layout->setContentsMargins(0,0,0,0);

    //~ contents_path /Bluetooth/Bluetooth
    TitleLabel *label_1 = new TitleLabel(frame_top);
    label_1->setText(tr("Bluetooth"));
    label_1->resize(100,25);
    top_layout->addWidget(label_1);

    QFrame *top_frame = new QFrame(frame_top);
    top_frame->setMinimumWidth(582);
    top_frame->setFrameShape(QFrame::Shape::Box);
    top_layout->addWidget(top_frame);

    QVBoxLayout *layout = new QVBoxLayout(top_frame);
    layout->setSpacing(2);
    layout->setContentsMargins(0,0,0,0);

    QFrame *frame_1 = new QFrame(top_frame);
    frame_1->setMinimumWidth(582);
    frame_1->setFrameShape(QFrame::Shape::Box);
    frame_1->setFixedHeight(60);
    frame_1->setAutoFillBackground(true);
    layout->addWidget(frame_1);

    QFrame *line_frame1 = new QFrame(top_frame);
    line_frame1->setFixedHeight(1);
    line_frame1->setMinimumWidth(582);
    line_frame1->setFrameStyle(QFrame::HLine);
    layout->addWidget(line_frame1);

    QHBoxLayout *frame_1_layout = new QHBoxLayout(frame_1);
    frame_1_layout->setSpacing(8);
    frame_1_layout->setContentsMargins(16,0,16,0);

//    TitleLabel *label_2 = new TitleLabel(frame_1);
//    label_2->setAlignment(Qt::AlignCenter);
//    label_2->setText(tr("Turn on"));
//    label_2->resize(56,20);
    //~ contents_path /Bluetooth/Turn on
    label_2 = new QLabel(tr("Turn on"),frame_1);
    label_2->setStyleSheet("QLabel{\
                           width: 56px;\
                           height: 20px;\
                           font-weight: 400;\
                           line-height: 20px;}");
    frame_1_layout->addWidget(label_2);

    bluetooth_name = new BluetoothNameLabel(frame_1,300,38);
    connect(bluetooth_name,&BluetoothNameLabel::sendAdapterName,this,&BlueToothMain::setDefaultAdapterName);
    connect(this,&BlueToothMain::defaultAdapterNameChanged,bluetooth_name,&BluetoothNameLabel::setLabelText);
    frame_1_layout->addWidget(bluetooth_name);
    frame_1_layout->addStretch();

    m_open_bluetooth_btn = new KSwitchButton(frame_1);
    connect(m_open_bluetooth_btn,SIGNAL(stateChanged(bool)),this,SLOT(onClick_Open_Bluetooth(bool)));

    waitLabel = new QLabel(frame_1);
    waitLabel->setFixedSize(36,36);
    waitLabel->setVisible(false);
    if (!w_timer) {
        w_timer = new QTimer(this);
        w_timer->setInterval(100);
        connect(w_timer,&QTimer::timeout,this,&BlueToothMain::refreshWaitLabelIcon);
    }

    frame_1_layout->addWidget(m_open_bluetooth_btn);
    frame_1_layout->addWidget(waitLabel,0, Qt::AlignCenter);

    line_frame2 = new QFrame(top_frame);
    line_frame2->setFixedHeight(1);
    line_frame2->setMinimumWidth(582);
    line_frame2->setFrameStyle(QFrame::HLine);

    frame_2 = new QFrame(top_frame);
    frame_2->setMinimumWidth(582);
    frame_2->setFrameShape(QFrame::Shape::Box);
    frame_2->setFixedHeight(60);
    layout->addWidget(frame_2);
    layout->addWidget(line_frame2);
    if(m_adapter_address_list.size() <= 1) {
        frame_2->setVisible(false);
        line_frame2->setVisible(false);
    }

    QHBoxLayout *frame_2_layout = new QHBoxLayout(frame_2);
    frame_2_layout->setSpacing(8);
    frame_2_layout->setContentsMargins(16,0,16,0);

//    TitleLabel *label_3 = new TitleLabel(frame_2);
//    label_3->setAlignment(Qt::AlignCenter);
//    label_3->setText(tr("Bluetooth adapter"));
//    label_3->resize(56,20);
    //~ contents_path /Bluetooth/Bluetooth adapter
    QLabel *label_3 = new QLabel(tr("Bluetooth adapter"),frame_2);
    label_3->setStyleSheet("QLabel{\
                           width: 56px;\
                           height: 20px;\
                           font-weight: 400;\
                           line-height: 20px;}");
    frame_2_layout->addWidget(label_3);
    frame_2_layout->addStretch();

    m_adapter_list_cmbox = new QComboBox(frame_2);
    m_adapter_list_cmbox->clear();
    m_adapter_list_cmbox->setMinimumWidth(300);
    m_adapter_list_cmbox->addItems(m_adapter_name_list);
    //m_adapter_list_cmbox->setCurrentIndex(adapter_address_list.indexOf(m_default_adapter_address));
    m_adapter_list_cmbox->setCurrentIndex(m_adapter_name_list.indexOf(m_default_adapter_name));
    connect(m_adapter_list_cmbox,SIGNAL(currentIndexChanged(int)),this,SLOT(adapterComboxChanged(int)));
    frame_2_layout->addWidget(m_adapter_list_cmbox);

    if(Environment::LAIKA != envPC && Environment::MAVIS != envPC)
    {
        QFrame *frame_3 = new QFrame(top_frame);
        frame_3->setMinimumWidth(582);
        frame_3->setFrameShape(QFrame::Shape::Box);
        frame_3->setFixedHeight(60);
        layout->addWidget(frame_3);

        QFrame *line_frame3 = new QFrame(top_frame);
        line_frame3->setFixedHeight(1);
        line_frame3->setMinimumWidth(582);
        line_frame3->setFrameStyle(QFrame::HLine);
        layout->addWidget(line_frame3);

        QHBoxLayout *frame_3_layout = new QHBoxLayout(frame_3);
        frame_3_layout->setSpacing(8);
        frame_3_layout->setContentsMargins(16,0,16,0);


//        TitleLabel *label_4 = new TitleLabel(frame_3);
//        label_4->setAlignment(Qt::AlignCenter);
//        label_4->setText(tr("Show icon on taskbar"));
//        label_4->resize(56,20);
        //~ contents_path /Bluetooth/Show icon on taskbar
        QLabel *label_4 = new QLabel(tr("Show icon on taskbar"),frame_3);
        label_4->setStyleSheet("QLabel{\
                               width: 56px;\
                               height: 20px;\
                               font-weight: 400;\
                               line-height: 20px;}");
        frame_3_layout->addWidget(label_4);
        frame_3_layout->addStretch();

        m_show_panel_btn = new KSwitchButton(frame_3);
        frame_3_layout->addWidget(m_show_panel_btn);
        if(m_settings)
        {
            m_show_panel_btn->setChecked(m_settings->get("tray-show").toBool());
        }
        else
        {
            m_show_panel_btn->setChecked(false);
            //m_show_panel_btn->setDisabledFlag(false);
        }
        connect(m_show_panel_btn,&KSwitchButton::stateChanged,this,&BlueToothMain::setTrayVisible);
    }

    QFrame *frame_4 = new QFrame(top_frame);
    frame_4->setMinimumWidth(582);
    frame_4->setFrameShape(QFrame::Shape::Box);
    frame_4->setFixedHeight(60);
    layout->addWidget(frame_4);

    QFrame *line_frame4 = new QFrame(top_frame);
    line_frame4->setFixedHeight(1);
    line_frame4->setMinimumWidth(582);
    line_frame4->setFrameStyle(QFrame::HLine);
    layout->addWidget(line_frame4);

    QHBoxLayout *frame_4_layout = new QHBoxLayout(frame_4);
    frame_4_layout->setSpacing(8);
    frame_4_layout->setContentsMargins(16,0,16,0);

//    TitleLabel *label_5 = new TitleLabel(frame_4);
//    label_5->setAlignment(Qt::AlignCenter);
//    label_5->setText(tr("Discoverable by nearby Bluetooth devices"));
//    label_5->resize(56,20);
    //~ contents_path /Bluetooth/Discoverable
    QLabel *label_5 = new QLabel(tr("Discoverable by nearby Bluetooth devices"),frame_4);
    label_5->setStyleSheet("QLabel{\
                           width: 56px;\
                           height: 20px;\
                           font-weight: 400;\
                           line-height: 20px;}");
    frame_4_layout->addWidget(label_5);
    frame_4_layout->addStretch();

    m_discover_switch_btn = new KSwitchButton(frame_4);
    frame_4_layout->addWidget(m_discover_switch_btn);

    //switch_discover->setChecked(m_localDevice->isDiscoverable());
    m_discover_switch_btn->setChecked(m_current_adapter_disconvery_swtich);
    connect(m_discover_switch_btn,&KSwitchButton::stateChanged,this,&BlueToothMain::setDefaultAdapterDiscoverable);


    QFrame *frame_5 = new QFrame(top_frame);
    frame_5->setMinimumWidth(582);
    frame_5->setFrameShape(QFrame::Shape::Box);
    frame_5->setFixedHeight(60);
    layout->addWidget(frame_5);

    QHBoxLayout *frame_5_layout = new QHBoxLayout(frame_5);
    frame_5_layout->setSpacing(8);
    frame_5_layout->setContentsMargins(16,0,16,0);

//    TitleLabel *label_6 = new TitleLabel(frame_5);
//    label_6->setAlignment(Qt::AlignCenter);
//    label_6->setText(tr("Auto discover Bluetooth audio devices"));
//    label_6->resize(56,20);
    //~ contents_path /Bluetooth/Automatically discover Bluetooth audio devices
    QLabel *label_6 = new QLabel(tr("Auto discover Bluetooth audio devices"),frame_5);
    label_6->setStyleSheet("QLabel{\
                           width: 56px;\
                           height: 20px;\
                           font-weight: 400;\
                           line-height: 20px;}");
    frame_5_layout->addWidget(label_6);
    frame_5_layout->addStretch();

    m_active_scan_switch_btn = new KSwitchButton(frame_5);
    frame_5_layout->addWidget(m_active_scan_switch_btn);
    m_active_scan_switch_btn->setChecked(m_current_active_connection_status);
    connect(m_active_scan_switch_btn,&KSwitchButton::stateChanged,this,&BlueToothMain::setGSettingsActiveConnectionStatus);

    frame_top->setLayout(top_layout);
    qDebug() << Q_FUNC_INFO << "end" << __LINE__;

}


void BlueToothMain::initMainWindowMiddleUI()
{
    QVBoxLayout *middle_layout = new QVBoxLayout(frame_middle);
    middle_layout->setSpacing(8);
    middle_layout->setContentsMargins(0,0,0,0);


    QHBoxLayout *label_layout = new QHBoxLayout();
    label_layout->setSpacing(0);
    label_layout->setContentsMargins(0,0,0,0);

    TitleLabel *middle_label = new TitleLabel(frame_middle);
    middle_label->setText(tr("My Devices"));
    middle_label->resize(72,25);

    label_layout->addWidget(middle_label);

    mDev_frame = new QFrame(frame_middle);
    mDev_frame->setObjectName("mDev_frame");
    mDev_frame->setMinimumWidth(582);
    mDev_frame->setFrameShape(QFrame::Shape::Box);

    paired_dev_layout = new QVBoxLayout(mDev_frame);
    paired_dev_layout->setSpacing(2);
    paired_dev_layout->setContentsMargins(0,0,0,0);

    middle_layout->addLayout(label_layout,Qt::AlignTop);
    middle_layout->addWidget(mDev_frame,Qt::AlignTop);

    frame_middle->setLayout(middle_layout);
}

void BlueToothMain::initMainWindowbottomUI()
{
    QHBoxLayout *title_layout = new QHBoxLayout();
    title_layout->setSpacing(10);
    title_layout->setContentsMargins(0,0,16,0);

    TitleLabel *label_1 = new TitleLabel(frame_bottom);
    //~ contents_path /Bluetooth/Other Devices
    label_1->setText(tr("Other Devices"));
    label_1->resize(72,25);

    loadLabel = new QLabel(frame_bottom);
    loadLabel->setFixedSize(18,18);

    if (!m_timer) {
        m_timer = new QTimer(this);
        m_timer->setInterval(100);
        connect(m_timer,&QTimer::timeout,this,&BlueToothMain::refreshLoadLabelIcon);
    }

    //开启时延迟1.8s后开启扫描，留点设备回连时间
    delayStartDiscover_timer = new QTimer(this);
    delayStartDiscover_timer->setInterval(2000);
    connect(delayStartDiscover_timer,&QTimer::timeout,this,[=]
    {
        qDebug() << "delay Start Discover time out:" << __LINE__;
        delayStartDiscover_timer->stop();
        if (!m_current_adapter_scan_status)
        {
            setDefaultAdapterScanOn(true);
        }
    });

    title_layout->addWidget(label_1);
    title_layout->addWidget(loadLabel);
    title_layout->addStretch();

    cacheDevTypeList = new QComboBox(frame_bottom);
    cacheDevTypeList->clear();
    cacheDevTypeList->setMinimumWidth(120);
    QStringList devStrList;
    devStrList << tr("All");
    devStrList << tr("Audio");
    devStrList << tr("Peripherals");
    devStrList << tr("PC");
    devStrList << tr("Phone");
    devStrList << tr("Other");

    cacheDevTypeList->addItems(devStrList);

    connect(cacheDevTypeList,SIGNAL(currentIndexChanged(int)),this,SLOT(changeListOfDiscoveredDevices(int)));
    title_layout->addWidget(cacheDevTypeList);

    QVBoxLayout *bottom_layout = new QVBoxLayout(frame_bottom);
    bottom_layout->setSpacing(8);
    bottom_layout->setContentsMargins(0,0,0,0);
    bottom_layout->addLayout(title_layout);

    device_list = new QFrame(frame_bottom);
    device_list->setObjectName("device_list");
    device_list->setMinimumWidth(582);
    device_list->setFrameShape(QFrame::Shape::Box);
    device_list->setVisible(false);

    bottom_layout->addWidget(device_list);

    device_list_layout  = new QVBoxLayout(device_list);
    device_list_layout->setSpacing(2);
    device_list_layout->setContentsMargins(0,0,0,0);
    device_list_layout->setAlignment(Qt::AlignTop);
    device_list->setLayout(device_list_layout);

    frame_bottom->setLayout(bottom_layout);
}

void BlueToothMain::stopAllTimer()
{
    qDebug() << Q_FUNC_INFO << __LINE__;
    if (delayStartDiscover_timer && delayStartDiscover_timer->isActive())
        delayStartDiscover_timer->stop();
}

void BlueToothMain::showBluetoothErrorMainWindow()
{
    qDebug() << Q_FUNC_INFO << __LINE__;
    stopAllTimer();
    m_error_main_widget       = new QWidget(_MCentralWidget);
    errorWidgetLayout = new QVBoxLayout(m_error_main_widget);
    errorWidgetIcon   = new QLabel(m_error_main_widget);
    errorWidgetTip0   = new QLabel(m_error_main_widget);

    m_error_main_widget->setObjectName("errorWidget");

    errorWidgetLayout->setSpacing(10);
    errorWidgetLayout->setMargin(0);
    errorWidgetLayout->setContentsMargins(0,0,0,0);

    QPalette pe;
    pe.setColor(QPalette::WindowText,"#818181");
    errorWidgetIcon->setFixedSize(54,88);
    errorWidgetTip0->resize(200,30);
    errorWidgetTip0->setFont(QFont("Noto Sans CJK SC",18,QFont::Medium));
    errorWidgetTip0->setPalette(pe);
    if (QIcon::hasThemeIcon("bluetooth-disabled-symbolic")) {
        if (isblack)
            errorWidgetIcon->setPixmap(ImageUtil::drawSymbolicColoredPixmap(QIcon::fromTheme("bluetooth-disabled-symbolic").pixmap(54,88),"white"));
        else
            errorWidgetIcon->setPixmap(QIcon::fromTheme("bluetooth-disabled-symbolic").pixmap(54,88));
    }

    errorWidgetTip0->setText(tr("Bluetooth driver abnormal"));

    errorWidgetLayout->addStretch(7);
    errorWidgetLayout->addWidget(errorWidgetIcon,1,Qt::AlignCenter);
    errorWidgetLayout->addWidget(errorWidgetTip0,1,Qt::AlignCenter);
    errorWidgetLayout->addStretch(10);

    _MCentralWidget->addWidget(m_error_main_widget);
}


void BlueToothMain::refreshUIWhenAdapterChanged()
{
    qInfo() << Q_FUNC_INFO << __LINE__;
    // =============清空我的设备和蓝牙发现设备栏布局下的所有设备item=================
    cleanPairDevices();
    if("errorWidget" == _MCentralWidget->currentWidget()->objectName())
    {
        qDebug() << Q_FUNC_INFO << "errorWidget==================!" << __LINE__;
        return;
    }

    m_default_adapter_address.clear();
    m_default_adapter_address = getDefaultAdapterAddress();
    if (m_default_adapter_address.isEmpty())
    {
        qDebug() << Q_FUNC_INFO << "m_default_adapter_address is empty!" << __LINE__;
        return;
    }

    getDefaultAdapterData(m_default_adapter_address);
    // ========================END===========================================
    m_myDev_show_flag = false;

    for (int i = 0 ; i < m_default_bluetooth_adapter->m_bluetooth_device_list.size(); i++)
    {
        if(m_default_bluetooth_adapter->m_bluetooth_device_list.at(i)->isPaired())
            addMyDeviceItemUI(m_default_bluetooth_adapter->m_bluetooth_device_list.at(i));
        else
        {
            if(this->whetherToAddCurrentInterface(m_default_bluetooth_adapter->m_bluetooth_device_list.at(i)))
                addOneBluetoothDeviceItemUi(m_default_bluetooth_adapter->m_bluetooth_device_list.at(i));
        }

    }

    qInfo() << Q_FUNC_INFO << "m_default_adapter_address:" << m_default_adapter_address << __LINE__;
    qInfo() << Q_FUNC_INFO << "m_current_adapter_scan_status:" << m_current_adapter_scan_status << __LINE__;

    if (m_current_adapter_scan_status)
    {
        if(!loadLabel->isVisible())
            loadLabel->setVisible(true);
        if (!m_timer->isActive())
            m_timer->start();
    }

    //==============初始化蓝牙信息和基础信息====================================
    bluetooth_name->set_dev_name(m_default_adapter_name);

    qDebug() << Q_FUNC_INFO << "m_current_adapter_power_swtich:" << m_current_adapter_power_swtich ;
    if(m_current_adapter_power_swtich)
    {
        if (!m_open_bluetooth_btn->isChecked())
        {
            //添加标识，从蓝牙服务获取的数据
            m_service_dbus_adapter_power_change_flag = true;
            m_open_bluetooth_btn->setChecked(true);
        }
//        bluetooth_name->setVisible(true);
        frame_middle->setVisible(m_myDev_show_flag);
        if (!frame_bottom->isVisible() && frame_bottom->children().size())
            frame_bottom->setVisible(true);
    }
    else
    {
        if (m_open_bluetooth_btn->isChecked())
        {
            //添加标识，从蓝牙服务获取的数据
            m_service_dbus_adapter_power_change_flag = true;
            m_open_bluetooth_btn->setChecked(false);
        }
        frame_bottom->setVisible(false);
        frame_middle->setVisible(false);
    }

    if (m_open_bluetooth_btn->isChecked())
    {
        qDebug() << Q_FUNC_INFO << "m_open_bluetooth_btn->isChecked() == " << m_open_bluetooth_btn->isChecked() << "stary discover" ;
        if (delayStartDiscover_timer->isActive())
            delayStartDiscover_timer->stop();
        delayStartDiscover_timer->start();
    }

    //===========================END==========================================
    qDebug() << Q_FUNC_INFO << "end" << __LINE__;

    //return ;
}

void BlueToothMain::changeDeviceParentWindow(const QString &address)
{
    qDebug() << Q_FUNC_INFO << __LINE__;
    if(!frame_middle->isVisible()){
        frame_middle->setVisible(true);
    }

    DeviceInfoItem *item = device_list->findChild<DeviceInfoItem *>(address);
    QFrame *line_item = device_list->findChild<QFrame *>("line-"+address);
    if(item){

        mDevFrameAddLineFrame(0,"paired",address);

        device_list_layout->removeWidget(item);
        item->setParent(frame_middle);

        if(line_item) {
            device_list_layout->removeWidget(line_item);
            line_item->setParent(NULL);
            line_item->deleteLater();
        }

        //设备配对成功后，先读取设备的连接状态，设置对应的设备状态
        bool isConnect = getDevConnectStatus(address);
        reportDevConnectStatusSignal(address,isConnect);

        paired_dev_layout->addWidget(item);
        m_myDev_show_flag = true;
    }else{
        return;
    }
}

void BlueToothMain::onClick_Open_Bluetooth(bool ischeck)
{
    qInfo() << Q_FUNC_INFO << "Set Power:" << ischeck;

    if (!m_service_dbus_adapter_power_change_flag) //不由服务发送的设置
    {
        if (!w_timer->isActive())
            w_timer->start();
        m_open_bluetooth_btn->hide();
        waitLabel->show();
        setDefaultAdapterPower(ischeck);
    }
    if(ischeck && m_myDev_show_flag)
        frame_middle->setVisible(ischeck);
    else
        frame_middle->setVisible(false);

    frame_bottom->setVisible(ischeck);
    if(ischeck && loadLabel->isVisible())
    {
        loadLabel->setVisible(false);
    }

    if(ischeck)
    {
        if(delayStartDiscover_timer->isActive())
            delayStartDiscover_timer->stop();
        delayStartDiscover_timer->start();
    }
    else
    {
        loadLabel->setVisible(false);
        if (m_timer->isActive())
            m_timer->stop();

        stopAllTimer();
    }

    if (m_service_dbus_adapter_power_change_flag)
    {
        m_service_dbus_adapter_power_change_flag = false;
    }
}

bool BlueToothMain::isInvalidDevice(QString devName , bluetoothdevice::DEVICE_TYPE devType)
{
    //qDebug() << Q_FUNC_INFO << __LINE__;
    if (devName.split("-").length() == 6 && bluetoothdevice::DEVICE_TYPE::uncategorized == devType)
        return true;
    else
        return false;
}

void BlueToothMain::refreshLoadLabelIcon()
{
    if("normalWidget" == _MCentralWidget->currentWidget()->objectName())
    {
        if(i > 7)
            i = 0;
        if (isblack)
            loadLabel->setPixmap(ImageUtil::drawSymbolicColoredPixmap(QIcon::fromTheme("ukui-loading-"+QString::number(i,10)+"-symbolic").pixmap(18,18),"white"));
        else
            loadLabel->setPixmap(QIcon::fromTheme("ukui-loading-"+QString::number(i,10)+"-symbolic").pixmap(18,18));

        loadLabel->update();
        i++;
    }
}

void BlueToothMain::refreshWaitLabelIcon()
{
    if("normalWidget" == _MCentralWidget->currentWidget()->objectName())
    {
        if(cnt > 7)
            cnt = 0;
        if (isblack)
            waitLabel->setPixmap(ImageUtil::drawSymbolicColoredPixmap(QIcon::fromTheme("ukui-loading-"+QString::number(cnt,10)+"-symbolic").pixmap(18,18),"white"));
        else
            waitLabel->setPixmap(QIcon::fromTheme("ukui-loading-"+QString::number(cnt,10)+"-symbolic").pixmap(24,24));
        waitLabel->update();
        cnt++;
    }
}

void BlueToothMain::removeDeviceItemUI(QString address)
{
    qDebug() << Q_FUNC_INFO << address << __LINE__;

    //移除蓝牙设备列表的设备
    DeviceInfoItem *item = device_list->findChild<DeviceInfoItem *>(address);
    if(item)
    {
        qDebug() << Q_FUNC_INFO << item->objectName();
        device_list_layout->removeWidget(item);
        item->setParent(NULL);
        item->deleteLater();
    }
    else
    {
        qDebug() << Q_FUNC_INFO << "bluetooth device list NULL!!"<<__LINE__;
    }

    QFrame *line_item = device_list->findChild<QFrame *>("line-"+address);
    if(line_item) {
        qDebug() << Q_FUNC_INFO << line_item->objectName();
        device_list_layout->removeWidget(line_item);
        line_item->setParent(NULL);
        line_item->deleteLater();
    }

    //移除已配对设备列表的设备
    item = mDev_frame->findChild<DeviceInfoItem *>(address);
    line_item = mDev_frame->findChild<QFrame *>("line-"+address);
    if(item)
    {
        paired_dev_layout->removeWidget(item);
        item->setParent(NULL);
        item->deleteLater();

        if (line_item) {
            paired_dev_layout->removeWidget(line_item);
            line_item->setParent(NULL);
            line_item->deleteLater();
        } else
            removeMDevFrameLineFrame("paired");

        qDebug() << Q_FUNC_INFO << "paired_dev_layout->count():" << paired_dev_layout->count() <<__LINE__;
        if(!paired_dev_layout->count()){
            m_myDev_show_flag = false;
            frame_middle->setVisible(false);
        }
        else
        {
            m_myDev_show_flag = true;
        }
    }
    else
    {
        qDebug() << Q_FUNC_INFO << "pair list is NULL"<<__LINE__;
        return;
    }

    for(auto it = devShowListVec.begin(); it != devShowListVec.end() ;++it)
    {
        if (it->first == address)
        {
            qDebug() << Q_FUNC_INFO << devShowListVec;
            devShowListVec.removeAll(fruit(it->first,it->second));
            qDebug() << Q_FUNC_INFO << devShowListVec;
            break;
        }
    }
    qDebug() << Q_FUNC_INFO << "remove Item UI end" <<__LINE__;

}


void BlueToothMain::changeListOfDiscoveredDevices(int index)
{
    //qDebug() << Q_FUNC_INFO << index << m_discovery_device_address_all_list;
    qDebug() << Q_FUNC_INFO << index << __LINE__;
    discoverDevFlag = DevTypeShow(index);

    //清空列表数据
    QLayoutItem *child;

    while(device_list_layout->count()!=0)
    {
        child = device_list_layout->takeAt(0);
        if(child->widget() != 0)
        {
            delete child->widget();
        }
        delete child;
    }


//    while ((child = device_list_layout->takeAt(0)) != 0)
//    {
//        if(child->widget())
//        {
//            child->widget()->setParent(NULL);
//        }

//        QFrame *line_item = device_list->findChild<QFrame *>("line-"+child->widget()->objectName());
//        if(line_item) {
//            device_list_layout->removeWidget(line_item);
//            line_item->setParent(NULL);
//            line_item->deleteLater();
//        }

//        delete child;
//        child = nullptr;
//    }

    device_list->setVisible(false);
    //清除所有记录，重新加载后排序
    devShowListVec.clear();
    addDiscoverDevListByFlag(discoverDevFlag);
}


void BlueToothMain::addDiscoverDevListByFlag(BlueToothMain::DevTypeShow flag)
{
    qDebug() << Q_FUNC_INFO << flag;
    for (bluetoothdevice *dev : m_default_bluetooth_adapter->m_bluetooth_device_list)
    {
        if (dev->isPaired() || isInvalidDevice(dev->getDevName(),dev->getDevType()))
            continue;
        switch (flag) {
        case DevTypeShow::All:
            addOneBluetoothDeviceItemUi(dev);
            break;
        case DevTypeShow::Audio:
            if (dev->getDevType() == bluetoothdevice::DEVICE_TYPE::headset ||
                dev->getDevType() == bluetoothdevice::DEVICE_TYPE::headphones ||
                dev->getDevType() == bluetoothdevice::DEVICE_TYPE::audiovideo) {
                addOneBluetoothDeviceItemUi(dev);
            }
            break;
        case DevTypeShow::Peripherals:
            if (dev->getDevType() == bluetoothdevice::DEVICE_TYPE::mouse ||
                dev->getDevType() == bluetoothdevice::DEVICE_TYPE::keyboard) {
                addOneBluetoothDeviceItemUi(dev);
            }
            break;
        case DevTypeShow::PC:
            if (dev->getDevType() == bluetoothdevice::DEVICE_TYPE::computer) {
                addOneBluetoothDeviceItemUi(dev);
            }
            break;
        case DevTypeShow::Phone:
            if (dev->getDevType() == bluetoothdevice::DEVICE_TYPE::phone) {
                addOneBluetoothDeviceItemUi(dev);
            }
            break;
        case DevTypeShow::Other:
            if (dev->getDevType() == bluetoothdevice::DEVICE_TYPE::headset ||
                dev->getDevType() == bluetoothdevice::DEVICE_TYPE::headphones ||
                dev->getDevType() == bluetoothdevice::DEVICE_TYPE::audiovideo ||
                dev->getDevType() == bluetoothdevice::DEVICE_TYPE::phone ||
                dev->getDevType() == bluetoothdevice::DEVICE_TYPE::mouse ||
                dev->getDevType() == bluetoothdevice::DEVICE_TYPE::keyboard ||
                dev->getDevType() == bluetoothdevice::DEVICE_TYPE::computer)
                break;
            else
                addOneBluetoothDeviceItemUi(dev);
            break;
        default:
            addOneBluetoothDeviceItemUi(dev);
            break;
        }
    }
}

void BlueToothMain::monitorSleepSignal()
{
    if (QDBusConnection::systemBus().connect("org.freedesktop.login1",
                                             "/org/freedesktop/login1",
                                             "org.freedesktop.login1.Manager",
                                             "PrepareForSleep",
                                             this,
                                             SLOT(monitorSleepSlot(bool)))){
        qDebug() << Q_FUNC_INFO << "PrepareForSleep signal connected successfully to slot";
    } else {
        qDebug() << Q_FUNC_INFO << "PrepareForSleep signal connected was not successful";
    }
}

void BlueToothMain::monitorSleepSlot(bool value)
{
    //connectBluetoothServiceSignal();
    if (!value) {
        if (sleep_status)
        {
//            if (this->centralWidget()->objectName() == "normalWidget")
//                adapterPoweredChanged(true);
        }
    } else {
        sleep_status = m_current_adapter_power_swtich;
    }
}

void BlueToothMain::adapterPoweredChanged(bool value)
{
    qDebug() << Q_FUNC_INFO << value;
//    if(m_settings)
//        m_settings->set("switch",QVariant::fromValue(value));

    if(value)
    {
        bluetooth_name->set_dev_name(m_default_adapter_name);
        bluetooth_name->setVisible(true);
        frame_bottom->setVisible(true);

        if(m_myDev_show_flag)
            frame_middle->setVisible(true);

//         if(!frame_middle->isVisible())
//         {
//            frame_middle->setVisible(true);
//         }
        if (!m_open_bluetooth_btn->isChecked())
            m_open_bluetooth_btn->setChecked(true);

        //延时2S开启扫描，给用户回连缓冲
        delayStartDiscover_timer->start();

    }
    else
    {
//        if (bluetooth_name->isVisible())
//            bluetooth_name->setVisible(false);

        if (m_open_bluetooth_btn->isChecked())
            m_open_bluetooth_btn->setChecked(false);

        if (frame_bottom->isVisible())
            frame_bottom->setVisible(false);

        if(frame_middle->isVisible())
            frame_middle->setVisible(false);

        if (!paired_dev_layout->isEmpty())
            m_myDev_show_flag = true ;
        else
            m_myDev_show_flag = false ;
    }
}


void BlueToothMain::adapterListComboxEnabledState(bool state)
{
    qDebug() << Q_FUNC_INFO << "set state:"<< state << __LINE__;
    if (!state)
    {
        if (adapterListCmbox_timer->isActive())
        {
            adapterListCmbox_timer->stop();
            adapterListCmbox_timer->start();
        }
        else
        {
            adapterListCmbox_timer->start();
        }
    }
    else
    {
        if (adapterListCmbox_timer->isActive())
        {
            adapterListCmbox_timer->stop();
        }
    }
    m_adapter_list_cmbox->setEnabled(state);
}

void BlueToothMain::adapterComboxChanged(int i)
{
    qDebug() << Q_FUNC_INFO << i
             << m_adapter_address_list.at(i)
             << m_adapter_name_list.at(i)
             << m_adapter_address_list
             << m_adapter_name_list;

    if(m_adapter_address_list.size() != m_adapter_name_list.size())
        return;

    adapterListComboxEnabledState(false);

    if(i != -1)
    {
        qDebug() << Q_FUNC_INFO << "=================3" << __LINE__;
        setDefaultAdapterScanOn(false);

        if(i < m_bluetooth_adapter_list.size())
            setDefaultAdapter(m_bluetooth_adapter_list.at(i)->getDevAddress());

        refreshUIWhenAdapterChanged();
    }else{
        if(m_open_bluetooth_btn->isChecked()){
            qDebug() << __FUNCTION__<< "index - i : "<< i << __LINE__ ;
            m_open_bluetooth_btn->setChecked(false);
//#ifndef DEVICE_IS_INTEL
//            m_open_bluetooth_btn->setDisabledFlag(false);
//#endif
        }
        if(frame_middle->isVisible()){
            frame_middle->setVisible(false);
        }
    }
    qDebug() << Q_FUNC_INFO << "end";
}

void BlueToothMain::mDevFrameAddLineFrame(int index,QString str,QString addr)
{
    qDebug() << Q_FUNC_INFO << "#########################" << str << addr;

    if ("paired" == str) {

        if (!frame_middle->findChildren<DeviceInfoItem *>().size()) {
            return;
        }

        QFrame *line_frame = new QFrame(mDev_frame);
        line_frame->setObjectName("line-"+addr);
        line_frame->setFixedHeight(1);
        line_frame->setMinimumWidth(582);
        line_frame->setFrameStyle(QFrame::HLine);
        paired_dev_layout->addWidget(line_frame,Qt::AlignTop);

    } else if ("other" == str) {

//        if (frame_bottom->findChildren<DeviceInfoItem *>().size() < 1) {
//            return;
//        }

        QFrame *line_frame = new QFrame(device_list);
        line_frame->setObjectName("line-"+addr);
        line_frame->setFixedHeight(1);
        line_frame->setMinimumWidth(582);
        line_frame->setFrameStyle(QFrame::HLine);

        if (!device_list->isVisible())
            device_list->setVisible(true);

        device_list_layout->insertWidget(index,line_frame,Qt::AlignTop);

    } else {
        return;
    }
}

void BlueToothMain::removeMDevFrameLineFrame(QString str)
{
    qDebug() << Q_FUNC_INFO ;

    if ("paired" == str) {

        if ((frame_middle->findChildren<QFrame *>().size() - frame_middle->findChildren<DeviceInfoItem *>().size() - 2) == frame_middle->findChildren<DeviceInfoItem *>().size()) {
            QLayoutItem *child;
            if ((child = paired_dev_layout->takeAt(0)) != 0) {
                if (child->widget()->objectName().startsWith("line-")) {

                    delete child->widget();
                    delete child;
                    //child = nullptr;
                }
            }
        }
    } else if ("other" == str) {

    } else {
        return;
    }
}

void BlueToothMain::cleanPairDevices()
{
    qInfo() << Q_FUNC_INFO << __LINE__ ;

    QLayoutItem* child;
    //清除配对列表下的所有界面
    while(paired_dev_layout->count()!=0)
    {
        child = paired_dev_layout->takeAt(0);
        if(child->widget() != 0)
        {
            delete child->widget();
        }

        delete child;
    }

    //清除临时列表下的所有界面
    while(device_list_layout->count()!=0)
    {
        child = device_list_layout->takeAt(0);
        if(child->widget() != 0)
        {
            delete child->widget();
        }

        delete child;
    }

    device_list->setVisible(false);
    qDebug() << Q_FUNC_INFO << "end" ;

}

void BlueToothMain::gSettingsChanged(const QString &key)
{
    if ("styleName" == key) {
        if(styleSettings->get("style-name").toString() == "ukui-black" ||
           styleSettings->get("style-name").toString() == "ukui-dark")
            isblack = true;
        else
            isblack = false;

        if (isblack)
            errorWidgetIcon->setPixmap(ImageUtil::drawSymbolicColoredPixmap(QIcon::fromTheme("bluetooth-disabled-symbolic").pixmap(54,88),"white"));
        else
            errorWidgetIcon->setPixmap(QIcon::fromTheme("bluetooth-disabled-symbolic").pixmap(54,88));
    }
}

void BlueToothMain::gSettingsBluetoothChanged(const QString &key)
{
    qDebug()<< Q_FUNC_INFO << key <<__LINE__;

    if (GSETTING_STR_ACTIVE_CONNECTION_TRAN == key || GSETTING_STR_ACTIVE_CONNECTION == key)
    {
        m_current_active_connection_status = m_settings->get(GSETTING_STR_ACTIVE_CONNECTION).toBool();
        m_active_scan_switch_btn->setChecked(m_current_active_connection_status);
        qDebug()<< Q_FUNC_INFO << "m_current_active_connection_status:"
                << m_current_active_connection_status;

    }
}

void BlueToothMain::setGSettingsActiveConnectionStatus(bool state)
{
    qDebug() << Q_FUNC_INFO << "set curr state:" << state << __LINE__;
    m_settings->set(GSETTING_STR_ACTIVE_CONNECTION,QVariant(state));
    qInfo() << Q_FUNC_INFO << m_settings->keys();
}


bool BlueToothMain::registerClient()
{
    qDebug() << Q_FUNC_INFO << __LINE__;
    QDBusMessage message = QDBusMessage::createMethodCall(SERVICE,
                                                          PATH,
                                                          INTERFACE,
                                                          "registerClient");

    QString userName = QStandardPaths::writableLocation(QStandardPaths::HomeLocation) ;
    qInfo() << Q_FUNC_INFO << userName << __LINE__;

    //int(0) 控制面板ID
    message << QDBusConnection::sessionBus().baseService() << int(0) << userName;
    QDBusReply<bool> reply_res = QDBusConnection::sessionBus().call(message);
    return reply_res;
}


bool BlueToothMain::unregisterClient()
{
    QDBusMessage message = QDBusMessage::createMethodCall(SERVICE,
                                                          PATH,
                                                          INTERFACE,
                                                          "unregisterClient");
    message << QDBusConnection::sessionBus().baseService();
    QDBusReply<bool> reply_res = QDBusConnection::sessionBus().call(message);
    return reply_res;
}

//add function before
