예제 #1
0
bool AbstractQueryItem::load()
{
    if (m_account == nullptr || !isQueryValid() || m_status == Loading) {
        return false;
    }

    QNetworkReply *reply = createQuery();
    setStatusAndErrorMessage(Loading, QString());

    connect(reply, &QNetworkReply::finished, this, [this, reply]() {
        QObjectPtr<QNetworkReply> replyPtr {reply};
        const QByteArray &data {replyPtr->readAll()};
        handleReply(data, replyPtr->error(), replyPtr->errorString());
        if (m_status == Loading) {
            if (replyPtr->error() != QNetworkReply::NoError) {
                qCWarning(QLoggingCategory("query-item")) << "Error happened during query";
                qCWarning(QLoggingCategory("query-item")) << "Error code:" << replyPtr->error();
                qCWarning(QLoggingCategory("query-item")) << "Error message (Qt):" << replyPtr->errorString();
                qCWarning(QLoggingCategory("query-item")) << "Error message (Twitter):" << data;
                setStatusAndErrorMessage(Error, tr("Network error. Please try again later."));
            } else {
                setStatusAndErrorMessage(Idle, QString());
            }
        }
    });

    return true;
}
bool LoadSaveManager::load(ILoadSave &loadSave)
{
    QFile file {m_configFilePath};
    if (!file.exists()) {
        qCDebug(QLoggingCategory("load-save-manager")) << "Failed to find config file"
                                                       << m_configFilePath;
        return true; // No file, so no config to load
    }

    if (!file.open(QIODevice::ReadOnly)) {
        qCWarning(QLoggingCategory("load-save-manager")) << "Failed to open config file"
                                                         << m_configFilePath;
        return false;
    }
    QJsonParseError error {-1, QJsonParseError::NoError};
    QJsonDocument document {QJsonDocument::fromJson(file.readAll(), &error)};
    file.close();

    if (error.error != QJsonParseError::NoError) {
        qCWarning(QLoggingCategory("load-save-manager")) << "Failed to parse configuration file";
        qCWarning(QLoggingCategory("load-save-manager")) << "Error" << error.errorString();
        return false;
    }

    loadSave.load(document.object());
    return true;
}
QString LoadSaveManager::configFilePath()
{
    QDir dir {QStandardPaths::writableLocation(QStandardPaths::ConfigLocation)};
    if (!dir.exists()) {
        if (!QDir::root().mkpath(dir.absolutePath())) {
            qCWarning(QLoggingCategory("load-save-manager")) << "Failed to create root config dir"
                                                             << dir.absolutePath();
            return QString();
        }
    }
    const QString &appName {QCoreApplication::instance()->applicationName()};
    if (!dir.exists(appName)) {
        if (!dir.mkdir(appName)) {
            qCWarning(QLoggingCategory("load-save-manager")) << "Failed to create config dir"
                                                             << dir.absoluteFilePath(appName);
            return QString();
        }
    }
    if(!dir.cd(appName)) {
        qCWarning(QLoggingCategory("load-save-manager")) << "Failed to enter in config dir"
                                                         << dir.absoluteFilePath(appName);
        return QString();
    }
    return dir.absoluteFilePath(QLatin1String("config.json"));
}
bool LoadSaveManager::clear()
{
    QFile configFile {m_configFilePath};
    if (!configFile.exists()) {
        return true;
    }
    if (!configFile.remove()) {
        qCWarning(QLoggingCategory("load-save-manager")) << "Failed to remove config file"
                                                         << m_configFilePath;
        return false;
    }
    return true;
}
bool LoadSaveManager::save(const ILoadSave &loadSave)
{
    QFile file {m_configFilePath};
    if (!file.open(QIODevice::ReadWrite | QIODevice::Text)) {
        qCWarning(QLoggingCategory("load-save-manager")) << "Failed to open config file"
                                                         << m_configFilePath;
        return false;
    }
    QJsonParseError error {-1, QJsonParseError::NoError};
    QJsonDocument document {QJsonDocument::fromJson(file.readAll(), &error)};

    QJsonObject config {document.object()};
    loadSave.save(config);
    document.setObject(config);
    file.resize(0);
    file.write(document.toJson(QJsonDocument::Indented));
    file.close();
    return true;
}
SearchQueryHandler::SearchQueryHandler(const Query::Arguments &arguments)
{
    auto qIt = arguments.find(QLatin1String("q"));
    if (qIt != std::end(arguments)) {
        m_query = qIt->second;
    }
    auto resultTypeIt = arguments.find(QLatin1String("result_type"));
    if (resultTypeIt != std::end(arguments)) {
        const QString &resultType = resultTypeIt->second;
        if (resultType == QLatin1String("mixed")) {
            m_resultType = "mixed";
        } else if (resultType == QLatin1String("recent")) {
            m_resultType = "recent";
        } else if (resultType == QLatin1String("popular")) {
            m_resultType = "popular";
        }
    }
    qCDebug(QLoggingCategory("search-query-handler")) << "Searching using q:" << m_query
                                                      << "result_type:" << m_resultType;
}
예제 #7
0
void AbstractQueryItem::setStatusAndErrorMessage(AbstractQueryItem::Status status,
                                                 const QString &errorMessage)
{
    qCDebug(QLoggingCategory("query-item")) << "Current status" << status << errorMessage;
    if (m_status != status) {
        m_status = status;
        emit statusChanged();
        switch (m_status) {
        case Idle:
            emit finished();
            break;
        case Error:
            emit error();
            break;
        default:
            break;
        }
    }

    if (m_errorMessage != errorMessage) {
        m_errorMessage = errorMessage;
        emit errorMessageChanged();
    }
}