www.pudn.com > send.rar > dialog.cpp


#include  
#include  
#include  
#include  
#include  
#include "dialog.h" 
 
 
Dialog::Dialog(QWidget *parent) 
    : QDialog(parent) 
{ 
     tcpSocket = new QTcpSocket(this); 
     
     timer1=new QTimer(this); 
     //timer2=new QTimer(this); 
     
     serverLabel = new QLabel(QString::fromLocal8Bit("服务器ip:")); 
     ipInfoLabel = new QLabel(QString::fromLocal8Bit("服务器port:")); 
     sqlipLabel = new QLabel(QString::fromLocal8Bit("数据库ip:")); 
     databaseLabel = new QLabel(QString::fromLocal8Bit("数据库名:")); 
     sqluserLabel = new QLabel(QString::fromLocal8Bit("用户名:")); 
     sqlpassLabel = new QLabel(QString::fromLocal8Bit("数据库密码:")); 
     startdateLabel = new QLabel(QString::fromLocal8Bit("开始日期:")); 
     starttimeLabel = new QLabel(QString::fromLocal8Bit("开始时间:")); 
 
     ComboBox = new QComboBox; 
      
     ComboBox->addItem(tr("local")); 
     ComboBox->addItem(tr("center")); 
     ComboBox->addItem(tr("liantong")); 
     ComboBox->addItem(tr("yidong")); 
      
     ipInfoLineEdit = new QLineEdit(tr("hj")); 
     sqlipLineEdit = new QLineEdit(tr("hj")); 
     databaseLineEdit = new QLineEdit(tr("hj")); 
     sqluserLineEdit = new QLineEdit(tr("hj")); 
     sqlpassLineEdit = new QLineEdit(tr("hj")); 
     startdateLineEdit=new QLineEdit(QDate::currentDate().toString("yyyy.MM.dd")); 
     starttimeLineEdit=new QLineEdit(QTime::currentTime().toString("h:m:s")); 
 
     sqlpassLineEdit->setEchoMode(QLineEdit::Password); 
      
    // hostLineEdit->setReadOnly(true); 
    // portLineEdit->setReadOnly(true); 
     startdateLineEdit->setReadOnly(true); 
     starttimeLineEdit->setReadOnly(true); 
     //display.setReadOnly(true); 
 
     beginButton = new QPushButton(tr("Begin")); 
     beginButton->setDefault(true); 
     beginButton->setEnabled(true); 
 
     quitButton = new QPushButton(tr("Quit")); 
 
     buttonBox = new QDialogButtonBox; 
     buttonBox->addButton(beginButton, QDialogButtonBox::ActionRole); 
     buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole); 
 
     connect(quitButton, SIGNAL(clicked()), this, SLOT(close())); 
     connect(beginButton,SIGNAL(clicked()), this, SLOT(begin())); 
     connect(timer1, SIGNAL(timeout()), this, SLOT(sendFortune())); //设置发送数据报的大小 
     //connect(timer2, SIGNAL(timeout()), this, SLOT(copy())); 
      
     QGridLayout *mainLayout = new QGridLayout; 
     
     mainLayout->addWidget(serverLabel, 0, 0); 
     mainLayout->addWidget(ComboBox, 0, 1); 
     mainLayout->addWidget(ipInfoLabel, 0, 2); 
     mainLayout->addWidget(ipInfoLineEdit, 0, 3); 
     mainLayout->addWidget(sqlipLabel, 1, 0); 
     mainLayout->addWidget(sqlipLineEdit, 1, 1); 
     mainLayout->addWidget(databaseLabel,1,2); 
     mainLayout->addWidget(databaseLineEdit,1,3); 
     mainLayout->addWidget(sqluserLabel,2,0); 
     mainLayout->addWidget(sqluserLineEdit,2,1); 
     mainLayout->addWidget(sqlpassLabel,2,2); 
     mainLayout->addWidget(sqlpassLineEdit,2,3); 
     mainLayout->addWidget(startdateLabel,3,0); 
     mainLayout->addWidget(startdateLineEdit,3,1); 
     mainLayout->addWidget(starttimeLabel,3,2); 
     mainLayout->addWidget(starttimeLineEdit,3,3); 
     mainLayout->addWidget(&display,4,0,20,4); 
     mainLayout->addWidget(buttonBox,24,0,1,4);  
      
     setLayout(mainLayout); 
     setWindowTitle(tr("Client_sendDataToCenter")); 
    
} 
 
 
void Dialog::begin() 
{ 
		 
  //创建日志文件夹 
  filepath = dir.currentPath(); 
 
  if(!filepath.contains("log_center_send"))	 
   {	 
    dir.mkdir("log_center_send"); 
    filepath += "/log_center_send/"; 
    dir.setCurrent(filepath); 
   } 
	dir.cdUp(); 
	 
	//连接数据库,用户名和密码从界面上填入获得 
  db = QSqlDatabase::addDatabase("QSQLITE"); // 使用sqlserver数据库驱动     
  db.setDatabaseName("E:/sqlite/oracle.db"); // 之前建立的数据库名 
	if(!db.open()) 
	{ 
		// 打开数据库失败,显示错误原因 
   display.append("cannot open database."); 
   display.append("Reason: " + db.lastError().databaseText()); 
	} 
 
	getip(); //获得ip 
	  
	  
	tcpSocket->connectToHost(host,port); 
	if(tcpSocket->waitForConnected(5000)==false) 
     {   
      display.append("Failed to connect the server!!"); 
      return; 
     } 
   
  	  //和服务器交互信息 
  QByteArray block("IamOracleR"); //请求数据包格式 
	tcpSocket->write(block); 
  if(tcpSocket->waitForBytesWritten()==false) 
   {    
        display.append("Failed to communicate with  the server!"); 
        return; 
   } 
   
  timer1->start(100); 
   
  numflag=0; 
  
  
	display.append("The client is working now!!"); 
	  
 } 
 
