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

📄 process.cpp

📁 Linux/Windows 环境下跨平台程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/* Copyright (C) 2004 Per Johansson		This file is part of netGo.    netGo is free software; you can redistribute it and/or modify    it under the terms of the GNU General Public License as published by    the Free Software Foundation; either version 2 of the License, or    (at your option) any later version.    netGo is distributed in the hope that it will be useful,    but WITHOUT ANY WARRANTY; without even the implied warranty of    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    GNU General Public License for more details.    You should have received a copy of the GNU General Public License    along with netGo; if not, write to the Free Software    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA*/#include <qprocess.h>#include <qstringlist.h>#include <qtimer.h>#include <qmessagebox.h>#include <qregexp.h>#include <qstring.h>#include <qtextbrowser.h>#include <qpushbutton.h>#include "process.h"#include "profile.h"#include "settings.h"// Default constructor for class Process.Process::Process(QTextBrowser *procBrowser, QPushButton *okButton){	// Save the textbrowser in a variable that all the member-methods can access.	browser = procBrowser;		// Save the ok-button in variables that all the member-methods can acces.	okBut = okButton;		// Set the dhcp-error variable to 0.	DHCP_ERROR = 0;		// Bash escape sequences for coloured text.	blue = "\e[0;34m";	BLUE = "\e[1;34m";	green = "\e[0;32m";	GREEN = "\e[1;32m";	red = "\e[0;31m";	RED = "\e[1;31m";	magenta = "\e[0;35m";	MAGENTA = "\e[1;35m";	cyan = "\e[0;36m";	CYAN = "\e[1;36m";	normal = "\e[0m";	NORMAL = "\e[1m";}/*------------------------------------------      Functions for exiting a process.------------------------------------------*/// Function for a safe exit.void Process::cleanUp(){	// Mark that no process is running at the moment.	CURRENT_PROC = NONE;	// Enable the Ok-button again to show that we're done.	okBut->setEnabled(1);	okBut->setFocus();		// Show the command line user that we're done.	qWarning("\n" + NORMAL + "- Done!" + normal);		// Emit a signal to show that the processes are finished.	emit procsFinished();}// Function that is called when user press the Cancel-button.// It terminates the current network-process.void Process::terminateProc(){	// Find out which process that were running when the user clicked Cancel,	// and try to terminate that one.	switch (CURRENT_PROC) {		case NONE:			// Do nothing, this means that there was no process running.			qWarning("No process were running..");			break;		case IFACEDOWN:			ifaceDownProc->tryTerminate();			QTimer::singleShot( 1000, ifaceDownProc, SLOT( kill() ) );			qWarning("Killed interfaceDownProc..");			break;		case KILLDHCP:			killDhcpProc->tryTerminate();			QTimer::singleShot( 1000, killDhcpProc, SLOT( kill() ) );				qWarning("Killed killDhcpProc...");			break;		case DHCP:			dhcpProc->tryTerminate();			QTimer::singleShot( 1000, dhcpProc, SLOT( kill() ) );				qWarning("Killed dhcpProc..");			break;		case FETCHIP:			fetchIpProc->tryTerminate();			QTimer::singleShot( 1000, fetchIpProc, SLOT( kill() ) );			qWarning("Killed fetchIpProc..");			break;		case STATICIP:			ipProc->tryTerminate();			QTimer::singleShot( 1000, ipProc, SLOT( kill() ) );				qWarning("Killed ipProc..");			break;		case NETMASK:			netmaskProc->tryTerminate();			QTimer::singleShot( 1000, netmaskProc, SLOT( kill() ) );			qWarning("Killed netmaskProc..");				break;		case GW:			gwProc->tryTerminate();			QTimer::singleShot( 1000, gwProc, SLOT( kill() ) );			qWarning("Killed gwProc..");				break;		case MODE:			modeProc->tryTerminate();			QTimer::singleShot( 1000, modeProc, SLOT( kill() ) );			qWarning("Killed modeProc..");			break;		case ESSID:			essidProc->tryTerminate();			QTimer::singleShot( 1000, essidProc, SLOT( kill() ) );			qWarning("Killed essidProc..");			break;		case KEY:			keyProc->tryTerminate();			QTimer::singleShot( 1000, keyProc, SLOT( kill() ) );			qWarning("Killed keyProc..");			break;		};}/*------------------------------------------      Functions for executing a profile------------------------------------------*/// Function for executing a profile.void Process::runProfile(const QString &name){	// Clear the textbrowser.	browser->clear();			// Disable the ok-button during the execution of the profile.	okBut->setEnabled(0);		// Clear this variable to show that no process is running at the moment.	CURRENT_PROC = NONE;		// Loop through the vector and find the profile we want to execute.	for (current=0; current<Profile::profiles.size(); current++) {		if (Profile::profiles.at(current).name == name) // Check if the current profile is matching the arg.			break;	}		// Save each command in a QStringList.	ifaceDown << Settings::IFCONFIG_PATH << Profile::profiles.at(current).iface << "down";	killDhcp << Settings::DHCP_CLIENT_PATH << "-k" << Profile::profiles.at(current).iface;	setDhcp << Settings::DHCP_CLIENT_PATH << Profile::profiles.at(current).iface;	fetchIp << Settings::IFCONFIG_PATH << Profile::profiles.at(current).iface;	setIp << Settings::IFCONFIG_PATH << Profile::profiles.at(current).iface << Profile::profiles.at(current).ip;	setNetmask << Settings::IFCONFIG_PATH << Profile::profiles.at(current).iface << "netmask" << Profile::profiles.at(current).netmask;    setGw << Settings::ROUTE_PATH << "add" << "default" << "gw" << Profile::profiles.at(current).gw;	setMode << Settings::IWCONFIG_PATH << Profile::profiles.at(current).iface << "mode" << Profile::profiles.at(current).mode;	setEssid << Settings::IWCONFIG_PATH << Profile::profiles.at(current).iface << "essid" << Profile::profiles.at(current).essid;	setKey << Settings::IWCONFIG_PATH << Profile::profiles.at(current).iface << "key" << Profile::profiles.at(current).key; 		// Check if we should skip this task (which is bringing down the interface).	if (Profile::profiles.at(current).iface == "None" || Profile::profiles.at(current).ip.isEmpty() ) {		startGwProc();		return;	}			// Print that we're about to bring down the interface.	browser->setText("* Bringing down interface <b>" + Profile::profiles.at(current).iface + "</b><br>");	qWarning(green + "* Bringing down interface " + GREEN + Profile::profiles.at(current).iface + normal);			// Create the process that brings down the interface.	ifaceDownProc = new QProcess(0, "ifaceDownProc");	ifaceDownProc->setArguments(ifaceDown); // Pass the arguments to the process.	// Start the process.	if (!ifaceDownProc->start() ) { 		qWarning(red + "- Error: " + Settings::IFCONFIG_PATH + ": command not found!" + normal);		browser->setText(browser->text() + "<font color=red>Error: " + Settings::IFCONFIG_PATH + ": command not found.</font><br>");				// Check if we should continue with the dhcp process or set a static-ip.		if (Profile::profiles.at(current).ip == "dhcp") {			// Check which dhcp client that shall be used.			if (Settings::DHCP_CLIENT == "dhcpcd") {				startKillDhcp();				return;			}			else if (Settings::DHCP_CLIENT == "dhclient") {				startDhcpProc();				return;			}		}		// Or set a static ip.		else {			startIpProc();			return;		}	}			// Mark the current process.	CURRENT_PROC = IFACEDOWN;		// Check if we should broadcast for an ip-adress or set a static-ip.	if (Profile::profiles.at(current).ip == "dhcp") {		// Check which dhcp client that shall be used.		if (Settings::DHCP_CLIENT == "dhcpcd")			connect(ifaceDownProc, SIGNAL(processExited()), this, SLOT(startKillDhcp()) );		else if (Settings::DHCP_CLIENT == "dhclient")			connect(ifaceDownProc, SIGNAL(processExited()), this, SLOT(startDhcpProc()) );	}	else		connect(ifaceDownProc, SIGNAL(processExited()), this, SLOT(startIpProc()) );		// Establish connections between the process and its error-output.	connect(ifaceDownProc, SIGNAL(readyReadStderr()), this, SLOT(ifaceDownErr()) );}// Function that kills a possible dhcpcd process.void Process::startKillDhcp(){		// Mark that no process is running.	CURRENT_PROC = NONE;		// Check if we should skip this task.	if (Profile::profiles.at(current).ip.isEmpty() )		startNetmaskProc();		// Print that we're about to kill  a possible dhcpcd-process.	browser->setText(browser->text() + "* Killing a possible dhcpcd  process<br>");	qWarning(green + "* Killing a possible dhcpcd process" + normal);		// Start with creating a process that kills a possible dhcp-process.	killDhcpProc = new QProcess(0, "killDhcpProc");	killDhcpProc->setArguments(killDhcp);		// Start the process.	if (!killDhcpProc->start() ) {		qWarning(red + "- Error: " + Settings::DHCP_CLIENT_PATH + ": command not found!" + normal);		browser->setText(browser->text() + "<font color=red>" + Settings::DHCP_CLIENT_PATH + ": command not found.</font><br>");		startDhcpProc();		return;	}	// Mark the current process.	CURRENT_PROC = KILLDHCP;		// Continue with broadcasting for an ip after we have killed the dhcp-process.		connect(killDhcpProc, SIGNAL(processExited()), this, SLOT(startDhcpProc()) );		// Establish connections between the process and its error-output.	connect(killDhcpProc, SIGNAL(readyReadStderr()), this, SLOT(killDhcpErr()) );}// Function that does a dhcp broadcast.void Process::startDhcpProc(){	// Mark that no process is running.	CURRENT_PROC = NONE;		// Print that we're about to do a dhcp-request.	browser->setText(browser->text() + "* Requesting DHCP ip for <b> " + Profile::profiles.at(current).iface + "</b>...<br>");	qWarning(green + "* Requesting DHCP ip for " + GREEN + Profile::profiles.at(current).iface + normal);		// Create the process that broadcasts for ip.	dhcpProc = new QProcess(0, "dhcpProc");		dhcpProc->setArguments(setDhcp); // Pass the arguments to the process.		// Start the process.	if (!dhcpProc->start()) {		qWarning(red + "- Error: " + Settings::DHCP_CLIENT_PATH + ": command not found!" + normal);		browser->setText(browser->text() + " <font color=red>" + Settings::DHCP_CLIENT_PATH + ": command not found.</font><br>");		DHCP_ERROR = 1;		startFetchIpProc();		return;	}			// Mark the current process.	CURRENT_PROC = DHCP;		// Continue with different functions depending on dhcp client.	if (Settings::DHCP_CLIENT == "dhcpcd")		connect(dhcpProc, SIGNAL(processExited()), this, SLOT(startFetchIpProc()) );	else if (Settings::DHCP_CLIENT == "dhclient") 		connect(dhcpProc, SIGNAL(processExited()), this, SLOT(startNetmaskProc()) );	// Establish connections between the process and its error-output.	connect(dhcpProc, SIGNAL(readyReadStderr()), this, SLOT(dhcpErr()) );}// Function that parses out the recieved dhcp ip from `ifconfig`.void Process::startFetchIpProc() {	// Mark that no process is running.	CURRENT_PROC = NONE;		// We shall only try to fetch the recieved ip if the dhcp-process was successfull.	if (DHCP_ERROR == 1) {		startNetmaskProc();		return;	}		// Create and execute the process that fetches the ip.	fetchIpProc = new QProcess(0, "fetchIpProc");	fetchIpProc->setArguments(fetchIp);		// Start the process.	if (!fetchIpProc->start() ) {		qWarning(red + "Error: " + Settings::IFCONFIG_PATH + ": command not found." + normal);		browser->setText(browser->text() + "<font color=red>Error: " + Settings::IFCONFIG_PATH + ": command not found.</font><br>");		startIpProc();		return;	}		// Mark the current process.	CURRENT_PROC = FETCHIP;		// Establish a connection to the standard-output (so we can fetch the ip).		connect(fetchIpProc, SIGNAL(readyReadStdout()), this, SLOT(readIp()) );		// Continue with the netmask-process when we've fetched the ip.	connect(fetchIpProc, SIGNAL(processExited()), this, SLOT(startNetmaskProc()) );}// Function that sets an static ip adress.void Process::startIpProc(){	// Mark that no process is running.	CURRENT_PROC = NONE;		// Check if we should skip this task.	if (Profile::profiles.at(current).ip.isEmpty() ) {		startNetmaskProc();		return;	}		// Print that we're about to set a static ip.    browser->setText(browser->text() + "* Assigning ip <b>" + Profile::profiles.at(current).ip + "</b> to <b>" + Profile::profiles.at(current).iface + "</b><br>");	qWarning(green + "* Assigning ip " + GREEN + Profile::profiles.at(current).ip + green + " to " + GREEN + Profile::profiles.at(current).iface + normal + normal);		// Create and execute the process that shall set a static ip.	ipProc = new QProcess(0, "ipProc");	ipProc->setArguments(setIp); // Pass the arguments to the process.		// Start the process.	if (!ipProc->start()) {		qWarning(red + "- Error: " + Settings::IFCONFIG_PATH + ": command not found!" + normal);		browser->setText(browser->text() + "<font color=red>Error: " + Settings::IFCONFIG_PATH + ": command not found.</font><br>");		startNetmaskProc();		return;	}	// Mark the current process.	CURRENT_PROC = STATICIP;		// When the process is finished, continue with setting the netmask.	connect(ipProc, SIGNAL(processExited()), this, SLOT(startNetmaskProc()) );	// Establish connections between the process and its error-output.	connect(ipProc, SIGNAL(readyReadStderr()), this, SLOT(ipErr()) );}	// Function that sets the netmask.void Process::startNetmaskProc(){	// Mark that no process is running.	CURRENT_PROC = NONE;		// Check if we should skip this task.	if (Profile::profiles.at(current).netmask.isEmpty() ) {		startGwProc();		return;	}		// Print that we're about to set the netmask.	browser->setText(browser->text() + "* Setting netmask: <b>" + Profile::profiles.at(current).netmask + "</b><br>");	qWarning(green + "* Setting netmask: " + GREEN + Profile::profiles.at(current).netmask + normal);		// Create and execute the process that sets the netmask.	netmaskProc = new QProcess(0, "netmaskProc");	netmaskProc->setArguments(setNetmask); // Pass the arguments to the process.	

⌨️ 快捷键说明

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