~aleteoryx/muditaos

ref: 88a9fdedf22428cbfe52f91c8352eae52438e7f1 muditaos/module-apps/application-desktop/windows/Update.cpp -rw-r--r-- 11.6 KiB
88a9fded — Kuba [EGD-6883] Change time settings cleanup 4 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include <log.hpp>
#include "application-desktop/data/DesktopData.hpp"
#include <source/version.hpp>

// module-utils
#include "i18n/i18n.hpp"

#include "Update.hpp"
#include "../ApplicationDesktop.hpp"
// services
#include <service-appmgr/model/ApplicationManager.hpp>

#include "service-cellular/ServiceCellular.hpp"
#include <service-desktop/service-desktop/Constants.hpp>
#include <Style.hpp>
#include <boot/bootconfig.hpp>

namespace gui
{

    UpdateWindow::UpdateWindow(app::Application *app) : AppWindow(app, app::window::name::desktop_update)
    {
        buildInterface();
    }

    void UpdateWindow::rebuild()
    {
        // find which widget has focus
        uint32_t index = 0;
        for (uint32_t i = 0; i < selectionLabels.size(); i++)
            if (selectionLabels[i] == getFocusItem()) {
                index = i;
                break;
            }

        destroyInterface();
        buildInterface();
        setFocusItem(selectionLabels[index]);
    }
    void UpdateWindow::buildInterface()
    {
        AppWindow::buildInterface();
        bottomBar->setActive(BottomBar::Side::CENTER, true);
        bottomBar->setActive(BottomBar::Side::RIGHT, true);
        bottomBar->setText(BottomBar::Side::CENTER, utils::translate(style::strings::common::confirm));
        bottomBar->setText(BottomBar::Side::RIGHT, utils::translate(style::strings::common::back));

        // title label
        titleLabel = new gui::Label(this, 0, 60, 480, 40);
        titleLabel->setFilled(false);
        titleLabel->setBorderColor(gui::ColorFullBlack);
        titleLabel->setFont(style::header::font::title);
        titleLabel->setEdges(RectangleEdge::None);
        titleLabel->setAlignment(gui::Alignment(gui::Alignment::Horizontal::Center, gui::Alignment::Vertical::Top));
        titleLabel->setText(utils::translate("app_desktop_update"));

        // Update version information
        updateVersionInfo = new gui::Label(this, 10, 132, 480, 40);
        updateVersionInfo->setFilled(false);
        updateVersionInfo->setBorderColor(gui::ColorFullBlack);
        updateVersionInfo->setFont(style::window::font::smallbold);
        updateVersionInfo->setEdges(RectangleEdge::None);
        updateVersionInfo->setAlignment(
            gui::Alignment(gui::Alignment::Horizontal::Left, gui::Alignment::Vertical::Top));
        updateVersionInfo->setText(utils::translate("app_desktop_update"));

        // Update details
        updateDetails = new gui::Label(this, 40, 172, 440, 40);
        updateDetails->setFilled(false);
        updateDetails->setBorderColor(gui::ColorFullBlack);
        updateDetails->setFont(style::window::font::verysmall);
        updateDetails->setEdges(RectangleEdge::None);
        updateDetails->setAlignment(gui::Alignment(gui::Alignment::Horizontal::Left, gui::Alignment::Vertical::Top));
        updateDetails->setText(utils::translate("app_desktop_update"));

        // Current version information
        currentVersionInfo = new gui::Label(this, 10, 222, 480, 40);
        currentVersionInfo->setFilled(false);
        currentVersionInfo->setBorderColor(gui::ColorFullBlack);
        currentVersionInfo->setFont(style::window::font::small);
        currentVersionInfo->setEdges(RectangleEdge::None);
        currentVersionInfo->setAlignment(
            gui::Alignment(gui::Alignment::Horizontal::Left, gui::Alignment::Vertical::Top));
        currentVersionInfo->setText(utils::translate("app_desktop_update_current"));

        // Label Info
        infoLabel = new gui::Label(this, 20, 304, 440, 40);
        infoLabel->setFilled(false);
        infoLabel->setBorderColor(gui::ColorNoColor);
        infoLabel->setFont(style::window::font::medium);
        infoLabel->setAlignment(gui::Alignment(gui::Alignment::Horizontal::Center, gui::Alignment::Vertical::Bottom));
        infoLabel->setText(utils::translate("app_desktop_update_apply"));

        // Details during update
        detailLabel = new gui::Label(this, 20, 354, 440, 20);
        detailLabel->setFilled(false);
        detailLabel->setBorderColor(gui::ColorNoColor);
        detailLabel->setFont(style::window::font::small);
        detailLabel->setAlignment(gui::Alignment(gui::Alignment::Horizontal::Right, gui::Alignment::Vertical::Bottom));

        // update progress
        percentLabel = new gui::Label(this, 0, 374, 520, 128);
        percentLabel->setFilled(false);
        percentLabel->setBorderColor(gui::ColorNoColor);
        percentLabel->setFont(style::window::font::largelight);
        percentLabel->setAlignment(
            gui::Alignment(gui::Alignment::Horizontal::Center, gui::Alignment::Vertical::Bottom));
        percentLabel->setVisible(false);

        uint32_t pinLabelX = 46;
        uint32_t pinLabelY = 410;
        for (uint32_t i = 0; i < 4; i++) {
            gui::Label *label = new gui::Label(this, pinLabelX, pinLabelY, 193, 75);
            label->setFilled(false);
            label->setBorderColor(gui::ColorFullBlack);
            label->setPenWidth(0);
            label->setPenFocusWidth(2);
            label->setRadius(5);
            label->setFont(style::window::font::medium);
            label->setEdges(RectangleEdge::All);
            label->setAlignment(gui::Alignment(gui::Alignment::Horizontal::Center, gui::Alignment::Vertical::Center));
            selectionLabels.push_back(label);
            pinLabelX += 193;
        }
        selectionLabels[0]->setText(utils::translate(style::strings::common::no));
        selectionLabels[1]->setText(utils::translate(style::strings::common::yes));

        // define navigation between labels
        selectionLabels[0]->setNavigationItem(NavigationDirection::LEFT, selectionLabels[1]);
        selectionLabels[0]->setNavigationItem(NavigationDirection::RIGHT, selectionLabels[1]);

        selectionLabels[1]->setNavigationItem(NavigationDirection::LEFT, selectionLabels[0]);
        selectionLabels[1]->setNavigationItem(NavigationDirection::RIGHT, selectionLabels[0]);

        // callbacks for getting focus
        selectionLabels[0]->focusChangedCallback = [=](gui::Item &item) {
            if (item.focus)
                this->state = State::Return;
            return true;
        };

        selectionLabels[1]->focusChangedCallback = [=](gui::Item &item) {
            if (item.focus)
                this->state = State::UpdateNow;
            return true;
        };
    }
    void UpdateWindow::destroyInterface()
    {
        erase();
    }