void Dialog::getip() 
   { 
    QSqlQuery queryip; 
    QSqlError errorip; 
    queryip.prepare("select * from ipInfo where servername=:name "); 
    queryip.bindValue(":name",ComboBox->currentText());   
    if(queryip.exec()) 
      { 
       queryip.first(); 
       //对port和host赋值 
       port=queryip.value(2).toInt(); 
       host=queryip.value(1).toString(); 
       ipInfoLineEdit->setText(host+" "+queryip.value(2).toString()); 
     } 
     else 
       { 
         //如果查询失败,显示错误 
        errorip= queryip.lastError();  
        display.append("From sqlite database,the ipInfo table: " + errorip.databaseText()); 
       } 
    } 
 
void Dialog::sendFortune() 
	{ 
    timer1->stop(); 
	 
	  if((++numflag)==1000)     //标志位符合条件则备份 
      { 
    		display.append("here"); 
    		copy(); 
     		numflag=0; 
    	} 
     
	QByteArray data; //数据库中包含的输出字段  
     
    QSqlQuery query; 
    QSqlError error;  
    int numRows,temp; 
    if(query.exec("select * from tempOracle where flag='f' ")) 
      {   
         // 询问数据库驱动,是否驱动含有记录影响行数的特性 
       if(db.driver()->hasFeature(QSqlDriver::QuerySize)) 
        { 
          numRows = query.size(); // 如果支持结果影响的行数,那么直接记录下来 
        }  
        else 
        { 
          query.last();           //否则定位到结果最后 
          numRows = query.at() + 1; 
        } 
       query.first(); 
       if(numRows==-1) 
       	{ 
       		timer1->start(100); 
       		return; 
       	} 
       else 
        {  
          do 
            { 
             //依次取出数据,并存于字符串数组中 
             data=query.value(1).toByteArray()+query.value(0).toByteArray();   
             
             fortune[query.at()].clear(); 
             fortune[query.at()]=data; //设置类型,包类型(1字节)+socket号(2字节)+数据 
              
             display.append(query.value(0).toString()); 
             //对于已充值成功的记录,改变发送标志位 
             QSqlQuery queryupdate; 
			 queryupdate.prepare("update tempOracle set flag='t' where data=:da"); 
			 queryupdate.bindValue(":da",query.value(0)); 
             if(!queryupdate.exec()) 
              { 
                QSqlError errorup = queryupdate.lastError();  
                display.append("Failed to change the sendflag!Reason:"+errorup.databaseText()); 
                log("Failed to change the sendflag"+fortune[query.at()]);              
              } 
             temp=query.at(); 
             } 
           while(query.next()&&temp<4); 
        }      
      } 
     else 
      { 
        //如果查询失败,发出错误信号 
        error= query.lastError();  
        display.append("From sqlite database,the handled table: " + error.databaseText()); 
        timer1->start(100); 
        return; 
      } 
      
     QByteArray string; 
	 QString tempstring; 
     int i,j,fortunelen[5]; 
     for(i=0;i<=4;i++) 
       { 
     	 fortunelen[i]=fortune[i].size(); 
		 string+=fortune[i]; 
       } 
      
	 for(i=0;i<5;i++) 
		{ 
	       tempstring=QString("%1").arg(fortunelen[i]); 
		   int b=tempstring.size(); 
		   for(j=0;j<4-b;j++) 
			{ 
		     tempstring="0"+tempstring; 
			} 
		    
		   string+=tempstring.toLocal8Bit(); 
		   fortune[i].clear(); 
		} 
	 tcpSocket->write(string); 
     display.append("send"+string); 
      
     if(tcpSocket->waitForBytesWritten()==false) 
       {    
         display.append("Failed to send the data to center"); 
         log("Failed to send the data to center"+string); 
         return; 
       } 
      
     string.clear(); 
      
     timer1->start(100); 
      
	} 
 
  
