#include "mologram/ui/dialogs/CallPanel.hpp" #include #include "mologram/ui/dialogs/DialogUtil.hpp" namespace mologram::ui { using namespace ttgui; CallModel CallModel::fromJson(const js::Value& v) { CallModel c; c.id = js::getId(v, "id"); if (const js::Value* u = v.find("user")) c.user = Peer::fromJson(*u); c.outgoing = js::getBool(v, "outgoing"); c.video = js::getBool(v, "video"); c.needRating = js::getBool(v, "need_rating"); c.state = js::getStr(v, "state", "ended"); c.endReason = js::getStr(v, "end_reason"); c.media = js::getStr(v, "media", "signaling_only"); c.fingerprint = js::getId(v, "fingerprint"); c.duration = js::getInt(v, "duration"); return c; } std::vector CallPanel::buttonsFor(const CallModel& c) { if (c.ended()) { std::vector b; if (c.needRating) for (int i = 1; i <= 5; i++) b.push_back(std::to_string(i) + " star" + (i == 1 ? "" : "s")); b.push_back("Close"); return b; } if (c.incomingRinging()) return {"Accept", "Decline"}; return {"Hang up"}; } std::string CallPanel::statusText(const CallModel& c) { const char* kind = c.video ? "Video call" : "Call"; if (c.state == "requesting") return std::string(kind) + ": calling..."; if (c.state == "ringing") return c.outgoing ? std::string(kind) + ": ringing..." : std::string("Incoming ") + (c.video ? "video call" : "call"); if (c.state == "accepting") return std::string(kind) + ": accepting..."; if (c.state == "exchanging") return std::string(kind) + ": exchanging keys..."; if (c.state == "active") return std::string(kind) + ": keys verified, connected"; std::string s = std::string(kind) + " ended"; if (!c.endReason.empty()) s += " (" + c.endReason + ")"; if (c.duration > 0) s += ", " + std::to_string(c.duration) + " s"; return s; } std::string CallPanel::fingerprintText(const CallModel& c) { if (c.fingerprint == 0) return "Key fingerprint: not exchanged yet"; char buf[40]; unsigned long long f = (unsigned long long)c.fingerprint; std::snprintf(buf, sizeof(buf), "%04llX %04llX %04llX %04llX", (f >> 48) & 0xffff, (f >> 32) & 0xffff, (f >> 16) & 0xffff, f & 0xffff); return std::string("Key fingerprint: ") + buf + " (compare with the other person)"; } std::string CallPanel::notice(const CallModel& c) { if (c.media == "signaling_only") return "Audio is not available in this build. The call is set up and its keys are verified, but no voice or video is carried."; return ""; } Window* CallPanel::open(UiContext& c, const CallModel& call, const std::string& personName, const Actions& actions) { auto f = c.kit.open("Call", "CallPanel", 58, 14, false); Window* win = f.win; c.kit.label(f, personName); c.kit.label(f, statusText(call)); c.kit.label(f, fingerprintText(call)); c.kit.text(f, notice(call), 1); auto* row = c.kit.row(f); PushButton* focus = nullptr; for (const std::string& b : buttonsFor(call)) { std::function fn; if (b == "Accept") fn = actions.accept; else if (b == "Decline") fn = actions.decline; else if (b == "Hang up") fn = actions.hangup; else if (b == "Close") fn = actions.close; else if (actions.rate) { int stars = b[0] - '0'; auto rate = actions.rate; fn = [rate, stars]() { rate(stars); }; } auto* btn = c.kit.rowButton(row, b, [fn]() { if (fn) fn(); }); if (!focus) focus = btn; } c.kit.show(f, focus); return win; } } // namespace mologram::ui