diff --git a/CMakeLists.txt b/CMakeLists.txt index ef50a774..c0efb7a2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,96 +1,96 @@ -set(RUQOLA_VERSION "0.7.7") +set(RUQOLA_VERSION "0.7.8") cmake_minimum_required(VERSION 3.1) project(Ruqola VERSION ${RUQOLA_VERSION}) set (CMAKE_CXX_STANDARD 11) set(KF5_VERSION "5.42.0") find_package(ECM ${KF5_VERSION} CONFIG REQUIRED) set(CMAKE_MODULE_PATH ${Ruqola_SOURCE_DIR}/cmake/modules ${ECM_MODULE_PATH}) if (POLICY CMP0063) cmake_policy(SET CMP0063 NEW) endif() if (POLICY CMP0071) cmake_policy(SET CMP0071 NEW) endif() include(KDECompilerSettings) include(KDEInstallDirs) include(KDECMakeSettings) include(ECMInstallIcons) include(ECMQtDeclareLoggingCategory) include(ECMAddTests) include(GenerateExportHeader) include(FeatureSummary) set(REQUIRED_QT_VERSION "5.9.0") option(PLUGINS_AUTHENTICATION_BASED_ON_O2 "Build authentication based on o2 lib (experimental)" FALSE) find_package(Qt5 ${REQUIRED_QT_VERSION} REQUIRED COMPONENTS Core Gui Widgets Qml Quick WebSockets Network NetworkAuth Test) find_package(KF5 ${KF5_VERSION} REQUIRED COMPONENTS Kirigami2 CoreAddons I18n Crash KIO Notifications SyntaxHighlighting DocTools ) if (WIN32 OR APPLE) find_package(KF5 ${KF5_VERSION} REQUIRED COMPONENTS IconThemes ) endif() find_package(Qt5Keychain REQUIRED) set_package_properties(Qt5Keychain PROPERTIES DESCRIPTION "Provides support for secure credentials storage" URL "https://github.com/frankosterfeld/qtkeychain" TYPE OPTIONAL) if (Qt5Keychain_FOUND) set(HAVE_QT5KEYCHAIN 1) else() set(HAVE_QT5KEYCHAIN 0) endif() include_directories(${CMAKE_CURRENT_BINARY_DIR}) add_definitions("-DQT_NO_CAST_FROM_ASCII -DQT_NO_CAST_TO_ASCII") add_definitions(-DQT_NO_NARROWING_CONVERSIONS_IN_CONNECT) add_definitions(-DQT_DISABLE_DEPRECATED_BEFORE=0x060000) add_definitions(-DQT_NO_URL_CAST_FROM_STRING) set(RUQOLA_LIB_VERSION "${RUQOLA_VERSION}") set(RUQOLA_LIB_SOVERSION "0") configure_file(config-ruqola.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/config-ruqola.h) if(BUILD_TESTING) add_definitions(-DBUILD_TESTING) endif(BUILD_TESTING) if (PLUGINS_AUTHENTICATION_BASED_ON_O2) find_package(o2) endif() add_subdirectory(src) if (BUILD_TESTING) add_subdirectory(autotests) add_subdirectory(tests) endif() add_subdirectory(doc) install(FILES ruqola.categories DESTINATION ${KDE_INSTALL_CONFDIR}) feature_summary(WHAT ALL INCLUDE_QUIET_PACKAGES FATAL_ON_MISSING_REQUIRED_PACKAGES) diff --git a/autotests/CMakeLists.txt b/autotests/CMakeLists.txt index 5f70277b..eb4bacf5 100644 --- a/autotests/CMakeLists.txt +++ b/autotests/CMakeLists.txt @@ -1,69 +1,74 @@ 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_subdirectory(qml) diff --git a/autotests/getavatarjobtest.cpp b/autotests/getavatarjobtest.cpp index 844e127f..e60f86aa 100644 --- a/autotests/getavatarjobtest.cpp +++ b/autotests/getavatarjobtest.cpp @@ -1,35 +1,54 @@ /* 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 "getavatarjobtest.h" #include "restapi/getavatarjob.h" +#include "restapi/restapimethod.h" #include QTEST_GUILESS_MAIN(GetAvatarJobTest) GetAvatarJobTest::GetAvatarJobTest(QObject *parent) : QObject(parent) { } void GetAvatarJobTest::shouldHaveDefaultValue() { + GetAvatarJob job; + QVERIFY(!job.restApiMethod()); + QVERIFY(!job.networkAccessManager()); + QVERIFY(!job.start()); + QVERIFY(!job.requireHttpAuthentication()); + QVERIFY(job.avatarUserId().isEmpty()); +} +void GetAvatarJobTest::shouldGenerateRequest() +{ + GetAvatarJob job; + RestApiMethod *method = new RestApiMethod; + method->setServerUrl(QStringLiteral("http://www.kde.org")); + job.setRestApiMethod(method); + const QString avatarUserId = QStringLiteral("avat"); + job.setAvatarUserId(avatarUserId); + const QNetworkRequest request = job.request(); + QCOMPARE(request.url(), QUrl(QStringLiteral("http://www.kde.org/api/v1/users.getAvatar?userId=avat"))); + delete method; } diff --git a/autotests/getavatarjobtest.h b/autotests/getavatarjobtest.h index 810611cb..1ffb77e3 100644 --- a/autotests/getavatarjobtest.h +++ b/autotests/getavatarjobtest.h @@ -1,36 +1,37 @@ /* 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 GETAVATARJOBTEST_H #define GETAVATARJOBTEST_H #include class GetAvatarJobTest : public QObject { Q_OBJECT public: explicit GetAvatarJobTest(QObject *parent = nullptr); ~GetAvatarJobTest() = default; private Q_SLOTS: void shouldHaveDefaultValue(); + void shouldGenerateRequest(); }; #endif // GETAVATARJOBTEST_H diff --git a/autotests/getavatarjobtest.cpp b/autotests/loginjobtest.cpp similarity index 82% copy from autotests/getavatarjobtest.cpp copy to autotests/loginjobtest.cpp index 844e127f..127186c6 100644 --- a/autotests/getavatarjobtest.cpp +++ b/autotests/loginjobtest.cpp @@ -1,35 +1,29 @@ /* 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 "getavatarjobtest.h" -#include "restapi/getavatarjob.h" +#include "loginjobtest.h" #include -QTEST_GUILESS_MAIN(GetAvatarJobTest) +QTEST_GUILESS_MAIN(LoginJobTest) -GetAvatarJobTest::GetAvatarJobTest(QObject *parent) +LoginJobTest::LoginJobTest(QObject *parent) : QObject(parent) { } - -void GetAvatarJobTest::shouldHaveDefaultValue() -{ - -} diff --git a/autotests/getavatarjobtest.cpp b/autotests/loginjobtest.h similarity index 79% copy from autotests/getavatarjobtest.cpp copy to autotests/loginjobtest.h index 844e127f..48de0868 100644 --- a/autotests/getavatarjobtest.cpp +++ b/autotests/loginjobtest.h @@ -1,35 +1,34 @@ /* 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 "getavatarjobtest.h" -#include "restapi/getavatarjob.h" -#include -QTEST_GUILESS_MAIN(GetAvatarJobTest) +#ifndef LOGINJOBTEST_H +#define LOGINJOBTEST_H -GetAvatarJobTest::GetAvatarJobTest(QObject *parent) - : QObject(parent) -{ - -} +#include -void GetAvatarJobTest::shouldHaveDefaultValue() +class LoginJobTest : public QObject { + Q_OBJECT +public: + explicit LoginJobTest(QObject *parent = nullptr); + ~LoginJobTest() = default; +}; -} +#endif // LOGINJOBTEST_H diff --git a/autotests/logoutjobtest.cpp b/autotests/logoutjobtest.cpp new file mode 100644 index 00000000..e3a0fc7a --- /dev/null +++ b/autotests/logoutjobtest.cpp @@ -0,0 +1,60 @@ +/* + 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 "logoutjobtest.h" +#include "restapi/logoutjob.h" +#include +#include +QTEST_GUILESS_MAIN(LogoutJobTest) + +LogoutJobTest::LogoutJobTest(QObject *parent) + : QObject(parent) +{ + +} + +void LogoutJobTest::shouldHaveDefaultValue() +{ + LogoutJob job; + QVERIFY(!job.restApiMethod()); + QVERIFY(!job.networkAccessManager()); + QVERIFY(!job.start()); + QVERIFY(job.requireHttpAuthentication()); + QVERIFY(job.authToken().isEmpty()); + QVERIFY(job.userId().isEmpty()); +} + + +void LogoutJobTest::shouldGenerateRequest() +{ + LogoutJob job; + const QString authToken = QStringLiteral("foo"); + const QString userId = QStringLiteral("user"); + job.setUserId(userId); + job.setAuthToken(authToken); + RestApiMethod *method = new RestApiMethod; + method->setServerUrl(QStringLiteral("http://www.kde.org")); + job.setRestApiMethod(method); + const QNetworkRequest request = job.request(); + QCOMPARE(request.url(), QUrl(QStringLiteral("http://www.kde.org/api/v1/logout"))); + QCOMPARE(request.rawHeader(QByteArrayLiteral("X-Auth-Token")), authToken.toLocal8Bit()); + QCOMPARE(request.rawHeader(QByteArrayLiteral("X-User-Id")), userId.toLocal8Bit()); + delete method; +} diff --git a/autotests/getavatarjobtest.h b/autotests/logoutjobtest.h similarity index 81% copy from autotests/getavatarjobtest.h copy to autotests/logoutjobtest.h index 810611cb..9028fbbd 100644 --- a/autotests/getavatarjobtest.h +++ b/autotests/logoutjobtest.h @@ -1,36 +1,37 @@ /* 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 GETAVATARJOBTEST_H -#define GETAVATARJOBTEST_H +#ifndef LOGOUTJOBTEST_H +#define LOGOUTJOBTEST_H #include -class GetAvatarJobTest : public QObject +class LogoutJobTest : public QObject { Q_OBJECT public: - explicit GetAvatarJobTest(QObject *parent = nullptr); - ~GetAvatarJobTest() = default; + explicit LogoutJobTest(QObject *parent = nullptr); + ~LogoutJobTest() = default; private Q_SLOTS: void shouldHaveDefaultValue(); + void shouldGenerateRequest(); }; -#endif // GETAVATARJOBTEST_H +#endif // LOGOUTJOBTEST_H diff --git a/autotests/getavatarjobtest.cpp b/autotests/privateinfojobtest.cpp similarity index 82% copy from autotests/getavatarjobtest.cpp copy to autotests/privateinfojobtest.cpp index 844e127f..1527f579 100644 --- a/autotests/getavatarjobtest.cpp +++ b/autotests/privateinfojobtest.cpp @@ -1,35 +1,29 @@ /* 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 "getavatarjobtest.h" -#include "restapi/getavatarjob.h" +#include "privateinfojobtest.h" #include -QTEST_GUILESS_MAIN(GetAvatarJobTest) +QTEST_GUILESS_MAIN(PrivateInfoJobTest) -GetAvatarJobTest::GetAvatarJobTest(QObject *parent) +PrivateInfoJobTest::PrivateInfoJobTest(QObject *parent) : QObject(parent) { } - -void GetAvatarJobTest::shouldHaveDefaultValue() -{ - -} diff --git a/autotests/getavatarjobtest.h b/autotests/privateinfojobtest.h similarity index 78% copy from autotests/getavatarjobtest.h copy to autotests/privateinfojobtest.h index 810611cb..c61f1aa9 100644 --- a/autotests/getavatarjobtest.h +++ b/autotests/privateinfojobtest.h @@ -1,36 +1,35 @@ /* 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 GETAVATARJOBTEST_H -#define GETAVATARJOBTEST_H +#ifndef PRIVATEINFOJOBTEST_H +#define PRIVATEINFOJOBTEST_H #include -class GetAvatarJobTest : public QObject +class PrivateInfoJobTest : public QObject { Q_OBJECT public: - explicit GetAvatarJobTest(QObject *parent = nullptr); - ~GetAvatarJobTest() = default; -private Q_SLOTS: - void shouldHaveDefaultValue(); + explicit PrivateInfoJobTest(QObject *parent = nullptr); + ~PrivateInfoJobTest() = default; + }; -#endif // GETAVATARJOBTEST_H +#endif // PRIVATEINFOJOBTEST_H diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 40acd201..9fadde80 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,169 +1,172 @@ 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 ) 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/getavatarjob.cpp b/src/restapi/getavatarjob.cpp index 09388497..79f658f1 100644 --- a/src/restapi/getavatarjob.cpp +++ b/src/restapi/getavatarjob.cpp @@ -1,77 +1,97 @@ /* 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 "getavatarjob.h" #include "ruqola_ddpapi_debug.h" #include "restapimethod.h" #include "restapirequest.h" #include #include #include GetAvatarJob::GetAvatarJob(QObject *parent) : RestApiAbstractJob(parent) { } GetAvatarJob::~GetAvatarJob() { } bool GetAvatarJob::start() { if (mAvatarUserId.isEmpty()) { qCWarning(RUQOLA_DDPAPI_LOG) << "Avatar userid is empty"; deleteLater(); return false; } if (!canStart()) { qCWarning(RUQOLA_DDPAPI_LOG) << "Impossible to start getavatar job"; deleteLater(); return false; } + QNetworkReply *reply = mNetworkAccessManager->get(request()); + connect(reply, &QNetworkReply::finished, this, &GetAvatarJob::slotGetAvatarInfo); + reply->setProperty("method", QVariant::fromValue(RestApiRequest::RestMethod::GetAvatar)); + reply->setProperty("userId", mAvatarUserId); + + return true; +} + +void GetAvatarJob::slotGetAvatarInfo() +{ + QNetworkReply *reply = qobject_cast(sender()); + if (reply) { + const QByteArray data = reply->readAll(); + //qCDebug(RUQOLA_RESTAPI_LOG) << "RestApiRequest::parseGetAvatar: " << data << " userId "<property("userId").toString(); + Q_EMIT avatar(userId, str); + } + deleteLater(); +} + +QNetworkRequest GetAvatarJob::request() const +{ QUrl url = mRestApiMethod->generateUrl(RestApiUtil::RestApiUrlType::UsersGetAvatar); QUrlQuery queryUrl; queryUrl.addQueryItem(QStringLiteral("userId"), mAvatarUserId); url.setQuery(queryUrl); QNetworkRequest request(url); - QNetworkReply *reply = mNetworkAccessManager->get(request); - reply->setProperty("method", QVariant::fromValue(RestApiRequest::RestMethod::GetAvatar)); - reply->setProperty("userId", mAvatarUserId); - - return true; + return request; } bool GetAvatarJob::requireHttpAuthentication() const { - return true; + return false; } QString GetAvatarJob::avatarUserId() const { return mAvatarUserId; } void GetAvatarJob::setAvatarUserId(const QString &avatarUserId) { mAvatarUserId = avatarUserId; } diff --git a/src/restapi/getavatarjob.h b/src/restapi/getavatarjob.h index 6d06c06a..341650b5 100644 --- a/src/restapi/getavatarjob.h +++ b/src/restapi/getavatarjob.h @@ -1,47 +1,53 @@ /* 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 GETAVATARJOB_H #define GETAVATARJOB_H #include "libruqola_private_export.h" #include "restapiabstractjob.h" #include class LIBRUQOLACORE_TESTS_EXPORT GetAvatarJob : public RestApiAbstractJob { Q_OBJECT public: explicit GetAvatarJob(QObject *parent = nullptr); ~GetAvatarJob() override; bool start() override; bool requireHttpAuthentication() const override; QString avatarUserId() const; void setAvatarUserId(const QString &avatarUserId); + QNetworkRequest request() const; + +Q_SIGNALS: + void avatar(const QString &userId, const QString &url); + private: Q_DISABLE_COPY(GetAvatarJob) + void slotGetAvatarInfo(); QString mAvatarUserId; }; #endif // GETAVATARJOB_H diff --git a/autotests/getavatarjobtest.cpp b/src/restapi/loginjob.cpp similarity index 77% copy from autotests/getavatarjobtest.cpp copy to src/restapi/loginjob.cpp index 844e127f..54200052 100644 --- a/autotests/getavatarjobtest.cpp +++ b/src/restapi/loginjob.cpp @@ -1,35 +1,43 @@ /* 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 "getavatarjobtest.h" -#include "restapi/getavatarjob.h" -#include -QTEST_GUILESS_MAIN(GetAvatarJobTest) +#include "loginjob.h" +#include "ruqola_restapi_debug.h" +LoginJob::LoginJob(QObject *parent) + : RestApiAbstractJob(parent) +{ + +} -GetAvatarJobTest::GetAvatarJobTest(QObject *parent) - : QObject(parent) +LoginJob::~LoginJob() { } -void GetAvatarJobTest::shouldHaveDefaultValue() +bool LoginJob::start() { + //TODO + return false; +} +bool LoginJob::requireHttpAuthentication() const +{ + return true; } diff --git a/src/restapi/getavatarjob.h b/src/restapi/loginjob.h similarity index 75% copy from src/restapi/getavatarjob.h copy to src/restapi/loginjob.h index 6d06c06a..9cee19b4 100644 --- a/src/restapi/getavatarjob.h +++ b/src/restapi/loginjob.h @@ -1,47 +1,41 @@ /* 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 GETAVATARJOB_H -#define GETAVATARJOB_H - +#ifndef LOGINJOB_H +#define LOGINJOB_H #include "libruqola_private_export.h" #include "restapiabstractjob.h" #include -class LIBRUQOLACORE_TESTS_EXPORT GetAvatarJob : public RestApiAbstractJob +class LIBRUQOLACORE_TESTS_EXPORT LoginJob : public RestApiAbstractJob { Q_OBJECT public: - explicit GetAvatarJob(QObject *parent = nullptr); - ~GetAvatarJob() override; + explicit LoginJob(QObject *parent = nullptr); + ~LoginJob() override; bool start() override; bool requireHttpAuthentication() const override; - - QString avatarUserId() const; - void setAvatarUserId(const QString &avatarUserId); - private: - Q_DISABLE_COPY(GetAvatarJob) - QString mAvatarUserId; + Q_DISABLE_COPY(LoginJob) }; -#endif // GETAVATARJOB_H +#endif // LOGINJOB_H diff --git a/src/restapi/owninfojob.cpp b/src/restapi/logoutjob.cpp similarity index 59% copy from src/restapi/owninfojob.cpp copy to src/restapi/logoutjob.cpp index affe4c86..dbe5f8f5 100644 --- a/src/restapi/owninfojob.cpp +++ b/src/restapi/logoutjob.cpp @@ -1,78 +1,87 @@ /* 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 "owninfojob.h" -#include "ruqola_ddpapi_debug.h" +#include "logoutjob.h" +#include "ruqola_restapi_debug.h" #include "restapimethod.h" #include "restapirequest.h" + #include #include #include #include -OwnInfoJob::OwnInfoJob(QObject *parent) +LogoutJob::LogoutJob(QObject *parent) : RestApiAbstractJob(parent) { } -OwnInfoJob::~OwnInfoJob() +LogoutJob::~LogoutJob() { } -bool OwnInfoJob::requireHttpAuthentication() const -{ - return true; -} - -bool OwnInfoJob::start() +bool LogoutJob::start() { if (!canStart()) { - qCWarning(RUQOLA_DDPAPI_LOG) << "Impossible to start owninfo job"; + qCWarning(RUQOLA_RESTAPI_LOG) << "Impossible to start LogoutJob job"; deleteLater(); return false; } + QNetworkReply *reply = mNetworkAccessManager->get(request()); - connect(reply, &QNetworkReply::finished, this, &OwnInfoJob::slotServerInfoFinished); - reply->setProperty("method", QVariant::fromValue(RestApiRequest::RestMethod::Me)); - return true; + connect(reply, &QNetworkReply::finished, this, &LogoutJob::slotLogout); + reply->setProperty("method", QVariant::fromValue(RestApiRequest::RestMethod::Logout)); + return false; } -void OwnInfoJob::slotServerInfoFinished() +void LogoutJob::slotLogout() { QNetworkReply *reply = qobject_cast(sender()); if (reply) { const QByteArray data = reply->readAll(); - Q_EMIT ownInfoDone(data); + const QJsonDocument replyJson = QJsonDocument::fromJson(data); + const QJsonObject replyObject = replyJson.object(); + + if (replyObject[QStringLiteral("status")].toString() == QStringLiteral("success")) { + qCDebug(RUQOLA_RESTAPI_LOG) << " Logout"; + Q_EMIT logoutDone(); + } else { + qCWarning(RUQOLA_RESTAPI_LOG) <<" Problem when we try to logout"; + } + + qCDebug(RUQOLA_RESTAPI_LOG) << " void RestApiRequest::parseLogout(const QByteArray &data)" << data; } deleteLater(); } -QNetworkRequest OwnInfoJob::request() const +QNetworkRequest LogoutJob::request() const +{ + const QUrl url = mRestApiMethod->generateUrl(RestApiUtil::RestApiUrlType::Logout); + QNetworkRequest req(url); + addAuthRawHeader(req); + return req; +} + +bool LogoutJob::requireHttpAuthentication() const { - const QUrl url = mRestApiMethod->generateUrl(RestApiUtil::RestApiUrlType::ServerInfo); - QNetworkRequest request(url); - request.setRawHeader(QByteArrayLiteral("X-Auth-Token"), mAuthToken.toLocal8Bit()); - request.setRawHeader(QByteArrayLiteral("X-User-Id"), mUserId.toLocal8Bit()); - request.setAttribute(QNetworkRequest::HttpPipeliningAllowedAttribute, true); - request.setAttribute(QNetworkRequest::HTTP2AllowedAttribute, true); - return request; + return true; } diff --git a/src/restapi/getavatarjob.h b/src/restapi/logoutjob.h similarity index 75% copy from src/restapi/getavatarjob.h copy to src/restapi/logoutjob.h index 6d06c06a..7d8a2663 100644 --- a/src/restapi/getavatarjob.h +++ b/src/restapi/logoutjob.h @@ -1,47 +1,48 @@ /* 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 GETAVATARJOB_H -#define GETAVATARJOB_H - +#ifndef LOGOUTJOB_H +#define LOGOUTJOB_H #include "libruqola_private_export.h" #include "restapiabstractjob.h" #include -class LIBRUQOLACORE_TESTS_EXPORT GetAvatarJob : public RestApiAbstractJob +class LIBRUQOLACORE_TESTS_EXPORT LogoutJob : public RestApiAbstractJob { Q_OBJECT public: - explicit GetAvatarJob(QObject *parent = nullptr); - ~GetAvatarJob() override; + explicit LogoutJob(QObject *parent = nullptr); + ~LogoutJob() override; bool start() override; bool requireHttpAuthentication() const override; - QString avatarUserId() const; - void setAvatarUserId(const QString &avatarUserId); + QNetworkRequest request() const; + +Q_SIGNALS: + void logoutDone(); private: - Q_DISABLE_COPY(GetAvatarJob) - QString mAvatarUserId; + Q_DISABLE_COPY(LogoutJob) + void slotLogout(); }; -#endif // GETAVATARJOB_H +#endif // LOGOUTJOB_H diff --git a/src/restapi/owninfojob.cpp b/src/restapi/owninfojob.cpp index affe4c86..95cbf064 100644 --- a/src/restapi/owninfojob.cpp +++ b/src/restapi/owninfojob.cpp @@ -1,78 +1,77 @@ /* 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 "owninfojob.h" -#include "ruqola_ddpapi_debug.h" #include "restapimethod.h" +#include "ruqola_restapi_debug.h" #include "restapirequest.h" #include #include #include #include OwnInfoJob::OwnInfoJob(QObject *parent) : RestApiAbstractJob(parent) { } OwnInfoJob::~OwnInfoJob() { } bool OwnInfoJob::requireHttpAuthentication() const { return true; } bool OwnInfoJob::start() { if (!canStart()) { - qCWarning(RUQOLA_DDPAPI_LOG) << "Impossible to start owninfo job"; + qCWarning(RUQOLA_RESTAPI_LOG) << "Impossible to start owninfo job"; deleteLater(); return false; } QNetworkReply *reply = mNetworkAccessManager->get(request()); connect(reply, &QNetworkReply::finished, this, &OwnInfoJob::slotServerInfoFinished); reply->setProperty("method", QVariant::fromValue(RestApiRequest::RestMethod::Me)); return true; } void OwnInfoJob::slotServerInfoFinished() { QNetworkReply *reply = qobject_cast(sender()); if (reply) { const QByteArray data = reply->readAll(); Q_EMIT ownInfoDone(data); } deleteLater(); } QNetworkRequest OwnInfoJob::request() const { const QUrl url = mRestApiMethod->generateUrl(RestApiUtil::RestApiUrlType::ServerInfo); QNetworkRequest request(url); - request.setRawHeader(QByteArrayLiteral("X-Auth-Token"), mAuthToken.toLocal8Bit()); - request.setRawHeader(QByteArrayLiteral("X-User-Id"), mUserId.toLocal8Bit()); + addAuthRawHeader(request); request.setAttribute(QNetworkRequest::HttpPipeliningAllowedAttribute, true); request.setAttribute(QNetworkRequest::HTTP2AllowedAttribute, true); return request; } diff --git a/src/restapi/owninfojob.cpp b/src/restapi/privateinfojob.cpp similarity index 64% copy from src/restapi/owninfojob.cpp copy to src/restapi/privateinfojob.cpp index affe4c86..b4794814 100644 --- a/src/restapi/owninfojob.cpp +++ b/src/restapi/privateinfojob.cpp @@ -1,78 +1,76 @@ /* 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 "owninfojob.h" -#include "ruqola_ddpapi_debug.h" +#include "privateinfojob.h" +#include "ruqola_restapi_debug.h" #include "restapimethod.h" #include "restapirequest.h" #include #include #include #include -OwnInfoJob::OwnInfoJob(QObject *parent) +PrivateInfoJob::PrivateInfoJob(QObject *parent) : RestApiAbstractJob(parent) { } -OwnInfoJob::~OwnInfoJob() +PrivateInfoJob::~PrivateInfoJob() { } -bool OwnInfoJob::requireHttpAuthentication() const -{ - return true; -} - -bool OwnInfoJob::start() +bool PrivateInfoJob::start() { if (!canStart()) { - qCWarning(RUQOLA_DDPAPI_LOG) << "Impossible to start owninfo job"; + qCWarning(RUQOLA_RESTAPI_LOG) << "Impossible to start PrivateInfoJob job"; deleteLater(); return false; } QNetworkReply *reply = mNetworkAccessManager->get(request()); - connect(reply, &QNetworkReply::finished, this, &OwnInfoJob::slotServerInfoFinished); - reply->setProperty("method", QVariant::fromValue(RestApiRequest::RestMethod::Me)); + connect(reply, &QNetworkReply::finished, this, &PrivateInfoJob::slotPrivateInfoDone); + + reply->setProperty("method", QVariant::fromValue(RestApiRequest::RestMethod::PrivateInfo)); return true; } -void OwnInfoJob::slotServerInfoFinished() +void PrivateInfoJob::slotPrivateInfoDone() { QNetworkReply *reply = qobject_cast(sender()); if (reply) { const QByteArray data = reply->readAll(); - Q_EMIT ownInfoDone(data); + Q_EMIT privateInfoDone(data); } deleteLater(); } -QNetworkRequest OwnInfoJob::request() const +bool PrivateInfoJob::requireHttpAuthentication() const +{ + return true; +} + +QNetworkRequest PrivateInfoJob::request() const { - const QUrl url = mRestApiMethod->generateUrl(RestApiUtil::RestApiUrlType::ServerInfo); - QNetworkRequest request(url); - request.setRawHeader(QByteArrayLiteral("X-Auth-Token"), mAuthToken.toLocal8Bit()); - request.setRawHeader(QByteArrayLiteral("X-User-Id"), mUserId.toLocal8Bit()); - request.setAttribute(QNetworkRequest::HttpPipeliningAllowedAttribute, true); - request.setAttribute(QNetworkRequest::HTTP2AllowedAttribute, true); - return request; + const QUrl url = mRestApiMethod->generateUrl(RestApiUtil::RestApiUrlType::Settings); + QNetworkRequest req(url); + addAuthRawHeader(req); + return req; } diff --git a/src/restapi/getavatarjob.h b/src/restapi/privateinfojob.h similarity index 74% copy from src/restapi/getavatarjob.h copy to src/restapi/privateinfojob.h index 6d06c06a..9cfac166 100644 --- a/src/restapi/getavatarjob.h +++ b/src/restapi/privateinfojob.h @@ -1,47 +1,47 @@ /* 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 GETAVATARJOB_H -#define GETAVATARJOB_H +#ifndef PRIVATEINFOJOB_H +#define PRIVATEINFOJOB_H -#include "libruqola_private_export.h" #include "restapiabstractjob.h" +#include "libruqola_private_export.h" + #include -class LIBRUQOLACORE_TESTS_EXPORT GetAvatarJob : public RestApiAbstractJob +class LIBRUQOLACORE_TESTS_EXPORT PrivateInfoJob : public RestApiAbstractJob { Q_OBJECT public: - explicit GetAvatarJob(QObject *parent = nullptr); - ~GetAvatarJob() override; + explicit PrivateInfoJob(QObject *parent = nullptr); + ~PrivateInfoJob() override; bool start() override; bool requireHttpAuthentication() const override; - - QString avatarUserId() const; - void setAvatarUserId(const QString &avatarUserId); - + QNetworkRequest request() const; +Q_SIGNALS: + void privateInfoDone(const QByteArray &data); private: - Q_DISABLE_COPY(GetAvatarJob) - QString mAvatarUserId; + Q_DISABLE_COPY(PrivateInfoJob) + void slotPrivateInfoDone(); }; -#endif // GETAVATARJOB_H +#endif // PRIVATEINFOJOB_H diff --git a/src/restapi/restapiabstractjob.cpp b/src/restapi/restapiabstractjob.cpp index bc2f2b2a..10ca8ea8 100644 --- a/src/restapi/restapiabstractjob.cpp +++ b/src/restapi/restapiabstractjob.cpp @@ -1,90 +1,98 @@ /* 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 "restapiabstractjob.h" #include "ruqola_restapi_debug.h" +#include + RestApiAbstractJob::RestApiAbstractJob(QObject *parent) : QObject(parent) { } QNetworkAccessManager *RestApiAbstractJob::networkAccessManager() const { return mNetworkAccessManager; } void RestApiAbstractJob::setNetworkAccessManager(QNetworkAccessManager *networkAccessManager) { mNetworkAccessManager = networkAccessManager; } RestApiMethod *RestApiAbstractJob::restApiMethod() const { return mRestApiMethod; } void RestApiAbstractJob::setRestApiMethod(RestApiMethod *restApiMethod) { mRestApiMethod = restApiMethod; } QString RestApiAbstractJob::authToken() const { return mAuthToken; } void RestApiAbstractJob::setAuthToken(const QString &authToken) { mAuthToken = authToken; } QString RestApiAbstractJob::userId() const { return mUserId; } void RestApiAbstractJob::setUserId(const QString &userId) { mUserId = userId; } bool RestApiAbstractJob::hasAuthenticationValue() const { return !mAuthToken.isEmpty() && !mUserId.isEmpty(); } bool RestApiAbstractJob::canStart() const { if (!mNetworkAccessManager) { qCWarning(RUQOLA_RESTAPI_LOG) << "Network manager not defined"; return false; } if (!mRestApiMethod) { qCWarning(RUQOLA_RESTAPI_LOG) << "RestaApiMethod not defined"; return false; } if (requireHttpAuthentication() && !hasAuthenticationValue()) { qCWarning(RUQOLA_RESTAPI_LOG) << "Auth settings is empty. It's a bug"; return false; } return true; } + +void RestApiAbstractJob::addAuthRawHeader(QNetworkRequest &request) const +{ + request.setRawHeader(QByteArrayLiteral("X-Auth-Token"), mAuthToken.toLocal8Bit()); + request.setRawHeader(QByteArrayLiteral("X-User-Id"), mUserId.toLocal8Bit()); +} diff --git a/src/restapi/restapiabstractjob.h b/src/restapi/restapiabstractjob.h index 15764696..2354eaf5 100644 --- a/src/restapi/restapiabstractjob.h +++ b/src/restapi/restapiabstractjob.h @@ -1,58 +1,60 @@ /* 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 RESTAPIABSTRACTJOB_H #define RESTAPIABSTRACTJOB_H #include #include "libruqola_private_export.h" class QNetworkAccessManager; +class QNetworkRequest; class RestApiMethod; class LIBRUQOLACORE_TESTS_EXPORT RestApiAbstractJob : public QObject { public: explicit RestApiAbstractJob(QObject *parent = nullptr); QNetworkAccessManager *networkAccessManager() const; void setNetworkAccessManager(QNetworkAccessManager *networkAccessManager); RestApiMethod *restApiMethod() const; void setRestApiMethod(RestApiMethod *restApiMethod); QString authToken() const; void setAuthToken(const QString &authToken); QString userId() const; void setUserId(const QString &userId); bool hasAuthenticationValue() const; virtual bool start() = 0; virtual bool requireHttpAuthentication() const = 0; protected: Q_DISABLE_COPY(RestApiAbstractJob) bool canStart() const; + void addAuthRawHeader(QNetworkRequest &request) const; QString mAuthToken; QString mUserId; QNetworkAccessManager *mNetworkAccessManager = nullptr; RestApiMethod *mRestApiMethod = nullptr; }; #endif // RESTAPIABSTRACTJOB_H diff --git a/src/restapi/restapimethod.cpp b/src/restapi/restapimethod.cpp index 12c43cac..f70b8ed7 100644 --- a/src/restapi/restapimethod.cpp +++ b/src/restapi/restapimethod.cpp @@ -1,49 +1,49 @@ /* 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 "restapimethod.h" -#include "ruqola_ddpapi_debug.h" +#include "ruqola_restapi_debug.h" RestApiMethod::RestApiMethod() { } QString RestApiMethod::serverUrl() const { return mServerUrl; } void RestApiMethod::setServerUrl(const QString &serverUrl) { mServerUrl = serverUrl; } QUrl RestApiMethod::generateUrl(RestApiUtil::RestApiUrlType type, const QString &urlExtension) { if (mServerUrl.isEmpty()) { - qCWarning(RUQOLA_DDPAPI_LOG) << "Server url is empty"; + qCWarning(RUQOLA_RESTAPI_LOG) << "Server url is empty"; return {}; } QString urlStr = RestApiUtil::adaptUrl(mServerUrl) + RestApiUtil::apiUri() + RestApiUtil::restUrl(type); if (!urlExtension.isEmpty()) { urlStr += QLatin1Char('/') + urlExtension; } return QUrl(urlStr); } diff --git a/src/restapi/restapirequest.cpp b/src/restapi/restapirequest.cpp index 27fc1bf7..cdbc330e 100644 --- a/src/restapi/restapirequest.cpp +++ b/src/restapi/restapirequest.cpp @@ -1,415 +1,383 @@ /* 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 "getavatarjob.h" +#include "logoutjob.h" +#include "privateinfojob.h" #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::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::parseLogin(const QByteArray &data) { const QJsonDocument replyJson = QJsonDocument::fromJson(data); const QJsonObject replyObject = replyJson.object(); if (replyObject[QStringLiteral("status")].toString() == QStringLiteral("success") && replyObject.contains(QLatin1String("data"))) { QJsonObject data = replyObject[QStringLiteral("data")].toObject(); if (data.contains(QLatin1String("authToken")) && data.contains(QLatin1String("userId"))) { mAuthToken = data[QStringLiteral("authToken")].toString(); mUserId = data[QStringLiteral("userId")].toString(); } } else { qCWarning(RUQOLA_RESTAPI_LOG) << "Error during login" << data; } } -void RestApiRequest::parseLogout(const QByteArray &data) -{ - const QJsonDocument replyJson = QJsonDocument::fromJson(data); - const QJsonObject replyObject = replyJson.object(); - - if (replyObject[QStringLiteral("status")].toString() == QStringLiteral("success")) { - qCDebug(RUQOLA_RESTAPI_LOG) << " Logout"; - mUserId.clear(); - mAuthToken.clear(); - } else { - qCWarning(RUQOLA_RESTAPI_LOG) <<" Problem when we try to logout"; - } - - qCDebug(RUQOLA_RESTAPI_LOG) << " void RestApiRequest::parseLogout(const QByteArray &data)" << data; -} - void RestApiRequest::parseChannelList(const QByteArray &data) { //qDebug() << " void RestApiRequest::parseChannelList(const QByteArray &data)" << data; const QJsonDocument replyJson = QJsonDocument::fromJson(data); const QJsonObject replyObject = replyJson.object(); //qDebug() << " replyObject"<error() == QNetworkReply::NoError) { //Exclude it until we port to new api const RestMethod restMethod = reply->property("method").value(); - if (restMethod == ServerInfo || restMethod == UploadFile || restMethod == Me) { + if (restMethod == ServerInfo || + restMethod == UploadFile || + restMethod == Me || + restMethod == GetAvatar || + restMethod == Logout || + restMethod == PrivateInfo) { return; } const QByteArray data = reply->readAll(); switch (restMethod) { case Login: parseLogin(data); break; - case Logout: - parseLogout(data); - break; case ChannelList: parseChannelList(data); break; case PrivateInfo: - parsePrivateInfo(data); - break; + case Logout: case GetAvatar: - parseGetAvatar(data, reply->property("userId").toString()); - break; case ServerInfo: - return; case Me: - return; case UploadFile: return; case Get: { const int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); if (status == 200) { bool storeInCache = true; QUrl localFile; QVariant var = reply->property("storeInCache"); if (var.isValid()) { storeInCache = var.toBool(); } var = reply->property("localFile"); if (var.isValid()) { localFile = var.toUrl(); } parseGet(data, reply->url(), storeInCache, localFile); } else { qCWarning(RUQOLA_RESTAPI_LOG) << "Unable to download " << reply->url(); } break; } case Post: parsePost(data); break; case Unknown: qCWarning(RUQOLA_RESTAPI_LOG) << " Unknown restapi method" << data; break; } } else { const RestMethod restMethod = reply->property("method").value(); qCWarning(RUQOLA_RESTAPI_LOG) << " Error reply - "<errorString() << " restMethod "<deleteLater(); } 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() { if (!mUserName.isEmpty() && !mPassword.isEmpty() && !serverUrl().isEmpty()) { QUrl url = mRestApiMethod->generateUrl(RestApiUtil::RestApiUrlType::Login); QNetworkRequest request(url); request.setHeader(QNetworkRequest::ContentTypeHeader, QStringLiteral("application/json")); QVariantMap loginMap; loginMap.insert(QStringLiteral("user"), mUserName); loginMap.insert(QStringLiteral("password"), mPassword); const QJsonDocument postData = QJsonDocument::fromVariant(loginMap); const QByteArray baPostData = postData.toJson(QJsonDocument::Compact); QNetworkReply *reply = mNetworkAccessManager->post(request, baPostData); reply->setProperty("method", QVariant::fromValue(RestMethod::Login)); } else { qCWarning(RUQOLA_RESTAPI_LOG) << "Password or user or url is empty"; } } +void RestApiRequest::slotLogout() +{ + mUserId.clear(); + mAuthToken.clear(); + Q_EMIT logoutDone(); +} + void RestApiRequest::logout() { - const QUrl url = mRestApiMethod->generateUrl(RestApiUtil::RestApiUrlType::Logout); - QNetworkRequest request(url); - request.setRawHeader(QByteArrayLiteral("X-Auth-Token"), mAuthToken.toLocal8Bit()); - request.setRawHeader(QByteArrayLiteral("X-User-Id"), mUserId.toLocal8Bit()); - QNetworkReply *reply = mNetworkAccessManager->get(request); - reply->setProperty("method", QVariant::fromValue(RestMethod::Logout)); + LogoutJob *job = new LogoutJob(this); + connect(job, &LogoutJob::logoutDone, this, &RestApiRequest::slotLogout); + job->setNetworkAccessManager(mNetworkAccessManager); + job->setRestApiMethod(mRestApiMethod); + job->setAuthToken(mAuthToken); + job->setUserId(mUserId); + job->start(); } void RestApiRequest::channelList() { if (mUserId.isEmpty() || mAuthToken.isEmpty()) { qCWarning(RUQOLA_RESTAPI_LOG) << "RestApiRequest::channelList problem with mUserId or mAuthToken"; } else { const QUrl url = mRestApiMethod->generateUrl(RestApiUtil::RestApiUrlType::ChannelsList); QNetworkRequest request(url); request.setRawHeader(QByteArrayLiteral("X-Auth-Token"), mAuthToken.toLocal8Bit()); request.setRawHeader(QByteArrayLiteral("X-User-Id"), mUserId.toLocal8Bit()); QNetworkReply *reply = mNetworkAccessManager->get(request); reply->setProperty("method", QVariant::fromValue(RestMethod::ChannelList)); } } void RestApiRequest::getAvatar(const QString &userId) { - if (mUserId.isEmpty() || mAuthToken.isEmpty()) { - qCWarning(RUQOLA_RESTAPI_LOG) << "RestApiRequest::getAvatar problem with mUserId or mAuthToken"; - } else { - QUrl url = mRestApiMethod->generateUrl(RestApiUtil::RestApiUrlType::UsersGetAvatar); - QUrlQuery queryUrl; - queryUrl.addQueryItem(QStringLiteral("userId"), userId); - url.setQuery(queryUrl); - QNetworkRequest request(url); - QNetworkReply *reply = mNetworkAccessManager->get(request); - reply->setProperty("method", QVariant::fromValue(RestMethod::GetAvatar)); - reply->setProperty("userId", userId); - } + GetAvatarJob *job = new GetAvatarJob(this); + connect(job, &GetAvatarJob::avatar, this, &RestApiRequest::avatar); + job->setNetworkAccessManager(mNetworkAccessManager); + job->setRestApiMethod(mRestApiMethod); + job->setAvatarUserId(userId); + job->start(); } void RestApiRequest::getPrivateSettings() { - if (mUserId.isEmpty() || mAuthToken.isEmpty()) { - qCWarning(RUQOLA_RESTAPI_LOG) << "RestApiRequest::getPrivateSettings problem with mUserId or mAuthToken"; - } else { - QUrl url = mRestApiMethod->generateUrl(RestApiUtil::RestApiUrlType::Settings); - QNetworkRequest request(url); - request.setRawHeader(QByteArrayLiteral("X-Auth-Token"), mAuthToken.toLocal8Bit()); - request.setRawHeader(QByteArrayLiteral("X-User-Id"), mUserId.toLocal8Bit()); - - QNetworkReply *reply = mNetworkAccessManager->get(request); - reply->setProperty("method", QVariant::fromValue(RestMethod::PrivateInfo)); - } + PrivateInfoJob *job = new PrivateInfoJob(this); + connect(job, &PrivateInfoJob::privateInfoDone, this, &RestApiRequest::privateInfoDone); + job->setNetworkAccessManager(mNetworkAccessManager); + job->setRestApiMethod(mRestApiMethod); + job->setAuthToken(mAuthToken); + job->setUserId(mUserId); + job->start(); } void RestApiRequest::getOwnInfo() { OwnInfoJob *job = new OwnInfoJob(this); connect(job, &OwnInfoJob::ownInfoDone, this, &RestApiRequest::getOwnInfoDone); job->setNetworkAccessManager(mNetworkAccessManager); job->setRestApiMethod(mRestApiMethod); job->setAuthToken(mAuthToken); job->setUserId(mUserId); job->start(); } void RestApiRequest::post(const QUrl &url, const QByteArray &data, const QString &mimeType) { QNetworkRequest request(url); request.setHeader(QNetworkRequest::ContentTypeHeader, mimeType); request.setRawHeader(QByteArrayLiteral("X-Auth-Token"), mAuthToken.toLocal8Bit()); request.setRawHeader(QByteArrayLiteral("X-User-Id"), mUserId.toLocal8Bit()); request.setAttribute(QNetworkRequest::HttpPipeliningAllowedAttribute, true); request.setAttribute(QNetworkRequest::HTTP2AllowedAttribute, true); QNetworkReply *reply = mNetworkAccessManager->post(request, data); reply->setProperty("method", QVariant::fromValue(RestMethod::Post)); } QNetworkReply *RestApiRequest::get(const QUrl &url, const QString &mimeType) { QNetworkRequest request(url); request.setRawHeader(QByteArrayLiteral("X-Auth-Token"), mAuthToken.toLocal8Bit()); request.setRawHeader(QByteArrayLiteral("X-User-Id"), mUserId.toLocal8Bit()); request.setHeader(QNetworkRequest::ContentTypeHeader, mimeType); request.setAttribute(QNetworkRequest::HttpPipeliningAllowedAttribute, true); request.setAttribute(QNetworkRequest::HTTP2AllowedAttribute, true); QNetworkReply *reply = mNetworkAccessManager->get(request); reply->setProperty("method", QVariant::fromValue(RestMethod::Get)); return reply; } void RestApiRequest::serverInfo() { ServerInfoJob *job = new ServerInfoJob(this); job->setNetworkAccessManager(mNetworkAccessManager); job->setRestApiMethod(mRestApiMethod); 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); job->setNetworkAccessManager(mNetworkAccessManager); job->setRestApiMethod(mRestApiMethod); job->setDescription(description); job->setMessageText(text); job->setFilenameUrl(filename); job->setRoomId(roomId); job->setAuthToken(mAuthToken); job->setUserId(mUserId); job->start(); } diff --git a/src/restapi/restapirequest.h b/src/restapi/restapirequest.h index b624fca9..a29a93ad 100644 --- a/src/restapi/restapirequest.h +++ b/src/restapi/restapirequest.h @@ -1,108 +1,107 @@ /* 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. */ #ifndef RESTAPIREQUEST_H #define RESTAPIREQUEST_H #include #include #include "restapiutil.h" class QNetworkAccessManager; class QNetworkReply; class QNetworkCookieJar; class RestApiMethod; class RestApiRequest : public QObject { Q_OBJECT public: enum RestMethod { Unknown = 0, Login, Logout, ChannelList, GetAvatar, Get, Post, ServerInfo, PrivateInfo, Me, UploadFile, }; Q_ENUM(RestMethod) explicit RestApiRequest(QObject *parent = nullptr); ~RestApiRequest(); QString userId() const; QString authToken() const; void setUserId(const QString &userId); void setAuthToken(const QString &authToken); //Assign/get server url QString serverUrl() const; void setServerUrl(const QString &serverUrl); void setUserName(const QString &userName); void setPassword(const QString &password); void login(); void logout(); void channelList(); void getAvatar(const QString &userId); void post(const QUrl &url, const QByteArray &data, const QString &mimeType = QStringLiteral("text/plain")); QNetworkReply *get(const QUrl &url, const QString &mimeType = QStringLiteral("text/plain")); void serverInfo(); void getPrivateSettings(); void getOwnInfo(); void uploadFile(const QString &roomId, const QString &description, const QString &text, const QUrl &filename); Q_SIGNALS: void avatar(const QString &userId, const QString &url); void logoutDone(); void loginDone(const QString &authToken, const QString &userId); void getDataDone(const QByteArray &data, const QUrl &url, bool useCache, const QUrl &localFileUrl); void getServerInfoDone(const QString &version); void getOwnInfoDone(const QByteArray &data); + void privateInfoDone(const QByteArray &data); private: Q_DISABLE_COPY(RestApiRequest) void initializeCookies(); void slotResult(QNetworkReply *reply); void slotSslErrors(QNetworkReply *reply, const QList &error); + void slotLogout(); - void parsePrivateInfo(const QByteArray &data); - void parseGetAvatar(const QByteArray &data, const QString &userId); - void parseLogout(const QByteArray &data); void parseLogin(const QByteArray &data); void parseChannelList(const QByteArray &data); void parseGet(const QByteArray &data, const QUrl &url, bool storeInCache, const QUrl &localFile); void parsePost(const QByteArray &data); QNetworkAccessManager *mNetworkAccessManager = nullptr; QNetworkCookieJar *mCookieJar = nullptr; RestApiMethod *mRestApiMethod = nullptr; QString mUserId; QString mAuthToken; QString mUserName; QString mPassword; }; #endif // RESTAPIREQUEST_H diff --git a/src/restapi/uploadfilejob.cpp b/src/restapi/uploadfilejob.cpp index 3f481f9e..c7575394 100644 --- a/src/restapi/uploadfilejob.cpp +++ b/src/restapi/uploadfilejob.cpp @@ -1,160 +1,159 @@ /* 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 "uploadfilejob.h" #include "ruqola_restapi_debug.h" #include "restapimethod.h" #include "restapirequest.h" #include #include #include #include #include #include #include UploadFileJob::UploadFileJob(QObject *parent) : RestApiAbstractJob(parent) { } UploadFileJob::~UploadFileJob() { } bool UploadFileJob::start() { if (!canStart()) { qCWarning(RUQOLA_RESTAPI_LOG) << "Impossible to start upload file job"; deleteLater(); return false; } QFile *file = new QFile(mFilenameUrl.path()); if (!file->open(QIODevice::ReadOnly)) { qCWarning(RUQOLA_RESTAPI_LOG) << " Impossible to open filename " << mFilenameUrl; delete file; deleteLater(); return false; } QMimeDatabase db; const QMimeType mimeType = db.mimeTypeForFile(mFilenameUrl.path()); QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType); QHttpPart filePart; filePart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant(mimeType.name())); const QString filePartInfo = QStringLiteral("form-data; name=\"file\"; filename=\"%1\"").arg(mFilenameUrl.fileName()); filePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant(filePartInfo)); filePart.setBodyDevice(file); file->setParent(multiPart); // we cannot delete the file now, so delete it with the multiPart multiPart->append(filePart); QHttpPart msgPart; msgPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant(QLatin1String("form-data; name=\"msg\""))); msgPart.setBody(mMessageText.toUtf8()); multiPart->append(msgPart); QHttpPart descriptionPart; descriptionPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant(QLatin1String("form-data; name=\"description\""))); descriptionPart.setBody(mDescription.toUtf8()); multiPart->append(descriptionPart); QNetworkReply *reply = mNetworkAccessManager->post(request(), multiPart); connect(reply, &QNetworkReply::finished, this, &UploadFileJob::slotUploadFinished); reply->setProperty("method", QVariant::fromValue(RestApiRequest::RestMethod::UploadFile)); multiPart->setParent(reply); // delete the multiPart with the reply //TODO upload progress ? //TODO signal error ? return true; } QNetworkRequest UploadFileJob::request() const { const QUrl url = mRestApiMethod->generateUrl(RestApiUtil::RestApiUrlType::RoomsUpload, mRoomId); QNetworkRequest request(url); - request.setRawHeader(QByteArrayLiteral("X-Auth-Token"), mAuthToken.toLocal8Bit()); - request.setRawHeader(QByteArrayLiteral("X-User-Id"), mUserId.toLocal8Bit()); + addAuthRawHeader(request); request.setAttribute(QNetworkRequest::HttpPipeliningAllowedAttribute, true); request.setAttribute(QNetworkRequest::HTTP2AllowedAttribute, true); return request; } void UploadFileJob::slotUploadFinished() { 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.value(QLatin1String("success")).toBool()) { qCWarning(RUQOLA_RESTAPI_LOG) << "RestApiRequest::parseUploadFile method had a problem " << replyJson; } } deleteLater(); } QString UploadFileJob::roomId() const { return mRoomId; } void UploadFileJob::setRoomId(const QString &roomId) { mRoomId = roomId; } QString UploadFileJob::description() const { return mDescription; } void UploadFileJob::setDescription(const QString &description) { mDescription = description; } QString UploadFileJob::messageText() const { return mMessageText; } void UploadFileJob::setMessageText(const QString &messageText) { mMessageText = messageText; } QUrl UploadFileJob::filenameUrl() const { return mFilenameUrl; } void UploadFileJob::setFilenameUrl(const QUrl &filenameUrl) { mFilenameUrl = filenameUrl; } bool UploadFileJob::requireHttpAuthentication() const { return true; }