Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

filemanager.cpp

Go to the documentation of this file.
00001 /**********************************************************************
00002 ** Copyright (C) 2000 Trolltech AS.  All rights reserved.
00003 **
00004 ** This file is part of Qtopia Environment.
00005 **
00006 ** This file may be distributed and/or modified under the terms of the
00007 ** GNU General Public License version 2 as published by the Free Software
00008 ** Foundation and appearing in the file LICENSE.GPL included in the
00009 ** packaging of this file.
00010 **
00011 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00012 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00013 **
00014 ** See http://www.trolltech.com/gpl/ for GPL licensing information.
00015 **
00016 ** Contact info@trolltech.com if any conditions of this licensing are
00017 ** not clear to you.
00018 **
00019 **********************************************************************/
00020 
00021 #include "filemanager.h"
00022 #include "applnk.h"
00023 
00024 /* QT */
00025 #include <qdir.h>
00026 #include <qfileinfo.h>
00027 #include <qtextstream.h>
00028 
00029 /* STD */
00030 #include <stdlib.h>
00031 #include <errno.h>
00032 #include <sys/stat.h>
00033 
00042 FileManager::FileManager()
00043 {
00044 }
00045 
00049 FileManager::~FileManager()
00050 {
00051 }
00052 
00058 bool FileManager::saveFile( const DocLnk &f, const QByteArray &data )
00059 {
00060     QString fileName = f.file() + ".new";
00061     ensurePathExists( fileName );
00062     QFile file( fileName );
00063 
00064     //write data in temporary .new file
00065     if ( !file.open( IO_WriteOnly|IO_Raw ) )
00066     {
00067         qWarning( "open failed" );
00068         return FALSE;
00069     }
00070     int total_written = file.writeBlock( data );
00071     file.close();
00072     //check if every was written
00073     if ( total_written != int( data.size() ) || !f.writeLink() )
00074     {
00075         QFile::remove( fileName );
00076         return FALSE;
00077     }
00078     qDebug( "total written %d out of %d", total_written, data.size() );
00079 
00080     //rename temporary .new file in original filenam
00081     if ( !renameFile( fileName,  f.file() ) )
00082         QFile::remove( fileName);
00083     return TRUE;
00084 }
00085 
00093 bool FileManager::saveFile( const DocLnk &f, const QString &text )
00094 {
00095     QString fileName = f.file() + ".new";
00096     ensurePathExists( fileName );
00097     QFile file( fileName );
00098 
00099     //write data in temporary .new file
00100     if ( !file.open( IO_WriteOnly|IO_Raw ) )
00101     {
00102         qWarning( "open failed" );
00103         return FALSE;
00104     }
00105 
00106     QCString cstr = text.utf8();
00107     int total_written;
00108     total_written = file.writeBlock( cstr.data(), cstr.length() );
00109     file.close();
00110     if ( total_written != int( cstr.length()) || !f.writeLink() )
00111     {
00112         QFile::remove( fileName );
00113         return FALSE;
00114     }
00115 
00116     // okay now rename the file..
00117     if ( !renameFile( fileName,  f.file() ) )
00118         QFile::remove( fileName);
00119     return TRUE;
00120 }
00121 
00122 
00130 bool FileManager::loadFile( const DocLnk &f, QString &text )
00131 {
00132     QString fn = f.file();
00133     QFile fl( fn );
00134     if ( !fl.open( IO_ReadOnly ) )
00135         return FALSE;
00136     QTextStream ts( &fl );
00137 #if QT_VERSION <= 230 && defined(QT_NO_CODECS)
00138     // The below should work, but doesn't in Qt 2.3.0
00139     ts.setCodec( QTextCodec::codecForMib( 106 ) );
00140 #else
00141     ts.setEncoding( QTextStream::UnicodeUTF8 );
00142 #endif
00143     text = ts.read();
00144     fl.close();
00145     return TRUE;
00146 }
00147 
00148 
00154 bool FileManager::loadFile( const DocLnk &f, QByteArray &ba )
00155 {
00156     QString fn = f.file();
00157     QFile fl( fn );
00158     if ( !fl.open( IO_ReadOnly ) )
00159         return FALSE;
00160     ba.resize( fl.size() );
00161     if ( fl.size() > 0 )
00162         fl.readBlock( ba.data(), fl.size() );
00163     fl.close();
00164     return TRUE;
00165 }
00166 
00173 bool FileManager::copyFile( const AppLnk &src, const AppLnk &dest )
00174 {
00175     QFile srcFile( src.file() );
00176     if ( !srcFile.open( IO_ReadOnly ) )
00177         return FALSE;
00178 
00179     QString fileName = dest.file() + ".new";
00180 
00181     ensurePathExists( fileName );
00182 
00183     bool ok = TRUE;
00184     ok = copyFile( src.file(), fileName );
00185 
00186     if ( ok )
00187         ok = dest.writeLink();
00188 
00189     if ( ok )
00190     {
00191         // okay now rename the file...
00192         if ( !renameFile( fileName, dest.file() )  )
00193             // remove the tmp file, otherwise, it will just lay around...
00194             QFile::remove( fileName );
00195     }
00196     else
00197     {
00198         QFile::remove( fileName );
00199     }
00200     return ok;
00201 }
00202 
00203 bool FileManager::copyFile( const QString & src, const QString & dest )
00204 {
00205     //open read file
00206     QFile srcFile( src );
00207     if( !srcFile.open( IO_ReadOnly|IO_Raw) )
00208     {
00209         qWarning( "open read failed %s, %s", src.latin1(), dest.latin1() );
00210         return FALSE;
00211     }
00212 
00213     //open write file
00214     QFile destFile( dest );
00215     if( !destFile.open( IO_WriteOnly|IO_Raw ) )
00216     {
00217         qWarning( "open write failed %s, %s", src.latin1(), dest.latin1() );
00218         srcFile.close();
00219         return FALSE;
00220     }
00221 
00222     //copy content
00223     const int bufsize = 16384;
00224     char buffer[bufsize];
00225     bool ok = TRUE;
00226     int bytesRead = 0;
00227     while ( ok && !srcFile.atEnd() )
00228     {
00229         bytesRead = srcFile.readBlock( buffer, bufsize );
00230         if ( bytesRead < 0 )
00231             ok = FALSE;
00232         while ( ok && bytesRead > 0 )
00233         {
00234             int bytesWritten = destFile.writeBlock( buffer, bytesRead );
00235             if ( bytesWritten < 0 )
00236                 ok = FALSE;
00237             else
00238                 bytesRead -= bytesWritten;
00239         }
00240     }
00241     srcFile.close();
00242     destFile.close();
00243     // Set file permissions
00244     struct stat status;
00245     if( stat( QFile::encodeName( src ), &status ) == 0 )
00246     {
00247         chmod( QFile::encodeName( dest ), status.st_mode );
00248     }
00249     return ok;
00250 }
00251 
00252 
00253 bool FileManager::renameFile( const QString & src, const QString & dest )
00254 {
00255     if( rename( QFile::encodeName( src ), QFile::encodeName( dest ) ) == -1);
00256     {
00257         if ( errno != 2 && errno != 11 ) //ignore ENOENT and EAGAIN - bug in system?
00258         {
00259             qWarning( "problem renaming file %s to %s, errno: %d", src.latin1(), dest.latin1(), errno );
00260             return false;
00261         }
00262     }
00263     return true;
00264 }
00265 
00272 QIODevice* FileManager::openFile( const DocLnk& f )
00273 {
00274     QString fn = f.file();
00275     QFile* fl = new QFile( fn );
00276     if ( !fl->open( IO_ReadOnly ) )
00277     {
00278         delete fl;
00279         fl = 0;
00280     }
00281     return fl;
00282 }
00283 
00290 QIODevice* FileManager::saveFile( const DocLnk& f )
00291 {
00292     QString fn = f.file();
00293     ensurePathExists( fn );
00294     QFile* fl = new QFile( fn );
00295     if ( fl->open( IO_WriteOnly ) )
00296     {
00297         f.writeLink();
00298     }
00299     else
00300     {
00301         delete fl;
00302         fl = 0;
00303     }
00304     return fl;
00305 }
00306 
00311 bool FileManager::exists( const DocLnk &f )
00312 {
00313     return QFile::exists(f.file());
00314 }
00315 
00320 bool FileManager::ensurePathExists( const QString &fileName )
00321 {
00322     QDir directory = QFileInfo( fileName ).dir();
00323     if ( !directory.exists() )
00324     {
00325         if ( !directory.mkdir( directory.absPath() ) )
00326             return FALSE;
00327     }
00328     return TRUE;
00329 }

Generated on Sat Nov 5 16:16:35 2005 for OPIE by  doxygen 1.4.2