- dialog.c
- #include <QtGui>
- #include <QtNetwork>
- #include <QFile>
- #include <QDir>
- #include <stdlib.h>
- #include "dialog.h"
-
-
- Dialog::Dialog(QWidget *parent)
- : QDialog(parent)
- {
- tcpSocket = new QTcpSocket(this);
-
- timer1=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);
-
-
-
- startdateLineEdit->setReadOnly(true);
- starttimeLineEdit->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()));
-
-
- 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");
- db.setDatabaseName("E:/sqlite/oracle.db");
- if(!db.open())
- {
-
- display.append("cannot open database.");
- display.append("Reason: " + db.lastError().databaseText());
- }
-
- getip();
-
-
- 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=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;
-
- 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 <<QString::fromLocal8Bit(fortune);
- }
dialog.h
#ifndef DIALOG_H
#define DIALOG_H
#include <QDialog>
#include <QTcpSocket>
#include <QtSql>
#include <QSqlDatabase>
#include <QTextEdit>
#include <QTime>
#include <QFile>
#include <QDir>
#include <QTimer>
class QDialogButtonBox;
class QLabel;
class QLineEdit;
class QPushButton;
class QTcpSocket;
class QTextEdit;
class QSqlDatabase;
class QSqlQuery;
class QTime;
class QDate;
class Dialog : public QDialog
{
Q_OBJECT
public:
Dialog(QWidget *parent = 0);
void getip();
void log(QByteArray);
void copy(); //备份
private:
//图形界面
QLabel *serverLabel;
QLabel *ipInfoLabel;
QLabel *sqlipLabel;
QLabel *databaseLabel;
QLabel *sqluserLabel;
QLabel *sqlpassLabel;
QLabel *startdateLabel;
QLabel *starttimeLabel;
QComboBox *ComboBox;
QLineEdit *ipInfoLineEdit;
QLineEdit *sqlipLineEdit;
QLineEdit *databaseLineEdit;
QLineEdit *sqluserLineEdit;
QLineEdit *sqlpassLineEdit;
QLineEdit *startdateLineEdit;
QLineEdit *starttimeLineEdit;
QPushButton *beginButton;
QPushButton *quitButton;
QDialogButtonBox *buttonBox;
QTextEdit display;
//存储服务器信息
int port;
QString host;
QString filepath;
QDir dir;
QByteArray fortune[5];
QTimer *timer1;
// QTimer *timer2;
QTcpSocket *tcpSocket;
QSqlDatabase db;
long numflag;
public slots:
//从套接字读数据,并存于数据库中
void sendFortune();
//创建数据库连接,开始定时
void begin();
};
#endif
main.c
- #include <QApplication>
- #include <QtCore>
-
- #include <stdlib.h>
-
- #include "dialog.h"
-
- int main(int argc, char *argv[])
- {
- QApplication app(argc, argv);
- Dialog dialog;
- dialog.show();
- return dialog.exec();
- }