File indexing completed on 2023-09-24 04:04:55
0001 /* This file is part of the KDE libraries 0002 * Copyright 2006 Jaison Lee <lee.jaison@gmail.com> 0003 * 0004 * This library is free software; you can redistribute it and/or 0005 * modify it under the terms of the GNU Library General Public 0006 * License as published by the Free Software Foundation; either 0007 * version 2 of the License, or (at your option) any later version. 0008 * 0009 * This library 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 GNU 0012 * Library General Public License for more details. 0013 * 0014 * You should have received a copy of the GNU Library General Public License 0015 * along with this library; see the file COPYING.LIB. If not, write to 0016 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0017 * Boston, MA 02110-1301, USA. 0018 */ 0019 0020 #include "ktemporaryfile.h" 0021 #include "kcomponentdata.h" 0022 0023 #include <QDir> 0024 0025 class KTemporaryFilePrivate 0026 { 0027 public: 0028 KTemporaryFilePrivate(const KComponentData &c) 0029 : componentData(c) 0030 { 0031 } 0032 0033 inline QString defaultPrefix() const 0034 { 0035 // was: KStandardDirs::locateLocal("tmp", componentData.componentName(), componentData); 0036 return QDir::tempPath() + QLatin1Char('/') + componentData.componentName(); 0037 } 0038 0039 KComponentData componentData; 0040 }; 0041 0042 KTemporaryFile::KTemporaryFile(const KComponentData &componentData) 0043 : d(new KTemporaryFilePrivate(componentData)) 0044 { 0045 QString temp(d->defaultPrefix()); 0046 setFileTemplate(temp + QLatin1String("XXXXXX.tmp")); 0047 } 0048 0049 KTemporaryFile::~KTemporaryFile() 0050 { 0051 delete d; 0052 } 0053 0054 void KTemporaryFile::setPrefix(const QString &prefix) 0055 { 0056 QString oldTemplate = fileTemplate(); 0057 QString suffix = oldTemplate.mid(oldTemplate.lastIndexOf(QLatin1String("XXXXXX")) + 6); 0058 QString newPrefix = prefix; 0059 0060 if (newPrefix.isEmpty()) { 0061 newPrefix = d->defaultPrefix(); 0062 } else { 0063 if (!QDir::isAbsolutePath(newPrefix)) { 0064 newPrefix.prepend(QDir::tempPath() + QLatin1Char('/')); 0065 } 0066 } 0067 0068 setFileTemplate(newPrefix + QLatin1String("XXXXXX") + suffix); 0069 } 0070 0071 void KTemporaryFile::setSuffix(const QString &suffix) 0072 { 0073 QString oldTemplate = fileTemplate(); 0074 QString prefix = oldTemplate.left(oldTemplate.indexOf(QLatin1String("XXXXXX"))); 0075 0076 setFileTemplate(prefix + QLatin1String("XXXXXX") + suffix); 0077 }