⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 svcprowindow.cpp

📁 j2me is based on j2mepolish, client & server for mobile application.
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	logger.post(new event_type(event_logger::info, L"Building new list..."));
	std::set<nt_service::service_settings> services;

	enum_services_sequence ess(sc, SC_ENUM_PROCESS_INFO, SERVICE_WIN32, SERVICE_STATE_ALL);
	
	for (enum_services_sequence::const_iterator i = ess.begin(), e = ess.end(); i != e; ++i)
	{
	
	try
	{
		nt_service svc(*i, SERVICE_QUERY_STATUS|SERVICE_QUERY_CONFIG);

		services.insert(nt_service::service_settings(svc));
	}
	catch(const nt_service_exception& e)
	{
		std::wstring e_str = nt_service_erorr_to_string(e.error());

		logger.post(new event_type(event_logger::critical, 
			wformat(L"Error: %1%! Service: %2%.") % e_str % e.lpServiceName()));
	}
	catch(const std::exception& e)
	{
		aux::wlog() << boost::wformat(L"save_backup_file std::exception: %1%") % e.what();
	}

	}
	
	logger.post(new event_type(event_logger::info, L"List complete."));

	std::stringstream xml_data;	
	{
	aux::xml::txml_oarchive oxml(xml_data);	
	oxml << boost::serialization::make_nvp("services", services);
	}

	adapter.save_stream_data(xml_data);
	service_ini.save_data();

	logger.post(new event_type(event_logger::info, L"Backup List saved!"));


	// Add backup to list of profiles also

	std::wstring time = boost::lexical_cast<std::wstring>(
		boost::posix_time::second_clock::universal_time());

	std::wstring name = (boost::wformat(L"Backup %1%") % time).str();

	size_t i = profiles_.new_profile(name, -1, true);
	WTL::CTreeItem new_file = tree_view_.InsertItem(name.c_str(), TVI_ROOT, TVI_LAST);

	new_file.SetData(i); 
	active_profile_ = profiles_.get_profile(i);

	active_profile_->set_saved(true);
	
	foreach(nt_service::service_settings& setting, services)
		active_profile_->settings().push_back(setting);

	profiles_.save_profile(active_profile_);
	update_title();

	return;
}

void SvcProWindow::load_backup_file(boost::filesystem::wpath filename)
{
	aux::txml_ini service_ini(filename);
	aux::txml_ini_adapter adapter("services_backup", service_ini);
	
	if (!service_ini.load_data())
	{
		logger.post(new event_type(event_logger::critical, L"Error loading backup file!"));
		return;
	}

	std::set<nt_service::service_settings> settings;
	std::stringstream xml_data;	

	if (!adapter.load_stream_data(xml_data))
	{
		logger.post(new event_type(event_logger::critical, L"Parse error with the backup file!"));
		return;
	}

	{
	aux::xml::txml_iarchive ixml(xml_data);				
	ixml >> boost::serialization::make_nvp("services", settings);
	}

	logger.post(new event_type(event_logger::info, wformat(L"Loaded backup file, %1% enteries.") % settings.size()));
	
	UISetText(1, L"Restoring Backup");	

	boost::thread(boost::bind(&SvcProWindow::apply_profile_settings, this, settings));
}

void SvcProWindow::OnLoadBackup(UINT, int, HWND)
{
	CSSFileDialog dlgOpen(true, NULL, NULL, OFN_HIDEREADONLY, 
		L"ServiceProfile Backup. (*.spbxml)|*.spbxml|All Files (*.*)|*.*|", m_hWnd);

	if (dlgOpen.DoModal() == IDOK) 
	{
		load_backup_file(std::wstring(dlgOpen.m_ofn.lpstrFile));
	}
}

void SvcProWindow::OnImport(UINT, int, HWND)
{
	CSSFileDialog dlgOpen(true, NULL, NULL, OFN_HIDEREADONLY, 
		L"ServiceProfile (*.spxml)|*.spxml|All Files (*.*)|*.*|", m_hWnd);

	if (dlgOpen.DoModal() == IDOK) 
	{
		profiles_.load_profile(std::wstring(dlgOpen.m_ofn.lpstrFile), true);
	}
}

void SvcProWindow::OnExport(UINT, int, HWND)
{
	ExportTreeDlg tree_dlg(tree_view_);
	tree_dlg.DoModal();

	std::set<size_t> set = tree_dlg.get_data_set();

	if (!set.empty())
	{
		logger.post(new event_type(event_logger::info, 
			boost::wformat(L"%1% profiles selected.") % set.size()));

		CSSFileDialog dlgOpen(false, NULL, NULL, OFN_HIDEREADONLY, 
			L"ServiceProfile (*.spxml)|*.spxml|All Files (*.*)|*.*|", m_hWnd);

		if (dlgOpen.DoModal() == IDOK) 
		{
			boost::filesystem::wpath filepath = std::wstring(dlgOpen.m_ofn.lpstrFile);

			std::wstring filename = filepath.leaf();
			if (!boost::algorithm::contains(filename, L"."))
				filename += L".spxml";

			profiles_.export_profile_set(filename, set);
		}
	}
}

void SvcProWindow::scan_and_load(boost::filesystem::wpath directory)
{
	namespace fs = boost::filesystem;
	namespace xp = boost::xpressive;

	if (fs::is_directory(directory))
	{		
		for (fs::wdirectory_iterator i(directory), e;
				i != e; ++i )
		{
			if (fs::is_regular(i->status()))
			{
				xp::wsregex rex = xp::wsregex::compile(L".+\\.spxml", xp::regex_constants::icase);
				xp::wsmatch what;

				if(xp::regex_match(i->path().leaf(), what, rex))
				{
					logger.post(new event_type(event_logger::info, 
						boost::wformat(L"Loading profile %1%") % i->path().leaf()));

					profiles_.load_profile(i->path());
				}
			}
		}
	}
}

