Messagelib

attachmenttemporaryfilesdirs.cpp
1/*
2 SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5
6*/
7
8#include "attachmenttemporaryfilesdirs.h"
9
10#include <QDir>
11#include <QFile>
12#include <QTimer>
13
14using namespace MimeTreeParser;
15
16class MimeTreeParser::AttachmentTemporaryFilesDirsPrivate
17{
18public:
19 AttachmentTemporaryFilesDirsPrivate() = default;
20
21 QStringList mTempFiles;
22 QStringList mTempDirs;
23 int mDelayRemoveAll = 10000;
24};
25
26AttachmentTemporaryFilesDirs::AttachmentTemporaryFilesDirs(QObject *parent)
27 : QObject(parent)
28 , d(new AttachmentTemporaryFilesDirsPrivate)
29{
30}
31
32AttachmentTemporaryFilesDirs::~AttachmentTemporaryFilesDirs() = default;
33
34void AttachmentTemporaryFilesDirs::setDelayRemoveAllInMs(int ms)
35{
36 d->mDelayRemoveAll = (ms < 0) ? 0 : ms;
37}
38
39void AttachmentTemporaryFilesDirs::removeTempFiles()
40{
41 QTimer::singleShot(d->mDelayRemoveAll, this, &AttachmentTemporaryFilesDirs::slotRemoveTempFiles);
42}
43
44void AttachmentTemporaryFilesDirs::forceCleanTempFiles()
45{
46 QStringList::ConstIterator end = d->mTempFiles.constEnd();
47 for (QStringList::ConstIterator it = d->mTempFiles.constBegin(); it != end; ++it) {
48 QFile::remove(*it);
49 }
50 d->mTempFiles.clear();
51 end = d->mTempDirs.constEnd();
52 for (QStringList::ConstIterator it = d->mTempDirs.constBegin(); it != end; ++it) {
53 QDir(*it).rmdir(*it);
54 }
55 d->mTempDirs.clear();
56}
57
58void AttachmentTemporaryFilesDirs::slotRemoveTempFiles()
59{
60 forceCleanTempFiles();
61 // Delete it after cleaning
63}
64
65void AttachmentTemporaryFilesDirs::addTempFile(const QString &file)
66{
67 if (!d->mTempFiles.contains(file)) {
68 d->mTempFiles.append(file);
69 }
70}
71
72void AttachmentTemporaryFilesDirs::addTempDir(const QString &dir)
73{
74 if (!d->mTempDirs.contains(dir)) {
75 d->mTempDirs.append(dir);
76 }
77}
78
79QStringList AttachmentTemporaryFilesDirs::temporaryFiles() const
80{
81 return d->mTempFiles;
82}
83
84QStringList AttachmentTemporaryFilesDirs::temporaryDirs() const
85{
86 return d->mTempDirs;
87}
88
89#include "moc_attachmenttemporaryfilesdirs.cpp"
const QList< QKeySequence > & end()
bool rmdir(const QString &dirName) const const
bool remove()
void deleteLater()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:59:13 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.