~aleteoryx/muditaos

muditaos/module-apps/application-desktop/widgets/DBNotificationsHandler.cpp -rw-r--r-- 2.6 KiB
a405cad6Aleteoryx trim readme 6 days ago
                                                                                
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
// Copyright (c) 2017-2024, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/blob/master/LICENSE.md

#include "DBNotificationsHandler.hpp"

#include <module-db/queries/messages/threads/QueryThreadsGetCount.hpp>
#include <module-db/queries/calllog/QueryCalllogGetCount.hpp>
#include <service-appmgr/Controller.hpp>
#include <service-db/DBNotificationMessage.hpp>

using namespace app;

namespace
{
    bool requestUnreadThreadsCount(app::ApplicationCommon *app)
    {
        const auto [succeed, _] = DBServiceAPI::GetQuery(
            app, db::Interface::Name::SMSThread, std::make_unique<db::query::ThreadGetCount>(EntryState::UNREAD));
        return succeed;
    }

    bool requestUnreadCallsCount(app::ApplicationCommon *app)
    {
        const auto [succeed, _] = DBServiceAPI::GetQuery(
            app, db::Interface::Name::Calllog, std::make_unique<db::query::CalllogGetCount>(EntryState::UNREAD));
        return succeed;
    }
} // namespace

DBNotificationsHandler::DBNotificationsHandler(ApplicationCommon *ownerApp)
    : ownerApp(ownerApp), notifications{{Type::notReadSMS, 0}, {Type::notReadCall, 0}}
{}

void DBNotificationsHandler::setNotification(Type type, int value)
{
    notifications[type] = value;
}

bool DBNotificationsHandler::hasNotification(Type type) const
{
    try {
        return notifications.at(type) > 0;
    }
    catch (const std::out_of_range &e) {
        LOG_ERROR("Uninitialized notification type");
    }
    return false;
}

bool DBNotificationsHandler::handle(db::NotificationMessage *msg)
{
    if (msg->type == db::Query::Type::Read) {
        return false;
    }

    if (auto msgInterface = msg->interface; msgInterface == db::Interface::Name::Calllog) {
        return requestUnreadCallsCount(ownerApp);
    }
    else if (msgInterface == db::Interface::Name::SMSThread || msgInterface == db::Interface::Name::SMS) {
        return requestUnreadThreadsCount(ownerApp);
    }
    return false;
}

bool DBNotificationsHandler::handle(db::QueryResult *msg)
{
    if (auto response = dynamic_cast<db::query::ThreadGetCountResult *>(msg);
        response != nullptr && response->getState() == EntryState::UNREAD) {
        notifications[Type::notReadSMS] = response->getCount();
        return true;
    }
    if (auto response = dynamic_cast<db::query::CalllogGetCountResult *>(msg);
        response != nullptr && response->getState() == EntryState::UNREAD) {
        notifications[Type::notReadCall] = response->getCount();
        return true;
    }
    return false;
}

void DBNotificationsHandler::initHandler()
{
    requestUnreadThreadsCount(ownerApp);
    requestUnreadCallsCount(ownerApp);
}