Добавлено временное смещение и перевод зон

pull/31/head
Igor Belitskiy 3 years ago
parent bf45c57066
commit c8d763e64a

@ -7,6 +7,7 @@ pkg_check_modules(GTK REQUIRED gtkmm-3.0)
include_directories(${GTK_INCLUDE_DIRS})
link_directories(${GTK_LIBRARY_DIRS})
add_definitions(${GTK_CFLAGS_OTHER})
find_package(ICU REQUIRED COMPONENTS uc dt in io)
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic -Wall -Wextra -Werror -Wmissing-declarations -fdiagnostics-color=always -std=c++2a -g")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic -Wall -Wextra -Werror -Wmissing-declarations -fdiagnostics-color=always -g")
@ -21,5 +22,5 @@ set(LIBRARIES
pthread)
add_executable(ubl-settings-datetime ${SOURCE_FILES})
target_link_libraries(ubl-settings-datetime ${LIBRARIES})
install(TARGETS ubl-settings-datetime DESTINATION bin)
target_link_libraries(ubl-settings-datetime ${LIBRARIES} ${ICU_LIBRARIES})
install(TARGETS ubl-settings-datetime DESTINATION bin)

@ -22,6 +22,16 @@
#include <string>
#include <sstream>
#include <vector>
#include <unicode/unistr.h>
#include <unicode/ustdio.h>
#include <unicode/uloc.h>
#include <unicode/uldnames.h>
#include "unicode/utypes.h"
#include "unicode/utext.h"
#include <unicode/timezone.h>
#include <unicode/timezone.h>
#include <unicode/calendar.h>
#include "ubl-settings-datetime.h"
using namespace std;
@ -36,7 +46,8 @@ bool flag_ntp = false;
bool flag_update = false;
CmdArgParser::CmdArgParser(const std::string& p_name, const std::string& p_description, const std::string& p_help)
: Glib::OptionGroup{p_name, p_description, p_help}{
: Glib::OptionGroup{p_name, p_description, p_help}
{
Glib::OptionEntry socketIDArg;
socketIDArg.set_long_name("socket-id");
socketIDArg.set_short_name('s');
@ -97,6 +108,7 @@ void MainWindow::settings(){
lblRegGlob->set_sensitive(false);
lblZone1Glob->set_sensitive(false);
lblDateTimeSetting->set_sensitive(false);
}
}
@ -108,7 +120,6 @@ int MainWindow::check_root(){
}
return 0;
}
void MainWindow::lacalization(){
txtDate->set_tooltip_text(gettext("Date of\nFormat: DD.MM.YYYY"));
lblSynchronizebChkGLob->set_text(gettext("Synchronize via NTP"));
@ -135,10 +146,14 @@ void MainWindow::lacalization(){
lblBanerStopNtp->set_text(gettext("Active service detected\nAutomatic time and date synchronization service detected"));
lblNtpStop->set_text(gettext("Stop the synchronization service"));
lblNtpClose->set_text(gettext("Close"));
//_lblSynchronizebChk->set_text(gettext("Synchronize via NTP"));
}
void MainWindow::flag_block_gui(){
if (flag_datetime==true){
//numTimeHrs->set_sensitive(false);
//numTimeMin->set_sensitive(false);
//txtDate->set_sensitive(false);
btnUpdateDateTime->set_sensitive(false);
numTimeHrs->set_sensitive(false);
numTimeMin->set_sensitive(false);
@ -148,12 +163,14 @@ void MainWindow::flag_block_gui(){
btnHardwareTime->set_sensitive(false);
lblTime->set_sensitive(false);
lblData->set_sensitive(false);
//cbxSynchronizeNtpGlob->set_sensitive(false);
}
if (flag_timezone==true){
cbRegion->set_sensitive(false);
cbZone->set_sensitive(false);
cbRegionGlob->set_sensitive(false);
cbZoneGlob->set_sensitive(false);
//btnChooseDate->set_sensitive(false);
cbDhcp->set_sensitive(false);
}
if (flag_ntp==true){
@ -163,6 +180,7 @@ void MainWindow::flag_block_gui(){
}
if (flag_update==true){
lblDateTimeSettingGlob->set_sensitive(false);
//lblSynchronizebChkGLob->set_sensitive(false);
cbxSynchronizeNtpGlob->set_sensitive(false);
lblTimeZoneGlob->set_sensitive(false);
lblRegGlob->set_sensitive(false);
@ -178,8 +196,8 @@ void MainWindow::add_CSS(){
Glib::RefPtr<Gtk::CssProvider> cssProvider = Gtk::CssProvider::create();
cssProvider->load_from_path(path_css);
Glib::RefPtr<Gtk::StyleContext> styleContext = Gtk::StyleContext::create();
Glib::RefPtr<Gdk::Screen> screen = Gdk::Screen::get_default();
styleContext->add_provider_for_screen(screen, cssProvider, GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
Glib::RefPtr<Gdk::Screen> screen = Gdk::Screen::get_default();//get default screen
styleContext->add_provider_for_screen(screen, cssProvider, GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);//add provider for screen in all application
Glib::RefPtr<Gtk::StyleContext> context = boxColor->get_style_context();
Glib::RefPtr<Gtk::StyleContext> context_lbl_head = lblHead->get_style_context();
context->add_class("cssboxColor1");
@ -216,6 +234,7 @@ void MainWindow::get_builder(){
builder->get_widget("lblRegGlob",lblRegGlob);
builder->get_widget("lblZone1Glob",lblZone1Glob);
builder->get_widget("lblTimeZoneGlob",lblTimeZoneGlob);
//builder->get_widget("lblSynchronizebChk",_lblSynchronizebChk);
builder->get_widget("lblSynchronizebChkGLob",lblSynchronizebChkGLob);
builder->get_widget("lblSynchronizeBtn",lblSynchronizeBtn);
builder->get_widget("lblDateTimeSettingGlob",lblDateTimeSettingGlob);
@ -230,9 +249,11 @@ void MainWindow::get_builder(){
builder->get_widget("lblNtpStop",lblNtpStop);
builder->get_widget("lblNtpClose",lblNtpClose);
builder->get_widget("lblBanerStopNtp",lblBanerStopNtp);
//builder->get_widget("cbxSynchronizeNtp",cbxSynchronizeNtp);
this->add_CSS();
}
void MainWindow::gui_exit(){
exit(1);
}
@ -263,7 +284,6 @@ void MainWindow::event(){
btnNtpWinClose->signal_clicked().connect(sigc::mem_fun(*this, &MainWindow::wind_close_ntp));
btnNtpStop->signal_clicked().connect(sigc::mem_fun(*this, &MainWindow::stop_ntp));
}
void MainWindow::wind_close_ntp(){
windowsNnpClose->hide();
}
@ -282,6 +302,7 @@ void MainWindow::stop_ntp(){
this->call(cmd.c_str());
}
}
system(cmd.c_str());
this->update_time_date();
@ -307,6 +328,7 @@ bool MainWindow::focus_ntp(GdkEventFocus* event){
cmd = "/usr/bin/ubconfig set network NTPSERVERS=" + txtNtpServer->get_text();
this->call(cmd.c_str());
}
return true;
}
@ -447,6 +469,8 @@ void MainWindow::get_config(){
this->update_hour_minute();
this->update_calendar();
this->enry_dhcp_mess();
string read_reg_zon_cfg = this->call("/usr/bin/ubconfig get clock ZONE");
if ((read_reg_zon_cfg != "") && (strstr(read_reg_zon_cfg.c_str() ,"(null)")==NULL)){
read_reg_zon_cfg = read_reg_zon_cfg.substr(read_reg_zon_cfg.find("=")+1,read_reg_zon_cfg.length());
@ -463,15 +487,29 @@ void MainWindow::get_config(){
}
void MainWindow::enter_zone(){
Glib::ustring zone_text = cbZone->get_active_text();
Glib::ustring reg_text = cbRegion->get_active_text();
if(!(zone_text.empty()) && !(reg_text.empty())){
str_zone=zone_text.substr(zone_text.find(") ")+2,zone_text.length());
str_region=reg_text;
string cmd = "rm -f /etc/localtime";
system(cmd.c_str());
string cmd1 = "ln -s /usr/share/zoneinfo/" + str_region + "/" + str_zone+" /etc/localtime";
system(cmd1.c_str());
string reg_text = cbRegion->get_active_text();
int activ_index = cbZone->get_active_row_number();
string zone_text = "";
if(!(reg_text.empty())){
int index=0;
for (const auto &zone :time_reg_map.at(reg_text)){
if (index==activ_index){
zone_text=zone;
}
index+=1;
}
if (!(zone_text.empty())){
str_zone=zone_text;
str_region=reg_text;
string cmd = "rm -f /etc/localtime";
system(cmd.c_str());
string cmd1 = "ln -s /usr/share/zoneinfo/" + str_region + "/" + str_zone+" /etc/localtime";
system(cmd1.c_str());
}
else{
str_zone="";
str_region="";
}
}
else{
str_zone="";
@ -489,16 +527,30 @@ void MainWindow::parse_text_date(){
}
void MainWindow::enter_zone_glob(){
Glib::ustring zone_text = cbZoneGlob->get_active_text();
Glib::ustring reg_text = cbRegionGlob->get_active_text();
if(!(zone_text.empty()) && !(reg_text.empty())){
string reg_text = cbRegionGlob->get_active_text();
int activ_index = cbZoneGlob->get_active_row_number();
string zone_text="";
if(!(reg_text.empty())){
int index=0;
for (const auto &zone :time_reg_map.at(reg_text)){
if (index==activ_index){
zone_text=zone;
}
index+=1;
}
if (!(zone_text.empty())){
//str_zoneGlob = zone_text.substr(zone_text.find(") ")+2,zone_text.length());
string cmd = "/usr/bin/ubconfig set clock ZONE=" +reg_text +"/" + zone_text;
system(cmd.c_str());
string cmd = "/usr/bin/ubconfig set clock ZONE=" +reg_text +"/" + zone_text;
system(cmd.c_str());
}
else{
str_zoneGlob="";
str_region_glob="";
}
}
else{
str_zoneGlob="";
str_region_glob="";
str_region_glob="";
}
}
@ -518,9 +570,12 @@ void MainWindow::set_ntp_toggle_glob(){
void MainWindow::append_region_zone(string region, string zone){
int index = 0;
for ( const auto &text : time_reg_map ) {
cbRegion->append(text.first);
cbRegionGlob->append(text.first);
string reg_local = "";
for (auto &text : time_reg_map ) {
reg_local = text.first;
//reg_local = string(gettext(reg_local.c_str()));
cbRegion->append(reg_local);
cbRegionGlob->append(reg_local);
if (text.first == region){
cbRegionGlob->set_active(index);
cbRegion->set_active(index);
@ -528,9 +583,13 @@ void MainWindow::append_region_zone(string region, string zone){
index+=1;
}
index = 0;
string zone_mixing = "";
string path_dir = "";
for (const auto &_str_zone : time_reg_map.at(region)){
cbZone->append(_str_zone);
cbZoneGlob->append(_str_zone);
path_dir=region+"/"+_str_zone;
zone_mixing = this->zone_file_read(path_dir) + string(gettext(_str_zone.c_str()));
cbZone->append(zone_mixing);
cbZoneGlob->append(zone_mixing);
if (_str_zone!=""){
if (_str_zone == zone){
cbZone->set_active(index);
@ -541,7 +600,6 @@ void MainWindow::append_region_zone(string region, string zone){
}
}
string MainWindow::call(string cmd){
FILE *fp;
int status;
@ -551,6 +609,7 @@ string MainWindow::call(string cmd){
exit(1);
}
while (fgets(path, PATH_MAX, fp) != NULL){
//printf("%s", path);
break;
}
status = pclose(fp);
@ -562,23 +621,35 @@ string MainWindow::call(string cmd){
}
void MainWindow::append_zone(){
Glib::ustring text = cbRegion->get_active_text();
cbZone->remove_all();
Glib::ustring text = cbRegion->get_active_text();
string zone_mixing="";
string path_dir="";
if(!(text.empty())){
str_region = text;
for (const auto &_str_zone : time_reg_map.at(str_region)){
cbZone->append(_str_zone);
path_dir=str_region+"/"+_str_zone;
zone_mixing = this->zone_file_read(path_dir) + string(gettext(_str_zone.c_str()));
cbZone->append(zone_mixing);
//cbZone->append(_str_zone);
}
}
}
void MainWindow::append_zone_glob(){
Glib::ustring text = cbRegionGlob->get_active_text();
cbZoneGlob->remove_all();
Glib::ustring text = cbRegionGlob->get_active_text();
string zone_mixing="";
string path_dir="";
if(!(text.empty())){
str_region_glob = text;
for (const auto &_str_zone : time_reg_map.at(str_region_glob)){
cbZoneGlob->append(_str_zone);
path_dir=str_region+"/"+_str_zone;
zone_mixing = this->zone_file_read(path_dir) + string(gettext(_str_zone.c_str()));
cbZoneGlob->append(zone_mixing);
//cbZoneGlob->append(_str_zone);
}
}
}
@ -595,6 +666,7 @@ void MainWindow::wrapper_update_time_date(){
}
void MainWindow::update_time_date(){
//this->enter_zone();
string cmd = "";
if ((year!=0) && (month !=0) && (day !=0)){
string str_month = "";
@ -642,29 +714,24 @@ void MainWindow::update_time_date(){
system(cmd.c_str());
}
void MainWindow::post_entry_data(){
string cmd = "";
string data = txtDate->get_text();
if (data.length()==10){
string str_day = data.substr(0,2);
string str_month = data.substr(3,2);
string str_year = data.substr(6,data.length());
cmd = "date +%Y%m%d -s \"" + str_year+
str_month+str_day+"\"";
string str_error_clock = this->call(cmd);
size_t index = str_error_clock.find("date: ");
if (index==std::string::npos){
string data = txtDate->get_text();
if (data.length()==10){
string str_day = data.substr(0,2);
string str_month = data.substr(3,2);
string str_year = data.substr(6,data.length());
cmd = "date +%Y%m%d -s \"" + str_year+
str_month+str_day+"\"";
string str_error_clock = this->call(cmd);
size_t index = str_error_clock.find("date: ");
if (index==std::string::npos){
}
}
else{
lblMessage->set_text(gettext("Incorrect date format"));
mess_dchp->show();
mess_dchp->show();
}
}
else{
lblMessage->set_text(gettext("Incorrect date format"));
mess_dchp->show();
}
}
void MainWindow::update_hour_minute(){
@ -703,7 +770,7 @@ void MainWindow::read_file(){
std::string line;
namespace fs = std::filesystem;
for (const auto & entry : fs::directory_iterator("/usr/share/zoneinfo/")){
std::ifstream in(entry.path());
std::ifstream in(entry.path()); // окрываем файл для чтения
if (in.is_open()){
string key_reg = entry.path().filename().string();
string path_dir = "/usr/share/zoneinfo/"+key_reg;
@ -715,16 +782,17 @@ void MainWindow::read_file(){
vector<string> zone;
zone.push_back("");
for (const auto & entry_zone : fs::directory_iterator(path_dir)){
std::ifstream in1(entry_zone.path());
std::ifstream in1(entry_zone.path()); // окрываем файл для чтения
if (in1.is_open()){
string key_zone = entry_zone.path().filename().string();
if (key_zone.length()!=0 || key_zone!=" "){
zone.push_back(string(gettext(key_zone.c_str())));
zone.push_back(key_zone);
}
}
in1.close();
}
key_reg = string(gettext(key_reg.c_str()));
key_reg = string(key_reg);
time_reg_map.insert({key_reg, zone});
}}}}
}
@ -733,6 +801,39 @@ void MainWindow::read_file(){
}
}
string MainWindow::zone_file_read(string zone){
UErrorCode success = U_ZERO_ERROR;
UDate curDate;
int32_t stdOffset,dstOffset;
U_ICU_NAMESPACE::TimeZone *tzWest = U_ICU_NAMESPACE::TimeZone::createTimeZone(zone.c_str());
U_ICU_NAMESPACE::Calendar* calendar = U_ICU_NAMESPACE::Calendar::createInstance(success);
curDate = calendar->getNow();
tzWest->getOffset(curDate,true,stdOffset,dstOffset,success);
int offset = stdOffset/(1000*60*60);
string str_utc = "";
if (offset>=0){
if (offset<=9){
str_utc = "(UTC +0" + to_string(offset) + ") ";
}
else{
str_utc = "(UTC +" + to_string(offset) + ") ";
}
}
else{
if (offset>=-9){
offset*=-1;
str_utc = "(UTC -0" + to_string(offset) + ") ";
}
else{
str_utc = "(UTC " + to_string(offset) + ") ";
}
}
delete calendar;
delete tzWest;
return str_utc;
}
vector<std::string> MainWindow::split(const std::string &s, char delim){
std::stringstream ss(s);
std::string item;
@ -744,7 +845,8 @@ vector<std::string> MainWindow::split(const std::string &s, char delim){
}
SettingsPlug::SettingsPlug(::Window p_socketID, Glib::RefPtr<Gtk::Builder> builder)
: Gtk::Plug{p_socketID}{
: Gtk::Plug{p_socketID}
{
builder->get_widget("plugBox", plugBox);
plugBox->get_parent()->remove(*plugBox);
add(*plugBox);

@ -21,6 +21,16 @@
#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>
#include <unicode/unistr.h>
#include <unicode/ustdio.h>
#include <unicode/uloc.h>
#include <unicode/uldnames.h>
#include "unicode/utypes.h"
#include "unicode/utext.h"
#include <unicode/timezone.h>
#include <unicode/timezone.h>
#include <unicode/calendar.h>
using namespace std;
extern string path_app;
@ -81,6 +91,7 @@ class MainWindow : public Gtk::ApplicationWindow {
void stop_ntp();
void wind_close_ntp();
void wrapper_update_time_date();
string zone_file_read(string zone);
string str_remove(std::string& source, const std::string to_remove);
vector<std::string> split(const std::string &s, char delim);
private:
@ -130,6 +141,7 @@ class MainWindow : public Gtk::ApplicationWindow {
Gtk::Entry *txtNtpServer;
Gtk::Entry *txtDate;
std::map <string, vector<string>> time_reg_map;
std::map <string, string> time_reg_map_local;
unsigned int year=0;
unsigned int month=0;
unsigned int day=0;

BIN
test

Binary file not shown.
Loading…
Cancel
Save