File indexing completed on 2024-04-21 05:10:33

0001 /*
0002     This file is part of Akregator.
0003 
0004     SPDX-FileCopyrightText: 2004 Stanislav Karchebny <Stanislav.Karchebny@kdemail.net>
0005 
0006     SPDX-License-Identifier: GPL-2.0-or-later WITH Qt-Commercial-exception-1.0
0007 */
0008 
0009 #include "addfeeddialog.h"
0010 #include "feed.h"
0011 #include "kernel.h"
0012 
0013 #include <KLocalizedString>
0014 #include <KMessageBox>
0015 #include <QDialogButtonBox>
0016 #include <QPushButton>
0017 #include <QStyle>
0018 #include <QUrl>
0019 #include <QVBoxLayout>
0020 
0021 using namespace Akregator;
0022 AddFeedWidget::AddFeedWidget(QWidget *parent)
0023     : QWidget(parent)
0024 {
0025     setupUi(this);
0026     pixmapLabel1->setPixmap(QIcon::fromTheme(QStringLiteral("applications-internet")).pixmap(style()->pixelMetric(QStyle::PM_MessageBoxIconSize)));
0027     statusLabel->setText(QString());
0028 }
0029 
0030 AddFeedWidget::~AddFeedWidget() = default;
0031 
0032 QSize AddFeedDialog::sizeHint() const
0033 {
0034     QSize sh = QDialog::sizeHint();
0035     sh.setHeight(minimumSize().height());
0036     sh.setWidth(sh.width() * 1.2);
0037     return sh;
0038 }
0039 
0040 Feed *AddFeedDialog::feed() const
0041 {
0042     return m_feed;
0043 }
0044 
0045 AddFeedDialog::AddFeedDialog(QWidget *parent, const QString &name)
0046     : QDialog(parent)
0047 {
0048     setObjectName(name);
0049     setWindowTitle(i18nc("@title:window", "Add Feed"));
0050     auto mainLayout = new QVBoxLayout(this);
0051 
0052     widget = new AddFeedWidget(this);
0053     auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, this);
0054     mOkButton = buttonBox->button(QDialogButtonBox::Ok);
0055     mOkButton->setDefault(true);
0056     mOkButton->setShortcut(Qt::CTRL | Qt::Key_Return);
0057     connect(buttonBox, &QDialogButtonBox::accepted, this, &AddFeedDialog::accept);
0058     connect(buttonBox, &QDialogButtonBox::rejected, this, &AddFeedDialog::reject);
0059 
0060     mainLayout->addWidget(widget);
0061     mainLayout->addWidget(buttonBox);
0062 
0063     widget->urlEdit->setFocus();
0064     connect(widget->urlEdit, &QLineEdit::textChanged, this, &AddFeedDialog::textChanged);
0065     mOkButton->setEnabled(false);
0066 }
0067 
0068 AddFeedDialog::~AddFeedDialog() = default;
0069 
0070 void AddFeedDialog::setUrl(const QString &t)
0071 {
0072     widget->urlEdit->setText(t);
0073 }
0074 
0075 void AddFeedDialog::accept()
0076 {
0077     mOkButton->setEnabled(false);
0078     mFeedUrl = widget->urlEdit->text().trimmed();
0079 
0080     delete m_feed;
0081     m_feed = new Feed(Kernel::self()->storage());
0082 
0083     // HACK: make weird wordpress links ("feed:http://foobar/rss") work
0084     if (mFeedUrl.startsWith(QLatin1StringView("feed:http"))) {
0085         mFeedUrl = mFeedUrl.right(mFeedUrl.length() - 5);
0086     }
0087 
0088     if (!mFeedUrl.contains(QLatin1StringView(":/"))) {
0089         mFeedUrl.prepend(QLatin1StringView("https://"));
0090     }
0091 
0092     QUrl asUrl(mFeedUrl);
0093     if (asUrl.scheme() == QLatin1StringView("feed")) {
0094         asUrl.setScheme(QStringLiteral("https"));
0095         mFeedUrl = asUrl.url();
0096     }
0097     m_feed->setXmlUrl(mFeedUrl);
0098 
0099     widget->statusLabel->setText(i18n("Downloading %1", mFeedUrl));
0100 
0101     connect(m_feed, &Feed::fetched, this, &AddFeedDialog::fetchCompleted);
0102     connect(m_feed, &Feed::fetchError, this, &AddFeedDialog::fetchError);
0103     connect(m_feed, &Feed::fetchDiscovery, this, &AddFeedDialog::fetchDiscovery);
0104 
0105     m_feed->fetch(true);
0106 }
0107 
0108 void AddFeedDialog::fetchCompleted(Feed * /*f*/)
0109 {
0110     QDialog::accept();
0111 }
0112 
0113 void AddFeedDialog::fetchError(Feed *)
0114 {
0115     KMessageBox::error(this, i18n("Feed not found from %1.", mFeedUrl));
0116     QDialog::reject();
0117 }
0118 
0119 void AddFeedDialog::fetchDiscovery(Feed *f)
0120 {
0121     widget->statusLabel->setText(i18n("Feed found, downloading..."));
0122     mFeedUrl = f->xmlUrl();
0123 }
0124 
0125 void AddFeedDialog::textChanged(const QString &text)
0126 {
0127     mOkButton->setEnabled(!text.trimmed().isEmpty());
0128 }
0129 
0130 #include "moc_addfeeddialog.cpp"