Commit 28c0fad2 authored by Sandro Knauß's avatar Sandro Knauß

Move PimCommon::AttachmentTemporaryFilesDirs into MimeTreeParser

parent 856653ab
......@@ -18,7 +18,7 @@ include(KDEFrameworkCompilerSettings NO_POLICY_SCOPE)
include(ECMQtDeclareLoggingCategory)
include(ECMAddTests)
set(MESSAGELIB_LIB_VERSION "5.2.52")
set(MESSAGELIB_LIB_VERSION "5.2.53")
set(KF5_VERSION "5.19.0")
set(KDEPIMLIBS_LIB_VERSION "5.2.40")
......
......@@ -38,5 +38,6 @@ install(FILES
add_subdirectory(src)
if (BUILD_TESTING)
add_subdirectory(autotests)
add_subdirectory(src/temporaryfile/autotests)
endif()
@PACKAGE_INIT@
find_dependency(KF5PimCommon "@PIMCOMMON_LIB_VERSION@")
find_dependency(KF5MessageCore "@MESSAGELIB_LIB_VERSION@")
find_dependency(KF5Mime "@KMIME_LIB_VERSION@")
......
......@@ -46,6 +46,9 @@ set(libmimetreeparser_extra_SRCS
htmlwriter/queuehtmlwriter.cpp
)
set(mimetreeparser_temporaryfile_SRCS
temporaryfile/attachmenttemporaryfilesdirs.cpp
)
ecm_generate_headers(MimeTreeParser_Camelcaseviewer_HEADERS
HEADER_NAMES
......@@ -97,12 +100,21 @@ ecm_generate_headers(MimeTreeParser_Camelcasehtmlwriter_HEADERS
RELATIVE htmlwriter
)
ecm_generate_headers(MimeTreeParser_Camelcasetemporaryfile_HEADERS
HEADER_NAMES
AttachmentTemporaryFilesDirs
REQUIRED_HEADERS MimeTreeParser_temporaryfile_HEADERS
PREFIX MimeTreeParser
RELATIVE temporaryfile
)
install(FILES
${MimeTreeParser_Camelcasehtmlwriter_HEADERS}
${MimeTreeParser_Camelcaseutils_HEADERS}
${MimeTreeParser_Camelcaseinterfaces_HEADERS}
${MimeTreeParser_Camelcaseviewer_HEADERS}
${MimeTreeParser_Camelcasemain_HEADERS}
${MimeTreeParser_Camelcasetemporaryfile_HEADERS}
DESTINATION ${KDE_INSTALL_INCLUDEDIR_KF5}/MimeTreeParser
COMPONENT Devel
)
......@@ -113,6 +125,7 @@ install(FILES
${MimeTreeParser_interfaces_HEADERS}
${MimeTreeParser_viewer_HEADERS}
${MimeTreeParser_main_HEADERS}
${MimeTreeParser_temporaryfile_HEADERS}
${CMAKE_CURRENT_BINARY_DIR}/mimetreeparser_export.h
${CMAKE_CURRENT_BINARY_DIR}/mimetreeparser_debug.h
......@@ -122,7 +135,7 @@ install(FILES
ecm_generate_pri_file(BASE_NAME MimeTreeParser
LIB_NAME KF5MimeTreeParser
DEPS "PimCommon MessageCore Libkleo" FILENAME_VAR PRI_FILENAME INCLUDE_INSTALL_DIR ${KDE_INSTALL_INCLUDEDIR_KF5}/MimeTreeParser
DEPS "MessageCore Libkleo" FILENAME_VAR PRI_FILENAME INCLUDE_INSTALL_DIR ${KDE_INSTALL_INCLUDEDIR_KF5}/MimeTreeParser
)
install(FILES
......@@ -133,6 +146,7 @@ install(FILES
set(libmimetreeparser_SRCS
${libmimetreeparser_main_SRCS}
${libmimetreeparser_extra_SRCS}
${mimetreeparser_temporaryfile_SRCS}
)
ecm_qt_declare_logging_category(libmimetreeparser_SRCS HEADER mimetreeparser_debug.h IDENTIFIER MIMETREEPARSER_LOG CATEGORY_NAME log_mimetreeparser)
......@@ -147,12 +161,15 @@ add_library(KF5::MimeTreeParser ALIAS KF5MimeTreeParser)
target_link_libraries(KF5MimeTreeParser
PUBLIC
KF5::MessageCore #NodeHelper, StringUtils
KF5::PimCommon #AttachmentTemporaryFile handling
KF5::Libkleo
PRIVATE
KF5::Codecs
KF5::ConfigCore
KF5::CoreAddons
KF5::IconThemes
KF5::I18n
KF5::Mime
Qt5::WebKitWidgets
KF5::IconThemes
Grantlee5::TextDocument
)
......
/*
Copyright (c) 2013-2016 Montel Laurent <montel@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 "attachmenttemporaryfilesdirs.h"
#include <QDir>
#include <QFile>
#include <QTimer>
using namespace MimeTreeParser;
class MimeTreeParser::AttachmentTemporaryFilesDirsPrivate
{
public:
AttachmentTemporaryFilesDirsPrivate()
: mDelayRemoveAll(10000)
{
}
QStringList mTempFiles;
QStringList mTempDirs;
int mDelayRemoveAll;
};
AttachmentTemporaryFilesDirs::AttachmentTemporaryFilesDirs(QObject *parent)
: QObject(parent),
d(new AttachmentTemporaryFilesDirsPrivate)
{
}
AttachmentTemporaryFilesDirs::~AttachmentTemporaryFilesDirs()
{
delete d;
}
void AttachmentTemporaryFilesDirs::setDelayRemoveAllInMs(int ms)
{
d->mDelayRemoveAll = (ms < 0) ? 0 : ms;
}
void AttachmentTemporaryFilesDirs::removeTempFiles()
{
QTimer::singleShot(d->mDelayRemoveAll, this, &AttachmentTemporaryFilesDirs::slotRemoveTempFiles);
}
void AttachmentTemporaryFilesDirs::forceCleanTempFiles()
{
QStringList::ConstIterator end = d->mTempFiles.constEnd();
for (QStringList::ConstIterator it = d->mTempFiles.constBegin(); it != end; ++it) {
QFile::remove(*it);
}
d->mTempFiles.clear();
end = d->mTempDirs.constEnd();
for (QStringList::ConstIterator it = d->mTempDirs.constBegin(); it != end; ++it) {
QDir(*it).rmdir(*it);
}
d->mTempDirs.clear();
}
void AttachmentTemporaryFilesDirs::slotRemoveTempFiles()
{
forceCleanTempFiles();
//Delete it after cleaning
deleteLater();
}
void AttachmentTemporaryFilesDirs::addTempFile(const QString &file)
{
if (!d->mTempFiles.contains(file)) {
d->mTempFiles.append(file);
}
}
void AttachmentTemporaryFilesDirs::addTempDir(const QString &dir)
{
if (!d->mTempDirs.contains(dir)) {
d->mTempDirs.append(dir);
}
}
QStringList AttachmentTemporaryFilesDirs::temporaryFiles() const
{
return d->mTempFiles;
}
QStringList AttachmentTemporaryFilesDirs::temporaryDirs() const
{
return d->mTempDirs;
}
/*
Copyright (c) 2013-2016 Montel Laurent <montel@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.
*/
#ifndef ATTACHMENTTEMPORARYFILESDIRS_H
#define ATTACHMENTTEMPORARYFILESDIRS_H
#include <QObject>
#include <QStringList>
#include "mimetreeparser_export.h"
namespace MimeTreeParser
{
class AttachmentTemporaryFilesDirsPrivate;
class MIMETREEPARSER_EXPORT AttachmentTemporaryFilesDirs : public QObject
{
Q_OBJECT
public:
explicit AttachmentTemporaryFilesDirs(QObject *parent = Q_NULLPTR);
~AttachmentTemporaryFilesDirs();
void addTempFile(const QString &file);
void addTempDir(const QString &dir);
QStringList temporaryFiles() const;
void removeTempFiles();
void forceCleanTempFiles();
QStringList temporaryDirs() const;
void setDelayRemoveAllInMs(int ms);
private Q_SLOTS:
void slotRemoveTempFiles();
private:
AttachmentTemporaryFilesDirsPrivate *const d;
};
}
#endif // ATTACHMENTTEMPORARYFILESDIRS_H
ecm_add_test(attachmenttemporaryfilesdirstest.cpp
NAME_PREFIX "mimeptreeparser-temporaryfile-"
LINK_LIBRARIES Qt5::Test KF5::MimeTreeParser
)
/*
Copyright (c) 2014-2016 Montel Laurent <montel@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 "attachmenttemporaryfilesdirstest.h"
#include "../attachmenttemporaryfilesdirs.h"
#include <QTemporaryDir>
#include <qtest.h>
#include <QDebug>
using namespace MimeTreeParser;
AttachmentTemporaryFilesDirsTest::AttachmentTemporaryFilesDirsTest(QObject *parent)
: QObject(parent)
{
}
AttachmentTemporaryFilesDirsTest::~AttachmentTemporaryFilesDirsTest()
{
}
void AttachmentTemporaryFilesDirsTest::shouldHaveDefaultValue()
{
AttachmentTemporaryFilesDirs attachmentDir;
QVERIFY(attachmentDir.temporaryFiles().isEmpty());
QVERIFY(attachmentDir.temporaryDirs().isEmpty());
}
void AttachmentTemporaryFilesDirsTest::shouldAddTemporaryFiles()
{
AttachmentTemporaryFilesDirs attachmentDir;
attachmentDir.addTempFile(QStringLiteral("foo"));
QCOMPARE(attachmentDir.temporaryFiles().count(), 1);
attachmentDir.addTempFile(QStringLiteral("foo1"));
QCOMPARE(attachmentDir.temporaryFiles().count(), 2);
}
void AttachmentTemporaryFilesDirsTest::shouldAddTemporaryDirs()
{
AttachmentTemporaryFilesDirs attachmentDir;
attachmentDir.addTempDir(QStringLiteral("foo"));
QCOMPARE(attachmentDir.temporaryDirs().count(), 1);
attachmentDir.addTempDir(QStringLiteral("foo1"));
QCOMPARE(attachmentDir.temporaryDirs().count(), 2);
}
void AttachmentTemporaryFilesDirsTest::shouldNotAddSameFiles()
{
AttachmentTemporaryFilesDirs attachmentDir;
attachmentDir.addTempFile(QStringLiteral("foo"));
QCOMPARE(attachmentDir.temporaryFiles().count(), 1);
attachmentDir.addTempFile(QStringLiteral("foo"));
QCOMPARE(attachmentDir.temporaryFiles().count(), 1);
}
void AttachmentTemporaryFilesDirsTest::shouldNotAddSameDirs()
{
AttachmentTemporaryFilesDirs attachmentDir;
attachmentDir.addTempDir(QStringLiteral("foo"));
QCOMPARE(attachmentDir.temporaryDirs().count(), 1);
attachmentDir.addTempDir(QStringLiteral("foo"));
QCOMPARE(attachmentDir.temporaryDirs().count(), 1);
}
void AttachmentTemporaryFilesDirsTest::shouldForceRemoveTemporaryDirs()
{
AttachmentTemporaryFilesDirs attachmentDir;
attachmentDir.addTempDir(QStringLiteral("foo"));
attachmentDir.addTempDir(QStringLiteral("foo1"));
QCOMPARE(attachmentDir.temporaryDirs().count(), 2);
attachmentDir.forceCleanTempFiles();
QCOMPARE(attachmentDir.temporaryDirs().count(), 0);
QCOMPARE(attachmentDir.temporaryFiles().count(), 0);
}
void AttachmentTemporaryFilesDirsTest::shouldForceRemoveTemporaryFiles()
{
AttachmentTemporaryFilesDirs attachmentDir;
attachmentDir.addTempFile(QStringLiteral("foo"));
attachmentDir.addTempFile(QStringLiteral("foo2"));
QCOMPARE(attachmentDir.temporaryFiles().count(), 2);
attachmentDir.forceCleanTempFiles();
QCOMPARE(attachmentDir.temporaryFiles().count(), 0);
QCOMPARE(attachmentDir.temporaryDirs().count(), 0);
}
void AttachmentTemporaryFilesDirsTest::shouldCreateDeleteTemporaryFiles()
{
QTemporaryDir tmpDir;
QVERIFY(tmpDir.isValid());
QFile file(tmpDir.path() + QStringLiteral("/foo"));
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
qDebug() << "Can open file";
return;
}
tmpDir.setAutoRemove(false);
file.close();
QVERIFY(file.exists());
AttachmentTemporaryFilesDirs attachmentDir;
attachmentDir.addTempDir(tmpDir.path());
attachmentDir.addTempFile(file.fileName());
QVERIFY(!attachmentDir.temporaryFiles().isEmpty());
QCOMPARE(attachmentDir.temporaryFiles().first(), file.fileName());
const QString path = tmpDir.path();
attachmentDir.forceCleanTempFiles();
QCOMPARE(attachmentDir.temporaryFiles().count(), 0);
QCOMPARE(attachmentDir.temporaryDirs().count(), 0);
QVERIFY(!QDir(path).exists());
}
void AttachmentTemporaryFilesDirsTest::shouldRemoveTemporaryFilesAfterTime()
{
QTemporaryDir tmpDir;
QVERIFY(tmpDir.isValid());
QFile file(tmpDir.path() + QStringLiteral("/foo"));
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
qDebug() << "Can open file";
return;
}
tmpDir.setAutoRemove(false);
file.close();
QVERIFY(file.exists());
AttachmentTemporaryFilesDirs attachmentDir;
attachmentDir.addTempDir(tmpDir.path());
attachmentDir.addTempFile(file.fileName());
QVERIFY(!attachmentDir.temporaryFiles().isEmpty());
QCOMPARE(attachmentDir.temporaryFiles().first(), file.fileName());
attachmentDir.setDelayRemoveAllInMs(500);
QTest::qSleep(1000);
attachmentDir.removeTempFiles();
const QString path = tmpDir.path();
attachmentDir.forceCleanTempFiles();
QCOMPARE(attachmentDir.temporaryFiles().count(), 0);
QCOMPARE(attachmentDir.temporaryDirs().count(), 0);
QVERIFY(!QDir(path).exists());
}
QTEST_GUILESS_MAIN(AttachmentTemporaryFilesDirsTest)
/*
Copyright (c) 2014-2016 Montel Laurent <montel@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.
*/
#ifndef ATTACHMENTTEMPORARYFILESDIRSTEST_H
#define ATTACHMENTTEMPORARYFILESDIRSTEST_H
#include <QObject>
class AttachmentTemporaryFilesDirsTest : public QObject
{
Q_OBJECT
public:
explicit AttachmentTemporaryFilesDirsTest(QObject *parent = Q_NULLPTR);
~AttachmentTemporaryFilesDirsTest();
private Q_SLOTS:
void shouldHaveDefaultValue();
void shouldAddTemporaryFiles();
void shouldAddTemporaryDirs();
void shouldNotAddSameFiles();
void shouldNotAddSameDirs();
void shouldForceRemoveTemporaryDirs();
void shouldForceRemoveTemporaryFiles();
void shouldCreateDeleteTemporaryFiles();
void shouldRemoveTemporaryFilesAfterTime();
};
#endif // ATTACHMENTTEMPORARYFILESDIRSTEST_H
......@@ -23,7 +23,7 @@
#include "partmetadata.h"
#include "interfaces/bodypart.h"
#include "utils/mimetype.h"
#include "PimCommon/AttachmentTemporaryFilesDirs"
#include "temporaryfile/attachmenttemporaryfilesdirs.h"
#include <MessageCore/NodeHelper>
......@@ -54,7 +54,7 @@ QStringList replySubjPrefixes(QStringList() << QStringLiteral("Re\\s*:") << QStr
QStringList forwardSubjPrefixes(QStringList() << QStringLiteral("Fwd:") << QStringLiteral("FW:"));
NodeHelper::NodeHelper() :
mAttachmentFilesDir(new PimCommon::AttachmentTemporaryFilesDirs())
mAttachmentFilesDir(new AttachmentTemporaryFilesDirs())
{
//TODO(Andras) add methods to modify these prefixes
......@@ -316,7 +316,7 @@ void NodeHelper::removeTempFiles()
{
//Don't delete it it will delete in class
mAttachmentFilesDir->removeTempFiles();
mAttachmentFilesDir = new PimCommon::AttachmentTemporaryFilesDirs();
mAttachmentFilesDir = new AttachmentTemporaryFilesDirs();
}
void NodeHelper::addTempFile(const QString &file)
......
......@@ -38,16 +38,13 @@ class QTextCodec;
namespace MimeTreeParser
{
class AttachmentTemporaryFilesDirs;
namespace Interface
{
class BodyPartMemento;
}
}
namespace PimCommon
{
class AttachmentTemporaryFilesDirs;
}
namespace MimeTreeParser
{
......@@ -297,7 +294,7 @@ private:
QMap<QString, QMap<QByteArray, Interface::BodyPartMemento *> > mBodyPartMementoMap;
QMap<KMime::Content *, PartMetaData> mPartMetaDatas;
QMap<KMime::Message::Content *, QList<KMime::Content *> > mExtraContents;
PimCommon::AttachmentTemporaryFilesDirs *mAttachmentFilesDir;
AttachmentTemporaryFilesDirs *mAttachmentFilesDir;
friend class NodeHelperTest;
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment