~aleteoryx/muditaos

ref: 885fe1083abcf3a39e12d75b6ba0b179b36de0ee muditaos/module-apps/application-phonebook/ApplicationPhonebook.cpp -rw-r--r-- 5.5 KiB
885fe108 — Marcin Smoczyński db: improve query handling 5 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
#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 */