diff --git a/autotests/CMakeLists.txt b/autotests/CMakeLists.txt index 320c7d90..d91dbf66 100644 --- a/autotests/CMakeLists.txt +++ b/autotests/CMakeLists.txt @@ -1,82 +1,83 @@ add_definitions( -DRUQOLA_DATA_DIR="${CMAKE_CURRENT_SOURCE_DIR}/data") macro(add_ruqola_test _source) set( _test ${_source}) get_filename_component( _name ${_source} NAME_WE ) add_executable( ${_name} ${_test} ) add_test(NAME ${_name} COMMAND ${_name} ) ecm_mark_as_test(${_name}) target_link_libraries( ${_name} Qt5::Test libruqolacore) endmacro() add_ruqola_test(rocketchatmessagetest.cpp) add_ruqola_test(roommodeltest.cpp) add_ruqola_test(messagemodeltest.cpp) add_ruqola_test(typingnotificationtest.cpp) add_ruqola_test(utilstest.cpp) add_ruqola_test(usertest.cpp) add_ruqola_test(messageattachmenttest.cpp) add_ruqola_test(restapiutiltest.cpp) add_ruqola_test(rocketchataccountsettingstest.cpp) add_ruqola_test(messagetest.cpp) add_ruqola_test(messageurltest.cpp) add_ruqola_test(roomtest.cpp) add_ruqola_test(ruqolaserverconfigtest.cpp) add_ruqola_test(statusmodeltest.cpp) add_ruqola_test(rocketchatcachetest.cpp) add_ruqola_test(textconvertertest.cpp) add_ruqola_test(loadrecenthistorymanagertest.cpp) add_ruqola_test(notificationtest.cpp) add_ruqola_test(emojitest.cpp) add_ruqola_test(emojimanagertest.cpp) add_ruqola_test(otrtest.cpp) add_ruqola_test(otrmanagertest.cpp) add_ruqola_test(rocketchataccounttest.cpp) add_ruqola_test(usersmodeltest.cpp) add_ruqola_test(usersforroommodeltest.cpp) add_ruqola_test(filetest.cpp) add_ruqola_test(filesforroommodeltest.cpp) add_ruqola_test(filesforroomfilterproxymodeltest.cpp) add_ruqola_test(channeltest.cpp) add_ruqola_test(usersforroomfilterproxymodeltest.cpp) add_ruqola_test(usercompletermodeltest.cpp) add_ruqola_test(roomfilterproxymodeltest.cpp) add_ruqola_test(usercompleterfilterproxymodeltest.cpp) add_ruqola_test(searchchannelmodeltest.cpp) add_ruqola_test(searchchannelfilterproxymodeltest.cpp) add_ruqola_test(inputcompletermodeltest.cpp) add_ruqola_test(loginmethodmodeltest.cpp) add_ruqola_test(inputtextmanagertest.cpp) add_ruqola_test(clipboardproxytest.cpp) add_ruqola_test(authenticationinfotest.cpp) add_ruqola_test(searchmessagemodeltest.cpp) add_ruqola_test(searchmessagefilterproxymodeltest.cpp) add_ruqola_test(searchmessagetest.cpp) add_ruqola_test(accountmanagertest.cpp) add_ruqola_test(rocketchataccountmodeltest.cpp) add_ruqola_test(ruqolatest.cpp) add_ruqola_test(managerdatapathstest.cpp) add_ruqola_test(rocketchataccountfilterproxymodeltest.cpp) add_ruqola_test(roomwrappertest.cpp) add_ruqola_test(notificationoptionstest.cpp) add_ruqola_test(reactionstest.cpp) add_ruqola_test(reactiontest.cpp) #RESTAPI add_ruqola_test(restapimethodtest.cpp) add_ruqola_test(serverinfojobtest.cpp) add_ruqola_test(uploadfilejobtest.cpp) add_ruqola_test(owninfojobtest.cpp) add_ruqola_test(getavatarjobtest.cpp) add_ruqola_test(logoutjobtest.cpp) add_ruqola_test(loginjobtest.cpp) add_ruqola_test(privateinfojobtest.cpp) add_ruqola_test(channellistjobtest.cpp) add_ruqola_test(starmessagejobtest.cpp) add_ruqola_test(downloadfilejobtest.cpp) add_ruqola_test(pinmessagejobtest.cpp) add_ruqola_test(postmessagejobtest.cpp) add_ruqola_test(changechanneltopicjobtest.cpp) add_ruqola_test(changechannelannouncementjobtest.cpp) add_ruqola_test(changegroupstopicjobtest.cpp) add_ruqola_test(changegroupsannouncementjobtest.cpp) +add_ruqola_test(changechannelnamejobtest.cpp) add_subdirectory(qml) diff --git a/autotests/changechannelnamejobtest.cpp b/autotests/changechannelnamejobtest.cpp new file mode 100644 index 00000000..6d89321f --- /dev/null +++ b/autotests/changechannelnamejobtest.cpp @@ -0,0 +1,58 @@ +/* + Copyright (c) 2018 Montel Laurent + + 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 "changechannelnamejobtest.h" +#include "restapi/channels/changechannelnamejob.h" +#include "ruqola_restapi_helper.h" +#include +#include +QTEST_GUILESS_MAIN(ChangeChannelNameJobTest) + +ChangeChannelNameJobTest::ChangeChannelNameJobTest(QObject *parent) + : QObject(parent) +{ +} + +void ChangeChannelNameJobTest::shouldHaveDefaultValue() +{ + ChangeChannelNameJob job; + verifyDefaultValue(&job); + QVERIFY(job.name().isEmpty()); + QVERIFY(job.roomId().isEmpty()); +} + +void ChangeChannelNameJobTest::shouldGenerateRequest() +{ + ChangeChannelNameJob job; + QNetworkRequest request = QNetworkRequest(QUrl()); + verifyAuthentication(&job, request); + QCOMPARE(request.url(), QUrl(QStringLiteral("http://www.kde.org/api/v1/channels.setAnnouncement"))); + QCOMPARE(request.header(QNetworkRequest::ContentTypeHeader).toString(), QStringLiteral("application/json")); +} + +void ChangeChannelNameJobTest::shouldGenerateJson() +{ + ChangeChannelNameJob job; + const QString roomId = QStringLiteral("foo1"); + const QString name = QStringLiteral("topic1"); + job.setRoomId(roomId); + job.setName(name); + QCOMPARE(job.json().toJson(QJsonDocument::Compact), QStringLiteral("{\"name\":\"%1\",\"roomId\":\"%2\"}").arg(name).arg(roomId).toLatin1()); +} diff --git a/autotests/changechannelnamejobtest.h b/autotests/changechannelnamejobtest.h new file mode 100644 index 00000000..782cf319 --- /dev/null +++ b/autotests/changechannelnamejobtest.h @@ -0,0 +1,38 @@ +/* + Copyright (c) 2018 Montel Laurent + + 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. +*/ + +#ifndef CHANGECHANNELNAMEJOBTEST_H +#define CHANGECHANNELNAMEJOBTEST_H + +#include + +class ChangeChannelNameJobTest : public QObject +{ + Q_OBJECT +public: + explicit ChangeChannelNameJobTest(QObject *parent = nullptr); + ~ChangeChannelNameJobTest() = default; +private Q_SLOTS: + void shouldHaveDefaultValue(); + void shouldGenerateRequest(); + void shouldGenerateJson(); +}; + +#endif // CHANGECHANNELNAMEJOBTEST_H diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 6e295ae8..0e928797 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,180 +1,181 @@ set (Ruqola_restapi_SRCS restapi/restapirequest.cpp restapi/restapiutil.cpp restapi/restapimethod.cpp restapi/uploadfilejob.cpp restapi/serverinfojob.cpp restapi/restapiabstractjob.cpp restapi/owninfojob.cpp restapi/getavatarjob.cpp restapi/logoutjob.cpp restapi/loginjob.cpp restapi/privateinfojob.cpp restapi/channellistjob.cpp restapi/starmessagejob.cpp restapi/downloadfilejob.cpp restapi/pinmessagejob.cpp restapi/postmessagejob.cpp restapi/channels/changechanneltopicjob.cpp restapi/channels/changechannelannouncementjob.cpp + restapi/channels/changechannelnamejob.cpp restapi/groups/changegroupstopicjob.cpp restapi/groups/changegroupsannouncementjob.cpp ) set(Ruqola_ddpapi_SRCS ddpapi/ddpclient.cpp ) set (Ruqola_model_core_srcs model/messagemodel.cpp model/roommodel.cpp model/roomfilterproxymodel.cpp model/usersforroommodel.cpp model/usersforroomfilterproxymodel.cpp model/usersmodel.cpp model/usercompleterfilterproxymodel.cpp model/usercompletermodel.cpp model/statusmodel.cpp model/filesforroommodel.cpp model/filesforroomfilterproxymodel.cpp model/searchchannelmodel.cpp model/searchchannelfilterproxymodel.cpp model/inputcompletermodel.cpp model/loginmethodmodel.cpp model/searchmessagemodel.cpp model/searchmessagefilterproxymodel.cpp model/rocketchataccountmodel.cpp model/rocketchataccountfilterproxymodel.cpp ) set(Ruqola_plugins_srcs plugins/pluginauthenticationinterface.cpp plugins/pluginauthentication.cpp ) set(Ruqola_aboutdata_srcs aboutdata/qmlaboutdata.cpp aboutdata/qmlaboutdataauthormodel.cpp ) set (Ruqola_core_srcs ${Ruqola_model_core_srcs} ${Ruqola_plugins_srcs} ${Ruqola_aboutdata_srcs} ${Ruqola_ddpapi_SRCS} ${Ruqola_restapi_SRCS} authenticationinfo.cpp message.cpp room.cpp roomwrapper.cpp ruqola.cpp ruqolautils.cpp rocketchatbackend.cpp notification.cpp messagequeue.cpp rocketchatmessage.cpp typingnotification.cpp changetemporarystatus.cpp user.cpp utils.cpp emoji.cpp emojimanager.cpp clipboardproxy.cpp otr.cpp otrmanager.cpp abstractwebsocket.cpp ruqolawebsocket.cpp messageattachment.cpp messageurl.cpp rocketchataccount.cpp rocketchataccountsettings.cpp ruqolalogger.cpp ruqolaregisterengine.cpp ruqolaserverconfig.cpp rocketchatcache.cpp texthighlighter.cpp textconverter.cpp loadrecenthistorymanager.cpp unityservicemanager.cpp file.cpp channel.cpp inputtextmanager.cpp authenticationmanager.cpp searchmessage.cpp accountmanager.cpp managerdatapaths.cpp notificationoptions.cpp reactions.cpp reaction.cpp ) ecm_qt_declare_logging_category(Ruqola_core_srcs HEADER ruqola_debug.h IDENTIFIER RUQOLA_LOG CATEGORY_NAME org.kde.ruqola) ecm_qt_declare_logging_category(Ruqola_core_srcs HEADER ruqola_message_debug.h IDENTIFIER RUQOLA_MESSAGE_LOG CATEGORY_NAME org.kde.ruqola.message) ecm_qt_declare_logging_category(Ruqola_core_srcs HEADER ruqola_ddpapi_debug.h IDENTIFIER RUQOLA_DDPAPI_LOG CATEGORY_NAME org.kde.ruqola.ddpapi) ecm_qt_declare_logging_category(Ruqola_core_srcs HEADER ruqola_ddpapi_command_debug.h IDENTIFIER RUQOLA_DDPAPI_COMMAND_LOG CATEGORY_NAME org.kde.ruqola.ddpapi.command) ecm_qt_declare_logging_category(Ruqola_core_srcs HEADER ruqola_restapi_debug.h IDENTIFIER RUQOLA_RESTAPI_LOG CATEGORY_NAME org.kde.ruqola.restapi) add_library(libruqolacore ${Ruqola_core_srcs}) generate_export_header(libruqolacore BASE_NAME libruqolacore) target_link_libraries(libruqolacore Qt5::Core Qt5::Gui Qt5::Widgets Qt5::Qml Qt5::Quick Qt5::WebSockets Qt5::Network Qt5::NetworkAuth Qt5::DBus KF5::CoreAddons KF5::I18n KF5::Crash KF5::Notifications KF5::KIOCore KF5::SyntaxHighlighting ) if (WIN32 OR APPLE) target_link_libraries(libruqolacore KF5::IconThemes) endif() if (Qt5Keychain_FOUND) target_link_libraries(libruqolacore qt5keychain) target_include_directories(libruqolacore PRIVATE ${QTKEYCHAIN_INCLUDE_DIRS}) endif() set_target_properties(libruqolacore PROPERTIES OUTPUT_NAME ruqolacore VERSION ${RUQOLA_LIB_VERSION} SOVERSION ${RUQOLA_LIB_SOVERSION} ) qt5_add_resources(RuqolaResources qml/qml.qrc) add_executable(ruqola main.cpp ${RuqolaResources}) target_link_libraries(ruqola libruqolacore) install(TARGETS ruqola ${KDE_INSTALL_TARGETS_DEFAULT_ARGS}) install(TARGETS libruqolacore ${KDE_INSTALL_TARGETS_DEFAULT_ARGS} LIBRARY NAMELINK_SKIP) install(PROGRAMS data/org.kde.ruqola.desktop DESTINATION ${KDE_INSTALL_APPDIR}) install(FILES data/org.kde.ruqola.appdata.xml DESTINATION ${KDE_INSTALL_METAINFODIR}) add_subdirectory(plugins) add_subdirectory(icons) diff --git a/src/restapi/channels/changechannelnamejob.cpp b/src/restapi/channels/changechannelnamejob.cpp new file mode 100644 index 00000000..a9b6bb7a --- /dev/null +++ b/src/restapi/channels/changechannelnamejob.cpp @@ -0,0 +1,131 @@ +/* + Copyright (c) 2018 Montel Laurent + + 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 "changechannelnamejob.h" + +#include "ruqola_restapi_debug.h" +#include "restapimethod.h" +#include +#include +#include + +ChangeChannelNameJob::ChangeChannelNameJob(QObject *parent) + : RestApiAbstractJob(parent) +{ +} + +ChangeChannelNameJob::~ChangeChannelNameJob() +{ +} + +bool ChangeChannelNameJob::start() +{ + if (!canStart()) { + deleteLater(); + return false; + } + const QByteArray baPostData = json().toJson(QJsonDocument::Compact); + addLoggerInfo("ChangeChannelNameJob::start: " + baPostData); + QNetworkReply *reply = mNetworkAccessManager->post(request(), baPostData); + connect(reply, &QNetworkReply::finished, this, &ChangeChannelNameJob::slotChangeTopicFinished); + return true; +} + +void ChangeChannelNameJob::slotChangeTopicFinished() +{ + QNetworkReply *reply = qobject_cast(sender()); + if (reply) { + const QByteArray data = reply->readAll(); + const QJsonDocument replyJson = QJsonDocument::fromJson(data); + const QJsonObject replyObject = replyJson.object(); + + if (replyObject[QStringLiteral("success")].toBool()) { + qCDebug(RUQOLA_RESTAPI_LOG) << "Change Topic success"; + Q_EMIT changeAnnouncementDone(); + } else { + qCWarning(RUQOLA_RESTAPI_LOG) <<" Problem when we tried to change topic" << data; + } + } + qDebug() << " DONE !"; + deleteLater(); +} + +bool ChangeChannelNameJob::requireHttpAuthentication() const +{ + return true; +} + +bool ChangeChannelNameJob::canStart() const +{ + if (mName.isEmpty()) { + qCWarning(RUQOLA_RESTAPI_LOG) << "ChangeChannelNameJob: name is empty"; + return false; + } + if (mRoomId.isEmpty()) { + qCWarning(RUQOLA_RESTAPI_LOG) << "ChangeChannelNameJob: RoomId is empty"; + return false; + } + if (!RestApiAbstractJob::canStart()) { + qCWarning(RUQOLA_RESTAPI_LOG) << "Impossible to start ChangeChannelNameJob job"; + return false; + } + return true; +} + +QJsonDocument ChangeChannelNameJob::json() const +{ + QJsonObject jsonObj; + jsonObj[QLatin1String("roomId")] = roomId(); + jsonObj[QLatin1String("name")] = name(); + + const QJsonDocument postData = QJsonDocument(jsonObj); + return postData; +} + +QString ChangeChannelNameJob::roomId() const +{ + return mRoomId; +} + +void ChangeChannelNameJob::setRoomId(const QString &roomId) +{ + mRoomId = roomId; +} + +QString ChangeChannelNameJob::name() const +{ + return mName; +} + +void ChangeChannelNameJob::setName(const QString &t) +{ + mName = t; +} + +QNetworkRequest ChangeChannelNameJob::request() const +{ + const QUrl url = mRestApiMethod->generateUrl(RestApiUtil::RestApiUrlType::ChannelsRename); + QNetworkRequest request(url); + addAuthRawHeader(request); + request.setAttribute(QNetworkRequest::HttpPipeliningAllowedAttribute, true); + request.setAttribute(QNetworkRequest::HTTP2AllowedAttribute, true); + request.setHeader(QNetworkRequest::ContentTypeHeader, QStringLiteral("application/json")); + return request; +} diff --git a/src/restapi/channels/changechannelnamejob.h b/src/restapi/channels/changechannelnamejob.h new file mode 100644 index 00000000..9a84e94d --- /dev/null +++ b/src/restapi/channels/changechannelnamejob.h @@ -0,0 +1,57 @@ +/* + Copyright (c) 2018 Montel Laurent + + 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. +*/ + +#ifndef CHANGECHANNELNAMEJOB_H +#define CHANGECHANNELNAMEJOB_H + +#include "restapiabstractjob.h" +#include "libruqola_private_export.h" + +class LIBRUQOLACORE_TESTS_EXPORT ChangeChannelNameJob : public RestApiAbstractJob +{ + Q_OBJECT +public: + explicit ChangeChannelNameJob(QObject *parent = nullptr); + ~ChangeChannelNameJob() override; + + bool start() override; + bool requireHttpAuthentication() const override; + bool canStart() const override; + + QString name() const; + void setName(const QString &t); + QNetworkRequest request() const override; + + QJsonDocument json() const; + + QString roomId() const; + void setRoomId(const QString &roomId); + +Q_SIGNALS: + void changeAnnouncementDone(); + +private: + Q_DISABLE_COPY(ChangeChannelNameJob) + void slotChangeTopicFinished(); + QString mName; + QString mRoomId; +}; + +#endif // CHANGECHANNELANNOUNCEMENT_H