KDECore
ktemporaryfile.cpp
Go to the documentation of this file.00001 /* kate: tab-indents off; replace-tabs on; tab-width 4; remove-trailing-space on; encoding utf-8;*/ 00002 /* This file is part of the KDE libraries 00003 * Copyright 2006 Jaison Lee <lee.jaison@gmail.com> 00004 * 00005 * This library is free software; you can redistribute it and/or 00006 * modify it under the terms of the GNU Library General Public 00007 * License as published by the Free Software Foundation; either 00008 * version 2 of the License, or (at your option) any later version. 00009 * 00010 * This library is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 * Library General Public License for more details. 00014 * 00015 * You should have received a copy of the GNU Library General Public License 00016 * along with this library; see the file COPYING.LIB. If not, write to 00017 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00018 * Boston, MA 02110-1301, USA. 00019 */ 00020 00021 #include "ktemporaryfile.h" 00022 #include "kcomponentdata.h" 00023 #include "kstandarddirs.h" 00024 00025 #include <QDir> 00026 00027 class KTemporaryFilePrivate 00028 { 00029 public: 00030 KTemporaryFilePrivate(const KComponentData &c) 00031 : componentData(c) 00032 { 00033 } 00034 00035 inline QString defaultPrefix() const 00036 { 00037 return KStandardDirs::locateLocal("tmp", componentData.componentName(), componentData); 00038 } 00039 00040 KComponentData componentData; 00041 }; 00042 00043 KTemporaryFile::KTemporaryFile(const KComponentData &componentData) 00044 : d(new KTemporaryFilePrivate(componentData)) 00045 { 00046 QString temp(d->defaultPrefix()); 00047 setFileTemplate(temp + "XXXXXX.tmp"); 00048 } 00049 00050 KTemporaryFile::~KTemporaryFile() 00051 { 00052 delete d; 00053 } 00054 00055 void KTemporaryFile::setPrefix(const QString &prefix) 00056 { 00057 QString oldTemplate = fileTemplate(); 00058 QString suffix = oldTemplate.mid(oldTemplate.lastIndexOf("XXXXXX")+6); 00059 QString newPrefix = prefix; 00060 00061 if ( newPrefix.isEmpty() ) { 00062 newPrefix = d->defaultPrefix(); 00063 } else { 00064 if ( !QDir::isAbsolutePath(newPrefix) ) { 00065 newPrefix.prepend ( KStandardDirs::locateLocal("tmp", "") ); 00066 } 00067 } 00068 00069 setFileTemplate(newPrefix + "XXXXXX" + suffix); 00070 } 00071 00072 void KTemporaryFile::setSuffix(const QString &suffix) 00073 { 00074 QString oldTemplate = fileTemplate(); 00075 QString prefix = oldTemplate.left(oldTemplate.indexOf("XXXXXX")); 00076 00077 setFileTemplate(prefix + "XXXXXX" + suffix); 00078 }