void Dialog::copy() 
	{ 
      int nums=0; 
      display.append("copy"); 
      QSqlQuery querycopy; 
      QByteArray data1; 
      if(querycopy.exec("select * from tempOracle where flag='t'")) 
      {   
         // 询问数据库驱动,是否驱动含有记录影响行数的特性 
        if(db.driver()->hasFeature(QSqlDriver::QuerySize)) 
        { 
          nums = querycopy.size(); // 如果支持结果影响的行数,那么直接记录下来 
        }  
        else 
        { 
          querycopy.last();           //否则定位到结果最后 
          nums= querycopy.at() + 1; 
        } 
       querycopy.first(); 
       if(nums==-1) 
       	{ 
           display.append("No data in the handled table,and cann`t copy"); 
      		 return; 
	   } 
	   else 
	   {    
		 do 
	     { 
        //依次取出数据,并存于字符串数组中 
         data1=querycopy.value(0).toByteArray();   
 	        
	     QSqlQuery queryinsert; 
	     queryinsert.prepare("insert into copyTempOracle(data) values (:da1)"); 
	     queryinsert.bindValue(":da1",(QString)data1); 
	     if(!queryinsert.exec()) 
          { 
            QSqlError errorinsert; 
            errorinsert = queryinsert.lastError();  
            display.append("Failed to copy the data.Reason:"+errorinsert.databaseText()); 
            log("Failed to copy"+data1); 
          } 
	        
	     QSqlQuery querydelete; 
	     querydelete.prepare("delete from tempOracle where data=:dat and flag='t'"); 
         querydelete.bindValue(":dat",querycopy.value(0).toString()); 
         if(!querydelete.exec()) 
          { 
            QSqlError errordelete; 
            errordelete = querydelete.lastError();  
            display.append("Failed to delete the data after copy.Reason:"+errordelete.databaseText()); 
            log("Failed to delete from the handled."+data1); 
          } 
	     } 
	     while(querycopy.next()); 
	   } 
	 } 
	  else 
	  { 
	      QSqlError errorcopy=querycopy.lastError(); 
		  display.append("Failed to query the handled table.Reason:"+errorcopy.databaseText()); 
      } 
 	} 
		 
 void Dialog::log(QByteArray fortune) 
 	 { 
 	   QFile file(QString::fromLocal8Bit("%1.txt").arg(QString::fromLocal8Bit(fortune))); 
       file.open(QIODevice::WriteOnly); 
       QTextStream log(&file);  
       log <