1 // For license of this file, see <project-root-folder>/LICENSE.md.
2 
3 #include "services/tt-rss/gui/ttrssfeeddetails.h"
4 
5 #include "services/abstract/category.h"
6 
TtRssFeedDetails(QWidget * parent)7 TtRssFeedDetails::TtRssFeedDetails(QWidget* parent) : QWidget(parent) {
8   ui.setupUi(this);
9 
10   ui.m_txtUrl->lineEdit()->setPlaceholderText(tr("Full feed URL including scheme"));
11   ui.m_txtUrl->lineEdit()->setToolTip(tr("Provide URL for your feed."));
12 
13   connect(ui.m_txtUrl->lineEdit(), &BaseLineEdit::textChanged, this, &TtRssFeedDetails::onUrlChanged);
14   onUrlChanged(QString());
15 }
16 
onUrlChanged(const QString & new_url)17 void TtRssFeedDetails::onUrlChanged(const QString& new_url) {
18   if (QRegularExpression(QSL(URL_REGEXP)).match(new_url).hasMatch()) {
19     // New url is well-formed.
20     ui.m_txtUrl->setStatus(LineEditWithStatus::StatusType::Ok, tr("The URL is ok."));
21   }
22   else if (!new_url.simplified().isEmpty()) {
23     // New url is not well-formed but is not empty on the other hand.
24     ui.m_txtUrl->setStatus(LineEditWithStatus::StatusType::Warning,
25                            tr(R"(The URL does not meet standard pattern. Does your URL start with "http://" or "https://" prefix.)"));
26   }
27   else {
28     // New url is empty.
29     ui.m_txtUrl->setStatus(LineEditWithStatus::StatusType::Error, tr("The URL is empty."));
30   }
31 }
32 
loadCategories(const QList<Category * > & categories,RootItem * root_item,RootItem * parent_to_select)33 void TtRssFeedDetails::loadCategories(const QList<Category*>& categories, RootItem* root_item, RootItem* parent_to_select) {
34   ui.m_cmbParentCategory->addItem(root_item->fullIcon(), root_item->title(), QVariant::fromValue((void*) root_item));
35 
36   for (Category* category : categories) {
37     ui.m_cmbParentCategory->addItem(category->fullIcon(), category->title(), QVariant::fromValue((void*) category));
38   }
39 
40   if (parent_to_select != nullptr) {
41     if (parent_to_select->kind() == RootItem::Kind::Category) {
42       ui.m_cmbParentCategory->setCurrentIndex(ui.m_cmbParentCategory->findData(QVariant::fromValue((void*)parent_to_select)));
43     }
44     else if (parent_to_select->kind() == RootItem::Kind::Feed) {
45       int target_item = ui.m_cmbParentCategory->findData(QVariant::fromValue((void*)parent_to_select->parent()));
46 
47       if (target_item >= 0) {
48         ui.m_cmbParentCategory->setCurrentIndex(target_item);
49       }
50     }
51   }
52 }
53