diff options
Diffstat (limited to 'examples')
-rw-r--r-- | examples/imapresource/imapresource.cpp | 34 |
1 files changed, 24 insertions, 10 deletions
diff --git a/examples/imapresource/imapresource.cpp b/examples/imapresource/imapresource.cpp index 8cbb9fa..b3aafed 100644 --- a/examples/imapresource/imapresource.cpp +++ b/examples/imapresource/imapresource.cpp | |||
@@ -83,13 +83,13 @@ static QHash<QString, QByteArray> sSpecialPurposeNames = specialPurposeNames(); | |||
83 | class DraftsProcessor : public Sink::Preprocessor | 83 | class DraftsProcessor : public Sink::Preprocessor |
84 | { | 84 | { |
85 | public: | 85 | public: |
86 | DraftsProcessor() {} | 86 | DraftsProcessor(const QByteArray &resourceType, const QByteArray &resourceInstanceIdentifier) : mResourceType(resourceType), mResourceInstanceIdentifier(resourceInstanceIdentifier) {} |
87 | 87 | ||
88 | QByteArray ensureDraftsFolder(Sink::Storage::Transaction &transaction) | 88 | QByteArray ensureDraftsFolder(Sink::Storage::Transaction &transaction) |
89 | { | 89 | { |
90 | if (mDraftsFolder.isEmpty()) { | 90 | if (mDraftsFolder.isEmpty()) { |
91 | //Try to find an existing drafts folder | 91 | //Try to find an existing drafts folder |
92 | Sink::EntityReader<ApplicationDomain::Folder> reader(mResourceInstanceIdentifier, mResourceType, transaction); | 92 | Sink::EntityReader<ApplicationDomain::Folder> reader(mResourceType, mResourceInstanceIdentifier, transaction); |
93 | reader.query(Sink::Query().filter<ApplicationDomain::Folder::SpecialPurpose>(Query::Comparator("drafts", Query::Comparator::Contains)), | 93 | reader.query(Sink::Query().filter<ApplicationDomain::Folder::SpecialPurpose>(Query::Comparator("drafts", Query::Comparator::Contains)), |
94 | [this](const ApplicationDomain::Folder &f) -> bool{ | 94 | [this](const ApplicationDomain::Folder &f) -> bool{ |
95 | mDraftsFolder = f.identifier(); | 95 | mDraftsFolder = f.identifier(); |
@@ -125,8 +125,8 @@ public: | |||
125 | } | 125 | } |
126 | 126 | ||
127 | QByteArray mDraftsFolder; | 127 | QByteArray mDraftsFolder; |
128 | QByteArray mResourceInstanceIdentifier; | ||
129 | QByteArray mResourceType; | 128 | QByteArray mResourceType; |
129 | QByteArray mResourceInstanceIdentifier; | ||
130 | }; | 130 | }; |
131 | 131 | ||
132 | class MailPropertyExtractor : public Sink::EntityPreprocessor<ApplicationDomain::Mail> | 132 | class MailPropertyExtractor : public Sink::EntityPreprocessor<ApplicationDomain::Mail> |
@@ -142,14 +142,19 @@ public: | |||
142 | Warning() << "Failed to open the file: " << mimeMessagePath; | 142 | Warning() << "Failed to open the file: " << mimeMessagePath; |
143 | return; | 143 | return; |
144 | } | 144 | } |
145 | auto mapped = f.map(0, qMin((qint64)8000, f.size())); | 145 | if (!f.size()) { |
146 | Warning() << "The file is empty."; | ||
147 | return; | ||
148 | } | ||
149 | const auto mappedSize = qMin((qint64)8000, f.size()); | ||
150 | auto mapped = f.map(0, mappedSize); | ||
146 | if (!mapped) { | 151 | if (!mapped) { |
147 | Warning() << "Failed to map file"; | 152 | Warning() << "Failed to map the file: " << f.errorString(); |
148 | return; | 153 | return; |
149 | } | 154 | } |
150 | 155 | ||
151 | KMime::Message *msg = new KMime::Message; | 156 | KMime::Message *msg = new KMime::Message; |
152 | msg->setHead(KMime::CRLFtoLF(QByteArray::fromRawData(reinterpret_cast<const char*>(mapped), f.size()))); | 157 | msg->setHead(KMime::CRLFtoLF(QByteArray::fromRawData(reinterpret_cast<const char*>(mapped), mappedSize))); |
153 | msg->parse(); | 158 | msg->parse(); |
154 | 159 | ||
155 | mail.setExtractedSubject(msg->subject(true)->asUnicodeString()); | 160 | mail.setExtractedSubject(msg->subject(true)->asUnicodeString()); |
@@ -176,12 +181,21 @@ public: | |||
176 | 181 | ||
177 | QString moveMessage(const QString &oldPath, const Sink::ApplicationDomain::Mail &mail) | 182 | QString moveMessage(const QString &oldPath, const Sink::ApplicationDomain::Mail &mail) |
178 | { | 183 | { |
179 | const auto directory = Sink::resourceStorageLocation(mResourceInstanceIdentifier) + "/" + mail.getFolder(); | 184 | const auto directory = Sink::resourceStorageLocation(mResourceInstanceIdentifier); |
180 | const auto filePath = directory + "/" + mail.identifier(); | 185 | const auto filePath = directory + "/" + mail.identifier(); |
181 | if (oldPath != filePath) { | 186 | if (oldPath != filePath) { |
182 | QDir().mkpath(directory); | 187 | if (!QDir().mkpath(directory)) { |
188 | Warning() << "Failed to create the directory: " << directory; | ||
189 | } | ||
183 | QFile::remove(filePath); | 190 | QFile::remove(filePath); |
184 | QFile::rename(oldPath, filePath); | 191 | QFile origFile(oldPath); |
192 | if (!origFile.open(QIODevice::ReadWrite)) { | ||
193 | Warning() << "Failed to open the original file with write rights: " << origFile.errorString(); | ||
194 | } | ||
195 | if (!origFile.rename(filePath)) { | ||
196 | Warning() << "Failed to move the file from: " << oldPath << " to " << filePath << ". " << origFile.errorString(); | ||
197 | } | ||
198 | origFile.close(); | ||
185 | return filePath; | 199 | return filePath; |
186 | } | 200 | } |
187 | return oldPath; | 201 | return oldPath; |
@@ -633,7 +647,7 @@ ImapResource::ImapResource(const QByteArray &instanceIdentifier, const QSharedPo | |||
633 | changereplay->mPassword = mPassword; | 647 | changereplay->mPassword = mPassword; |
634 | setupChangereplay(changereplay); | 648 | setupChangereplay(changereplay); |
635 | 649 | ||
636 | setupPreprocessors(ENTITY_TYPE_MAIL, QVector<Sink::Preprocessor*>() << new DraftsProcessor << new MimeMessageMover(mResourceInstanceIdentifier) << new MailPropertyExtractor << new DefaultIndexUpdater<Sink::ApplicationDomain::Mail>); | 650 | setupPreprocessors(ENTITY_TYPE_MAIL, QVector<Sink::Preprocessor*>() << new DraftsProcessor(mResourceType, mResourceInstanceIdentifier) << new MimeMessageMover(mResourceInstanceIdentifier) << new MailPropertyExtractor << new DefaultIndexUpdater<Sink::ApplicationDomain::Mail>); |
637 | setupPreprocessors(ENTITY_TYPE_FOLDER, QVector<Sink::Preprocessor*>() << new DefaultIndexUpdater<Sink::ApplicationDomain::Folder>); | 651 | setupPreprocessors(ENTITY_TYPE_FOLDER, QVector<Sink::Preprocessor*>() << new DefaultIndexUpdater<Sink::ApplicationDomain::Folder>); |
638 | } | 652 | } |
639 | 653 | ||