summaryrefslogtreecommitdiff
path: root/library/filemanager.cpp
Unidiff
Diffstat (limited to 'library/filemanager.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--library/filemanager.cpp274
1 files changed, 274 insertions, 0 deletions
diff --git a/library/filemanager.cpp b/library/filemanager.cpp
new file mode 100644
index 0000000..2b97846
--- a/dev/null
+++ b/library/filemanager.cpp
@@ -0,0 +1,274 @@
1/**********************************************************************
2** Copyright (C) 2000 Trolltech AS. All rights reserved.
3**
4** This file is part of Qtopia Environment.
5**
6** This file may be distributed and/or modified under the terms of the
7** GNU General Public License version 2 as published by the Free Software
8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file.
10**
11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13**
14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15**
16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you.
18**
19**********************************************************************/
20#include "filemanager.h"
21#include "applnk.h"
22
23#include <qdir.h>
24#include <qfile.h>
25#include <qfileinfo.h>
26#include <qtextstream.h>
27#include <qtextcodec.h>
28
29#include <errno.h>
30#include <stdlib.h>
31
32/*!
33 \class FileManager
34 \brief The FileManager class assists with AppLnk input/output.
35*/
36
37/*!
38 Constructs a FileManager.
39*/
40FileManager::FileManager()
41{
42}
43
44/*!
45 Destroys a FileManager.
46*/
47FileManager::~FileManager()
48{
49
50}
51
52/*!
53 Saves \a data as the document specified by \a f.
54
55 Returns whether the operation succeeded.
56*/
57bool FileManager::saveFile( const DocLnk &f, const QByteArray &data )
58{
59 QString fn = f.file() + ".new";
60 ensurePathExists( fn );
61 QFile fl( fn );
62 if ( !fl.open( IO_WriteOnly|IO_Raw ) )
63 return FALSE;
64 int total_written = fl.writeBlock( data );
65 fl.close();
66 if ( total_written != int(data.size()) || !f.writeLink() ) {
67 QFile::remove( fn );
68 return FALSE;
69 }
70 // else rename the file...
71 if ( ::rename( fn.latin1(), f.file().latin1() ) < 0 ) {
72 qWarning( "problem renaming file %s to %s, errno: %d", fn.latin1(),
73 f.file().latin1(), errno );
74 // remove the file...
75 QFile::remove( fn );
76 }
77 return TRUE;
78}
79
80/*!
81 Saves \a text as the document specified by \a f.
82
83 The text is saved in UTF8 format.
84
85 Returns whether the operation succeeded.
86*/
87bool FileManager::saveFile( const DocLnk &f, const QString &text )
88{
89 QString fn = f.file() + ".new";
90 ensurePathExists( fn );
91 QFile fl( fn );
92 if ( !fl.open( IO_WriteOnly|IO_Raw ) ) {
93 qDebug( "open failed: %s", fn.latin1() );
94 return FALSE;
95 }
96
97 QCString cstr = text.utf8();
98 int total_written;
99 total_written = fl.writeBlock( cstr.data(), cstr.length() );
100 fl.close();
101 if ( total_written != int(cstr.length()) || !f.writeLink() ) {
102 QFile::remove( fn );
103 return FALSE;
104 }
105 // okay now rename the file...
106 if ( ::rename( fn.latin1(), f.file().latin1() ) < 0 ) {
107 qWarning( "problem renaming file %s to %s, errno: %d", fn.latin1(),
108 f.file().latin1(), errno );
109 // remove the tmp file, otherwise, it will just lay around...
110 QFile::remove( fn.latin1() );
111 }
112 return TRUE;
113}
114
115
116/*!
117 Loads \a text from the document specified by \a f.
118
119 The text is required to be in UTF8 format.
120
121 Returns whether the operation succeeded.
122*/
123bool FileManager::loadFile( const DocLnk &f, QString &text )
124{
125 QString fn = f.file();
126 QFile fl( fn );
127 if ( !fl.open( IO_ReadOnly ) )
128 return FALSE;
129 QTextStream ts( &fl );
130#if QT_VERSION <= 230 && defined(QT_NO_CODECS)
131 // The below should work, but doesn't in Qt 2.3.0
132 ts.setCodec( QTextCodec::codecForMib( 106 ) );
133#else
134 ts.setEncoding( QTextStream::UnicodeUTF8 );
135#endif
136 text = ts.read();
137 fl.close();
138 return TRUE;
139}
140
141
142/*!
143 Loads \a ba from the document specified by \a f.
144
145 Returns whether the operation succeeded.
146*/
147bool FileManager::loadFile( const DocLnk &f, QByteArray &ba )
148{
149 QString fn = f.file();
150 QFile fl( fn );
151 if ( !fl.open( IO_ReadOnly ) )
152 return FALSE;
153 ba.resize( fl.size() );
154 if ( fl.size() > 0 )
155 fl.readBlock( ba.data(), fl.size() );
156 fl.close();
157 return TRUE;
158}
159
160/*!
161 Copies the document specified by \a src to the document specified
162 by \a dest.
163
164 Returns whether the operation succeeded.
165*/
166bool FileManager::copyFile( const AppLnk &src, const AppLnk &dest )
167{
168 QFile sf( src.file() );
169 if ( !sf.open( IO_ReadOnly ) )
170 return FALSE;
171
172 QString fn = dest.file() + ".new";
173 ensurePathExists( fn );
174 QFile df( fn );
175 if ( !df.open( IO_WriteOnly|IO_Raw ) )
176 return FALSE;
177
178 const int bufsize = 16384;
179 char buffer[bufsize];
180 bool ok = TRUE;
181 int bytesRead = 0;
182 while ( ok && !sf.atEnd() ) {
183 bytesRead = sf.readBlock( buffer, bufsize );
184 if ( bytesRead < 0 )
185 ok = FALSE;
186 while ( ok && bytesRead > 0 ) {
187 int bytesWritten = df.writeBlock( buffer, bytesRead );
188 if ( bytesWritten < 0 )
189 ok = FALSE;
190 else
191 bytesRead -= bytesWritten;
192 }
193 }
194
195 if ( ok )
196 ok = dest.writeLink();
197
198 if ( ok ) {
199 // okay now rename the file...
200 if ( ::rename( fn.latin1(), dest.file().latin1() ) < 0 ) {
201 qWarning( "problem renaming file %s to %s, errno: %d", fn.latin1(),
202 dest.file().latin1(), errno );
203 // remove the tmp file, otherwise, it will just lay around...
204 QFile::remove( fn.latin1() );
205 }
206 } else {
207 QFile::remove( fn.latin1() );
208 }
209
210 return ok;
211}
212
213/*!
214 Opens the document specified by \a f as a readable QIODevice.
215 The caller must delete the return value.
216
217 Returns 0 if the operation fails.
218*/
219QIODevice* FileManager::openFile( const DocLnk& f )
220{
221 QString fn = f.file();
222 QFile* fl = new QFile( fn );
223 if ( !fl->open( IO_ReadOnly ) ) {
224 delete fl;
225 fl = 0;
226 }
227 return fl;
228}
229
230/*!
231 Opens the document specified by \a f as a writable QIODevice.
232 The caller must delete the return value.
233
234 Returns 0 if the operation fails.
235*/
236QIODevice* FileManager::saveFile( const DocLnk& f )
237{
238 QString fn = f.file();
239 ensurePathExists( fn );
240 QFile* fl = new QFile( fn );
241 if ( fl->open( IO_WriteOnly ) ) {
242 f.writeLink();
243 } else {
244 delete fl;
245 fl = 0;
246 }
247 return fl;
248}
249
250/*!
251 Returns whether the document specified by \a f current exists
252 as a file on disk.
253*/
254bool FileManager::exists( const DocLnk &f )
255{
256 return QFile::exists(f.file());
257}
258
259
260/*!
261 Ensures that the path \a fn exists, by creating required directories.
262 Returns TRUE if successful.
263*/
264bool FileManager::ensurePathExists( const QString &fn )
265{
266 QFileInfo fi(fn);
267 fi.setFile( fi.dirPath(TRUE) );
268 if ( !fi.exists() ) {
269 if ( system(("mkdir -p "+fi.filePath())) )
270 return FALSE;
271 }
272
273 return TRUE;
274}