Repository navigation
Expand file tree
/
Copy pathsnippetliveclass.cpp
More file actions
371 lines (309 loc) 路 10.7 KB
/
Copy pathsnippetliveclass.cpp
File metadata and controls
371 lines (309 loc) 路 10.7 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
#include "snippetliveclass.h"
#include "mainwindow.h"
snippetLiveClass::snippetLiveClass() {}
snippetLiveClass::~snippetLiveClass()
{
//nothing to delete coz no dynamic memory was allocated here
SNIPPET.clear();
name.clear();
filename.clear();
lang.clear();
}
void snippetLiveClass::innit(std::string nam, std::string filenam, int linNum, std::string lng, std::vector<std::string> tgs, QMainWindow *MM)
// : name(nam)
// , filename(filenam)
// , lineNum(linNum)
// , lang(lng)
// , tags(tgs)
{
name=nam;
filename=filenam;
lineNum=linNum;
lang=lng;
tags=tgs;
masterWindow=MM;
MainWindow* mm=static_cast<MainWindow*>(masterWindow);
if (mm) {
mm->test(); // Now you can call MainWindow-specific functions
}
size_t pos = filename.find_last_of('.');
if (pos != std::string::npos) {
// Extract the extension
std::string extension = filename.substr(pos + 1);
qDebug("got the file extension: %s",extension.c_str());
// Perform actions based on the extension
if (extension == "cdh") {
isLockedVar=false;
} else if (extension == "scdh") {
isLockedVar=true;
}
}
qDebug() << "Snippet Initialized: " ;
qDebug() << "Name: " << name;
qDebug() << "Filename: " << filename;
qDebug() << "Line Number: " << lineNum;
qDebug() << "Language: " << lang;
qDebug() << "Locked Status: " << isLockedVar;
std::cout << "Tags: ";
if (tags.empty()) {
std::cout << "No tags" << std::endl;
} else {
for (const auto& tag : tags) {
std::cout << tag << " ";
}
std::cout << std::endl;
}
}
std::vector<std::string>* snippetLiveClass::getInfo(std::string& nam, std::string& lng)
{
nam=name;
lng=lang;
return &tags;
}
std::string snippetLiveClass::getSnippet()
{
if (!SNIPPET.empty()) {
return SNIPPET;
}
else if( isLocked() ){
MainWindow* mainW=static_cast<MainWindow*>(masterWindow);
if (mainW) {
return mainW->decryptText(filename.c_str()).toStdString(); // Now you can call MainWindow-specific functions
}
}
else {
char snippetCodeFile[assist::PATH_SIZE];
// for now considering vaultLocation is default
if (true /*vaultLocation == "default"*/) {
qDebug("the vault location is default");
std::strncpy(snippetCodeFile, filename.c_str(), sizeof(snippetCodeFile) - 1);
snippetCodeFile[sizeof(snippetCodeFile) - 1] = '\0';
assist::make_appData_filePath(snippetCodeFile);
} else {
// Implement other vault location handling later
}
std::ifstream file(snippetCodeFile, std::ios::in);
if (!file) {
//throw std::runtime_error("Failed to open file: " + filename);
MainWindow* mainW= static_cast<MainWindow*>(masterWindow);
mainW->warnUser("file failed to open, maybe doesn't exist");
}
std::ostringstream buffer;
buffer << file.rdbuf();
SNIPPET = buffer.str();
return SNIPPET;
}
}
std::vector<std::string> snippetLiveClass::getTags()
{
return tags;
}
bool search(const std::string &str, std::vector<snippetBaseClass*> ret){
return true;
}
// void snippetLiveClass::setEditor(QTextDocument *)
// {
// }
std::string snippetLiveClass::getLang() {
std::cerr<<"get lang in live class was called"<<std::endl;
return lang;
}
bool snippetLiveClass::isCustom()
{
return false;
}
void snippetLiveClass::putColors(std::unordered_map<std::string, std::vector<std::string>> &colorsData)
{
return;
}
void snippetLiveClass::putTags(std::vector<std::string> &foundTags)
{
tags=foundTags;
}
bool snippetLiveClass::isLocked()
{
return isLockedVar;
}
// <<<<<<< ryexocious-making-search-page
//search option
void snippetLiveClass::insert(const std::string &str, snippetBaseClass *targ){
//absolutely nothing;
}
bool snippetLiveClass::search(const std::string& str, std::vector<snippetBaseClass*>& ret)
{
return false;
}
std::vector<std::pair<std::string, std::vector<snippetBaseClass *>>> snippetLiveClass::searchWithPrefix(const std::string &prefix)
{
return std::vector<std::pair<std::string, std::vector<snippetBaseClass *>>>();
}
// =======
std::string& snippetLiveClass::EditSnippet()
{
if (!SNIPPET.empty()) {
return SNIPPET;
} else {
char snippetCodeFile[assist::PATH_SIZE];
// for now considering vaultLocation is default
if (true /*vaultLocation == "default"*/) {
qDebug("the vault location is default");
std::strncpy(snippetCodeFile, filename.c_str(), sizeof(snippetCodeFile) - 1);
snippetCodeFile[sizeof(snippetCodeFile) - 1] = '\0';
assist::make_appData_filePath(snippetCodeFile);
} else {
// Implement other vault location handling later
}
std::ifstream file(snippetCodeFile, std::ios::in);
if (!file) {
throw std::runtime_error("Failed to open file: " + filename);
}
std::ostringstream buffer;
buffer << file.rdbuf();
SNIPPET = buffer.str();
return SNIPPET;
}
}
bool snippetLiveClass::saveSnippetToFile(string snippet)
{
SNIPPET=snippet;
if(isLocked()){
MainWindow* mainW=static_cast<MainWindow*>(masterWindow);
mainW->encryptText( QString( filename.c_str() ) , QString::fromStdString(snippet));
return true;
}
char snippetCodeFile[assist::PATH_SIZE];
// Assuming vaultLocation is "default" for now
if (true /*vaultLocation == "default"*/) {
qDebug("The vault location is default");
std::strncpy(snippetCodeFile, filename.c_str(), sizeof(snippetCodeFile) - 1);
snippetCodeFile[sizeof(snippetCodeFile) - 1] = '\0';
assist::make_appData_filePath(snippetCodeFile);
} else {
// Implement other vault location handling later
}
// Open file for writing
std::ofstream file(snippetCodeFile, std::ios::out | std::ios::trunc);
if (!file) {
throw std::runtime_error("Failed to open file for writing: " + filename);
assist::errLog(std::string("Failed to open file for writing: " + filename).c_str());
return false;
}
file << snippet;
file.close();
return true;
}
bool snippetLiveClass::updateSnippetDetails(std::string nam,std::string filenam, std::vector<std::string> tgs, std::string lng, bool lock ){
if (lock) {
filenam+=".scdh";
}
else{
filenam+=".cdh";
}
if(updateSnippetFilename(filenam) ) filename=filenam;
tags.clear();
tags=tgs;
if(name!=nam) name=nam;
if(lang!=lng) lang=lng;
std::string vaultDat=name+","+filename+","+lang+",";
if(tags.size()>0){
vaultDat+="tags";
for ( auto& tag : tags) {
vaultDat+=","+tag;
}
}
else vaultDat+="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);
/*return*/ assist::editLine(vaultFilePath,lineNum,vaultDat);
if( isLockedVar != lock) updateFileSecurity(lock);
}
bool snippetLiveClass::updateSnippetFilename(std::string newFilename) {
if(filename==newFilename){
return false;
}
// Attempt to rename the file
char snippetCodeFileOld[assist::PATH_SIZE];
if (true /*vaultLocation == "default"*/) {
qDebug("The vault location is default");
std::strncpy(snippetCodeFileOld, filename.c_str(), sizeof(snippetCodeFileOld) - 1);
snippetCodeFileOld[sizeof(snippetCodeFileOld) - 1] = '\0';
assist::make_appData_filePath(snippetCodeFileOld);
} else {
// Implement other vault location handling later
}
char snippetCodeFileNew[assist::PATH_SIZE];
if (true /*vaultLocation == "default"*/) {
qDebug("The vault location is default");
std::strncpy(snippetCodeFileNew, newFilename.c_str(), sizeof(snippetCodeFileNew) - 1);
snippetCodeFileNew[sizeof(snippetCodeFileNew) - 1] = '\0';
assist::make_appData_filePath(snippetCodeFileNew);
} else {
// Implement other vault location handling later
}
std::string oldFilePath = snippetCodeFileOld;
std::string newFilePath = snippetCodeFileNew;
if (std::rename(oldFilePath.c_str(), newFilePath.c_str()) != 0) {
qDebug() << "Error renaming file: " << strerror(errno);
return false;
}
qDebug()<<"the code file name was updated successfully";
return true;
}
void snippetLiveClass::updateFileSecurity(bool lock)
{
MainWindow* mainW=static_cast<MainWindow*>(masterWindow);
if (mainW) {
if(lock){
mainW->encryptText( QString( filename.c_str() ) , QString( getSnippet().c_str() ) );
}
else if(!lock){
SNIPPET = mainW->decryptText( QString( filename.c_str() ) ).toStdString();
saveSnippetToFile(SNIPPET);
}
}
isLockedVar = lock;
}
std::string snippetLiveClass::getOldFilename(){
size_t lastDot = filename.find_last_of(".");
std::string nameWithoutExt = (lastDot == std::string::npos) ? filename : filename.substr(0, lastDot);
return nameWithoutExt;
}
bool snippetLiveClass::deleteFromVault()
{
// 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::removeLine(vaultFilePath,lineNum)){
char snippetCodeFileOld[assist::PATH_SIZE];
if (true /*vaultLocation == "default"*/) {
qDebug("The vault location is default");
std::strncpy(snippetCodeFileOld, filename.c_str(), sizeof(snippetCodeFileOld) - 1);
snippetCodeFileOld[sizeof(snippetCodeFileOld) - 1] = '\0';
assist::make_appData_filePath(snippetCodeFileOld);
// if (std::remove(snippetCodeFileOld) == 0) {
// qDebug() << "File deleted successfully.\n";
// } else {
// std::perror("Error deleting file");
// return false;
// }
MainWindow* mainW=static_cast<MainWindow*>(masterWindow);
if (mainW) {
mainW->scheduleDeletion(lineNum, QString(snippetCodeFileOld));
}
return true;
} else {
// Implement other vault location handling later
}
// }
// else{
// return false;
// }
}
bool snippetLiveClass::remove(snippetBaseClass *obj)
{
return false;
}