void MusicFileReceiveServer::readPendingDatagrams()
{
    while(m_receiveSocket->hasPendingDatagrams())
    {
        QByteArray datagram;
        datagram.resize(m_receiveSocket->pendingDatagramSize());
        QHostAddress sender;
        quint16 senderPort;

        m_receiveSocket->readDatagram(datagram.data(), datagram.size(),
                                      &sender, &senderPort);

        if(datagram.contains(SPLITE_STR))
        {
            if(m_saveDir.isEmpty())
            {
                M_LOGGER_ERROR("The Save Path Is Empty!");
                return;
            }
            QString path = QString(datagram).split(SPLITE_STR).first();
            m_file->close();
            m_file->setFileName(m_saveDir + "/" + QFileInfo(path).fileName() );
            m_file->open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Unbuffered);
        }
        else
        {
            m_file->write(datagram.data(),datagram.size());
        }

        QByteArray msg(DATAGRAM);
        m_receiveSocket->writeDatagram(msg, msg.length(), sender, SEND_PORT);
    }
}
void MusicCoreMPlayer::setMedia(Category type, const QString &data, int winId)
{
    m_timer.stop();
    if(m_process)
    {
        m_process->kill();
        delete m_process;
        m_process = nullptr;
    }
    if(!QFile::exists(MAKE_PLAYER_AL))
    {
        M_LOGGER_ERROR(tr("Lack of plugin file!"));
        return;
    }

    m_category = type;
    m_playState = StoppedState;
    m_process = new QProcess(this);
    connect(m_process, SIGNAL(finished(int)), SIGNAL(finished()));

    switch(m_category)
    {
        case MusicCategory: setMusicMedia(data); break;
        case RadioCategory: setRadioMedia(data); break;
        case VideoCategory: setVideoMedia(data, winId); break;
        case NullCategory: break;
        default: break;
    }
}
void MusicKWBackgroundThread::downLoadDataFinished(const QByteArray &bytes)
{
    QJson::Parser parser;
    bool ok;

    if(bytes != "NO_PIC")
    {
        QVariant data = parser.parse(bytes, &ok);
        if(ok)
        {
            QVariantMap dataMap = data.toMap();
            QVariantList datas = dataMap["array"].toList();
            foreach(const QVariant &value, datas)
            {
                dataMap = value.toMap();
                if(m_counter < 5 && !dataMap.isEmpty())
                {
                    QString url = dataMap.values().first().toString();
                    M_LOGGER_ERROR(url);
                    MusicDataDownloadThread *download = new MusicDataDownloadThread(url, QString("%1%2%3%4").arg(BACKGROUND_DIR_FULL)
                                                            .arg(m_savePath).arg(m_counter++).arg(SKN_FILE),
                                                            MusicDownLoadThreadAbstract::DownloadBigBG, this);
                    connect(download, SIGNAL(downLoadDataChanged(QString)), SLOT(downLoadFinished()));
                    download->startToDownload();
                }
            }
        }
QString MusicBackgroundManager::getArtPhotoPathByIndex(int index) const
{
    if((0 < index && index != -1) || index > m_photos.count() || m_photos.isEmpty())
    {
        M_LOGGER_ERROR("index out of range");
        return QString();
    }
    return (index == -1) ? m_photos[m_currentIndex < m_photos.count() ? m_currentIndex : 0]
                         : m_photos[index];
}
void MusicSourceDownloadThread::downLoadFinished()
{
    if(m_reply && m_reply->error() == QNetworkReply::NoError)
    {
        emit recievedData(m_reply->readAll());
    }
    else
    {
        M_LOGGER_ERROR("Download source data error");
        emit recievedData(QByteArray());
    }
    deleteAll();
}
void MusicNetworkAbstract::sslErrorsString(QNetworkReply *reply, const QList<QSslError> &errors)
{
    QString errorString;
    foreach(const QSslError &error, errors)
    {
        if(!errorString.isEmpty())
        {
            errorString += ", ";
        }
        errorString += error.errorString();
    }

    M_LOGGER_ERROR(QString("sslErrors: %1").arg(errorString));
    reply->ignoreSslErrors();
}
void MusicSourceDownloadThread::sslErrors(QNetworkReply* reply, const QList<QSslError> &errors)
{
    QString errorString;
    foreach(const QSslError &error, errors)
    {
        if(!errorString.isEmpty())
        {
            errorString += ", ";
        }
        errorString += error.errorString();
    }

    M_LOGGER_ERROR(QString("sslErrors: %1").arg(errorString));
    reply->ignoreSslErrors();
    emit recievedData(QByteArray());
    deleteAll();
}
void MusicDataDownloadThread::startToDownload()
{
    if( !m_file->exists() || m_file->size() < 4 )
    {
        if( m_file->open(QIODevice::WriteOnly) )
        {
            m_manager = new QNetworkAccessManager(this);
#ifndef QT_NO_SSL
            connect(m_manager, SIGNAL(sslErrors(QNetworkReply*,QList<QSslError>)),
                               SLOT(sslErrors(QNetworkReply*,QList<QSslError>)));
            M_LOGGER_INFO(QString("MusicDataDownloadThread Support ssl: %1").arg(QSslSocket::supportsSsl()));
#endif
            startRequest(m_url);
        }
        else
        {
            M_LOGGER_ERROR("The data file create failed");
            emit downLoadDataChanged("The data file create failed");
            deleteAll();
        }
    }
}
void MusicSourceDownloadThread::downLoadFinished()
{
    if(m_reply && m_reply->error() == QNetworkReply::NoError)
    {
        if(m_reply->attribute(QNetworkRequest::RedirectionTargetAttribute).isValid())
        {
            QString newUrl = m_reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toString();
            startToDownload(newUrl); ///redirection target url
        }
        else
        {
            emit downLoadByteDataChanged(m_reply->readAll());
            deleteAll();
        }
    }
    else
    {
        M_LOGGER_ERROR("Download source data error");
        emit downLoadByteDataChanged(QByteArray());
        deleteAll();
    }
}
void MusicSourceDownloadThread::replyError(QNetworkReply::NetworkError)
{
    M_LOGGER_ERROR("Abnormal network connection");
    emit downLoadByteDataChanged(QByteArray());
    deleteAll();
}
void MusicNetworkAbstract::replyError(QNetworkReply::NetworkError)
{
    M_LOGGER_ERROR("Abnormal network connection");
    emit downLoadDataChanged(QString());
    deleteAll();
}