File indexing completed on 2024-04-28 04:55:51

0001 /*
0002     This file is part of Choqok, the KDE micro-blogging client
0003 
0004     SPDX-FileCopyrightText: 2011-2012 Mehrdad Momeny <mehrdad.momeny@gmail.com>
0005     SPDX-FileCopyrightText: 2011 Farhad Hedayati-Fard <hf.farhad@gmail.com>
0006 
0007     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0008 */
0009 
0010 #include "quickfilter.h"
0011 
0012 #include <QAction>
0013 #include <QLabel>
0014 #include <QLineEdit>
0015 #include <QPushButton>
0016 #include <QToolBar>
0017 
0018 #include <KActionCollection>
0019 #include <KLocalizedString>
0020 #include <KPluginFactory>
0021 
0022 #include "choqoktypes.h"
0023 #include "choqokuiglobal.h"
0024 #include "microblogwidget.h"
0025 #include "postwidget.h"
0026 #include "quickpost.h"
0027 #include "timelinewidget.h"
0028 
0029 K_PLUGIN_CLASS_WITH_JSON(QuickFilter, "choqok_quickfilter.json")
0030 
0031 QuickFilter::QuickFilter(QObject *parent, const QList< QVariant > &args)
0032     : Choqok::Plugin(QLatin1String("choqok_quickfilter"), parent)
0033 {
0034     Q_UNUSED(args);
0035     m_authorAction = new QAction(QIcon::fromTheme(QLatin1String("document-preview")), i18n("Filter by author"), this);
0036     m_authorAction->setCheckable(true);
0037     m_textAction = new QAction(QIcon::fromTheme(QLatin1String("document-preview")), i18n("Filter by content"), this);
0038     m_textAction->setCheckable(true);
0039     actionCollection()->addAction(QLatin1String("filterByAuthor"), m_authorAction);
0040     actionCollection()->addAction(QLatin1String("filterByContent"), m_textAction);
0041     setXMLFile(QLatin1String("quickfilterui.rc"));
0042     createUiInterface();
0043     connect(Choqok::UI::Global::mainWindow(), &Choqok::UI::MainWindow::currentMicroBlogWidgetChanged,
0044             this, &QuickFilter::showAllPosts);
0045 }
0046 
0047 QuickFilter::~QuickFilter()
0048 {
0049 
0050 }
0051 
0052 void QuickFilter::filterByAuthor()
0053 {
0054     m_filterUser = m_aledit->text();
0055     if (!m_filterUser.isEmpty() && Choqok::UI::Global::mainWindow()->currentMicroBlog()->currentTimeline()) {
0056         for (Choqok::UI::PostWidget *postwidget:
0057                    Choqok::UI::Global::mainWindow()->currentMicroBlog()->currentTimeline()->postWidgets()) {
0058             if (!postwidget->currentPost()->author.userName.contains(m_filterUser, Qt::CaseInsensitive)) {
0059                 postwidget->hide();
0060             } else {
0061                 postwidget->show();
0062             }
0063         }
0064         connect(Choqok::UI::Global::SessionManager::self(), &Choqok::UI::Global::SessionManager::newPostWidgetAdded,
0065                 this, &QuickFilter::filterNewPost);
0066     } else {
0067         showAllPosts();
0068     }
0069 }
0070 
0071 void QuickFilter::filterByContent()
0072 {
0073     m_filterText = m_tledit->text();
0074     if (!m_filterText.isEmpty() && Choqok::UI::Global::mainWindow()->currentMicroBlog()->currentTimeline()) {
0075         for (Choqok::UI::PostWidget *postwidget:
0076                    Choqok::UI::Global::mainWindow()->currentMicroBlog()->currentTimeline()->postWidgets()) {
0077             if (!postwidget->currentPost()->content.contains(m_filterText, Qt::CaseInsensitive)) {
0078                 postwidget->hide();
0079             } else {
0080                 postwidget->show();
0081             }
0082         }
0083         connect(Choqok::UI::Global::SessionManager::self(), &Choqok::UI::Global::SessionManager::newPostWidgetAdded,
0084                 this, &QuickFilter::filterNewPost);
0085     } else {
0086         showAllPosts();
0087     }
0088 }
0089 
0090 void QuickFilter::createUiInterface()
0091 {
0092     m_authorToolbar = new QToolBar(i18n("Filter out timeline by author"), Choqok::UI::Global::mainWindow());
0093     m_authorToolbar->setObjectName(QLatin1String("authorFilterToolbar"));
0094     m_textToolbar = new QToolBar(i18n("Filter out timeline by text"), Choqok::UI::Global::mainWindow());
0095     m_textToolbar->setObjectName(QLatin1String("textFilterToolbar"));
0096     connect(m_authorAction, &QAction::toggled, m_authorToolbar, &QToolBar::setVisible);
0097     connect(m_textAction, &QAction::toggled, m_textToolbar, &QToolBar::setVisible);
0098     connect(m_authorToolbar, &QToolBar::visibilityChanged, this, &QuickFilter::showAuthorFilterUiInterface);
0099     connect(m_textToolbar, &QToolBar::visibilityChanged, this, &QuickFilter::showContentFilterUiInterface);
0100     m_aledit = new QLineEdit(m_authorToolbar);
0101     m_aledit->setClearButtonEnabled(true);
0102 
0103     m_tledit = new QLineEdit(m_textToolbar);
0104     m_tledit->setClearButtonEnabled(true);
0105 
0106     QLabel *alabel = new QLabel(i18n("Author"), m_authorToolbar);
0107     QLabel *tlabel = new QLabel(i18n("Text"), m_textToolbar);
0108     m_authorToolbar->addWidget(alabel);
0109     m_authorToolbar->addWidget(m_aledit);
0110     QPushButton *authorCloseButton = new QPushButton(QIcon::fromTheme(QLatin1String("dialog-close")), QString() , m_authorToolbar);
0111     authorCloseButton->setMaximumWidth(authorCloseButton->height());
0112     connect(authorCloseButton, &QPushButton::clicked, m_authorToolbar, &QToolBar::hide);
0113     m_authorToolbar->addWidget(authorCloseButton);
0114 
0115     m_textToolbar->addWidget(tlabel);
0116     m_textToolbar->addWidget(m_tledit);
0117     QPushButton *textCloseButton = new QPushButton(QIcon::fromTheme(QLatin1String("dialog-close")), QString() , m_textToolbar);
0118     textCloseButton->setMaximumWidth(textCloseButton->height());
0119     connect(textCloseButton, &QPushButton::clicked, m_textToolbar, &QToolBar::hide);
0120     m_textToolbar->addWidget(textCloseButton);
0121 
0122     connect(m_aledit, &QLineEdit::editingFinished, this, &QuickFilter::filterByAuthor);
0123     connect(m_aledit, &QLineEdit::textChanged, this, &QuickFilter::updateUser);
0124 
0125     connect(m_tledit, &QLineEdit::editingFinished, this, &QuickFilter::filterByContent);
0126     connect(m_tledit, &QLineEdit::textChanged, this, &QuickFilter::updateContent);
0127 
0128     Choqok::UI::Global::mainWindow()->addToolBar(Qt::BottomToolBarArea, m_authorToolbar);
0129     Choqok::UI::Global::mainWindow()->addToolBar(Qt::BottomToolBarArea, m_textToolbar);
0130     m_authorToolbar->hide();
0131     m_textToolbar->hide();
0132 }
0133 
0134 void QuickFilter::showAuthorFilterUiInterface(bool show)
0135 {
0136     m_authorToolbar->setVisible(show);
0137     if (show) {
0138         m_textAction->setChecked(false);
0139         m_aledit->setFocus();
0140     } else {
0141         m_aledit->clear();
0142         m_authorAction->setChecked(false);
0143     }
0144 }
0145 
0146 void QuickFilter::showContentFilterUiInterface(bool show)
0147 {
0148     m_textToolbar->setVisible(show);
0149     if (show) {
0150         m_authorAction->setChecked(false);
0151         m_tledit->setFocus();
0152     } else {
0153         m_tledit->clear();
0154         m_textAction->setChecked(false);
0155     }
0156 }
0157 
0158 void QuickFilter::updateUser(QString user)
0159 {
0160     if (user.isEmpty()) {
0161         filterByAuthor();
0162     }
0163 }
0164 
0165 void QuickFilter::updateContent(QString text)
0166 {
0167     if (text.isEmpty()) {
0168         filterByContent();
0169     }
0170 }
0171 
0172 void QuickFilter::showAllPosts()
0173 {
0174     if (Choqok::UI::Global::mainWindow()->currentMicroBlog()->currentTimeline()) {
0175         for (Choqok::UI::PostWidget *postwidget:
0176                    Choqok::UI::Global::mainWindow()->currentMicroBlog()->currentTimeline()->postWidgets()) {
0177             postwidget->show();
0178         }
0179         m_aledit->clear();
0180         m_tledit->clear();
0181         disconnect(Choqok::UI::Global::SessionManager::self(),
0182                    SIGNAL(newPostWidgetAdded(Choqok::UI::PostWidget*,Choqok::Account*,QString)),
0183                    this, SLOT(filterNewPost(Choqok::UI::PostWidget*,Choqok::Account*,QString)));
0184     }
0185 }
0186 
0187 void QuickFilter::filterNewPost(Choqok::UI::PostWidget *np, Choqok::Account *acc, QString timeline)
0188 {
0189     //qDebug()<<Choqok::UI::Global::mainWindow()->currentMicroBlog()->currentAccount()->alias()<<acc->alias()<<timeline;
0190     if (Choqok::UI::Global::mainWindow()->currentMicroBlog()->currentAccount() == acc &&
0191             Choqok::UI::Global::mainWindow()->currentMicroBlog()->currentTimeline()->timelineName() == timeline) {
0192         if (!m_aledit->text().isEmpty()) {
0193             if (!np->currentPost()->author.userName.contains(m_aledit->text())) {
0194                 np->hide();
0195             } else {
0196                 np->show();
0197             }
0198         }
0199         if (!m_tledit->text().isEmpty()) {
0200             if (!np->currentPost()->content.contains(m_tledit->text())) {
0201                 np->hide();
0202             } else {
0203                 np->show();
0204             }
0205         }
0206     }
0207 }
0208 
0209 #include "moc_quickfilter.cpp"
0210 #include "quickfilter.moc"