📄 setip_form.cpp
字号:
/****************************************************************************** Form implementation generated from reading ui file 'setip_form.ui'**** Created: Fri Jun 2 09:20:14 2006** by: The User Interface Compiler (uic)**** WARNING! All changes made in this file will be lost!****************************************************************************/#include "setip_form.h"#include <qlabel.h>#include <qlineedit.h>#include <qpushbutton.h>#include <qradiobutton.h>#include <qlayout.h>#include <qvariant.h>#include <qtooltip.h>#include <qwhatsthis.h>#include <qmessagebox.h>#include <qfile.h>#include <qstring.h>#include <stdio.h>#include <unistd.h>#include <sys/wait.h>#include <signal.h>#define Run(al1, al2, al3, al4) { char *arglist[4]; arglist[0]=al1; arglist[1]=al2; arglist[2]=al3; arglist[3]=al4; if(execute(arglist)) return 1; }int execute(char *arglist[]){ int pid,exitstatus; pid =fork(); switch (pid) { case -1: perror("fork failed"); return 1; case 0: execvp(arglist[0],arglist); perror("execvp failed"); return 1; default: while( wait(&exitstatus)!=pid); } return 0; } int open_net(const char ip[20]){ char buf[20]; strcpy(buf,ip); Run ("mc2005_pp","ETHER","ON",NULL); sleep(5); Run("insmod","/lib/modules/2.6.12-rc4/at91_ether.ko",NULL,NULL); Run("/sbin/ifconfig","eth0",buf,NULL); return 0; } int close_net(){ Run ("/sbin/ifconfig","eth0","down",NULL); Run ("rmmod","at91_ether",NULL,NULL); Run ("mc2005_pp","ETHER","OFF",NULL); return 0;} static int check_net(){ char buf[128]; QFile file( "/proc/net/dev"); if ( !file.open( IO_ReadOnly )) { printf("Could not open file /proc/net/dev"); return 1; } while ( !file.atEnd() ) { file.readBlock(buf,128); if (strstr(buf,"eth0:")!=NULL) { printf("net on."); file.close(); return 0; } } file.close(); printf("net off."); return 1; }/* * Constructs a setip_form which is a child of 'parent', with the * name 'name' and widget flags set to 'f' * * The dialog will by default be modeless, unless you set 'modal' to * TRUE to construct a modal dialog. */setip_form::setip_form( QWidget* parent, const char* name, bool modal, WFlags fl ) : QDialog( parent, name, modal, fl ){ char buf[20]; if ( !name ) setName( "setip_form" ); resize( 285, 161 ); setBaseSize( QSize( 320, 240 ) ); setCaption( tr( "Set IP" ) ); TLabnetstat = new QLabel( this, "TLabnetstat" ); TLabnetstat->setGeometry( QRect( 10, 20, 261, 30 ) ); RadioButton1 = new QRadioButton( this, "RadioButton1" ); RadioButton1->setGeometry( QRect( 10, 70, 100, 21 ) ); IP_Label1 = new QLabel( this, "IP_Label1" ); IP_Label1->setGeometry( QRect( 140, 70, 20, 21 ) ); IP_Label1->setText( tr( "IP " ) ); QFile file( "/mnt/a/etc/mc2005IP"); memset(buf,'\0',20); if ( !file.open( IO_ReadWrite)) { QMessageBox::critical(this,"Open failed","Could not open file for reading:"); return; } file.readBlock(buf,20); file.close(); LineEdit1 = new QLineEdit( this, "LineEdit1" ); LineEdit1->setGeometry( QRect( 160, 70, 120, 22 ) ); LineEdit1->setText( tr( buf ) ); if(check_net()==0) { TLabnetstat->setText( tr( "Network opened." ) ); RadioButton1->setText( tr( "Ethernet Off" ) ); } else { TLabnetstat->setText( tr( "Network closed." ) ); RadioButton1->setText( tr( "Ethernet On" ) ); } OK_Button = new QPushButton( this, "OK_Button" ); OK_Button->setGeometry( QRect( 10, 110, 80, 30 ) ); OK_Button->setText( tr( "OK" ) ); Cancel_Button = new QPushButton( this, "Cancel_Button" ); Cancel_Button->setGeometry( QRect( 200, 110, 80, 30 ) ); Cancel_Button->setText( tr( "Quit" ) ); // signals and slots connections connect( Cancel_Button, SIGNAL( clicked() ), this, SLOT( accept() ) ); connect( OK_Button, SIGNAL( clicked() ), this, SLOT( ok_slot() ) );}/* * Destroys the object and frees any allocated resources */setip_form::~setip_form(){ // no need to delete child widgets, Qt does it all for us}void setip_form::ok_slot(){ if(RadioButton1->isChecked ()) if (RadioButton1->text()== "Ethernet Off" ){ close_net(); QMessageBox::information(this,"Ethernet Off","Ethernet is Off."); TLabnetstat->setText( tr( "Network closed." ) ); RadioButton1->setText( tr( "Ethernet On" ) ); } else if(RadioButton1->text()== "Ethernet On" ) { open_net(LineEdit1->text()); QMessageBox::information(this,"Ethernet On","Ethernet is On."); TLabnetstat->setText( tr( "Network opened." ) ); RadioButton1->setText( tr( "Ethernet Off" ) ); QFile file( "/mnt/a/etc/mc2005IP"); if ( !file.open( IO_ReadWrite)) { QMessageBox::critical(this,"Open failed","Could not open file for reading:"); return; } file.writeBlock(LineEdit1->text(),20); file.close(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -