CSpotContext.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #pragma once
  2. #include <stdint.h>
  3. #include <memory>
  4. #include "Crypto.h"
  5. #include "LoginBlob.h"
  6. #include "MercurySession.h"
  7. #include "TimeProvider.h"
  8. #include "protobuf/authentication.pb.h" // for AuthenticationType_AUTHE...
  9. #include "protobuf/metadata.pb.h"
  10. #ifdef BELL_ONLY_CJSON
  11. #include "cJSON.h"
  12. #else
  13. #include "nlohmann/detail/json_pointer.hpp" // for json_pointer<>::string_t
  14. #include "nlohmann/json.hpp" // for basic_json<>::object_t, basic_json
  15. #include "nlohmann/json_fwd.hpp" // for json
  16. #endif
  17. namespace cspot {
  18. struct Context {
  19. struct ConfigState {
  20. // Setup default bitrate to 160
  21. AudioFormat audioFormat = AudioFormat::AudioFormat_OGG_VORBIS_160;
  22. std::string deviceId;
  23. std::string deviceName;
  24. std::vector<uint8_t> authData;
  25. int volume;
  26. std::string username;
  27. std::string countryCode;
  28. };
  29. ConfigState config;
  30. std::shared_ptr<TimeProvider> timeProvider;
  31. std::shared_ptr<cspot::MercurySession> session;
  32. std::string getCredentialsJson() {
  33. #ifdef BELL_ONLY_CJSON
  34. cJSON* json_obj = cJSON_CreateObject();
  35. cJSON_AddStringToObject(json_obj, "authData",
  36. Crypto::base64Encode(config.authData).c_str());
  37. cJSON_AddNumberToObject(
  38. json_obj, "authType",
  39. AuthenticationType_AUTHENTICATION_STORED_SPOTIFY_CREDENTIALS);
  40. cJSON_AddStringToObject(json_obj, "username", config.username.c_str());
  41. char* str = cJSON_PrintUnformatted(json_obj);
  42. cJSON_Delete(json_obj);
  43. std::string json_objStr(str);
  44. free(str);
  45. return json_objStr;
  46. #else
  47. nlohmann::json obj;
  48. obj["authData"] = Crypto::base64Encode(config.authData);
  49. obj["authType"] =
  50. AuthenticationType_AUTHENTICATION_STORED_SPOTIFY_CREDENTIALS;
  51. obj["username"] = config.username;
  52. return obj.dump();
  53. #endif
  54. }
  55. static std::shared_ptr<Context> createFromBlob(
  56. std::shared_ptr<LoginBlob> blob) {
  57. auto ctx = std::make_shared<Context>();
  58. ctx->timeProvider = std::make_shared<TimeProvider>();
  59. ctx->session = std::make_shared<MercurySession>(ctx->timeProvider);
  60. ctx->config.deviceId = blob->getDeviceId();
  61. ctx->config.deviceName = blob->getDeviceName();
  62. ctx->config.authData = blob->authData;
  63. ctx->config.volume = 0;
  64. ctx->config.username = blob->getUserName();
  65. return ctx;
  66. }
  67. };
  68. } // namespace cspot