<commit_before>/*
 * This file is part of telepathy-contactslist-prototype
 *
 * Copyright (C) 2011 <NAME> <<EMAIL>>
 * Copyright (C) 2012 <NAME> <<EMAIL>>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "join-chat-room-dialog.h"
#include "ui_join-chat-room-dialog.h"

#include <KTp/Models/rooms-model.h>

#include <KConfig>
#include <KDebug>
#include <KInputDialog>
#include <KMessageBox>
#include <KNotification>
#include <KPushButton>
#include <KCompletionBox>

#include <TelepathyQt/AccountSet>
#include <TelepathyQt/AccountCapabilityFilter>
#include <TelepathyQt/AccountManager>
#include <TelepathyQt/AccountPropertyFilter>
#include <TelepathyQt/AndFilter>
#include <TelepathyQt/ChannelTypeRoomListInterface>
#include <TelepathyQt/PendingChannel>
#include <TelepathyQt/PendingReady>
#include <TelepathyQt/RequestableChannelClassSpec>
#include <TelepathyQt/RoomListChannel>

#include <QSortFilterProxyModel>

KTp::JoinChatRoomDialog::JoinChatRoomDialog(Tp::AccountManagerPtr accountManager, QWidget* parent)
    : KDialog(parent, Qt::Dialog)
    , ui(new Ui::JoinChatRoomDialog)
    , m_model(new RoomsModel(this))
    , m_favoritesModel(new FavoriteRoomsModel(this))
    , m_favoritesProxyModel(new QSortFilterProxyModel(this))
    , m_recentComp(new KCompletion)
{
    QWidget *joinChatRoomDialog = new QWidget(this);
    ui->setupUi(joinChatRoomDialog);
    setMainWidget(joinChatRoomDialog);
    setWindowIcon(KIcon(QLatin1String("telepathy-kde")));

    // config
    KSharedConfigPtr commonConfig = KSharedConfig::openConfig(QLatin1String("ktelepathyrc"));
    m_favoriteRoomsGroup = commonConfig->group(QLatin1String("FavoriteRooms"));
    m_recentRoomsGroup = commonConfig->group(QLatin1String("RecentChatRooms"));

    Q_FOREACH (const QString &key, m_recentRoomsGroup.keyList()) {
        if (!m_recentRoomsGroup.readEntry(key, QStringList()).isEmpty()) {
            m_recentRooms.insert(key, m_recentRoomsGroup.readEntry(key, QStringList()));
        }
    }

    loadFavoriteRooms();

    // disable OK button on start
    button(Ok)->setEnabled(false);

    //set icons
    ui->addFavoritePushButton->setIcon(KIcon(QLatin1String("list-add")));
    ui->editFavoritePushButton->setIcon(KIcon(QLatin1String("list-edit")));
    ui->removeFavoritePushButton->setIcon(KIcon(QLatin1String("list-remove")));
    ui->removeRecentPushButton->setIcon(KIcon(QLatin1String("list-remove")));
    ui->clearRecentPushButton->setIcon(KIcon(QLatin1String("edit-clear-list")));

    connect(accountManager->becomeReady(), SIGNAL(finished(Tp::PendingOperation*)),
            this, SLOT(onAccountManagerReady(Tp::PendingOperation*)));

    // favoritesTab
    m_favoritesProxyModel->setSourceModel(m_favoritesModel);
    m_favoritesProxyModel->setFilterKeyColumn(FavoriteRoomsModel::AccountIdentifierColumn);
    m_favoritesProxyModel->setDynamicSortFilter(true);

    ui->listView->setModel(m_favoritesProxyModel);
    ui->listView->setModelColumn(FavoriteRoomsModel::NameColumn);

    // recentTab
    m_recentComp->setCompletionMode(KGlobalSettings::CompletionPopup);
    m_recentComp->setIgnoreCase(true);

    ui->lineEdit->setCompletionObject(m_recentComp);
    ui->lineEdit->setAutoDeleteCompletionObject(true);

    QSortFilterProxyModel *proxyModel = new QSortFilterProxyModel(this);
    proxyModel->setSourceModel(m_model);
    proxyModel->setSortLocaleAware(true);
    proxyModel->setSortCaseSensitivity(Qt::CaseInsensitive);
    proxyModel->setFilterKeyColumn(RoomsModel::NameColumn);
    proxyModel->setDynamicSortFilter(true);

    ui->treeView->setModel(proxyModel);
    ui->treeView->header()->setResizeMode(QHeaderView::ResizeToContents);
    ui->treeView->sortByColumn(RoomsModel::NameColumn, Qt::AscendingOrder);

    // connects
    connect(ui->lineEdit, SIGNAL(textChanged(QString)), this, SLOT(onTextChanged(QString)));
    connect(ui->listView, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(accept()));
    connect(ui->listView->selectionModel(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),
            this, SLOT(onFavoriteRoomSelectionChanged(QModelIndex,QModelIndex)));
    connect(m_favoritesModel, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
            this, SLOT(onFavoriteRoomDataChanged(QModelIndex,QModelIndex)));
    connect(ui->addFavoritePushButton, SIGNAL(clicked(bool)), this, SLOT(addFavorite()));
    connect(ui->editFavoritePushButton, SIGNAL(clicked(bool)), this, SLOT(editFavorite()));
    connect(ui->removeFavoritePushButton, SIGNAL(clicked(bool)), this, SLOT(removeFavorite()));
    connect(ui->recentListWidget, SIGNAL(currentTextChanged(QString)), ui->lineEdit, SLOT(setText(QString)));
    connect(ui->recentListWidget, SIGNAL(currentTextChanged(QString)), this, SLOT(onRecentRoomClicked()));
    connect(ui->recentListWidget, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(accept()));
    connect(ui->removeRecentPushButton, SIGNAL(clicked(bool)), this , SLOT(removeRecentRoom()));
    connect(ui->clearRecentPushButton, SIGNAL(clicked(bool)), this, SLOT(clearRecentRooms()));
    connect(ui->serverLineEdit, SIGNAL(returnPressed()), this, SLOT(getRoomList()));
    connect(ui->queryPushButton, SIGNAL(clicked(bool)), this, SLOT(getRoomList()));
    connect(ui->stopPushButton, SIGNAL(clicked(bool)), this, SLOT(stopListing()));
    connect(ui->treeView, SIGNAL(clicked(QModelIndex)), this, SLOT(onRoomClicked(QModelIndex)));
    connect(ui->treeView, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(accept()));
    connect(ui->filterBar, SIGNAL(textChanged(QString)), proxyModel, SLOT(setFilterFixedString(QString)));
    connect(ui->comboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(onAccountSelectionChanged(int)));
    connect(button(Ok), SIGNAL(clicked(bool)), this, SLOT(addRecentRoom()));
}

void KTp::JoinChatRoomDialog::onAccountManagerReady(Tp::PendingOperation* operation)
{
    Tp::AccountManagerPtr accountManager = Tp::AccountManagerPtr::qObjectCast(
        qobject_cast<Tp::PendingReady*>(operation)->proxy()
    );
    Tp::AccountPropertyFilterPtr isOnlineFilter = Tp::AccountPropertyFilter::create();
    isOnlineFilter->addProperty(QLatin1String("online"), true);

    Tp::AccountCapabilityFilterPtr capabilityFilter = Tp::AccountCapabilityFilter::create(
                Tp::RequestableChannelClassSpecList() << Tp::RequestableChannelClassSpec::textChatroom());
    Tp::AccountFilterPtr filter = Tp::AndFilter<Tp::Account>::create((QList<Tp::AccountFilterConstPtr>() <<
                                             isOnlineFilter <<
                                             capabilityFilter));

    ui->comboBox->setAccountSet(accountManager->filterAccounts(filter));

    // queryTab
    if (ui->comboBox->count() > 0) {
        ui->queryPushButton->setEnabled(true);
    }

    // apply the filter after populating
    onAccountSelectionChanged(ui->comboBox->currentIndex());
}

KTp::JoinChatRoomDialog::~JoinChatRoomDialog()
{
    delete ui;
}

Tp::AccountPtr KTp::JoinChatRoomDialog::selectedAccount() const
{
    return ui->comboBox->currentAccount();
}

void KTp::JoinChatRoomDialog::onAccountSelectionChanged(int newIndex)
{
    // Show only favorites associated with the selected account
    Q_UNUSED(newIndex)

    if (!ui->comboBox->currentAccount()) {
        return;
    }

    QString accountIdentifier = ui->comboBox->currentAccount()->uniqueIdentifier();
    m_favoritesProxyModel->setFilterFixedString(accountIdentifier);

    // Provide only rooms recently used with the selected account for completion
    m_recentComp->clear();

    Q_FOREACH (const QString &room, m_recentRooms.value(accountIdentifier)) {
        m_recentComp->addItem(room);
    }

    // Show only rooms recently used with the selected account in the list
    ui->recentListWidget->clear();
    ui->recentListWidget->addItems(m_recentRooms.value(accountIdentifier));

    // Enable/disable the buttons as appropriate
    bool recentListNonEmpty = m_recentRooms.value(accountIdentifier).size() > 0;<commit_msg>Don't display any favorite chat rooms if no account is online<commit_after>/*
 * This file is part of telepathy-contactslist-prototype
 *
 * Copyright (C) 2011 <NAME> <<EMAIL>>
 * Copyright (C) 2012 <NAME> <<EMAIL>>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "join-chat-room-dialog.h"
#include "ui_join-chat-room-dialog.h"

#include <KTp/Models/rooms-model.h>

#include <KConfig>
#include <KDebug>
#include <KInputDialog>
#include <KMessageBox>
#include <KNotification>
#include <KPushButton>
#include <KCompletionBox>

#include <TelepathyQt/AccountSet>
#include <TelepathyQt/AccountCapabilityFilter>
#include <TelepathyQt/AccountManager>
#include <TelepathyQt/AccountPropertyFilter>
#include <TelepathyQt/AndFilter>
#include <TelepathyQt/ChannelTypeRoomListInterface>
#include <TelepathyQt/PendingChannel>
#include <TelepathyQt/PendingReady>
#include <TelepathyQt/RequestableChannelClassSpec>
#include <TelepathyQt/RoomListChannel>

#include <QSortFilterProxyModel>

KTp::JoinChatRoomDialog::JoinChatRoomDialog(Tp::AccountManagerPtr accountManager, QWidget* parent)
    : KDialog(parent, Qt::Dialog)
    , ui(new Ui::JoinChatRoomDialog)
    , m_model(new RoomsModel(this))
    , m_favoritesModel(new FavoriteRoomsModel(this))
    , m_favoritesProxyModel(new QSortFilterProxyModel(this))
    , m_recentComp(new KCompletion)
{
    QWidget *joinChatRoomDialog = new QWidget(this);
    ui->setupUi(joinChatRoomDialog);
    setMainWidget(joinChatRoomDialog);
    setWindowIcon(KIcon(QLatin1String("telepathy-kde")));

    // config
    KSharedConfigPtr commonConfig = KSharedConfig::openConfig(QLatin1String("ktelepathyrc"));
    m_favoriteRoomsGroup = commonConfig->group(QLatin1String("FavoriteRooms"));
    m_recentRoomsGroup = commonConfig->group(QLatin1String("RecentChatRooms"));

    Q_FOREACH (const QString &key, m_recentRoomsGroup.keyList()) {
        if (!m_recentRoomsGroup.readEntry(key, QStringList()).isEmpty()) {
            m_recentRooms.insert(key, m_recentRoomsGroup.readEntry(key, QStringList()));
        }
    }

    loadFavoriteRooms();

    // disable OK button on start
    button(Ok)->setEnabled(false);

    //set icons
    ui->addFavoritePushButton->setIcon(KIcon(QLatin1String("list-add")));
    ui->editFavoritePushButton->setIcon(KIcon(QLatin1String("list-edit")));
    ui->removeFavoritePushButton->setIcon(KIcon(QLatin1String("list-remove")));
    ui->removeRecentPushButton->setIcon(KIcon(QLatin1String("list-remove")));
    ui->clearRecentPushButton->setIcon(KIcon(QLatin1String("edit-clear-list")));

    connect(accountManager->becomeReady(), SIGNAL(finished(Tp::PendingOperation*)),
            this, SLOT(onAccountManagerReady(Tp::PendingOperation*)));

    // favoritesTab
    m_favoritesProxyModel->setSourceModel(m_favoritesModel);
    m_favoritesProxyModel->setFilterKeyColumn(FavoriteRoomsModel::AccountIdentifierColumn);
    m_favoritesProxyModel->setDynamicSortFilter(true);

    ui->listView->setModel(m_favoritesProxyModel);
    ui->listView->setModelColumn(FavoriteRoomsModel::NameColumn);

    // recentTab
    m_recentComp->setCompletionMode(KGlobalSettings::CompletionPopup);
    m_recentComp->setIgnoreCase(true);

    ui->lineEdit->setCompletionObject(m_recentComp);
    ui->lineEdit->setAutoDeleteCompletionObject(true);

    QSortFilterProxyModel *proxyModel = new QSortFilterProxyModel(this);
    proxyModel->setSourceModel(m_model);
    proxyModel->setSortLocaleAware(true);
    proxyModel->setSortCaseSensitivity(Qt::CaseInsensitive);
    proxyModel->setFilterKeyColumn(RoomsModel::NameColumn);
    proxyModel->setDynamicSortFilter(true);

    ui->treeView->setModel(proxyModel);
    ui->treeView->header()->setResizeMode(QHeaderView::ResizeToContents);
    ui->treeView->sortByColumn(RoomsModel::NameColumn, Qt::AscendingOrder);

    // connects
    connect(ui->lineEdit, SIGNAL(textChanged(QString)), this, SLOT(onTextChanged(QString)));
    connect(ui->listView, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(accept()));
    connect(ui->listView->selectionModel(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),
            this, SLOT(onFavoriteRoomSelectionChanged(QModelIndex,QModelIndex)));
    connect(m_favoritesModel, SIGNAL(dataChanged(QModelIndex,QModelIndex)),
            this, SLOT(onFavoriteRoomDataChanged(QModelIndex,QModelIndex)));
    connect(ui->addFavoritePushButton, SIGNAL(clicked(bool)), this, SLOT(addFavorite()));
    connect(ui->editFavoritePushButton, SIGNAL(clicked(bool)), this, SLOT(editFavorite()));
    connect(ui->removeFavoritePushButton, SIGNAL(clicked(bool)), this, SLOT(removeFavorite()));
    connect(ui->recentListWidget, SIGNAL(currentTextChanged(QString)), ui->lineEdit, SLOT(setText(QString)));
    connect(ui->recentListWidget, SIGNAL(currentTextChanged(QString)), this, SLOT(onRecentRoomClicked()));
    connect(ui->recentListWidget, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(accept()));
    connect(ui->removeRecentPushButton, SIGNAL(clicked(bool)), this , SLOT(removeRecentRoom()));
    connect(ui->clearRecentPushButton, SIGNAL(clicked(bool)), this, SLOT(clearRecentRooms()));
    connect(ui->serverLineEdit, SIGNAL(returnPressed()), this, SLOT(getRoomList()));
    connect(ui->queryPushButton, SIGNAL(clicked(bool)), this, SLOT(getRoomList()));
    connect(ui->stopPushButton, SIGNAL(clicked(bool)), this, SLOT(stopListing()));
    connect(ui->treeView, SIGNAL(clicked(QModelIndex)), this, SLOT(onRoomClicked(QModelIndex)));
    connect(ui->treeView, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(accept()));
    connect(ui->filterBar, SIGNAL(textChanged(QString)), proxyModel, SLOT(setFilterFixedString(QString)));
    connect(ui->comboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(onAccountSelectionChanged(int)));
    connect(button(Ok), SIGNAL(clicked(bool)), this, SLOT(addRecentRoom()));
}

void KTp::JoinChatRoomDialog::onAccountManagerReady(Tp::PendingOperation* operation)
{
    Tp::AccountManagerPtr accountManager = Tp::AccountManagerPtr::qObjectCast(
        qobject_cast<Tp::PendingReady*>(operation)->proxy()
    );
    Tp::AccountPropertyFilterPtr isOnlineFilter = Tp::AccountPropertyFilter::create();
    isOnlineFilter->addProperty(QLatin1String("online"), true);

    Tp::AccountCapabilityFilterPtr capabilityFilter = Tp::AccountCapabilityFilter::create(
                Tp::RequestableChannelClassSpecList() << Tp::RequestableChannelClassSpec::textChatroom());
    Tp::AccountFilterPtr filter = Tp::AndFilter<Tp::Account>::create((QList<Tp::AccountFilterConstPtr>() <<
                                             isOnlineFilter <<
                                             capabilityFilter));

    ui->comboBox->setAccountSet(accountManager->filterAccounts(filter));

    // queryTab
    if (ui->comboBox->count() > 0) {
        ui->queryPushButton->setEnabled(true);
    }

    // apply the filter after populating
    onAccountSelectionChanged(ui->comboBox->currentIndex());
}

KTp::JoinChatRoomDialog::~JoinChatRoomDialog()
{
    delete ui;
}

Tp::AccountPtr KTp::JoinChatRoomDialog::selectedAccount() const
{
    return ui->comboBox->currentAccount();
}

void KTp::JoinChatRoomDialog::onAccountSelectionChanged(int newIndex)
{
    // Show only favorites associated with the selected account
    Q_UNUSED(newIndex)

    if (!ui->comboBox->currentAccount()) {
        // Set a filter expression that matches no account identifier
        m_favoritesProxyModel->setFilterRegExp(QLatin1String("a^"));
        return;
    }

    QString accountIdentifier = ui->comboBox->currentAccount()->uniqueIdentifier();
    m_favoritesProxyModel->setFilterFixedString(accountIdentifier);

    // Provide only rooms recently used with the selected account for completion
    m_recentComp->clear();

    Q_FOREACH (const QString &room, m_recentRooms.value(accountIdentifier)) {
        m_recentComp->addItem(room);
    }

    // Show only rooms recently used with the selected account in the list
    ui->recentListWidget->clear();
    ui->recentListWidget->addItems(m_recentRooms.value(accountIdentifier));

    // Enable/disable the buttons as appropriate
    bool recentListNonEmpty = m_recentRooms.value(accountIdentifier).size() > 0;
{'fraction_non_alphanumeric': 0.1268550633991016, 'fraction_numerical': 0.0038096321146301247, 'mean_word_length': 5.589733982765081, 'pattern_counts': {'":': 0, '<': 71, '<?xml version=': 0, '>': 234, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7643088', 'n_tokens_mistral': 5326, 'n_tokens_neox': 5109, 'n_words': 903}