diff --git a/src/widgets/dialogs/autotests/channelinfowidgettest.cpp b/src/widgets/dialogs/autotests/channelinfowidgettest.cpp index 87aa07b9..f07e43e0 100644 --- a/src/widgets/dialogs/autotests/channelinfowidgettest.cpp +++ b/src/widgets/dialogs/autotests/channelinfowidgettest.cpp @@ -1,113 +1,116 @@ /* Copyright (c) 2020 Laurent Montel This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License or ( at your option ) version 3 or, at the discretion of KDE e.V. ( which shall act as a proxy as in section 14 of the GPLv3 ), 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 Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "channelinfowidgettest.h" #include "dialogs/channelinfowidget.h" #include #include #include #include #include #include #include QTEST_MAIN(ChannelInfoWidgetTest) ChannelInfoWidgetTest::ChannelInfoWidgetTest(QObject *parent) : QObject(parent) { } void ChannelInfoWidgetTest::shouldHaveDefaultValues() { ChannelInfoWidget w; auto *mainLayout = w.findChild(QStringLiteral("mainLayout")); QVERIFY(mainLayout); QCOMPARE(mainLayout->contentsMargins(), QMargins(0, 0, 0, 0)); auto *mStackedWidget = w.findChild(QStringLiteral("mStackedWidget")); QVERIFY(mStackedWidget); //Editable channel auto *mEditableChannel = w.findChild(QStringLiteral("mEditableChannel")); QVERIFY(mEditableChannel); auto *layout = mEditableChannel->findChild(QStringLiteral("layout")); QVERIFY(layout); QCOMPARE(layout->contentsMargins(), QMargins(0, 0, 0, 0)); auto *mComment = mEditableChannel->findChild(QStringLiteral("mComment")); QVERIFY(mComment); auto *mAnnouncement = mEditableChannel->findChild(QStringLiteral("mAnnouncement")); QVERIFY(mAnnouncement); auto *mDescription = mEditableChannel->findChild(QStringLiteral("mDescription")); QVERIFY(mDescription); auto *mPasswordLineEdit = mEditableChannel->findChild(QStringLiteral("mPasswordLineEdit")); QVERIFY(mPasswordLineEdit); auto *mArchive = mEditableChannel->findChild(QStringLiteral("mArchive")); QVERIFY(mArchive); QVERIFY(mArchive->text().isEmpty()); auto *mReadOnly = mEditableChannel->findChild(QStringLiteral("mReadOnly")); QVERIFY(mReadOnly); QVERIFY(mReadOnly->text().isEmpty()); auto *mPrivate = mEditableChannel->findChild(QStringLiteral("mPrivate")); QVERIFY(mPrivate); QVERIFY(mPrivate->text().isEmpty()); auto *mDeleteChannel = mEditableChannel->findChild(QStringLiteral("mDeleteChannel")); QVERIFY(mDeleteChannel); QVERIFY(!mDeleteChannel->text().isEmpty()); //ReadOnly Channel auto *mReadOnlyChannel = w.findChild(QStringLiteral("mReadOnlyChannel")); QVERIFY(mReadOnlyChannel); auto *layoutreadonly = mReadOnlyChannel->findChild(QStringLiteral("layoutReadOnly")); QVERIFY(layoutreadonly); QCOMPARE(layoutreadonly->contentsMargins(), QMargins(0, 0, 0, 0)); auto *mNameReadOnly = mReadOnlyChannel->findChild(QStringLiteral("mNameReadOnly")); QVERIFY(mNameReadOnly); QVERIFY(mNameReadOnly->text().isEmpty()); QCOMPARE(mNameReadOnly->textInteractionFlags(), Qt::TextBrowserInteraction); QCOMPARE(mNameReadOnly->textFormat(), Qt::RichText); auto *mCommentReadOnly = mReadOnlyChannel->findChild(QStringLiteral("mCommentReadOnly")); QVERIFY(mCommentReadOnly); QVERIFY(mCommentReadOnly->text().isEmpty()); QCOMPARE(mCommentReadOnly->textFormat(), Qt::RichText); QCOMPARE(mCommentReadOnly->textInteractionFlags(), Qt::TextBrowserInteraction); + QVERIFY(mCommentReadOnly->wordWrap()); auto *mAnnouncementReadOnly = mReadOnlyChannel->findChild(QStringLiteral("mAnnouncementReadOnly")); QVERIFY(mAnnouncementReadOnly); QVERIFY(mAnnouncementReadOnly->text().isEmpty()); QCOMPARE(mAnnouncementReadOnly->textFormat(), Qt::RichText); QCOMPARE(mAnnouncementReadOnly->textInteractionFlags(), Qt::TextBrowserInteraction); + QVERIFY(mAnnouncementReadOnly->wordWrap()); auto *mDescriptionReadOnly = mReadOnlyChannel->findChild(QStringLiteral("mDescriptionReadOnly")); QVERIFY(mDescriptionReadOnly); QCOMPARE(mDescriptionReadOnly->textFormat(), Qt::RichText); QVERIFY(mDescriptionReadOnly->text().isEmpty()); QCOMPARE(mDescriptionReadOnly->textInteractionFlags(), Qt::TextBrowserInteraction); + QVERIFY(mDescriptionReadOnly->wordWrap()); } diff --git a/src/widgets/dialogs/channelinfowidget.cpp b/src/widgets/dialogs/channelinfowidget.cpp index cb50f3b9..9ad61324 100644 --- a/src/widgets/dialogs/channelinfowidget.cpp +++ b/src/widgets/dialogs/channelinfowidget.cpp @@ -1,327 +1,330 @@ /* Copyright (c) 2020 Laurent Montel This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License or ( at your option ) version 3 or, at the discretion of KDE e.V. ( which shall act as a proxy as in section 14 of the GPLv3 ), 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 Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "channelinfowidget.h" #include "roomwrapper.h" #include "ruqola.h" #include "rocketchataccount.h" #include #include #include #include #include #include #include #include #include #include #include #include #include ChannelInfoWidget::ChannelInfoWidget(QWidget *parent) : QWidget(parent) { auto *mainLayout = new QVBoxLayout(this); mainLayout->setObjectName(QStringLiteral("mainLayout")); mainLayout->setContentsMargins(0, 0, 0, 0); mStackedWidget = new QStackedWidget(this); mStackedWidget->setObjectName(QStringLiteral("mStackedWidget")); mainLayout->addWidget(mStackedWidget); //Editable channel mEditableChannel = new QWidget(this); mEditableChannel->setObjectName(QStringLiteral("mEditableChannel")); mStackedWidget->addWidget(mEditableChannel); auto *layout = new QFormLayout(mEditableChannel); layout->setObjectName(QStringLiteral("layout")); layout->setContentsMargins(0, 0, 0, 0); QString str = i18n("Name:"); mName = new ChangeTextWidget(this); mName->setObjectName(QStringLiteral("mName")); connect(mName, &ChangeTextWidget::textChanged, this, [this](const QString &name) { Ruqola::self()->rocketChatAccount()->changeChannelSettings(mRoomWrapper->roomId(), RocketChatAccount::Name, name, mRoomWrapper->channelType()); }); mName->setLabelText(str); layout->addRow(str, mName); mComment = new ChangeTextWidget(this); mComment->setObjectName(QStringLiteral("mComment")); str = i18n("Comment:"); mComment->setLabelText(str); layout->addRow(str, mComment); connect(mComment, &ChangeTextWidget::textChanged, this, [this](const QString &name) { Ruqola::self()->rocketChatAccount()->changeChannelSettings(mRoomWrapper->roomId(), RocketChatAccount::Topic, name, mRoomWrapper->channelType()); }); mAnnouncement = new ChangeTextWidget(this); mAnnouncement->setObjectName(QStringLiteral("mAnnouncement")); connect(mAnnouncement, &ChangeTextWidget::textChanged, this, [this](const QString &name) { Ruqola::self()->rocketChatAccount()->changeChannelSettings(mRoomWrapper->roomId(), RocketChatAccount::Announcement, name, mRoomWrapper->channelType()); }); str = i18n("Announcement:"); mAnnouncement->setLabelText(str); layout->addRow(str, mAnnouncement); mDescription = new ChangeTextWidget(this); mDescription->setObjectName(QStringLiteral("mDescription")); connect(mDescription, &ChangeTextWidget::textChanged, this, [this](const QString &name) { Ruqola::self()->rocketChatAccount()->changeChannelSettings(mRoomWrapper->roomId(), RocketChatAccount::Description, name, mRoomWrapper->channelType()); }); str = i18n("Description:"); mDescription->setLabelText(str); layout->addRow(str, mDescription); mPasswordLineEdit = new KPasswordLineEdit(this); mPasswordLineEdit->setObjectName(QStringLiteral("mPasswordLineEdit")); layout->addRow(i18n("Password:"), mPasswordLineEdit); connect(mPasswordLineEdit, &KPasswordLineEdit::passwordChanged, this, [](const QString &password) { //Ruqola::self()->rocketChatAccount()->changeChannelSettings(mRoomWrapper->roomId(), RocketChatAccount::Password, password, mRoomWrapper->channelType()); }); mReadOnly = new QCheckBox(this); mReadOnly->setObjectName(QStringLiteral("mReadOnly")); layout->addRow(i18n("ReadOnly:"), mReadOnly); connect(mReadOnly, &QCheckBox::clicked, this, [this](bool checked) { Ruqola::self()->rocketChatAccount()->changeChannelSettings(mRoomWrapper->roomId(), RocketChatAccount::ReadOnly, checked, mRoomWrapper->channelType()); }); mArchive = new QCheckBox(this); mArchive->setObjectName(QStringLiteral("mArchive")); layout->addRow(i18n("Archive:"), mArchive); connect(mArchive, &QCheckBox::clicked, this, [this](bool checked) { const QString text = checked ? i18n("Do you want to archive this room?") : i18n("Do you want to unarchive this room?"); const QString title = checked ? i18n("Archive Channel") : i18n("Unarchive Channel"); if (KMessageBox::Yes == KMessageBox::questionYesNo(this, text, title)) { Ruqola::self()->rocketChatAccount()->changeChannelSettings(mRoomWrapper->roomId(), RocketChatAccount::Archive, checked, mRoomWrapper->channelType()); } }); mPrivate = new QCheckBox(this); mPrivate->setObjectName(QStringLiteral("mPrivate")); layout->addRow(i18n("Private:"), mPrivate); connect(mPrivate, &QCheckBox::clicked, this, [this](bool checked) { Ruqola::self()->rocketChatAccount()->changeChannelSettings(mRoomWrapper->roomId(), RocketChatAccount::RoomType, checked, mRoomWrapper->channelType()); }); //TODO add encrypted too! mDeleteChannel = new QPushButton(QIcon::fromTheme(QStringLiteral("edit-delete-shred")), i18n("Delete"), this); mDeleteChannel->setObjectName(QStringLiteral("mDeleteChannel")); layout->addRow(QStringLiteral(" "), mDeleteChannel); connect(mDeleteChannel, &QPushButton::clicked, this, [this]() { if (KMessageBox::Yes == KMessageBox::questionYesNo(this, i18n("Do you want to delete this room?"), i18n("Delete Room"))) { Ruqola::self()->rocketChatAccount()->eraseRoom(mRoomWrapper->roomId(), mRoomWrapper->channelType()); Q_EMIT channelDeleted(); } }); //ReadOnly Channel mReadOnlyChannel = new QWidget(this); mReadOnlyChannel->setObjectName(QStringLiteral("mReadOnlyChannel")); mStackedWidget->addWidget(mReadOnlyChannel); auto *layoutReadOnly = new QFormLayout(mReadOnlyChannel); layoutReadOnly->setObjectName(QStringLiteral("layoutReadOnly")); layoutReadOnly->setContentsMargins(0, 0, 0, 0); mNameReadOnly = new QLabel(this); mNameReadOnly->setTextFormat(Qt::RichText); mNameReadOnly->setObjectName(QStringLiteral("mNameReadOnly")); mNameReadOnly->setTextInteractionFlags(Qt::TextBrowserInteraction); mNameReadOnly->setOpenExternalLinks(true); mNameReadOnly->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); layoutReadOnly->addRow(i18n("Name:"), mNameReadOnly); mCommentReadOnly = new QLabel(this); mCommentReadOnly->setTextFormat(Qt::RichText); mCommentReadOnly->setObjectName(QStringLiteral("mCommentReadOnly")); mCommentReadOnly->setTextInteractionFlags(Qt::TextBrowserInteraction); mCommentReadOnly->setOpenExternalLinks(true); + mCommentReadOnly->setWordWrap(true); mCommentReadOnly->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); layoutReadOnly->addRow(i18n("Comment:"), mCommentReadOnly); mAnnouncementReadOnly = new QLabel(this); mAnnouncementReadOnly->setTextFormat(Qt::RichText); mAnnouncementReadOnly->setObjectName(QStringLiteral("mAnnouncementReadOnly")); mAnnouncementReadOnly->setTextInteractionFlags(Qt::TextBrowserInteraction); mAnnouncementReadOnly->setOpenExternalLinks(true); + mAnnouncementReadOnly->setWordWrap(true); mAnnouncementReadOnly->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); layoutReadOnly->addRow(i18n("Announcement:"), mAnnouncementReadOnly); mDescriptionReadOnly = new QLabel(this); mDescriptionReadOnly->setTextFormat(Qt::RichText); mDescriptionReadOnly->setObjectName(QStringLiteral("mDescriptionReadOnly")); mDescriptionReadOnly->setTextInteractionFlags(Qt::TextBrowserInteraction); mDescriptionReadOnly->setOpenExternalLinks(true); + mDescriptionReadOnly->setWordWrap(true); mDescriptionReadOnly->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); layoutReadOnly->addRow(i18n("Description:"), mDescriptionReadOnly); } ChannelInfoWidget::~ChannelInfoWidget() { } void ChannelInfoWidget::setRoomWrapper(RoomWrapper *roomWrapper) { mRoomWrapper = roomWrapper; if (mRoomWrapper->canBeModify()) { mStackedWidget->setCurrentWidget(mEditableChannel); updateEditableChannelInfo(); connectEditableWidget(); } else { mStackedWidget->setCurrentWidget(mReadOnlyChannel); updateReadOnlyChannelInfo(); connectReadOnlyWidget(); } } void ChannelInfoWidget::updateReadOnlyChannelInfo() { mNameReadOnly->setText(mRoomWrapper->name()); mCommentReadOnly->setText(mRoomWrapper->topic()); mAnnouncementReadOnly->setText(mRoomWrapper->announcement()); mDescriptionReadOnly->setText(mRoomWrapper->description()); } void ChannelInfoWidget::updateEditableChannelInfo() { mName->setText(mRoomWrapper->name()); mComment->setText(mRoomWrapper->topic()); mAnnouncement->setText(mRoomWrapper->announcement()); mDescription->setText(mRoomWrapper->description()); mReadOnly->setChecked(mRoomWrapper->readOnly()); mArchive->setChecked(mRoomWrapper->archived()); mPrivate->setChecked(mRoomWrapper->channelType() == QStringLiteral("p")); joinCodeChanged(); } void ChannelInfoWidget::joinCodeChanged() { mPasswordLineEdit->lineEdit()->setPlaceholderText(mRoomWrapper->joinCodeRequired() ? i18n("This Room has a password") : i18n("Add password")); } void ChannelInfoWidget::connectReadOnlyWidget() { connect(mRoomWrapper, &RoomWrapper::announcementChanged, this, [this]() { mAnnouncementReadOnly->setText(mRoomWrapper->announcement()); }); connect(mRoomWrapper, &RoomWrapper::topicChanged, this, [this]() { mCommentReadOnly->setText(mRoomWrapper->topic()); }); connect(mRoomWrapper, &RoomWrapper::nameChanged, this, [this]() { mNameReadOnly->setText(mRoomWrapper->name()); }); connect(mRoomWrapper, &RoomWrapper::descriptionChanged, this, [this]() { mDescriptionReadOnly->setText(mRoomWrapper->description()); }); } void ChannelInfoWidget::connectEditableWidget() { connect(mRoomWrapper, &RoomWrapper::announcementChanged, this, [this]() { mAnnouncement->setText(mRoomWrapper->announcement()); }); connect(mRoomWrapper, &RoomWrapper::topicChanged, this, [this]() { mComment->setText(mRoomWrapper->topic()); }); connect(mRoomWrapper, &RoomWrapper::nameChanged, this, [this]() { mName->setText(mRoomWrapper->name()); }); connect(mRoomWrapper, &RoomWrapper::descriptionChanged, this, [this]() { mDescription->setText(mRoomWrapper->description()); }); connect(mRoomWrapper, &RoomWrapper::readOnlyChanged, this, [this]() { mReadOnly->setChecked(mRoomWrapper->readOnly()); }); connect(mRoomWrapper, &RoomWrapper::archivedChanged, this, [this]() { mArchive->setChecked(mRoomWrapper->archived()); }); connect(mRoomWrapper, &RoomWrapper::joinCodeRequiredChanged, this, [this]() { joinCodeChanged(); }); connect(mRoomWrapper, &RoomWrapper::channelTypeChanged, this, [this]() { mPrivate->setChecked(mRoomWrapper->channelType() == QStringLiteral("p")); }); //TODO react when we change settings connect(mReadOnly, &QCheckBox::clicked, this, [this](bool checked) { Ruqola::self()->rocketChatAccount()->changeChannelSettings(mRoomWrapper->roomId(), RocketChatAccount::ReadOnly, checked, mRoomWrapper->channelType()); }); connect(mArchive, &QCheckBox::clicked, this, [this](bool checked) { if (KMessageBox::Yes == KMessageBox::questionYesNo(this, checked ? i18n("Do you want to archive this room?") : i18n("Do you want to unarchive this room?"), i18n("Archive room"))) { Ruqola::self()->rocketChatAccount()->changeChannelSettings(mRoomWrapper->roomId(), RocketChatAccount::Archive, checked, mRoomWrapper->channelType()); } }); connect(mPrivate, &QCheckBox::clicked, this, [this](bool checked) { Ruqola::self()->rocketChatAccount()->changeChannelSettings(mRoomWrapper->roomId(), RocketChatAccount::RoomType, checked, mRoomWrapper->channelType()); }); } ChangeTextWidget::ChangeTextWidget(QWidget *parent) : QWidget(parent) { auto *mainLayout = new QHBoxLayout(this); mainLayout->setObjectName(QStringLiteral("mainLayout")); mainLayout->setContentsMargins(0, 0, 0, 0); mLabel = new QLabel(this); mLabel->setObjectName(QStringLiteral("mLabel")); mLabel->setWordWrap(true); mLabel->setTextInteractionFlags(Qt::TextBrowserInteraction); mLabel->setOpenExternalLinks(true); mLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); mainLayout->addWidget(mLabel); mChangeTextToolButton = new QToolButton(this); mChangeTextToolButton->setIcon(QIcon::fromTheme(QStringLiteral("document-edit"))); mChangeTextToolButton->setObjectName(QStringLiteral("mChangeTextToolButton")); mainLayout->addWidget(mChangeTextToolButton); connect(mChangeTextToolButton, &QToolButton::clicked, this, [this]() { //Convert html to text. Otherwise we will have html tag QString text = mLabel->text(); QTextDocument doc; doc.setHtml(text); text = doc.toPlainText(); const QString result = QInputDialog::getText(this, i18n("Change Text"), mLabelText, QLineEdit::Normal, text); if (!result.trimmed().isEmpty()) { if (result != text) { Q_EMIT textChanged(result); } } }); } ChangeTextWidget::~ChangeTextWidget() { } void ChangeTextWidget::setText(const QString &str) { mLabel->setText(str); } void ChangeTextWidget::setLabelText(const QString &str) { mLabelText = str; }