Rev 18013 | Rev 19917 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
11605 | jghali | 1 | /* |
2 | For general Scribus (>=1.3.2) copyright and licensing information please refer |
||
3 | to the COPYING file provided with the program. Following this notice may exist |
||
4 | a copyright and/or license notice that predates the release of Scribus 1.3.2 |
||
5 | for which a new license (GPL+exception) is in place. |
||
6 | */ |
||
7 | |||
8 | #include "util_file.h" |
||
9 | |||
14539 | jghali | 10 | #ifdef _MSC_VER |
11 | # include <sys/utime.h> |
||
12 | #else |
||
13 | # include <utime.h> |
||
14 | #endif |
||
15 | |||
11605 | jghali | 16 | #include <QDataStream> |
15434 | craig | 17 | #include <QDir> |
11605 | jghali | 18 | #include <QFile> |
15434 | craig | 19 | #include <QFileInfo> |
11605 | jghali | 20 | #include <QString> |
15434 | craig | 21 | #include <QProcess> |
12219 | jghali | 22 | #include <QTemporaryFile> |
23 | |||
11605 | jghali | 24 | #include "scstreamfilter.h" |
25 | |||
12219 | jghali | 26 | bool copyData(QIODevice& src, QIODevice& dest) |
27 | { |
||
28 | bool success = false; |
||
29 | if ((src.openMode() & QIODevice::ReadOnly) == 0) |
||
30 | return false; |
||
31 | if ((dest.openMode() & QIODevice::WriteOnly) == 0) |
||
32 | return false; |
||
33 | QByteArray bb( 65536, ' ' ); |
||
34 | if (bb.size() > 0) // Check for memory allocation failure |
||
35 | { |
||
36 | qint64 byteswritten; |
||
37 | qint64 bytesread = src.read( bb.data(), bb.size() ); |
||
38 | success = (bytesread > 0); |
||
39 | while (bytesread > 0) |
||
40 | { |
||
41 | byteswritten = dest.write( bb.data(), bytesread ); |
||
42 | success &= (bytesread == byteswritten); |
||
43 | bytesread = src.read( bb.data(), bb.size() ); |
||
44 | } |
||
45 | } |
||
46 | return success; |
||
47 | } |
||
48 | |||
11605 | jghali | 49 | bool copyFile(const QString& source, const QString& target) |
50 | { |
||
12219 | jghali | 51 | bool success = true; |
52 | if ((source.isEmpty()) || (target.isEmpty())) |
||
11605 | jghali | 53 | return false; |
54 | if (source == target) |
||
55 | return false; |
||
56 | QFile s(source); |
||
57 | if (!s.exists()) |
||
58 | return false; |
||
59 | QFile t(target); |
||
60 | if (s.open(QIODevice::ReadOnly)) |
||
61 | { |
||
62 | if (t.open(QIODevice::WriteOnly)) |
||
63 | { |
||
12219 | jghali | 64 | success = copyData(s, t); |
65 | success &= (s.error() == QFile::NoError && t.error() == QFile::NoError); |
||
11605 | jghali | 66 | t.close(); |
67 | } |
||
68 | s.close(); |
||
69 | } |
||
12219 | jghali | 70 | return success; |
11605 | jghali | 71 | } |
72 | |||
12219 | jghali | 73 | bool copyFileAtomic(const QString& source, const QString& target) |
74 | { |
||
75 | bool success = false; |
||
76 | if ((source.isEmpty()) || (target.isEmpty())) |
||
77 | return false; |
||
78 | if (source == target) |
||
79 | return false; |
||
80 | QFile srcFile(source); |
||
81 | QString tempFileName; |
||
13700 | jghali | 82 | QTemporaryFile* tempFile = new QTemporaryFile(target + "_XXXXXX"); |
83 | if (!tempFile) |
||
84 | return false; |
||
12219 | jghali | 85 | if (srcFile.open(QIODevice::ReadOnly)) |
86 | { |
||
13700 | jghali | 87 | if (tempFile->open()) |
12219 | jghali | 88 | { |
13700 | jghali | 89 | tempFileName = tempFile->fileName(); |
90 | success = copyData(srcFile, *tempFile); |
||
91 | success &= (srcFile.error() == QFile::NoError && tempFile->error() == QFile::NoError); |
||
92 | tempFile->close(); |
||
12219 | jghali | 93 | } |
94 | srcFile.close(); |
||
95 | } |
||
96 | if (success) |
||
97 | { |
||
98 | if (QFile::exists(target)) |
||
99 | success = QFile::remove(target); |
||
100 | if (success) |
||
101 | { |
||
13700 | jghali | 102 | // We delete temporary file now to force file close |
103 | // QTemporaryFile::close() do not really close file |
||
104 | tempFile->setAutoRemove(false); |
||
105 | delete tempFile; |
||
106 | tempFile = NULL; |
||
12219 | jghali | 107 | success = QFile::rename(tempFileName, target); |
108 | } |
||
109 | } |
||
13700 | jghali | 110 | if (tempFile) |
111 | delete tempFile; |
||
12219 | jghali | 112 | return success; |
113 | } |
||
114 | |||
11605 | jghali | 115 | bool copyFileToFilter(const QString& source, ScStreamFilter& target) |
116 | { |
||
117 | bool copySucceed = true; |
||
12231 | cbradney | 118 | int bytesread = 0; |
11605 | jghali | 119 | if (source.isEmpty()) |
120 | return false; |
||
121 | if (!QFile::exists(source)) |
||
122 | return false; |
||
123 | QFile s(source); |
||
124 | QByteArray bb( 65536, ' ' ); |
||
125 | if (bb.size() <= 0) // Check for memory allocation failure |
||
126 | return false; |
||
127 | if (s.open(QIODevice::ReadOnly)) |
||
128 | { |
||
129 | bytesread = s.read( bb.data(), bb.size() ); |
||
130 | while (bytesread > 0) |
||
131 | { |
||
132 | copySucceed &= target.writeData(bb.data(), bytesread); |
||
133 | bytesread = s.read( bb.data(), bb.size() ); |
||
134 | } |
||
135 | copySucceed &= (s.error() == QFile::NoError); |
||
136 | s.close(); |
||
137 | } |
||
138 | return copySucceed; |
||
139 | } |
||
140 | |||
141 | bool copyFileToStream(const QString& source, QDataStream& target) |
||
142 | { |
||
143 | bool copySucceed = true; |
||
144 | int bytesread, byteswrite; |
||
145 | if (source.isEmpty()) |
||
146 | return false; |
||
147 | if (!QFile::exists(source)) |
||
148 | return false; |
||
149 | if (!target.device()->isOpen() || !target.device()->isWritable()) |
||
150 | return false; |
||
151 | QFile s(source); |
||
152 | QByteArray bb( 65536, ' ' ); |
||
153 | if (bb.size() <= 0) // Check for memory allocation failure |
||
154 | return false; |
||
155 | if (s.open(QIODevice::ReadOnly)) |
||
156 | { |
||
157 | bytesread = s.read( bb.data(), bb.size() ); |
||
158 | while (bytesread > 0) |
||
159 | { |
||
160 | byteswrite = target.writeRawData(bb.data(), bytesread); |
||
161 | copySucceed &= (byteswrite == bytesread); |
||
162 | bytesread = s.read( bb.data(), bb.size() ); |
||
163 | } |
||
164 | copySucceed &= (s.error() == QFile::NoError); |
||
165 | s.close(); |
||
166 | } |
||
167 | return copySucceed; |
||
168 | } |
||
169 | |||
170 | bool moveFile(const QString& source, const QString& target) |
||
171 | { |
||
172 | if (source.isEmpty() || target.isEmpty()) |
||
173 | return false; |
||
174 | if (source == target) |
||
175 | return false; |
||
176 | bool moveSucceed = copyFile(source, target); |
||
177 | if (moveSucceed) |
||
178 | moveSucceed &= QFile::remove(source); |
||
179 | return moveSucceed; |
||
180 | } |
||
14539 | jghali | 181 | |
182 | bool touchFile(const QString& file) |
||
183 | { |
||
184 | #if defined(_WIN32) && defined(HAVE_UNICODE) |
||
185 | return _wutime((const wchar_t*) file.utf16(), NULL) == 0; |
||
186 | #else |
||
187 | QByteArray fname = file.toLocal8Bit(); |
||
188 | return utime(fname.data(), NULL) == 0; |
||
189 | #endif |
||
190 | } |
||
15434 | craig | 191 | |
192 | |||
193 | bool fileInPath(const QString& filename) |
||
194 | { |
||
195 | if (filename.isEmpty()) |
||
196 | return false; |
||
197 | QString file = filename.split(' ', QString::SkipEmptyParts).at(0); //Ignore parameters |
||
19108 | jghali | 198 | #if defined(Q_OS_WIN32) |
199 | if (QFileInfo(file).suffix().isEmpty()) |
||
200 | file += ".exe"; |
||
201 | #endif |
||
15434 | craig | 202 | |
203 | file = QDir::fromNativeSeparators(file); |
||
204 | if (file.indexOf('/') >= 0) |
||
205 | { |
||
206 | //Looks like an absolute path |
||
207 | QFileInfo info(file); |
||
208 | return info.exists(); |
||
209 | } |
||
210 | |||
211 | //Get $PATH |
||
212 | QStringList env = QProcess::systemEnvironment(); |
||
213 | QString path; |
||
214 | foreach (QString line, env) |
||
215 | { |
||
216 | if (line.indexOf("PATH") == 0) |
||
217 | { |
||
218 | path = line.mid(5); //Strip "PATH=" |
||
219 | break; |
||
220 | } |
||
221 | } |
||
222 | QStringList splitpath; |
||
18013 | craig | 223 | #if defined(Q_OS_WIN32) || defined (Q_OS_OS2) |
15434 | craig | 224 | splitpath = path.split(';', QString::SkipEmptyParts); |
225 | #else |
||
226 | splitpath = path.split(':', QString::SkipEmptyParts); |
||
227 | #endif |
||
228 | foreach (QString dir, splitpath) |
||
229 | { |
||
230 | QFileInfo info(dir, file); |
||
231 | if (info.exists()) |
||
232 | return true; |
||
233 | } |
||
234 | return false; |
||
235 | } |