2 * Copyright 2003-2018 The Music Player Daemon Project
3 * http://www.musicpd.org
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #ifndef QOBUZ_CLIENT_HXX
21 #define QOBUZ_CLIENT_HXX
24 #include "QobuzSession.hxx"
25 #include "QobuzLoginRequest.hxx"
26 #include "lib/curl/Init.hxx"
27 #include "thread/Mutex.hxx"
28 #include "event/DeferEvent.hxx"
30 #include <boost/intrusive/list.hpp>
36 class QobuzSessionHandler
37 : public boost::intrusive::list_base_hook
<boost::intrusive::link_mode
<boost::intrusive::safe_link
>>
40 virtual void OnQobuzSession() noexcept
= 0;
43 class QobuzClient final
: QobuzLoginHandler
{
44 const char *const base_url
;
45 const char *const app_id
, *const app_secret
;
46 const char *const device_manufacturer_id
;
47 const char *const username
, *const email
, *const password
;
48 const char *const format_id
;
52 DeferEvent defer_invoke_handlers
;
55 * Protects #session, #error, #login_request, #handlers.
61 std::exception_ptr error
;
63 typedef boost::intrusive::list
<QobuzSessionHandler
,
64 boost::intrusive::constant_time_size
<false>> LoginHandlerList
;
66 LoginHandlerList handlers
;
68 std::unique_ptr
<QobuzLoginRequest
> login_request
;
71 QobuzClient(EventLoop
&event_loop
,
72 const char *_base_url
,
73 const char *_app_id
, const char *_app_secret
,
74 const char *_device_manufacturer_id
,
75 const char *_username
, const char *_email
,
76 const char *_password
,
77 const char *_format_id
);
79 const char *GetFormatId() const noexcept
{
84 CurlGlobal
&GetCurl() noexcept
;
86 void AddLoginHandler(QobuzSessionHandler
&h
) noexcept
;
88 void RemoveLoginHandler(QobuzSessionHandler
&h
) noexcept
{
89 const std::lock_guard
<Mutex
> protect(mutex
);
91 handlers
.erase(handlers
.iterator_to(h
));
97 QobuzSession
GetSession() const;
99 std::string
MakeUrl(const char *object
, const char *method
,
100 const std::multimap
<std::string
, std::string
> &query
) const noexcept
;
102 std::string
MakeSignedUrl(const char *object
, const char *method
,
103 const std::multimap
<std::string
, std::string
> &query
) const noexcept
;
108 void InvokeHandlers() noexcept
;
110 void ScheduleInvokeHandlers() noexcept
{
111 defer_invoke_handlers
.Schedule();
114 /* virtual methods from QobuzLoginHandler */
115 void OnQobuzLoginSuccess(QobuzSession
&&session
) noexcept override
;
116 void OnQobuzLoginError(std::exception_ptr error
) noexcept override
;