-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilenavigator.cpp
745 lines (616 loc) · 20.1 KB
/
filenavigator.cpp
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
//
// FileNavigator
//
// BACKGROUND
// ----------
//
// This function managed a file navigation widget, which is a line-edit style input
// with the text split into three distinct fields:
//
// FOLDER : FILE : BACKUP
//
// Folders can contain plaintext or encrypted files. Folders with encrypted files have a .enc extension.
// File extensions are .txt or .enctxt depending on whether they are encrypted or not.
// Backup file extensions are .bak or .encbak, depending on whether they are encrypted or not.
//
// CLASS VARIABLES
// ---------------
//
// directory - Location of the save files
// tempdirectory - temporary directory, used for imports / exports
// folders - menu list of the current folders
// files - menu list of the files in the selected folders (if any)
// backups - menu list of the backup files for the selected file (if any)
//
// EXAMPLE USE
// -----------
//
// FileNavigator nav ; // Create Navigator
// nav.setPath("/home/data") ; // Initialise and define storage area
// if (nav.exec()==0) return ; // Get the user input
// if (nav.isRenameFolder()) {
// QString& folder = nav.currentFolderPath() ;
// // Input new folder name and rename
// } else if (nav.isDeleteFolder()) {
// QString& folder = nav.currentFolderPath() ;
// // Confirm and delete folder
// } else if (nav.isCreateFolder()) {
// // Input and create new folder
// } else if (nav.isCreateEncryptedFolder()) {
// // Input and create encrypted folder
// } else if (nav.isCreateFile()) {
// // Input and create a dummy file
// }
#include <QDir>
#include <QRegularExpression>
#include <QDate>
#include <QApplication>
#include <QInputDialog>
#include "filetypes.h"
#include "filenavigator.h"
#include "../Lib/supportfunctions.h"
#include "../Lib/alertsound.h"
FileNavigator::FileNavigator(QWidget *parent) :
QLineEdit(parent)
{
folders.clear() ;
files.clear() ;
backups.clear() ;
folderindex=0 ;
fileindex=0 ;
backupindex=0 ;
selection=0 ;
selectnew=false ;
isbackup=false ;
nofilename=true ;
connect(this, SIGNAL(cursorPositionChanged(int, int)), this, SLOT(on_cursorPositionChanged(int, int))) ;
update(0) ;
}
FileNavigator::~FileNavigator()
{
}
//====================================================
//
// Set the storage path for all saved files
//
void FileNavigator::setPath(QString directory)
{
QDir dir ;
nofilename=false ;
this->directory = directory ;
this->tempdirectory = directory + "/__temp__" ;
dir.mkpath(this->directory) ;
dir.mkpath(this->tempdirectory) ;
// Load Folders
setFilename() ;
if (folders.size()>2) {
folderindex=2 ;
// Load Files
setFilename() ;
if (files.size()>2) {
fileindex=2 ;
// Load Backups
setFilename() ;
} else {
// Leave on Create File
fileindex=1 ;
}
}
update(0) ;
}
//====================================================
//
// Clear and Set the Filename
//
void FileNavigator::clearFilename()
{
nofilename=true ;
}
//
// Re-scan the folders, and set the current folder/file/backup indexes
//
void FileNavigator::setFilename(QString foldername, QString filename, QString backupname)
{
QString folder ;
QString file, filenoe ;
QString backup ;
bool foundfolder=false ;
bool foundfile=false ;
nofilename=false ;
folder="" ;
file="" ;
backup="" ;
// Capture the names of the current folder/file/backup
if (!foldername.isEmpty()) folder=foldername ;
else if (folderindex>0) folder = folders[folderindex] ;
if (!filename.isEmpty()) file=filename ;
else if (folderindex>0 && fileindex>0) file = files[fileindex] ;
filenoe=file.replace(TXT,"").replace(ENC,"") ;
if (!backupname.isEmpty()) backup=backupname ;
else if (folderindex>0 && fileindex>0 && backupindex>=0) backup = backups[backupindex] ;
// Reset indexes, and re-scan
folderindex=-1 ;
fileindex=-1 ;
backupindex=-1 ;
folders.clear() ;
files.clear() ;
backups.clear() ;
bool encrypted = folder.endsWith(ENF) ;
// load folders and search for folder by name
loadFolders(directory) ;
foundfolder=false ;
for (int i=0, sz=folders.size(); i<sz && !foundfolder; i++) {
if (folders.at(i).compare(folder)==0) {
folderindex=i ;
foundfolder=true ;
}
}
// Load files and seach for file by name (without extension)
if (foundfolder) {
loadFiles(directory + "/" + folders[folderindex], encrypted?ENC:TXT) ;
foundfile=false ;
for (int i=0, sz=files.size(); i<sz && !foundfile; i++) {
QString thisfile = files.at(i) ;
if (filenoe.compare(thisfile.replace(TXT,"").replace(ENC,""))==0) {
fileindex=i ;
foundfile=true ;
}
}
}
// Load backups and search for backup by name
if (foundfile) {
loadBackups(directory + "/" + folders[folderindex] + "/" + filenoe, encrypted?ENB:BAK) ;
for (int i=0, sz=backups.size(); i<sz; i++) {
if (backups.at(i).compare(backup)==0) {
backupindex=i ;
}
}
}
// Hide backups if file is --
if (isCreateFile()) {
backupindex=-1 ;
}
// Set the isbackup (readonly) flag
if (backupindex>0) isbackup=true ;
else isbackup=false ;
// Force the current selection
if (backupindex<0 && selection==2) selection=1 ;
if (fileindex<0 && selection==1) selection=0 ;
// Update the lineedit contents
update(0) ;
}
//
// Set the current folder/file/backup indexes based on a given path
//
void FileNavigator::setFilenameFromPath(QString path)
{
QString folder, file, backup ;
QRegularExpression filex("/([^/]*)/([^/]*)") ;
QRegularExpression backupx("/([^/]*)/([^/]*)/([^/]*)") ;
path = path.replace(directory,"") ;
nofilename=false ;
QRegularExpressionMatch filem = filex.match(path) ;
QRegularExpressionMatch backupm = backupx.match(path) ;
if (backupm.hasMatch()) {
folder = backupm.captured(1) ;
file = backupm.captured(2) ;
backup = backupm.captured(3) ;
} else if (filem.hasMatch()) {
folder = filem.captured(1) ;
file = filem.captured(2) ;
backup = "-" ;
}
setFilename(folder, file, backup) ;
}
//
// Update the lineedit contents
//
void FileNavigator::update(int cursorpos)
{
if (folders.size()==0) return ;
static int reentrant=0 ;
if (reentrant==1) return ;
reentrant++ ;
QString entry ;
QString txt ;
int s1=0, s2=0 ;
entry = folders.at(folderindex) ;
entry.replace("_", "") ;
entry.replace(ENF, " (encrypted) ") ;
txt=entry ;
if (selection==0) s2=txt.length() ;
if (folderindex>=2 && fileindex>=0) {
if (cursorpos>=txt.length()) selection=1 ;
txt = txt + ": " ;
if (selection==1) s1 = txt.length() ;
entry = files.at(fileindex) ;
entry.replace("_", "") ;
entry.replace(TXT, "") ;
entry.replace(ENC, "") ;
txt = txt + entry ;
if (selection==1) s2 = txt.length() ;
}
txt = txt + " " ;
if (folderindex>=2 && fileindex>=2) {
if (cursorpos>=txt.length()) selection=2 ;
QString backupdate ;
if (backupindex<=0) backupdate = "latest" ;
else backupdate = parseBackupDate(backups.at(backupindex)) ;
txt = txt + "(" ;
if (selection==2) s1 = txt.length() ;
txt = txt + backupdate ;
if (selection==2) s2 = txt.length() ;
txt = txt + ") " ;
}
isbackup = backupindex>0 ;
this->setText(txt);
this->setCursorPosition(s1) ;
#ifndef BEFOREQT54
// There are some bugs with respect to the braille diplay
// so we can't show the selection
this->setSelection(s1, s2-s1) ;
#endif
reentrant-- ;
}
QString& FileNavigator::getPath()
{
return this->directory ;
}
QString& FileNavigator::getTempFolderPath()
{
return this->tempdirectory ;
}
void FileNavigator::loadFolders(QString directory)
{
parseDirectory(directory, folders, "", false, true) ;
// Find temp folder and remove it
int idx=-1 ;
for (int i=0; i<folders.length(); i++) {
QString entry = folders.at(i) ;
if (entry.compare("__temp__")==0) idx=i ;
}
if (idx>=0) folders.removeAt(idx) ;
folders.insert(0, "-- create folder --") ;
folders.insert(0, "-- create encrypted folder --") ;
if (folders.size()==2) folderindex=0 ;
else folderindex=2 ;
}
void FileNavigator::loadFiles(QString directory, QString mask1)
{
parseDirectory(directory, files, mask1, false, true) ;
files.insert(0, "-- create file --") ;
files.insert(0, "-- rename folder --") ;
if (files.size()==2) {
files.insert(0, "-- delete folder --") ;
}
// Index is either first file, or --createfile--. Either way, it is index 2
fileindex=2 ;
}
void FileNavigator::loadBackups(QString directory, QString mask1)
{
if (isCreateFile() || isRenameFolder() || isDeleteFolder()) {
backups.clear() ;
backupindex=-1 ;
} else {
parseDirectory(directory, backups, mask1, true, true) ;
if (backups.size()>0) {
backupindex=0 ;
} else {
backupindex=-1 ;
}
}
}
QString& FileNavigator::getFolderPath(QString foldername)
{
if (!foldername.isEmpty()) {
folderPath = directory + "/" + foldername ;
} else {
folderPath = directory + "/" + folders.at(folderindex) ;
}
return folderPath ;
}
QString& FileNavigator::getEditableFilePath(QString filename)
{
if (filename.isEmpty() && fileindex<0) {
editableFilePath = "" ;
} else {
//bool preferencrypted = isFolderEncrypted() ;
editableFilePath = getFolderPath() + "/" ;
if (filename.isEmpty()) {
editableFilePath = editableFilePath + files.at(fileindex) ;
} else {
editableFilePath = editableFilePath + filename ;
}
}
return editableFilePath ;
}
QString& FileNavigator::getFilePath(QString filename, bool preferbackups)
{
bool preferencrypted = isFolderEncrypted() ;
if (isDeleteFolder() || isRenameFolder()) {
filePath = getFolderPath() ;
} else if (isCreateFolder()) {
filePath="" ;
} else if (!filename.isEmpty()) {
filePath = getFolderPath() + "/" + filename + (preferencrypted?ENC:TXT) ;
} else {
filePath = getFolderPath() ;
if (backupindex>0 || preferbackups) {
QString filename = files.at(fileindex) ;
filename = filename.replace(ENC, "").replace(TXT,"") ;
filePath = filePath + "/" + filename + "/" + backups.at(backupindex) ;
} else if (fileindex>=0) {
filePath = filePath + "/" + files.at(fileindex) ;
}
}
return filePath ;
}
QString& FileNavigator::getBackupFolderPath()
{
backupFolderPath = "" ;
if (fileindex>=0 && !isCreateFolder() && !isCreateFile() && !isRenameFolder()) {
QString filename = files.at(fileindex) ;
backupFolderPath = getFolderPath() + "/" + filename.replace(ENC,QString("")).replace(TXT,QString("")) ;
}
return backupFolderPath ;
}
// return latest backup
// or path to today's backup (create)
QString& FileNavigator::getBackupFilePath(bool create)
{
bool preferencrypted = isFolderEncrypted() ;
backupFilePath = "" ;
backupFilePath = getBackupFolderPath() ;
if (!backupFilePath.isEmpty()) {
if (create) {
QDate today = QDate::currentDate() ;
backupFilePath = backupFilePath + "/" + today.toString("yyyyMMdd") + (preferencrypted?ENB:BAK) ;
} else if (backupindex>=0) {
backupFilePath = backupFilePath + "/" + backups.at(0) + (preferencrypted?ENB:BAK) ;
} else {
backupFilePath="" ;
}
}
return backupFilePath ;
}
QString& FileNavigator::getFileDescription(bool includefolder, bool includebackupdate)
{
fileName = "" ;
if (nofilename) {
fileName="<none>" ;
} else {
bool isdeletefolder = isDeleteFolder() ;
bool isrenamefolder = isRenameFolder() ;
if (includefolder) {
QString foldername = folders.at(folderindex) ;
if (folderindex>0) fileName = foldername.replace(ENF,"") + ": " ;
}
if (folderindex>0 && !isrenamefolder && !isdeletefolder && fileindex>0) {
QString filename = files.at(fileindex) ;
fileName = fileName + filename.replace(TXT,"").replace(ENC,"") ;
}
if (includebackupdate) {
QString backupdate ;
if (backupindex<0) backupdate="" ;
else if (backupindex==0) backupdate=" (latest)" ;
else backupdate = " (" + parseBackupDate(backups.at(backupindex)) + ")" ;
if (folderindex>0 && !isdeletefolder && !isrenamefolder && fileindex>0 && backupindex>=0) fileName = fileName + backupdate ;
}
}
return fileName ;
}
bool FileNavigator::isCreateFolder()
{
if (folderindex>=folders.size() || folderindex<0) return false ;
return (folders.at(folderindex).compare("-- create folder --")==0) ;
}
bool FileNavigator::isDeleteFolder()
{
if (fileindex>=files.size() || fileindex<0) return false ;
return (files.at(fileindex).compare("-- delete folder --")==0) ;
}
bool FileNavigator::isRenameFolder()
{
if (fileindex>=files.size() || fileindex<0) return false ;
return (files.at(fileindex).compare("-- rename folder --")==0) ;
}
bool FileNavigator::isCreateEncryptedFolder()
{
if (folderindex>=folders.size() || folderindex<0) return false ;
return (folders.at(folderindex).compare("-- create encrypted folder --")==0) ;
}
bool FileNavigator::isCreateFile()
{
if (fileindex>=files.size() || fileindex<0) return false ;
return (files.at(fileindex).compare("-- create file --")==0) ;
}
bool FileNavigator::isBackup()
{
return (backupindex>0) ;
}
bool FileNavigator::isFolderEncrypted()
{
if (folderindex>=folders.size() || folderindex<0) return false ;
return folders.at(folderindex).endsWith(ENF) ;
}
bool FileNavigator::isFileEncrypted()
{
if (backupindex>0) {
if (backupindex>=files.size()) return false ;
return backups.at(backupindex).endsWith(ENB) ;
} else {
if (fileindex>=files.size() || fileindex<0) return false ;
return files.at(fileindex).endsWith(ENC) ;
}
}
void FileNavigator::on_cursorPositionChanged(int from, int to)
{
Q_UNUSED(from) ;
// Nasty hack: the default is to leave the cursor at the end of the text
// when the text is first added
if (to>=text().length()-2) to=0 ;
update(to) ;
}
void FileNavigator::keyPressEvent(QKeyEvent *event)
{
bool changed=false, reloadfiles=false, reloadbackups=false ;
int key = event->key() ;
if (key==Qt::Key_Enter || key==Qt::Key_Return || key==Qt::Key_Escape) {
QLineEdit::keyPressEvent(event);
return ;
}
else if (key==Qt::Key_Left) {
selection-- ;
if (selection<0) selection=0 ;
else changed=true ;
}
else if (key==Qt::Key_Right) {
selection++;
if (selection>2) selection=2 ;
if (selection>1 && backups.size()<=0) selection=1 ;
if (selection>0 && files.size()<=0) selection=0 ;
changed=true ;
}
else if (key==Qt::Key_Up) {
switch (selection) {
case 0: folderindex-- ;
if (folderindex<0) folderindex=0 ;
reloadfiles=true ; reloadbackups=true ;
fileindex=0 ;
break ;
case 1: fileindex-- ;
if (fileindex<0) fileindex=0 ;
reloadbackups=true ;
break ;
case 2: backupindex-- ;
if (backupindex<0) backupindex=0 ;
break ;
}
changed=true ;
}
else if (key==Qt::Key_Down) {
switch (selection) {
case 0: folderindex++ ;
if (folderindex>=folders.size()) folderindex=folders.size()-1 ;
reloadfiles=true ; reloadbackups=true ;
fileindex=0 ;
break ;
case 1: fileindex++ ;
if (fileindex>=files.size()) fileindex=files.size()-1 ;
reloadbackups=true ;
break ;
case 2: backupindex++ ;
if (backupindex>=backups.size()) backupindex=backups.size()-1 ;
break ;
}
changed=true ;
}
else if (key==Qt::Key_Minus || (key>=Qt::Key_A && key<=Qt::Key_Z) || (key>=Qt::Key_0 && key<=Qt::Key_9)) {
QChar letter ;
if (key==Qt::Key_Minus) letter = '-' ;
else if (key>=Qt::Key_A && key<=Qt::Key_Z) letter = QChar('a' + key - Qt::Key_A) ;
else letter = QChar('0' + key - Qt::Key_0) ;
switch (selection) {
case 0:
if (folders.size()<=0) {
play(Error) ;
} else {
int currentindex=folderindex ;
bool finished=false ;
while (!finished) {
currentindex++ ;
if (currentindex>=folders.size()) currentindex=0 ;
QChar ch = folders.at(currentindex).at(0).toLower() ;
if (ch==letter) {
finished=true ;
reloadfiles=true ;
changed=true ;
}
if (currentindex==folderindex) {
finished=true ;
}
}
if (!changed) {
play(Error) ;
} else if (currentindex>folderindex) {
play(Found) ;
} else {
play(Wrapped) ;
}
folderindex=currentindex ;
fileindex=0 ;
backupindex=0 ;
}
break ;
case 1:
if (files.size()<=0) {
play(Error) ;
} else {
int currentindex=fileindex ;
bool finished=false ;
while (!finished) {
currentindex++ ;
if (currentindex>=files.size()) currentindex=0 ;
QChar ch = files.at(currentindex).at(0).toLower() ;
if (ch==letter) {
finished=true ;
reloadbackups=true ;
changed=true ;
}
if (currentindex==fileindex) {
finished=true ;
}
}
if (!changed) {
play(Error) ;
} else if (currentindex>fileindex) {
play(Found) ;
} else {
play(Wrapped) ;
}
fileindex=currentindex ;
backupindex=0 ;
}
break ;
case 2:
play(Error) ;
break ;
}
}
if (isCreateEncryptedFolder() || isCreateFolder()) {
fileindex=-1 ;
files.clear() ;
}
if (fileindex>=0 && reloadfiles) {
QString& folderpath = getFolderPath() ;
bool encrypted = folderpath.endsWith(ENF) ;
loadFiles(folderpath, encrypted?ENC:TXT) ;
}
if (fileindex<0 || isCreateFile() || isRenameFolder() || isDeleteFolder()) {
backupindex=-1 ;
backups.clear() ;
}
if (fileindex>=0 && reloadbackups) {
QString filename = files.at(fileindex) ;
bool encrypted = filename.endsWith(ENC) ;
filename = filename.replace(TXT,"").replace(ENC,"") ;
loadBackups(getFolderPath() + "/" + filename, encrypted?ENB:BAK) ;
}
if (changed) update() ;
}
// Used by EasyNotepad
QString& FileNavigator::parseBackupDate(QString backupdate)
{
static QString parsedBackupDate ;
parsedBackupDate = backupdate ;
if (backupdate.length()>=8) {
int d, m, y ;
y = backupdate.left(4).toInt() ;
m = backupdate.mid(4,2).toInt() ;
d = backupdate.mid(6,2).toInt() ;
QDate date ;
date.setDate(y, m, d) ;
if (date.isValid()) parsedBackupDate = date.toString("dd MMM yyyy") ;
}
return parsedBackupDate ;
}