interaction.h 5.84 KB
Newer Older
Nicolas Jager's avatar
Nicolas Jager committed
1
/****************************************************************************
Guillaume Roguez's avatar
Guillaume Roguez committed
2
 *   Copyright (C) 2017-2018 Savoir-faire Linux                                  *
Nicolas Jager's avatar
Nicolas Jager committed
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
 *   Author: Nicolas Jäger <nicolas.jager@savoirfairelinux.com>             *
 *   Author: Sébastien Blin <sebastien.blin@savoirfairelinux.com>           *
 *                                                                          *
 *   This library is free software; you can redistribute it and/or          *
 *   modify it under the terms of the GNU Lesser General Public             *
 *   License as published by the Free Software Foundation; either           *
 *   version 2.1 of the License, or (at your option) 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      *
 *   Lesser General Public License for more details.                        *
 *                                                                          *
 *   You should have received a copy of the GNU General Public License      *
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.  *
 ***************************************************************************/
#pragma once

// Std
#include <ctime>
#include <string>

namespace lrc
{

namespace api
{

namespace interaction
{

enum class Type {
    INVALID,
    TEXT,
    CALL,
Nicolas Jager's avatar
Nicolas Jager committed
38 39 40
    CONTACT,
    OUTGOING_DATA_TRANSFER,
    INCOMING_DATA_TRANSFER
Nicolas Jager's avatar
Nicolas Jager committed
41 42
};

43
static inline const std::string
Nicolas Jager's avatar
Nicolas Jager committed
44 45 46 47 48 49 50 51 52
to_string(const Type& type)
{
    switch(type) {
    case Type::TEXT:
        return "TEXT";
    case Type::CALL:
        return "CALL";
    case Type::CONTACT:
        return "CONTACT";
Nicolas Jager's avatar
Nicolas Jager committed
53 54 55 56
    case Type::OUTGOING_DATA_TRANSFER:
        return "OUTGOING_DATA_TRANSFER";
    case Type::INCOMING_DATA_TRANSFER:
        return "INCOMING_DATA_TRANSFER";
Nicolas Jager's avatar
Nicolas Jager committed
57 58 59 60 61 62
    case Type::INVALID:
    default:
        return "INVALID";
    }
}

63
static inline Type
Nicolas Jager's avatar
Nicolas Jager committed
64 65 66 67 68 69 70 71
to_type(const std::string& type)
{
    if (type == "TEXT")
        return interaction::Type::TEXT;
    else if (type == "CALL")
        return interaction::Type::CALL;
    else if (type == "CONTACT")
        return interaction::Type::CONTACT;
Nicolas Jager's avatar
Nicolas Jager committed
72 73 74 75
    else if (type == "OUTGOING_DATA_TRANSFER")
        return interaction::Type::OUTGOING_DATA_TRANSFER;
    else if (type == "INCOMING_DATA_TRANSFER")
        return interaction::Type::INCOMING_DATA_TRANSFER;
Nicolas Jager's avatar
Nicolas Jager committed
76 77 78 79 80 81 82
    else
        return interaction::Type::INVALID;
}


enum class Status {
    INVALID,
83
    UNKNOWN,
Nicolas Jager's avatar
Nicolas Jager committed
84 85 86 87
    SENDING,
    FAILED,
    SUCCEED,
    READ,
Nicolas Jager's avatar
Nicolas Jager committed
88 89 90 91 92
    UNREAD,
    TRANSFER_CREATED, /*[jn] mettre à jour les fonctions de conversion */
    TRANSFER_ACCEPTED,
    TRANSFER_CANCELED,
    TRANSFER_ERROR,
93
    TRANSFER_UNJOINABLE_PEER,
Nicolas Jager's avatar
Nicolas Jager committed
94 95 96
    TRANSFER_ONGOING,
    TRANSFER_AWAITING,
    TRANSFER_FINISHED
Nicolas Jager's avatar
Nicolas Jager committed
97 98
};

99
static inline const std::string
Nicolas Jager's avatar
Nicolas Jager committed
100 101 102
to_string(const Status& status)
{
    switch(status) {
103 104
    case Status::UNKNOWN:
        return "UNKNOWN";
Nicolas Jager's avatar
Nicolas Jager committed
105 106 107 108 109 110 111 112 113 114
    case Status::SENDING:
        return "SENDING";
    case Status::FAILED:
        return "FAILED";
    case Status::SUCCEED:
        return "SUCCEED";
    case Status::READ:
        return "READ";
    case Status::UNREAD:
        return "UNREAD";
Nicolas Jager's avatar
Nicolas Jager committed
115 116 117 118 119 120 121 122
    case Status::TRANSFER_CREATED:
        return "TRANSFER_CREATED";
    case Status::TRANSFER_ACCEPTED:
        return "TRANSFER_ACCEPTED";
    case Status::TRANSFER_CANCELED:
        return "TRANSFER_CANCELED";
    case Status::TRANSFER_ERROR:
        return "TRANSFER_ERROR";
123 124
    case Status::TRANSFER_UNJOINABLE_PEER:
        return "TRANSFER_UNJOINABLE_PEER";
Nicolas Jager's avatar
Nicolas Jager committed
125 126 127 128 129 130
    case Status::TRANSFER_ONGOING:
        return "TRANSFER_ONGOING";
    case Status::TRANSFER_AWAITING:
        return "TRANSFER_AWAITING";
    case Status::TRANSFER_FINISHED:
        return "TRANSFER_FINISHED";
Anthony Léonard's avatar
Anthony Léonard committed
131
    case Status::INVALID:
Nicolas Jager's avatar
Nicolas Jager committed
132 133 134 135 136
    default:
        return "INVALID";
    }
}

137
static inline Status
Nicolas Jager's avatar
Nicolas Jager committed
138 139
to_status(const std::string& status)
{
140 141 142
    if (status == "UNKNOWN")
        return interaction::Status::UNKNOWN;
    else if (status == "SENDING")
Nicolas Jager's avatar
Nicolas Jager committed
143 144 145 146 147 148 149 150 151
        return interaction::Status::SENDING;
    else if (status == "FAILED")
        return interaction::Status::FAILED;
    else if (status == "SUCCEED")
        return interaction::Status::SUCCEED;
    else if (status == "READ")
        return interaction::Status::READ;
    else if (status == "UNREAD")
        return interaction::Status::UNREAD;
Nicolas Jager's avatar
Nicolas Jager committed
152 153 154 155 156 157 158 159
    else if (status == "TRANSFER_CREATED")
        return interaction::Status::TRANSFER_CREATED;
    else if (status == "TRANSFER_ACCEPTED")
        return interaction::Status::TRANSFER_ACCEPTED;
    else if (status == "TRANSFER_CANCELED")
        return interaction::Status::TRANSFER_CANCELED;
    else if (status == "TRANSFER_ERROR")
        return interaction::Status::TRANSFER_ERROR;
160 161
    else if (status == "TRANSFER_UNJOINABLE_PEER")
        return interaction::Status::TRANSFER_UNJOINABLE_PEER;
Nicolas Jager's avatar
Nicolas Jager committed
162 163 164 165 166 167
    else if (status == "TRANSFER_ONGOING")
        return interaction::Status::TRANSFER_ONGOING;
    else if (status == "TRANSFER_AWAITING")
        return interaction::Status::TRANSFER_AWAITING;
    else if (status == "TRANSFER_FINISHED")
        return interaction::Status::TRANSFER_FINISHED;
Nicolas Jager's avatar
Nicolas Jager committed
168 169 170 171 172 173 174 175 176 177 178 179 180 181
    else
        return interaction::Status::INVALID;

}

struct Info
{
    std::string authorUri;
    std::string body;
    std::time_t timestamp = 0;
    Type type = Type::INVALID;
    Status status = Status::INVALID;
};

182
static inline bool isOutgoing(const Info& interaction) {
Nicolas Jager's avatar
Nicolas Jager committed
183
    return (interaction.status != lrc::api::interaction::Status::READ
184 185
    && interaction.status != lrc::api::interaction::Status::UNREAD
    && interaction.type != lrc::api::interaction::Type::INCOMING_DATA_TRANSFER)
Nicolas Jager's avatar
Nicolas Jager committed
186
    || interaction.type == lrc::api::interaction::Type::OUTGOING_DATA_TRANSFER;
Nicolas Jager's avatar
Nicolas Jager committed
187 188 189 190 191
}

} // namespace interaction
} // namespace api
} // namespace lrc