Repository navigation
Expand file tree
/
Copy pathUserAccount.cpp
More file actions
858 lines (702 loc) · 25.8 KB
/
Copy pathUserAccount.cpp
File metadata and controls
858 lines (702 loc) · 25.8 KB
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
#include "UserAccount.h"
#include "Misc.h"
#include "UserForm.h"
void PrintDate(SYSTEMTIME d)
{
cout << d.wDay << "-" << d.wMonth << "-" << d.wYear << " " << d.wHour << ":" << d.wMinute << endl;
}
bool compareByTime(const pair<UserMessage, int>& a, const pair<UserMessage, int>& b) {
::FILETIME aFileTime, bFileTime;
SystemTimeToFileTime(&a.first.SentDate, &aFileTime);
SystemTimeToFileTime(&b.first.SentDate, &bFileTime);
return CompareFileTime(&aFileTime, &bFileTime) < 0;
}
UserAccount::UserAccount(void)
{
}
UserAccount::UserAccount(int id, string username, string pw)
{
Set(id, username, pw);
}
void UserAccount::Set(int id, string username, string pw)
{
m_id = id;
m_username = username;
m_password = pw;
}
int UserAccount::ID(void) const
{
return m_id;
}
string UserAccount::Username(void) const
{
return m_username;
}
string UserAccount::Password(void) const
{
return m_password;
}
bool UserAccount::AddContact(int User_ID) {
if (ReceivedMessages.find(User_ID) != ReceivedMessages.end()) {
if (Contacts.find(User_ID) == Contacts.end()) {
Contacts.insert(User_ID);
return true;
}
}
return false;
}
bool UserAccount::RemoveContact(int User_ID) {
if (Contacts.find(User_ID) != Contacts.end()) {
Contacts.erase((User_ID));
return true;
}
return false;
}
bool UserAccount::Block(int User_ID)
{
if (!IsBlocked(User_ID))
{
Blocked.insert(User_ID);
return true;
}
return false;
}
bool UserAccount::Unblock(int User_ID)
{
return Blocked.erase(User_ID);
}
char UserAccount::SendUserMessage(UserAccount* recipient, string content)
{
if (!recipient)
return 1;
else if (IsBlocked(recipient->m_id))
return 2;
else if (recipient->IsBlocked(m_id))
return 3;
SYSTEMTIME time;
GetSystemTime(&time);
UserMessage msg{ 0, content, time };
// >> SENDER SIDE <<
msg.Index = (!SentMessages.empty() ? SentMessages.top().second.Index : 0) + 1;
SentMessages.push(pair<int, UserMessage>(recipient->m_id, msg));
// >> RECIPIENT/RECEIVER SIDE <<
auto it = recipient->ReceivedMessages.find(m_id);
// if received msgs from this user before
if (it != recipient->ReceivedMessages.end())
{
it->second.push(msg);
}
// first time to receive msg from this user
else
{
stack<UserMessage> msgs;
msgs.push(msg);
recipient->ReceivedMessages[m_id] = msgs;
}
return 0;
}
bool UserAccount::PopUserMessage(UserAccount* user) {
// Pop from sender side
if (SentMessages.empty())
return false;
UserMessage tempMsg= SentMessages.top().second;
SentMessages.pop();
// Pop from receiver side
auto it = user->ReceivedMessages.find(m_id);
if (it == user->ReceivedMessages.end() || it->second.empty()) {
return false;
}
else {
it->second.pop();
return true;
}
// Remove if in favorite
user->RemoveFavorite(user->m_id, tempMsg.Index);
}
void UserAccount::SetSentMessageSeen(int Receiver_ID, int Msg_Index, bool seen)
{
stack<pair<int, UserMessage>> sent;
pair<int, UserMessage> msg;
while (!SentMessages.empty())
{
msg = SentMessages.top();
if (msg.first == Receiver_ID && msg.second.Index == Msg_Index)
msg.second.Seen = true;
sent.push(msg);
SentMessages.pop();
}
while (!sent.empty())
{
SentMessages.push(sent.top());
sent.pop();
}
}
Panel^ UserAccount::CreateMessageBox(String^ message)
{
const int MaxWidthPerLine = 2600;
Panel^ messageContainer = gcnew Panel();
RichTextBox^ messageBox = gcnew RichTextBox();
int count = 0;
String^ messageContent = message;
// Single character size
int charsize = TextRenderer::MeasureText("W", messageBox->Font).Width;
// Maximum characters per line
charsize = MaxWidthPerLine / charsize;
//Limiting the number of characters per line
String^ processedText = "";
for each (char word in messageContent)
{
processedText += Char::ToString(word);
if (count > 0 && !(count % charsize))
processedText += "\n";
count++;
}
messageBox->Text = processedText;
//Measure size of text in textbox
Drawing::Size size = TextRenderer::MeasureText(messageBox->Text, messageBox->Font);
// Adjust the size of the textbox to fit the text
messageBox->Width = size.Width;
messageBox->Height = size.Height;
//Adjust the size of the panel to fit the textbox
messageContainer->Width = messageBox->Width + 20;
messageContainer->Height = messageBox->Height + 20;
//Properties for Panel
messageContainer->BorderStyle = BorderStyle::None;
messageContainer->BackColor = Color::DarkSlateGray;
messageContainer->Padding = System::Windows::Forms::Padding(10);
//Properties for Textbox
messageBox->BorderStyle = BorderStyle::None;
messageBox->BackColor = Color::DarkSlateGray;
messageBox->Dock = DockStyle::Fill;
messageBox->ForeColor = Color::White;
messageBox->Multiline = true;
messageBox->ReadOnly = true;
messageBox->WordWrap = true;
messageBox->MaxLength = 25;
//Add the textbox inside panel
messageContainer->Controls->Add(messageBox);
return messageContainer;
}
Panel^ UserAccount::CreateUIDPanel(Form^ form, String^ ID, String^ date, UserMessage& msg, string type)
{
int User_ID = atoi(SystemStringToCpp(ID).c_str());
Panel^ uIDContainer = gcnew Panel();
Label^ uIDLabel = gcnew Label();
Label^ uDateLabel = gcnew Label();
//Label for UID
uIDLabel->Text = "By UID: " + ID;
uIDLabel->ForeColor = msg.Seen ? Color::FromArgb(83, 189, 235) : Color::White;
uIDLabel->AutoSize = true;
//Label for date
uDateLabel->Text = date;
uDateLabel->ForeColor = msg.Seen ? Color::FromArgb(83, 189, 235) : Color::White;
uDateLabel->Location = Point(uIDLabel->Width, 0);
uDateLabel->AutoSize = true;
//Properties for Panel
uIDContainer->Width = uIDLabel->Width + uDateLabel->Width;
uIDContainer->Height = uIDLabel->Height;
uIDContainer->BorderStyle = BorderStyle::None;
uIDContainer->BackColor = Color::FromArgb(12, 12, 12);
uIDContainer->AutoSize = true;
//Adding the label inside panel
uIDContainer->Controls->Add(uIDLabel);
uIDContainer->Controls->Add(uDateLabel);
Button^ ContactButton = gcnew Button();
Button^ FavoriteButton = gcnew Button();
Button^ BlockButton = gcnew Button();
///
ContactButton->Text = "";
ContactButton->BackColor = Color::Transparent;
ContactButton->Location = Point(uDateLabel->Location.X + uDateLabel->Width + 25, -5);
ContactButton->FlatAppearance->BorderSize = 0;
ContactButton->FlatAppearance->BorderColor = System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(12)),
static_cast<System::Int32>(static_cast<System::Byte>(12)), static_cast<System::Int32>(static_cast<System::Byte>(12)));
ContactButton->FlatAppearance->BorderSize = 0;
ContactButton->FlatAppearance->MouseDownBackColor = System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(12)),
static_cast<System::Int32>(static_cast<System::Byte>(12)), static_cast<System::Int32>(static_cast<System::Byte>(12)));
ContactButton->FlatAppearance->MouseOverBackColor = System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(12)),
static_cast<System::Int32>(static_cast<System::Byte>(12)), static_cast<System::Int32>(static_cast<System::Byte>(12)));
ContactButton->FlatStyle = System::Windows::Forms::FlatStyle::Flat;
ContactButton->ForeColor = System::Drawing::Color::Transparent;
ContactButton->Image = System::Drawing::Image::FromFile(GetContact(User_ID) ? "img/IsContact.png" : "img/NotContact.png");
ContactButton->Name = ID;
ContactButton->Size = System::Drawing::Size(28, 28);
ContactButton->TabIndex = 4;
///
FavoriteButton->Text = "";
FavoriteButton->BackColor = Color::Transparent;
FavoriteButton->Location = Point(ContactButton->Location.X + ContactButton->Width + 2, -5);
FavoriteButton->FlatAppearance->BorderSize = 0;
FavoriteButton->FlatAppearance->BorderColor = System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(12)),
static_cast<System::Int32>(static_cast<System::Byte>(12)), static_cast<System::Int32>(static_cast<System::Byte>(12)));
FavoriteButton->FlatAppearance->BorderSize = 0;
FavoriteButton->FlatAppearance->MouseDownBackColor = System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(12)),
static_cast<System::Int32>(static_cast<System::Byte>(12)), static_cast<System::Int32>(static_cast<System::Byte>(12)));
FavoriteButton->FlatAppearance->MouseOverBackColor = System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(12)),
static_cast<System::Int32>(static_cast<System::Byte>(12)), static_cast<System::Int32>(static_cast<System::Byte>(12)));
FavoriteButton->FlatStyle = System::Windows::Forms::FlatStyle::Flat;
FavoriteButton->ForeColor = System::Drawing::Color::Transparent;
FavoriteButton->Image = System::Drawing::Image::FromFile(msg.IsFavorite ? "img/IsFavorite.png" : "img/NotFavorite.png");
FavoriteButton->Name = String::Format("{0},{1}", ID, CPPSTR2SYSTEM(to_string(msg.Index).c_str()));
FavoriteButton->Size = System::Drawing::Size(28, 28);
FavoriteButton->TabIndex = 4;
///
BlockButton->Text = "";
BlockButton->BackColor = Color::Transparent;
BlockButton->Location = Point(FavoriteButton->Location.X + FavoriteButton->Width + 2, -5);
BlockButton->FlatAppearance->BorderSize = 0;
BlockButton->FlatAppearance->BorderColor = System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(12)),
static_cast<System::Int32>(static_cast<System::Byte>(12)), static_cast<System::Int32>(static_cast<System::Byte>(12)));
BlockButton->FlatAppearance->BorderSize = 0;
BlockButton->FlatAppearance->MouseDownBackColor = System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(12)),
static_cast<System::Int32>(static_cast<System::Byte>(12)), static_cast<System::Int32>(static_cast<System::Byte>(12)));
BlockButton->FlatAppearance->MouseOverBackColor = System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(12)),
static_cast<System::Int32>(static_cast<System::Byte>(12)), static_cast<System::Int32>(static_cast<System::Byte>(12)));
BlockButton->FlatStyle = System::Windows::Forms::FlatStyle::Flat;
BlockButton->ForeColor = System::Drawing::Color::Transparent;
BlockButton->Image = System::Drawing::Image::FromFile(IsBlocked(User_ID) ? "img/IsBlocked.png" : "img/NotBlocked.png");
BlockButton->Name = ID;
BlockButton->Size = System::Drawing::Size(28, 28);
BlockButton->TabIndex = 4;
ContactButton->Click += gcnew System::EventHandler(static_cast<SarahaWithGUI::UserForm^>(form), &SarahaWithGUI::UserForm::MessageContact_Click);
FavoriteButton->Click += gcnew System::EventHandler(static_cast<SarahaWithGUI::UserForm^>(form), &SarahaWithGUI::UserForm::MessageFavorite_Click);
BlockButton->Click += gcnew System::EventHandler(static_cast<SarahaWithGUI::UserForm^>(form), &SarahaWithGUI::UserForm::MessageBlock_Click);
if (type == "Received") {
uIDContainer->Controls->Add(ContactButton);
uIDContainer->Controls->Add(FavoriteButton);
uIDContainer->Controls->Add(BlockButton);
}
return uIDContainer;
}
Panel^ UserAccount::CreateMainMessagePanel(Form^ form, int User_ID, UserMessage& msg, string type)
{
Panel^ MainPanel = gcnew Panel();
Panel^ MessageBoxPanel = CreateMessageBox(CPPSTR2SYSTEM(msg.Content.c_str()));
Panel^ UIDPanel = CreateUIDPanel(form, CPPSTR2SYSTEM(to_string(User_ID).c_str()), CPPSTR2SYSTEM(GetMessageDate(msg.SentDate).c_str()), msg, type);
//Properties of Main Panel
MainPanel->Width = MessageBoxPanel->Width;
UIDPanel->Location = Point(0, MessageBoxPanel->Height + 5);
MainPanel->AutoSize = true;
//Add Panels into Main Panel
MainPanel->Controls->Add(MessageBoxPanel);
MainPanel->Controls->Add(UIDPanel);
return MainPanel;
}
void UserAccount::CreateMessageLayout(FlowLayoutPanel^ container, Form^ form, vector<pair<UserMessage, int>>& messages)
{
//Flow Layout Properties
container->FlowDirection = FlowDirection::TopDown;
container->AutoScroll = true;
container->WrapContents = false;
//Add the Messages
for (int i = 0; i < messages.size(); i++)
{
container->Controls->Add(CreateMainMessagePanel(form, messages[i].second, messages[i].first, "Received"));
}
}
void UserAccount::CreateMessageLayout(FlowLayoutPanel^ container, Form^ form, stack<UserMessage>& messages, int User_ID)
{
//Flow Layout Properties
container->FlowDirection = FlowDirection::TopDown;
container->AutoScroll = true;
container->WrapContents = false;
UserMessage message;
//Add the Messages
while (!messages.empty())
{
message = messages.top();
container->Controls->Add(CreateMainMessagePanel(form, User_ID, message, "Recieved"));
messages.pop();
}
}
void UserAccount::CreateMessageLayout(FlowLayoutPanel^ container, Form^ form, stack<pair<int, UserMessage>>& messages)
{
//Flow Layout Properties
container->FlowDirection = FlowDirection::TopDown;
container->AutoScroll = true;
container->WrapContents = false;
pair<int, UserMessage> message;
//Add the Messages
while (!messages.empty())
{
message = messages.top();
container->Controls->Add(CreateMainMessagePanel(form, message.first, message.second, "Sent"));
messages.pop();
}
}
void UserAccount::CreateMessageLayout(FlowLayoutPanel^ container, Form^ form, queue<pair<int, UserMessage>>& messages)
{
//Flow Layout Properties
container->FlowDirection = FlowDirection::TopDown;
container->AutoScroll = true;
container->WrapContents = false;
pair<int, UserMessage> message;
//Add the Messages
while (!messages.empty())
{
message = messages.front();
container->Controls->Add(CreateMainMessagePanel(form, message.first, message.second, "Favorite"));
messages.pop();
}
}
void UserAccount::CreateBlocksLayout(FlowLayoutPanel^ container, Form^ form)
{
//Properties of FlowLayoutPanels
container->FlowDirection = FlowDirection::TopDown;
container->AutoScroll = true;
container->WrapContents = false;
for (auto it = Blocked.begin(); it != Blocked.end(); ++it)
container->Controls->Add(CreateBlockPanel(container, form, *it));
}
Panel^ UserAccount::CreateBlockPanel(FlowLayoutPanel^ container, Form^ form, int User_ID)
{
String^ userid = Convert::ToString(User_ID);
Panel^ BlockPanel = gcnew Panel();
PictureBox^ BlockIcon = gcnew PictureBox();
Label^ uIDLabel = gcnew Label();
Label^ placeHolder = gcnew Label();
Button^ UnblockButton = gcnew Button();
//Properties for Panel
BlockPanel->Size = Drawing::Size(480, 61);
BlockPanel->BorderStyle = BorderStyle::FixedSingle;
BlockPanel->Location = System::Drawing::Point(204, 10);
BlockPanel->Margin = System::Windows::Forms::Padding(204, 10, 3, 3);
BlockPanel->Name = L"BlockPanel";
//Properties of PictureBox
BlockIcon->Size = Drawing::Size(32, 32);
BlockIcon->Location = Point(15, 14);
BlockIcon->Image = System::Drawing::Image::FromFile("img/ProfileIcon.png");
//Properties of Labels
uIDLabel->ForeColor = Color::White;
uIDLabel->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
uIDLabel->Size = Drawing::Size(34, 20);
uIDLabel->Location = Drawing::Point(66, 20);
uIDLabel->AutoSize = true;
placeHolder->Text = userid;
placeHolder->Name = L"placeHolder";
uIDLabel->Text = "Blocked user: " + placeHolder->Text;
placeHolder->ForeColor = Color::White;
placeHolder->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
placeHolder->AutoSize = true;
//Properties of Button (Unblock)
UnblockButton->AutoSize = true;
UnblockButton->FlatStyle = System::Windows::Forms::FlatStyle::Flat;
UnblockButton->ForeColor = System::Drawing::Color::White;
UnblockButton->Location = System::Drawing::Point(375, 19);
UnblockButton->Size = System::Drawing::Size(89, 25);
UnblockButton->Text = "Unblock";
UnblockButton->Name = userid;
UnblockButton->Click += gcnew System::EventHandler(static_cast<SarahaWithGUI::UserForm^>(form), &SarahaWithGUI::UserForm::BlockPanel_Unblock_Click);
BlockPanel->Controls->Add(BlockIcon);
BlockPanel->Controls->Add(uIDLabel);
BlockPanel->Controls->Add(UnblockButton);
return BlockPanel;
}
Panel^ CreateContactPanel(String^ user_ID, String^ numMsgs, Form^ form)
{
Panel^ ContactPanel = gcnew Panel();
PictureBox^ ContactIcon = gcnew PictureBox();
Label^ uIDLabel = gcnew Label();
Label^ uIDNumberLabel = gcnew Label();
Label^ placeHolder = gcnew Label();
Button^ SeeMessages = gcnew Button();
Button^ BlockContact = gcnew Button();
//Properties for Panel
ContactPanel->Size = Drawing::Size(569, 61);
ContactPanel->BorderStyle = BorderStyle::FixedSingle;
ContactPanel->Padding = System::Windows::Forms::Padding(5);
ContactPanel->Name = L"something";
//Properties of PictureBox
ContactIcon->Size = Drawing::Size(32, 32);
ContactIcon->Location = Point(15, 14);
ContactIcon->Image = System::Drawing::Image::FromFile("img/ProfileIcon.png");
//Properties of Labels
uIDLabel->ForeColor = Color::White;
uIDLabel->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
uIDLabel->Size = Drawing::Size(34, 20);
uIDLabel->Location = Drawing::Point(66, 20);
uIDLabel->AutoSize = true;
placeHolder->Text = user_ID;
placeHolder->Name = L"placeHolder";
uIDLabel->Text = "Contact with ID " + placeHolder->Text + ": ";
placeHolder->ForeColor = Color::White;
placeHolder->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
placeHolder->AutoSize = true;
uIDNumberLabel->AutoSize = true;
uIDNumberLabel->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
uIDNumberLabel->ForeColor = Drawing::Color::White;
uIDNumberLabel->Location = Drawing::Point(240, 20);
uIDNumberLabel->Size = Drawing::Size(203, 20);
uIDNumberLabel->Text = numMsgs + " Messages";
//Properties of Button (See Messages)
SeeMessages->AutoSize = true;
SeeMessages->FlatStyle = System::Windows::Forms::FlatStyle::Flat;
SeeMessages->ForeColor = System::Drawing::Color::White;
SeeMessages->Location = System::Drawing::Point(470, 19);
SeeMessages->Size = System::Drawing::Size(89, 25);
SeeMessages->Text = "See Messages";
SeeMessages->Name = user_ID;
SeeMessages->Click += gcnew System::EventHandler(static_cast<SarahaWithGUI::UserForm^>(form), &SarahaWithGUI::UserForm::SeeMessages_btn_Click);
//Properties of Button (Block Contact)
BlockContact->AutoSize = true;
BlockContact->FlatStyle = System::Windows::Forms::FlatStyle::Flat;
BlockContact->ForeColor = System::Drawing::Color::White;
BlockContact->Location = System::Drawing::Point(375, 19);
BlockContact->Size = System::Drawing::Size(89, 25);
BlockContact->Text = "Block Contact";
BlockContact->Name = user_ID;
BlockContact->Click += gcnew System::EventHandler(static_cast<SarahaWithGUI::UserForm^>(form), &SarahaWithGUI::UserForm::BlockContact_btn_Click);
ContactPanel->Controls->Add(ContactIcon);
ContactPanel->Controls->Add(uIDLabel);
ContactPanel->Controls->Add(uIDNumberLabel);
ContactPanel->Controls->Add(SeeMessages);
ContactPanel->Controls->Add(BlockContact);
return ContactPanel;
}
void CreateContactLayout(FlowLayoutPanel^ container, String^ user_ID, String^ numMsgs,Form^ form)
{
//Properties of FlowLayoutPanels
container->Size = System::Drawing::Size(879, 546);
container->FlowDirection = FlowDirection::TopDown;
container->AutoScroll = true;
container->WrapContents = false;
//Testing
container->Controls->Add(CreateContactPanel(user_ID,numMsgs,form));
}
vector<pair<UserMessage, int>> UserAccount::ViewReceivedMessages(FlowLayoutPanel^ container, Form^ form, bool full)
{
vector <pair<UserMessage, int>> AllUserMessages;
if (ReceivedMessages.empty() == false)
{
UserMessage Message;
unordered_map<int, stack<UserMessage>>::iterator it;
for (it = ReceivedMessages.begin(); it != ReceivedMessages.end(); it++)
{
stack<UserMessage> UserMessages;
while (!it->second.empty())
{
Message = it->second.top();
if (full == true || Message.Seen == false)
AllUserMessages.emplace_back(Message, it->first);
//set messages as seen for receiver
Message.Seen = true;
UserMessages.push(Message);
it->second.pop();
}
//re-set received messages (to update IsSeen)
while (!UserMessages.empty())
{
it->second.push(UserMessages.top());
UserMessages.pop();
}
}
std::sort(AllUserMessages.begin(), AllUserMessages.end(), compareByTime);
CreateMessageLayout(container, form, AllUserMessages);
}
return AllUserMessages;
}
void UserAccount::ViewSentMessages(FlowLayoutPanel^ container, Form^ form)
{
if (!SentMessages.empty())
{
stack<pair<int, UserMessage>> msgs = SentMessages;
CreateMessageLayout(container, form, msgs);
}
}
bool UserAccount::ViewUserMessages(FlowLayoutPanel^ container, Form^ form, int User_ID)
{
//Flow Layout Properties
/*container->Location = Point(110, 135);
container->FlowDirection = FlowDirection::TopDown;
container->AutoScroll = true;
container->WrapContents = false;*/
stack<UserMessage> UserMessages;
if (ReceivedMessages.find(User_ID) == ReceivedMessages.end())
{
return false;
}
else
{
UserMessages = ReceivedMessages[User_ID];
CreateMessageLayout(container, form, UserMessages, User_ID);
return true;
}
}
bool UserAccount::PutFavorite(int User_ID, int Msg_Index) {
auto it = ReceivedMessages.find(User_ID);
if (it == ReceivedMessages.end())
{
return false;
}
else
{
bool found = false;
stack<UserMessage> chat;
UserMessage msg;
while (!it->second.empty())
{
msg = it->second.top();
if (!found && msg.Index == Msg_Index)
{
found = true;
msg.IsFavorite = true;
Favorites.emplace(User_ID, msg);
}
chat.push(msg);
it->second.pop();
}
while (!chat.empty())
{
it->second.push(chat.top());
chat.pop();
}
return found;
}
}
bool UserAccount::RemoveOldestFavorite(void)
{
return RemoveFavorite(Favorites.back().first, Favorites.back().second.Index);
}
bool UserAccount::RemoveFavorite(int User_ID, int Msg_Index)
{
auto it = ReceivedMessages.find(User_ID);
if (it == ReceivedMessages.end())
{
return false;
}
else
{
bool found = false;
stack<UserMessage> chat;
UserMessage msg;
queue<pair<int, UserMessage>> favs;
pair<int, UserMessage> favmsg;
//set messages flag
while (!it->second.empty())
{
msg = it->second.top();
if (!found && msg.Index == Msg_Index)
{
found = true;
msg.IsFavorite = false;
}
chat.push(msg);
it->second.pop();
}
while (!chat.empty())
{
it->second.push(chat.top());
chat.pop();
}
//removing from Favorites queue
while (!Favorites.empty())
{
favmsg = Favorites.front();
if (favmsg.first != User_ID || favmsg.second.Index != Msg_Index)
favs.push(favmsg);
Favorites.pop();
}
Favorites = favs;
return found;
}
}
bool UserAccount::IsFavorite(int User_ID, int Msg_Index)
{
auto it = ReceivedMessages.find(User_ID);
if (it != ReceivedMessages.end())
{
stack<UserMessage> chat = it->second;
//set messages flag
while (!chat.empty())
{
if (chat.top().Index == Msg_Index)
return chat.top().IsFavorite;
chat.pop();
}
}
return false;
}
bool UserAccount::ViewFavorites(FlowLayoutPanel^ container, Form^ form)
{
if (Favorites.empty())
{
return false;
}
queue<pair<int, UserMessage>> msgs = Favorites;
CreateMessageLayout(container, form, msgs);
return true;
}
bool UserAccount::ViewBlocks(FlowLayoutPanel^ container, Form^ form)
{
if (Blocked.empty())
return false;
CreateBlocksLayout(container, form);
return true;
}
int UserAccount::GetUserMessagesFromUser(UserAccount* user) {
return ReceivedMessages[user->m_id].size();
}
bool UserAccount::GetContact(int User_ID) {
auto it = Contacts.find(User_ID);
if (it != Contacts.end()) {
return true;
}
return NULL;
}
bool UserAccount::IsBlocked(int User_ID)
{
return Blocked.find(User_ID) != Blocked.end();
}
void UserAccount::ViewContacts(FlowLayoutPanel^ container,Form^ form)
{
set<int>::iterator itr;
vector<pair<int, int>>ContactTotalUserMessages;
Label^ noContactMessage = gcnew Label();
for (itr = Contacts.begin(); itr != Contacts.end(); itr++) // Retrieving Contact's Sent UserMessages
{
int ContactID = *itr;
if (!IsBlocked(ContactID))
{
int ContactUserMessages = ReceivedMessages[ContactID].size();
ContactTotalUserMessages.push_back(make_pair(ContactUserMessages, ContactID)); // Storing as (UserMessages,ID) for easier sort lol :')
}
}
if (ContactTotalUserMessages.empty()) {
//Properties for Label
noContactMessage->Text = "Oh no, you don't have any contacts. :(";
noContactMessage->ForeColor = Color::White;
noContactMessage->AutoSize = true;
noContactMessage->Margin = Padding(20);
noContactMessage->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
container->Controls->Add(noContactMessage);
}
else {
sort(ContactTotalUserMessages.begin(), ContactTotalUserMessages.end(), greater<>());
for (auto i : ContactTotalUserMessages) {
CreateContactLayout(container, Convert::ToString(i.second),Convert::ToString(i.first),form);
}
}
}
void UserAccount::ViewContacts(FlowLayoutPanel^ container, Form^ form, int User_ID)
{
if (GetContact(User_ID) && !IsBlocked(User_ID))
CreateContactLayout(container, Convert::ToString(User_ID), Convert::ToString(ReceivedMessages[User_ID].size()), form);
else
{
Label^ noContactMessage = gcnew Label();
//Properties for Label
noContactMessage->Text = "Couldn't find this contact. :(";
noContactMessage->ForeColor = Color::White;
noContactMessage->AutoSize = true;
noContactMessage->Margin = Padding(20);
noContactMessage->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 12, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
container->Controls->Add(noContactMessage);
}
}