diff --git a/autotests/CMakeLists.txt b/autotests/CMakeLists.txt index 29fa9b6b..5a22749f 100644 --- a/autotests/CMakeLists.txt +++ b/autotests/CMakeLists.txt @@ -1,85 +1,86 @@ 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_ruqola_test(changegroupsnamejobtest.cpp) add_ruqola_test(deletemessagejobtest.cpp) +add_ruqola_test(createchanneljobtest.cpp) add_subdirectory(qml) diff --git a/autotests/createchanneljobtest.cpp b/autotests/createchanneljobtest.cpp new file mode 100644 index 00000000..1ecc0b97 --- /dev/null +++ b/autotests/createchanneljobtest.cpp @@ -0,0 +1,96 @@ +/* + 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 "createchanneljobtest.h" +#include "restapi/channels/createchanneljob.h" +#include "ruqola_restapi_helper.h" +#include +#include +QTEST_GUILESS_MAIN(CreateChannelJobTest) + +CreateChannelJobTest::CreateChannelJobTest(QObject *parent) + : QObject(parent) +{ +} + +void CreateChannelJobTest::shouldHaveDefaultValue() +{ + CreateChannelJob job; + verifyDefaultValue(&job); + QVERIFY(job.requireHttpAuthentication()); + QVERIFY(!job.readOnly()); + QVERIFY(job.channelName().isEmpty()); + QVERIFY(job.members().isEmpty()); +} + +void CreateChannelJobTest::shouldGenerateRequest() +{ + CreateChannelJob job; + QNetworkRequest request = QNetworkRequest(QUrl()); + verifyAuthentication(&job, request); + QCOMPARE(request.url(), QUrl(QStringLiteral("http://www.kde.org/api/v1/channels.create"))); + QCOMPARE(request.header(QNetworkRequest::ContentTypeHeader).toString(), QStringLiteral("application/json")); +} + +void CreateChannelJobTest::shouldGenerateJson() +{ + CreateChannelJob job; + const QString channelname = QStringLiteral("foo1"); + job.setChannelName(channelname); + QCOMPARE(job.json().toJson(QJsonDocument::Compact), QStringLiteral("{\"name\":\"%1\"}").arg(channelname).toLatin1()); + + bool readOnly = false; + job.setReadOnly(readOnly); + QCOMPARE(job.json().toJson(QJsonDocument::Compact), QStringLiteral("{\"name\":\"%1\"}").arg(channelname).toLatin1()); + + readOnly = true; + job.setReadOnly(readOnly); + QCOMPARE(job.json().toJson(QJsonDocument::Compact), QStringLiteral("{\"name\":\"%1\",\"readOnly\":true}").arg(channelname).toLatin1()); + + const QStringList members = {QStringLiteral("foo"), QStringLiteral("bla")}; + job.setMembers(members); + QCOMPARE(job.json().toJson(QJsonDocument::Compact), QStringLiteral("{\"members\":[\"foo\",\"bla\"],\"name\":\"%1\",\"readOnly\":true}").arg(channelname).toLatin1()); + +} + +void CreateChannelJobTest::shouldNotStarting() +{ + CreateChannelJob job; + + RestApiMethod *method = new RestApiMethod; + method->setServerUrl(QStringLiteral("http://www.kde.org")); + job.setRestApiMethod(method); + + QNetworkAccessManager *mNetworkAccessManager = new QNetworkAccessManager; + job.setNetworkAccessManager(mNetworkAccessManager); + QVERIFY(!job.canStart()); + const QString auth = QStringLiteral("foo"); + const QString userId = QStringLiteral("foo"); + job.setAuthToken(auth); + QVERIFY(!job.canStart()); + job.setUserId(userId); + QVERIFY(!job.canStart()); + const QString roomId = QStringLiteral("foo1"); + job.setChannelName(roomId); + QVERIFY(job.canStart()); + + delete method; + delete mNetworkAccessManager; +} diff --git a/autotests/owninfojobtest.cpp b/autotests/createchanneljobtest.h similarity index 59% copy from autotests/owninfojobtest.cpp copy to autotests/createchanneljobtest.h index 0f02e276..37046f2d 100644 --- a/autotests/owninfojobtest.cpp +++ b/autotests/createchanneljobtest.h @@ -1,46 +1,39 @@ /* 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 "owninfojobtest.h" -#include "restapi/owninfojob.h" -#include "ruqola_restapi_helper.h" -#include -#include -QTEST_GUILESS_MAIN(OwnInfoJobTest) +#ifndef CREATECHANNELJOBTEST_H +#define CREATECHANNELJOBTEST_H -OwnInfoJobTest::OwnInfoJobTest(QObject *parent) - : QObject(parent) -{ -} +#include -void OwnInfoJobTest::shouldHaveDefaultValue() +class CreateChannelJobTest : public QObject { - OwnInfoJob job; - verifyDefaultValue(&job); - QVERIFY(job.requireHttpAuthentication()); -} + Q_OBJECT +public: + explicit CreateChannelJobTest(QObject *parent = nullptr); + ~CreateChannelJobTest() = default; +private Q_SLOTS: + void shouldHaveDefaultValue(); + void shouldGenerateRequest(); + void shouldGenerateJson(); + void shouldNotStarting(); +}; -void OwnInfoJobTest::shouldGenerateRequest() -{ - OwnInfoJob job; - QNetworkRequest request = QNetworkRequest(QUrl()); - verifyAuthentication(&job, request); - QCOMPARE(request.url(), QUrl(QStringLiteral("http://www.kde.org/api/v1/info"))); -} +#endif // CREATECHANNELJOBTEST_H diff --git a/autotests/owninfojobtest.cpp b/autotests/owninfojobtest.cpp index 0f02e276..01af2835 100644 --- a/autotests/owninfojobtest.cpp +++ b/autotests/owninfojobtest.cpp @@ -1,46 +1,46 @@ /* 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 "owninfojobtest.h" -#include "restapi/owninfojob.h" +#include "restapi/misc/owninfojob.h" #include "ruqola_restapi_helper.h" #include #include QTEST_GUILESS_MAIN(OwnInfoJobTest) OwnInfoJobTest::OwnInfoJobTest(QObject *parent) : QObject(parent) { } void OwnInfoJobTest::shouldHaveDefaultValue() { OwnInfoJob job; verifyDefaultValue(&job); QVERIFY(job.requireHttpAuthentication()); } void OwnInfoJobTest::shouldGenerateRequest() { OwnInfoJob job; QNetworkRequest request = QNetworkRequest(QUrl()); verifyAuthentication(&job, request); QCOMPARE(request.url(), QUrl(QStringLiteral("http://www.kde.org/api/v1/info"))); } diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index b25ea6fa..6d806c7b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,186 +1,189 @@ 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/misc/owninfojob.cpp + restapi/getavatarjob.cpp restapi/logoutjob.cpp restapi/loginjob.cpp restapi/privateinfojob.cpp restapi/channellistjob.cpp restapi/downloadfilejob.cpp restapi/chat/starmessagejob.cpp restapi/chat/pinmessagejob.cpp restapi/chat/postmessagejob.cpp restapi/chat/deletemessagejob.cpp restapi/channels/changechanneltopicjob.cpp restapi/channels/changechannelannouncementjob.cpp restapi/channels/changechannelnamejob.cpp + restapi/channels/createchanneljob.cpp restapi/groups/changegroupstopicjob.cpp restapi/groups/changegroupsannouncementjob.cpp restapi/groups/changegroupsnamejob.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/changechannelannouncementjob.cpp b/src/restapi/channels/changechannelannouncementjob.cpp index 9a44239b..10569385 100644 --- a/src/restapi/channels/changechannelannouncementjob.cpp +++ b/src/restapi/channels/changechannelannouncementjob.cpp @@ -1,131 +1,130 @@ /* 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 "changechannelannouncementjob.h" #include "ruqola_restapi_debug.h" #include "restapimethod.h" #include #include #include ChangeChannelAnnouncementJob::ChangeChannelAnnouncementJob(QObject *parent) : RestApiAbstractJob(parent) { } ChangeChannelAnnouncementJob::~ChangeChannelAnnouncementJob() { } bool ChangeChannelAnnouncementJob::start() { if (!canStart()) { deleteLater(); return false; } const QByteArray baPostData = json().toJson(QJsonDocument::Compact); addLoggerInfo("ChangeChannelAnnouncementJob::start: " + baPostData); QNetworkReply *reply = mNetworkAccessManager->post(request(), baPostData); connect(reply, &QNetworkReply::finished, this, &ChangeChannelAnnouncementJob::slotChangeTopicFinished); return true; } void ChangeChannelAnnouncementJob::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 ChangeChannelAnnouncementJob::requireHttpAuthentication() const { return true; } bool ChangeChannelAnnouncementJob::canStart() const { if (mAnnouncement.isEmpty()) { qCWarning(RUQOLA_RESTAPI_LOG) << "ChangeChannelAnnouncementJob: announcement is empty"; return false; } if (mRoomId.isEmpty()) { qCWarning(RUQOLA_RESTAPI_LOG) << "ChangeChannelAnnouncementJob: RoomId is empty"; return false; } if (!RestApiAbstractJob::canStart()) { qCWarning(RUQOLA_RESTAPI_LOG) << "Impossible to start ChangeChannelAnnouncementJob job"; return false; } return true; } QJsonDocument ChangeChannelAnnouncementJob::json() const { QJsonObject jsonObj; jsonObj[QLatin1String("roomId")] = roomId(); jsonObj[QLatin1String("announcement")] = announcement(); const QJsonDocument postData = QJsonDocument(jsonObj); return postData; } QString ChangeChannelAnnouncementJob::roomId() const { return mRoomId; } void ChangeChannelAnnouncementJob::setRoomId(const QString &roomId) { mRoomId = roomId; } QString ChangeChannelAnnouncementJob::announcement() const { return mAnnouncement; } void ChangeChannelAnnouncementJob::setAnnouncement(const QString &t) { mAnnouncement = t; } QNetworkRequest ChangeChannelAnnouncementJob::request() const { const QUrl url = mRestApiMethod->generateUrl(RestApiUtil::RestApiUrlType::ChannelsSetAnnouncement); 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.cpp b/src/restapi/channels/changechannelnamejob.cpp index a9b6bb7a..875ff277 100644 --- a/src/restapi/channels/changechannelnamejob.cpp +++ b/src/restapi/channels/changechannelnamejob.cpp @@ -1,131 +1,130 @@ /* 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/changechanneltopicjob.cpp b/src/restapi/channels/changechanneltopicjob.cpp index 61c36b40..a43346ee 100644 --- a/src/restapi/channels/changechanneltopicjob.cpp +++ b/src/restapi/channels/changechanneltopicjob.cpp @@ -1,130 +1,129 @@ /* 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 "changechanneltopicjob.h" #include "ruqola_restapi_debug.h" #include "restapimethod.h" #include #include #include ChangeChannelTopicJob::ChangeChannelTopicJob(QObject *parent) : RestApiAbstractJob(parent) { } ChangeChannelTopicJob::~ChangeChannelTopicJob() { } bool ChangeChannelTopicJob::start() { if (!canStart()) { deleteLater(); return false; } const QByteArray baPostData = json().toJson(QJsonDocument::Compact); addLoggerInfo("ChangeChannelTopicJob::start: " + baPostData); QNetworkReply *reply = mNetworkAccessManager->post(request(), baPostData); connect(reply, &QNetworkReply::finished, this, &ChangeChannelTopicJob::slotChangeTopicFinished); return true; } void ChangeChannelTopicJob::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 changeTopicDone(); } else { qCWarning(RUQOLA_RESTAPI_LOG) <<" Problem when we tried to change topic" << data; } } - qDebug() << " DONE !"; deleteLater(); } bool ChangeChannelTopicJob::requireHttpAuthentication() const { return true; } bool ChangeChannelTopicJob::canStart() const { if (mTopic.isEmpty()) { qCWarning(RUQOLA_RESTAPI_LOG) << "ChangeChannelTopicJob: Topic is empty"; return false; } if (mRoomId.isEmpty()) { qCWarning(RUQOLA_RESTAPI_LOG) << "ChangeChannelTopicJob: RoomId is empty"; return false; } if (!RestApiAbstractJob::canStart()) { qCWarning(RUQOLA_RESTAPI_LOG) << "Impossible to start ChangeChannelTopicJob job"; return false; } return true; } QJsonDocument ChangeChannelTopicJob::json() const { QJsonObject jsonObj; jsonObj[QLatin1String("roomId")] = roomId(); jsonObj[QLatin1String("topic")] = topic(); const QJsonDocument postData = QJsonDocument(jsonObj); return postData; } QString ChangeChannelTopicJob::roomId() const { return mRoomId; } void ChangeChannelTopicJob::setRoomId(const QString &roomId) { mRoomId = roomId; } QString ChangeChannelTopicJob::topic() const { return mTopic; } void ChangeChannelTopicJob::setTopic(const QString &topic) { mTopic = topic; } QNetworkRequest ChangeChannelTopicJob::request() const { const QUrl url = mRestApiMethod->generateUrl(RestApiUtil::RestApiUrlType::ChannelsSetTopic); 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.cpp b/src/restapi/channels/createchanneljob.cpp similarity index 62% copy from src/restapi/channels/changechannelnamejob.cpp copy to src/restapi/channels/createchanneljob.cpp index a9b6bb7a..476fd26b 100644 --- a/src/restapi/channels/changechannelnamejob.cpp +++ b/src/restapi/channels/createchanneljob.cpp @@ -1,131 +1,142 @@ /* 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 "createchanneljob.h" #include "ruqola_restapi_debug.h" #include "restapimethod.h" +#include #include #include #include -ChangeChannelNameJob::ChangeChannelNameJob(QObject *parent) +CreateChannelJob::CreateChannelJob(QObject *parent) : RestApiAbstractJob(parent) { } -ChangeChannelNameJob::~ChangeChannelNameJob() +CreateChannelJob::~CreateChannelJob() { } -bool ChangeChannelNameJob::start() +bool CreateChannelJob::start() { if (!canStart()) { deleteLater(); return false; } const QByteArray baPostData = json().toJson(QJsonDocument::Compact); - addLoggerInfo("ChangeChannelNameJob::start: " + baPostData); + addLoggerInfo("CreateChannelJob::start: " + baPostData); QNetworkReply *reply = mNetworkAccessManager->post(request(), baPostData); - connect(reply, &QNetworkReply::finished, this, &ChangeChannelNameJob::slotChangeTopicFinished); + connect(reply, &QNetworkReply::finished, this, &CreateChannelJob::slotCreateChannelFinished); return true; } -void ChangeChannelNameJob::slotChangeTopicFinished() +void CreateChannelJob::slotCreateChannelFinished() { 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(); + Q_EMIT createChannelDone(); } else { qCWarning(RUQOLA_RESTAPI_LOG) <<" Problem when we tried to change topic" << data; } } - qDebug() << " DONE !"; deleteLater(); } -bool ChangeChannelNameJob::requireHttpAuthentication() const +QStringList CreateChannelJob::members() const { - return true; + return mMembers; } -bool ChangeChannelNameJob::canStart() const +void CreateChannelJob::setMembers(const QStringList &members) { - 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; + mMembers = members; } -QJsonDocument ChangeChannelNameJob::json() const +QString CreateChannelJob::channelName() const { - QJsonObject jsonObj; - jsonObj[QLatin1String("roomId")] = roomId(); - jsonObj[QLatin1String("name")] = name(); + return mChannelName; +} - const QJsonDocument postData = QJsonDocument(jsonObj); - return postData; +void CreateChannelJob::setChannelName(const QString &channelName) +{ + mChannelName = channelName; } -QString ChangeChannelNameJob::roomId() const +bool CreateChannelJob::readOnly() const { - return mRoomId; + return mReadOnly; } -void ChangeChannelNameJob::setRoomId(const QString &roomId) +void CreateChannelJob::setReadOnly(bool readOnly) { - mRoomId = roomId; + mReadOnly = readOnly; } -QString ChangeChannelNameJob::name() const +bool CreateChannelJob::requireHttpAuthentication() const { - return mName; + return true; } -void ChangeChannelNameJob::setName(const QString &t) +bool CreateChannelJob::canStart() const { - mName = t; + if (mChannelName.isEmpty()) { + qCWarning(RUQOLA_RESTAPI_LOG) << "CreateChannelJob: channelname is empty"; + return false; + } + if (!RestApiAbstractJob::canStart()) { + qCWarning(RUQOLA_RESTAPI_LOG) << "Impossible to start CreateChannelJob job"; + return false; + } + return true; +} + +QJsonDocument CreateChannelJob::json() const +{ + QJsonObject jsonObj; + if (!mMembers.isEmpty()) { + jsonObj[QLatin1String("members")] = QJsonArray::fromStringList(mMembers); + } + jsonObj[QLatin1String("name")] = channelName(); + if (mReadOnly) { + jsonObj[QLatin1String("readOnly")] = true; + } //Default is false + + const QJsonDocument postData = QJsonDocument(jsonObj); + return postData; } -QNetworkRequest ChangeChannelNameJob::request() const +QNetworkRequest CreateChannelJob::request() const { - const QUrl url = mRestApiMethod->generateUrl(RestApiUtil::RestApiUrlType::ChannelsRename); + const QUrl url = mRestApiMethod->generateUrl(RestApiUtil::RestApiUrlType::ChannelsCreate); 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/createchanneljob.h b/src/restapi/channels/createchanneljob.h new file mode 100644 index 00000000..8c50a38a --- /dev/null +++ b/src/restapi/channels/createchanneljob.h @@ -0,0 +1,62 @@ +/* + 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 CREATECHANNELJOB_H +#define CREATECHANNELJOB_H + +#include "restapiabstractjob.h" +#include "libruqola_private_export.h" + +class LIBRUQOLACORE_TESTS_EXPORT CreateChannelJob : public RestApiAbstractJob +{ + Q_OBJECT +public: + explicit CreateChannelJob(QObject *parent = nullptr); + ~CreateChannelJob() override; + + bool start() override; + bool requireHttpAuthentication() const override; + bool canStart() const override; + + QNetworkRequest request() const override; + + QJsonDocument json() const; + + bool readOnly() const; + void setReadOnly(bool readOnly); + + QString channelName() const; + void setChannelName(const QString &channelName); + + QStringList members() const; + void setMembers(const QStringList &members); + +Q_SIGNALS: + void createChannelDone(); + +private: + Q_DISABLE_COPY(CreateChannelJob) + void slotCreateChannelFinished(); + QString mChannelName; + QStringList mMembers; + bool mReadOnly = false; +}; + +#endif diff --git a/src/restapi/groups/changegroupsannouncementjob.cpp b/src/restapi/groups/changegroupsannouncementjob.cpp index 4f7d3867..be53b621 100644 --- a/src/restapi/groups/changegroupsannouncementjob.cpp +++ b/src/restapi/groups/changegroupsannouncementjob.cpp @@ -1,131 +1,130 @@ /* 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 "changegroupsannouncementjob.h" #include "ruqola_restapi_debug.h" #include "restapimethod.h" #include #include #include ChangeGroupsAnnouncementJob::ChangeGroupsAnnouncementJob(QObject *parent) : RestApiAbstractJob(parent) { } ChangeGroupsAnnouncementJob::~ChangeGroupsAnnouncementJob() { } bool ChangeGroupsAnnouncementJob::start() { if (!canStart()) { deleteLater(); return false; } const QByteArray baPostData = json().toJson(QJsonDocument::Compact); addLoggerInfo("ChangeGroupsAnnouncementJob::start: " + baPostData); QNetworkReply *reply = mNetworkAccessManager->post(request(), baPostData); connect(reply, &QNetworkReply::finished, this, &ChangeGroupsAnnouncementJob::slotChangeTopicFinished); return true; } void ChangeGroupsAnnouncementJob::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 ChangeGroupsAnnouncementJob::requireHttpAuthentication() const { return true; } bool ChangeGroupsAnnouncementJob::canStart() const { if (mAnnouncement.isEmpty()) { qCWarning(RUQOLA_RESTAPI_LOG) << "ChangeGroupsAnnouncementJob: announcement is empty"; return false; } if (mRoomId.isEmpty()) { qCWarning(RUQOLA_RESTAPI_LOG) << "ChangeGroupsAnnouncementJob: RoomId is empty"; return false; } if (!RestApiAbstractJob::canStart()) { qCWarning(RUQOLA_RESTAPI_LOG) << "Impossible to start ChangeGroupsAnnouncementJob job"; return false; } return true; } QJsonDocument ChangeGroupsAnnouncementJob::json() const { QJsonObject jsonObj; jsonObj[QLatin1String("roomId")] = roomId(); jsonObj[QLatin1String("announcement")] = announcement(); const QJsonDocument postData = QJsonDocument(jsonObj); return postData; } QString ChangeGroupsAnnouncementJob::roomId() const { return mRoomId; } void ChangeGroupsAnnouncementJob::setRoomId(const QString &roomId) { mRoomId = roomId; } QString ChangeGroupsAnnouncementJob::announcement() const { return mAnnouncement; } void ChangeGroupsAnnouncementJob::setAnnouncement(const QString &t) { mAnnouncement = t; } QNetworkRequest ChangeGroupsAnnouncementJob::request() const { const QUrl url = mRestApiMethod->generateUrl(RestApiUtil::RestApiUrlType::GroupsSetAnnouncement); 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/groups/changegroupsnamejob.cpp b/src/restapi/groups/changegroupsnamejob.cpp index e51933e4..7e65d01c 100644 --- a/src/restapi/groups/changegroupsnamejob.cpp +++ b/src/restapi/groups/changegroupsnamejob.cpp @@ -1,131 +1,130 @@ /* 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 "changegroupsnamejob.h" #include "ruqola_restapi_debug.h" #include "restapimethod.h" #include #include #include ChangeGroupsNameJob::ChangeGroupsNameJob(QObject *parent) : RestApiAbstractJob(parent) { } ChangeGroupsNameJob::~ChangeGroupsNameJob() { } bool ChangeGroupsNameJob::start() { if (!canStart()) { deleteLater(); return false; } const QByteArray baPostData = json().toJson(QJsonDocument::Compact); addLoggerInfo("ChangeGroupsNameJob::start: " + baPostData); QNetworkReply *reply = mNetworkAccessManager->post(request(), baPostData); connect(reply, &QNetworkReply::finished, this, &ChangeGroupsNameJob::slotChangeTopicFinished); return true; } void ChangeGroupsNameJob::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 ChangeGroupsNameJob::requireHttpAuthentication() const { return true; } bool ChangeGroupsNameJob::canStart() const { if (mName.isEmpty()) { qCWarning(RUQOLA_RESTAPI_LOG) << "ChangeGroupsNameJob: name is empty"; return false; } if (mRoomId.isEmpty()) { qCWarning(RUQOLA_RESTAPI_LOG) << "ChangeGroupsNameJob: RoomId is empty"; return false; } if (!RestApiAbstractJob::canStart()) { qCWarning(RUQOLA_RESTAPI_LOG) << "Impossible to start ChangeGroupsNameJob job"; return false; } return true; } QJsonDocument ChangeGroupsNameJob::json() const { QJsonObject jsonObj; jsonObj[QLatin1String("roomId")] = roomId(); jsonObj[QLatin1String("name")] = name(); const QJsonDocument postData = QJsonDocument(jsonObj); return postData; } QString ChangeGroupsNameJob::roomId() const { return mRoomId; } void ChangeGroupsNameJob::setRoomId(const QString &roomId) { mRoomId = roomId; } QString ChangeGroupsNameJob::name() const { return mName; } void ChangeGroupsNameJob::setName(const QString &t) { mName = t; } QNetworkRequest ChangeGroupsNameJob::request() const { const QUrl url = mRestApiMethod->generateUrl(RestApiUtil::RestApiUrlType::GroupsRename); 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/groups/changegroupstopicjob.cpp b/src/restapi/groups/changegroupstopicjob.cpp index 54064290..bf91f425 100644 --- a/src/restapi/groups/changegroupstopicjob.cpp +++ b/src/restapi/groups/changegroupstopicjob.cpp @@ -1,130 +1,129 @@ /* 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 "changegroupstopicjob.h" #include "ruqola_restapi_debug.h" #include "restapimethod.h" #include #include #include ChangeGroupsTopicJob::ChangeGroupsTopicJob(QObject *parent) : RestApiAbstractJob(parent) { } ChangeGroupsTopicJob::~ChangeGroupsTopicJob() { } bool ChangeGroupsTopicJob::start() { if (!canStart()) { deleteLater(); return false; } const QByteArray baPostData = json().toJson(QJsonDocument::Compact); addLoggerInfo("ChangeGroupsTopicJob::start: " + baPostData); QNetworkReply *reply = mNetworkAccessManager->post(request(), baPostData); connect(reply, &QNetworkReply::finished, this, &ChangeGroupsTopicJob::slotChangeTopicFinished); return true; } void ChangeGroupsTopicJob::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 changeTopicDone(); } else { qCWarning(RUQOLA_RESTAPI_LOG) <<" Problem when we tried to change topic" << data; } } - qDebug() << " DONE !"; deleteLater(); } bool ChangeGroupsTopicJob::requireHttpAuthentication() const { return true; } bool ChangeGroupsTopicJob::canStart() const { if (mTopic.isEmpty()) { qCWarning(RUQOLA_RESTAPI_LOG) << "ChangeGroupsTopicJob: Topic is empty"; return false; } if (mRoomId.isEmpty()) { qCWarning(RUQOLA_RESTAPI_LOG) << "ChangeGroupsTopicJob: RoomId is empty"; return false; } if (!RestApiAbstractJob::canStart()) { qCWarning(RUQOLA_RESTAPI_LOG) << "Impossible to start ChangeGroupsTopicJob job"; return false; } return true; } QJsonDocument ChangeGroupsTopicJob::json() const { QJsonObject jsonObj; jsonObj[QLatin1String("roomId")] = roomId(); jsonObj[QLatin1String("topic")] = topic(); const QJsonDocument postData = QJsonDocument(jsonObj); return postData; } QString ChangeGroupsTopicJob::roomId() const { return mRoomId; } void ChangeGroupsTopicJob::setRoomId(const QString &roomId) { mRoomId = roomId; } QString ChangeGroupsTopicJob::topic() const { return mTopic; } void ChangeGroupsTopicJob::setTopic(const QString &topic) { mTopic = topic; } QNetworkRequest ChangeGroupsTopicJob::request() const { const QUrl url = mRestApiMethod->generateUrl(RestApiUtil::RestApiUrlType::GroupsSetTopic); 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/restapirequest.cpp b/src/restapi/restapirequest.cpp index 07657f70..85fcd920 100644 --- a/src/restapi/restapirequest.cpp +++ b/src/restapi/restapirequest.cpp @@ -1,342 +1,342 @@ /* Copyright (c) 2017-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 "restapimethod.h" #include "restapirequest.h" #include "ruqola_restapi_debug.h" #include "serverinfojob.h" #include "uploadfilejob.h" -#include "owninfojob.h" +#include "misc/owninfojob.h" #include "getavatarjob.h" #include "logoutjob.h" #include "privateinfojob.h" #include "channellistjob.h" #include "loginjob.h" #include "downloadfilejob.h" #include "chat/starmessagejob.h" #include "chat/postmessagejob.h" #include "chat/deletemessagejob.h" #include "channels/changechanneltopicjob.h" #include "groups/changegroupstopicjob.h" #include "channels/changechannelannouncementjob.h" #include "groups/changegroupsannouncementjob.h" #include #include #include #include #include #include #include #include #include #include #include #include #include RestApiRequest::RestApiRequest(QObject *parent) : QObject(parent) { mRestApiMethod = new RestApiMethod; mCookieJar = new QNetworkCookieJar; mNetworkAccessManager = new QNetworkAccessManager(this); mNetworkAccessManager->setCookieJar(mCookieJar); connect(mNetworkAccessManager, &QNetworkAccessManager::finished, this, &RestApiRequest::slotResult); connect(mNetworkAccessManager, &QNetworkAccessManager::sslErrors, this, &RestApiRequest::slotSslErrors); } RestApiRequest::~RestApiRequest() { delete mRestApiMethod; } void RestApiRequest::setRuqolaLogger(RuqolaLogger *logger) { mRuqolaLogger = logger; } void RestApiRequest::initializeCookies() { QString url = serverUrl(); if (!url.isEmpty()) { QString host; QStringList lsthost = url.split(QStringLiteral("//")); if (lsthost.count() < 2) { host = url; } else { host = lsthost.at(1); } if (!mUserId.isEmpty()) { QNetworkCookie userIdCookie; userIdCookie.setDomain(host); userIdCookie.setName(QByteArrayLiteral("rc_uid")); userIdCookie.setValue(mUserId.toLocal8Bit()); mCookieJar->insertCookie(userIdCookie); } if (!mAuthToken.isEmpty()) { QNetworkCookie tokenCookie; tokenCookie.setDomain(host); tokenCookie.setName(QByteArrayLiteral("rc_token")); tokenCookie.setValue(mAuthToken.toLocal8Bit()); mCookieJar->insertCookie(tokenCookie); } } else { qCWarning(RUQOLA_RESTAPI_LOG) << "We can not initialize cookies as server url is empty."; } } void RestApiRequest::setAuthToken(const QString &authToken) { const bool isChanged = (mAuthToken != authToken); mAuthToken = authToken; if (isChanged) { if (!mAuthToken.isEmpty()) { initializeCookies(); } } } void RestApiRequest::setUserId(const QString &userId) { const bool isChanged = (mUserId != userId); mUserId = userId; if (isChanged) { if (!mUserId.isEmpty()) { initializeCookies(); } } } void RestApiRequest::slotResult(QNetworkReply *reply) { if (reply->error() != QNetworkReply::NoError) { qCWarning(RUQOLA_RESTAPI_LOG) << " Error reply - "<errorString(); } } void RestApiRequest::slotSslErrors(QNetworkReply *reply, const QList &error) { qCDebug(RUQOLA_RESTAPI_LOG) << " void RestApiRequest::slotSslErrors(QNetworkReply *reply, const QList &error)" << error.count(); reply->ignoreSslErrors(error); } void RestApiRequest::setPassword(const QString &password) { mPassword = password; } void RestApiRequest::setUserName(const QString &userName) { mUserName = userName; } QString RestApiRequest::serverUrl() const { return mRestApiMethod->serverUrl(); } void RestApiRequest::setServerUrl(const QString &serverUrl) { mRestApiMethod->setServerUrl(serverUrl); } QString RestApiRequest::authToken() const { return mAuthToken; } QString RestApiRequest::userId() const { return mUserId; } void RestApiRequest::login() { LoginJob *job = new LoginJob(this); connect(job, &LoginJob::loginDone, this, &RestApiRequest::slotLogin); initializeRestApiJob(job, false); job->setPassword(mPassword); job->setUserName(mUserName); job->start(); } void RestApiRequest::slotLogin(const QString &authToken, const QString &userId) { mAuthToken = authToken; mUserId = userId; } void RestApiRequest::slotLogout() { mUserId.clear(); mAuthToken.clear(); Q_EMIT logoutDone(); } void RestApiRequest::initializeRestApiJob(RestApiAbstractJob *job, bool needAuthentication) { job->setNetworkAccessManager(mNetworkAccessManager); job->setRuqolaLogger(mRuqolaLogger); job->setRestApiMethod(mRestApiMethod); if (needAuthentication) { job->setAuthToken(mAuthToken); job->setUserId(mUserId); } } void RestApiRequest::logout() { LogoutJob *job = new LogoutJob(this); connect(job, &LogoutJob::logoutDone, this, &RestApiRequest::slotLogout); initializeRestApiJob(job, true); job->start(); } void RestApiRequest::channelList() { ChannelListJob *job = new ChannelListJob(this); //TODO connect(job, &ChannelListJob::channelListDone, this, &RestApiRequest::slotLogout); initializeRestApiJob(job, true); job->start(); } void RestApiRequest::getAvatar(const QString &userId) { GetAvatarJob *job = new GetAvatarJob(this); connect(job, &GetAvatarJob::avatar, this, &RestApiRequest::avatar); initializeRestApiJob(job, false); job->setAvatarUserId(userId); job->start(); } void RestApiRequest::getPrivateSettings() { PrivateInfoJob *job = new PrivateInfoJob(this); connect(job, &PrivateInfoJob::privateInfoDone, this, &RestApiRequest::privateInfoDone); initializeRestApiJob(job, true); job->start(); } void RestApiRequest::getOwnInfo() { OwnInfoJob *job = new OwnInfoJob(this); connect(job, &OwnInfoJob::ownInfoDone, this, &RestApiRequest::getOwnInfoDone); initializeRestApiJob(job, true); job->start(); } void RestApiRequest::starMessage(const QString &messageId, bool starred) { StarMessageJob *job = new StarMessageJob(this); initializeRestApiJob(job, true); job->setMessageId(messageId); job->setStarMessage(starred); job->start(); } void RestApiRequest::downloadFile(const QUrl &url, const QString &mimeType, bool storeInCache, const QUrl &localFileUrl) { DownloadFileJob *job = new DownloadFileJob(this); //Rename signal connect(job, &DownloadFileJob::downloadFileDone, this, &RestApiRequest::getDataDone); job->setUrl(url); job->setMimeType(mimeType); job->setLocalFileUrl(localFileUrl); job->setStoreInCache(storeInCache); initializeRestApiJob(job, true); job->start(); } void RestApiRequest::serverInfo() { ServerInfoJob *job = new ServerInfoJob(this); initializeRestApiJob(job, false); connect(job, &ServerInfoJob::serverInfoDone, this, &RestApiRequest::getServerInfoDone); job->start(); } void RestApiRequest::uploadFile(const QString &roomId, const QString &description, const QString &text, const QUrl &filename) { UploadFileJob *job = new UploadFileJob(this); initializeRestApiJob(job, true); job->setDescription(description); job->setMessageText(text); job->setFilenameUrl(filename); job->setRoomId(roomId); job->start(); } void RestApiRequest::changeChannelTopic(const QString &roomId, const QString &topic) { ChangeChannelTopicJob *job = new ChangeChannelTopicJob(this); initializeRestApiJob(job, true); job->setRoomId(roomId); job->setTopic(topic); job->start(); } void RestApiRequest::changeGroupsTopic(const QString &roomId, const QString &topic) { ChangeGroupsTopicJob *job = new ChangeGroupsTopicJob(this); initializeRestApiJob(job, true); job->setRoomId(roomId); job->setTopic(topic); job->start(); } void RestApiRequest::changeChannelAnnouncement(const QString &roomId, const QString &announcement) { ChangeGroupsAnnouncementJob *job = new ChangeGroupsAnnouncementJob(this); initializeRestApiJob(job, true); job->setRoomId(roomId); job->setAnnouncement(announcement); job->start(); } void RestApiRequest::changeGroupsAnnouncement(const QString &roomId, const QString &announcement) { ChangeGroupsAnnouncementJob *job = new ChangeGroupsAnnouncementJob(this); initializeRestApiJob(job, true); job->setRoomId(roomId); job->setAnnouncement(announcement); job->start(); } void RestApiRequest::postMessage(const QString &roomId, const QString &text) { PostMessageJob *job = new PostMessageJob(this); initializeRestApiJob(job, true); job->setRoomId(roomId); job->setText(text); job->start(); } void RestApiRequest::deleteMessage(const QString &roomId, const QString &messageId) { DeleteMessageJob *job = new DeleteMessageJob(this); initializeRestApiJob(job, true); job->setRoomId(roomId); job->setMessageId(messageId); job->start(); }