kdecore Library API Documentation

ksavefile.cpp

00001 /*
00002   This file is part of the KDE libraries
00003   Copyright (c) 1999 Waldo Bastian <bastian@kde.org>
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 version 2 as published by the Free Software Foundation.
00008 
00009   This library is distributed in the hope that it will be useful,
00010   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012   Library General Public License for more details.
00013 
00014   You should have received a copy of the GNU Library General Public License
00015   along with this library; see the file COPYING.LIB.  If not, write to
00016   the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00017   Boston, MA 02111-1307, USA.
00018 */
00019 
00020 #include <config.h>
00021 
00022 #include <sys/types.h>
00023 
00024 #ifdef HAVE_SYS_STAT_H
00025 #include <sys/stat.h>
00026 #endif
00027 
00028 #include <unistd.h>
00029 #include <fcntl.h>
00030 
00031 #ifdef HAVE_TEST
00032 #include <test.h>
00033 #endif
00034 
00035 #include <qdatetime.h>
00036 #include <qdir.h>
00037 
00038 #include "kapplication.h"
00039 #include "ksavefile.h"
00040 
00041 KSaveFile::KSaveFile(const QString &filename, int mode)
00042  : mTempFile(true) 
00043 {
00044    // we only check here if the directory can be written to
00045    // the actual filename isn't written to, but replaced later
00046    // with the contents of our tempfile
00047    if (!checkAccess(filename, W_OK)) 
00048    {
00049       mTempFile.setError(EACCES);
00050       return;
00051    }
00052 
00053    if (mTempFile.create(filename, QString::fromLatin1(".new"), mode))
00054    {
00055       mFileName = filename; // Set filename upon success
00056    }
00057    return;
00058 }
00059 
00060 KSaveFile::~KSaveFile()
00061 {
00062    close();
00063 }
00064 
00065 QString 
00066 KSaveFile::name() const
00067 {
00068    return mFileName;
00069 }
00070 
00071 void
00072 KSaveFile::abort()
00073 {
00074    mTempFile.unlink();
00075    mTempFile.close();
00076 }
00077 
00078 bool
00079 KSaveFile::close()
00080 {
00081    if (mTempFile.name().isEmpty())
00082       return false; // Save was aborted already
00083    if (mTempFile.close())
00084    {
00085       QDir dir;
00086       bool result = dir.rename( mTempFile.name(), mFileName);
00087       if ( result )
00088       {
00089          return true; // Success!
00090       }
00091       mTempFile.setError(errno);
00092    }
00093     
00094    // Something went wrong, make sure to delete the interim file.
00095    mTempFile.unlink();
00096    return false;
00097 }
00098 
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 12:46:50 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001