#include "Sources.hpp" #include "Commands.hpp" #include "Cache.hpp" #include "Graphics.hpp" #include #include #include #include #include #include #include #include #include #include #define T(String) gettext(String) using namespace std; using namespace std::placeholders; using namespace net::lliurex::apt; using namespace net::lliurex::apt::graphics; class Application { private: Glib::RefPtr glade; //Main window Gtk::Window * winMain; Gtk::Viewport * vpPreview; Gtk::Box * boxA; Gtk::Box * boxB; Gtk::Box * boxC; Gtk::Button * btnOk; Gtk::Button * btnCancel; Gtk::Button * btnUpdateCancel; Gtk::CheckButton * chkWipeSources; Gtk::Box * boxPreview; Gtk::Box * boxChooser; //Update window Gtk::Window * winUpdate; Gtk::ProgressBar * pbarUpdate; Gtk::TextView * txtUpdate; Gtk::Box * boxStep1; Gtk::Paned * panedStep2; Gtk::RadioButton * rbCustom; vector conf; Sources sources_list; Sources sources_current; Cache cache; vector > bubbles; vector > toggleButtons; string locale; public: Application() { locale = getenv("LANG"); string glade_path; glade=Gtk::Builder::create_from_resource("/net/lliurex/apt/interface.ui"); glade->get_widget("winMain",winMain); glade->get_widget("vpPreview",vpPreview); glade->get_widget("boxA",boxA); glade->get_widget("boxB",boxB); glade->get_widget("boxC",boxC); glade->get_widget("btnOk",btnOk); glade->get_widget("btnCancel",btnCancel); glade->get_widget("btnUpdateCancel",btnUpdateCancel); glade->get_widget("chkWipeSources",chkWipeSources); glade->get_widget("winUpdate",winUpdate); glade->get_widget("pbarUpdate",pbarUpdate); glade->get_widget("txtUpdate",txtUpdate); //connecting to events winMain->signal_delete_event().connect(sigc::mem_fun(*this,&Application::OnWinMainClose)); chkWipeSources->signal_toggled().connect(sigc::mem_fun(*this,&Application::OnToggle)); //btnUpdate->signal_clicked().connect(sigc::mem_fun(*this,&Application::OnUpdateClick)); btnOk->signal_clicked().connect(sigc::mem_fun(*this,&Application::OnOkClick)); btnCancel->signal_clicked().connect(sigc::mem_fun(*this,&Application::OnCancelClick)); btnUpdateCancel->signal_clicked().connect(sigc::mem_fun(*this,&Application::OnUpdateCancelClick)); if (Gio::File::create_for_path(Sources::SourcesList)->query_exists()){ sources_list = Sources(Sources::SourcesList); } conf = command::LoadConf(); vector status = command::Check(sources_list,conf); map source_status; Sources * mandatory = nullptr; int analysis=0; //0-unknown, 1-detected, 2-inconsistent for(command::Match match : status) { if(match.status==FindLineStatus::NotFound)continue; if(!match.sources->optional && match.status==FindLineStatus::Found) { if(mandatory==nullptr) { mandatory=match.sources; analysis=1; } else { analysis=2; } } string name=GetName(match.sources->name); if(source_status.find(name)==source_status.end()) { source_status[name]=match.status; } else { FindLineStatus s = source_status[name]; //Partial status cannot overwrite Found status if(s==FindLineStatus::Partial && match.status==FindLineStatus::Found) { source_status[name]=FindLineStatus::Found; } } } for(pair p : source_status) { string text; if(p.second!=FindLineStatus::Found) continue; text=T("Found: "); text=text+"["+p.first+"]"; Pango::FontDescription font("Ubuntu 11.0"); Gtk::Box * box = new Gtk::Box(Gtk::Orientation::ORIENTATION_HORIZONTAL); Gtk::Arrow * arrow = new Gtk::Arrow(Gtk::ArrowType::ARROW_RIGHT,Gtk::ShadowType::SHADOW_OUT); box->pack_start(*arrow,false,false); Gtk::Label *label = new Gtk::Label(text); label->override_font(font); label->set_justify(Gtk::Justification::JUSTIFY_LEFT); label->set_alignment(0.0,0.5); label->set_margin_top(5); label->set_margin_bottom(5); box->pack_start(*label,false,false); boxC->pack_start(*box,false,false); } //source view boxPreview = new Gtk::Box(Gtk::Orientation::ORIENTATION_VERTICAL); vpPreview->add(*boxPreview); //chooser view boxChooser = boxA; Gtk::RadioButtonGroup group; rbCustom = new Gtk::RadioButton(T("Custom")); rbCustom->set_group(group); boxChooser->pack_start(*rbCustom,false,false); //toggleButtons.push_back(make_pair(rbc,&sources_list)); Gtk::RadioButton * rbn = new Gtk::RadioButton(T("None")); rbn->set_group(group); boxChooser->pack_start(*rbn,false,false); rbn->signal_toggled().connect(sigc::mem_fun(*this,&Application::OnToggle)); for(int n=0;nset_group(group); boxChooser->pack_start(*rb,false,false); rb->signal_toggled().connect(sigc::mem_fun(*this,&Application::OnToggle)); toggleButtons.push_back(make_pair(rb,&conf[n])); } } for(int n=0;npack_start(*cb,false,false); cb->signal_toggled().connect(sigc::mem_fun(*this,&Application::OnToggle)); toggleButtons.push_back(make_pair(cb,&conf[n])); } } //connecto to cache hooks function p1 = std::bind(&Application::UpdateProgress,this,_1,_2); function p2 = std::bind(&Application::UpdateReady,this); cache=Cache(p1,p2); //force an initial update OnToggle(); winMain->show_all(); } ~Application() { } /*! Window close event */ bool OnWinMainClose(GdkEventAny* event) { Gtk::Main::quit(); return true; } /*! Toggle/Check button pressed */ void OnToggle() { if(rbCustom->get_active()) { sources_current = sources_list; for(int n=0;nget_active()) { sources_current = sources_current + *(toggleButtons[n].second); } } } else { vector targets; for(int n=0;nget_active()) { targets.push_back(toggleButtons[n].second->name["C"]); } } sources_current = command::Set(sources_list,conf,targets,chkWipeSources->get_active()); } cout<show_all(); cache.Update(); } void OnUpdateCancelClick() { cout<<"Update canceled by user"<hide(); } /*! Cancel pressed, just quit */ void OnCancelClick() { Gtk::Main::quit(); } /*! Does a best effort looking for a translated string */ string GetName(map & name) { string ret=""; map::iterator it; it=name.find(locale); if(it!=name.end()) { ret=it->second; } else { it=name.find("C"); if(it!=name.end()) { ret=it->second; } } return ret; } /*! Build bubble source preview */ void BuildPreview() { //clear current bubbles, if any for(int n=0;nremove(*bubbles[n].first); delete bubbles[n].first; delete bubbles[n].second; } bubbles.clear(); //rebuild images for(int n=0;nset(line->surface); img->set_alignment(0.0,0.5); boxPreview->pack_start(*img,false,false); bubbles.push_back(make_pair(img,line)); } winMain->show_all(); } /*! Update progress callback */ void UpdateProgress(int progress,string details) { //cout<<"["<set_fraction(fraction); string line = details + "\n"; Gtk::TextIter it = txtUpdate->get_buffer()->end(); it=txtUpdate->get_buffer()->insert(it,line); txtUpdate->scroll_to(it); } /*! Update completed... and quit */ void UpdateReady() { cout<<"Update complete"<set_fraction(1.0); //btnUpdate->set_sensitive(true); Gtk::MessageDialog* dialog = new Gtk::MessageDialog(T("Update is succesful"), false, Gtk::MESSAGE_INFO, Gtk::BUTTONS_CLOSE,true); dialog->set_secondary_text(T("Software sources has been updated")); dialog->run(); delete dialog; Gtk::Main::quit(); } }; int main(int argc,char * argv[]) { textdomain("lliurex-apt"); Gtk::Main kit(argc, argv); Application app; Gtk::Main::run(); return 0; }