    void UpdateWindow::onBeforeShow(ShowMode mode, SwitchData *data)
    {
        if (data == nullptr) {
            LOG_ERROR("Received null pointer");
        }
        else {
            gui::UpdateSwitchData *item = dynamic_cast<gui::UpdateSwitchData *>(data);
            if (item != nullptr) {
                std::stringstream currentVersion;
                std::stringstream updateVersion;
                std::stringstream updateFileDetails;

                sdesktop::UpdateOsMessage msg = item->getUpdateOsMessage();
                updateFile                    = msg.updateStats.updateFile;
                currentVersion << "Current: ";
                currentVersion << VERSION;
                currentVersion << " (";
                currentVersion << GIT_REV;
                currentVersion << ")";

                updateVersion << utils::translate("app_desktop_update_to");
                updateVersion << ": ";
                updateVersion << msg.updateStats.versionInformation[boot::json::os_version][boot::json::version_string]
                                     .string_value();
                updateVersion << " (";
                updateVersion << msg.updateStats.versionInformation[boot::json::git_info][boot::json::os_git_revision]
                                     .string_value();
                updateVersion << ")";

                updateFileDetails << utils::translate("app_desktop_update_size");
                updateFileDetails << ": ";
                updateFileDetails << std::to_string(msg.updateStats.totalBytes / 1024);
                updateFileDetails << "Kb (";
                updateFileDetails
                    << msg.updateStats.versionInformation[boot::json::misc][boot::json::builddate].string_value();
                updateFileDetails << ")";

                currentVersionInfo->setText(currentVersion.str());
                updateVersionInfo->setText(updateVersion.str());
                updateDetails->setText(updateFileDetails.str());
            }
        }

        state = State::Return;
        setFocusItem(selectionLabels[0]);
    }

    bool UpdateWindow::onInput(const InputEvent &inputEvent)
    {
        // check if any of the lower inheritance onInput methods catch the event
        if (AppWindow::onInput(inputEvent)) {
            return true;
        }

        // process only short press, consume rest
        if (!inputEvent.isShortRelease())
            return true;

        // if enter was pressed check state and power down or return to main desktop's window
        if (inputEvent.is(KeyCode::KEY_ENTER)) {
            if (state == State::Return) {
                application->switchWindow(app::window::name::desktop_main_window);
            }
            if (state == State::UpdateNow) {
                bottomBar->setActive(BottomBar::Side::CENTER, false);
                bottomBar->setActive(BottomBar::Side::RIGHT, false);

                selectionLabels[0]->setVisible(false);
                selectionLabels[1]->setVisible(false);

                percentLabel->setVisible(true);
                percentLabel->setText(utils::translate("app_desktop_update_start"));
                auto msgToSend = std::make_shared<sdesktop::UpdateOsMessage>(updateFile.c_str(), 0);
                application->bus.sendUnicast(msgToSend, service::name::service_desktop);

                return true;
            }
        }

        return false;
    }

    bool UpdateWindow::handleSwitchData(SwitchData *data)
    {
        gui::UpdateSwitchData *item = dynamic_cast<gui::UpdateSwitchData *>(data);
        if (item != nullptr) {
            std::stringstream ssi;
            std::stringstream sizeStream;

            updateos::UpdateState status =
                static_cast<updateos::UpdateState>(item->getUpdateOsMessage().updateStats.status);

            if (status == updateos::UpdateState::ExtractingFiles) {
                progressPercent =
                    static_cast<int>((static_cast<float>(item->getUpdateOsMessage().updateStats.currentExtractedBytes) /
                                      static_cast<float>(item->getUpdateOsMessage().updateStats.totalBytes)) *
                                     100.0);
                ssi << utils::translate("app_desktop_update_unpacking");
                ssi << ": ";
                ssi << std::to_string(progressPercent);
                ssi << " %";
                percentLabel->setText(ssi.str());
                infoLabel->setText(item->getUpdateOsMessage().updateStats.messageText);
            }
            else if (item->getUpdateOsMessage().updateStats.messageText != "") {
                percentLabel->setText(item->getUpdateOsMessage().updateStats.messageText);
            }

            sizeStream << utils::translate("app_desktop_update_size");
            sizeStream << ": ";
            sizeStream << std::to_string(item->getUpdateOsMessage().updateStats.fileExtractedSize);
            sizeStream << " ";
            sizeStream << utils::translate("app_desktop_update_bytes");
            detailLabel->setText(sizeStream.str());
            this->application->refreshWindow(gui::RefreshModes::GUI_REFRESH_FAST);
        }

        return true;
    }
} /* namespace gui */