-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
2073 lines (1642 loc) 路 66 KB
/
Copy pathmainwindow.cpp
File metadata and controls
2073 lines (1642 loc) 路 66 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
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 "browselangwidget.h"
#include "browsetagwidget.h"
#include "qgraphicseffect.h"
#include "qmenu.h"
#include "qpainter.h"
#include "qpropertyanimation.h"
#include "snippetpreviewbox.h"
#include "snippetsettingspopup.h"
#include "tagadder.h"
#include "ui_mainwindow.h"
#include "predefines.h"
#include "editorwidget.h"
#include "searchsyetem.h"
#include "welcomescreen.h"
#include <QDir>
#include <QSettings>
//Global and Static Space
QString MainWindow::company="AronoxStudios";
QString MainWindow::appName="Codenheimer";
//==================
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
loadConfig();
addTagtoList();
//tagwidget
//taglayout= new QVBoxLayout();
//ui->tagWidget->setLayout(taglayout);
//ui->tagWidget->layout()->addWidget(tagListWidget);
//taglayout->addWidget(tagListWidget);
}
MainWindow::~MainWindow()
{
delete ui;
qDebug("===========================Main window destructor was called");
delete mainLangHolder;
delete mainTagHolder;
}
//START OF ADDITIONAL NON-SLOT BASED FUNCTIONS
/**
* @brief sandBox
* this function is mainly designed as an experimentation area for the ui or just any thing really
*/
void MainWindow::sandBox(){
ui->sandBox->clear();
// ********Implementation using QGroupBox
// for(int i=0;i<12;i++){
// snippetPreviewBox* pb=new snippetPreviewBox(this,this);
// pb->assignSnippet(mainStorage[i]);
// ui->sandBox->layout()->addWidget(pb);
// }
// QSpacerItem *verticalSpacer = new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding);
// ui->sandBox->layout()->addItem(verticalSpacer);
//********
//*********implementation using list widget
//ui->sandBox->clear();
ui->sandBox->setSpacing(3);
ui->sandBox->setStyleSheet(
"QListWidget::item {"
" border: 0px solid black;"
// " border-radius: 5px;"
// " padding: 5px;"
// " margin: 3px;"
"}"
"QListWidget::item:selected {"
" color: white;"
" border: 1px solid red;"
"}"
);
for (auto& itr: filenameStorage) {
snippetBaseClass*& snip = itr.second;
// Create the custom widget
snippetPreviewBox* pb = new snippetPreviewBox(this, this);
pb->assignSnippet(snip);
// Create a QListWidgetItem to hold the custom widget
QListWidgetItem* item = new QListWidgetItem(ui->sandBox);
// Set the size of the item to match the widget
item->setSizeHint(pb->sizeHint());
// Store snippetPreviewBox pointer inside Qt::UserRole
item->setData(Qt::UserRole, QVariant::fromValue(pb));
// Add the item to the list widget
ui->sandBox->addItem(item);
// Set the custom widget for this item (for display only)
ui->sandBox->setItemWidget(item, pb);
}
}
void MainWindow::loadConfig(){
setWindowTitle("Codenheimer");
ui->sidebarButton->setIcon(QIcon(":/images/sidebarButtonSVG.svg"));
ui->sidebarButton->setIconSize(QSize(30, 33));
loadCustomFonts();
ui->topbarTitle->setFont(CutiveMonoFont);
ui->sidebarButtons->setFont(CreteRoundFont); //set the font of the sidebar page change buttons
ui->usernameAndMainSettingsButton->setFont(CreteRoundFont); //set the same font for the settings button
ui->sidebar->layout()->setAlignment(ui->usernameAndMainSettingsButton, Qt::AlignHCenter); //align the settings button at the centre
centreSidebarButtons();
setSidebarButtonIcons();
readUconfig();
ui->usernameAndMainSettingsButton->setText(QString(username.c_str())+" ");
//preparing the holders and other objects
mainTagHolder=new tagHolder(tagCount);
mainLangHolder=new langHolder(additionalTypeCount);
clipboard = QApplication::clipboard(); // Get the clipboard object
searchObj=new searchSystem;
Julius= new cryptographicAgent();
Julius->setHash(hashResult);
Julius->tellUsename(username);
readData();
prepareCentralArea();
searchPageSearchbar();
prepareAddNewComboBox();
prepareSettingsPage();
prepareBrowsePage();
//load complete, land on add new page
ui->maincontentsStack->setCurrentIndex(0);
//add to system tray
createTrayActions();
setClickableOptions(true);
createSysTray();
connect(trayIcon, &QSystemTrayIcon::activated, this,
&MainWindow::iconActivated);
if(trayEnabled) trayIcon->show();
}
void MainWindow::saveToSettings(const QString &username, const QString &hs, const QString &vault, int tag, int type) {
// Step 1: Create or open the QSettings object
QSettings settings(company, appName);
// Step 2: Write data to settings
settings.setValue("username", username);
settings.setValue("hashres", hs);
settings.setValue("vault", vault);
settings.setValue("tag", tag);
settings.setValue("type", type);
settings.setValue("loginRun",false);
settings.setValue("trayIcon",false);
}
int MainWindow::firstTimeInit()
{
//std::ifstream inFile("firstrun.txt", std::ios::in); // Attempt to open the file in read mode
QString appDataPath = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
QDir().mkpath(appDataPath); // Ensure the directory exists
QString firstRunFile = appDataPath + "/firstrun.txt";
std::ifstream inFile(firstRunFile.toStdString(), std::ios::in);
if (!inFile.is_open()) { // Check if the file could not be opened and if not then this is the first time this is being run
std::ofstream make(firstRunFile.toStdString());
make.close();
std::cerr << "Error: First time run checker File does not exist or cannot be opened!\n";
std::string strn;
char str[assist::PATH_SIZE];
assist::getAppData_folder(str);
std::string mode="wb";
assist::ensure_directory_and_open_file(str,NULL,mode.c_str());
mode="a";
std::vector<std::string> filenames = {
"tagDat.cdh",
"langDat.cdh",
"snipDatVault.cdh"
};
char filePathBuffer[assist::PATH_SIZE];
for (const auto& filename : filenames) {
std::strncpy(filePathBuffer, filename.c_str(), sizeof(filePathBuffer) - 1);
filePathBuffer[sizeof(filePathBuffer) - 1] = '\0';
assist::make_appData_filePath(filePathBuffer);
if (assist::ensure_directory_and_open_file(str, filePathBuffer, mode.c_str())) {
QMessageBox::critical(nullptr, "Error", "Failed to initialize the application, please contact devs: ERR_NO_CREATE:"+QString().fromStdString(filename));
return -1;
}
}
QSettings settings(company, appName);
settings.setValue("username", "dummyUser");
settings.setValue("hashres", "5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8");
settings.setValue("vault", "default");
settings.setValue("tag", 6); //working on this
settings.setValue("type", 0);
return 1;
//makeSampleFile();
}
else return 0;
}
void MainWindow::loadCustomFonts(){
int fontId = QFontDatabase::addApplicationFont(":/fonts/CutiveMono-Regular.ttf");
if (fontId != -1) {
QString family = QFontDatabase::applicationFontFamilies(fontId).at(0);
CutiveMonoFont=QFont(family);
CutiveMonoFont.setPointSize(36);
} else {
qDebug() << "Font loading failed!";
}
int fontId2 = QFontDatabase::addApplicationFont(":/fonts/CreteRound-Regular.ttf");
if (fontId2 != -1) {
QString family = QFontDatabase::applicationFontFamilies(fontId2).at(0);
CreteRoundFont=QFont(family);
CreteRoundFont.setPointSize(14);
} else {
qDebug() << "Font loading failed!";
}
}
///centering the sidebar buttons
void MainWindow::centreSidebarButtons(){
// Get the layout from the UI file
QVBoxLayout *layout = qobject_cast<QVBoxLayout*>(ui->sidebarButtons->layout());
if (layout) {
// Iterate through each item in the layout and set alignment to center horizontally
for (int i = 0; i < layout->count(); ++i) {
QWidget *widget = layout->itemAt(i)->widget();
if (widget) {
layout->setAlignment(widget, Qt::AlignHCenter); // Align each widget horizontally center
}
}
}
}
///assigning the icons of the sidebar buttons
void MainWindow::setSidebarButtonIcons(){
ui->EditorsSidebarButton->setFont(CreteRoundFont);
ui->AddnewSidebarButton->setFont(CreteRoundFont);
ui->SearchSidebarButton->setFont(CreteRoundFont);
ui->BrowseSidebarButton->setFont(CreteRoundFont);
ui->EditorsDefaultTabButton->setFont(CreteRoundFont);
ui->defaultTabExplainer->setFont(CreteRoundFont);
// search button
ui->SearchSidebarButton->setIcon(QIcon(":/images/searchIcon.svg"));
ui->SearchSidebarButton->setIconSize(QSize(18, 18)); // Adjust icon size as needed
ui->SearchSidebarButton->setLayoutDirection(Qt::RightToLeft); // Puts the icon on the right side
// editors button
ui->EditorsSidebarButton->setIcon(QIcon(":/images/editorIcon.svg"));
ui->EditorsSidebarButton->setIconSize(QSize(19, 19));
ui->EditorsSidebarButton->setLayoutDirection(Qt::RightToLeft);
// browse button
ui->downarrow->setIcon(QIcon(":/images/downArrowIcon.svg"));
ui->downarrow->setIconSize(QSize(17, 7));
ui->downarrow->setLayoutDirection(Qt::RightToLeft);
// username And Main Settings Button button
ui->usernameAndMainSettingsButton->setIcon(QIcon(":/images/settingsIcon.svg"));
ui->usernameAndMainSettingsButton->setIconSize(QSize(21, 21));
ui->usernameAndMainSettingsButton->setLayoutDirection(Qt::RightToLeft);
}
QFont MainWindow::getFont(std::string str){
if(str=="Cutive") return CutiveMonoFont;
else if(str=="Crete") return CreteRoundFont;
}
///Sets the current page as specified and sets the button shading correspondingly
void MainWindow::setMainIndex(int n){
int x=ui->maincontentsStack->currentIndex();
if(x==n) return;
ui->maincontentsStack->setCurrentIndex(n);
switch (n) {
case 0:
ui->AddnewSidebarButton->setStyleSheet(styles::buttonVisited);
break;
case 1:
ui->SearchSidebarButton->setStyleSheet(styles::buttonVisited);
break;
case 2:
ui->EditorsSidebarButton->setStyleSheet(styles::buttonVisited);
break;
case 3:
ui->BrowseSidebarButton->setStyleSheet(styles::buttonBrowseVisited);
break;
default:
break;
}
switch (x) {
case 0:
ui->AddnewSidebarButton->setStyleSheet(styles::buttonNormal);
break;
case 1:
ui->SearchSidebarButton->setStyleSheet(styles::buttonNormal);
break;
case 2:
ui->EditorsSidebarButton->setStyleSheet(styles::buttonNormal);
break;
case 3:
ui->BrowseSidebarButton->setStyleSheet(styles::buttonBrowseNormal);
break;
default:
break;
}
}
//setting the font and font size of the central elements in the add new page
void MainWindow::prepareCentralArea(){
//new snippet box
QFont centralElementsFont=CutiveMonoFont;
centralElementsFont.setPointSize(20);
ui->newSnippetNameBox->setFont(centralElementsFont);
//search box
ui->centralSearchBoxLE->setFont(centralElementsFont);
/*QPixmap pixmap(":/images/searchIcon.svg");
ui->centralSearchIcon->setPixmap(pixmap.scaled(25, 25, Qt::KeepAspectRatio, Qt::SmoothTransformation))*/;
ui->centralSearchIcon->setIcon(QIcon(":/images/searchIcon.svg"));
ui->centralSearchIcon->setIconSize(QSize(25, 25));
//browse box
ui->centralBrowseButton->setFont(centralElementsFont);
ui->centralBrowseButton->setIcon(QIcon(":images/centralBrowseIcon.svg"));
ui->centralBrowseButton->setIconSize(QSize(32, 28));
ui->addNewButton->setFont(centralElementsFont);
ui->addNewButton->hide();
centralElementsFont.setPointSize(13);
ui->addNewLangDropdown->setFont(centralElementsFont);
}
// <<<<<<< ryexocious-making-search-page
void MainWindow::searchPageSearchbar(){
//the useless dummy icon
ui->searchDummyIcon->setText("");
ui->searchDummyIcon->setIcon(QIcon(":/images/searchIcon.svg"));
ui->searchDummyIcon->setIconSize(QSize(25, 25));
//the search box font
QFont searchBoxFont=CutiveMonoFont;
searchBoxFont.setPointSize(26);
ui->searchBoxLineEdit->setFont(searchBoxFont);
//the snippet settings icon
ui->snippetSettingsOnSearchPage->setText("");
ui->snippetSettingsOnSearchPage->setIcon(QIcon(":/images/settingsIcon.svg"));
ui->snippetSettingsOnSearchPage->setIconSize(QSize(23,23));
ui->snippetPreviewBoxAreaOnSearchPage->setStyleSheet(
"QListWidget{"
"border:none;"
"}"
"QListWidget::item {"
" border: 0px solid black;"
// " border-radius: 5px;"
// " padding: 5px;"
// " margin: 3px;"
"}"
"QListWidget::item:selected {"
" color: white;"
" border: 1px solid red;"
"}"
);
}
// void MainWindow::readUconfig(){
// char uconfigFile[500]="uconfig.cdh";
// assist::make_appData_filePath(uconfigFile);
// std::ifstream uconfigStream(uconfigFile, std::ios::in);
// if(!uconfigStream.is_open()){
// qDebug("Failed to open uconfig.cdh");
// return;
// =======
void MainWindow::prepareAddNewComboBox()
{
std::vector<string> langs=mainLangHolder->getLangList();
for(auto& it:langs){
ui->addNewLangDropdown->addItem(QString(it.c_str()));
}
ui->addNewLangDropdown->addItem("Select");
ui->addNewLangDropdown->setCurrentText("Select");
ui->addNewLangDropdown->setStyleSheet(
"QComboBox {"
" qproperty-alignment: AlignCenter;" // Centers text horizontally and vertically
" border:3px solid black;"
"}"
);
}
void MainWindow::prepareSettingsPage(){
ui->settingsBackButton->setIcon(QIcon(":/images/backArrowIcon.svg"));
ui->settingsBackButton->setIconSize(QSize(20,23));
CreteRoundFont.setPointSize(23);
ui->settingsTitle->setFont(CreteRoundFont);
ui->generalSettingsTitle->setFont(CreteRoundFont);
ui->snLSettingTitle->setFont(CreteRoundFont);
ui->fileSettingsTitle->setFont(CreteRoundFont);
CreteRoundFont.setPointSize(12);
ui->showPasswordButton->setFont(CreteRoundFont);
ui->usernameTitle->setFont(CreteRoundFont);
ui->oldPasswordTitle->setFont(CreteRoundFont);
ui->newPasswordTitle->setFont(CreteRoundFont);
ui->userUpdateButton->setFont(CreteRoundFont);
ui->usernameEdit->setFont(CreteRoundFont);
ui->oldPasswordEdit->setFont(CreteRoundFont);
ui->newPasswordEdit->setFont(CreteRoundFont);
ui->vaultLocationTitle->setFont(CreteRoundFont);
ui->vaultLocationEdit->setFont(CreteRoundFont);
ui->sysTrayCheckBox->setCheckState( trayEnabled ? Qt::Checked : Qt::Unchecked);
ui->OpenAtLoginCheckBox->setCheckState( loginEnabled ? Qt::Checked : Qt::Unchecked);
ui->usernameEdit->setPlaceholderText(QString::fromStdString(username));
ui->tbaLabe->setFont(CreteRoundFont);
}
void MainWindow::readUconfig(){
QSettings settings(company, appName);
QString u =settings.value("username","default_user").toString();
QString hs =settings.value("hashres","default_val").toString();
QString va =settings.value("vault","default").toString();
int ty =settings.value("type",0).toInt();
int tg =settings.value("tag",0).toInt();
bool login =settings.value("loginRun",false).toBool();
bool tray =settings.value("trayIcon",false).toBool();
int perpage =settings.value("showPerPage", 10).toInt();
username=u.toStdString();
hashResult=hs.toStdString();
vaultLocation=va.toStdString();
tagCount=tg;
additionalTypeCount=ty;
trayEnabled= tray;
loginEnabled= login;
showPerPage=perpage;
qDebug() << "got from settings==" << u << hs << va << ty << tg;
qDebug() << "got from settings==" << u.toStdString().c_str() << hs.toStdString().c_str() << va.toStdString().c_str() << ty << tg;
qDebug("the stuff got from uconfig was:\nusername\t%s\nhashres\t%s\nvault\t%s\ntag\t%d\ntype\t%d\n",username.c_str(),hashResult.c_str(),vaultLocation.c_str(),tagCount,additionalTypeCount);
}
void MainWindow::readData(){
char snippetVaultFile[assist::PATH_SIZE];
if(vaultLocation=="default"){
qDebug("the vault location is default");
std::strncpy(snippetVaultFile, "snipDatVault.cdh", sizeof(snippetVaultFile) - 1);
snippetVaultFile[sizeof(snippetVaultFile) - 1] = '\0';
assist::make_appData_filePath(snippetVaultFile);
}else{
std::strncpy(snippetVaultFile, vaultLocation.c_str(), sizeof(snippetVaultFile) - 1);
snippetVaultFile[sizeof(snippetVaultFile) - 1] = '\0';
}
std::string lineStore;
std::ifstream snippetVaultStream(snippetVaultFile,std::ios::in);
lineNum=1;
while(std::getline(snippetVaultStream,lineStore)){
string ifTags,name,filename,lang,tag;
std::vector<std::string> tags;
std::stringstream ss(lineStore);
bool lockStat;
getline(ss,name,',');
getline(ss,filename,',');
getline(ss,lang,',');
getline(ss,ifTags,',');
if (ifTags == "tags") {
// Read the remaining part of the line for tags
while (std::getline(ss, tag, ',')) {
tags.push_back(tag);
}
}
snippetBaseClass* obj=generateSnippetObject(lang);
obj->innit(name,filename,lineNum,lang,tags,this);
size_t lastDot = filename.find_last_of(".");
std::string nameWithoutExt = (lastDot == std::string::npos) ? filename : filename.substr(0, lastDot);
filenameStorage[nameWithoutExt] = obj;
if(obj->isLocked()) lockedStorage[nameWithoutExt]=obj;
mainLangHolder->insert(obj);
if(ifTags=="tags")mainTagHolder->insert(obj);
// deprecated mainStorage.push_back(obj);
//THIS IS WHERE JESSAN WILL ADD INSERT OF SEARCH CLASS
searchObj->insert(name,obj);
// Output or use the tags for testing
std::cout << "Name: " << name << ", Filename: " << filename
<< ", Lang: " << lang << ", Tags: ";
if(ifTags=="tags"){
for (const auto& t : tags) {
std::cout << t << " ";
}
}
std::cout << std::endl;
lineNum++;
}
qDebug()<<"read data complete.\ntotal snippets in filenamestorage: "<<filenameStorage.size()<<"\nTotal snippets in lockedstorage: "<<lockedStorage.size();
totalCount = filenameStorage.size();
searchObj->tellTotalCount(totalCount);
}
snippetBaseClass* MainWindow::generateSnippetObject(std::string lang){
snippetBaseClass* obj;
if(lang=="c"){
obj=new snippetC;
return obj;
}
if(lang=="cpp"){
obj=new snippetCPP;
return obj;
}
if(lang=="css"){
obj=new snippetCSS;
return obj;
}
if(lang=="java"){
obj=new snippetJAVA;
return obj;
}
if(lang=="py"){
obj=new snippetPY;
return obj;
}
else{
obj=new snippetCustom;
return obj;
}
}
void MainWindow::getTagInfo(string tagName, std::string &passedName, std::string& passedColor){
// mainTagHolder->getTagInfo(tagName,passedName,passedColor);
auto it = (*mainTagHolder)[tagName]; // Check if tagName exists in the map
if (it != nullptr) {
passedName = it->tagName;
passedColor = it->tagColor;
} else {
qDebug("Error: Tag not found for name: %s",tagName.c_str());
passedName = "NoTag";
passedColor = "";
}
}
void MainWindow::copyToClipboard(const QString& text){
//qDebug("copy to clipboard called");
clipboard->setText(text);
if(QApplication::clipboard()->text() == text){
ui->statusBar->showMessage("Copied to clipboard!", 2500);
} else {
warnUser("Text copy to clipboard failed! Please contact devs to report bug");
}
}
void MainWindow::addNewAction(){
QString newName=ui->newSnippetNameBox->text();
if(newName!=""){
if(containsSpaces(newName)){
warnUser("Please use name without spaces!!");
return;
}
if(ui->addNewLangDropdown->currentText()!="Select")
addNewSnippet(newName,ui->addNewLangDropdown->currentText());
else warnUser("Please Select Language");
}
else warnUser("Please give a name");
return;
}
void MainWindow::addNewSnippet(QString name, QString lang){
qDebug()<<"Add new final reached: "<<name<<" "<<lang;
//generate filename
std::string filename =generateUniqueFilename(name,lang,1);
if (filename == "stop") return;
std::string filenameWithoutExt= filename;
filename+=".cdh";
snippetBaseClass* obj=generateSnippetObject(lang.toStdString());
obj->innit( name.toStdString() , filename , lineNum , lang.toStdString() , std::vector<std::string>() ,this);
mainLangHolder->insert(obj);
filenameStorage[filenameWithoutExt]=obj;
//Insert into search here
searchObj->insert(name.toStdString(),obj);
totalCount++;
searchObj->tellTotalCount(totalCount);
//=====updating the vault file
std::string vaultDat=name.toStdString() + "," + filename + "," + lang.toStdString() + "," + "noTags";
qDebug()<<"gonna write to vault file: "<<vaultDat;
char vaultFilePath[assist::PATH_SIZE];
std::strncpy(vaultFilePath, "snipDatVault.cdh", sizeof(vaultFilePath) - 1);
vaultFilePath[sizeof(vaultFilePath) - 1] = '\0';
assist::make_appData_filePath(vaultFilePath);
if(assist::addLine(vaultFilePath,-1,vaultDat))
showAutoCloseMessageBox(this,"Success!","Snippet added to vault success!");
else{
warnUser("Snippets failed to add in vault! \n Please check logs and contact devs");
return;
}
//=============
//======making the snippet code file
obj->saveSnippetToFile("");
openSnippetInEditor(obj,name,false);
}
///
/// \brief MainWindow::generateUniqueFilename a function for the careful and well considered generation of filename for snippets
/// \param name the name of the snippet
/// \param lang the lang of the snippet
/// \param mode if mode =1, new snippet no obj exists yet, if mode =2, generating for renamed snippet
/// \return the generated filename
///
std::string MainWindow::generateUniqueFilename(const QString& name, const QString& lang, int mode, std::string oldFilename, snippetBaseClass *obj) {
std::string filename = name.toStdString() + lang.toStdString();
int i = 0; // Start from 0 to check `name+".cdh"` first
do {
if (i == 3) {
warnUser("This name has been used 3 times, please use another name");
return "stop";
}
if (i > 0) {
filename = name.toStdString() + lang.toStdString() + std::to_string(i);
}
i++;
} while (filenameStorage.find(filename) != filenameStorage.end());
if(mode==1){
//filenameStorage[filename]=obj;
return filename;
}
if(mode ==2){
filenameStorage.erase(oldFilename);
filenameStorage[filename]=obj;
return filename;
}
}
void MainWindow::showAutoCloseMessageBox(QWidget *parent,QString errTitle, QString msg) {
QMessageBox *msgBox = new QMessageBox(QMessageBox::Information,
errTitle,
msg,
QMessageBox::Ok,
parent);
msgBox->setAttribute(Qt::WA_DeleteOnClose); // Delete after closing
// Close the message box after 3000 ms (3 seconds)
QTimer::singleShot(3000, msgBox, &QMessageBox::accept);
msgBox->exec();
}
void MainWindow::warnUser(QString str)
{
// /*Using tooltip to warn user*/ QToolTip::showText(QCursor::pos(), str, nullptr, QRect(), 2000);
showAutoCloseMessageBox(this, "Waring!!",str);
qDebug()<<"User was warned: "<<str;
// >>>>>>> main
}
void MainWindow::closeTab()
{
//int idx = ui->editorTabs->indexOf(tab);
// if (idx != -1) { // Ensure the tab exists
// ui->editorTabs->removeTab(idx);
// }
// editorWidget* editor = qobject_cast<editorWidget*>(ui->editorTabs->widget(idx));
// if (editor) {
// editor->close();
// }
qDebug() << "Tab count: " << ui->editorTabs->count();
// ui->editorTabs->setCurrentIndex(0);
// if (!ui->editorTabs) {
// qDebug() << "ERROR: ui->editorTabs is NULL!";
// return;
// }
// qDebug() << "Editor tabs pointer:" << ui->editorTabs;
QWidget* currentTab = ui->editorTabs->currentWidget();
if (!currentTab) {
qDebug() << "No current tab selected!";
return;
}
int currentIdx=ui->editorTabs->indexOf(currentTab);
ui->editorTabs->removeTab(currentIdx);
currentTab->close();
}
std::vector<string> MainWindow::getLangList(){
return mainLangHolder->getLangList();
}
void MainWindow::openSnippetInEditor(snippetBaseClass* snipObj, QString& tabname, bool isOld)
{
editorWidget* newEditor=new editorWidget(this,this);
newEditor->assign(snipObj,isOld);
ui->editorTabs->addTab(newEditor,tabname);
newEditor->tellIdx(ui->editorTabs->indexOf(newEditor));
setMainIndex(2);
ui->editorTabs->setCurrentWidget(newEditor);
// ui->editorTabs->setCurrentIndex(ui->editorTabs->indexOf(newEditor));
if(ui->editorTabs->currentWidget()==ui->defaultTab) {
qDebug()<<"the current tab is default tab";
}
int i=ui->editorTabs->indexOf(ui->defaultTab);
if(ui->editorTabs->isTabVisible(i)){
ui->editorTabs->setTabVisible(i,false);
}
}
bool MainWindow::deleteSnippet(snippetBaseClass* obj){
if(obj->isLocked()){
if( !Julius->authenticate() ){
warnUser("Delete attempt stopped bacsue of wrong password!!");
return false;
}
}
//remove from tag holder
if(mainTagHolder->removeSnippet(obj)) qDebug()<<"removed from tagHolder";
else qDebug()<<"Snippet failed to remove from or didn't exist in tag holder";
//remove from lang holder
if(mainLangHolder->removeSnippet(obj)) qDebug()<<"snippet removed from lang holder";
else qDebug()<<"snippet should have been in the lang holder";
//remove from search trie
searchObj->remove(obj);
//remove from filehashmap
if (filenameStorage.erase(obj->getOldFilename())) qDebug() << "Filename removed from storage: " << QString::fromStdString(obj->getOldFilename());
else qDebug() << "Filename not found in storage: " << QString::fromStdString(obj->getOldFilename());
//delete from file
if(obj->deleteFromVault())
showAutoCloseMessageBox(this,"Success!","Snippet deleted from vault success!");
else{
warnUser("Snippets failed to delete from vault! \n Please check logs and contact devs");
return false;
}
//delete the snippet itself that is call it's destructor
delete obj;
totalCount--;
searchObj->tellTotalCount(totalCount);
return true;
}
void MainWindow::renameSnippet(std::string newName, snippetBaseClass *obj)
{
//change name in lang holder
//names are not stored here lol
//change name in tag holder
//names are not stored here lol
//name was already changed in filename holder
//change name in search trie
searchObj->rename(newName , obj);
}
void MainWindow::snipetLangChanged( snippetBaseClass *obj, std::string lang)
{
mainLangHolder->removeSnippet(obj);
mainLangHolder->insert(obj, lang);
}
void MainWindow::tagChanged(snippetBaseClass *obj)
{
mainTagHolder->removeSnippet(obj);
mainTagHolder->insert(obj);
}
bool MainWindow::containsSpaces(QString& str) {
return str.indexOf(' ') != -1; // indexOf returns -1 if no match is found
}
//END OF ADDITIONAL NON-SLOT BASED FUNCTIONS
void MainWindow::on_sidebarButton_clicked()
{
if(!ui->sidebar->isHidden()) ui->sidebar->hide();
else ui->sidebar->show();
}
void MainWindow::on_AddnewSidebarButton_clicked()
{
setMainIndex(0);
}
void MainWindow::on_SearchSidebarButton_clicked()
{
setMainIndex(1);
}
void MainWindow::on_EditorsSidebarButton_clicked()
{
setMainIndex(2);
}
void MainWindow::on_BrowseSidebarButton_clicked()
{
setMainIndex(3);
}
void MainWindow::on_usernameAndMainSettingsButton_clicked()
{
setMainIndex(4);
}
void MainWindow::on_centralSearchIcon_clicked()
{
}
void MainWindow::on_centralSearchBoxLE_returnPressed()
{
}
// <<<<<<< ryexocious-making-search-page
void MainWindow::on_searchBoxLineEdit_textChanged(const QString &arg1)
{
ui->snippetPreviewBoxAreaOnSearchPage->clear();
if(arg1!="" /*un comment this to see all*/ /*true*/) {
std::vector<std::pair<std::string, std::vector<snippetBaseClass *>>> searchRet= searchObj->searchWithPrefix(arg1.toStdString());
for (auto& itr : searchRet){
for (auto& itr2 : itr.second) {
// Create the custom widget
snippetPreviewBox* pb = new snippetPreviewBox(this, this);
pb->assignSnippet(itr2);
// Create a QListWidgetItem to hold the custom widget
QListWidgetItem* item = new QListWidgetItem(ui->snippetPreviewBoxAreaOnSearchPage);
// Set the size of the item to match the widget
item->setSizeHint(pb->sizeHint());
// Store snippetPreviewBox pointer inside Qt::UserRole
item->setData(Qt::UserRole, QVariant::fromValue(pb));
// Add the item to the list widget
ui->snippetPreviewBoxAreaOnSearchPage->addItem(item);
// Set the custom widget for this item (for display only)
ui->snippetPreviewBoxAreaOnSearchPage->setItemWidget(item, pb);
}
}
}
else return;
}
// =======
void MainWindow::on_newSnippetNameBox_textChanged(const QString &arg1)
{
if(arg1!="") ui->addNewButton->show();
else ui->addNewButton->hide();
}
void MainWindow::on_newSnippetNameBox_returnPressed()
{
addNewAction();
}
void MainWindow::on_addNewButton_clicked()
{
addNewAction();
}
void MainWindow::on_EditorsDefaultTabButton_clicked()
{
setMainIndex(3);
}
void MainWindow::on_downarrow_clicked()
{
if(devmode){
sandBox();
ui->maincontentsStack->setCurrentIndex(5);
}
else{
setMainIndex(3);
ui->browsePageStack->setCurrentIndex(1);
updateBrowseView();
}
}
void MainWindow::on_centralBrowseButton_clicked()
{
setMainIndex(3);
// >>>>>>> main
}
void MainWindow::on_snippetSettingsTestButton_clicked()