summaryrefslogtreecommitdiffstats
path: root/examples/maildirresource/libmaildir/maildir.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'examples/maildirresource/libmaildir/maildir.cpp')
-rw-r--r--examples/maildirresource/libmaildir/maildir.cpp144
1 files changed, 13 insertions, 131 deletions
diff --git a/examples/maildirresource/libmaildir/maildir.cpp b/examples/maildirresource/libmaildir/maildir.cpp
index de704f2..c47e679 100644
--- a/examples/maildirresource/libmaildir/maildir.cpp
+++ b/examples/maildirresource/libmaildir/maildir.cpp
@@ -18,7 +18,6 @@
18*/ 18*/
19 19
20#include "maildir.h" 20#include "maildir.h"
21// #include "keycache.h"
22 21
23#include <QDateTime> 22#include <QDateTime>
24#include <QDir> 23#include <QDir>
@@ -30,31 +29,6 @@
30#include <time.h> 29#include <time.h>
31#include <unistd.h> 30#include <unistd.h>
32 31
33//Define it to get more debug output to expense of operating speed
34// #define DEBUG_KEYCACHE_CONSITENCY
35
36
37// static void initRandomSeed()
38// {
39// static bool init = false;
40// if (!init) {
41// unsigned int seed;
42// init = true;
43// int fd = KDE_open("/dev/urandom", O_RDONLY);
44// if (fd < 0 || ::read(fd, &seed, sizeof(seed)) != sizeof(seed)) {
45// // No /dev/urandom... try something else.
46// srand(getpid());
47// seed = rand() + time(0);
48// }
49//
50// if (fd >= 0)
51// close(fd);
52//
53// qsrand(seed);
54// }
55// }
56
57
58bool removeDirAndContentsRecursively(const QString & path) 32bool removeDirAndContentsRecursively(const QString & path)
59{ 33{
60 bool success = true; 34 bool success = true;
@@ -92,14 +66,6 @@ public:
92 :path(p), isRoot(isRoot) 66 :path(p), isRoot(isRoot)
93 { 67 {
94 hostName = QHostInfo::localHostName(); 68 hostName = QHostInfo::localHostName();
95 // The default implementation of QUuid::createUuid() doesn't use
96 // a seed that is random enough. Therefor we use our own initialization
97 // until this issue will be fixed in Qt 4.7.
98 // initRandomSeed();
99
100 //Cache object is created the first time this runs.
101 //It will live throughout the lifetime of the application
102 // KeyCache::self()->addKeys(path);
103 } 69 }
104 70
105 Private(const Private& rhs) 71 Private(const Private& rhs)
@@ -145,41 +111,14 @@ public:
145 qWarning() << "Empty key: " << key; 111 qWarning() << "Empty key: " << key;
146 return key; 112 return key;
147 } 113 }
148 // KeyCache* keyCache = KeyCache::self();
149 // if (keyCache->isNewKey(path, key)) {
150 if (QFile::exists(path + QString::fromLatin1("/new/") + key)) {
151#ifdef DEBUG_KEYCACHE_CONSITENCY
152 if (!QFile::exists(path + QString::fromLatin1("/new/") + key)) {
153 qDebug() << "WARNING: key is in cache, but the file is gone: " << path + QString::fromLatin1("/new/") + key;
154 }
155#endif
156 return path + QString::fromLatin1("/new/") + key;
157 }
158 // if (keyCache->isCurKey(path, key)) {
159 if (QFile::exists(path + QString::fromLatin1("/cur/") + key)) { 114 if (QFile::exists(path + QString::fromLatin1("/cur/") + key)) {
160#ifdef DEBUG_KEYCACHE_CONSITENCY
161 if (!QFile::exists(path + QString::fromLatin1("/cur/") + key)) {
162 qDebug() << "WARNING: key is in cache, but the file is gone: " << path + QString::fromLatin1("/cur/") + key;
163 }
164#endif
165 return path + QString::fromLatin1("/cur/") + key; 115 return path + QString::fromLatin1("/cur/") + key;
166 } 116 }
167 QString realKey = path + QString::fromLatin1("/new/") + key; 117 if (QFile::exists(path + QString::fromLatin1("/new/") + key)) {
168 118 return path + QString::fromLatin1("/new/") + key;
169 // QFile f(realKey); 119 }
170 // if (f.exists()) {
171 // keyCache->addNewKey(path, key);
172 // } else { //not in "new", search in "cur"
173 // realKey = path + QString::fromLatin1("/cur/") + key;
174 // QFile f2(realKey);
175 // if (f2.exists()) {
176 // keyCache->addCurKey(path, key);
177 // } else {
178 // realKey.clear(); //not in "cur" either
179 // }
180 // }
181 120
182 return realKey; 121 return QString();
183 } 122 }
184 123
185 static QString stripFlags(const QString& key) 124 static QString stripFlags(const QString& key)
@@ -228,7 +167,6 @@ public:
228 QString path; 167 QString path;
229 bool isRoot; 168 bool isRoot;
230 QString hostName; 169 QString hostName;
231 QString lastError;
232}; 170};
233 171
234Maildir::Maildir(const QString& path, bool isRoot) 172Maildir::Maildir(const QString& path, bool isRoot)
@@ -272,8 +210,6 @@ Maildir::~Maildir()
272 210
273bool Maildir::Private::canAccess(const QString& path) const 211bool Maildir::Private::canAccess(const QString& path) const
274{ 212{
275 //return access(QFile::encodeName(path), R_OK | W_OK | X_OK) != 0;
276 // FIXME X_OK?
277 QFileInfo d(path); 213 QFileInfo d(path);
278 return d.isReadable() && d.isWritable(); 214 return d.isReadable() && d.isWritable();
279} 215}
@@ -287,18 +223,17 @@ bool Maildir::Private::accessIsPossible(bool createMissingFolders)
287 Q_FOREACH (const QString &p, paths) { 223 Q_FOREACH (const QString &p, paths) {
288 if (!QFile::exists(p)) { 224 if (!QFile::exists(p)) {
289 if (!createMissingFolders) { 225 if (!createMissingFolders) {
290 // lastError = i18n("Error opening %1; this folder is missing.", p); 226 qWarning() << QString("Error opening %1; this folder is missing.").arg(p);
291 return false; 227 return false;
292 } 228 }
293 QDir().mkpath(p); 229 QDir().mkpath(p);
294 if (!QFile::exists(p)) { 230 if (!QFile::exists(p)) {
295 // lastError = i18n("Error opening %1; this folder is missing.", p); 231 qWarning() << QString("Error opening %1; this folder is missing.").arg(p);
296 return false; 232 return false;
297 } 233 }
298 } 234 }
299 if (!canAccess(p)) { 235 if (!canAccess(p)) {
300 // lastError = i18n("Error opening %1; either this is not a valid " 236 qWarning() << QString("Error opening %1; either this is not a valid maildir folder, or you do not have sufficient access permissions.").arg(p);
301 // "maildir folder, or you do not have sufficient access permissions." ,p);
302 return false; 237 return false;
303 } 238 }
304 } 239 }
@@ -318,7 +253,6 @@ bool Maildir::isValid(bool createMissingFolders) const
318 Q_FOREACH (const QString &sf, subFolderList()) { 253 Q_FOREACH (const QString &sf, subFolderList()) {
319 const Maildir subMd = Maildir(path() + QLatin1Char('/') + sf); 254 const Maildir subMd = Maildir(path() + QLatin1Char('/') + sf);
320 if (!subMd.isValid()) { 255 if (!subMd.isValid()) {
321 d->lastError = subMd.lastError();
322 return false; 256 return false;
323 } 257 }
324 } 258 }
@@ -519,19 +453,16 @@ QByteArray Maildir::readEntry(const QString& key) const
519 453
520 QString realKey(d->findRealKey(key)); 454 QString realKey(d->findRealKey(key));
521 if (realKey.isEmpty()) { 455 if (realKey.isEmpty()) {
522 // FIXME error handling?
523 qWarning() << "Maildir::readEntry unable to find: " << key; 456 qWarning() << "Maildir::readEntry unable to find: " << key;
524 // d->lastError = i18n("Cannot locate mail file %1." ,key);
525 return result; 457 return result;
526 } 458 }
527 459
528 QFile f(realKey); 460 QFile f(realKey);
529 if (!f.open(QIODevice::ReadOnly)) { 461 if (!f.open(QIODevice::ReadOnly)) {
530 // d->lastError = i18n("Cannot open mail file %1.", realKey); 462 qWarning() << QString("Cannot open mail file %1.").arg(realKey);
531 return result; 463 return result;
532 } 464 }
533 465
534 // FIXME be safer than this
535 result = f.readAll(); 466 result = f.readAll();
536 467
537 return result; 468 return result;
@@ -540,15 +471,13 @@ qint64 Maildir::size(const QString& key) const
540{ 471{
541 QString realKey(d->findRealKey(key)); 472 QString realKey(d->findRealKey(key));
542 if (realKey.isEmpty()) { 473 if (realKey.isEmpty()) {
543 // FIXME error handling?
544 qWarning() << "Maildir::size unable to find: " << key; 474 qWarning() << "Maildir::size unable to find: " << key;
545 // d->lastError = i18n("Cannot locate mail file %1." , key);
546 return -1; 475 return -1;
547 } 476 }
548 477
549 QFileInfo info(realKey); 478 QFileInfo info(realKey);
550 if (!info.exists()) { 479 if (!info.exists()) {
551 // d->lastError = i18n("Cannot open mail file %1." ,realKey); 480 qWarning() << "Cannot open mail file:" << realKey;
552 return -1; 481 return -1;
553 } 482 }
554 483
@@ -560,7 +489,6 @@ QDateTime Maildir::lastModified(const QString& key) const
560 const QString realKey(d->findRealKey(key)); 489 const QString realKey(d->findRealKey(key));
561 if (realKey.isEmpty()) { 490 if (realKey.isEmpty()) {
562 qWarning() << "Maildir::lastModified unable to find: " << key; 491 qWarning() << "Maildir::lastModified unable to find: " << key;
563 // d->lastError = i18n("Cannot locate mail file %1." , key);
564 return QDateTime(); 492 return QDateTime();
565 } 493 }
566 494
@@ -603,9 +531,7 @@ QByteArray Maildir::readEntryHeadersFromFile(const QString& file)
603 531
604 QFile f(file); 532 QFile f(file);
605 if (!f.open(QIODevice::ReadOnly)) { 533 if (!f.open(QIODevice::ReadOnly)) {
606 // FIXME error handling?
607 qWarning() << "Maildir::readEntryHeaders unable to find: " << file; 534 qWarning() << "Maildir::readEntryHeaders unable to find: " << file;
608 // d->lastError = i18n("Cannot locate mail file %1." , file);
609 return result; 535 return result;
610 } 536 }
611 f.map(0, qMin((qint64)8000, f.size())); 537 f.map(0, qMin((qint64)8000, f.size()));
@@ -623,7 +549,6 @@ QByteArray Maildir::readEntryHeaders(const QString& key) const
623 const QString realKey(d->findRealKey(key)); 549 const QString realKey(d->findRealKey(key));
624 if (realKey.isEmpty()) { 550 if (realKey.isEmpty()) {
625 qWarning() << "Maildir::readEntryHeaders unable to find: " << key; 551 qWarning() << "Maildir::readEntryHeaders unable to find: " << key;
626 // d->lastError = i18n("Cannot locate mail file %1." , key);
627 return QByteArray(); 552 return QByteArray();
628 } 553 }
629 554
@@ -646,9 +571,7 @@ bool Maildir::writeEntry(const QString& key, const QByteArray& data)
646{ 571{
647 QString realKey(d->findRealKey(key)); 572 QString realKey(d->findRealKey(key));
648 if (realKey.isEmpty()) { 573 if (realKey.isEmpty()) {
649 // FIXME error handling?
650 qWarning() << "Maildir::writeEntry unable to find: " << key; 574 qWarning() << "Maildir::writeEntry unable to find: " << key;
651 // d->lastError = i18n("Cannot locate mail file %1." ,key);
652 return false; 575 return false;
653 } 576 }
654 QFile f(realKey); 577 QFile f(realKey);
@@ -656,7 +579,7 @@ bool Maildir::writeEntry(const QString& key, const QByteArray& data)
656 result = result & (f.write(data) != -1); 579 result = result & (f.write(data) != -1);
657 f.close(); 580 f.close();
658 if (!result) { 581 if (!result) {
659 // d->lastError = i18n("Cannot write to mail file %1." ,realKey); 582 qWarning() << "Cannot write to mail file %1." << realKey;
660 return false; 583 return false;
661 } 584 }
662 return true; 585 return true;
@@ -688,7 +611,6 @@ QString Maildir::addEntry(const QByteArray& data)
688 f.close(); 611 f.close();
689 if (!result) { 612 if (!result) {
690 qWarning() << "Cannot write to mail file: " << key; 613 qWarning() << "Cannot write to mail file: " << key;
691 // d->lastError = i18n("Cannot write to mail file %1." , key);
692 return QString(); 614 return QString();
693 } 615 }
694 /* 616 /*
@@ -700,14 +622,11 @@ QString Maildir::addEntry(const QByteArray& data)
700 * 622 *
701 * For reference: http://trolltech.com/developer/task-tracker/index_html?method=entry&id=211215 623 * For reference: http://trolltech.com/developer/task-tracker/index_html?method=entry&id=211215
702 */ 624 */
625 qDebug() << "New entry: " << finalKey;
703 if (!f.rename(finalKey)) { 626 if (!f.rename(finalKey)) {
704 qWarning() << "Maildir: Failed to add entry: " << finalKey << "! Error: " << f.errorString(); 627 qWarning() << "Maildir: Failed to add entry: " << finalKey << "! Error: " << f.errorString();
705 // d->lastError = i18n("Failed to create mail file %1. The error was: %2" , finalKey, f.errorString());
706 return QString(); 628 return QString();
707 } 629 }
708 // KeyCache *keyCache = KeyCache::self();
709 // keyCache->removeKey(d->path, key); //remove all keys, be it "cur" or "new" first
710 // keyCache->addNewKey(d->path, key); //and add a key for "new", as the mail was moved there
711 return uniqueKey; 630 return uniqueKey;
712} 631}
713 632
@@ -736,12 +655,8 @@ QString Maildir::addEntryFromPath(const QString& path)
736 655
737 if (!f.rename(curKey)) { 656 if (!f.rename(curKey)) {
738 qWarning() << "Maildir: Failed to add entry: " << curKey << "! Error: " << f.errorString(); 657 qWarning() << "Maildir: Failed to add entry: " << curKey << "! Error: " << f.errorString();
739 // d->lastError = i18n("Failed to create mail file %1. The error was: %2" , finalKey, f.errorString());
740 return QString(); 658 return QString();
741 } 659 }
742 // KeyCache *keyCache = KeyCache::self();
743 // keyCache->removeKey(d->path, key); //remove all keys, be it "cur" or "new" first
744 // keyCache->addNewKey(d->path, key); //and add a key for "new", as the mail was moved there
745 return uniqueKey; 660 return uniqueKey;
746} 661}
747 662
@@ -752,24 +667,20 @@ bool Maildir::removeEntry(const QString& key)
752 qWarning() << "Maildir::removeEntry unable to find: " << key; 667 qWarning() << "Maildir::removeEntry unable to find: " << key;
753 return false; 668 return false;
754 } 669 }
755 // KeyCache *keyCache = KeyCache::self();
756 // keyCache->removeKey(d->path, key);
757 qWarning() << "Real key" << realKey;
758 QFile file(realKey); 670 QFile file(realKey);
759 if (!file.remove()) { 671 if (!file.remove()) {
760 qWarning() << file.errorString() << file.error(); 672 qWarning() << file.errorString() << file.error();
761 return false; 673 return false;
762 } 674 }
763 return true; 675 return true;
764 // return QFile::remove(realKey);
765} 676}
766 677
767QString Maildir::changeEntryFlags(const QString& key, const Maildir::Flags& flags) 678QString Maildir::changeEntryFlags(const QString& key, const Maildir::Flags& flags)
768{ 679{
769 QString realKey(d->findRealKey(key)); 680 QString realKey(d->findRealKey(key));
681 qWarning() << "Change entiry flags: " << key << realKey;
770 if (realKey.isEmpty()) { 682 if (realKey.isEmpty()) {
771 qWarning() << "Maildir::changeEntryFlags unable to find: " << key; 683 qWarning() << "Maildir::changeEntryFlags unable to find: " << key;
772 // d->lastError = i18n("Cannot locate mail file %1." , key);
773 return QString(); 684 return QString();
774 } 685 }
775 686
@@ -835,15 +746,10 @@ QString Maildir::changeEntryFlags(const QString& key, const Maildir::Flags& flag
835 746
836 if (!f.rename(finalKey)) { 747 if (!f.rename(finalKey)) {
837 qWarning() << "Maildir: Failed to rename entry: " << f.fileName() << " to " << finalKey << "! Error: " << f.errorString(); 748 qWarning() << "Maildir: Failed to rename entry: " << f.fileName() << " to " << finalKey << "! Error: " << f.errorString();
838 // d->lastError = i18n("Failed to update the file name %1 to %2 on the disk. The error was: %3." , f.fileName(), finalKey, f.errorString());
839 return QString(); 749 return QString();
840 } 750 }
841 qWarning() << "Renamed file: " << f.fileName() << finalKey; 751 qWarning() << "Renamed file: " << f.fileName() << finalKey;
842 752
843 // KeyCache *keyCache = KeyCache::self();
844 // keyCache->removeKey(d->path, key);
845 // keyCache->addCurKey(d->path, newUniqueKey);
846
847 return newUniqueKey; 753 return newUniqueKey;
848} 754}
849 755
@@ -910,23 +816,16 @@ QString Maildir::moveEntryTo(const QString &key, const Maildir &destination)
910 const QString realKey(d->findRealKey(key)); 816 const QString realKey(d->findRealKey(key));
911 if (realKey.isEmpty()) { 817 if (realKey.isEmpty()) {
912 qWarning() << "Unable to find: " << key; 818 qWarning() << "Unable to find: " << key;
913 // d->lastError = i18n("Cannot locate mail file %1." , key);
914 return QString(); 819 return QString();
915 } 820 }
916 QFile f(realKey); 821 QFile f(realKey);
917 // ### is this safe regarding the maildir locking scheme? 822 // ### is this safe regarding the maildir locking scheme?
918 const QString targetKey = destination.path() + QDir::separator() + QLatin1String("cur") + QDir::separator() + key; 823 const QString targetKey = destination.path() + QDir::separator() + QLatin1String("cur") + QDir::separator() + key;
919 if (!f.rename(targetKey)) { 824 if (!f.rename(targetKey)) {
920 qDebug() << "Failed to rename" << realKey << "to" << targetKey << "! Error: " << f.errorString();; 825 qWarning() << "Failed to rename" << realKey << "to" << targetKey << "! Error: " << f.errorString();;
921 d->lastError = f.errorString();
922 return QString(); 826 return QString();
923 } 827 }
924 828
925 // KeyCache* keyCache = KeyCache::self();
926 //
927 // keyCache->addNewKey(destination.path(), key);
928 // keyCache->removeKey(d->path, key);
929
930 return key; 829 return key;
931} 830}
932 831
@@ -943,20 +842,3 @@ QString Maildir::subDirNameForFolderName(const QString &folderName)
943 return Private::subDirNameForFolderName(folderName); 842 return Private::subDirNameForFolderName(folderName);
944} 843}
945 844
946void Maildir::removeCachedKeys(const QStringList & keys)
947{
948 // KeyCache *keyCache = KeyCache::self();
949 // Q_FOREACH (const QString &key, keys) {
950 // keyCache->removeKey(d->path, key);
951 // }
952}
953
954void Maildir::refreshKeyCache()
955{
956 // KeyCache::self()->refreshKeys(d->path);
957}
958
959QString Maildir::lastError() const
960{
961 return d->lastError;
962}