| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #ifndef FORMFEEDDETAILS_H |
| 4 | #define FORMFEEDDETAILS_H |
| 5 | |
| 6 | #include "3rd-party/boolinq/boolinq.h" |
| 7 | #include "definitions/definitions.h" |
| 8 | |
| 9 | #include <QDialog> |
| 10 | |
| 11 | namespace Ui { |
| 12 | class FormFeedDetails; |
| 13 | } |
| 14 | |
| 15 | class ServiceRoot; |
| 16 | class Feed; |
| 17 | class Category; |
| 18 | class RootItem; |
| 19 | class QDialogButtonBox; |
| 20 | class MultiFeedEditCheckBox; |
| 21 | |
| 22 | class RSSGUARD_DLLSPEC FormFeedDetails : public QDialog { |
| 23 | Q_OBJECT |
| 24 | |
| 25 | public: |
| 26 | explicit FormFeedDetails(ServiceRoot* service_root, QWidget* parent = nullptr); |
| 27 | virtual ~FormFeedDetails(); |
| 28 | |
| 29 | template <class T> |
| 30 | QList<T*> addEditFeed(const QList<Feed*>& feeds_to_edit = {}); |
| 31 | |
| 32 | // Returns first feed. |
| 33 | template <class T> |
| 34 | T* feed() const; |
| 35 | |
| 36 | // Returns all feeds. |
| 37 | template <class T> |
| 38 | QList<T*> feeds() const; |
| 39 | |
| 40 | protected slots: |
| 41 | void activateTab(int index); |
| 42 | void clearTabs(); |
| 43 | |
| 44 | // Applies changes. |
| 45 | // NOTE: This must be reimplemented in subclasses. Also this |
| 46 | // base implementation must be called first. |
| 47 | virtual void apply(); |
| 48 | |
| 49 | protected: |
| 50 | QDialogButtonBox* buttonBox() const; |
| 51 | |
| 52 | bool isChangeAllowed(MultiFeedEditCheckBox* mcb) const; |
| 53 | |
| 54 | // Inserts custom tab made by plugin into its editing form. When index is less |
| 55 | // than zero, tab is inserted to the last position. |
| 56 | void insertCustomTab(QWidget* custom_tab, const QString& title, int index = -1); |
| 57 | |
| 58 | // Sets the feed which will be edited. |
| 59 | // NOTE: This must be reimplemented in subclasses. Also this |
| 60 | // base implementation must be called first. |
| 61 | virtual void loadFeedData(); |
| 62 | |
| 63 | private slots: |
| 64 | void acceptIfPossible(); |
| 65 | void onAutoUpdateTypeChanged(int new_index); |
| 66 | |
| 67 | private: |
| 68 | void createConnections(); |
| 69 | void initialize(); |
| 70 | |
| 71 | protected: |
| 72 | QScopedPointer<Ui::FormFeedDetails> m_ui; |
| 73 | QList<Feed*> m_feeds; |
| 74 | ServiceRoot* m_serviceRoot; |
| 75 | bool m_creatingNew; |
| 76 | bool m_isBatchEdit; |
| 77 | }; |
| 78 | |
| 79 | template <class T> |
| 80 | inline QList<T*> FormFeedDetails::addEditFeed(const QList<Feed*>& feeds_to_edit) { |
| 81 | m_creatingNew = feeds_to_edit.isEmpty(); |
| 82 | m_isBatchEdit = feeds_to_edit.size() > 1; |
| 83 | |
| 84 | if (m_creatingNew) { |
| 85 | m_feeds.append(new T()); |
| 86 | } |
| 87 | else { |
| 88 | m_feeds.append(feeds_to_edit); |
| 89 | } |
| 90 | |
| 91 | loadFeedData(); |
| 92 | |
| 93 | if (exec() == QDialog::DialogCode::Accepted) { |
| 94 | return feeds<T>(); |
| 95 | } |
| 96 | else { |
| 97 | return {}; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | template <class T> |
| 102 | inline T* FormFeedDetails::feed() const { |
| 103 | return qobject_cast<T*>(m_feeds.first()); |
| 104 | } |
| 105 | |
| 106 | template <class T> |
| 107 | inline QList<T*> FormFeedDetails::feeds() const { |
| 108 | std::list<T*> std_fds = boolinq::from(m_feeds) |
| 109 | .select([](Feed* fd) { |
| 110 | return qobject_cast<T*>(fd); |
| 111 | }) |
| 112 | .toStdList(); |
| 113 | |
| 114 | return FROM_STD_LIST(QList<T*>, std_fds); |
| 115 | } |
| 116 | |
| 117 | #endif // FORMFEEDDETAILS_H |