#include "ApplicationPhonebook.hpp"
#include "Dialog.hpp"
#include "messages/QueryMessage.hpp"
#include "models/PhonebookModel.hpp"
#include "windows/PhonebookContact.hpp"
#include "windows/PhonebookContactDetails.hpp"
#include "windows/PhonebookContactOptions.hpp"
#include "windows/PhonebookErrors.hpp"
#include "windows/PhonebookMainWindow.hpp"
#include "windows/PhonebookNewContact.hpp"
#include "windows/PhonebookNamecardOptions.hpp"
#include "windows/PhonebookSearch.hpp"
#include "windows/PhonebookSearchResults.hpp"
#include <service-appmgr/ApplicationManager.hpp>
namespace app
{
ApplicationPhonebook::ApplicationPhonebook(std::string name, std::string parent, bool startBackgound)
: Application(name, parent, startBackgound, phonebook_stack_size)
{}
// Invoked upon receiving data message
auto ApplicationPhonebook::DataReceivedHandler(sys::DataMessage *msgl, sys::ResponseMessage *resp) -> sys::Message_t
{
auto retMsg = Application::DataReceivedHandler(msgl);
// if message was handled by application's template there is no need to process further.
if (reinterpret_cast<sys::ResponseMessage *>(retMsg.get())->retCode == sys::ReturnCodes::Success) {
return retMsg;
}
// this variable defines whether message was processed.
bool handled = false;
// handle database response
if (resp != nullptr) {
handled = true;
switch (resp->responseTo) {
case MessageType::DBQuery: {
auto queryResponse = dynamic_cast<db::QueryResponse *>(resp);
assert(queryResponse);
if (queryResponse->getResult()->handle()) {
refreshWindow(gui::RefreshModes::GUI_REFRESH_FAST);
}
} break;
default:
break;
}
}
if (handled) {
return std::make_shared<sys::ResponseMessage>();
}
else {
return std::make_shared<sys::ResponseMessage>(sys::ReturnCodes::Unresolved);
}
}
// Invoked during initialization
auto ApplicationPhonebook::InitHandler() -> sys::ReturnCodes
{
auto ret = Application::InitHandler();
if (ret != sys::ReturnCodes::Success) {
return ret;
}
createUserInterface();
return ret;
}
auto ApplicationPhonebook::DeinitHandler() -> sys::ReturnCodes
{
return sys::ReturnCodes::Success;
}
void ApplicationPhonebook::createUserInterface()
{
windows.insert({gui::name::window::main_window, new gui::PhonebookMainWindow(this)});
windows.insert({gui::window::name::new_contact, new gui::PhonebookNewContact(this)});
windows.insert({gui::window::name::contact, new gui::PhonebookContact(this)});
// windows.insert({gui::window::name::contact, new gui::PhonebookContactDetails(this)});
windows.insert({gui::window::name::search, new gui::PhonebookSearch(this)});
windows.insert({gui::window::name::no_results, new gui::NoResults(this)});
windows.insert({gui::window::name::contact_blocked, new gui::ContactBlocked(this)});
windows.insert({gui::window::name::search_results, new gui::PhonebookSearchResults(this)});
windows.insert(
{gui::window::name::dialog_yes_no, new gui::DialogYesNo(this, gui::window::name::dialog_yes_no)});
windows.insert(
{gui::window::name::dialog_confirm, new gui::DialogConfirm(this, gui::window::name::dialog_confirm)});
windows.insert({gui::window::name::contact_options, new gui::PhonebookContactOptions(this)});
windows.insert({gui::window::name::namecard_options, new gui::PhonebookNamecardOptions(this)});
}
void ApplicationPhonebook::destroyUserInterface()
{}
void ApplicationPhonebook::onSearchRequest(const std::string &searchFilter)
{
auto searchModel = std::make_unique<PhonebookModel>(this, searchFilter);
LOG_DEBUG("Search results count: %d", searchModel->getItemCount());
if (searchModel->getItemCount() > 0) {
auto main_window = dynamic_cast<gui::PhonebookMainWindow *>(windows[gui::name::window::main_window]);
if (main_window == nullptr) {
LOG_ERROR("Failed to get main window.");
return;
}
if (main_window->isSearchRequested()) {
searchModel->messagesSelectCallback = [=](gui::PhonebookItem *item) {
std::unique_ptr<PhonebookSearchReuqest> data = std::make_unique<PhonebookSearchReuqest>();
data->result = item->contact;
data->setDescription("PhonebookSearchRequest");
return sapm::ApplicationManager::messageSwitchPreviousApplication(
this, std::make_unique<sapm::APMSwitchPrevApp>(GetName(), std::move(data)));
};
}
LOG_DEBUG("Switching to search results window.");
auto data = std::make_unique<PhonebookSearchResultsData>(std::move(searchModel));
switchWindow("SearchResults", gui::ShowMode::GUI_SHOW_INIT, std::move(data));
}
else {
LOG_DEBUG("Switching to no results window.");
std::unique_ptr<gui::SwitchData> data = std::make_unique<PhonebookSearchQuery>(searchFilter);
switchWindow("NoResults", gui::ShowMode::GUI_SHOW_INIT, std::move(data));
}
}
} /* namespace app */