Advertisement
Guest User

qt socket double emit

a guest
Feb 19th, 2013
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.59 KB | None | 0 0
  1. #include "profiledialog.h"
  2. #include "ui_profiledialog.h"
  3. #include "packettypes.h"
  4.  
  5. #include <QMessageBox>
  6. #include <QDebug>
  7. #include <QSqlQuery>
  8. #include <QTcpSocket>
  9.  
  10. ProfileDialog::ProfileDialog(QWidget *parent, QSqlDatabase *db) :
  11.     QDialog(parent),
  12.     ui(new Ui::ProfileDialog)
  13. {
  14.     ui->setupUi(this);
  15.     this->db = db;
  16.     socket = NULL;
  17.    
  18.     this->setFixedSize(this->size());
  19.     this->setWindowFlags(Qt::MSWindowsFixedSizeDialogHint | Qt::WindowCloseButtonHint | Qt::Window);
  20.    
  21.     this->parent = parent;
  22.     parent->setEnabled(false);
  23.     this->setEnabled(true);
  24.    
  25.     connect(ui->BCancel, SIGNAL(clicked()), this, SLOT(BCancel_Clicked()));
  26.     connect(ui->BCreateProfile, SIGNAL(clicked()), this, SLOT(BCreateProfile_Clicked()));
  27.     connect(ui->BCreateAccount, SIGNAL(clicked()), this, SLOT(BCreateAccount_Clicked()));
  28. }
  29.  
  30. void ProfileDialog::closeEvent(QCloseEvent *event)
  31. {
  32.     parent->setEnabled(true);
  33.     emit DialogClosing();
  34. }
  35.  
  36. void ProfileDialog::BCancel_Clicked()
  37. {
  38.     this->close();  
  39. }
  40.  
  41. void ProfileDialog::BCreateAccount_Clicked()
  42. {
  43.     ui->BCreateAccount->setEnabled(false);
  44.    
  45.    
  46.     socket = new QTcpSocket(this);
  47.     connect(socket, SIGNAL(readyRead()), this, SLOT(ReceivedPacket()));
  48.     connect(socket, SIGNAL(error(QAbstractSocket::SocketError)),
  49.             this, SLOT(connectionError(QAbstractSocket::SocketError)));
  50.    
  51.     QStringList list = ui->LEServerIP->text().split(":");
  52.     if(list.length() != 2)
  53.     {
  54.         ui->BCreateAccount->setEnabled(true);
  55.         QMessageBox::warning(this, "Blad", "IP musi byc w formacie host:port");
  56.         return;  
  57.     }
  58.    
  59.     QString ip = list.at(0);
  60.     int port = list.at(1).toInt();
  61.    
  62.     socket->connectToHost(QHostAddress::QHostAddress(ip), port);
  63.    
  64.     QByteArray block;
  65.     QDataStream out(&block, QIODevice::WriteOnly);
  66.     out.setVersion(QDataStream::Qt_4_8);
  67.    
  68.     out << 0;
  69.     out << P_Register;
  70.     out << ui->LEServerLogin->text();
  71.     out << ui->LEServerPassword->text();
  72.     out.device()->seek(0);
  73.     out << block.size()-sizeof(int);
  74.    
  75.     socket->write(block);
  76. }
  77.  
  78. void ProfileDialog::ReceivedPacket()
  79. {
  80.     QDataStream in(socket);
  81.     in.setVersion(QDataStream::Qt_4_8);
  82.    
  83.     qDebug() << socket->bytesAvailable();
  84.     int PacketID;
  85.     in >> PacketID;
  86.    
  87.     if(PacketID == P_RegisterResponse)
  88.     {
  89.         int response;
  90.         in >> response;
  91.         qDebug() << "Response: " << response;
  92.         if(response == 1)
  93.             QMessageBox::warning(this, "Zakonczono", "Konto zostalo utworzone.");
  94.         else
  95.             QMessageBox::warning(this, "Blad", "Nie udalo sie utworzyć konta.");
  96.         ui->BCreateAccount->setEnabled(true);
  97.     }
  98.     else
  99.     {
  100.         socket->readAll();
  101.     }
  102. }
  103.  
  104. void ProfileDialog::connectionError(QAbstractSocket::SocketError error)
  105. {
  106.     //popr: trlumaczenie na polski
  107.     qDebug() << error;
  108.     switch (error) {
  109.     case QAbstractSocket::RemoteHostClosedError:
  110.         break;
  111.     case QAbstractSocket::HostNotFoundError:
  112.         QMessageBox::information(this, tr("KominikatorDaZe"),
  113.                                  tr("Nie odnaleziono serwera. Prosze sprawdzic "
  114.                                     "ip oraz port."));
  115.         break;
  116.     case QAbstractSocket::ConnectionRefusedError:
  117.         QMessageBox::information(this, tr("KomunikatorDaZe"),
  118.                                  tr("Polaczenie zostalo odrzucone przez hosta. "
  119.                                     "Upewnij sie, ze serwer dziala oraz"
  120.                                     "sprawdz poprawnosc adresu ip oraz portu "));
  121.         break;
  122.     default:
  123.         QMessageBox::information(this, tr("KominukatorDaZe"),
  124.                                  tr("Wystapil blad: %1.")
  125.                                  .arg(socket->errorString()));
  126.     }
  127.     ui->BCreateAccount->setEnabled(true);
  128.     if(socket)
  129.     {
  130.         delete socket;
  131.         socket = NULL;
  132.     }
  133. }
  134.  
  135. void ProfileDialog::BCreateProfile_Clicked()
  136. {
  137.     int length = ui->LEProfileName->text().length();
  138.     if(length > 30)
  139.     {
  140.         QMessageBox::warning(this, tr("Niepoprawne dane"), tr("Nazwa profilu moze miec max 30 znakow."));
  141.         return;
  142.     }
  143.     else if(length == 0)
  144.     {
  145.         QMessageBox::warning(this, tr("Niepoprawne dane"), tr("Wpisz nazwe profilu."));
  146.         return;
  147.     }
  148.    
  149.    
  150.     QSqlQuery query(*db);
  151.     query.prepare("SELECT profilename FROM profile WHERE profilename = :pname");
  152.     query.bindValue(":profilename", ui->LEProfileName->text());
  153.     query.exec();
  154.     if(query.next())
  155.     {
  156.         QMessageBox::warning(this, tr("Blad"), tr("Konto o takiej nazwie juz istnieje."));
  157.         return;
  158.     }
  159.    
  160.     if(ui->LEProfilePassword->text().length() > 30)
  161.     {
  162.         QMessageBox::warning(this, tr("Niepoprawne dane"), tr("Haslo profilu moze miec max 30 znakow."));
  163.         return;
  164.     }
  165.    
  166.     length = ui->LEServerLogin->text().length();
  167.     if(length > 30)
  168.     {
  169.         QMessageBox::warning(this, tr("Niepoprawne dane"), tr("Haslo logowania moze miec max 30 znakow."));
  170.         return;
  171.     }
  172.     else if(length == 0)
  173.     {
  174.         QMessageBox::warning(this, tr("Niepoprawne dane"), tr("Wpisz login do serwera."));
  175.         return;
  176.     }
  177.    
  178.     length = ui->LEServerPassword->text().length();
  179.     if(length > 30)
  180.     {
  181.         QMessageBox::warning(this, tr("Niepoprawne dane"), tr("Haslo logowania moze miec max 30 znakow."));
  182.         return;
  183.     }
  184.     else if(length == 0)
  185.     {
  186.         QMessageBox::warning(this, tr("Niepoprawne dane"), tr("Wpisz haslo logowania."));
  187.         return;
  188.     }
  189.    
  190.     query.prepare("INSERT INTO profile (login, profilename, lpassword, spassword, server) "
  191.                   "VALUES (:login, :profilename, :lpassword, :spassword, :server)");
  192.     query.bindValue(":profilename", ui->LEProfileName->text());
  193.     query.bindValue(":lpassword", b64_enc(ui->LEProfilePassword->text()));
  194.     query.bindValue(":login", ui->LEServerLogin->text());
  195.     query.bindValue(":spassword", b64_enc(ui->LEServerPassword->text()));
  196.     query.bindValue(":server", ui->LEServerIP->text());
  197.     query.exec();
  198.    
  199.     if(query.numRowsAffected() == 0)
  200.         QMessageBox::warning(this, tr("Blad"), tr("Tworzenie profilu nieudane."));
  201.     else
  202.         QMessageBox::information(this, tr("Informacja"), tr("Profil utworzony."));
  203. }
  204.  
  205. QString ProfileDialog::b64_enc(QString text)
  206. {
  207.    QByteArray toencode;
  208.    toencode.append(text);
  209.    return toencode.toBase64();
  210. }
  211.  
  212. ProfileDialog::~ProfileDialog()
  213. {
  214.     if(socket)
  215.     {
  216.         delete socket;
  217.         socket = NULL;
  218.     }
  219.     delete ui;
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement