~aleteoryx/muditaos

ref: 028229a9177e91be0798edeac6a0f12fa27d96fc muditaos/module-apps/application-settings/models/network/SimContactsRepository.cpp -rw-r--r-- 5.9 KiB
028229a9 — Maciej-Mudita [MOS-924] Fix redundant logs about CSQ reporting mode 2 years 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright (c) 2017-2023, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "SimContactsRepository.hpp"

#include <queries/phonebook/QueryMergeContactsList.hpp>
#include <queries/phonebook/QueryCheckContactsListDuplicates.hpp>
#include <service-cellular/ServiceCellular.hpp>

#include <application-settings/ApplicationSettings.hpp>

SimContactsRepository::SimContactsRepository(app::ApplicationCommon *application)
    : app::AsyncCallbackReceiver{application}, application{application}
{}

const std::vector<ContactRecord> &SimContactsRepository::getImportedRecords()
{
    return importedRecords;
}

const std::vector<ContactRecord> &SimContactsRepository::getDuplicatedRecords()
{
    return duplicatedRecords;
}

void SimContactsRepository::clear()
{
    importedRecords.clear();
    uniqueRecords.clear();
    duplicatedRecords.clear();
}

void SimContactsRepository::findDuplicates(const std::vector<bool> &selectedContacts,
                                           AbstractSimContactsRepository::OnDupplicatesCheckCallback callback)
{
    std::vector<ContactRecord> recordsToSave;
    for (unsigned int i = 0; i < selectedContacts.size(); i++) {
        if (selectedContacts[i]) {
            recordsToSave.push_back(importedRecords[i]);
        }
    }

#if DEBUG_SIM_IMPORT_DATA == 1
    printRecordsData("Sim import selected", recordsToSave);
#endif

    auto query = std::make_unique<db::query::CheckContactsListDuplicates>(recordsToSave);
    auto task  = app::AsyncQuery::createFromQuery(std::move(query), db::Interface::Name::Contact);
    task->setCallback([&, callback](auto response) {
        auto result = dynamic_cast<db::query::CheckContactsListDuplicatesResult *>(response);
        if (result == nullptr) {
            return false;
        }

        auto duplicatesFound = !result->getDuplicates().empty();

        if (callback) {
            if (duplicatesFound) {
                uniqueRecords     = std::move(result->getUnique());
                duplicatedRecords = std::move(result->getDuplicates());

#if DEBUG_SIM_IMPORT_DATA == 1
                printRecordsData("Sim import uniques", uniqueRecords);
                printRecordsData("Sim import duplicates", duplicatedRecords);
#endif
            }
            callback(duplicatesFound);
        }
        return true;
    });
    task->execute(application, this);
}

void SimContactsRepository::save(const std::vector<bool> &selectedContacts,
                                 bool duplicatesFound,
                                 OnSaveCallback callback)
{
    std::vector<ContactRecord> recordsToSave = uniqueRecords;
    for (unsigned int i = 0; i < selectedContacts.size(); i++) {
        if (selectedContacts[i]) {
            recordsToSave.push_back(duplicatesFound ? duplicatedRecords[i] : importedRecords[i]);
        }
    }

#if DEBUG_SIM_IMPORT_DATA == 1
    printRecordsData("Sim import to save data", recordsToSave);
#endif

    auto query = std::make_unique<db::query::MergeContactsList>(recordsToSave);
    auto task  = app::AsyncQuery::createFromQuery(std::move(query), db::Interface::Name::Contact);
    task->setCallback([&, callback](auto response) {
        auto result = dynamic_cast<db::query::MergeContactsListResult *>(response);
        if (result == nullptr) {
            return false;
        }
        for (const auto &r : result->getResult()) {
            sendNotification(r);
        }
        if (callback) {
            callback();
        }
        return true;
    });

    task->execute(application, this);
}

void SimContactsRepository::read(AbstractSimContactsRepository::OnReadCallback readDoneCallback)
{
    std::function<void(const std::vector<cellular::SimContact> &simData)> callback =
        [&](const std::vector<cellular::SimContact> &simData) { updateImportedRecords(simData); };

    auto msg  = std::make_unique<cellular::GetSimContactsRequest>();
    auto task = app::AsyncRequest::createFromMessage(std::move(msg), cellular::service::name);
    auto cb   = [callback, readDoneCallback](auto response) {
        auto result = dynamic_cast<cellular::GetSimContactsResponse *>(response);
        if (result != nullptr && result->retCode == sys::ReturnCodes::Success) {
            callback(*result->getContacts());
        }
        readDoneCallback();
        return true;
    };
    task->execute(this->application, this, cb);
}

void SimContactsRepository::updateImportedRecords(const std::vector<cellular::SimContact> &simData)
{
    for (const auto &simRecord : simData) {
        ContactRecord rec = ContactRecord();

        auto description = simRecord.name;
        auto splitPos    = simRecord.name.find(' ');

        rec.primaryName = description.substr(0, splitPos);
        rec.alternativeName =
            splitPos != std::string::npos ? description.substr(rec.primaryName.length() + 1, description.length()) : "";
        rec.numbers.push_back(
            ContactRecord::Number(utils::PhoneNumber(simRecord.number, utils::country::Id::UNKNOWN).getView()));

        importedRecords.push_back(rec);
    }

#if DEBUG_SIM_IMPORT_DATA == 1
    printRecordsData("Imported records from sim", importedRecords);
#endif
}

void SimContactsRepository::sendNotification(const NotificationData &notificationData)
{
    auto notificationMessage = std::make_shared<db::NotificationMessage>(
        db::Interface::Name::Contact, notificationData.first, notificationData.second);
    application->bus.sendMulticast(notificationMessage, sys::BusChannel::ServiceDBNotifications);
}

#if DEBUG_SIM_IMPORT_DATA == 1
void SimContactsRepository::printRecordsData(const std::string &name, const std::vector<ContactRecord> &data)
{
    for (auto record : data) {
        LOG_SENSITIVE(LOGDEBUG,
                      "%s: %s %s, Number: %s",
                      name.c_str(),
                      record.primaryName.c_str(),
                      record.alternativeName.c_str(),
                      record.numbers.front().number.getFormatted().c_str());
    }
}
#endif