diff --git a/Makefile b/Makefile index 165a648..e79e92a 100644 --- a/Makefile +++ b/Makefile @@ -56,6 +56,9 @@ build: depend prepare make --directory=${CMAKE_BUILD_DIR}; \ echo "Build: OK" # g++ -O2 -std=c++20 -lstdc++ -o ubl-settings-datetime ubl-settings-datetime.cpp `pkg-config --cflags --libs gtkmm-3.0` -Wl,-export-dynamic -g + g++ -O2 -std=c++20 -o ubl_settings_datetime.o -c -lstdc++ ubl_settings_datetime.cc `pkg-config --cflags --libs gtkmm-3.0` -g + g++ -O2 -std=c++20 -o main.o -c -lstdc++ main.cc `pkg-config --cflags --libs gtkmm-3.0` -Wl,-export-dynamic -g + g++ -o ubl_settings_datetime main.o ubl_settings_datetime.o `pkg-config --cflags --libs gtkmm-3.0` -Wl,-export-dynamic -g uninstall: @echo "Uninstall ..." diff --git a/source/main.cc b/source/main.cc new file mode 100644 index 0000000..3d7f809 --- /dev/null +++ b/source/main.cc @@ -0,0 +1,100 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "ubl_settings_datetime.h" +int main(int argc, char* argv[]) { + + //path_app=filesystem::current_path(); + //path_app+="/"; + //cout << *argv[0] << endl; + //app_name=to_string(*argv[0]); + //app_name=app_name.substr(1, app_name.length()); + string str_cmd_argv = ""; + for (int i=1; iget_widget_derived("window", wnd); + auto r = app->run(*wnd); + + delete wnd; + return r; + } + else{ + Glib::init(); + setlocale(LC_ALL, ""); + Glib::OptionContext context; + + CmdArgParser parser{ + "Socket ID", + "Command line argument for socket ID communication.", + "No help available, sorry" + }; + + context.set_main_group(parser); + context.parse(argc, argv); + + ::Window socketID = parser.GetSocketID(); + + // Handle plug: + SettingsPlug plug{socketID, builder}; + plug.show(); + + app->run(plug); + + } + + +} \ No newline at end of file diff --git a/source/ubl-settings-datetime.cpp b/source/ubl-settings-datetime.cpp deleted file mode 100644 index 6c05d25..0000000 --- a/source/ubl-settings-datetime.cpp +++ /dev/null @@ -1,684 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -using namespace std; - -string path_app= "/usr/bin/"; -string path_glade= "/usr/share/ubl-settings-datetime/ui/"; -string app_name = "ubl-settings-datetime"; -bool flag_datetime = false; -bool flag_timezone = false; -bool flag_ntp = false; -bool flag_update = false; - - -class CmdArgParser : public Glib::OptionGroup -{ - -public: - - CmdArgParser(const std::string& p_name, const std::string& p_description, const std::string& p_help) - : Glib::OptionGroup{p_name, p_description, p_help} - { - // Define the 'socket ID' argument options: - Glib::OptionEntry socketIDArg; - socketIDArg.set_long_name("socket-id"); - socketIDArg.set_short_name('s'); - socketIDArg.set_flags(Glib::OptionEntry::FLAG_IN_MAIN); - socketIDArg.set_description("Settings manager socket"); - - // Register it in the parser. It value will be recorded in m_socketID for later usage. - add_entry(socketIDArg, m_socketID); - } - - // Override this to handle errors. I skipped it for simplicity. - // void on_error(Glib::OptionContext& context, const Glib::Error& error) override; - - ::Window GetSocketID() const - { - return m_socketID; - } - -private: - - int m_socketID = 0; - -}; - - - -class MainWindow : public Gtk::ApplicationWindow { -public: - MainWindow(BaseObjectType* obj, Glib::RefPtr const& builder) - : Gtk::ApplicationWindow(obj) - , builder{builder} - { - this->settings(); - - } - MainWindow(Glib::RefPtr const& builder) { - this->builder = builder; - this->settings(); - } - - virtual ~MainWindow() = default; - - void settings(){ - this->get_builder(); - _numTimeHrs->set_range(0,23); - _numTimeMin->set_range(0,59); - _numTimeHrs->set_increments(1.0,1.0); - _numTimeMin->set_increments(1.0,1.0); - _txtDate->set_sensitive(false); - this->get_config(); - this->flag_block_gui(); - this->event(); - } - - void flag_block_gui(){ - if (flag_datetime==true){ - _numTimeHrs->set_sensitive(false); - _numTimeMin->set_sensitive(false); - } - else if (flag_timezone==true){ - _cbRegion->set_sensitive(false); - _cbZone->set_sensitive(false); - _cbRegionGlob->set_sensitive(false); - _cbZoneGlob->set_sensitive(false); - _btnChooseDate->set_sensitive(false); - } - else if (flag_ntp==true){ - _cbxSynchronizeNtp->set_sensitive(false); - _cbDhcp->set_sensitive(false); - _txtNtpServer->set_sensitive(false); - } - else if (flag_update==true){ - _btnUpdateDateTime->set_sensitive(false); - } - } - void add_CSS(){ - Glib::RefPtr cssProvider = Gtk::CssProvider::create(); - cssProvider->load_from_path("style.css"); - Glib::RefPtr styleContext = Gtk::StyleContext::create(); - Glib::RefPtr 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 context = _btnUpdateDateTime->get_style_context(); - context->add_class("myButton"); - } - - void get_builder(){ - builder->set_translation_domain(app_name); - - builder->get_widget("btnUpdateDateTime", _btnUpdateDateTime); - builder->get_widget("btnChooseDate", _btnChooseDate); - builder->get_widget("btnMessClose", _btnMessClose); - builder->get_widget("cbxSynchronizeNtp", _cbxSynchronizeNtp); - builder->get_widget("popCalendar", _popCalendar); - builder->get_widget("numTimeHrs", _numTimeHrs); - builder->get_widget("numTimeMin", _numTimeMin); - builder->get_widget("cbDhcp", _cbDhcp); - builder->get_widget("txtDate", _txtDate); - builder->get_widget("cbRegion", _cbRegion); - builder->get_widget("cbRegionGlob", _cbRegionGlob); - builder->get_widget("cbZone", _cbZone); - builder->get_widget("cbZoneGlob", _cbZoneGlob); - builder->get_widget("txtNtpServer", _txtNtpServer); - builder->get_widget("cldrDate", _cldrDate); - builder->get_widget("mess_dchp", _mess_dchp); - //this->add_CSS(); - } - void event(){ - _btnMessClose->signal_clicked().connect(sigc::mem_fun(*this, &MainWindow::gui_mess_close)); - _cldrDate->signal_day_selected().connect(sigc::mem_fun(*this, &MainWindow::get_calendar)); - _cbxSynchronizeNtp->signal_toggled().connect(sigc::mem_fun(*this, &MainWindow::set_ntp_toggle)); - _btnUpdateDateTime->signal_clicked().connect(sigc::mem_fun(*this, &MainWindow::update_time_date)); - _btnChooseDate->signal_clicked().connect(sigc::mem_fun(*this, &MainWindow::calendar_show)); - _cbDhcp->signal_changed().connect(sigc::mem_fun(*this, &MainWindow::event_entry_cbDhcp)); - _cbRegion->signal_changed().connect(sigc::mem_fun(*this, &MainWindow::append_zone)); - _cbRegionGlob->signal_changed().connect(sigc::mem_fun(*this, &MainWindow::append_zone_glob)); - _cbZone->signal_changed().connect(sigc::mem_fun(*this, &MainWindow::enter_zone)); - _cbZoneGlob->signal_changed().connect(sigc::mem_fun(*this, &MainWindow::enter_zone_glob)); - _txtNtpServer->signal_focus_out_event().connect(sigc::mem_fun(*this, &MainWindow::focus_ntp)); - //_txtNtpServer->grab_focus(); - } - - void gui_mess_close(){ - _mess_dchp->hide(); - } - - bool focus_ntp(GdkEventFocus* event){ - if (_txtNtpServer->get_text()=="" || _txtNtpServer->get_text()==" "){ - _mess_dchp->show(); - } - else{ - string cmd = ""; - cmd = "/usr/bin/ubconfig set network NTPSERVERS=" + _txtNtpServer->get_text(); - this->call(cmd.c_str()); - } - - return true; - } - - - - - void event_entry_cbDhcp(){ - Glib::ustring str_dhcp = _cbDhcp->get_active_text(); - if (str_dhcp.length()!=0){ - if (str_dhcp=="DHCP"){ - string cmd = "/usr/bin/ubconfig set network NTPSERVERS=dhcp"; - system(cmd.c_str()); - _txtNtpServer->set_sensitive(false); - _cbDhcp->set_active(0); - _txtNtpServer->set_text(""); - } - else{ - string str_dhcp = this->call("/usr/bin/ubconfig get network NTPSERVERS"); - - if ((str_dhcp!="") && (str_dhcp!="(null)") ){ - str_dhcp = str_dhcp.substr(str_dhcp.find("=")+1,str_dhcp.length()); - str_dhcp = str_dhcp.substr(0,str_dhcp.find("\n")); - if (str_dhcp!="dhcp"){ - - - _txtNtpServer->set_text(str_dhcp); - } - _txtNtpServer->set_sensitive(true); - _cbDhcp->set_active(1); - } - } - } - else{ - string cmd = "/usr/bin/ubconfig set network NTPSERVERS=dhcp"; - system(cmd.c_str()); - //_txtNtpServer->set_sensitive(false); - _cbDhcp->set_active(0); - } - } - - void get_calendar(){ - _cldrDate->get_date(year, month, day); - string str_month = ""; - string str_day = ""; - month+=1; - if (month <10){ - str_month = "0"+to_string(month); - } - else{ - str_month = to_string(month); - } - if (day <10){ - str_day = "0"+to_string(day); - } - else{ - str_day = to_string(day); - } - string date = str_day + '.' + str_month + '.' + to_string(year); - _txtDate->set_text(date); - } - - - void calendar_show(){ - time_t now = time(0); - tm *ltm = localtime(&now); - unsigned int y=1900+ ltm->tm_year; - unsigned int m=ltm->tm_mon; - unsigned int d=ltm->tm_mday; - - - _cldrDate->select_month(m,y); - _cldrDate->select_day(d); - _popCalendar->show(); - } - - void enry_dhcp_mess(){ - string str_dhcp = this->call("/usr/bin/ubconfig get network NTPSERVERS"); - if ((str_dhcp!="") && (str_dhcp!="(null)")){ - _cbxSynchronizeNtp->set_active(1); - str_dhcp = str_dhcp.substr(str_dhcp.find("=")+1,str_dhcp.length()); - str_dhcp = str_dhcp.substr(0,str_dhcp.find("\n")); - if (str_dhcp=="dhcp"){ - _cbDhcp->set_active(0); - _txtNtpServer->set_sensitive(false); - //_cbxSynchronizeNtp->set_sensitive(true); - } - else{ - if (str_dhcp==""){ - _mess_dchp->show(); - } - else{ - _cbDhcp->set_active(1); - //_txtNtpServer->set_sensitive(true); - //_cbxSynchronizeNtp->set_sensitive(false); - _txtNtpServer->set_text(str_dhcp); - } - } - - - //_cbxDhcp->set_active(true); - this->set_ntp_toggle(); - } - else{ - _cbxSynchronizeNtp->set_active(0); - _cbDhcp->set_sensitive(0); - _txtNtpServer->set_sensitive(0); - - } - } - void get_config(){ - this->read_file(); - this->update_hour_minute(); - this->update_calendar(); - //this->event_entry_cbDhcp(); - this->enry_dhcp_mess(); - - string str_ntp = this->call("systemctl status ntpd.service systemd-timesyncd.service | grep \"Active: active\""); - if (str_ntp.length()<50) { - _cbxSynchronizeNtp->set_active(false); - } - else{ - _cbxSynchronizeNtp->set_active(true); - } - - string read_reg_zon_cfg = this->call("/usr/bin/ubconfig get clock ZONE"); - if ((read_reg_zon_cfg != "") && (read_reg_zon_cfg !="(null)")){ - read_reg_zon_cfg = read_reg_zon_cfg.substr(read_reg_zon_cfg.find("=")+1,read_reg_zon_cfg.length()); - read_reg_zon_cfg = read_reg_zon_cfg.substr(0,read_reg_zon_cfg.find("\n")); - string str_filling_reg = read_reg_zon_cfg.substr(0,read_reg_zon_cfg.find("/")); - string str_filling_zon = read_reg_zon_cfg.substr(read_reg_zon_cfg.find("/")+1,read_reg_zon_cfg.length()); - 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); - } - else { - this->append_region_zone("Asia","Omsk"); - - } - - } - - void 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 = "timedatectl set-timezone '" + str_region +"/" + str_zone+"'"; - system(cmd.c_str()); - } - else{ - str_zone=""; - str_region=""; - } - } - - void 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())){ - str_zoneGlob = zone_text.substr(zone_text.find(") ")+2,zone_text.length()); - str_region_glob = reg_text; - string cmd = "/usr/bin/ubconfig set clock ZONE=" +str_region_glob +"/" + str_zoneGlob; - system(cmd.c_str()); - } - else{ - str_zoneGlob=""; - str_region_glob=""; - } - } - - void set_ntp_toggle(){ - bool flag = _cbxSynchronizeNtp->get_active(); - if (flag_datetime==true){ - - } - else { - _numTimeHrs->set_sensitive(!flag); - _numTimeMin->set_sensitive(!flag); - _btnChooseDate->set_sensitive(!flag); - } - - //_txtDate->set_sensitive(!flag); - - //_btnUpdateDateTime->set_sensitive(!flag); - if (flag==false){ - system("systemctl --now disable systemd-timesyncd.service ntpd.service"); - } - else { - system("systemctl --now enable systemd-timesyncd.service"); - } - - } - - void 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); - if (text.first == region){ - _cbRegionGlob->set_active(index); - _cbRegion->set_active(index); - - } - index+=1; - } - index = 0; - string cheek_zone = ""; - for (const auto &_str_zone : time_reg_map.at(region)){ - _cbZone->append(_str_zone); - _cbZoneGlob->append(_str_zone); - if (_str_zone!=""){ - cheek_zone = _str_zone.substr(_str_zone.find(") ")+2,_str_zone.length()); - if (cheek_zone == zone){ - _cbZone->set_active(index); - _cbZoneGlob->set_active(index); - } - } - index+=1; - } - } - string call(string cmd){ - FILE *fp; - int status; - char path[PATH_MAX]; - fp = popen(cmd.c_str(), "r"); - if (fp == NULL){ - //return ""; - } - - while (fgets(path, PATH_MAX, fp) != NULL){ - printf("%s", path); - break; - - } - - status = pclose(fp); - if (status == -1) { - //return ""; - } - else { - //return ""; - } - return path; - } - - void append_zone(){ - Glib::ustring text = _cbRegion->get_active_text(); - _cbZone->remove_all(); - - if(!(text.empty())){ - str_region = text; - for (const auto &_str_zone : time_reg_map.at(str_region)){ - _cbZone->append(_str_zone); - } - } - - } - - void 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); - } - } - - } - void update_time_date(){ - this->enter_zone(); - bool flag = _cbxSynchronizeNtp->get_active(); - if (flag==false){ - hour = _numTimeHrs->get_value_as_int(); - minute = _numTimeMin->get_value_as_int(); - time_t now = time(0); - tm *ltm = localtime(&now); - string cmd = "timedatectl set-time \""+ - to_string(hour) + ":" + to_string(minute) - + ":" + to_string(ltm->tm_sec) +"\""; - system(cmd.c_str()); - if ((year==0) && (month == 0) && (day == 0)){ - time_t now = time(0); - tm *ltm = localtime(&now); - year=1900+ ltm->tm_year; - month=ltm->tm_mon; - day=ltm->tm_mday; - } - string str_month = ""; - string str_day = ""; - if (month <10){ - str_month = "0"+to_string(month+1); - } - else{ - str_month = to_string(month); - } - if (day <10){ - str_day = "0"+to_string(day); - } - else{ - str_day = to_string(day); - } - string cmd1 = "date --set=\"" +to_string(year)+ - str_month+str_day + " " +to_string(hour) + ":" + - to_string(minute)+"\""; - system(cmd1.c_str()); - } - - - } - void update_hour_minute(){ - time_t now = time(0); - tm *ltm = localtime(&now); - _numTimeHrs->set_value(ltm->tm_hour); - _numTimeMin->set_value(ltm->tm_min); - } - - void update_calendar(){ - time_t now = time(0); - tm *ltm = localtime(&now); - int year = 1900 + (int)ltm->tm_year; - string str_month = ""; - string str_day = ""; - unsigned int month, day; - month=ltm->tm_mon+1; - day=ltm->tm_mday; - if (month <=10){ - str_month = "0"+to_string(month); - } - else{ - str_month = to_string(month); - } - if (day <10){ - str_day = "0"+to_string(day); - } - else{ - str_day = to_string(day); - } - string date = str_day + '.' + str_month + '.' + to_string(year); - _txtDate->set_text(date); - } - void read_file(){ - std::string line; - namespace fs = std::filesystem; - for (const auto & entry : fs::directory_iterator("/usr/share/ubl-settings-datetime/reg/")){ - std::ifstream in(entry.path()); // окрываем файл для чтения - if (in.is_open()) - { - vector zone; - zone.push_back(""); - while (getline(in, line)) - { - zone.push_back(string(gettext(line.c_str()))); - } - string key_reg = entry.path().filename().string(); - key_reg = string(gettext(key_reg.c_str())); - time_reg_map.insert({key_reg, zone}); - } - in.close(); - } - } - -private: - Glib::RefPtr builder; - Gtk::Button *_btnUpdateDateTime; - Gtk::Button *_btnChooseDate; - Gtk::Button *_btnMessClose; - Gtk::ComboBoxText *_cbDhcp; - Gtk::SpinButton *_numTimeHrs; - Gtk::SpinButton *_numTimeMin; - Gtk::CheckButton *_cbxSynchronizeNtp; - - Gtk::Entry *_txtDate; - Gtk::ComboBoxText *_cbRegion; - Gtk::ComboBoxText *_cbRegionGlob; - Gtk::ComboBoxText *_cbZone; - Gtk::ComboBoxText *_cbZoneGlob; - Gtk::Entry *_txtNtpServer; - Gtk::MessageDialog *_mess_dchp; - Gtk::Popover *_popCalendar; - Gtk::Calendar *_cldrDate; - unsigned int year, month, day; - int hour; - int minute; - string str_region=""; - string str_region_glob=""; - string str_txtDate; - string str_txtNtpServer; - string str_zone=""; - string str_zoneGlob; - std::map > time_reg_map; - -}; - -class SettingsPlug : public Gtk::Plug -{ - -public: - Gtk::Window *window; - SettingsPlug(::Window p_socketID, Glib::RefPtr builder) - : Gtk::Plug{p_socketID} - { - - //MainWindow* wnd = nullptr; - //builder->get_widget_derived("window", wnd); - builder->get_widget("plugBox", plugBox); - plugBox->get_parent()->remove(*plugBox); - add(*plugBox); - - show_all_children(); - } - -private: - - Gtk::Widget *plugBox; - Gtk::Widget *parent; -}; - -void help(){ - g_print("Usage: ubl-settings-datetime [--lock-timezone] [--lock-ntp] [--lock-datetime] [--lock-update]\n"); - g_print("Options:\n"); - g_print(" --lock-timezone Disable timezone field editing\n"); - g_print(" --lock-ntp Disable ntp field editing\n"); - g_print(" --lock-datetime Disable datetime field editing\n"); - g_print(" --lock-update Disable save changes\n"); - cout << "Параметры командной строки --lock-datetime\n --lock-timezone\n --lock-ntp\n --lock-update\n" << endl; -} - - -int main(int argc, char* argv[]) { - - //path_app=filesystem::current_path(); - //path_app+="/"; - //cout << *argv[0] << endl; - //app_name=to_string(*argv[0]); - //app_name=app_name.substr(1, app_name.length()); - string str_cmd_argv = ""; - for (int i=1; iget_widget_derived("window", wnd); - auto r = app->run(*wnd); - - delete wnd; - return r; - } - else{ - Glib::init(); - setlocale(LC_ALL, ""); - Glib::OptionContext context; - - CmdArgParser parser{ - "Socket ID", - "Command line argument for socket ID communication.", - "No help available, sorry" - }; - - context.set_main_group(parser); - context.parse(argc, argv); - - ::Window socketID = parser.GetSocketID(); - - // Handle plug: - SettingsPlug plug{socketID, builder}; - plug.show(); - - app->run(plug); - - } - - -} \ No newline at end of file diff --git a/source/ubl_settings_datetime.cc b/source/ubl_settings_datetime.cc new file mode 100644 index 0000000..9d29f63 --- /dev/null +++ b/source/ubl_settings_datetime.cc @@ -0,0 +1,518 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "ubl_settings_datetime.h" +using namespace std; +string path_app= "/usr/bin/"; +string path_glade= "/usr/share/ubl-settings-datetime/ui/"; +string app_name = "ubl-settings-datetime"; +bool flag_datetime = false; +bool flag_timezone = false; +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::OptionEntry socketIDArg; + socketIDArg.set_long_name("socket-id"); + socketIDArg.set_short_name('s'); + socketIDArg.set_flags(Glib::OptionEntry::FLAG_IN_MAIN); + socketIDArg.set_description("Settings manager socket"); + + // Register it in the parser. It value will be recorded in m_socketID for later usage. + add_entry(socketIDArg, m_socketID); +} +::Window CmdArgParser::GetSocketID() const +{ + return m_socketID; +} +MainWindow::MainWindow(BaseObjectType* obj, Glib::RefPtr const& builder) + : Gtk::ApplicationWindow(obj) + , builder{builder} +{ + this->settings(); + +} +MainWindow::MainWindow(Glib::RefPtr const& builder) { + this->builder = builder; + this->settings(); +} + +void MainWindow::settings(){ + this->get_builder(); + _numTimeHrs->set_range(0,23); + _numTimeMin->set_range(0,59); + _numTimeHrs->set_increments(1.0,1.0); + _numTimeMin->set_increments(1.0,1.0); + _txtDate->set_sensitive(false); + this->get_config(); + this->flag_block_gui(); + this->event(); +} + +void MainWindow::flag_block_gui(){ + if (flag_datetime==true){ + _numTimeHrs->set_sensitive(false); + _numTimeMin->set_sensitive(false); + } + else if (flag_timezone==true){ + _cbRegion->set_sensitive(false); + _cbZone->set_sensitive(false); + _cbRegionGlob->set_sensitive(false); + _cbZoneGlob->set_sensitive(false); + _btnChooseDate->set_sensitive(false); + } + else if (flag_ntp==true){ + _cbxSynchronizeNtp->set_sensitive(false); + _cbDhcp->set_sensitive(false); + _txtNtpServer->set_sensitive(false); + } + else if (flag_update==true){ + _btnUpdateDateTime->set_sensitive(false); + } +} +void MainWindow::add_CSS(){ + Glib::RefPtr cssProvider = Gtk::CssProvider::create(); + cssProvider->load_from_path("style.css"); + Glib::RefPtr styleContext = Gtk::StyleContext::create(); + Glib::RefPtr 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 context = _btnUpdateDateTime->get_style_context(); + context->add_class("myButton"); +} +void MainWindow::get_builder(){ + builder->set_translation_domain(app_name); + builder->get_widget("btnUpdateDateTime", _btnUpdateDateTime); + builder->get_widget("btnChooseDate", _btnChooseDate); + builder->get_widget("btnMessClose", _btnMessClose); + builder->get_widget("cbxSynchronizeNtp", _cbxSynchronizeNtp); + builder->get_widget("popCalendar", _popCalendar); + builder->get_widget("numTimeHrs", _numTimeHrs); + builder->get_widget("numTimeMin", _numTimeMin); + builder->get_widget("cbDhcp", _cbDhcp); + builder->get_widget("txtDate", _txtDate); + builder->get_widget("cbRegion", _cbRegion); + builder->get_widget("cbRegionGlob", _cbRegionGlob); + builder->get_widget("cbZone", _cbZone); + builder->get_widget("cbZoneGlob", _cbZoneGlob); + builder->get_widget("txtNtpServer", _txtNtpServer); + builder->get_widget("cldrDate", _cldrDate); + builder->get_widget("mess_dchp", _mess_dchp); + //this->add_CSS(); +} +void MainWindow::event(){ + _btnMessClose->signal_clicked().connect(sigc::mem_fun(*this, &MainWindow::gui_mess_close)); + _cldrDate->signal_day_selected().connect(sigc::mem_fun(*this, &MainWindow::get_calendar)); + _cbxSynchronizeNtp->signal_toggled().connect(sigc::mem_fun(*this, &MainWindow::set_ntp_toggle)); + _btnUpdateDateTime->signal_clicked().connect(sigc::mem_fun(*this, &MainWindow::update_time_date)); + _btnChooseDate->signal_clicked().connect(sigc::mem_fun(*this, &MainWindow::calendar_show)); + _cbDhcp->signal_changed().connect(sigc::mem_fun(*this, &MainWindow::event_entry_cbDhcp)); + _cbRegion->signal_changed().connect(sigc::mem_fun(*this, &MainWindow::append_zone)); + _cbRegionGlob->signal_changed().connect(sigc::mem_fun(*this, &MainWindow::append_zone_glob)); + _cbZone->signal_changed().connect(sigc::mem_fun(*this, &MainWindow::enter_zone)); + _cbZoneGlob->signal_changed().connect(sigc::mem_fun(*this, &MainWindow::enter_zone_glob)); + _txtNtpServer->signal_focus_out_event().connect(sigc::mem_fun(*this, &MainWindow::focus_ntp)); +} + +void MainWindow::gui_mess_close(){ + _mess_dchp->hide(); +} + + +bool MainWindow::focus_ntp(GdkEventFocus* event){ + if (event!=NULL){} + if (_txtNtpServer->get_text()=="" || _txtNtpServer->get_text()==" "){ + _mess_dchp->show(); + } + else{ + string cmd = ""; + cmd = "/usr/bin/ubconfig set network NTPSERVERS=" + _txtNtpServer->get_text(); + this->call(cmd.c_str()); + } + + return true; +} + + + + +void MainWindow::event_entry_cbDhcp(){ + Glib::ustring str_dhcp = _cbDhcp->get_active_text(); + if (str_dhcp.length()!=0){ + if (str_dhcp=="DHCP"){ + string cmd = "/usr/bin/ubconfig set network NTPSERVERS=dhcp"; + system(cmd.c_str()); + _txtNtpServer->set_sensitive(false); + _cbDhcp->set_active(0); + _txtNtpServer->set_text(""); + } + else{ + string str_dhcp = this->call("/usr/bin/ubconfig get network NTPSERVERS"); + if ((str_dhcp!="") && (str_dhcp!="(null)") ){ + str_dhcp = str_dhcp.substr(str_dhcp.find("=")+1,str_dhcp.length()); + str_dhcp = str_dhcp.substr(0,str_dhcp.find("\n")); + if (str_dhcp!="dhcp"){ + _txtNtpServer->set_text(str_dhcp); + } + _txtNtpServer->set_sensitive(true); + _cbDhcp->set_active(1); + } + } + } + else{ + string cmd = "/usr/bin/ubconfig set network NTPSERVERS=dhcp"; + system(cmd.c_str()); + //_txtNtpServer->set_sensitive(false); + _cbDhcp->set_active(0); + } +} + +void MainWindow::get_calendar(){ + _cldrDate->get_date(year, month, day); + string str_month = ""; + string str_day = ""; + month+=1; + if (month <10){ + str_month = "0"+to_string(month); + } + else{ + str_month = to_string(month); + } + if (day <10){ + str_day = "0"+to_string(day); + } + else{ + str_day = to_string(day); + } + string date = str_day + '.' + str_month + '.' + to_string(year); + _txtDate->set_text(date); + } + + +void MainWindow::calendar_show(){ + time_t now = time(0); + tm *ltm = localtime(&now); + unsigned int y=1900+ ltm->tm_year; + unsigned int m=ltm->tm_mon; + unsigned int d=ltm->tm_mday; + _cldrDate->select_month(m,y); + _cldrDate->select_day(d); + _popCalendar->show(); +} + +void MainWindow::enry_dhcp_mess(){ + string str_dhcp = this->call("/usr/bin/ubconfig get network NTPSERVERS"); + if ((str_dhcp!="") && (str_dhcp!="(null)")){ + _cbxSynchronizeNtp->set_active(1); + str_dhcp = str_dhcp.substr(str_dhcp.find("=")+1,str_dhcp.length()); + str_dhcp = str_dhcp.substr(0,str_dhcp.find("\n")); + if (str_dhcp=="dhcp"){ + _cbDhcp->set_active(0); + _txtNtpServer->set_sensitive(false); + //_cbxSynchronizeNtp->set_sensitive(true); + } + else{ + if (str_dhcp==""){ + _mess_dchp->show(); + } + else{ + _cbDhcp->set_active(1); + //_txtNtpServer->set_sensitive(true); + //_cbxSynchronizeNtp->set_sensitive(false); + _txtNtpServer->set_text(str_dhcp); + } + } + //_cbxDhcp->set_active(true); + this->set_ntp_toggle(); + } + else{ + _cbxSynchronizeNtp->set_active(0); + _cbDhcp->set_sensitive(0); + _txtNtpServer->set_sensitive(0); + } +} +void MainWindow::get_config(){ + this->read_file(); + this->update_hour_minute(); + this->update_calendar(); + //this->event_entry_cbDhcp(); + this->enry_dhcp_mess(); + string str_ntp = this->call("systemctl status ntpd.service systemd-timesyncd.service | grep \"Active: active\""); + if (str_ntp.length()<50) { + _cbxSynchronizeNtp->set_active(false); + } + else{ + _cbxSynchronizeNtp->set_active(true); + } + + string read_reg_zon_cfg = this->call("/usr/bin/ubconfig get clock ZONE"); + if ((read_reg_zon_cfg != "") && (read_reg_zon_cfg !="(null)")){ + read_reg_zon_cfg = read_reg_zon_cfg.substr(read_reg_zon_cfg.find("=")+1,read_reg_zon_cfg.length()); + read_reg_zon_cfg = read_reg_zon_cfg.substr(0,read_reg_zon_cfg.find("\n")); + string str_filling_reg = read_reg_zon_cfg.substr(0,read_reg_zon_cfg.find("/")); + string str_filling_zon = read_reg_zon_cfg.substr(read_reg_zon_cfg.find("/")+1,read_reg_zon_cfg.length()); + 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); + } + else { + this->append_region_zone("Asia","Omsk"); + } +} + +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 = "timedatectl set-timezone '" + str_region +"/" + str_zone+"'"; + system(cmd.c_str()); + } + else{ + str_zone=""; + str_region=""; + } +} + +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())){ + str_zoneGlob = zone_text.substr(zone_text.find(") ")+2,zone_text.length()); + str_region_glob = reg_text; + string cmd = "/usr/bin/ubconfig set clock ZONE=" +str_region_glob +"/" + str_zoneGlob; + system(cmd.c_str()); + } + else{ + str_zoneGlob=""; + str_region_glob=""; + } +} + +void MainWindow::set_ntp_toggle(){ + bool flag = _cbxSynchronizeNtp->get_active(); + if (flag_datetime==true){ + + } + else { + _numTimeHrs->set_sensitive(!flag); + _numTimeMin->set_sensitive(!flag); + _btnChooseDate->set_sensitive(!flag); + } + //_txtDate->set_sensitive(!flag); + //_btnUpdateDateTime->set_sensitive(!flag); + if (flag==false){ + system("systemctl --now disable systemd-timesyncd.service ntpd.service"); + } + else { + system("systemctl --now enable systemd-timesyncd.service"); + } +} + +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); + if (text.first == region){ + _cbRegionGlob->set_active(index); + _cbRegion->set_active(index); + } + index+=1; + } + index = 0; + string cheek_zone = ""; + for (const auto &_str_zone : time_reg_map.at(region)){ + _cbZone->append(_str_zone); + _cbZoneGlob->append(_str_zone); + if (_str_zone!=""){ + cheek_zone = _str_zone.substr(_str_zone.find(") ")+2,_str_zone.length()); + if (cheek_zone == zone){ + _cbZone->set_active(index); + _cbZoneGlob->set_active(index); + } + } + index+=1; + } + +} +string MainWindow::call(string cmd){ + FILE *fp; + int status; + char path[PATH_MAX]; + fp = popen(cmd.c_str(), "r"); + if (fp == NULL){ + //return ""; + } + + while (fgets(path, PATH_MAX, fp) != NULL){ + printf("%s", path); + break; + } + + status = pclose(fp); + if (status == -1) { + //return ""; + } + else { + //return ""; + } + return path; +} + +void MainWindow::append_zone(){ + Glib::ustring text = _cbRegion->get_active_text(); + _cbZone->remove_all(); + + if(!(text.empty())){ + str_region = text; + for (const auto &_str_zone : time_reg_map.at(str_region)){ + _cbZone->append(_str_zone); + } + } + +} + +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); + } + } +} +void MainWindow::update_time_date(){ + this->enter_zone(); + bool flag = _cbxSynchronizeNtp->get_active(); + if (flag==false){ + hour = _numTimeHrs->get_value_as_int(); + minute = _numTimeMin->get_value_as_int(); + time_t now = time(0); + tm *ltm = localtime(&now); + string cmd = "timedatectl set-time \""+ + to_string(hour) + ":" + to_string(minute) + + ":" + to_string(ltm->tm_sec) +"\""; + system(cmd.c_str()); + if ((year==0) && (month == 0) && (day == 0)){ + time_t now = time(0); + tm *ltm = localtime(&now); + year=1900+ ltm->tm_year; + month=ltm->tm_mon; + day=ltm->tm_mday; + } + string str_month = ""; + string str_day = ""; + if (month <10){ + str_month = "0"+to_string(month+1); + } + else{ + str_month = to_string(month); + } + if (day <10){ + str_day = "0"+to_string(day); + } + else{ + str_day = to_string(day); + } + string cmd1 = "date --set=\"" +to_string(year)+ + str_month+str_day + " " +to_string(hour) + ":" + + to_string(minute)+"\""; + system(cmd1.c_str()); + } +} +void MainWindow::update_hour_minute(){ + time_t now = time(0); + tm *ltm = localtime(&now); + _numTimeHrs->set_value(ltm->tm_hour); + _numTimeMin->set_value(ltm->tm_min); +} + +void MainWindow::update_calendar(){ + time_t now = time(0); + tm *ltm = localtime(&now); + int year = 1900 + (int)ltm->tm_year; + string str_month = ""; + string str_day = ""; + unsigned int month, day; + month=ltm->tm_mon+1; + day=ltm->tm_mday; + if (month <=10){ + str_month = "0"+to_string(month); + } + else{ + str_month = to_string(month); + } + if (day <10){ + str_day = "0"+to_string(day); + } + else{ + str_day = to_string(day); + } + string date = str_day + '.' + str_month + '.' + to_string(year); + _txtDate->set_text(date); +} + +void MainWindow::read_file(){ + std::string line; + namespace fs = std::filesystem; + for (const auto & entry : fs::directory_iterator("/usr/share/ubl-settings-datetime/reg/")){ + std::ifstream in(entry.path()); // окрываем файл для чтения + if (in.is_open()) + { + vector zone; + zone.push_back(""); + while (getline(in, line)) + { + zone.push_back(string(gettext(line.c_str()))); + } + string key_reg = entry.path().filename().string(); + key_reg = string(gettext(key_reg.c_str())); + time_reg_map.insert({key_reg, zone}); + } + in.close(); + } +} + + +SettingsPlug::SettingsPlug(::Window p_socketID, Glib::RefPtr builder) +: Gtk::Plug{p_socketID} +{ + //MainWindow* wnd = nullptr; + //builder->get_widget_derived("window", wnd); + builder->get_widget("plugBox", plugBox); + plugBox->get_parent()->remove(*plugBox); + add(*plugBox); + show_all_children(); +} + +void help(){ + g_print("Usage: ubl-settings-datetime [--lock-timezone] [--lock-ntp] [--lock-datetime] [--lock-update]\n"); + g_print("Options:\n"); + g_print(" --lock-timezone Disable timezone field editing\n"); + g_print(" --lock-ntp Disable ntp field editing\n"); + g_print(" --lock-datetime Disable datetime field editing\n"); + g_print(" --lock-update Disable save changes\n"); + cout << "Параметры командной строки --lock-datetime\n --lock-timezone\n --lock-ntp\n --lock-update\n" << endl; +} \ No newline at end of file diff --git a/source/ubl_settings_datetime.h b/source/ubl_settings_datetime.h new file mode 100644 index 0000000..2cbc199 --- /dev/null +++ b/source/ubl_settings_datetime.h @@ -0,0 +1,132 @@ +#ifndef UBL_SETTINGS_DATETIME_H +#define UBL_SETTINGS_DATETIME_H +//g++ -O2 -std=c++20 -lstdc++-c ubl-settings-datetime.h ubl-settings-datetime.cc `pkg-config --cflags --libs gtkmm-3.0` -Wl,-export-dynamic -g +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +// g++ -O2 -std=c++20 -lstdc++ -o main.cc `pkg-config --cflags --libs gtkmm-3.0` -Wl,-export-dynamic -g +extern string path_app; +extern string path_glade; +extern string app_name; +extern bool flag_datetime; +extern bool flag_timezone; +extern bool flag_ntp; +extern bool flag_update; + + +class CmdArgParser : public Glib::OptionGroup +{ + +public: + + CmdArgParser(const std::string& p_name, const std::string& p_description, const std::string& p_help); + + + // Override this to handle errors. I skipped it for simplicity. + // void on_error(Glib::OptionContext& context, const Glib::Error& error) override; + + ::Window GetSocketID() const; + +private: + + int m_socketID = 0; + +}; + + + +class MainWindow : public Gtk::ApplicationWindow { +public: + MainWindow(BaseObjectType* obj, Glib::RefPtr const& builder); + MainWindow(Glib::RefPtr const& builder); + virtual ~MainWindow() = default; + void settings(); + void flag_block_gui(); + void add_CSS(); + void get_builder(); + void event(); + void gui_mess_close(); + bool focus_ntp(GdkEventFocus* event); + void event_entry_cbDhcp(); + void get_calendar(); + void calendar_show(); + void enry_dhcp_mess(); + void get_config(); + void enter_zone(); + void enter_zone_glob(); + void set_ntp_toggle(); + void append_region_zone(string region, string zone); + string call(string cmd); + void append_zone(); + void append_zone_glob(); + void update_time_date(); + void update_hour_minute(); + void update_calendar(); + void read_file(); + +private: + Glib::RefPtr builder; + Gtk::Button *_btnUpdateDateTime; + Gtk::Button *_btnChooseDate; + Gtk::Button *_btnMessClose; + Gtk::ComboBoxText *_cbDhcp; + Gtk::SpinButton *_numTimeHrs; + Gtk::SpinButton *_numTimeMin; + Gtk::CheckButton *_cbxSynchronizeNtp; + + Gtk::Entry *_txtDate; + Gtk::ComboBoxText *_cbRegion; + Gtk::ComboBoxText *_cbRegionGlob; + Gtk::ComboBoxText *_cbZone; + Gtk::ComboBoxText *_cbZoneGlob; + Gtk::Entry *_txtNtpServer; + Gtk::MessageDialog *_mess_dchp; + Gtk::Popover *_popCalendar; + Gtk::Calendar *_cldrDate; + unsigned int year, month, day; + int hour; + int minute; + string str_region=""; + string str_region_glob=""; + string str_txtDate; + string str_txtNtpServer; + string str_zone=""; + string str_zoneGlob; + std::map > time_reg_map; + +}; + +class SettingsPlug : public Gtk::Plug +{ + +public: + Gtk::Window *window; + SettingsPlug(::Window p_socketID, Glib::RefPtr builder); +private: + + Gtk::Widget *plugBox; + Gtk::Widget *parent; +}; + +void help(); +#endif diff --git a/ubl-settings-datetime.glade b/ubl-settings-datetime.glade index 6087d6f..98c3482 100644 --- a/ubl-settings-datetime.glade +++ b/ubl-settings-datetime.glade @@ -10,6 +10,7 @@ False + usr/share/ubl-settings-datetime/images/ubl-settings-datetime.png dialog @@ -21,6 +22,9 @@ False True end + + + @@ -28,16 +32,19 @@ True True True + 5 + 5 + 5 + 5 + 6 + 6 True True - 0 + 1 - - - False @@ -49,11 +56,17 @@ True False + 6 + 90 True False - 50 + 5 + 5 + 5 + 5 + 60 gtk-dialog-warning @@ -66,6 +79,10 @@ True False + 6 + 5 + 5 + 5 Введите DHCP @@ -76,7 +93,7 @@ - False + True True 2 @@ -87,7 +104,7 @@ False Настройки даты и времени - /usr/share/ubl-settings-datetime/images/ubl-settings-datetime.png + usr/share/ubl-settings-datetime/images/ubl-settings-datetime.png True @@ -108,7 +125,7 @@ 5 6 6 - /usr/share/ubl-settings-datetime/images/ubl-settings-datetime.png + usr/share/ubl-settings-datetime/images/ubl-settings-datetime.png False