-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
967 lines (805 loc) · 33.2 KB
/
main.cpp
File metadata and controls
967 lines (805 loc) · 33.2 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
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
#include "scraping-handler.h"
#include <wx/wx.h>
#include <wx/stattext.h>
#include "csv_handler.h"
#include <mutex>
// More info and understanding
// https://brightdata.com/blog/how-tos/web-scraping-in-c-plus-plus
// preallocate the vector with the size of arguments.
// It is not worth preallocating when the vector is reallocated
// Read more here:
// https://stackoverflow.com/questions/11888265/is-it-more-efficient-to-preallocate-a-vector
// Wx Widgets Initialization
// Source: https://docs.wxwidgets.org/3.2/overview_helloworld.html
// For later
// System command safety, use execvp()
// https://www.digitalocean.com/community/tutorials/execvp-function-c-plus-plus
// https://developers.redhat.com/articles/2023/03/29/4-essentials-prevent-os-command-injection-attacks#how_os_command_injection_works
class ScraperApp : public wxApp
{
public:
bool OnInit() override;
};
wxIMPLEMENT_APP(ScraperApp);
// the public keyword is needed, otherwise the inherited code will be considered as
// private member
class MainFrame : public wxFrame
{
// Constructor
public:
MainFrame();
// Methods
private:
static void StartScraping(int amount, int counter,
std::vector<std::string> keywords,
std::vector<std::string> getUrls);
static Scraper scraper;
static AnalyzePages pageAnalyzer;
// Class Properties
// Inserting properties to event functors is not possible
// Create class properties for easy manipulation
private:
wxStaticText *searchSettings = nullptr;
wxStaticText *database = nullptr;
wxStaticText *run = nullptr;
wxStaticText *title = nullptr;
wxPanel *top = nullptr;
wxPanel *options = nullptr;
static wxPanel *content;
wxStaticBitmap *optionsImage = nullptr;
wxSize contentPanelSize;
// Starting Content
private:
wxStaticText *instructions;
// Search Settings
private:
// element arrays
std::array<wxTextCtrl*, 5> urlInput;
std::array<wxTextCtrl*, 5> keywords1;
std::array<wxTextCtrl*, 5> keywords2;
std::array<wxTextCtrl*, 5> keywords3;
std::array<wxTextCtrl*, 5> keywords4;
// Buttons
wxButton *confirmButton;
// Sizers
wxSizer *elementHolder = nullptr;
wxSizer* firstRow = nullptr;
wxSizer* secondRow = nullptr;
wxSizer* thirdRow = nullptr;
wxSizer* fourthRow = nullptr;
wxSizer* fifthRow = nullptr;
wxSizer* buttonSizer = nullptr;
wxSizer* mainSizer = nullptr;
wxSizer* titleRow = nullptr;
// Static Text
wxStaticText *urlInputStaticText = nullptr;
wxStaticText *keywords1StaticText = nullptr;
wxStaticText *keywords2StaticText = nullptr;
wxStaticText *keywords3StaticText = nullptr;
wxStaticText *keywords4StaticText = nullptr;
// Run Elements
private:
std::vector<std::string> getSettingsUrl;
std::vector<std::string> getSettingsKeywords;
wxStaticText *runInstructions = nullptr;
wxButton *startButton = nullptr;
wxButton *stopButton = nullptr;
static wxSizer *runContentHolder;
wxSizer *runInstructionsholder = nullptr;
wxSizer *startButtonHolder = nullptr;
wxSizer *stopButtonHolder = nullptr;
wxSizer *buttonsHolder = nullptr;
void StartOperation(wxMouseEvent &event);
void StopOperation(wxMouseEvent &event);
static std::vector<std::thread> threads;
static std::mutex m;
CSV_Handler handler;
static wxSizer *scrapingInfoSizer;
static wxStaticText *scrapingInfoText;
static int operationCounter;
static int operationSize;
// States and IDs
private:
static int currentState;
static int scrapingState;
enum events
{
ID_Start = 1,
};
enum ElementID
{
eID_SearchSettings = 0,
eID_Title,
eID_Database,
eID_Run,
eID_OptionsImage,
eID_TopPanel,
eID_OptionsPanel,
eID_ContentPanel,
eID_ConfirmButton,
eID_Instructions,
eID_Frame,
eID_StartButton,
eID_StopButton
};
enum TextInput
{
itID_SearchSettingsURL = 0,
itID_Keyword1,
itID_Keyword2,
itID_Keyword3,
itID_Keyword4,
};
enum states
{
ST_Instructions = 0,
ST_SearchSettings,
ST_Run,
};
enum scrapingState
{
SST_Waiting = 0,
SST_Running,
};
// Window Events
private:
void OnExit(wxCommandEvent& event);
void OnAbout(wxCommandEvent& event);
void OnStart(wxCommandEvent& event);
// Hover Events
private:
void HoverSearchSettings(wxMouseEvent& event);
void HoverDatabase(wxMouseEvent& event);
void HoverRun(wxMouseEvent& event);
// Stop Hovering Event
private:
void StopHoverSearchSettings(wxMouseEvent& event);
void StopHoverDatabase(wxMouseEvent& event);
void StopHoverRun(wxMouseEvent& event);
// Click Button Events
private:
void PressSearchSettings(wxEvent &event);
void PressDatabase(wxMouseEvent& event);
void PressRun(wxMouseEvent& event);
void PressConfirm(wxMouseEvent &event);
};
class AboutWindow: public wxFrame
{
public:
AboutWindow();
private:
void OnExit(wxCommandEvent& event);
};
// Static Initialisation
int MainFrame::currentState = ST_Instructions;
int MainFrame::scrapingState = SST_Waiting;
wxSizer *MainFrame::scrapingInfoSizer = nullptr;
wxSizer *MainFrame::runContentHolder = nullptr;
wxStaticText *MainFrame::scrapingInfoText = nullptr;
wxPanel *MainFrame::content = nullptr;
std::vector<std::thread> MainFrame::threads;
std::mutex MainFrame::m;
int MainFrame::operationCounter = 0;
int MainFrame::operationSize = 0;
//Define
bool ScraperApp::OnInit()
{
auto* frame = new MainFrame();
// Use Hex to set the background color, read more here:
// https://docs.wxwidgets.org/3.2/classwx_colour.html#aac96e7922132d672a1f83d59ecf07343
// Stylizing the frame needs to use wxWindows class members
// Read: https://docs.wxwidgets.org/stable/classwx_window.html
// By Chat GPT
// Read more here:
// wxSize https://docs.wxwidgets.org/3.2/classwx_size.html#a89bbb1a42ad12573ff42809221e243a7
// Graphic Device Interface(wxGetDisplaySize() can be found here):
// https://docs.wxwidgets.org/3.2/group__group__funcmacro__gdi.html
// SetSize: https://docs.wxwidgets.org/3.2/classwx_window.html
wxSize screenSize = wxGetDisplaySize();
wxSize windowSize(static_cast<int>((float)screenSize.GetWidth() * 0.98f),
static_cast<int>((float)screenSize.GetHeight() * 0.95f));
frame->SetSize(windowSize);
frame->SetBackgroundColour(wxColour(0,0,0));
frame->Center(wxBOTH);
frame->Show(true);
return true;
}
MainFrame::MainFrame()
: wxFrame(nullptr, eID_Frame, "Info Hunter")
{
// Set Font
// Frame Layout
top = new wxPanel(this, eID_TopPanel, wxDefaultPosition, wxSize(200,100));
top->SetBackgroundColour("#4C6E81");
// Set Locale
boost::locale::generator gen;
std::locale::global(gen("el_GR.UTF-8"));
// This is how I managed to make font settings to work. Helvetica Neue indeed changes the font
// Read more here: https://docs.wxwidgets.org/latest/classwx_font_info.html#a7273ff25fbd808e83ee79103d117ecaf
top->SetFont(wxFontInfo(65).FaceName("Helvetica Neue").Bold());
title = new wxStaticText(top, eID_Title, "Info Hunter", wxPoint(5,10),
wxDefaultSize, 0, "Info Hunter");
title->SetForegroundColour("#FFFFFFBB");
options = new wxPanel(this, eID_OptionsPanel, wxDefaultPosition, wxDefaultSize);
options->SetBackgroundColour("#4C6E81");
// Options Text
// Read more on static text:
// https://docs.wxwidgets.org/3.2/classwx_static_text.html#a9291a72fe2317f4a9e30c6eb7d02e014
// Set Font size for the options panel
// Found here: https://stackoverflow.com/questions/67843563/font-size-scaling-problems
options->SetFont(wxFontInfo(55).FaceName("Helvetica Neue"));
wxSize optionsPanelSize = options->GetSize();
// This is important to make sure that every supported
// file format works.
// Read more here: https://forums.wxwidgets.org/viewtopic.php?t=47660
wxInitAllImageHandlers();
optionsImage = new wxStaticBitmap(options, eID_OptionsImage,
wxBitmap("../graphics/Infohunter_Options_Panel_2.png",
wxBITMAP_TYPE_PNG),
wxDefaultPosition, wxDefaultSize);
searchSettings = new wxStaticText(options, eID_SearchSettings, "Search Settings" ,
wxPoint(optionsPanelSize.GetWidth()/2,
static_cast<int>((float)optionsPanelSize.GetHeight() * 1.5f)),
wxDefaultSize, 0, "Search Settings");
searchSettings->SetForegroundColour("#FFFFFFAA");
database = new wxStaticText(options, eID_Database, "Database" ,
wxPoint(optionsPanelSize.GetWidth()/2,
static_cast<int>((float)optionsPanelSize.GetHeight() * 6.5f)),
wxDefaultSize, 0, "Database");
database->SetForegroundColour("#FFFFFFAA");
run = new wxStaticText(options, eID_Run, "Run" ,
wxPoint(optionsPanelSize.GetWidth()/2,
static_cast<int>((float)optionsPanelSize.GetHeight() * 11.5f)),
wxDefaultSize, 0, "Run");
run->SetForegroundColour("#FFFFFFAA");
content = new wxPanel(this, eID_ContentPanel, wxDefaultPosition, wxDefaultSize);
content->SetBackgroundColour("#4C6E81");
content->SetFont(wxFontInfo(30).FaceName("Roboto"));
wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL);
sizer->Add(top, 1, wxEXPAND);
wxBoxSizer* contentSizer = new wxBoxSizer(wxHORIZONTAL);
contentSizer->Add(options, 1, wxEXPAND);
contentSizer->Add(content, 5, wxEXPAND);
sizer->Add(contentSizer, 9, wxEXPAND);
this->SetSizerAndFit(sizer);
// Menu
wxMenu* menuFile = new wxMenu;
menuFile->Append(ID_Start, "Start", "It starts the application");
menuFile->AppendSeparator();
menuFile->Append(wxID_EXIT);
wxMenu* menuHelp = new wxMenu;
menuHelp->Append(wxID_ABOUT);
wxMenuBar* menubar = new wxMenuBar;
menubar->Append(menuFile, "&File");
menubar->Append(menuHelp, "&Help");
SetMenuBar( menubar );
Bind(wxEVT_MENU, &MainFrame::OnStart, this, ID_Start);
Bind(wxEVT_MENU, &MainFrame::OnAbout, this, wxID_ABOUT);
Bind(wxEVT_MENU, &MainFrame::OnExit, this, wxID_EXIT);
content->SetFont(wxFontInfo(35).FaceName("Helvetica").Bold());
content->SetForegroundColour("#FFFFFF");
instructions = new wxStaticText(content, eID_Instructions,
std::string("How to use the application:\n\n1. Add the keywords and the URLs ")
+ std::string("in the search settings.\n")
+ std::string("2. Press the run option to start the search.\n")
+ std::string("3. Press the database option to see the results"),
wxPoint(20, 50), wxDefaultSize, 0, "Instructions");
// Handle events for certain elements in the window
// The argument after the handles has to be the id of the element
// Read more: https://docs.wxwidgets.org/3.2.3/overview_events.html
// On hover events
searchSettings->Bind(wxEVT_ENTER_WINDOW, &MainFrame::HoverSearchSettings,
this, eID_SearchSettings);
database->Bind(wxEVT_ENTER_WINDOW,
&MainFrame::HoverDatabase, this,
eID_Database);
run->Bind(wxEVT_ENTER_WINDOW,
&MainFrame::HoverRun, this,
eID_Run);
// On Exit hover events
searchSettings->Bind(wxEVT_LEAVE_WINDOW,
&MainFrame::StopHoverSearchSettings, this,
eID_SearchSettings);
database->Bind(wxEVT_LEAVE_WINDOW,
&MainFrame::StopHoverDatabase, this,
eID_Database);
run->Bind(wxEVT_LEAVE_WINDOW, &MainFrame::StopHoverRun, this,
eID_Run);
// On click events
searchSettings->Bind(wxEVT_LEFT_UP,
&MainFrame::PressSearchSettings, this,
eID_SearchSettings);
database->Bind(wxEVT_LEFT_UP,
&MainFrame::PressDatabase, this,
eID_Database);
run->Bind(wxEVT_LEFT_UP, &MainFrame::PressRun, this,
eID_Run);
}
// Start Scraping function
void MainFrame::StartScraping(int amount, int counter, std::vector<std::string> keywords,
std::vector<std::string> getUrls)
{
m.lock();
if (Scraper::isCanceled || Scraper::hasDisconnected)
{
// if (!threads.empty())
// {
// threads.clear();
// }
if (scrapingInfoText != nullptr) {
scrapingInfoText->Destroy();
scrapingInfoText = nullptr;
}
content->SetFont(wxFontInfo(32).FaceName("Helvetica Neue").Bold());
scrapingInfoText = new wxStaticText(MainFrame::content, wxID_ANY,
"Please wait while stopping.",
wxDefaultPosition, wxDefaultSize);
scrapingInfoSizer = new wxBoxSizer(wxVERTICAL);
scrapingInfoSizer->Add(scrapingInfoText, 0, wxCENTER);
runContentHolder->Add(scrapingInfoSizer, 1, wxEXPAND);
content->SetSizer(runContentHolder);
content->Layout();
operationCounter++;
}
else
{
std::vector<std::string> scraperKeywords;
scraperKeywords.reserve(amount);
for (int j = 0; j < amount; j++) {
if (counter == 0)
{
scraperKeywords.push_back(keywords[j]);
}
else
{
scraperKeywords.push_back(keywords[j + amount + counter]);
}
}
if (scrapingInfoText != nullptr) {
scrapingInfoText->Destroy();
scrapingInfoText = nullptr;
}
content->SetFont(wxFontInfo(32).FaceName("Helvetica Neue").Bold());
Scraper::SetupScraper(scraperKeywords, getUrls[counter]);
scrapingInfoText = new wxStaticText(MainFrame::content, wxID_ANY,
std::string("Currently checking: ") +
std::string(getUrls[counter]),
wxDefaultPosition, wxDefaultSize);
scrapingInfoSizer = new wxBoxSizer(wxVERTICAL);
scrapingInfoSizer->Add(scrapingInfoText, 0, wxCENTER);
runContentHolder->Add(scrapingInfoSizer, 1, wxEXPAND);
content->SetSizer(runContentHolder);
content->Layout();
cpr::Response r = Scraper::request_info(getUrls[counter]);
std::string convertToLowerCase = boost::locale::to_lower(r.text);
std::vector<std::string> urls = Scraper::ParseContent(convertToLowerCase);
// Iterate through the urls
for (const std::string &item: urls) {
if (!Scraper::CheckForConnection()) {
wxMessageBox("You have been disconnected from the internet", "",
wxOK);
if (scrapingInfoText != nullptr) {
scrapingInfoText->Destroy();
scrapingInfoText = nullptr;
}
Scraper::hasDisconnected = true;
scrapingState = SST_Waiting;
break;
}
if (Scraper::isCanceled) {
break;
}
AnalyzePages::analyzeEntry(item, scraperKeywords, scraper);
}
// if (scrapingInfoText != nullptr) {
// scrapingInfoText->Destroy();
// scrapingInfoText = nullptr;
// }
operationCounter++;
}
m.unlock();
if (Scraper::isCanceled && operationCounter >= operationSize ||
Scraper::hasDisconnected && operationCounter >= operationSize)
{
operationCounter = 0;
operationSize = 0;
if (scrapingInfoText != nullptr) {
scrapingInfoText->Destroy();
scrapingInfoText = nullptr;
}
Scraper::hasDisconnected = false;
Scraper::isCanceled = false;
AnalyzePages::hasStarted = false;
scrapingState = SST_Waiting;
wxMessageBox("Operation has been canceled.", "",wxOK);
return;
}
if (operationCounter >= operationSize)
{
if (!threads.empty())
{
threads.clear();
}
if (scrapingInfoText != nullptr) {
scrapingInfoText->Destroy();
scrapingInfoText = nullptr;
}
operationCounter = 0;
operationSize = 0;
scrapingState = SST_Waiting;
AnalyzePages::hasStarted = false;
wxMessageBox("Operation has been completed.", "", wxOK);
return;
}
if (!threads.empty())
{
threads.clear();
}
}
// Start Scraping button
void MainFrame::StartOperation(wxMouseEvent &event)
{
Scraper::isCanceled = false;
if (scrapingState == SST_Waiting)
{
wxMessageBox("The operation has started.", "", wxOK);
scrapingState = SST_Running;
}
else
{
wxMessageBox("An operation is already running","" ,wxOK);
return;
}
handler.ReadSettings();
// Separate urls and keywords
for (const std::string &line : handler.csvLines)
{
int index = (int)line.find(",");
getSettingsUrl.push_back(line.substr(0, index));
getSettingsKeywords.push_back(line.substr(index + 1));
}
std::vector<std::string> getUrls;
std::vector<int> urlCounterHolder;
// count how many keywords each url has
for (const auto &url : getSettingsUrl)
{
if (urlCounterHolder.empty())
{
int count = (int)std::count(getSettingsUrl.begin(), getSettingsUrl.end(),
url);
urlCounterHolder.push_back((count));
getUrls.push_back(url);
}
if (std::find(getUrls.begin(), getUrls.end(), url) != std::end(getUrls))
{
continue;
}
else
{
int count = (int)std::count(getSettingsUrl.begin(), getSettingsUrl.end(),
url);
urlCounterHolder.push_back((count));
getUrls.push_back(url);
}
}
getSettingsUrl.clear();
int counter = 0;
operationSize = urlCounterHolder.size();
for (int amount : urlCounterHolder)
{
// It is not possible to pass by reference when using threads
// More information here:
// https://www.reddit.com/r/cpp_questions/comments/kurtkw/error_attempt_to_use_a_deleted_function/
std::thread t(StartScraping,amount, counter, getSettingsKeywords, getUrls);
threads.emplace_back(std::move(t));
counter++;
}
for (auto &t : threads)
{
if (t.joinable())
{
t.detach();
}
}
}
// Cancel Scraping button
void MainFrame::StopOperation(wxMouseEvent &event)
{
if (scrapingState == SST_Waiting)
{
wxMessageBox("There are no operations running", "", wxOK);
return;
}
Scraper::isCanceled = true;
}
// Hover Events Functions
void MainFrame::HoverSearchSettings(wxMouseEvent &event){
searchSettings->SetForegroundColour("#FFFFFFFF");
}
void MainFrame::HoverDatabase(wxMouseEvent &event){
database->SetForegroundColour("#FFFFFFFF");
}
void MainFrame::HoverRun(wxMouseEvent &event){
run->SetForegroundColour("#FFFFFFFF");
}
// Stop Hovering Functions
void MainFrame::StopHoverSearchSettings(wxMouseEvent &event){
searchSettings->SetForegroundColour("#FFFFFFAA");
}
void MainFrame::StopHoverDatabase(wxMouseEvent &event){
database->SetForegroundColour("#FFFFFFAA");
}
void MainFrame::StopHoverRun(wxMouseEvent &event){
run->SetForegroundColour("#FFFFFFAA");
}
// Click Events Functions
void MainFrame::PressSearchSettings(wxEvent &event) {
if (currentState == ST_Instructions) {
instructions->Destroy();
}
// Destroying vector elements:
/*The error message you are seeing indicates that the elem variable is not a structure or union,
* but a pointer to a wxTextCtrl object. Therefore, you cannot use the -> operator to access its members.
* Instead, you should use the * operator to dereference the pointer and access the object’s members/*/
if (currentState == ST_SearchSettings) {
for (int i = 0; i < 5; i++) {
urlInput[i]->Destroy();
keywords1[i]->Destroy();
keywords2[i]->Destroy();
keywords3[i]->Destroy();
keywords4[i]->Destroy();
}
urlInputStaticText->Destroy();
keywords1StaticText->Destroy();
keywords2StaticText->Destroy();
keywords3StaticText->Destroy();
keywords4StaticText->Destroy();
confirmButton->Destroy();
}
if (currentState == ST_Run)
{
runInstructions->Destroy();
startButton->Destroy();
stopButton->Destroy();
}
content->SetFont(wxFontInfo(wxDefaultSize).FaceName("Helvetica").Bold());
content->SetSizeHints(wxDefaultSize, wxDefaultSize);
contentPanelSize = content->GetSize();
// Make sure to cast to the correct type before operating
const int elementBorder = static_cast<int>((float)contentPanelSize.GetWidth() * 0.01f);
const int rowBottomBorder = static_cast<int>((float)contentPanelSize.GetHeight() * 0.01f);
const int mainSizerTopBorder = static_cast<int>((float)contentPanelSize.GetHeight() * 0.15f);
elementHolder = new wxBoxSizer(wxVERTICAL);
mainSizer = new wxBoxSizer(wxHORIZONTAL);
firstRow = new wxBoxSizer(wxHORIZONTAL);
secondRow = new wxBoxSizer(wxHORIZONTAL);
thirdRow = new wxBoxSizer(wxHORIZONTAL);
fourthRow = new wxBoxSizer(wxHORIZONTAL);
fifthRow = new wxBoxSizer(wxHORIZONTAL);
titleRow = new wxBoxSizer(wxHORIZONTAL);
// Add static text to the panel
urlInputStaticText = new wxStaticText(content, wxID_ANY, "URL",
wxDefaultPosition, wxDefaultSize,
0, "URL");
keywords1StaticText = new wxStaticText(content, wxID_ANY,
"Keyword 1", wxDefaultPosition,
wxDefaultSize, wxBOLD,
"Keyword 1");
keywords2StaticText = new wxStaticText(content, wxID_ANY,
"Keyword 2", wxDefaultPosition,
wxDefaultSize, wxBOLD,
"Keyword 2");
keywords3StaticText = new wxStaticText(content, wxID_ANY,
"Keyword 3", wxDefaultPosition,
wxDefaultSize, wxBOLD,
"Keyword 3");
keywords4StaticText = new wxStaticText(content,
wxID_ANY, "Keyword 4",
wxDefaultPosition, wxDefaultSize,
wxBOLD, "Keyword 4");
titleRow->Add(urlInputStaticText, 1, wxEXPAND|wxRIGHT|wxLEFT, elementBorder);
titleRow->Add(keywords1StaticText, 1, wxEXPAND|wxRIGHT|wxLEFT, elementBorder);
titleRow->Add(keywords2StaticText, 1, wxEXPAND|wxRIGHT|wxLEFT, elementBorder);
titleRow->Add(keywords3StaticText, 1, wxEXPAND|wxRIGHT|wxLEFT, elementBorder);
titleRow->Add(keywords4StaticText, 1, wxEXPAND|wxRIGHT|wxLEFT, elementBorder);
elementHolder->Add(titleRow, 0, wxEXPAND|wxBOTTOM, rowBottomBorder);
// Add Elements to the panel
for (int i = 0; i < 5; i++)
{
urlInput[i] = new wxTextCtrl( content, itID_SearchSettingsURL, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0 );
keywords1[i] = new wxTextCtrl( content, itID_Keyword1, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0 );
keywords2[i] = new wxTextCtrl( content, itID_Keyword2, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0 );
keywords3[i] = new wxTextCtrl( content, itID_Keyword3, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0 );
keywords4[i] = new wxTextCtrl( content, itID_Keyword4, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0 );
switch (i)
{
case 0:
firstRow->Add( urlInput[i], 1, wxEXPAND|wxRIGHT|wxLEFT, elementBorder);
firstRow->Add( keywords1[i], 1, wxEXPAND|wxRIGHT|wxLEFT,elementBorder);
firstRow->Add( keywords2[i], 1, wxEXPAND|wxRIGHT|wxLEFT,elementBorder);
firstRow->Add( keywords3[i], 1, wxEXPAND|wxRIGHT|wxLEFT,elementBorder);
firstRow->Add( keywords4[i], 1, wxEXPAND|wxRIGHT|wxLEFT,elementBorder);
elementHolder->Add( firstRow, 0, wxEXPAND|wxBOTTOM,rowBottomBorder);
break;
case 1:
secondRow->Add( urlInput[i], 1, wxEXPAND|wxRIGHT|wxLEFT,elementBorder);
secondRow->Add( keywords1[i], 1, wxEXPAND|wxRIGHT|wxLEFT,elementBorder);
secondRow->Add( keywords2[i], 1, wxEXPAND|wxRIGHT|wxLEFT, elementBorder);
secondRow->Add( keywords3[i], 1, wxEXPAND|wxRIGHT|wxLEFT, elementBorder);
secondRow->Add( keywords4[i], 1, wxEXPAND|wxRIGHT|wxLEFT, elementBorder);
elementHolder->Add( secondRow, 0, wxEXPAND|wxBOTTOM, rowBottomBorder);
break;
case 2:
thirdRow->Add( urlInput[i], 1, wxEXPAND|wxRIGHT|wxLEFT, elementBorder);
thirdRow->Add( keywords1[i], 1, wxEXPAND|wxRIGHT|wxLEFT, elementBorder);
thirdRow->Add( keywords2[i], 1, wxEXPAND|wxRIGHT|wxLEFT, elementBorder);
thirdRow->Add( keywords3[i], 1, wxEXPAND|wxRIGHT|wxLEFT, elementBorder);
thirdRow->Add( keywords4[i], 1, wxEXPAND|wxRIGHT|wxLEFT, elementBorder);
elementHolder->Add( thirdRow, 0, wxEXPAND|wxBOTTOM, rowBottomBorder);
break;
case 3:
fourthRow->Add( urlInput[i], 1, wxEXPAND|wxRIGHT|wxLEFT, elementBorder);
fourthRow->Add( keywords1[i], 1, wxEXPAND|wxRIGHT|wxLEFT, elementBorder);
fourthRow->Add( keywords2[i], 1, wxEXPAND|wxRIGHT|wxLEFT, elementBorder);
fourthRow->Add( keywords3[i], 1, wxEXPAND|wxRIGHT|wxLEFT, elementBorder);
fourthRow->Add( keywords4[i], 1, wxEXPAND|wxRIGHT|wxLEFT, elementBorder);
elementHolder->Add( fourthRow, 0, wxEXPAND|wxBOTTOM, rowBottomBorder);
break;
case 4:
fifthRow->Add( urlInput[i], 1, wxEXPAND|wxRIGHT|wxLEFT, elementBorder);
fifthRow->Add( keywords1[i], 1, wxEXPAND|wxRIGHT|wxLEFT, elementBorder);
fifthRow->Add( keywords2[i], 1, wxEXPAND|wxRIGHT|wxLEFT, elementBorder);
fifthRow->Add( keywords3[i], 1, wxEXPAND|wxRIGHT|wxLEFT, elementBorder);
fifthRow->Add( keywords4[i], 1, wxEXPAND|wxRIGHT|wxLEFT, elementBorder);
elementHolder->Add( fifthRow, 0, wxEXPAND|wxBOTTOM, rowBottomBorder);
break;
default:
break;
}
}
buttonSizer = new wxBoxSizer( wxVERTICAL );
confirmButton = new wxButton( content, eID_ConfirmButton, wxT("Confirm"), wxDefaultPosition, wxDefaultSize, 0 );
buttonSizer->Add( confirmButton, 0, wxALIGN_CENTER|wxALL|wxTOP, 20 );
elementHolder->Add( buttonSizer, 1, wxEXPAND, 5 );
mainSizer->Add(elementHolder, 1, wxEXPAND|wxTOP, mainSizerTopBorder);
content->SetSizer( mainSizer );
content->Layout();
confirmButton->Bind(wxEVT_LEFT_UP, &MainFrame::PressConfirm, this, eID_ConfirmButton);
currentState = ST_SearchSettings;
}
void MainFrame::PressConfirm(wxMouseEvent &event)
{
CSV_Handler::ClearPreviousOptions();
// Get Value:
// https://docs.wxwidgets.org/3.2.4/classwx_text_entry.html#a39335d9009b2053b5daf850c7b9d2974
for (int i = 0; i < 5; i++)
{
std::string getUrlValue = std::string(urlInput[i]->GetValue());
if (getUrlValue.empty())
{
break;
}
if (!keywords1[i]->GetValue().empty())
{
std::string getKeyword = std::string(keywords1[i]->GetValue());
handler.WriteSavedSearchOptions(getKeyword,
getUrlValue);
}
if (!keywords2[i]->GetValue().empty())
{
std::string getKeyword = std::string(keywords2[i]->GetValue());
handler.WriteSavedSearchOptions(getKeyword,
getUrlValue);
}
if (!keywords3[i]->GetValue().empty())
{
std::string getKeyword = std::string(keywords3[i]->GetValue());
handler.WriteSavedSearchOptions(getKeyword,
getUrlValue);
}
if (!keywords4[i]->GetValue().empty())
{
std::string getKeyword = std::string(keywords4[i]->GetValue());
handler.WriteSavedSearchOptions(getKeyword,
getUrlValue);
}
}
wxMessageBox("Your settings have been saved", "", wxOK);
}
void MainFrame::PressDatabase(wxMouseEvent &event)
{
int systemCode = std::system("cd ../content && open content.txt");
if (systemCode != 0)
{
wxMessageBox("There is no file to read, make sure to run a search first.",
"", wxOK);
}
}
void MainFrame::PressRun(wxMouseEvent &event)
{
if (currentState == ST_Instructions) {
instructions->Destroy();
}
if (currentState == ST_SearchSettings) {
for (int i = 0; i < 5; i++) {
urlInput[i]->Destroy();
keywords1[i]->Destroy();
keywords2[i]->Destroy();
keywords3[i]->Destroy();
keywords4[i]->Destroy();
}
urlInputStaticText->Destroy();
keywords1StaticText->Destroy();
keywords2StaticText->Destroy();
keywords3StaticText->Destroy();
keywords4StaticText->Destroy();
confirmButton->Destroy();
}
if (currentState == ST_Run) {
runInstructions->Destroy();
startButton->Destroy();
stopButton->Destroy();
}
content->SetFont(wxFontInfo(32).FaceName("Helvetica Neue").Bold());
wxSize getPanelSize = content->GetSize();
runInstructionsholder = new wxBoxSizer(wxVERTICAL);
buttonsHolder = new wxBoxSizer(wxHORIZONTAL);
startButtonHolder = new wxBoxSizer(wxVERTICAL);
stopButtonHolder = new wxBoxSizer(wxVERTICAL);
runContentHolder = new wxBoxSizer(wxVERTICAL);
std::string instructionsText = std::string("Press start to begin the scraping operation.\n") +
std::string("Press stop to cancel the current operation.");
runInstructions = new wxStaticText(content, eID_Instructions, instructionsText,
wxDefaultPosition, wxDefaultSize, 0);
runInstructionsholder->Add(runInstructions, 1, wxCENTER);
content->SetFont(wxFontInfo(wxDefaultSize).FaceName("Helvetica Neue").Bold());
startButton = new wxButton(content, eID_StartButton, "Start", wxDefaultPosition, wxDefaultSize,
0, wxDefaultValidator);
startButtonHolder->Add(startButton, 1, wxCENTER | wxALL,
static_cast<int>(getPanelSize.GetWidth() * 0.012));
stopButton = new wxButton(content, eID_StopButton, "Stop", wxDefaultPosition, wxDefaultSize,
0, wxDefaultValidator);
stopButtonHolder->Add(stopButton, 1, wxCENTER | wxALL,
static_cast<int>(getPanelSize.GetWidth() * 0.012));
buttonsHolder->Add(startButtonHolder, 1, wxTOP,
static_cast<int>(getPanelSize.GetHeight() * 0.05));
buttonsHolder->Add(stopButtonHolder, 1, wxTOP,
static_cast<int>(getPanelSize.GetHeight() * 0.05));
runContentHolder->Add(runInstructionsholder, 0, wxEXPAND | wxTOP,
static_cast<int>(getPanelSize.GetHeight() * 0.065));
runContentHolder->Add(buttonsHolder, 0, wxCENTER);
startButton->Bind(wxEVT_LEFT_UP, &MainFrame::StartOperation, this, eID_StartButton);
stopButton->Bind(wxEVT_LEFT_UP, &MainFrame::StopOperation, this, eID_StopButton);
content->SetSizer(runContentHolder);
content->Layout();
currentState = ST_Run;
}
AboutWindow::AboutWindow()
:wxFrame(NULL, wxID_ANY, "About")
{
CreateStatusBar();
SetStatusText("This is an about window. You will find instructions here.");
wxSize screen = wxGetDisplaySize();
wxSize windowSize(screen.GetWidth() * 0.1, screen.GetHeight() * 0.1);
SetSize(windowSize);
}
void MainFrame::OnExit(wxCommandEvent &event)
{
close(true);
}
void MainFrame::OnAbout(wxCommandEvent &event)
{
AboutWindow* aboutWindow = new AboutWindow();
aboutWindow->Center(wxBOTH);
// Set background color, Source: https://forums.wxwidgets.org/viewtopic.php?t=17706
aboutWindow->SetBackgroundColour("#AA5377");
aboutWindow->Show(true);
}
void MainFrame::OnStart(wxCommandEvent &event)
{
wxMessageBox("The app should start running now!", "", wxOK);
}
void AboutWindow::OnExit(wxCommandEvent &event)
{
close(true);
}