libaurum: apply smart pointer wider and extract impl out
[platform/core/uifw/aurum.git] / org.tizen.aurum-bootstrap / src / Commands / GetDeviceTimeCommand.cc
1 #include "GetDeviceTimeCommand.h"
2
3 #include <system_settings.h>
4 #include <utils_i18n.h>
5
6 #include <loguru.hpp>
7
8 #include "UiDevice.h"
9 #include <string>
10
11 GetDeviceTimeCommand::GetDeviceTimeCommand(
12     const ::aurum::ReqGetDeviceTime* request,
13     ::aurum::RspGetDeviceTime*       response)
14     : mRequest{request}, mResponse{response}
15 {
16 }
17
18 class TizenLocaleTimeConverter {
19 public:
20     static std::string convert(long long timestamp, const char *pattern)
21     {
22         if (timestamp < 0 || pattern == NULL) return "";
23
24         char *locale, *timezone;
25
26         i18n_udatepg_h pattern_generator = NULL;
27         i18n_udate_format_h formatter = NULL;
28
29         i18n_uchar timezone_i18[64] = {0,};
30         i18n_uchar pattern_i18[64]= {0,};
31         i18n_uchar best_pattern_i18[64]= {0,};
32         i18n_uchar result_i18[64]= {0,};
33         char result[64]= {0,};
34
35         int pattern_len, best_pattern_len, result_i18n_len;
36
37         if (SYSTEM_SETTINGS_ERROR_NONE != system_settings_get_value_string(SYSTEM_SETTINGS_KEY_LOCALE_COUNTRY, &locale))
38             return "";
39
40         if (SYSTEM_SETTINGS_ERROR_NONE != system_settings_get_value_string(SYSTEM_SETTINGS_KEY_LOCALE_TIMEZONE, &timezone)) {
41             if (locale) free(locale);
42             return "";
43         }
44
45         i18n_udatepg_create(locale, &pattern_generator);
46
47         if (!pattern_generator) {
48             free (locale);
49             free (timezone);
50             return "";
51         }
52
53         i18n_ustring_copy_ua_n(pattern_i18, pattern, strlen(pattern));
54         pattern_len = i18n_ustring_get_length(pattern_i18);
55
56         i18n_udatepg_get_best_pattern(pattern_generator,
57                                       pattern_i18, pattern_len,
58                                       best_pattern_i18, 64, &best_pattern_len);
59
60         i18n_ustring_copy_ua_n(timezone_i18, timezone, strlen(timezone));
61
62         i18n_udatepg_destroy(pattern_generator);
63
64         if (I18N_ERROR_NONE !=
65             i18n_udate_create(I18N_UDATE_PATTERN, I18N_UDATE_PATTERN, locale,
66                               timezone_i18, -1, best_pattern_i18, -1,
67                               &formatter)) {
68             free (locale);
69             free (timezone);
70             return "";
71         }
72
73         if (formatter) {
74             i18n_udate date = timestamp;
75             i18n_udate_format_date(formatter, date, result_i18, 64, NULL, &result_i18n_len);
76             i18n_ustring_copy_au_n(result , result_i18, 64);
77             i18n_udate_destroy(formatter);
78             return std::string{result};
79         }
80
81         free (locale);
82         free (timezone);
83         return "";
84     }
85 };
86
87 ::grpc::Status GetDeviceTimeCommand::execute()
88 {
89     LOG_SCOPE_F(INFO, "GetDeviceTime --------------- ");
90
91     std::shared_ptr<UiDevice> obj = UiDevice::getInstance();
92     ::aurum::ReqGetDeviceTime_TimeType type = mRequest->type();
93     long long                          utcStampMs;
94
95     switch (type) {
96     case ::aurum::ReqGetDeviceTime_TimeType::ReqGetDeviceTime_TimeType_WALLCLOCK:
97         utcStampMs = obj->getSystemTime(TimeRequestType::WALLCLOCK);
98         mResponse->set_localedatetime(
99             TizenLocaleTimeConverter::convert(utcStampMs, "EEE, MMM d, yyyy 'at' HH:mm:ss zzz").c_str()
100         );
101         mResponse->set_timestamputc(utcStampMs);
102         break;
103
104     case ::aurum::ReqGetDeviceTime_TimeType::ReqGetDeviceTime_TimeType_SYSTEM:
105     default:
106         utcStampMs = obj->getSystemTime(TimeRequestType::MONOTONIC);
107         mResponse->set_timestamputc(utcStampMs);
108         break;
109     }
110
111     mResponse->set_status(::aurum::RspStatus::OK);
112     return grpc::Status::OK;
113 }