📄 process.cpp
字号:
// Start the process. if (!netmaskProc->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>"); startGwProc(); return; } // Mark the current process. CURRENT_PROC = NETMASK; // When the process is finished, continue with setting the gateway. connect(netmaskProc, SIGNAL(processExited()), this, SLOT(startGwProc()) ); // Establish connections between the process and its error-output. connect(netmaskProc, SIGNAL(readyReadStderr()), this, SLOT(netmaskErr()) );}// Function that sets the gateway.void Process::startGwProc(){ // Mark that no process is running. CURRENT_PROC = NONE; // Check if we should skip this task. if (Profile::profiles.at(current).gw.isEmpty()) { startNsProc(); return; } // Print that we're about to set the gateway. browser->setText(browser->text() + "* Setting gateway: <b>" + Profile::profiles.at(current).gw + "</b><br>"); qWarning(green + "* Setting gateway: " + GREEN + Profile::profiles.at(current).gw + normal); // Create and execute the process that sets the gateway. gwProc = new QProcess(0, "gwProc"); gwProc->setArguments(setGw); // Fetch the profile-name from "row". // Start the process. if (!gwProc->start()) { qWarning(red + "- Error: " + Settings::IFCONFIG_PATH + ": command not found!" + normal); browser->setText(browser->text() + "<font color=red>" + Settings::ROUTE_PATH + ": command not found.</font><br>"); startNsProc(); return; } // Mark the current process. CURRENT_PROC = GW; // Continue with the nameservers when the gateway has been set. connect(gwProc, SIGNAL(processExited()), this, SLOT(startNsProc()) ); // Establish connections between the process and its error-output. connect(gwProc, SIGNAL(readyReadStderr()), this, SLOT(gwErr()) );}// Function that sets the nameserver(s) and the search domain.void Process::startNsProc(){ // Mark that no process is running. CURRENT_PROC = NONE; // Check if we should skip this task. if (Profile::profiles.at(current).ns1.isEmpty() && Profile::profiles.at(current).ns2.isEmpty() && Profile::profiles.at(current).search.isEmpty()) { startModeProc(); return; } // Open /etc/resolv.conf QFile resolv("/etc/resolv.conf"); if (!resolv.open(IO_WriteOnly)) { qWarning(red + "- Error: Couldn't open /etc/resolv.conf" + normal); browser->setText(browser->text() + "<font color=red>Error: Couldn't open /etc/resolv.conf</font><br>"); } else { QTextStream write(&resolv); // Print what we're doing and write the settings to /etc/resolv.conf if (!Profile::profiles.at(current).ns1.isEmpty()) { browser->setText(browser->text() + "* Setting nameserver #1: <b>" + Profile::profiles.at(current).ns1 + "</b><br>"); qWarning(green + "* Setting nameserver #1: " + GREEN + Profile::profiles.at(current).ns1 + normal); write << "nameserver " << Profile::profiles.at(current).ns1 << endl; } if (!Profile::profiles.at(current).ns2.isEmpty()) { browser->setText(browser->text() + "* Setting nameserver #2: <b>" + Profile::profiles.at(current).ns2 + "</b><br>"); qWarning(green + "* Setting nameserver #2: " + GREEN + Profile::profiles.at(current).ns2 + normal); write << "nameserver " << Profile::profiles.at(current).ns2 << endl; } if (!Profile::profiles.at(current).search.isEmpty()) { browser->setText(browser->text() + "* Setting search domain: <b>" + Profile::profiles.at(current).search + "</b><br>"); qWarning(green + "* Setting search domain: " + GREEN + Profile::profiles.at(current).search + normal); write << "nameserver " << Profile::profiles.at(current).search << endl; } resolv.close(); } // Continue with bringing down the specified interfaces. startModeProc();}// Function that sets the wireless mode (ie. managed, ad-hoc).void Process::startModeProc(){ // Mark that no process is running. CURRENT_PROC = NONE; // Check if we should skip this task. if (Profile::profiles.at(current).mode == "None") { startEssidProc(); return; } // Print that we're about to set the wireless-mode. browser->setText(browser->text() + "* Setting wireless mode: <b>" + Profile::profiles.at(current).mode + "</b><br>"); qWarning(green + "* Setting wireless mode: " + GREEN + Profile::profiles.at(current).mode + normal); // Create and execute the process that sets the wireless-mode. modeProc = new QProcess(0, "modeProc"); modeProc->setArguments(setMode); // Pass the arguments to the process. // Start the process. if (!modeProc->start()) { qWarning(red + "- Error: " + Settings::IWCONFIG_PATH + ": command not found!" + normal); browser->setText(browser->text() + "<font color=red>Error: " + Settings::IWCONFIG_PATH + ": command not found.</font><br>"); startEssidProc(); return; } // Mark the current process. CURRENT_PROC = MODE; // Continue with the nameservers when the gateway has been set. connect(modeProc, SIGNAL(processExited()), this, SLOT(startEssidProc()) ); // Establish connections between the process and its error-output. connect(modeProc, SIGNAL(readyReadStderr()), this, SLOT(modeErr()) );}// Function that sets the essid (for wireless networks).void Process::startEssidProc(){ // Mark that no process is running. CURRENT_PROC = NONE; // Check if we should skip this task. if (Profile::profiles.at(current).essid.isEmpty()) { startKeyProc(); return; } // Print that we're about to set the essid. browser->setText(browser->text() + "* Setting essid: <b>" + Profile::profiles.at(current).essid + "</b><br>"); qWarning(green + "* Setting essid: " + GREEN + Profile::profiles.at(current).essid + normal); // Create and execute the process that sets the essid. essidProc = new QProcess(0, "essidProc"); essidProc->setArguments(setEssid); // Pass the arguments to the process. // Start the process. if (!essidProc->start()) { qWarning(red + "- Error: " + Settings::IWCONFIG_PATH + ": command not found!" + normal); browser->setText(browser->text() + "<font color=red>Error: " + Settings::IWCONFIG_PATH + ": command not found.</font><br>"); startKeyProc(); return; } // Mark the current process. CURRENT_PROC = ESSID; // Continue with the nameservers when the gateway has been set. connect(essidProc, SIGNAL(processExited()), this, SLOT(startKeyProc()) ); // Establish connections between the process and its error-output. connect(essidProc, SIGNAL(readyReadStderr()), this, SLOT(essidErr()) );}// Function that sets the encryption (for wireless networks).void Process::startKeyProc(){ // Mark that no process is running. CURRENT_PROC = NONE; // Check if we should skip this task. if (Profile::profiles.at(current).key.isEmpty()) { cleanUp(); return; } // Print that we're about to set the encrypyion-key. browser->setText(browser->text() + "* Setting key: <b>" + Profile::profiles.at(current).key + "</b><br>"); qWarning(green + "* Setting key: " + GREEN + Profile::profiles.at(current).key + normal); // Create and execute the process that sets the encryption key. keyProc = new QProcess(0, "keyProc"); keyProc->setArguments(setKey); // Pass the arguments to the process. // Start the process. if (!keyProc->start()) { qWarning(red + "- Error: " + Settings::IWCONFIG_PATH + ": command not found!" + normal); browser->setText(browser->text() + "<font color=red>Error: " + Settings::IWCONFIG_PATH + ": command not found.</font><br>"); cleanUp(); return; } // Mark the current process. CURRENT_PROC = KEY; // Continue with the nameservers when the gateway has been set. connect(keyProc, SIGNAL(processExited()), this, SLOT(cleanUp()) ); // Establish connections between the process and its error-output. connect(keyProc, SIGNAL(readyReadStderr()), this, SLOT(keyErr()) );}// Function that prints out the recieved dhcp ip.void Process::readIp(){ QString output, ip; int first_pos, bcast_pos; // Save the output in a QString (it's the second line of the output we want). output = fetchIpProc->readLineStdout(); output = fetchIpProc->readLineStdout(); // Parse out the ip-adress. first_pos = output.find(":", 0); bcast_pos = output.find(" ", first_pos); ip = output.mid(first_pos+1, bcast_pos-first_pos-1); // Finally, show the ip that we've recieved. browser->setText(browser->text() + "<font color=#52AA00>- Dhcp-ip recieved: <b>" + ip + "</b></font><br>"); qWarning(cyan + "- Dhcp-ip recieved: " + CYAN + ip + normal);}/*------------------------------------------ Error-report functions.------------------------------------------*/// Prints error messages from the interface down process.void Process::ifaceDownErr(){ // Print the error output to the label. QString output = ifaceDownProc->readLineStderr(); browser->setText(browser->text() + "<font color=red>- Error: " + output + "</font><br>"); qWarning(red + "- Error: " + output + normal);} // Prints error messages from the kill dhcp process.void Process::killDhcpErr(){ // Read the error output. QString output = killDhcpProc->readLineStderr(); // If no dhcpcd proc is running, do not show that particular error msg. if (output.find("**** " + Settings::DHCP_CLIENT_PATH + ": not running", 0) != -1) return; // Print the error output to the label. browser->setText(browser->text() + "<font color=red>- Error: " + output + "</font><br>"); qWarning(red + "- Error: " + output + normal);} // Prints error messages from the dhcp process.void Process::dhcpErr(){ // Handle the error messages different depending on dhpc client. if (Settings::DHCP_CLIENT == "dhcpcd") { // Mark that we've got an error. DHCP_ERROR = 1; // Print the error output to the label. QString output = dhcpProc->readLineStderr(); browser->setText(browser->text() + "<font color=red>- Error: " + output + "</font><br>"); qWarning(red + "- Error: " + output + normal); } else if (Settings::DHCP_CLIENT == "dhclient") { // Loop through all the error output from dhclient. while (dhcpProc->canReadLineStderr()) { QRegExp recievedIp("bound to ((.+)) -- renewal in .+ seconds.$"); QRegExp noDevice("Bind socket to interface: No such device"); QRegExp noRecieved("No DHCPOFFERS received."); QRegExp noPermission("(SIOCSIFADDR: Permission denied|SIOCSIFFLAGS: Permission denied|Open a socket for LPF: Operation not permitted)"); QRegExp exited("exiting.$"); // Save each line in output. QString output = dhcpProc->readLineStderr(); // Check if the device didn't exist. if (noDevice.search(output) != -1) { browser->setText(browser->text() + "<font color=red>Error: " + Profile::profiles.at(current).iface + ": No such device.</font><br>"); qWarning(red + "- Error: " + Profile::profiles.at(current).iface + ": No such device." + normal); // When we got our error msg, terminate the process. dhcpProc->tryTerminate(); QTimer::singleShot( 1000, dhcpProc, SLOT( kill() ) ); } // Check if no dhcp ip was recieved. if (noRecieved.search(output) != -1) { browser->setText(browser->text() + "<font color=red>Error: No ip recieved.</font><br>"); qWarning(red + "- Error: No ip recieved." + normal); // When we got our error msg, terminate the process. dhcpProc->tryTerminate(); QTimer::singleShot( 1000, dhcpProc, SLOT( kill() ) ); } // Check if we got "permission denied". if (noPermission.search(output) != -1) { browser->setText(browser->text() + "<font color=red>Error: Permission denied.</font><br>"); qWarning(red + "- Error: Permission denied." + normal); // When we got our error msg, terminate the process. dhcpProc->tryTerminate(); QTimer::singleShot( 1000, dhcpProc, SLOT( kill() ) ); } // Check if something went wrong and dhclient was exited. if (exited.search(output) != -1) { browser->setText(browser->text() + "<font color=red>Error: dhclient exited.</font><br>"); qWarning(red + "- Error: dhclient exited." + normal); // When we got our error msg, terminate the process. dhcpProc->tryTerminate(); QTimer::singleShot( 1000, dhcpProc, SLOT( kill() ) ); } // Check if we have recieved any ip. if (recievedIp.search(output) != -1) { // Try to parse out the recieved ip. recievedIp.search(output); QString ip = recievedIp.cap(1); // Print out the ip that we recieved. browser->setText(browser->text() + "<font color=#52AA00>- Dhcp-ip recieved: <b>" + ip + "</b></font><br>"); qWarning(cyan + "- Dhcp-ip recieved: " + CYAN + ip + normal); } } }} // Prints error messages from the static ip process.void Process::ipErr(){ // Print the error output to the label. QString output = ipProc->readLineStderr(); browser->setText(browser->text() + "<font color=red>- Error: " + output + "</font><br>"); qWarning(red + "- Error: " + output + normal);} // Prints error messages from the netmask process.void Process::netmaskErr(){ // Print the error output to the label. QString output = netmaskProc->readLineStderr(); browser->setText(browser->text() + "<font color=red>- Error: " + output + "</font><br>"); qWarning(red + "- Error: " + output + normal);} // Prints error messages from the gateway process.void Process::gwErr(){ // Print the error output to the label. QString output = gwProc->readLineStderr(); browser->setText(browser->text() + " <font color=red>- Error: " + output + "</font><br>"); qWarning(red + "- Error: " + output + normal);} // Prints error messages from the wireless mode process.void Process::modeErr(){ // Print the error output to the label. QString output = modeProc->readLineStderr(); browser->setText(browser->text() + "<font color=red>- " + output + "</font><br>"); qWarning(red + "- " + output + normal);} // Prints error messages from the wireless essid process.void Process::essidErr(){ // Print the error output to the label. QString output = essidProc->readLineStderr(); browser->setText(browser->text() + "<font color=red>- " + output + "</font><br>"); qWarning(red + "- " + output + normal);} // Prints error messages from the wireless key process.void Process::keyErr(){ // Print the error output to the label. QString output = keyProc->readLineStderr(); browser->setText(browser->text() + "<font color=red>- " + output + "</font><br>"); qWarning(red + "- " + output + normal);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -