/
filemanager.cpp
753 lines (539 loc) · 22 KB
/
filemanager.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
746
747
748
749
750
751
752
753
/*******************************************************************************
Copyright (C) The University of Auckland
OpenCOR is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenCOR is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*******************************************************************************/
//==============================================================================
// File manager
//==============================================================================
#include "corecliutils.h"
#include "coreguiutils.h"
#include "filemanager.h"
//==============================================================================
#include <QApplication>
#include <QFile>
#include <QTimer>
//==============================================================================
namespace OpenCOR {
namespace Core {
//==============================================================================
FileManager::FileManager() :
mFiles(QMap<QString, File *>()),
mFilesReadable(QMap<QString, bool>()),
mFilesWritable(QMap<QString, bool>()),
mCheckFilesEnabled(true)
{
// Create our timer
mTimer = new QTimer(this);
// A connection to handle the timing out of our timer
connect(mTimer, SIGNAL(timeout()),
this, SLOT(checkFiles()));
// Keep track of when OpenCOR gets/loses the focus
// Note: the focusWindowChanged() signal comes from QGuiApplication, so we
// need to check whether we are running the console version of OpenCOR
// or its GUI version...
if (dynamic_cast<QGuiApplication *>(QCoreApplication::instance())) {
connect(qApp, SIGNAL(focusWindowChanged(QWindow *)),
this, SLOT(focusWindowChanged()));
}
}
//==============================================================================
FileManager::~FileManager()
{
// Delete some internal objects
delete mTimer;
// Remove all the managed files
foreach (File *file, mFiles)
delete file;
}
//==============================================================================
void FileManager::startStopTimer()
{
// Start our timer if OpenCOR is active and we have files, or stop it if
// either OpenCOR is not active or we don't have files anymore
// Note #1: if we are to start our timer, then we check files first since
// waiting one second may seem long to a user (when some files have
// changed and after having reactivated OpenCOR)...
// Note #2: checking files may result in a message box being shown and,
// therefore, in a focusWindowChanged() signal being emitted. To
// handle that signal would result in reentry, so we temporarily
// disable our handling of it...
if ( !mTimer->isActive()
&& Core::opencorActive() && !mFiles.isEmpty()) {
disconnect(qApp, SIGNAL(focusWindowChanged(QWindow *)),
this, SLOT(focusWindowChanged()));
checkFiles();
connect(qApp, SIGNAL(focusWindowChanged(QWindow *)),
this, SLOT(focusWindowChanged()));
mTimer->start(1000);
} else if ( mTimer->isActive()
&& (!Core::opencorActive() || mFiles.isEmpty())) {
mTimer->stop();
}
}
//==============================================================================
FileManager * FileManager::instance()
{
// Return the 'global' instance of our file manager class
static FileManager instance;
return static_cast<FileManager *>(globalInstance("OpenCOR::Core::FileManager::instance()",
&instance));
}
//==============================================================================
FileManager::Status FileManager::manage(const QString &pFileName,
const File::Type &pType,
const QString &pUrl)
{
// Manage the given file, should it not be already managed
QString nativeFileName = nativeCanonicalFileName(pFileName);
if (QFile::exists(nativeFileName)) {
if (file(nativeFileName)) {
return AlreadyManaged;
} else {
// The file isn't already managed, so add it to our list of managed
// files and let people know about it being now managed
mFiles.insert(nativeFileName, new File(nativeFileName, pType, pUrl));
startStopTimer();
emit fileManaged(nativeFileName);
return Added;
}
} else {
return DoesNotExist;
}
}
//==============================================================================
FileManager::Status FileManager::unmanage(const QString &pFileName)
{
// Unmanage the given file, should it be managed
QString nativeFileName = nativeCanonicalFileName(pFileName);
File *nativeFile = file(nativeFileName);
if (nativeFile) {
// The file is managed, so we can remove it
mFiles.remove(nativeFileName);
delete nativeFile;
startStopTimer();
emit fileUnmanaged(nativeFileName);
return Removed;
} else {
return NotManaged;
}
}
//==============================================================================
File * FileManager::file(const QString &pFileName) const
{
// Return the File object, if any, associated with the given file
return mFiles.value(nativeCanonicalFileName(pFileName));
}
//==============================================================================
QString FileManager::sha1(const QString &pFileName) const
{
// Return the SHA-1 value of the given file, should it be managed
File *nativeFile = file(nativeCanonicalFileName(pFileName));
if (nativeFile)
return nativeFile->sha1();
else
return QString();
}
//==============================================================================
void FileManager::reset(const QString &pFileName)
{
// Reset the given file, should it be managed
File *nativeFile = file(nativeCanonicalFileName(pFileName));
if (nativeFile)
nativeFile->reset();
}
//==============================================================================
int FileManager::newIndex(const QString &pFileName) const
{
// Return the given file's new index, if it is being managed
File *nativeFile = file(nativeCanonicalFileName(pFileName));
if (nativeFile)
return nativeFile->newIndex();
else
return 0;
}
//==============================================================================
QString FileManager::url(const QString &pFileName) const
{
// Return the given file's URL, if it is being managed
File *nativeFile = file(nativeCanonicalFileName(pFileName));
if (nativeFile)
return nativeFile->url();
else
return QString();
}
//==============================================================================
bool FileManager::isDifferent(const QString &pFileName) const
{
// Return whether the given file, if it is being managed, is different from
// its corresponding physical version
File *nativeFile = file(nativeCanonicalFileName(pFileName));
if (nativeFile)
return nativeFile->isDifferent();
else
return false;
}
//==============================================================================
bool FileManager::isDifferent(const QString &pFileName,
const QString &pFileContents) const
{
// Return whether the given file, if it is being managed, has the same
// contents has the given one
File *nativeFile = file(nativeCanonicalFileName(pFileName));
if (nativeFile)
return nativeFile->isDifferent(pFileContents);
else
return false;
}
//==============================================================================
bool FileManager::isNew(const QString &pFileName) const
{
// Return whether the given file, if it is being managed, is new
File *nativeFile = file(nativeCanonicalFileName(pFileName));
if (nativeFile)
return nativeFile->isNew();
else
return false;
}
//==============================================================================
bool FileManager::isRemote(const QString &pFileName) const
{
// Return whether the given file, if it is being managed, is a remote one
File *nativeFile = file(nativeCanonicalFileName(pFileName));
if (nativeFile)
return nativeFile->isRemote();
else
return false;
}
//==============================================================================
bool FileManager::isModified(const QString &pFileName) const
{
// Return whether the given file, if it is being managed, has been modified
File *nativeFile = file(nativeCanonicalFileName(pFileName));
if (nativeFile)
return nativeFile->isModified();
else
return false;
}
//==============================================================================
bool FileManager::isNewOrModified(const QString &pFileName) const
{
// Return whether the given file is new or modified
return isNew(pFileName) || isModified(pFileName);
}
//==============================================================================
bool FileManager::isLocalNewOrModified(const QString &pFileName) const
{
// Return whether the given file is a local one, as well as is new or
// modified
return !isRemote(pFileName) && (isNew(pFileName) || isModified(pFileName));
}
//==============================================================================
void FileManager::makeNew(const QString &pFileName)
{
// Make the given file new, should it be managed
File *nativeFile = file(nativeCanonicalFileName(pFileName));
if (nativeFile) {
QString fileName;
if (newFile(fileName))
nativeFile->makeNew(fileName);
}
}
//==============================================================================
void FileManager::setModified(const QString &pFileName, const bool &pModified)
{
// Set the modified state of the given file, should it be managed
QString nativeFileName = nativeCanonicalFileName(pFileName);
File *nativeFile = file(nativeFileName);
if (nativeFile && nativeFile->setModified(pModified))
emit fileModified(nativeFileName);
}
//==============================================================================
void FileManager::setDependenciesModified(const QString &pFileName,
const bool &pModified)
{
// Set the dependencies modified state of the given file, should it be
// managed
QString nativeFileName = nativeCanonicalFileName(pFileName);
File *nativeFile = file(nativeFileName);
if (nativeFile)
nativeFile->setDependenciesModified(pModified);
}
//==============================================================================
bool FileManager::isReadable(const QString &pFileName) const
{
// Return whether the given file, if it is being managed, is readable
File *nativeFile = file(nativeCanonicalFileName(pFileName));
if (nativeFile)
return nativeFile->isReadable();
else
return false;
}
//==============================================================================
bool FileManager::isWritable(const QString &pFileName) const
{
// Return whether the given file, if it is being managed, is writable
File *nativeFile = file(nativeCanonicalFileName(pFileName));
if (nativeFile)
return nativeFile->isWritable();
else
return false;
}
//==============================================================================
bool FileManager::isReadableAndWritable(const QString &pFileName) const
{
// Return whether the given file, if it is being managed, is readable and
// writable
return isReadable(pFileName) && isWritable(pFileName);
}
//==============================================================================
bool FileManager::isLocked(const QString &pFileName) const
{
// Return whether the given file, if it is being managed, is locked
File *nativeFile = file(nativeCanonicalFileName(pFileName));
if (nativeFile)
return nativeFile->isLocked();
else
return false;
}
//==============================================================================
FileManager::Status FileManager::setLocked(const QString &pFileName,
const bool &pLocked)
{
// Set the locked status of the given file, should it be managed
QString nativeFileName = nativeCanonicalFileName(pFileName);
File *nativeFile = file(nativeFileName);
if (nativeFile) {
File::Status status = nativeFile->setLocked(pLocked);
if (status == File::LockedSet)
emitFilePermissionsChanged(nativeFileName);
if (status == File::LockedNotNeeded)
return LockedNotNeeded;
else if (status == File::LockedSet)
return LockedSet;
else
return LockedNotSet;
} else {
return NotManaged;
}
}
//==============================================================================
QStringList FileManager::dependencies(const QString &pFileName) const
{
// Return the given file's dependencies, should it be managed
File *nativeFile = file(nativeCanonicalFileName(pFileName));
if (nativeFile)
return nativeFile->dependencies();
else
return QStringList();
}
//==============================================================================
void FileManager::setDependencies(const QString &pFileName,
const QStringList &pDependencies)
{
// Set the dependencies of the given file, should it be managed
File *nativeFile = file(nativeCanonicalFileName(pFileName));
if (nativeFile)
nativeFile->setDependencies(pDependencies);
}
//==============================================================================
void FileManager::reload(const QString &pFileName,
const bool &pForceFileChanged)
{
// Make sure that the given file is managed
QString nativeFileName = nativeCanonicalFileName(pFileName);
File *nativeFile = file(nativeFileName);
if (nativeFile) {
// The file is managed, so determine whether the file itself has been
// modified, reset its settings and let people know that it should be
// reloaded
File::Status nativeFileStatus = nativeFile->check();
nativeFile->reset();
emit fileReloaded(nativeFileName,
pForceFileChanged
|| (nativeFileStatus == File::Changed) || (nativeFileStatus == File::AllChanged));
}
}
//==============================================================================
bool FileManager::newFile(QString &pFileName, const QByteArray &pContents)
{
// Retrieve a temporary file name for our new file
QString fileName = temporaryFileName();
// Create a new file with the given contents
if (writeFileContentsToFile(fileName, pContents)) {
pFileName = fileName;
return true;
} else {
pFileName = QString();
return false;
}
}
//==============================================================================
FileManager::Status FileManager::create(const QString &pUrl,
const QByteArray &pContents)
{
// Create a new file
QString fileName;
if (newFile(fileName, pContents)) {
// Let people know that we have created a new file
emit fileCreated(fileName, pUrl);
return Created;
} else {
return NotCreated;
}
}
//==============================================================================
FileManager::Status FileManager::create(const QString &pUrl,
const QString &pContents)
{
// Create a new file
return create(pUrl, pContents.toUtf8());
}
//==============================================================================
FileManager::Status FileManager::rename(const QString &pOldFileName,
const QString &pNewFileName)
{
// Make sure that the given 'old' file is managed
File *nativeFile = file(pOldFileName);
if (nativeFile) {
// The 'old' file is managed, so rename it and let people know about it
QString newNativeFileName = nativeCanonicalFileName(pNewFileName);
if (nativeFile->setFileName(newNativeFileName)) {
QString oldNativeFileName = nativeCanonicalFileName(pOldFileName);
mFiles.insert(newNativeFileName, nativeFile);
mFiles.remove(oldNativeFileName);
emit fileRenamed(oldNativeFileName, newNativeFileName);
return Renamed;
} else {
return RenamingNotNeeded;
}
} else {
return NotManaged;
}
}
//==============================================================================
FileManager::Status FileManager::duplicate(const QString &pFileName)
{
// Make sure that the given file is managed
File *nativeFile = file(nativeCanonicalFileName(pFileName));
if (nativeFile) {
// The file is managed, so retrieve its contents
QByteArray fileContents;
if (readFileContentsFromFile(pFileName, fileContents)) {
// Now, we can create a new file, which contents will be that of our
// given file
QString fileName;
if (newFile(fileName, fileContents)) {
// Let people know that we have duplicated a file
emit fileDuplicated(fileName);
return Duplicated;
} else {
return NotDuplicated;
}
} else {
return NotDuplicated;
}
} else {
return NotManaged;
}
}
//==============================================================================
void FileManager::save(const QString &pFileName)
{
// Make sure that the given file is managed
QString nativeFileName = nativeCanonicalFileName(pFileName);
File *nativeFile = file(nativeFileName);
if (nativeFile) {
// The file is managed, so reset its settings and let people know that
// it has been saved
nativeFile->reset(false);
emit fileSaved(nativeFileName);
}
}
//==============================================================================
int FileManager::count() const
{
// Return the number of files currently being managed
return mFiles.count();
}
//==============================================================================
void FileManager::emitFilePermissionsChanged(const QString &pFileName)
{
// Update our internals and let people know that the given file has had its
// permissions changed
mFilesReadable.insert(pFileName, isReadable(pFileName));
mFilesWritable.insert(pFileName, isWritable(pFileName));
emit filePermissionsChanged(pFileName);
}
//==============================================================================
void FileManager::setCheckFilesEnabled(const bool &pCheckFilesEnabled)
{
// Specify whether we can check files
mCheckFilesEnabled = pCheckFilesEnabled;
}
//==============================================================================
void FileManager::focusWindowChanged()
{
// Start/stop our timer
startStopTimer();
}
//==============================================================================
void FileManager::checkFiles()
{
// Make sure that OpenCOR is active
// Note: indeed, although we try our best to enable/disable our timer as
// needed, there are cases (e.g. a QFileDialog is opened) that don't
// result in the focusWindowChanged() signal being emitted, which
// means that we can't enable/disable our timer in those acses, hence
// our checking that OpenCOR is really active indeed...
if (!Core::opencorActive() || !mCheckFilesEnabled)
return;
// Check our various files, as well as their locked status, but only if they
// are not being ignored
foreach (File *file, mFiles) {
QString fileName = file->fileName();
File::Status fileStatus = file->check();
switch (fileStatus) {
case File::Changed:
case File::DependenciesChanged:
case File::AllChanged:
// The file and/or one or several of its dependencies has changed,
// so let people know about it
emit fileChanged(fileName,
(fileStatus == File::Changed) || (fileStatus == File::AllChanged),
(fileStatus == File::DependenciesChanged) || (fileStatus == File::AllChanged));
break;
case File::Unchanged:
// The file has neither changed nor been deleted, so check whether
// its permissions have changed
if ( (mFilesReadable.value(fileName, false) != isReadable(fileName))
|| (mFilesWritable.value(fileName, false) != isWritable(fileName))
|| !( mFilesReadable.contains(fileName)
&& mFilesWritable.contains(fileName))) {
emitFilePermissionsChanged(fileName);
}
break;
case File::Deleted:
// The file has been deleted, so let people know about it
emit fileDeleted(fileName);
break;
default:
// Not a relevant status, so do nothing
;
}
}
}
//==============================================================================
} // namespace Core
} // namespace OpenCOR
//==============================================================================
// End of file
//==============================================================================