File indexing completed on 2024-12-01 04:37:04

0001 /*
0002    SPDX-FileCopyrightText: 2021-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "searchteamcompletionlineedit.h"
0008 #include "common/completionlistview.h"
0009 #include "connection.h"
0010 #include "model/teamcompletermodel.h"
0011 #include "rocketchataccount.h"
0012 #include "ruqolawidgets_debug.h"
0013 #include "teams/teamsautocompletejob.h"
0014 
0015 #include <QJsonArray>
0016 #include <QTimer>
0017 #include <chrono>
0018 
0019 using namespace std::chrono_literals;
0020 
0021 SearchTeamCompletionLineEdit::SearchTeamCompletionLineEdit(RocketChatAccount *account, QWidget *parent)
0022     : CompletionLineEdit(parent)
0023     , mTeamCompleterModel(new TeamCompleterModel(this))
0024     , mSearchTimer(new QTimer(this))
0025     , mRocketChatAccount(account)
0026 {
0027     setCompletionModel(mTeamCompleterModel);
0028     connect(this, &SearchTeamCompletionLineEdit::complete, this, &SearchTeamCompletionLineEdit::slotComplete);
0029 
0030     connect(mSearchTimer, &QTimer::timeout, this, &SearchTeamCompletionLineEdit::slotSearchTimerFired);
0031     connect(this, &QLineEdit::textChanged, this, &SearchTeamCompletionLineEdit::slotSearchTextEdited);
0032 }
0033 
0034 SearchTeamCompletionLineEdit::~SearchTeamCompletionLineEdit() = default;
0035 
0036 const QString &SearchTeamCompletionLineEdit::teamId() const
0037 {
0038     return mTeamId;
0039 }
0040 
0041 void SearchTeamCompletionLineEdit::slotSearchTimerFired()
0042 {
0043     mSearchTimer->stop();
0044     slotTextChanged(text());
0045 }
0046 
0047 void SearchTeamCompletionLineEdit::slotSearchTextEdited()
0048 {
0049     if (mSearchTimer->isActive()) {
0050         mSearchTimer->stop(); // eventually
0051     }
0052 
0053     mSearchTimer->setSingleShot(true);
0054     mSearchTimer->start(500ms);
0055 }
0056 
0057 void SearchTeamCompletionLineEdit::slotTextChanged(const QString &text)
0058 {
0059     if (text.trimmed().isEmpty()) {
0060         mTeamCompleterModel->clear();
0061         mTeamId.clear();
0062         Q_EMIT teamIdChanged(true);
0063     } else {
0064         auto job = new RocketChatRestApi::TeamsAutoCompleteJob(this);
0065 
0066         job->setName(text);
0067         mRocketChatAccount->restApi()->initializeRestApiJob(job);
0068         connect(job, &RocketChatRestApi::TeamsAutoCompleteJob::teamAutoCompleteDone, this, &SearchTeamCompletionLineEdit::slotTeamAutoCompleteDone);
0069         if (!job->start()) {
0070             qCWarning(RUQOLAWIDGETS_LOG) << "Impossible to start TeamsListRoomsJob job";
0071         }
0072     }
0073 }
0074 
0075 void SearchTeamCompletionLineEdit::slotTeamAutoCompleteDone(const QJsonObject &obj)
0076 {
0077     // qDebug() << " obj " << obj;
0078     const QJsonArray items = obj[QLatin1String("teams")].toArray();
0079     QVector<TeamCompleter> teams;
0080     const auto total = items.count();
0081     teams.reserve(total);
0082     for (auto i = 0; i < total; ++i) {
0083         TeamCompleter teamCompleter;
0084         teamCompleter.parse(items.at(i).toObject());
0085         teams.append(std::move(teamCompleter));
0086     }
0087     mTeamCompleterModel->insertTeams(teams);
0088     if (teams.isEmpty()) {
0089         mCompletionListView->hide();
0090     }
0091 }
0092 
0093 void SearchTeamCompletionLineEdit::slotComplete(const QModelIndex &index)
0094 {
0095     const QString completerName = index.data(TeamCompleterModel::TeamName).toString();
0096     const QString teamIdentifier = index.data(TeamCompleterModel::TeamId).toString();
0097     mCompletionListView->hide();
0098     disconnect(this, &QLineEdit::textChanged, this, &SearchTeamCompletionLineEdit::slotSearchTextEdited);
0099     setText(completerName);
0100     mTeamId = teamIdentifier;
0101     connect(this, &QLineEdit::textChanged, this, &SearchTeamCompletionLineEdit::slotSearchTextEdited);
0102     Q_EMIT teamIdChanged(mTeamId.isEmpty());
0103 }
0104 
0105 #include "moc_searchteamcompletionlineedit.cpp"