| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #include "services/abstract/searchsnode.h" |
| 4 | |
| 5 | #include "3rd-party/boolinq/boolinq.h" |
| 6 | #include "database/databasefactory.h" |
| 7 | #include "database/databasequeries.h" |
| 8 | #include "exceptions/applicationexception.h" |
| 9 | #include "miscellaneous/application.h" |
| 10 | #include "miscellaneous/iconfactory.h" |
| 11 | #include "services/abstract/gui/formaddeditprobe.h" |
| 12 | #include "services/abstract/serviceroot.h" |
| 13 | |
| 14 | SearchsNode::SearchsNode(RootItem* parent_item) : RootItem(parent_item), m_actProbeNew(nullptr) { |
| 15 | setKind(RootItem::Kind::Probes); |
| 16 | setId(ID_PROBES); |
| 17 | setIcon(qApp->icons()->fromTheme(QSL("system-search"))); |
| 18 | setTitle(tr("Regex queries")); |
| 19 | setDescription(tr("You can see all your permanent regex queries here.")); |
| 20 | } |
| 21 | |
| 22 | void SearchsNode::loadProbes(const QList<Search*>& probes) { |
| 23 | for (auto* prb : probes) { |
| 24 | appendChild(prb); |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | QList<Message> SearchsNode::undeletedMessages() const { |
| 29 | QSqlDatabase database = qApp->database()->driver()->connection(metaObject()->className()); |
| 30 | |
| 31 | return {}; |
| 32 | } |
| 33 | |
| 34 | Search* SearchsNode::probeById(const QString& custom_id) { |
| 35 | auto chi = childItems(); |
| 36 | |
| 37 | return qobject_cast<Search*>(boolinq::from(chi).firstOrDefault([custom_id](RootItem* it) { |
| 38 | return it->customId() == custom_id; |
| 39 | })); |
| 40 | } |
| 41 | |
| 42 | QList<Search*> SearchsNode::probes() const { |
| 43 | auto list = boolinq::from(childItems()) |
| 44 | .select([](RootItem* it) { |
| 45 | return static_cast<Search*>(it); |
| 46 | }) |
| 47 | .toStdList(); |
| 48 | |
| 49 | return FROM_STD_LIST(QList<Search*>, list); |
| 50 | } |
| 51 | |
| 52 | QList<QAction*> SearchsNode::contextMenuFeedsList() { |
| 53 | if (m_actProbeNew == nullptr) { |
| 54 | m_actProbeNew = new QAction(qApp->icons()->fromTheme(QSL("system-search")), tr("New regex query"), this); |
| 55 | |
| 56 | connect(m_actProbeNew, &QAction::triggered, this, &SearchsNode::createProbe); |
| 57 | } |
| 58 | |
| 59 | return QList<QAction*>{m_actProbeNew}; |
| 60 | } |
| 61 | |
| 62 | int SearchsNode::countOfUnreadMessages() const { |
| 63 | return -1; |
| 64 | } |
| 65 | |
| 66 | int SearchsNode::countOfAllMessages() const { |
| 67 | return -1; |
| 68 | } |
| 69 | |
| 70 | void SearchsNode::createProbe() { |
| 71 | FormAddEditProbe frm(qApp->mainFormWidget()); |
| 72 | Search* new_prb = frm.execForAdd(); |
| 73 | |
| 74 | if (new_prb != nullptr) { |
| 75 | QSqlDatabase db = qApp->database()->driver()->connection(metaObject()->className()); |
| 76 | |
| 77 | try { |
| 78 | DatabaseQueries::createProbe(db, new_prb, getParentServiceRoot()->accountId()); |
| 79 | |
| 80 | getParentServiceRoot()->requestItemReassignment(new_prb, this); |
| 81 | getParentServiceRoot()->requestItemExpand({this}, true); |
| 82 | |
| 83 | new_prb->updateCounts(true); |
| 84 | } |
| 85 | catch (const ApplicationException&) { |
| 86 | new_prb->deleteLater(); |
| 87 | } |
| 88 | } |
| 89 | } |