Skip to content

Commit

Permalink
Merge pull request qmlbook#97 from qmlbook/fix-ch12-networking
Browse files Browse the repository at this point in the history
Fixed the oauth example (cmake configuration was clunky).
  • Loading branch information
e8johan authored Sep 29, 2021
2 parents 2f26a3c + babcd71 commit fa9bb97
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 68 deletions.
8 changes: 4 additions & 4 deletions docs/ch12-networking/authentication.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ import QtQuick
import QtQuick.Window
import QtQuick.Controls
import SpotifyOAuth
import Spotify
ApplicationWindow {
width: 320
Expand All @@ -79,10 +79,10 @@ ApplicationWindow {
}
```

When the application starts, we will first import a custom library, `SpotifyOAuth`, that defines a `SpotifyAPI` component (we'll come to that later).
When the application starts, we will first import a custom library, `Spotify`, that defines a `SpotifyAPI` component (we'll come to that later).

```qml
import SpotifyOAuth
import Spotify
```

Once the application has been loaded, this `SpotifyAPI` component will request an authorization to Spotify:
Expand Down Expand Up @@ -173,7 +173,7 @@ The next step happens when the authorization has been granted. To display the li
}
```

The model `SpotifyModel` is defined in the `SpotifyOAuth` library. To work properly, it needs a `SpotifyAPI`:
The model `SpotifyModel` is defined in the `Spotify` library. To work properly, it needs a `SpotifyAPI`:

```qml
SpotifyModel {
Expand Down
64 changes: 18 additions & 46 deletions docs/ch12-networking/src/oauth/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,59 +1,31 @@
cmake_minimum_required(VERSION 3.14)
cmake_minimum_required(VERSION 3.16.0)

project(SpotifyOAuth VERSION 0.1 LANGUAGES CXX)
project(SpotifyOAuth VERSION 1.0.0 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(QT NAMES Qt6 Qt5 COMPONENTS Core Quick QuickControls2 Network NetworkAuth REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core Quick QuickControls2 Network NetworkAuth REQUIRED)
find_package(Qt6 COMPONENTS Core Quick QuickControls2 Network NetworkAuth REQUIRED)

set(
PROJECT_SOURCES
main.cpp
qt_add_executable(SpotifyOAuth main.cpp)

qt_add_qml_module(SpotifyOAuth
VERSION 1.0.0
URI Spotify
QML_FILES
main.qml
SOURCES
spotifyapi.h spotifyapi.cpp
spotifymodel.h spotifymodel.cpp
qml.qrc
)

if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
qt_add_executable(SpotifyOAuth MANUAL_FINALIZATION ${PROJECT_SOURCES})
else()
add_executable(SpotifyOAuth ${PROJECT_SOURCES})
endif()

target_compile_definitions(
SpotifyOAuth
PRIVATE
$<$<OR:$<CONFIG:Debug>,$<CONFIG:RelWithDebInfo>>:QT_QML_DEBUG>
)

target_link_libraries(
SpotifyOAuth
PRIVATE
Qt${QT_VERSION_MAJOR}::Core
Qt${QT_VERSION_MAJOR}::Quick
Qt${QT_VERSION_MAJOR}::QuickControls2
Qt${QT_VERSION_MAJOR}::Network
Qt${QT_VERSION_MAJOR}::NetworkAuth
)

set_target_properties(
SpotifyOAuth
PROPERTIES
QT_QML_MODULE_URI SpotifyOAuth
QT_QML_MODULE_VERSION 1.0
MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com
MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
set_target_properties(SpotifyOAuth PROPERTIES
WIN32_EXECUTABLE TRUE
MACOSX_BUNDLE TRUE
)

if(QT_VERSION_MAJOR EQUAL 6)
qt6_qml_type_registration(SpotifyOAuth)
qt_import_qml_plugins(SpotifyOAuth)
qt_finalize_executable(SpotifyOAuth)
endif()
target_link_libraries(SpotifyOAuth PRIVATE Qt6::Core Qt6::Quick Qt6::QuickControls2 Qt6::Network Qt6::NetworkAuth)
15 changes: 5 additions & 10 deletions docs/ch12-networking/src/oauth/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,15 @@

#include "spotifyapi.h"

int main(int argc, char *argv[])
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#endif

int main(int argc, char *argv[]) {
QGuiApplication app(argc, argv);

QQmlApplicationEngine engine;
const QUrl url(QStringLiteral("qrc:/main.qml"));
QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
&app, [url](QObject *obj, const QUrl &objUrl) {
if (!obj && url == objUrl)
const QUrl url(QStringLiteral("qrc:/Spotify/main.qml"));
QObject::connect(&engine, &QQmlApplicationEngine::objectCreated, &app, [url](QObject *obj, const QUrl &objUrl) {
if (!obj && url == objUrl) {
QCoreApplication::exit(-1);
}
}, Qt::QueuedConnection);

engine.load(url);
Expand Down
4 changes: 2 additions & 2 deletions docs/ch12-networking/src/oauth/main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import QtQuick
import QtQuick.Window
import QtQuick.Controls

import SpotifyOAuth
import Spotify

ApplicationWindow {
width: 320
Expand Down Expand Up @@ -82,7 +82,7 @@ ApplicationWindow {
}

Component.onCompleted: {
spotifyApi.setCredentials("334044b3d4534ed48a3a1d5e90f9155b", "972084f85236422296004be85f87ca1e")
spotifyApi.setCredentials("CLIENT_ID", "CLIENT_SECRET")
spotifyApi.authorize()
}
}
5 changes: 0 additions & 5 deletions docs/ch12-networking/src/oauth/qml.qrc

This file was deleted.

2 changes: 1 addition & 1 deletion docs/ch12-networking/web-sockets.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ In Qt/QML you can also simply use the WebSocket and WebSocketServer objects to c
You can use the web socket qml module by importing it first.

```qml
import Qt.WebSockets 1.0
import QtWebSockets
WebSocket {
id: socket
Expand Down

0 comments on commit fa9bb97

Please sign in to comment.