void SvcProWindow::profile_loaded(const std::wstring& name, size_t item, size_t parent)
{
	tree_view_.insert_item(name, item, parent);
}

void SvcProWindow::build_inherted_settings(nt_service_profile_ptr p_ptr, 
		std::set<nt_service::service_settings>& service_settings_set)
{
	foreach(nt_service::service_settings& settings, p_ptr->settings())
	{
		std::pair<std::set<nt_service::service_settings>::iterator, bool> i = 
				service_settings_set.insert(settings);

		i.first->set_inherited(false);
	}

	while(p_ptr->parent())
	{
		p_ptr = profiles_.get_profile(*(p_ptr->parent()));

		foreach(nt_service::service_settings& settings, p_ptr->settings())
		{
			std::pair<std::set<nt_service::service_settings>::iterator, bool> i = 
				service_settings_set.insert(settings);

			if (i.second) i.first->set_inherited();
		}		
	}
}

void SvcProWindow::active_profile_selected(size_t i)
{
	if (active_profile_ && !active_profile_->saved() && 
		MessageBox(L"Do you want to save the current profile before switching to another?", 
		L"ServiceProfiles", MB_YESNO) == IDYES)
	{
		OnFileSave(0,0,0);
	}

	active_profile_ = profiles_.get_profile(i);
	update_display();
	//boost::thread(bind(&SvcProWindow::update_display, this));
}

void SvcProWindow::update_display()
{
	sort_list_.set_overview_mode(false);

	std::set<nt_service::service_settings> service_settings_set;
	build_inherted_settings(active_profile_, service_settings_set);

	sort_list_.update_with_settings(service_settings_set);

	active_profile_->set_saved(true);
	update_title();
}

void SvcProWindow::update_title()
{
	std::wstring title = L"Service Profiles";

	if (active_profile_)
	{
		if (active_profile_->saved())
			title += (boost::wformat(L" <%1%>") % active_profile_->name()).str();
		else
			title += (boost::wformat(L" <%1%*>") % active_profile_->name()).str();

		if (active_profile_->readonly())			
			title += L" Readonly!";
	}
	else if (sort_list_.overview_mode())
	{
		title += L" - Overview Mode";
	}

	SetWindowText(title.c_str());
}

void SvcProWindow::listview_items_selected(std::vector<service_ptr> vec)
{
	if (!vec.empty()) tabbed_dialog_.display_service(vec.front());
}

void SvcProWindow::listview_setting_changed(size_t index)
{
	if (active_profile_ && !active_profile_->readonly()) 
	{
		active_profile_->set_saved(false);
		update_title();
	}
	else if (active_profile_ && active_profile_->readonly())
	{
		sort_list_.reset_display_services();
		new_profile(WTL::CTreeItem());
	}
	else
	{
		new_profile(WTL::CTreeItem());
	}

	sort_list_.set_overview_mode(false);
	update_title();
}

void SvcProWindow::delete_profile(size_t index)
{
	profiles_.delete_profile(index);
}

void SvcProWindow::apply_settings_thread(service_ptr svc_ptr, 
		std::set<nt_service::service_settings>::const_iterator ss_i)
{
	try
	{
	
	svc_ptr->ApplySettings(*ss_i);

	logger.post(new event_type(event_logger::info, 
		wformat(L"Succeded applying service \"%1%\" settings.") % svc_ptr->lpServiceName()));
		
	}
	catch(const nt_service_exception& e)
	{
		std::wstring e_str = nt_service_erorr_to_string(e.error());

		logger.post(new event_type(event_logger::critical, 
			wformat(L"Error: %1%! Service: %2%.") % e_str % svc_ptr->lpServiceName()));
	}
	catch(const std::exception& e)
	{
		logger.post(new event_type(event_logger::critical, 
			wformat(L"Exception: %1%! Service: %2%.") % aux::from_utf8_safe(e.what()) % svc_ptr->lpServiceName()));
	}
}

void SvcProWindow::apply_profile_settings(std::set<nt_service::service_settings> settings)
{
	if (!settings.empty())
	{
		boost::threadpool::pool tp(4);

		nt_service_control_manager_ptr 
			sc(new nt_service_control_manager (*(settings.begin())->manager()));

		for (std::set<nt_service::service_settings>::const_iterator 
			i = settings.begin(), e = settings.end(); i != e; ++i)
		{		
			try
			{	

			service_ptr svc_ptr(new nt_service(sc, *i, SERVICE_QUERY_STATUS|SERVICE_QUERY_CONFIG));

			if (!(*i).compare_state(*svc_ptr))
			{
				logger.post(new event_type(event_logger::info, 
					wformat(L"Service %1% needs settings changed.") % svc_ptr->lpServiceName()));

				tp.schedule(boost::bind(&SvcProWindow::apply_settings_thread, this, svc_ptr, i));
			}

			}
			catch(const nt_service_exception& e)
			{
				std::wstring e_str = nt_service_erorr_to_string(e.error());

				logger.post(new event_type(event_logger::critical, 
					wformat(L"Error: %1%! Service: %2%.") % e_str % i->lpServiceName()));
			}
			catch(const std::exception& e)
			{
				aux::wlog() << boost::wformat(L" !! std::exception: %1%") % e.what();
			}
		}
		
		tp.wait();
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -