KDELibs4Support

ktemporaryfile.cpp
1 /* This file is part of the KDE libraries
2  * Copyright 2006 Jaison Lee <[email protected]>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public License
15  * along with this library; see the file COPYING.LIB. If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 
20 #include "ktemporaryfile.h"
21 #include "kcomponentdata.h"
22 
23 #include <QDir>
24 
25 class KTemporaryFilePrivate
26 {
27 public:
28  KTemporaryFilePrivate(const KComponentData &c)
29  : componentData(c)
30  {
31  }
32 
33  inline QString defaultPrefix() const
34  {
35  // was: KStandardDirs::locateLocal("tmp", componentData.componentName(), componentData);
36  return QDir::tempPath() + QLatin1Char('/') + componentData.componentName();
37  }
38 
39  KComponentData componentData;
40 };
41 
43  : d(new KTemporaryFilePrivate(componentData))
44 {
45  QString temp(d->defaultPrefix());
46  setFileTemplate(temp + QLatin1String("XXXXXX.tmp"));
47 }
48 
50 {
51  delete d;
52 }
53 
55 {
56  QString oldTemplate = fileTemplate();
57  QString suffix = oldTemplate.mid(oldTemplate.lastIndexOf(QLatin1String("XXXXXX")) + 6);
58  QString newPrefix = prefix;
59 
60  if (newPrefix.isEmpty()) {
61  newPrefix = d->defaultPrefix();
62  } else {
63  if (!QDir::isAbsolutePath(newPrefix)) {
64  newPrefix.prepend(QDir::tempPath() + QLatin1Char('/'));
65  }
66  }
67 
68  setFileTemplate(newPrefix + QLatin1String("XXXXXX") + suffix);
69 }
70 
72 {
73  QString oldTemplate = fileTemplate();
74  QString prefix = oldTemplate.left(oldTemplate.indexOf(QLatin1String("XXXXXX")));
75 
76  setFileTemplate(prefix + QLatin1String("XXXXXX") + suffix);
77 }
int indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const const
void setPrefix(const QString &prefix)
Sets a prefix to use when creating the file.
QString & prepend(QChar ch)
void setSuffix(const QString &suffix)
Sets a suffix to use when creating the file.
int lastIndexOf(QChar ch, int from, Qt::CaseSensitivity cs) const const
QString tempPath()
bool isEmpty() const const
void setFileTemplate(const QString &name)
bool isAbsolutePath(const QString &path)
QString mid(int position, int n) const const
QString left(int n) const const
virtual ~KTemporaryFile()
Destructor.
QString fileTemplate() const const
KTemporaryFile(const KComponentData &componentData=KGlobal::mainComponent())
Construct a new KTemporaryFile.
Per component data.
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sun Jul 5 2020 22:58:45 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.