// Get the last known position from the Location Manager. Any error results in the return of an
// invalid position.
QGeoPositionInfo QGeoPositionInfoSourceBbPrivate::lastKnownPosition(
    bool fromSatellitePositioningMethodsOnly) const
{
    QGeoPositionInfo position = QGeoPositionInfo();
    bb::PpsObject ppsObject(global::locationManagerPpsFile);
    QVariantMap lastKnown = populateLastKnownPositionRequest(fromSatellitePositioningMethodsOnly);

    if (!ppsObject.open())
        return position;

    // Location Manager promises to reply immediately with the last known position or an error.
    ppsObject.setBlocking(true);

    if (!sendRequest(ppsObject, lastKnown))
        return position;

    if (!receiveReply(&lastKnown, ppsObject))
        return position;

    if (!lastKnown.contains("res") || lastKnown.value("res").toString() != "location")
        return position;

    // the return value of populatePositionInfo() is ignored since either way position is returned
    // by lastKnownPosition()
    (void)populatePositionInfo(&position, lastKnown);

    return position;
}
/* #TODO FTPConnect does not send a ftp Command, yet it is treated as one because
   there is a welcome message from teh server prepended with a reply status code.
    REMOVE THIS ANAMOLY.
*/
enum protocolStatus FTPClient::ctrlConnect()
{
    if ( connect(this->ctrlfd,(struct sockaddr*)&(this->serverCtrlAddr), sizeof(this->serverCtrlAddr) ) < 0 )
    {   perror("Connect :: ( Control Channel )");
        exit(1);
    }

    // #### Wait for the server to reply that the server is ready #####

    ssize_t numBytes = receiveReply(this->ctrlfd);
    std::cerr << interpretResponse(this->rbuffer, numBytes) << std::endl;
    return checkCommandStatus();
}
bool QGeoPositionInfoSourceBbPrivate::receivePositionReply(bb::PpsObject &ppsObject)
{
    QVariantMap reply;
    // receiveReply() tests for errors associated with the request being replied to
    if (!receiveReply(&reply, ppsObject)) {
        _replyErrorCode = bb::location::PositionErrorCode::FatalUnknown;

        // if there is an error from Location Manager report it so user can access it through the
        // properties when responding to the updateTimeout() signal.
        if (reply.contains("errCode")) {
            int errCode = reply.value("errCode").toInt();
            _replyErrorCode
                = intToPositionErrorCodeMap.value(errCode,
                                                  bb::location::PositionErrorCode::FatalUnknown);
            if (fatalError(_replyErrorCode)) {
                _sourceError
                    = positionErrorCodeToErrorMap.value(_replyErrorCode,
                                                        QGeoPositionInfoSource::UnknownSourceError);
            }

            if (reply.contains("err")) {
                _replyErr = reply.value("err").toString();
                if (reply.contains("errstr")) {
                    _replyErrStr = reply.value("errstr").toString();
                }
            }
        } else {
            _sourceError = QGeoPositionInfoSource::UnknownSourceError;
        }
        return false;
    }

    // clear any errors
    _replyErrorCode = bb::location::PositionErrorCode::None;
    _replyErr = QString();
    _replyErrStr = QString();

    // check that this is a location reply (could be a reply to another request type, eg. cancel,
    // which is ignored here)
    if (reply.contains("res") && reply.value("res").toString() == "location") {
        // keep the raw LM reply for access via Qt properties.
        _replyDat = reply.value("dat").toMap();

        // extract the geo position info from the reply into _currentPosition
        if (populatePositionInfo(&_currentPosition, reply)) {
            emitPositionUpdated(_currentPosition);
        }
    }

    return true;
}
// Here the argument 'command' contains "the FTP command + it's assoc. param"
enum protocolStatus FTPClient::writeCtrlSock(std::string command)
{
    const char* tptr;
    tptr = command.c_str();

    bzero(this->sbuffer,S_BUFFER_SIZE);
    strcpy((char *)this->sbuffer, tptr);

    writeSock(this->ctrlfd,command.length());

    ssize_t numBytes = receiveReply(this->ctrlfd);
    std::cerr << interpretResponse(this->rbuffer, numBytes) << std::endl;
    return checkCommandStatus();
}
// store the bounds and connect the future watcher to receiveReply()
bool GeoSearchReplyBb::initialize( const QtMobilitySubset::QGeoBoundingArea *bounds )
{
    // the BB Geocode Service does not provide bounding of the request so the bounds are
    // saved here for filtering of the BB Geocode Service reply manually.
    setBounds( bounds );

    // connect the future watcher to receiveReply()
    bool connected = connect( &_futureWatcher, SIGNAL(finished()), SLOT(receiveReply()) );
    if ( !connected ) {
        finishReply( QtMobilitySubset::QGeoSearchReply::CommunicationError );
        return false;
    }

    return true;
}
void FTPClient::listData()
{
    // Establish the data channel.
    dataConnect();
    ssize_t numBytes;
    numBytes = receiveReply(this->datafd);
    std::string s((char *)this->rbuffer);
    if ( ! (this->multipleCall) )
        std::cerr << this->rbuffer;
    else
    {
        std::replace( s.begin(), s.end(), '\n', ',');
        // Strip the last comma
        s = s.substr(0,s.size() - 1);

    }
    // Data connection closed by the server.
    close(this->datafd);
    // Create a new data socket for FUTURE.
    createDataSock();
    this->fileList = s;
}