Files
MuditaOS/module-apps/UiCommonActions.cpp
Marcin Smoczyński c47923d70a [EGD-3107] Call number validation
Use specialized class to to pass phone number between apps and services
instead of ambigious std::string. Introduced class (utils::PhoneNumber)
wraps calls to libphonenumber providing more convienient interface.
However using libphonenumber directly may be resource hungry in terms
of stack, heap and cpu usage, so helper class has been introduced to
pass information about phone numbers (utils::PhoneNumber::View). It is
designed to maintain integrity thus can be created from an instance of
utils::PhoneNumber only or moved/copied to.

Add new field to the calllog db holding e164 formatted number. Both
entered and e164 numbers will be needed to be able to match with
contacts correctly.

Add constexpr information about country codes (utils::country).
2020-05-05 00:51:53 +02:00

172 lines
6.5 KiB
C++

#include "UiCommonActions.hpp"
#include "application-call/ApplicationCall.hpp"
#include "application-call/data/CallSwitchData.hpp"
#include "application-messages/ApplicationMessages.hpp"
#include "application-messages/data/SMSdata.hpp"
#include "application-messages/windows/ThreadViewWindow.hpp"
#include "application-phonebook/ApplicationPhonebook.hpp"
#include "application-phonebook/data/PhonebookItemData.hpp"
#include "application-phonebook/windows/PhonebookContact.hpp"
#include "application-phonebook/windows/PhonebookDialogs.hpp"
#include "application-phonebook/windows/PhonebookNewContact.hpp"
#include "service-appmgr/ApplicationManager.hpp"
#include <i18/i18.hpp>
#include <log/log.hpp>
#include <PhoneNumber.hpp>
#include <cassert>
#include <string>
#include <utility>
#include <vector>
namespace app
{
bool call(Application *app, const ContactRecord &contact)
{
assert(app != nullptr);
if (contact.numbers.size() != 0) {
return call(app, std::string(contact.numbers[0].numberE164));
}
else {
LOG_ERROR("No contact numbers!");
return false;
}
}
bool call(Application *app, const utils::PhoneNumber::View &phoneNumber)
{
assert(app != nullptr);
auto data = std::make_unique<ExecuteCallData>(phoneNumber);
return sapm::ApplicationManager::messageSwitchApplication(
app, name_call, window::name_enterNumber, std::move(data));
}
bool call(Application *app, const std::string &e164number)
{
return call(app, utils::PhoneNumber::viewFromE164(e164number));
}
bool prepare_call(Application *app, const std::string &number)
{
assert(app != nullptr);
auto data = std::make_unique<EnterNumberData>(number);
return sapm::ApplicationManager::messageSwitchApplication(
app, name_call, window::name_enterNumber, std::move(data));
}
bool sms(Application *app, SmsOperation smsOperation, const ContactRecord &contact)
{
assert(app != nullptr);
// TODO return to current application doesn't change application window >_>
auto param = std::shared_ptr<ContactRecord>(new ContactRecord(contact));
switch (smsOperation) {
case SmsOperation::Add: {
return sapm::ApplicationManager::messageSwitchApplication(
app, name_messages, gui::name::window::thread_view, std::make_unique<SMSSendRequest>(param));
}
default: {
LOG_ERROR("SmsOperation not supported %" PRIu32, static_cast<uint32_t>(smsOperation));
return false;
}
}
}
bool sms(Application *app, SmsOperation smsOperation, const std::string &number)
{
assert(app != nullptr);
ContactRecord contactRec;
contactRec.numbers = std::vector<ContactRecord::Number>({ContactRecord::Number(number, number)});
return sms(app, smsOperation, contactRec);
}
bool contact(Application *app, ContactOperation contactOperation, const ContactRecord &contact)
{
assert(app != nullptr);
switch (contactOperation) {
case ContactOperation::Add: {
return sapm::ApplicationManager::messageSwitchApplication(
app,
name_phonebook,
gui::window::name::newContact,
std::make_unique<PhonebookItemData>(std::shared_ptr<ContactRecord>(new ContactRecord(contact))));
} break;
case ContactOperation::Details: {
return sapm::ApplicationManager::messageSwitchApplication(
app,
name_phonebook,
gui::window::name::contact,
std::make_unique<PhonebookItemData>(std::shared_ptr<ContactRecord>(new ContactRecord(contact))));
} break;
case ContactOperation::Edit: {
return sapm::ApplicationManager::messageSwitchApplication(
app,
name_phonebook,
gui::window::name::newContact, // TODO: need to be fixed when contact edition is working
std::make_unique<PhonebookItemData>(std::shared_ptr<ContactRecord>(new ContactRecord(contact))));
} break;
default: {
LOG_ERROR("ContactOperation not supported %" PRIu32, static_cast<uint32_t>(contactOperation));
return false;
}
}
}
bool contact(Application *app, ContactOperation contactOperation, const std::string &number)
{
assert(app != nullptr);
auto searchResults = DBServiceAPI::ContactSearch(app, "", "", number);
ContactRecord contactRec;
if (searchResults.get()->size() == 1) {
contactRec = searchResults->front();
LOG_INFO("Found contact matching search num %s : contact ID %" PRIu32 " - %s %s",
number.c_str(),
contactRec.ID,
contactRec.primaryName.c_str(),
contactRec.alternativeName.c_str());
if (contactOperation == ContactOperation::Add) {
// trying to add new contact for number already assigned to existing contact, display warning
return sapm::ApplicationManager::messageSwitchApplication(
app,
name_phonebook,
gui::window::name::duplicatedContact,
std::make_unique<PhonebookItemData>(std::shared_ptr<ContactRecord>(new ContactRecord(contactRec)),
number));
}
}
else if (searchResults.get()->size() > 1) {
LOG_FATAL("Found more than one contact for numer %s", number.c_str());
for (auto i : *searchResults) {
LOG_FATAL("ContactID = %" PRIu32, i.ID);
}
return false;
}
else if (contactOperation != ContactOperation::Add) {
LOG_ERROR("Invalid operation for not existing contact for numer %s", number.c_str());
return false;
}
return contact(app, contactOperation, contactRec);
}
bool contact(Application *app, ContactOperation contactOperation, uint32_t contactId)
{
assert(app != nullptr);
assert(contactOperation != ContactOperation::Add);
auto searchResults = DBServiceAPI::ContactGetByID(app, contactId);
if (searchResults.get()->size() == 1) {
ContactRecord contactRec = searchResults->front();
return contact(app, contactOperation, contactRec);
}
return false;
}
} // namespace app