-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.cxx
1088 lines (890 loc) · 32.3 KB
/
MainWindow.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "MainWindow.h"
#include "dialog.h"
#include "registration.h"
#include "protocol.h"
#include "settingsdialog.h"
#include "dialogadduser.h"
#include "newuserauthdialog.h"
#include "newdataauthdialog.h"
#include "dialogdeletefriendship.h"
#include "chatwidget.h"
#include "newtab.h"
#include "filetransfersender.h"
// Own classes to minify my sourcecode and make it more readable
#include "error.h"
#include <QRegExp>
#include <QMessageBox>
#include <QMenu>
#include <QSystemTrayIcon>
#include <QIcon>
#include <QKeyEvent>
#include <QFileDialog>
#include <QTimer>
#include <QDataStream>
#include <QCryptographicHash>
#include <QHostAddress>
#include <QSettings>
#include <QMultiMap>
#include <QFileInfo>
// This is our MainWindow constructor (you C++ n00b)
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
// When using Designer, you should always call setupUi(this)
// in your constructor. This creates and lays out all the widgets
// on the MainWindow that you setup in Designer.
setupUi(this);
// load icon
QIcon icon = QIcon(":/linux2.png");
// build menu
QMenu *trayMenu = new QMenu(this);
QAction *action1 = new QAction("Datei senden an",this);
QAction *action2 = new QAction("Beenden",this);
connect(action1, SIGNAL(triggered()), this, SLOT(on_actionSend_Data_triggered()) );
connect(action2, SIGNAL(triggered()), this, SLOT(close()) );
trayMenu->addAction(action1);
trayMenu->addAction(action2);
// set up and show the system tray icon
/*QSystemTrayIcon *trayIcon = new QSystemTrayIcon(this);
trayIcon->setIcon(icon);
trayIcon->setContextMenu(trayMenu);
trayIcon->show();
*/
// Make sure that we are showing the login page when we startup:
stackedWidget->setCurrentWidget(loginPage);
// Instantiate our socket (but don't actually connect to anything
// yet until the user clicks the loginButton:
socket = new QTcpSocket(this);
registrationTimer = new QTimer(this);
waitToRegistration = 120000;
RegistrationMode = true;
connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
connect(socket, SIGNAL(connected()), this, SLOT(connected()));
connect(socket, SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(socketError(QAbstractSocket::SocketError)));
connect(sayLineEdit,SIGNAL(textChanged(QString)),this,SLOT(setCountValue(QString)));
connect(userListWidget,SIGNAL(itemDoubleClicked(QListWidgetItem*)),this,SLOT(wasDoubleClicked(QListWidgetItem*)));
debug = new Debug();
QSettings s;
LineEditEmail->setText(s.value("UserEmail").toString());
if(s.value("FileTransferPath").toString() == "")
{
qDebug() << QDir::homePath();
debug->debugging(QDir::homePath());
QFileInfo path (QDir::homePath() + "/LagunaChat/downloads");
if(path.exists())
{
s.setValue("FileTransferPath",QDir::homePath() + "/LagunaChat/downloads");
}
else
{
QDir().mkdir(QDir::homePath() + "/LagunaChat/downloads");
s.setValue("FileTransferPath",QDir::homePath() + "/LagunaChat/downloads");
}
}
cw = new ChatWidget();
in.setDevice(socket);
out.setDevice(socket);
error = new Error();
}
void MainWindow::on_loginButton_clicked()
{
qDebug() << "LoginBUtton wurde geklickt";
debug->debugging("LoginBUtton wurde geklickt");
QDataStream out(socket);
out << (int) 4;
out << LineEditEmail->text();
out << toMD5(LineEditPassword->text());
out << "\n";
qDebug() << "Bytes written: " << socket->bytesToWrite();
qDebug() << "Status of Datastream" << out.status();
debug->debugging("Bytes written: " + socket->bytesToWrite());
debug->debugging("Status of Datastream" + out.status());
email = LineEditEmail->text();
QSettings s;
s.setValue("UserEmail",email);
disconnect(actionLogin,0,0,0);
connect(actionLogin,SIGNAL(triggered()),this,SLOT(logout_triggered()));
}
// This gets called when the user clicks the sayButton (next to where
// they type text to send to the chat room):
void MainWindow::on_sayButton_clicked()
{
// What did they want to say (minus white space around the string):
QString message = sayLineEdit->text().trimmed();
// Kills the Programm by this message
if(message == "/exit")
{
MainWindow::close();
}
// Delete written text in the chatwindow
else if(message == "/clear")
{
roomTextEdit->clear();
}
// Only send the text to the chat server if it's not empty:
else if(!message.isEmpty())
{
QDataStream out(socket);
//QString regi(" + reg->getUsername() + ":" + reg->getPassword() + "\n");
//int sizeOfText = regi.size();
out << (int) 3;
out << ipv4.toString();
out << QString("all");
out << QString("none");
out << message;
out << "\n";
qDebug() << "Bytes written: " << socket->bytesToWrite();
qDebug() << "Status of Datastream" << out.status();
}
// Clear out the input box so they can type something else:
sayLineEdit->clear();
// Put the focus back into the input box so they can type again:
sayLineEdit->setFocus();
}
// This function gets called whenever the chat server has sent us some text:
void MainWindow::readyRead()
{
// We'll loop over every (complete) line of text that the server has sent us:
while(socket->canReadLine())
{
QDataStream in(socket);
int messageID;
QString temp;
in >> messageID;
qDebug() << "messageID: " << messageID;
debug->debugging("messageID: " + messageID);
switch(messageID)
{
case 0: {
int errorID;
in >> errorID;
qDebug() << "Errornumber: " << errorID;
debug->debugging("Errornumber: " + errorID);
switch(errorID)
{
case 0: error->registrationFailed(); break;
case 1: error->loginFailed(); break;
case 2: error->sendFileFailedOffline(); break;
}
}break;
case 2: {
int stateID;
in >> stateID;
qDebug() << "Current status: " << stateID;
debug->debugging("Current status: " + stateID);
switch(stateID)
{
case 1: changePageChat(); break;
case 2: break;
}
}break;
case 3: gotTextMessage(); break;
case 5: {
int reQuestID;
in >> reQuestID;
switch(reQuestID)
{
case 3: newUserQuestion(); break;
case 6: newUserUpdate(); break;
case 7: hasUserAcceptFile(); break;
case 8: setupFileSender(); break;
case 9: {
// Request to get offline messages
// now get offline messages from friends
QDataStream out(socket);
out << (int) 5;
out << (int) 8;
out << this->email;
out << "\n";
break;
}
case 10: setupAcceptFileDialog(); break;
case 11: setupFileReceiver(); break;
}
}break;
case 6: setupUserlistAfterLogin(); break;
case 7: {
int answerID;
in >> answerID;
switch(answerID)
{
case 2: friendIsOnline(); break;
case 3: friendIsOffline(); break;
case 4: gotOfflineTextMessage(); break;
}
}break;
}
in >> temp;
qDebug() << "Endzeichen gelesen";
debug->debugging("Endzeichen gelesen");
}
}
// This function gets called when our socket has successfully connected to the chat
// server. (see the connect() call in the MainWindow constructor).
void MainWindow::connected()
{
qDebug() << "Verbindung zum Server hergestellt";
debug->debugging("Verbindung zum Server hergestellt");
statusBarMain->showMessage("Verbindung zum Server hergestellt");
ipv4 = socket->peerAddress();
qDebug() << socket->errorString();
debug->debugging(socket->errorString());
qDebug() << "Info: " << ipv4.toString() << " ||| ";
debug->debugging("Info: " + ipv4.toString() + " ||| ");
actionLogin->setVisible(true);
actionRegistration->setVisible(true);
actionAdd_User->setVisible(true);
actionDelete_User->setVisible(true);
actionCreate_Room->setVisible(true);
actionDelete_Room->setVisible(true);
actionSend_Data->setVisible(true);
actionLogin->setEnabled(true);
actionRegistration->setEnabled(true);
actionAdd_User->setEnabled(true);
actionDelete_User->setEnabled(true);
actionCreate_Room->setEnabled(true);
actionDelete_Room->setEnabled(true);
actionSend_Data->setVisible(true);
loginButton->setEnabled(true);
}
void MainWindow::wasDoubleClicked(QListWidgetItem *user)
{
qDebug("DOUBLEClicked");
if(!cw->isWindow())
{
qDebug() << "Erstelle Chatfenster";
debug->debugging("Erstelle Chatfenster");
cw->show();
}
cw->show();
if(!cw->existTab(user->text()))
{
cw->addTab(new newTab(0,socket),user->text());
}
cw->raise();
}
void MainWindow::setCountValue(QString newValue)
{
QString value = QString::number(sayLineEdit->text().size());
newValue = "Zeichen: " + value;
charLabel->setText(newValue);
}
void MainWindow::on_actionRegistration_triggered()
{
if(RegistrationMode)
{
Registration *reg = new Registration();
reg->show();
if(reg->exec())
{
qDebug() << "Ein neuer User wird registriert";
qDebug() << "Seine EMail: " << reg->getEmail();
qDebug() << "Sein Username: " << reg->getUsername();
qDebug() << "Passwort1: " << reg->getPasswordFirst();
qDebug() << "Passwort2: " << reg->getPasswordSecond();
debug->debugging("Ein neuer User wird registriert");
QDataStream out(socket);
//QString regi(" + reg->getUsername() + ":" + reg->getPassword() + "\n");
//int sizeOfText = regi.size();
QString email(reg->getEmail());
QString username(reg->getUsername());
QString passwordFi(reg->getPasswordFirst());
QString passwordSe(reg->getPasswordSecond());
QString emailPatt = "\\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}\\b";
QRegExp rx(emailPatt);
rx.setCaseSensitivity(Qt::CaseInsensitive);
rx.setPatternSyntax(QRegExp::RegExp);
if(!rx.exactMatch(email))
{
QMessageBox msgBox;
msgBox.setText("Email-Address is not vaild!");
msgBox.setWindowTitle("Infomation");
msgBox.setIcon(QMessageBox::Information);
msgBox.exec();
return;
}
if(passwordFi != passwordSe)
{
QMessageBox msgBox;
msgBox.setText("Passwords are not identical!");
msgBox.setWindowTitle("Infomation");
msgBox.setIcon(QMessageBox::Information);
msgBox.exec();
return;
}
out << (int) 1;
out << email;
out << username;
out << passwordFi;
out << "\n";
QChar * ch = email.data();
while(!ch->isNull())
{
qDebug() << ch->unicode();
ch++;
}
RegistrationMode = false;
//registrationTimer->start(1800000);
registrationTimer->start(1800);
connect(registrationTimer,SIGNAL(timeout()),this,SLOT(changeRegistrationMode()));
}delete reg;
}
else
{
qDebug() << "Registrierung erst in 30 Minuten wieder m�glich";
qDebug() << "Bitte warten...";
debug->debugging("Registrierung erst in 30 Minuten wieder m�glich");
debug->debugging("Bitte warten...");
}
}
void MainWindow::changeRegistrationMode()
{
registrationTimer->stop();
RegistrationMode = true;
}
void MainWindow::on_actionSettings_triggered()
{
debug->debugging("Settings were triggered");
SettingsDialog *setting = new SettingsDialog(this);
setting->show();
if(setting->exec())
{
setting->saveSettings();
}
}
QString MainWindow::toMD5(QString text)
{
return QString(QCryptographicHash::hash((text.toUtf8()),QCryptographicHash::Md5).toHex());
}
void MainWindow::socketError(QAbstractSocket::SocketError error)
{
qDebug() << "Socketerror: " << error;
debug->debugging("Socketerror: " + error);
if(error == QAbstractSocket::RemoteHostClosedError)
{
actionLogin->setText("Login");
disconnect(actionLogin,0,0,0);
connect(actionLogin,SIGNAL(triggered()),this,SLOT(on_actionLogin_triggered()));
stackedWidget->setCurrentWidget(loginPage);
userListWidget->clear();
cw->close();
QMessageBox msgBox;
msgBox.setText("Server hat die Verbindung abgebrochen");
msgBox.setWindowTitle("Infomation");
msgBox.setIcon(QMessageBox::Information);
msgBox.exec();
}
}
void MainWindow::on_actionConnect_triggered()
{
QSettings s;
qDebug() << "Verbindung zu: " << s.value("ServerAddress").toString() << " Port: " << s.value("ServerPort").toInt();
debug->debugging("Verbindung zu: " + s.value("ServerAddress").toString() + " Port: " + s.value("ServerPort").toString());
socket->connectToHost(s.value("ServerAddress").toString(),s.value("ServerPort").toInt());
}
void MainWindow::on_actionLogin_triggered()
{
qDebug() << "ActionLogin getriggerd";
debug->debugging("ActionLogin getriggerd");
emit on_loginButton_clicked();
}
void MainWindow::logout_triggered()
{
qDebug() << "Logout";
debug->debugging("Logout");
statusBarMain->showMessage("Logout");
QDataStream out(socket);
out << (int) 2;
out << (int) 3;
out << email;
out << "\n";
socket->close();
actionLogin->setText("Login");
disconnect(actionLogin,0,0,0);
connect(actionLogin,SIGNAL(triggered()),this,SLOT(on_actionLogin_triggered()));
actionLogin->setVisible(false);
actionRegistration->setVisible(false);
actionAdd_User->setVisible(false);
actionDelete_User->setVisible(false);
actionCreate_Room->setVisible(false);
actionDelete_Room->setVisible(false);
loginButton->setEnabled(false);
cw->clear();
cw->close();
stackedWidget->setCurrentWidget(loginPage);
}
void MainWindow::on_actionAdd_User_triggered()
{
DialogAddUser *dia = new DialogAddUser(this);
dia->show();
if(dia->exec())
{
QDataStream out(socket);
QString email(dia->getEmail());
out << (int) 5;
out << (int) 2;
out << this->email;
out << email;
out << "\n";
}
}
void MainWindow::on_actionDelete_User_triggered()
{
DialogDeleteFriendship *dia = new DialogDeleteFriendship(this);
dia->show();
if(dia->exec())
{
QDataStream out(socket);
QString email(dia->getEmail());
out << (int) 5;
out << (int) 7;
out << this->email;
out << email;
out << "\n";
for(int i = 0; i < userListWidget->count(); i++){
if(userListWidget->item(i)->text() == email){
delete userListWidget->takeItem(i);
}
}
}
}
void MainWindow::on_actionCreate_Room_triggered()
{
}
void MainWindow::on_actionDelete_Room_triggered()
{
}
void MainWindow::on_actionQuit_triggered()
{
qDebug() << "Programm cloesd";
qDebug() << "Logout";
debug->debugging("Program closed:::Logout");
statusBarMain->showMessage("Logout");
QDataStream out(socket);
out << (int) 2;
out << (int) 3;
out << email;
out << "\n";
socket->close();
debug->close();
this->close();
}
bool MainWindow::checkUserInList(QString user)
{
qDebug() << "***FUNKTION***-->checkUserInList";
debug->debugging("***FUNKTION***-->checkUserInList");
for(int i=0; i < userListWidget->count(); i++){
if(user == userListWidget->item(i)->text()){
return true;
}
}
return false;
}
QListWidgetItem* MainWindow::getUserInList(QString user)
{
qDebug() << "***FUNKTION***-->getUserInlist";
debug->debugging("***FUNKTION***-->getUserInlist");
for(int i=0; i < userListWidget->count(); i++){
if(user == userListWidget->item(i)->text()){
return userListWidget->item(i);
}
}
return 0;
}
void MainWindow::on_actionSend_Data_triggered()
{
qDebug() << "***FUNKTION***-->on_actionSend_Data_triggered";
debug->debugging("***FUNKTION***-->on_actionSend_Data_triggered");
Dialog *filesend = new Dialog();
for(int i = 0; i < userListWidget->count(); i++)
{
QString itemData = userListWidget->item(i)->text();
filesend->listWidget->addItem(itemData);
}
qDebug() << filesend->listWidget->currentItem();
debug->debugging(filesend->listWidget->currentItem()->text());
filesend->show();
if(filesend->listWidget->count() == 0)
{
filesend->close();
QMessageBox msgBox;
msgBox.setText("Du hast leider keine Freunde mit denen du Dateien tauschen könntest oder vergessen einen Freund auszuwählen");
msgBox.setWindowTitle("Infomation");
msgBox.setIcon(QMessageBox::Information);
msgBox.exec();
}
if(filesend->exec())
{
// FIRST OF ALL CHECK: selected user nad filename
if(filesend->listWidget->selectedItems().count() == 0)
{
QMessageBox msgBox;
msgBox.setText("Du hast leider keinen Freund ausgewählt");
msgBox.setWindowTitle("Infomation");
msgBox.setIcon(QMessageBox::Information);
msgBox.exec();
return;
}
// IF a user was selected then we can go on and get the path of file and email of user
QString toUser = filesend->listWidget->item(filesend->listWidget->currentRow())->text();//user@host
QString fileName = filesend->datei->text();
if(fileName == "")
{
QMessageBox msgBox;
msgBox.setText("Du hast leider keine Datei ausgewählt");
msgBox.setWindowTitle("Infomation");
msgBox.setIcon(QMessageBox::Information);
msgBox.exec();
return;
}
QFileInfo info(fileName);
this->fileName = info.fileName();
qDebug() << "Original: " << fileName << "Aka: " << info.fileName();
debug->debugging("Original: " + fileName + "Aka: " + info.fileName());
this->filePath = fileName;
QDataStream out(socket);
out << (int) 6;
out << toUser;
out << this->email;
out << this->fileName;
out << "\n";
}
delete filesend;
}
void MainWindow::changePageChat()
{
qDebug() << "***FUNKTION***-->changePageChat";
debug->debugging("***FUNKTION***-->changePageChat");
// Flip over to the chat page:
stackedWidget->setCurrentWidget(chatPage);
actionLogin->setText("Logout");
connect(actionLogin,SIGNAL(triggered()),this,SLOT(logout_triggered()));
sayLineEdit->setFocus();
QString name;
in >> name;
QSettings s;
s.setValue("Username",name);
//sende anfrage nach der userliste
QDataStream out(socket);
out << (int) 5;
out << (int) 1;
out << this->email;
out << "\n";
}
void MainWindow::gotTextMessage()
{
qDebug() << "***FUNKTION***-->gotTextMessage";
debug->debugging("***FUNKTION***-->gotTextMessage");
QString fromMail;
QString toMail;
QString room;
QString message;
in >> fromMail;
in >> toMail;
in >> room;
in >> message;
qDebug() << "A message from " << fromMail << " to " << toMail << endl
<< "Room: " << room << endl
<< "Message: " << message;
debug->debugging("A message from " + fromMail + " to " + toMail + "Room: " + room + "Message: " + message);
// look if the user has an open tab in the chatwidget
bool exist = false;
for(int i = 0; i < cw->count(); i++)
{
if(fromMail == cw->tabText(i))
{
// uhh the tab exist and we can write the text into the widget
// first we need access to the textfield in the specified tab
newTab *chat = (newTab*)cw->widget(i);
chat->writeInTextfield(message);
cw->raise();
exist = true;
// so the message is okay return here
break;
}
}
// if we come to here then there is no tab for the user
// we create on
if(!exist)
{
cw->addTab(new newTab(0,socket),fromMail);
qDebug() << "Tabs: " << cw->count();
debug->debugging("Tabs: " + cw->count());
newTab *chat = (newTab*)cw->widget(cw->count()-1);
cw->show();
chat->writeInTextfield(message);
cw->raise();
}
}
void MainWindow::newUserQuestion()
{
qDebug() << "***FUNKTION***-->newUserQuestion";
debug->debugging("***FUNKTION***-->newUserQuestion");
QMap<QString,QString> list;
in >> list;
QMapIterator<QString,QString>i(list);
while (i.hasNext())
{
i.next();
newUserAuthDialog *newUser = new newUserAuthDialog(this);
newUser->setEmailText(QString(i.key()));
newUser->setUsernametext(QString(i.value()));
newUser->show();
newUser->exec();
if(newUser->result())
{
//send message to server that the user accepts the other user to his list
qDebug() << "Client akzeptiert Freundesabfrage ab";
debug->debugging("Client akzeptiert Freundesabfrage ab");
QDataStream out(socket);
out << (int) 5;
out << (int) 5;
out << email;
out << QString(i.key());
out << (int) 1;
out << "\n";
}
else
{
//send message to server that the user dont accept the other user
qDebug() << "Client lehnt Freundesabfrage ab";
debug->debugging("Client lehnt Freundesabfrage ab");
QDataStream out(socket);
out << (int) 5;
out << (int) 5;
out << email;
out << QString(i.key());
out << (int) 0;
out << "\n";
}
}
qDebug() << "These Users wants to be a friend with you: " << list.size() << list.values();
debug->debugging("These Users wants to be a friend with you: ");
}
void MainWindow::newUserUpdate()
{
qDebug() << "***FUNKTION***-->newUserUpdate";
debug->debugging("***FUNKTION***-->newUserUpdate");
// get a new user and update the userlist
QString user;
in >> user;
new QListWidgetItem(QPixmap(":/user.png"), user, userListWidget);
}
void MainWindow::hasUserAcceptFile()
{
qDebug() << "***FUNKTION***-->hasUserAcceptFile";
debug->debugging("***FUNKTION***-->hasUserAcceptFile");
int ok = 0;
QString emailFromChoosedUser;
in >> ok;
in >> emailFromChoosedUser;
if(ok == 1)
{
QString ipFromUser;
in >> ipFromUser;
qDebug() << "User(" << ipFromUser << "==" << emailFromChoosedUser << ") accepted. So we can open the Connection";
debug->debugging("User(" + ipFromUser + "==" + emailFromChoosedUser + ") accepted. So we can open the Connection");
// send an info that the server is now listening
setupFileSender();
QDataStream out(socket);
out << (int) 7;
out << this->email;
out << emailFromChoosedUser;
out << "\n";
}
else
{
qDebug() << "User dont wats your fucking file!! ";
debug->debugging("User dont wats your fucking file!! ");
QMessageBox msgBox;
msgBox.setText("User dont wats your fucking file!!");
msgBox.setWindowTitle("Infomation");
msgBox.setIcon(QMessageBox::Information);
msgBox.exec();
}
}
void MainWindow::setupAcceptFileDialog()
{
qDebug() << "***FUNKTION***-->setupAcceptFileDialog";
debug->debugging("***FUNKTION***-->setupAcceptFileDialog");
QString fromEmail;
QString fromUserIp;
QString fromUser;
QString filename;
in >> fromEmail;
in >> fromUserIp;
in >> fromUser;
in >> filename;
this->fileName = filename;
newDataAuthDialog *newFile = new newDataAuthDialog(this);
newFile->setEmailText(fromEmail);
newFile->setUsernametext(fromUser);
newFile->setFileText(this->fileName);
newFile->show();
newFile->exec();
if(newFile->result())
{
// send message to server that the user accepts the other user to his list
qDebug() << "Client akzeptiert Datei von: " << fromUserIp;
debug->debugging("Client akzeptiert Datei von: " + fromUserIp);
QDataStream out(socket);
out << (int) 5;
out << (int) 6;
out << this->email; // Email from User who get the File Question
out << fromEmail; // Email from User who send the File Question
out << (int) 1;
out << "\n";
}
else
{
//send message to server that the user dont accept the other user
qDebug() << "Client lehnt Datei ab";
debug->debugging("Client lehnt Datei ab");
QDataStream out(socket);
out << (int) 5;
out << (int) 6;
out << this->email; // Email from User who get the File Question
out << fromEmail; // Email from User who send the File Question
out << (int) 0;
out << "\n";
}
}
void MainWindow::setupFileSender()
{
qDebug() << "***FUNKTION***-->setupFileSender";
debug->debugging("***FUNKTION***-->setupFileSender");
// setup server and socket for file transfer
fileSender = new FileTransferSender(this);
fileSender->listen(QHostAddress::Any,4242);
fileSender->setFilePath(this->filePath);
qDebug() << "Is sender listening? " << fileSender->isListening();
debug->debugging("Is sender listening? " + fileSender->isListening());
}
void MainWindow::setupFileReceiver()
{
qDebug() << "***FUNKTION***-->setupFileReceiver";
debug->debugging("***FUNKTION***-->setupFileReceiver");
QString senderIP;
in >> senderIP;
// now setup the tcpsocket and listen to the port 4242
fileReceiver = new FileTransferReceiver(this);
fileReceiver->connectToHost(QHostAddress(senderIP),4242);
fileReceiver->setFileName(this->fileName);
}
void MainWindow::setupUserlistAfterLogin()
{
qDebug() << "***FUNKTION***-->setupUserlistAfterLogin";
debug->debugging("***FUNKTION***-->setupUserlistAfterLogin");
// get the Userlist after login
qDebug() << "get the Userlist after login";
debug->debugging("get the Userlist after login");
QMap<QString, bool> userlist;
in >> userlist;
QMapIterator<QString, bool> i(userlist);
userListWidget->clear();
while(i.hasNext())
{
i.next();
if(i.value())
{
new QListWidgetItem(QPixmap(":/user.png"), i.key(), userListWidget);
}
else
{
new QListWidgetItem(QPixmap(":/user_offline.png"), i.key(), userListWidget);
}
}
qDebug() << userlist.size() << "User are copied to the list";
debug->debugging(userlist.size() + "User are copied to the list");
out << (int) 5;
out << (int) 4;
out << email;
out << "\n";
}
void MainWindow::gotOfflineTextMessage()
{
qDebug() << "***FUNKTION***-->gotOfflineTextMessage";
debug->debugging("***FUNKTION***-->gotOfflineTextMessage");
// ungelesene Nachrichten
QMultiMap<int, QString> list;
in >> list;
int count_messages = (list.size()/4);
qDebug() << "Anzahl ungelesener Nachrichten: " << count_messages;
debug->debugging("Anzahl ungelesener Nachrichten: " + count_messages);
for (int j = 0; j < count_messages; j++)
{
QList<QString> messageItem = list.values(j+1);
qDebug() << "Date: " << messageItem.at(0) << " Message: " << messageItem.at(1) << " FromEmail: " << messageItem.at(2) << "FromName: " << messageItem.at(3);
QString message = messageItem.at(0);