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
|
/*
Copyright (c) 2007 Till Adam <adam@kde.org>
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This library 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 Library General Public
License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
#include "maildir.h"
#include <QDateTime>
#include <QDir>
#include <QDirIterator>
#include <QFileInfo>
#include <QHostInfo>
#include <QUuid>
#include <QLoggingCategory>
Q_LOGGING_CATEGORY(log, "maildir");
#include <time.h>
#include <unistd.h>
bool removeDirAndContentsRecursively(const QString & path)
{
bool success = true;
QDir d;
d.setPath(path);
d.setFilter(QDir::Files | QDir::Dirs | QDir::Hidden | QDir::NoSymLinks);
QFileInfoList list = d.entryInfoList();
Q_FOREACH (const QFileInfo &fi, list) {
if (fi.isDir()) {
if (fi.fileName() != QLatin1String(".") && fi.fileName() != QLatin1String("..")) {
success = success && removeDirAndContentsRecursively(fi.absoluteFilePath());
}
} else {
success = success && d.remove(fi.absoluteFilePath());
}
}
if (success) {
success = success && d.rmdir(path); // nuke ourselves, we should be empty now
}
return success;
}
using namespace KPIM;
Q_GLOBAL_STATIC_WITH_ARGS(QRegExp, statusSeparatorRx, (":|!"))
class Maildir::Private
{
public:
Private(const QString& p, bool isRoot)
:path(p), isRoot(isRoot)
{
hostName = QHostInfo::localHostName();
}
Private(const Private& rhs)
{
path = rhs.path;
isRoot = rhs.isRoot;
hostName = rhs.hostName;
}
bool operator==(const Private& rhs) const
{
return path == rhs.path;
}
bool accessIsPossible(bool createMissingFolders = true);
bool canAccess(const QString& path) const;
QStringList subPaths() const
{
QStringList paths;
paths << path + QString::fromLatin1("/cur");
paths << path + QString::fromLatin1("/new");
paths << path + QString::fromLatin1("/tmp");
return paths;
}
QStringList listNew() const
{
QDir d(path + QString::fromLatin1("/new"));
d.setSorting(QDir::NoSort);
return d.entryList(QDir::Files);
}
QStringList listCurrent() const
{
QDir d(path + QString::fromLatin1("/cur"));
d.setSorting(QDir::NoSort);
return d.entryList(QDir::Files);
}
QString findRealKey(const QString& key) const
{
if (key.isEmpty()) {
qCWarning(log) << "Empty key: " << key;
return key;
}
if (QFile::exists(path + QString::fromLatin1("/cur/") + key)) {
return path + QString::fromLatin1("/cur/") + key;
}
if (QFile::exists(path + QString::fromLatin1("/new/") + key)) {
return path + QString::fromLatin1("/new/") + key;
}
return QString();
}
static QString stripFlags(const QString& key)
{
const QRegExp rx = *(statusSeparatorRx());
const int index = key.indexOf(rx);
return key.mid(0, index);
}
static QString subDirNameForFolderName(const QString &folderName)
{
return QString::fromLatin1(".%1.directory").arg(folderName);
}
QString subDirPath() const
{
QDir dir(path);
return subDirNameForFolderName(dir.dirName());
}
bool moveAndRename(QDir &dest, const QString &newName)
{
if (!dest.exists()) {
qCDebug(log) << "Destination does not exist";
return false;
}
if (dest.exists(newName) || dest.exists(subDirNameForFolderName(newName))) {
qCDebug(log) << "New name already in use";
return false;
}
if (!dest.rename(path, newName)) {
qCDebug(log) << "Failed to rename maildir";
return false;
}
const QDir subDirs(Maildir::subDirPathForFolderPath(path));
if (subDirs.exists() && !dest.rename(subDirs.path(), subDirNameForFolderName(newName))) {
qCDebug(log) << "Failed to rename subfolders";
return false;
}
path = dest.path() + QDir::separator() + newName;
return true;
}
QString path;
bool isRoot;
QString hostName;
};
Maildir::Maildir(const QString& path, bool isRoot)
:d(new Private(path, isRoot))
{
}
void Maildir::swap(const Maildir &rhs)
{
Private *p = d;
d = new Private(*rhs.d);
delete p;
}
Maildir::Maildir(const Maildir & rhs)
:d(new Private(*rhs.d))
{
}
Maildir& Maildir::operator= (const Maildir & rhs)
{
// copy and swap, exception safe, and handles assignment to self
Maildir temp(rhs);
swap(temp);
return *this;
}
bool Maildir::operator== (const Maildir & rhs) const
{
return *d == *rhs.d;
}
Maildir::~Maildir()
{
delete d;
}
bool Maildir::Private::canAccess(const QString& path) const
{
QFileInfo d(path);
return d.isReadable() && d.isWritable();
}
bool Maildir::Private::accessIsPossible(bool createMissingFolders)
{
QStringList paths = subPaths();
paths.prepend(path);
Q_FOREACH (const QString &p, paths) {
if (!QFile::exists(p)) {
if (!createMissingFolders) {
qCWarning(log) << QString("Error opening %1; this folder is missing.").arg(p);
return false;
}
QDir().mkpath(p);
if (!QFile::exists(p)) {
qCWarning(log) << QString("Error opening %1; this folder is missing.").arg(p);
return false;
}
}
if (!canAccess(p)) {
qCWarning(log) << QString("Error opening %1; either this is not a valid maildir folder, or you do not have sufficient access permissions.").arg(p);
return false;
}
}
return true;
}
bool Maildir::isValid(bool createMissingFolders) const
{
if (path().isEmpty()) {
return false;
}
if (!d->isRoot) {
if (d->accessIsPossible(createMissingFolders)) {
return true;
}
} else {
Q_FOREACH (const QString &sf, subFolderList()) {
const Maildir subMd = Maildir(path() + QLatin1Char('/') + sf);
if (!subMd.isValid()) {
return false;
}
}
return true;
}
return false;
}
bool Maildir::isRoot() const
{
return d->isRoot;
}
bool Maildir::create()
{
// FIXME: in a failure case, this will leave partially created dirs around
// we should clean them up, but only if they didn't previously existed...
Q_FOREACH (const QString &p, d->subPaths()) {
QDir dir(p);
if (!dir.exists(p)) {
if (!dir.mkpath(p))
return false;
}
}
return true;
}
bool Maildir::remove()
{
QDir dir(d->path);
dir.removeRecursively();
return true;
}
QString Maildir::path() const
{
return d->path;
}
QString Maildir::name() const
{
const QDir dir(d->path);
return dir.dirName();
}
QString Maildir::addSubFolder(const QString& path)
{
if (!isValid())
return QString();
// make the subdir dir
QDir dir(d->path);
if (!d->isRoot) {
dir.cdUp();
if (!dir.exists(d->subDirPath()))
dir.mkdir(d->subDirPath());
dir.cd(d->subDirPath());
}
const QString fullPath = dir.path() + QLatin1Char('/') + path;
Maildir subdir(fullPath);
if (subdir.create())
return fullPath;
return QString();
}
bool Maildir::removeSubFolder(const QString& folderName)
{
if (!isValid()) return false;
QDir dir(d->path);
if (!d->isRoot) {
dir.cdUp();
if (!dir.exists(d->subDirPath())) return false;
dir.cd(d->subDirPath());
}
if (!dir.exists(folderName)) return false;
// remove it recursively
bool result = removeDirAndContentsRecursively(dir.absolutePath() + QLatin1Char('/') + folderName);
QString subfolderName = subDirNameForFolderName(folderName);
if (dir.exists(subfolderName))
result &= removeDirAndContentsRecursively(dir.absolutePath() + QLatin1Char('/') + subfolderName);
return result;
}
Maildir Maildir::subFolder(const QString& subFolder) const
{
// make the subdir dir
QDir dir(d->path);
if (!d->isRoot) {
dir.cdUp();
if (dir.exists(d->subDirPath())) {
dir.cd(d->subDirPath());
}
}
return Maildir(dir.path() + QLatin1Char('/') + subFolder);
}
Maildir Maildir::parent() const
{
if (!isValid() || d->isRoot)
return Maildir();
QDir dir(d->path);
dir.cdUp();
//FIXME Figure out how this is acutally supposed to work
//There seem to be a bunch of conflicting standards, and nesting folders is apparently not what we're supposed to be doing,
//but it works for the time being.
// if (!dir.dirName().startsWith(QLatin1Char('.')) || !dir.dirName().endsWith(QLatin1String(".directory")))
// return Maildir();
// const QString parentName = dir.dirName().mid(1, dir.dirName().size() - 11);
// dir.cdUp();
// dir.cd(parentName);
return Maildir (dir.path());
}
QStringList Maildir::entryList() const
{
QStringList result;
if (isValid()) {
result += d->listNew();
result += d->listCurrent();
}
// qCDebug(log) <<"Maildir::entryList()" << result;
return result;
}
QStringList Maildir::listCurrent() const
{
QStringList result;
if (isValid()) {
result += d->listCurrent();
}
return result;
}
QString Maildir::findRealKey(const QString& key) const
{
return d->findRealKey(key);
}
QStringList Maildir::listNew() const
{
QStringList result;
if (isValid()) {
result += d->listNew();
}
return result;
}
QString Maildir::pathToNew() const
{
if (isValid()) {
return d->path + QString::fromLatin1("/new");
}
return QString();
}
QString Maildir::pathToCurrent() const
{
if (isValid()) {
return d->path + QString::fromLatin1("/cur");
}
return QString();
}
QString Maildir::subDirPath() const
{
QDir dir(d->path);
dir.cdUp();
return dir.path() + QDir::separator() + d->subDirPath();
}
QStringList Maildir::subFolderList() const
{
QDir dir(d->path);
// the root maildir has its subfolders directly beneath it
if (!d->isRoot) {
dir.cdUp();
if (!dir.exists(d->subDirPath()))
return QStringList();
dir.cd(d->subDirPath());
}
dir.setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
QStringList entries = dir.entryList();
entries.removeAll(QLatin1String("cur"));
entries.removeAll(QLatin1String("new"));
entries.removeAll(QLatin1String("tmp"));
return entries;
}
QByteArray Maildir::readEntry(const QString& key) const
{
QByteArray result;
QString realKey(d->findRealKey(key));
if (realKey.isEmpty()) {
qCWarning(log) << "Maildir::readEntry unable to find: " << key;
return result;
}
QFile f(realKey);
if (!f.open(QIODevice::ReadOnly)) {
qCWarning(log) << QString("Cannot open mail file %1.").arg(realKey);
return result;
}
result = f.readAll();
return result;
}
qint64 Maildir::size(const QString& key) const
{
QString realKey(d->findRealKey(key));
if (realKey.isEmpty()) {
qCWarning(log) << "Maildir::size unable to find: " << key;
return -1;
}
QFileInfo info(realKey);
if (!info.exists()) {
qCWarning(log) << "Cannot open mail file:" << realKey;
return -1;
}
return info.size();
}
QDateTime Maildir::lastModified(const QString& key) const
{
const QString realKey(d->findRealKey(key));
if (realKey.isEmpty()) {
qCWarning(log) << "Maildir::lastModified unable to find: " << key;
return QDateTime();
}
const QFileInfo info(realKey);
if (!info.exists())
return QDateTime();
return info.lastModified();
}
void Maildir::importNewMails()
{
QDirIterator entryIterator(pathToNew(), QDir::Files);
while (entryIterator.hasNext()) {
const QString filePath = QDir::fromNativeSeparators(entryIterator.next());
QFile file(filePath);
if (!file.rename(pathToCurrent() +"/" + entryIterator.fileName())) {
qCWarning(log) << "Failed to rename the file: " << file.errorString();
}
}
}
QString Maildir::getKeyFromFile(const QString& file)
{
return Maildir::Private::stripFlags(file.split('/').last());
}
QString Maildir::getDirectoryFromFile( const QString& file )
{
auto parts = file.split('/');
Q_ASSERT(parts.size() >= 2);
parts.removeLast(); //File
parts.removeLast(); //cur/new/tmp
return parts.join('/') + "/";
}
QByteArray Maildir::readEntryHeadersFromFile(const QString& file)
{
QByteArray result;
QFile f(file);
if (!f.open(QIODevice::ReadOnly)) {
qCWarning(log) << "Maildir::readEntryHeaders unable to find: " << file;
return result;
}
f.map(0, qMin((qint64)8000, f.size()));
forever {
QByteArray line = f.readLine();
if (line.isEmpty() || line.startsWith('\n'))
break;
result.append(line);
}
return result;
}
QByteArray Maildir::readEntryHeaders(const QString& key) const
{
const QString realKey(d->findRealKey(key));
if (realKey.isEmpty()) {
qCWarning(log) << "Maildir::readEntryHeaders unable to find: " << key;
return QByteArray();
}
return readEntryHeadersFromFile(realKey);
}
static QString createUniqueFileName()
{
qint64 time = QDateTime::currentMSecsSinceEpoch() / 1000;
int r = qrand() % 1000;
QString identifier = QLatin1String("R") + QString::number(r);
QString fileName = QString::number(time) + QLatin1Char('.') + identifier + QLatin1Char('.');
return fileName;
}
bool Maildir::writeEntry(const QString& key, const QByteArray& data)
{
QString realKey(d->findRealKey(key));
if (realKey.isEmpty()) {
qCWarning(log) << "Maildir::writeEntry unable to find: " << key;
return false;
}
QFile f(realKey);
bool result = f.open(QIODevice::WriteOnly);
result = result & (f.write(data) != -1);
f.close();
if (!result) {
qCWarning(log) << "Cannot write to mail file %1." << realKey;
return false;
}
return true;
}
QString Maildir::addEntry(const QByteArray& data)
{
QString uniqueKey;
QString key;
QString finalKey;
QString curKey;
// QUuid doesn't return globally unique identifiers, therefor we query until we
// get one that doesn't exists yet
do {
uniqueKey = createUniqueFileName() + d->hostName;
key = d->path + QLatin1String("/tmp/") + uniqueKey;
finalKey = d->path + QLatin1String("/cur/") + uniqueKey;
curKey = d->path + QLatin1String("/cur/") + uniqueKey;
} while (QFile::exists(key) || QFile::exists(finalKey) || QFile::exists(curKey));
QFile f(key);
bool result = f.open(QIODevice::WriteOnly);
if (!result) {
qCWarning(log) << f.errorString();
qCWarning(log) << "Cannot write to mail file: " << key;
}
result = result & (f.write(data) != -1);
f.close();
if (!result) {
qCWarning(log) << "Cannot write to mail file: " << key;
return QString();
}
/*
* FIXME:
*
* The whole point of the locking free maildir idea is that the moves between
* the internal directories are atomic. Afaik QFile::rename does not guarantee
* that, so this will need to be done properly. - ta
*
* For reference: http://trolltech.com/developer/task-tracker/index_html?method=entry&id=211215
*/
qCDebug(log) << "New entry: " << finalKey;
if (!f.rename(finalKey)) {
qCWarning(log) << "Maildir: Failed to add entry: " << finalKey << "! Error: " << f.errorString();
return QString();
}
return uniqueKey;
}
QString Maildir::addEntryFromPath(const QString& path)
{
QString uniqueKey;
QString key;
QString finalKey;
QString curKey;
// QUuid doesn't return globally unique identifiers, therefor we query until we
// get one that doesn't exists yet
do {
uniqueKey = createUniqueFileName() + d->hostName;
key = d->path + QLatin1String("/tmp/") + uniqueKey;
finalKey = d->path + QLatin1String("/new/") + uniqueKey;
curKey = d->path + QLatin1String("/cur/") + uniqueKey;
} while (QFile::exists(key) || QFile::exists(finalKey) || QFile::exists(curKey));
QFile f(path);
if (!f.open(QIODevice::ReadWrite)) {
qCWarning(log) << f.errorString();
qCWarning(log) << "Cannot open mail file: " << key;
return QString();
}
if (!f.rename(curKey)) {
qCWarning(log) << "Maildir: Failed to add entry: " << curKey << "! Error: " << f.errorString();
return QString();
}
return uniqueKey;
}
bool Maildir::removeEntry(const QString& key)
{
QString realKey(d->findRealKey(key));
if (realKey.isEmpty()) {
qCWarning(log) << "Maildir::removeEntry unable to find: " << key;
return false;
}
QFile file(realKey);
if (!file.remove()) {
qCWarning(log) << file.errorString() << file.error();
return false;
}
return true;
}
QString Maildir::changeEntryFlags(const QString& key, const Maildir::Flags& flags)
{
QString realKey(d->findRealKey(key));
qCWarning(log) << "Change entry flags: " << key << realKey;
if (realKey.isEmpty()) {
qCWarning(log) << "Maildir::changeEntryFlags unable to find: " << key << "in " << d->path;
return QString();
}
const QRegExp rx = *(statusSeparatorRx());
QString finalKey = key.left(key.indexOf(rx));
QStringList mailDirFlags;
if (flags & Forwarded)
mailDirFlags << QLatin1String("P");
if (flags & Replied)
mailDirFlags << QLatin1String("R");
if (flags & Seen)
mailDirFlags << QLatin1String("S");
if (flags & Deleted)
mailDirFlags << QLatin1String("T");
if (flags & Flagged)
mailDirFlags << QLatin1String("F");
mailDirFlags.sort();
if (!mailDirFlags.isEmpty()) {
#ifdef Q_OS_WIN
finalKey.append(QLatin1String("!2,") + mailDirFlags.join(QString()));
#else
finalKey.append(QLatin1String(":2,") + mailDirFlags.join(QString()));
#endif
}
QString newUniqueKey = finalKey; //key without path
finalKey.prepend(d->path + QString::fromLatin1("/cur/"));
if (realKey == finalKey) {
// Somehow it already is named this way (e.g. migration bug -> wrong status in sink)
qCWarning(log) << "File already named that way: " << newUniqueKey << finalKey;
return newUniqueKey;
}
QFile f(realKey);
if (QFile::exists(finalKey)) {
QFile destFile(finalKey);
QByteArray destContent;
if (destFile.open(QIODevice::ReadOnly)) {
destContent = destFile.readAll();
destFile.close();
}
QByteArray sourceContent;
if (f.open(QIODevice::ReadOnly)) {
sourceContent = f.readAll();
f.close();
}
if (destContent != sourceContent) {
QString newFinalKey = QLatin1String("1-") + newUniqueKey;
int i = 1;
while (QFile::exists(d->path + QString::fromLatin1("/cur/") + newFinalKey)) {
i++;
newFinalKey = QString::number(i) + QLatin1Char('-') + newUniqueKey;
}
finalKey = d->path + QString::fromLatin1("/cur/") + newFinalKey;
} else {
QFile::remove(finalKey); //they are the same
}
}
if (!f.rename(finalKey)) {
qCWarning(log) << "Maildir: Failed to rename entry from: " << f.fileName() << " to " << finalKey << "! Error: " << f.errorString();
return QString();
}
qCDebug(log) << "Renamed file from: " << f.fileName() << " to " << finalKey;
return newUniqueKey;
}
Maildir::Flags Maildir::readEntryFlags(const QString& key)
{
Flags flags;
const QRegExp rx = *(statusSeparatorRx());
const int index = key.indexOf(rx);
if (index != -1) {
const QString mailDirFlags = key.mid(index + 3); // after "(:|!)2,"
const int flagSize(mailDirFlags.size());
for (int i = 0; i < flagSize; ++i) {
if (mailDirFlags[i] == QLatin1Char('P'))
flags |= Forwarded;
else if (mailDirFlags[i] == QLatin1Char('R'))
flags |= Replied;
else if (mailDirFlags[i] == QLatin1Char('S'))
flags |= Seen;
else if (mailDirFlags[i] == QLatin1Char('F'))
flags |= Flagged;
}
}
return flags;
}
bool Maildir::moveTo(const Maildir &newParent)
{
if (d->isRoot)
return false; // not supported
QDir newDir(newParent.path());
if (!newParent.d->isRoot) {
newDir.cdUp();
if (!newDir.exists(newParent.d->subDirPath()))
newDir.mkdir(newParent.d->subDirPath());
newDir.cd(newParent.d->subDirPath());
}
QDir currentDir(d->path);
currentDir.cdUp();
if (newDir == currentDir)
return true;
return d->moveAndRename(newDir, name());
}
bool Maildir::rename(const QString &newName)
{
if (name() == newName)
return true;
if (d->isRoot)
return false; // not (yet) supported
QDir dir(d->path);
dir.cdUp();
return d->moveAndRename(dir, newName);
}
QString Maildir::moveEntryTo(const QString &key, const Maildir &destination)
{
const QString realKey(d->findRealKey(key));
if (realKey.isEmpty()) {
qCWarning(log) << "Unable to find: " << key;
return QString();
}
QFile f(realKey);
// ### is this safe regarding the maildir locking scheme?
const QString targetKey = destination.path() + QDir::separator() + QLatin1String("cur") + QDir::separator() + key;
if (!f.rename(targetKey)) {
qCWarning(log) << "Failed to rename" << realKey << "to" << targetKey << "! Error: " << f.errorString();;
return QString();
}
return key;
}
QString Maildir::subDirPathForFolderPath(const QString &folderPath)
{
QDir dir(folderPath);
const QString dirName = dir.dirName();
dir.cdUp();
return QFileInfo(dir, Private::subDirNameForFolderName(dirName)).filePath();
}
QString Maildir::subDirNameForFolderName(const QString &folderName)
{
return Private::subDirNameForFolderName(folderName);
}
|