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 "addteamroomcompletionlineedit.h"
0008 #include "common/completionlistview.h"
0009 #include "connection.h"
0010 #include "model/teamroomcompletermodel.h"
0011 #include "rocketchataccount.h"
0012 #include "ruqolawidgets_debug.h"
0013 #include "teams/roomsautocompleteavailableforteamsjob.h"
0014 #include "teams/teamroomcompleter.h"
0015 
0016 #include <KLocalizedString>
0017 
0018 #include <QJsonArray>
0019 #include <QTimer>
0020 #include <chrono>
0021 
0022 using namespace std::chrono_literals;
0023 
0024 AddTeamRoomCompletionLineEdit::AddTeamRoomCompletionLineEdit(RocketChatAccount *account, QWidget *parent)
0025     : CompletionLineEdit(parent)
0026     , mTeamRoomCompleterModel(new TeamRoomCompleterModel(this))
0027     , mSearchTimer(new QTimer(this))
0028     , mRocketChatAccount(account)
0029 {
0030     setPlaceholderText(i18n("Search rooms..."));
0031     setCompletionModel(mTeamRoomCompleterModel);
0032     connect(this, &AddTeamRoomCompletionLineEdit::complete, this, &AddTeamRoomCompletionLineEdit::slotComplete);
0033 
0034     connect(mSearchTimer, &QTimer::timeout, this, &AddTeamRoomCompletionLineEdit::slotSearchTimerFired);
0035     connect(this, &QLineEdit::textChanged, this, &AddTeamRoomCompletionLineEdit::slotSearchTextEdited);
0036 }
0037 
0038 AddTeamRoomCompletionLineEdit::~AddTeamRoomCompletionLineEdit() = default;
0039 
0040 void AddTeamRoomCompletionLineEdit::slotSearchTimerFired()
0041 {
0042     mSearchTimer->stop();
0043     slotTextChanged(text());
0044 }
0045 
0046 void AddTeamRoomCompletionLineEdit::slotSearchTextEdited()
0047 {
0048     if (mSearchTimer->isActive()) {
0049         mSearchTimer->stop(); // eventually
0050     }
0051 
0052     mSearchTimer->setSingleShot(true);
0053     mSearchTimer->start(500ms);
0054 }
0055 
0056 void AddTeamRoomCompletionLineEdit::slotTextChanged(const QString &text)
0057 {
0058     if (text.trimmed().isEmpty()) {
0059         mTeamRoomCompleterModel->clear();
0060     } else {
0061         auto job = new RocketChatRestApi::RoomsAutocompleteAvailableForTeamsJob(this);
0062 
0063         RocketChatRestApi::RoomsAutocompleteAvailableForTeamsJob::RoomsAutocompleteChannelAndPrivateInfo info;
0064         info.name = text;
0065         job->setRoomsCompleterInfo(info);
0066         mRocketChatAccount->restApi()->initializeRestApiJob(job);
0067         connect(job,
0068                 &RocketChatRestApi::RoomsAutocompleteAvailableForTeamsJob::roomsAutoCompleteChannelAndPrivateDone,
0069                 this,
0070                 &AddTeamRoomCompletionLineEdit::slotAutoCompletTeamRoomDone);
0071         if (!job->start()) {
0072             qCWarning(RUQOLAWIDGETS_LOG) << "Impossible to start TeamsListRoomsJob job";
0073         }
0074     }
0075 }
0076 
0077 void AddTeamRoomCompletionLineEdit::slotAutoCompletTeamRoomDone(const QJsonObject &obj)
0078 {
0079     // qDebug() << " obj " << obj;
0080     const QJsonArray items = obj[QLatin1String("items")].toArray();
0081     QVector<TeamRoomCompleter> teams;
0082     for (int i = 0, total = items.count(); i < total; ++i) {
0083         TeamRoomCompleter teamCompleter;
0084         teamCompleter.parse(items.at(i).toObject());
0085         teams.append(std::move(teamCompleter));
0086     }
0087     mTeamRoomCompleterModel->setRooms(teams);
0088     if (teams.isEmpty()) {
0089         mCompletionListView->hide();
0090     }
0091 }
0092 
0093 void AddTeamRoomCompletionLineEdit::slotComplete(const QModelIndex &index)
0094 {
0095     const QString completerName = index.data(TeamRoomCompleterModel::TeamName).toString();
0096     const QString roomId = index.data(TeamRoomCompleterModel::TeamId).toString();
0097     RoomCompletionInfo info;
0098     info.roomName = completerName;
0099     info.roomId = roomId;
0100     mCompletionListView->hide();
0101     disconnect(this, &QLineEdit::textChanged, this, &AddTeamRoomCompletionLineEdit::slotSearchTextEdited);
0102     Q_EMIT newRoomName(info);
0103     clear();
0104     connect(this, &QLineEdit::textChanged, this, &AddTeamRoomCompletionLineEdit::slotSearchTextEdited);
0105 }
0106 
0107 #include "moc_addteamroomcompletionlineedit.cpp"