aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/qlicensecore/status.cpp
blob: 039c83f32c8fdae0d4c95d1e9d9cdc8e2bdba3af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/* Copyright (C) 2024 The Qt Company Ltd.
 *
 * SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
*/

#include "status.h"

#include <map>

#define RESPONSE_CODE_SIZE 7

namespace QLicenseCore {

// String equivalents for reply codes
static const std::map<Status, std::string> sc_statusStringMap = {
    {Status::UNKNOWN_ERROR, "Unknown error"},
    {Status::SUCCESS, "Request successful"},
    {Status::LICENSE_REJECTED, "No valid license acquired"},
    {Status::LICENSE_POOL_FULL, "License pool fully reserved"},
    {Status::UNKNOWN_LICENSE_MODEL, "License model unknown"},
    {Status::UNKNOWN_RESERVATION_TYPE, "Reservation type unknown"},
    {Status::BAD_REQUEST, "Bad request"},
    {Status::INVALID_RESPONSE, "Server response is invalid"},
    {Status::BAD_CONNECTION, "No connection to server. Try again later"},
    {Status::UNAUTHORIZED, "Unauthorized. Please login with your Qt Account"},
    {Status::SERVER_ERROR, "Internal server error"},
    {Status::SERVER_BUSY, "Server is busy"},
    {Status::SERVICE_VERSION_TOO_LOW, "The client library is newer than the Qt License Service version"},
    {Status::SERVICE_VERSION_TOO_NEW, "The Qt License Service version is newer than the server version"},
    {Status::MISSING_SERVICE_VERSION, "Incorrect or missing service version in request"},
    {Status::NO_CLIENT_FOUND, "Client not found in cache"}
};

std::string statusString(Status status)
{
    auto it = sc_statusStringMap.find(status);
    if (it == sc_statusStringMap.end())
        return std::string();

    return it->second;
}


namespace ServerResponseCode {

static const std::map<std::string, Status> sc_responseInfoMap = {
    {"2000000", Status::SUCCESS}, // Request ok (default)
    {"2100001", Status::SUCCESS}, // Reservation successfully created
    {"2100011", Status::SUCCESS}, // Already reserved -> renew existing reservation
    {"2100002", Status::SUCCESS}, // Reservation successfully extended
    {"2100003", Status::SUCCESS}, // Reservation successfully released
    {"4001001", Status::BAD_REQUEST}, // Invalid payload
    {"4101001", Status::BAD_REQUEST}, // Bad input parameters
    {"4001002", Status::BAD_REQUEST}, // Username invalid
    {"4101002", Status::BAD_REQUEST}, // Username invalid
    {"4010000", Status::UNAUTHORIZED}, // Authentication failed
    {"4100000", Status::UNAUTHORIZED}, // Not authorized
    {"4100012", Status::LICENSE_POOL_FULL}, // License pool fully reserved
    {"4100010", Status::LICENSE_REJECTED}, // License not found
    {"4004000", Status::SERVICE_VERSION_TOO_NEW}, // Service version is newer than Server version
    {"4104000", Status::SERVICE_VERSION_TOO_NEW}, // Service version is newer than Server version
    {"4001005", Status::MISSING_SERVICE_VERSION}, // Incorrect or missing Service version in request body
    {"4101005", Status::MISSING_SERVICE_VERSION}, // Incorrect or missing Service version in request body
    {"5005002", Status::SERVER_BUSY}, // Server is busy
    {"9999999", Status::UNKNOWN_ERROR} // Unknown error
};

bool isSuccessCode(const std::string &code)
{
    if (code.length() != RESPONSE_CODE_SIZE)
        return false;

    return (code.rfind("2", 0) == 0);
}

Status codeToStatus(const std::string &code)
{
    if (code.length() != RESPONSE_CODE_SIZE)
        return Status::UNKNOWN_ERROR;

    auto it = sc_responseInfoMap.find(code);
    if (it == sc_responseInfoMap.end()) {
        if (isSuccessCode(code))
            return Status::SUCCESS;
        else
            return Status::UNKNOWN_ERROR;
    }

    return it->second;
}

} // namespace ServerResponseCode

} // namespace QLicenseCore