File indexing completed on 2025-01-05 04:58:39

0001 /*
0002  * Copyright (C) 2014 Christian Mollekopf <chrigi_1@fastmail.fm>
0003  *
0004  *   This program is free software; you can redistribute it and/or modify
0005  *   it under the terms of the GNU General Public License as published by
0006  *   the Free Software Foundation; either version 2 of the License, or
0007  *   (at your option) any later version.
0008  *
0009  *   This program is distributed in the hope that it will be useful,
0010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0012  *   GNU General Public License for more details.
0013  *
0014  *   You should have received a copy of the GNU General Public License
0015  *   along with this program; if not, write to the
0016  *   Free Software Foundation, Inc.,
0017  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
0018  */
0019 
0020 #include "facade.h"
0021 
0022 #include <QDir>
0023 #include <QFileInfo>
0024 
0025 #include "query.h"
0026 
0027 MaildirResourceMailFacade::MaildirResourceMailFacade(const Sink::ResourceContext &context)
0028     : Sink::GenericFacade<Sink::ApplicationDomain::Mail>(context)
0029 {
0030     mResultTransformation = [](Sink::ApplicationDomain::ApplicationDomainType &value) {
0031         Sink::Log::Context ctx{"maildirfacade"};
0032         if (value.hasProperty(Sink::ApplicationDomain::Mail::MimeMessage::name)) {
0033             auto mail = Sink::ApplicationDomain::Mail{value};
0034             const auto mimeMessage = mail.getMimeMessage();
0035             //Transform the mime message property into the actual path on disk.
0036             auto parts = mimeMessage.split('/');
0037             auto key = parts.takeLast();
0038             const auto folderPath = parts.join('/');
0039             const auto path =  folderPath + "/cur/";
0040 
0041             SinkTraceCtx(ctx) << "Looking for mail in: " << path << key;
0042             QDir dir(path);
0043             const QFileInfoList list = dir.entryInfoList(QStringList() << (key+"*"), QDir::Files);
0044             if (list.size() != 1) {
0045                 SinkErrorCtx(ctx) << "Failed to find message. Directory: " << path << "Key: " << key << "Number of matching files: " << list.size();
0046                 mail.setProperty(Sink::ApplicationDomain::Mail::MimeMessage::name, QVariant());
0047             } else {
0048                 QFile file{list.at(0).filePath()};
0049                 if (file.open(QIODevice::ReadOnly)) {
0050                     mail.setMimeMessage(file.readAll());
0051                 }
0052             }
0053         }
0054         value.setChangedProperties(QSet<QByteArray>());
0055     };
0056 }
0057 
0058 MaildirResourceMailFacade::~MaildirResourceMailFacade()
0059 {
0060 }
0061 
0062 
0063 MaildirResourceFolderFacade::MaildirResourceFolderFacade(const Sink::ResourceContext &context)
0064     : Sink::GenericFacade<Sink::ApplicationDomain::Folder>(context)
0065 {
0066 }
0067 
0068 MaildirResourceFolderFacade::~MaildirResourceFolderFacade()
0069 {
0070 }