Skip to content

FinceptTerminal is a modern finance application offering advanced market analytics, investment research, and economic data tools, designed for interactive exploration and data-driven decision-making in a user-friendly environment.

11.9 KB
Raw Blame
#include "screens/news/dialogs/RssFeedEditDialog.h"

#include "core/logging/Logger.h"
#include "services/news/RssFeedProbeService.h"
#include "ui/theme/Theme.h"

#include <QFormLayout>
#include <QHBoxLayout>
#include <QMessageBox>
#include <QPointer>
#include <QStyle>
#include <QUrl>
#include <QUuid>
#include <QVBoxLayout>

#include <algorithm>

namespace fincept::screens {

namespace {

QStringList default_categories() {
    return {"MARKETS", "GEOPOLITICS", "REGULATORY", "ECONOMIC", "ENERGY", "CRYPTO", "TECH", "DEFENSE", "EARNINGS"};
}

QStringList default_regions() {
    return {"GLOBAL", "US", "EU", "UK", "ASIA", "INDIA", "CHINA", "JAPAN", "ME"};
}

} // anonymous namespace

RssFeedEditDialog::RssFeedEditDialog(const services::RSSFeed& initial, bool is_builtin_id, QWidget* parent)
    : QDialog(parent), initial_(initial), is_builtin_id_(is_builtin_id) {
    setWindowTitle(initial.id.isEmpty() ? tr("Add RSS Feed") : tr("Edit RSS Feed"));
    setModal(true);
    resize(560, 0);
    build_ui();
}

void RssFeedEditDialog::changeEvent(QEvent* event) {
    if (event->type() == QEvent::LanguageChange)
        retranslateUi();
    QDialog::changeEvent(event);
}

void RssFeedEditDialog::retranslateUi() {
    setWindowTitle(initial_.id.isEmpty() ? tr("Add RSS Feed") : tr("Edit RSS Feed"));

    // QFormLayout owns the row caption labels — re-apply via labelForField.
    auto set_label = [this](QWidget* field, const QString& text) {
        if (!form_ || !field)
            return;
        if (auto* lbl = qobject_cast<QLabel*>(form_->labelForField(field)))
            lbl->setText(text);
    };
    set_label(id_input_, tr("ID"));
    set_label(source_input_, tr("Source"));
    set_label(name_input_, tr("Name"));
    set_label(url_input_, tr("URL"));
    set_label(category_combo_, tr("Category"));
    set_label(region_combo_, tr("Region"));
    set_label(tier_spin_, tr("Tier"));

    if (id_input_ && is_builtin_id_)
        id_input_->setToolTip(tr("Built-in feed ID is fixed."));
    if (source_input_)
        source_input_->setPlaceholderText(tr("e.g. BLOOMBERG"));
    if (name_input_)
        name_input_->setPlaceholderText(tr("e.g. Bloomberg Markets"));
    if (tier_spin_)
        tier_spin_->setToolTip(tr("1=wire, 2=major, 3=specialty, 4=blog"));

    if (test_btn_)
        test_btn_->setText(tr("Test URL"));
    if (cancel_btn_)
        cancel_btn_->setText(tr("Cancel"));
    if (ok_btn_)
        ok_btn_->setText(tr("Save"));
    // test_status_ reflects the last async test result — refreshed on next test.
}

void RssFeedEditDialog::set_status(const QString& text, const char* state) {
    test_status_->setText(text);
    test_status_->setProperty("probeState", QString::fromLatin1(state));
    test_status_->style()->unpolish(test_status_);
    test_status_->style()->polish(test_status_);
    test_status_->show();
}

void RssFeedEditDialog::build_ui() {
    auto* root = new QVBoxLayout(this);
    root->setContentsMargins(16, 16, 16, 12);
    root->setSpacing(10);

    form_ = new QFormLayout();
    QFormLayout* form = form_;
    form->setLabelAlignment(Qt::AlignRight);
    form->setFormAlignment(Qt::AlignLeft | Qt::AlignTop);

    id_input_ = new QLineEdit(this);
    id_input_->setObjectName("rssFeedEditId");
    if (initial_.id.isEmpty()) {
        // New feed — auto-generate a user prefix id; user can override.
        const QString uid = QUuid::createUuid().toString(QUuid::WithoutBraces).left(8);
        id_input_->setText("usr-" + uid);
    } else {
        id_input_->setText(initial_.id);
    }
    if (is_builtin_id_) {
        id_input_->setReadOnly(true);
        id_input_->setToolTip(tr("Built-in feed ID is fixed."));
    } else if (!initial_.id.isEmpty()) {
        // Editing an existing user feed: the id is its key. Changing it used to
        // upsert a second row and leave the original behind as a duplicate.
        id_input_->setReadOnly(true);
        id_input_->setToolTip(tr("A feed's ID cannot be changed after it is created."));
    }
    form->addRow(tr("ID"), id_input_);

    source_input_ = new QLineEdit(initial_.source, this);
    source_input_->setPlaceholderText(tr("e.g. BLOOMBERG"));
    form->addRow(tr("Source"), source_input_);

    name_input_ = new QLineEdit(initial_.name, this);
    name_input_->setPlaceholderText(tr("e.g. Bloomberg Markets"));
    form->addRow(tr("Name"), name_input_);

    url_input_ = new QLineEdit(initial_.url, this);
    url_input_->setPlaceholderText("https://example.com/rss.xml");
    // Editing the URL invalidates any earlier probe. Without this, testing one
    // URL, then typing a different one, and saving reused the *previous* URL's
    // pass/fail verdict — so a broken feed could be saved as "validated".
    connect(url_input_, &QLineEdit::textEdited, this, [this](const QString&) {
        test_run_ = false;
        last_test_ok_ = false;
        if (test_status_)
            test_status_->hide();
    });
    form->addRow(tr("URL"), url_input_);

    category_combo_ = new QComboBox(this);
    category_combo_->setEditable(true);
    category_combo_->addItems(default_categories());
    if (!initial_.category.isEmpty()) {
        int idx = category_combo_->findText(initial_.category);
        if (idx >= 0)
            category_combo_->setCurrentIndex(idx);
        else
            category_combo_->setEditText(initial_.category);
    }
    form->addRow(tr("Category"), category_combo_);

    region_combo_ = new QComboBox(this);
    region_combo_->setEditable(true);
    region_combo_->addItems(default_regions());
    if (!initial_.region.isEmpty()) {
        int idx = region_combo_->findText(initial_.region);
        if (idx >= 0)
            region_combo_->setCurrentIndex(idx);
        else
            region_combo_->setEditText(initial_.region);
    }
    form->addRow(tr("Region"), region_combo_);

    tier_spin_ = new QSpinBox(this);
    tier_spin_->setRange(1, 4);
    tier_spin_->setValue(initial_.tier > 0 ? initial_.tier : 3);
    tier_spin_->setToolTip(tr("1=wire, 2=major, 3=specialty, 4=blog"));
    form->addRow(tr("Tier"), tier_spin_);

    root->addLayout(form);

    // Test status line — one sheet colours it by state (was an inline sheet per update).
    test_status_ = new QLabel(this);
    test_status_->setObjectName("rssFeedEditTestStatus");
    test_status_->setWordWrap(true);
    test_status_->setTextFormat(Qt::PlainText); // carries the feed's own title
    test_status_->hide();
    setStyleSheet(QString("QLabel#rssFeedEditTestStatus[probeState=\"ok\"] { color: %1; }"
                          "QLabel#rssFeedEditTestStatus[probeState=\"warn\"] { color: %2; }"
                          "QLabel#rssFeedEditTestStatus[probeState=\"err\"] { color: %3; }"
                          "QLabel#rssFeedEditTestStatus[probeState=\"busy\"] { color: %4; }")
                      .arg(ui::colors::POSITIVE(), ui::colors::WARNING(), ui::colors::NEGATIVE(),
                           ui::colors::TEXT_SECONDARY()));
    root->addWidget(test_status_);

    // Buttons
    auto* btn_row = new QHBoxLayout();
    test_btn_ = new QPushButton(tr("Test URL"), this);
    test_btn_->setCursor(Qt::PointingHandCursor);
    btn_row->addWidget(test_btn_);
    btn_row->addStretch();
    cancel_btn_ = new QPushButton(tr("Cancel"), this);
    cancel_btn_->setCursor(Qt::PointingHandCursor);
    btn_row->addWidget(cancel_btn_);
    ok_btn_ = new QPushButton(tr("Save"), this);
    ok_btn_->setDefault(true);
    ok_btn_->setCursor(Qt::PointingHandCursor);
    btn_row->addWidget(ok_btn_);
    root->addLayout(btn_row);

    connect(test_btn_, &QPushButton::clicked, this, &RssFeedEditDialog::on_test);
    connect(cancel_btn_, &QPushButton::clicked, this, &QDialog::reject);
    connect(ok_btn_, &QPushButton::clicked, this, &RssFeedEditDialog::try_accept);
}

services::RSSFeed RssFeedEditDialog::feed() const {
    services::RSSFeed f;
    f.id = id_input_->text().trimmed();
    f.name = name_input_->text().trimmed();
    f.url = url_input_->text().trimmed();
    f.source = source_input_->text().trimmed();
    f.category = category_combo_->currentText().trimmed().toUpper();
    f.region = region_combo_->currentText().trimmed().toUpper();
    f.tier = tier_spin_->value();
    return f;
}

void RssFeedEditDialog::on_test() {
    const QString url = url_input_->text().trimmed();
    if (url.isEmpty() || !QUrl(url).isValid()) {
        set_status(tr("✗ Enter a valid URL first."), "err");
        return;
    }
    test_btn_->setEnabled(false);
    test_btn_->setText(tr("Testing..."));
    set_status(tr("Fetching..."), "busy");

    QPointer<RssFeedEditDialog> self = this;
    services::RssFeedProbeService::instance().probe(url, this, [self](const services::RssProbeResult& r) {
        if (!self)
            return;
        self->test_btn_->setEnabled(true);
        self->test_btn_->setText(tr("Test URL"));
        self->test_run_ = true;
        self->last_test_ok_ = r.ok() && r.item_count > 0;
        const QString text = services::RssFeedProbeService::describe(r);
        self->set_status(text, !r.ok() ? "err" : (text.startsWith(QStringLiteral("⚠")) ? "warn" : "ok"));
    });
}

void RssFeedEditDialog::try_accept() {
    auto f = feed();
    QStringList missing;
    if (f.id.isEmpty())
        missing << tr("ID");
    if (f.name.isEmpty())
        missing << tr("Name");
    if (f.url.isEmpty())
        missing << tr("URL");
    if (f.source.isEmpty())
        missing << tr("Source");
    if (!missing.isEmpty()) {
        QMessageBox::warning(this, tr("Missing fields"), tr("Please fill in: %1").arg(missing.join(", ")));
        return;
    }
    if (!QUrl(f.url).isValid() || !(f.url.startsWith("http://") || f.url.startsWith("https://"))) {
        QMessageBox::warning(this, tr("Invalid URL"), tr("URL must start with http:// or https://."));
        return;
    }
    if (initial_.id.isEmpty()) {
        // New feed: upsert would silently overwrite an existing feed (built-in or
        // user) that happens to share the typed ID.
        const auto existing = services::NewsService::instance().list_all_feeds_for_editor();
        const bool taken = std::any_of(existing.cbegin(), existing.cend(),
                                       [&f](const auto& e) { return e.feed.id == f.id; });
        if (taken) {
            QMessageBox::warning(this, tr("ID already in use"),
                                 tr("A feed with the ID \"%1\" already exists. Choose a different ID.").arg(f.id));
            return;
        }
    }

    if (!test_run_) {
        // Validate before saving (async — chains to accept() on success).
        test_btn_->setEnabled(false);
        ok_btn_->setEnabled(false);
        set_status(tr("Validating URL before save..."), "busy");

        QPointer<RssFeedEditDialog> self = this;
        services::RssFeedProbeService::instance().probe(f.url, this, [self](const services::RssProbeResult& r) {
            if (!self)
                return;
            self->test_btn_->setEnabled(true);
            self->ok_btn_->setEnabled(true);
            self->test_run_ = true;
            self->last_test_ok_ = r.ok() && r.item_count > 0;
            if (self->last_test_ok_) {
                self->accept();
                return;
            }
            const QString msg = services::RssFeedProbeService::describe(r);
            self->set_status(msg, r.ok() ? "warn" : "err");
            const auto choice =
                QMessageBox::question(self, tr("URL didn't validate"), tr("%1\n\nSave the feed anyway?").arg(msg),
                                      QMessageBox::Save | QMessageBox::Cancel);
            if (choice == QMessageBox::Save)
                self->accept();
        });
        return;
    }

    if (!last_test_ok_) {
        auto choice = QMessageBox::question(this, tr("URL didn't validate"),
                                            tr("The last URL test didn't return valid RSS. Save anyway?"),
                                            QMessageBox::Save | QMessageBox::Cancel);
        if (choice != QMessageBox::Save)
            return;
    }
    accept();
}

} // namespace fincept::screens