Messagelib

attachmenttemporaryfilesdirs.cpp
1 /*
2  SPDX-FileCopyrightText: 2013-2021 Laurent Montel <[email protected]>
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 
14 using namespace MimeTreeParser;
15 
16 class MimeTreeParser::AttachmentTemporaryFilesDirsPrivate
17 {
18 public:
19  AttachmentTemporaryFilesDirsPrivate() = default;
20 
21  QStringList mTempFiles;
22  QStringList mTempDirs;
23  int mDelayRemoveAll = 10000;
24 };
25 
26 AttachmentTemporaryFilesDirs::AttachmentTemporaryFilesDirs(QObject *parent)
27  : QObject(parent)
28  , d(new AttachmentTemporaryFilesDirsPrivate)
29 {
30 }
31 
32 AttachmentTemporaryFilesDirs::~AttachmentTemporaryFilesDirs() = default;
33 
34 void AttachmentTemporaryFilesDirs::setDelayRemoveAllInMs(int ms)
35 {
36  d->mDelayRemoveAll = (ms < 0) ? 0 : ms;
37 }
38 
39 void AttachmentTemporaryFilesDirs::removeTempFiles()
40 {
41  QTimer::singleShot(d->mDelayRemoveAll, this, &AttachmentTemporaryFilesDirs::slotRemoveTempFiles);
42 }
43 
44 void 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 
58 void AttachmentTemporaryFilesDirs::slotRemoveTempFiles()
59 {
60  forceCleanTempFiles();
61  // Delete it after cleaning
62  deleteLater();
63 }
64 
65 void AttachmentTemporaryFilesDirs::addTempFile(const QString &file)
66 {
67  if (!d->mTempFiles.contains(file)) {
68  d->mTempFiles.append(file);
69  }
70 }
71 
72 void AttachmentTemporaryFilesDirs::addTempDir(const QString &dir)
73 {
74  if (!d->mTempDirs.contains(dir)) {
75  d->mTempDirs.append(dir);
76  }
77 }
78 
79 QStringList AttachmentTemporaryFilesDirs::temporaryFiles() const
80 {
81  return d->mTempFiles;
82 }
83 
84 QStringList AttachmentTemporaryFilesDirs::temporaryDirs() const
85 {
86  return d->mTempDirs;
87 }
bool remove()
bool rmdir(const QString &dirName) const const
const QList< QKeySequence > & end()
typedef ConstIterator
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Thu Dec 2 2021 23:06:07 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.