Merge pull request 'develop' (#6) from develop into master

Reviewed-on: Igor1/ubl-settings-datetime#6
pull/31/head
Igor Belitskiy 3 years ago
commit 74ef1cad67

@ -7,9 +7,10 @@ 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")
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic -Wall -Wextra -Werror -Wmissing-declarations -fdiagnostics-color=always -std=c++2a")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic -Wall -Wextra -Werror -Wmissing-declarations -fdiagnostics-color=always")
set(SOURCE_FILES
main.cc
@ -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)

@ -1,22 +1,6 @@
#include <gtkmm/window.h>
#include <cstddef>
#include <cstdlib>
#include <memory>
#include <gtkmm.h>
#include <ostream>
#include <string>
#include <ctime>
#include <vector>
#include <map>
#include <fstream>
#include <filesystem>
#include <glibmm/i18n.h>
#include <stdio.h>
#include <gtkmm/plug.h>
#include <gtkmm/stock.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>
#include <iostream>
#include "ubl-settings-datetime.h"
int main(int argc, char* argv[]) {

@ -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;
@ -104,39 +114,39 @@ void MainWindow::settings(){
int MainWindow::check_root(){
if (geteuid()!=0){
lblMessageSudo->set_text(gettext("The program must be run with \nsuperuser privileges!"));
lblMessageSudo->set_text(_("The program must be run with \nsuperuser privileges!"));
mess_sudo->show();
return 1;
}
return 0;
}
void MainWindow::lacalization(){
txtDate->set_tooltip_text(gettext("Date of\nFormat: DD.MM.YYYY"));
lblSynchronizebChkGLob->set_text(gettext("Synchronize via NTP"));
cbDhcp->append(gettext("By default"));
cbDhcp->append(gettext("DHCP"));
cbDhcp->append(gettext("Manually"));
lblDateTimeSetting->set_text(gettext("Local Configuration"));
lblHead->set_text(gettext("Settings the date and time"));
lblTime->set_text(gettext("Time"));
lblData->set_text(gettext("Date"));
lblTimeZone->set_text(gettext("Time zone"));
lblReg->set_text(gettext("Region"));
lblZone->set_text(gettext("Zone"));
lblRegGlob->set_text(gettext("Region"));
lblZone1Glob->set_text(gettext("Zone"));
lblTimeZoneGlob->set_text(gettext("Time zone"));
lblSynchronizeBtn->set_text(gettext("Sync by"));
lblDateTimeSettingGlob->set_text(gettext("Global customization"));
this->set_title(gettext("ubl-settings-datetime"));
windowsNnpClose->set_title(gettext("Warning!"));
mess_sudo->set_title(gettext("Warning!"));
mess_dchp->set_title(gettext("Warning!"));
lblTimeBios->set_text(gettext("Synchronize hardware time"));
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"));
txtDate->set_tooltip_text(_("Date of\nFormat: DD.MM.YYYY"));
lblSynchronizebChkGLob->set_text(_("Synchronize via NTP"));
cbDhcp->append(_("By default"));
cbDhcp->append(_("DHCP"));
cbDhcp->append(_("Manually"));
lblDateTimeSetting->set_text(_("Local Configuration"));
lblHead->set_text(_("Settings the date and time"));
lblTime->set_text(_("Time"));
lblData->set_text(_("Date"));
lblTimeZone->set_text(_("Time zone"));
lblReg->set_text(_("Region"));
lblZone->set_text(_("Zone"));
lblRegGlob->set_text(_("Region"));
lblZone1Glob->set_text(_("Zone"));
lblTimeZoneGlob->set_text(_("Time zone"));
lblSynchronizeBtn->set_text(_("Sync by"));
lblDateTimeSettingGlob->set_text(_("Global customization"));
this->set_title(_("ubl-settings-datetime"));
windowsNnpClose->set_title(_("Warning!"));
mess_sudo->set_title(_("Warning!"));
mess_dchp->set_title(_("Warning!"));
lblTimeBios->set_text(_("Synchronize hardware time"));
lblBanerStopNtp->set_text(_("Active service detected\nAutomatic time and date synchronization service detected"));
lblNtpStop->set_text(_("Stop the synchronization service"));
lblNtpClose->set_text(_("Close"));
//_lblSynchronizebChk->set_text(_("Synchronize via NTP"));
}
void MainWindow::flag_block_gui(){
@ -280,24 +290,15 @@ void MainWindow::wind_close_ntp(){
void MainWindow::stop_ntp(){
windowsNnpClose->hide();
/*
// string cmd = "systemd-timesyncd [[ systemctl --quiet is-active systemd-timesyncd &>/dev/null ]] && systemctl stop systemd-timesyncd";
string cmd = "sudo systemctl stop systemd-timesyncd; " ;
cmd += "sudo systemctl stop ntp.service; sudo systemctl stop ntpdate.service; ";
cmd += "sudo systemctl stop chronyd; sudo service ntpd stop; sudo systemctl stop ptp4l; ";
cmd += "sudo systemctl stop openntpd.service; sudo timedatectl set-ntp false";
cmd += "sudo systemctl stop phc2sys";
*/
string cmd = "";
string response = "";
string array_servis[8] = {"systemd-timesyncd", "chronyd", "ntp.service", "ntpdate.service",
"ntpd", "ptp4l", "openntpd.service", "phc2sys"};
string array_servis[8] = {"systemd-timesyncd", "chronyd.service", "ntp.service", "ntpdate.service",
"ntpd.service", "ptp4l.service", "openntpd.service", "phc2sys.service"};
for (string &name_serive: array_servis){
cmd = "sudo systemctl status " + name_serive + " | grep \"Active: active\"";
cmd = "systemctl status " + name_serive + " | grep \"Active: active\"";
response = this->call(cmd.c_str());
if (response.length()!=0){
cmd = "sudo systemctl stop " + name_serive;
cmd = "systemctl --quiet is-active " + name_serive + " &>/dev/null && systemctl stop " + name_serive;
this->call(cmd.c_str());
}
}
@ -319,7 +320,7 @@ void MainWindow::gui_mess_close(){
bool MainWindow::focus_ntp(GdkEventFocus* event){
if (event!=NULL){}
if (txtNtpServer->get_text()=="" || txtNtpServer->get_text()==" "){
lblMessage->set_text(gettext("Enter DHCP!"));
lblMessage->set_text(_("Enter DHCP!"));
mess_dchp->show();
}
else{
@ -334,7 +335,6 @@ bool MainWindow::focus_ntp(GdkEventFocus* event){
void MainWindow::event_entry_cbDhcp(){
Glib::ustring str_dhcp = cbDhcp->get_active_text();
int activ_index = cbDhcp->get_active_row_number();
cout << activ_index << endl;
if (str_dhcp.length()!=0){
if (activ_index==0){
string cmd = "/usr/bin/ubconfig set network NTPSERVERS=default";
@ -442,7 +442,7 @@ void MainWindow::enry_dhcp_mess(){
}
else{
if (str_dhcp==""){
lblMessage->set_text(gettext("Enter DHCP!"));
lblMessage->set_text(_("Enter DHCP!"));
mess_dchp->show();
}
else if (str_dhcp=="(null)"){
@ -479,6 +479,7 @@ void MainWindow::get_config(){
str_filling_zon = str_filling_zon.substr(0,str_filling_zon.find("\n"));
string str_zone_check = "";
this->append_region_zone(str_filling_reg,str_filling_zon);
//this->append_region_zone("Africa","Abidjan");
}
else {
this->append_region_zone("Africa","Abidjan");
@ -486,15 +487,30 @@ 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());
int activ_index_reg = cbRegion->get_active_row_number();
string reg_text=array_region[activ_index_reg];
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="";
@ -512,16 +528,31 @@ 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())){
int activ_index_reg = cbRegionGlob->get_active_row_number();
string reg_text=array_region[activ_index_reg];
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="";
}
}
@ -540,22 +571,33 @@ void MainWindow::set_ntp_toggle_glob(){
}
void MainWindow::append_region_zone(string region, string zone){
str_region=region;
str_region_glob=region;
int index = 0;
for ( const auto &text : time_reg_map ) {
cbRegion->append(text.first);
cbRegionGlob->append(text.first);
if (text.first == region){
cbRegionGlob->set_active(index);
cbRegion->set_active(index);
Glib::ustring reg_local = "";
for (const auto &text : array_region) {
reg_local = text;
reg_local = string(_(reg_local.c_str()));
cbRegion->append(Glib::ustring(reg_local));
cbRegionGlob->append(Glib::ustring(reg_local));
if (text == region){
cbRegionGlob->set_active_text(reg_local);
cbRegion->set_active_text(reg_local);
}
index+=1;
}
index = 0;
for (const auto &_str_zone : time_reg_map.at(region)){
cbZone->append(_str_zone);
cbZoneGlob->append(_str_zone);
string zone_mixing = "";
string path_reg_zone = "";
string zone_offest = "";
path_reg_zone=region+"/"+zone;
zone_offest = this->zone_file_read(path_reg_zone) + _(zone.c_str());
for (string &_str_zone : time_reg_map_local.at(region)){
cbZone->append(zone_mixing);
cbZoneGlob->append(zone_mixing);
if (_str_zone!=""){
if (_str_zone == zone){
if (zone_offest == _str_zone){
cbZone->set_active(index);
cbZoneGlob->set_active(index);
}
@ -585,25 +627,33 @@ string MainWindow::call(string cmd){
}
void MainWindow::append_zone(){
Glib::ustring text = cbRegion->get_active_text();
//int length_zone = time_reg_map.at(str_region).size();
cbZone->remove_all();
if(!(text.empty())){
str_region = text;
for (const auto &_str_zone : time_reg_map.at(str_region)){
cbZone->append(_str_zone);
int index = cbRegion->get_active_row_number();
string str_region = array_region[index];
string zone_mixing="";
string path_dir="";
if(str_region.length()!=0){
for (const auto &_str_zone : time_reg_map_local.at(str_region)){
cbZone->append(Glib::ustring(_str_zone));
}
}
cbZone->set_active(0);
}
void MainWindow::append_zone_glob(){
Glib::ustring text = cbRegionGlob->get_active_text();
cbZoneGlob->remove_all();
if(!(text.empty())){
str_region_glob = text;
for (const auto &_str_zone : time_reg_map.at(str_region_glob)){
cbZoneGlob->append(_str_zone);
int index = cbRegionGlob->get_active_row_number();
string str_region_glob = array_region[index];
string zone_mixing="";
string path_dir="";
if(str_region_glob.length()!=0){
for (const auto &_str_zone : time_reg_map_local.at(str_region_glob)){
cbZoneGlob->append(Glib::ustring(_str_zone));
}
}
cbZoneGlob->set_active(0);
}
void MainWindow::wrapper_update_time_date(){
string cmd = "pidof systemd-timesyncd ntpd chronyd";
@ -681,7 +731,7 @@ void MainWindow::post_entry_data(){
}
}
else{
lblMessage->set_text(gettext("Incorrect date format"));
lblMessage->set_text(_("Incorrect date format"));
mess_dchp->show();
}
}
@ -720,6 +770,8 @@ void MainWindow::update_calendar(){
void MainWindow::read_file(){
std::string line;
string key_zone_local = "";
string path_reg_zone = "";
namespace fs = std::filesystem;
for (const auto & entry : fs::directory_iterator("/usr/share/zoneinfo/")){
std::ifstream in(entry.path()); // окрываем файл для чтения
@ -732,20 +784,24 @@ void MainWindow::read_file(){
if (key_reg!="Etc"){
if (key_reg!="right"){
vector<string> zone;
zone.push_back("");
vector<string> zone_local;
//zone.push_back("");
for (const auto & entry_zone : fs::directory_iterator(path_dir)){
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);
path_reg_zone=key_reg + "/" + key_zone;
key_zone_local = this->zone_file_read(path_reg_zone) +_(key_zone.c_str());
zone_local.push_back(key_zone_local);
}
}
in1.close();
}
key_reg = string(gettext(key_reg.c_str()));
time_reg_map.insert({key_reg, zone});
time_reg_map.insert({key_reg, zone});
time_reg_map_local.insert({key_reg, zone_local});
}}}}
}
}
@ -753,6 +809,43 @@ void MainWindow::read_file(){
}
}
string MainWindow::zone_file_read(string zone){
if (zone.length()==1){
return "";
}
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;

@ -1,26 +1,16 @@
#ifndef UBL_SETTINGS_DATETIME_H
#define UBL_SETTINGS_DATETIME_H
#include <gtkmm/window.h>
#include <stddef.h>
#include <cstddef>
#include <cstdlib>
#include <iostream>
#include <memory>
#include <gtkmm.h>
#include <ostream>
#include <string>
#include <ctime>
#include <vector>
#include <map>
#include <fstream>
#include <filesystem>
#include <glibmm/i18n.h>
#include <stdio.h>
#include <gtkmm/plug.h>
#include <gtkmm/stock.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>
using namespace std;
extern string path_app;
@ -81,6 +71,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 +121,7 @@ class MainWindow : public Gtk::ApplicationWindow {
Gtk::Entry *txtNtpServer;
Gtk::Entry *txtDate;
std::map <string, vector<string>> time_reg_map;
std::map <string, vector<string>> time_reg_map_local;
unsigned int year=0;
unsigned int month=0;
unsigned int day=0;
@ -141,7 +133,21 @@ class MainWindow : public Gtk::ApplicationWindow {
string strtxtNtpServer;
string str_zone="";
string str_zoneGlob;
string array_region[14]={
"Africa",
"America",
"Antarctica",
"Arctic",
"Asia",
"Atlantic",
"Australia",
"Brazil",
"Canada",
"Chile",
"Europe",
"Indian",
"Mexico",
"US"};
};
class SettingsPlug : public Gtk::Plug

BIN
test

Binary file not shown.

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save