insilmaril@606
|
1 |
#include <QDir>
|
insilmaril@408
|
2 |
#include <QMessageBox>
|
insilmaril@366
|
3 |
#include <QPixmap>
|
insilmaril@366
|
4 |
#include <QLabel>
|
insilmaril@366
|
5 |
#include <QTextStream>
|
insilmaril@195
|
6 |
#include <iostream>
|
insilmaril@195
|
7 |
|
insilmaril@195
|
8 |
#include "file.h"
|
insilmaril@195
|
9 |
#include "process.h"
|
insilmaril@195
|
10 |
|
insilmaril@606
|
11 |
// Avoid full inclusion of io.h by just defining mktemp's prototype here.
|
insilmaril@606
|
12 |
#if defined(Q_OS_WIN32)
|
insilmaril@606
|
13 |
extern "C" char *_mktemp(char *fmt);
|
insilmaril@606
|
14 |
#include <windows.h>
|
insilmaril@606
|
15 |
#endif
|
insilmaril@195
|
16 |
|
insilmaril@195
|
17 |
QString maskPath(QString p)
|
insilmaril@195
|
18 |
{
|
insilmaril@195
|
19 |
// Change " " to "\ " to enable blanks in filenames
|
insilmaril@195
|
20 |
p=p.replace(QChar('&'),"\\&");
|
insilmaril@195
|
21 |
return p.replace(QChar(' '),"\\ ");
|
insilmaril@195
|
22 |
}
|
insilmaril@195
|
23 |
|
insilmaril@195
|
24 |
QString convertToRel (const QString &src, const QString &dst)
|
insilmaril@195
|
25 |
{
|
insilmaril@195
|
26 |
QString s=src;
|
insilmaril@195
|
27 |
QString d=dst;
|
insilmaril@195
|
28 |
int i;
|
insilmaril@195
|
29 |
|
insilmaril@195
|
30 |
if (s==d)
|
insilmaril@195
|
31 |
{
|
insilmaril@195
|
32 |
// Special case, we just need the name of the file,
|
insilmaril@195
|
33 |
// not the complete path
|
insilmaril@195
|
34 |
i=d.findRev ("/");
|
insilmaril@195
|
35 |
d=d.right (d.length()-i-1);
|
insilmaril@195
|
36 |
} else
|
insilmaril@195
|
37 |
{
|
insilmaril@195
|
38 |
// Find relative path from src to dst
|
insilmaril@195
|
39 |
|
insilmaril@195
|
40 |
// Remove the first "/"
|
insilmaril@195
|
41 |
if (s.section ("/",0,0).isEmpty())
|
insilmaril@195
|
42 |
{
|
insilmaril@195
|
43 |
s=s.right (s.length()-1);
|
insilmaril@195
|
44 |
d=d.right (d.length()-1);
|
insilmaril@195
|
45 |
}
|
insilmaril@195
|
46 |
|
insilmaril@195
|
47 |
// remove identical left parts
|
insilmaril@195
|
48 |
while (s.section("/",0,0) == d.section("/",0,0) )
|
insilmaril@195
|
49 |
{
|
insilmaril@195
|
50 |
i=s.find ("/");
|
insilmaril@195
|
51 |
s=s.right (s.length()-i-1);
|
insilmaril@195
|
52 |
d=d.right (d.length()-i-1);
|
insilmaril@195
|
53 |
}
|
insilmaril@195
|
54 |
|
insilmaril@255
|
55 |
// Now take care of paths where we have to go back first
|
insilmaril@366
|
56 |
int srcsep=s.count("/");
|
insilmaril@366
|
57 |
int dstsep=d.count("/");
|
insilmaril@255
|
58 |
if (srcsep <= dstsep )
|
insilmaril@195
|
59 |
{
|
insilmaril@195
|
60 |
// find path to go up first and then back to dst
|
insilmaril@195
|
61 |
i=1;
|
insilmaril@195
|
62 |
while (i<=srcsep)
|
insilmaril@195
|
63 |
{
|
insilmaril@195
|
64 |
d="../"+d;
|
insilmaril@195
|
65 |
i++;
|
insilmaril@195
|
66 |
}
|
insilmaril@195
|
67 |
}
|
insilmaril@195
|
68 |
}
|
insilmaril@195
|
69 |
return d;
|
insilmaril@195
|
70 |
}
|
insilmaril@195
|
71 |
|
insilmaril@521
|
72 |
#include <QFileDialog>
|
insilmaril@521
|
73 |
extern QString vymName;
|
insilmaril@521
|
74 |
extern QDir lastFileDir;
|
insilmaril@521
|
75 |
|
insilmaril@521
|
76 |
QString browseDirectory (QWidget *parent,const QString &caption)
|
insilmaril@521
|
77 |
{
|
insilmaril@521
|
78 |
QFileDialog fd(parent,caption);
|
insilmaril@521
|
79 |
fd.setMode (QFileDialog::DirectoryOnly);
|
insilmaril@521
|
80 |
fd.setCaption(vymName+ " - "+caption);
|
insilmaril@521
|
81 |
fd.setDir (lastFileDir);
|
insilmaril@521
|
82 |
fd.show();
|
insilmaril@521
|
83 |
|
insilmaril@521
|
84 |
if ( fd.exec() == QDialog::Accepted )
|
insilmaril@521
|
85 |
return fd.selectedFile();
|
insilmaril@521
|
86 |
else
|
insilmaril@521
|
87 |
return "";
|
insilmaril@521
|
88 |
}
|
insilmaril@521
|
89 |
|
insilmaril@521
|
90 |
|
insilmaril@521
|
91 |
|
insilmaril@521
|
92 |
bool reallyWriteDirectory(const QString &dir)
|
insilmaril@521
|
93 |
{
|
insilmaril@521
|
94 |
QStringList eList = QDir(dir).entryList();
|
insilmaril@521
|
95 |
if (eList.first() ==".") eList.pop_front(); // remove "."
|
insilmaril@521
|
96 |
if (eList.first() =="..") eList.pop_front(); // remove "."
|
insilmaril@521
|
97 |
if (!eList.isEmpty())
|
insilmaril@521
|
98 |
{
|
insilmaril@521
|
99 |
QMessageBox mb( vymName,
|
insilmaril@521
|
100 |
QObject::tr("The directory %1 is not empty.\nDo you risk to overwrite its contents?","write directory").arg(dir),
|
insilmaril@521
|
101 |
QMessageBox::Warning,
|
insilmaril@521
|
102 |
QMessageBox::Yes ,
|
insilmaril@521
|
103 |
QMessageBox::Cancel | QMessageBox::Default,
|
insilmaril@606
|
104 |
QMessageBox::NoButton );
|
insilmaril@521
|
105 |
|
insilmaril@521
|
106 |
mb.setButtonText( QMessageBox::Yes, QObject::tr("Overwrite") );
|
insilmaril@521
|
107 |
mb.setButtonText( QMessageBox::No, QObject::tr("Cancel"));
|
insilmaril@521
|
108 |
switch( mb.exec() )
|
insilmaril@521
|
109 |
{
|
insilmaril@521
|
110 |
case QMessageBox::Yes:
|
insilmaril@521
|
111 |
// save
|
insilmaril@521
|
112 |
return true;
|
insilmaril@521
|
113 |
case QMessageBox::Cancel:
|
insilmaril@521
|
114 |
// do nothing
|
insilmaril@521
|
115 |
return false;
|
insilmaril@521
|
116 |
}
|
insilmaril@521
|
117 |
}
|
insilmaril@521
|
118 |
return true;
|
insilmaril@521
|
119 |
}
|
insilmaril@521
|
120 |
|
insilmaril@606
|
121 |
QString makeTmpDir (bool &ok, QString prefix)
|
insilmaril@606
|
122 |
{
|
insilmaril@606
|
123 |
bool b;
|
insilmaril@606
|
124 |
QString path=makeUniqueDir (b,QDir::tempPath()+"/"+prefix+"-XXXXXX");
|
insilmaril@606
|
125 |
ok=b;
|
insilmaril@606
|
126 |
return path;
|
insilmaril@606
|
127 |
}
|
insilmaril@606
|
128 |
|
insilmaril@606
|
129 |
bool isInTmpDir(QString fn)
|
insilmaril@606
|
130 |
{
|
insilmaril@606
|
131 |
QString temp=QDir::tempPath();
|
insilmaril@606
|
132 |
int l=temp.length();
|
insilmaril@606
|
133 |
return fn.left(l)==temp;
|
insilmaril@606
|
134 |
}
|
insilmaril@606
|
135 |
|
insilmaril@366
|
136 |
QString makeUniqueDir (bool &ok,QString s)
|
insilmaril@195
|
137 |
{
|
insilmaril@606
|
138 |
// Create unique directory e.g. for s="/tmp/vym-XXXXXX"
|
insilmaril@195
|
139 |
|
insilmaril@606
|
140 |
// Convert Separators
|
insilmaril@606
|
141 |
s=QDir::convertSeparators(s);
|
insilmaril@606
|
142 |
|
insilmaril@606
|
143 |
// Convert QString to string
|
insilmaril@366
|
144 |
ok=true;
|
insilmaril@195
|
145 |
char *p;
|
insilmaril@195
|
146 |
int bytes=s.length();
|
insilmaril@195
|
147 |
p=(char*) malloc (bytes+1);
|
insilmaril@195
|
148 |
int i;
|
insilmaril@195
|
149 |
for (i=0;i<bytes;i++)
|
insilmaril@195
|
150 |
p[i]=s.at(i).latin1();
|
insilmaril@195
|
151 |
p[bytes]=0;
|
insilmaril@606
|
152 |
|
insilmaril@606
|
153 |
#if defined(Q_OS_WIN32)
|
insilmaril@606
|
154 |
// There's no mkdtemp on VCEE.
|
insilmaril@606
|
155 |
QString r=_mktemp(p);
|
insilmaril@606
|
156 |
#else
|
insilmaril@195
|
157 |
QString r=mkdtemp (p);
|
insilmaril@606
|
158 |
#endif
|
insilmaril@366
|
159 |
if (r.isEmpty()) ok=false;
|
insilmaril@195
|
160 |
free (p);
|
insilmaril@195
|
161 |
return r;
|
insilmaril@195
|
162 |
}
|
insilmaril@195
|
163 |
|
insilmaril@195
|
164 |
void removeDir(QDir d)
|
insilmaril@195
|
165 |
{
|
insilmaril@606
|
166 |
// This check should_ not be necessary, but proved to be useful ;-)
|
insilmaril@606
|
167 |
if (!isInTmpDir(d.path()))
|
insilmaril@195
|
168 |
{
|
insilmaril@195
|
169 |
qWarning ("file.cpp::removeDir should remove "+d.path()+" - aborted.");
|
insilmaril@195
|
170 |
return;
|
insilmaril@195
|
171 |
}
|
insilmaril@195
|
172 |
|
insilmaril@195
|
173 |
// Traverse directories
|
insilmaril@195
|
174 |
d.setFilter( QDir::Dirs| QDir::Hidden | QDir::NoSymLinks );
|
insilmaril@366
|
175 |
QFileInfoList list = d.entryInfoList();
|
insilmaril@366
|
176 |
QFileInfo fi;
|
insilmaril@195
|
177 |
|
insilmaril@366
|
178 |
for (int i = 0; i < list.size(); ++i)
|
insilmaril@195
|
179 |
{
|
insilmaril@366
|
180 |
fi=list.at(i);
|
insilmaril@366
|
181 |
if (fi.fileName() != "." && fi.fileName() != ".." )
|
insilmaril@195
|
182 |
{
|
insilmaril@366
|
183 |
if ( !d.cd(fi.fileName()) )
|
insilmaril@366
|
184 |
qWarning ("removeDir() cannot find the directory "+fi.fileName());
|
insilmaril@195
|
185 |
else
|
insilmaril@195
|
186 |
{
|
insilmaril@195
|
187 |
// Recursively remove subdirs
|
insilmaril@195
|
188 |
removeDir (d);
|
insilmaril@195
|
189 |
d.cdUp();
|
insilmaril@195
|
190 |
}
|
insilmaril@195
|
191 |
}
|
insilmaril@366
|
192 |
}
|
insilmaril@366
|
193 |
|
insilmaril@195
|
194 |
// Traverse files
|
insilmaril@195
|
195 |
d.setFilter( QDir::Files| QDir::Hidden | QDir::NoSymLinks );
|
insilmaril@366
|
196 |
list = d.entryInfoList();
|
insilmaril@195
|
197 |
|
insilmaril@366
|
198 |
for (int i = 0; i < list.size(); ++i)
|
insilmaril@195
|
199 |
{
|
insilmaril@366
|
200 |
fi=list.at(i);
|
insilmaril@366
|
201 |
QFile (fi.filePath()).remove();
|
insilmaril@195
|
202 |
}
|
insilmaril@195
|
203 |
|
insilmaril@195
|
204 |
if (!d.rmdir(d.path()))
|
insilmaril@195
|
205 |
qWarning ("removeDir("+d.path()+") failed!");
|
insilmaril@195
|
206 |
}
|
insilmaril@195
|
207 |
|
insilmaril@502
|
208 |
void copyDir (QDir src, QDir dst)
|
insilmaril@502
|
209 |
{
|
insilmaril@502
|
210 |
system ("cp -r "+src.path()+"/* "+dst.path());
|
insilmaril@502
|
211 |
|
insilmaril@502
|
212 |
/*
|
insilmaril@502
|
213 |
ErrorCode err=success;
|
insilmaril@502
|
214 |
|
insilmaril@502
|
215 |
Process *cpProc=new Process ();
|
insilmaril@502
|
216 |
QStringList args;
|
insilmaril@502
|
217 |
cpProc->setWorkingDirectory (src.path());
|
insilmaril@502
|
218 |
args <<"-r";
|
insilmaril@502
|
219 |
args <<src.path();
|
insilmaril@502
|
220 |
args <<dst.path();
|
insilmaril@502
|
221 |
|
insilmaril@502
|
222 |
cpProc->start ("cp",args);
|
insilmaril@502
|
223 |
if (!cpProc->waitForStarted() )
|
insilmaril@502
|
224 |
{
|
insilmaril@502
|
225 |
// zip could not be started
|
insilmaril@502
|
226 |
QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
|
insilmaril@502
|
227 |
QObject::tr("Couldn't start zip to compress data."));
|
insilmaril@502
|
228 |
err=aborted;
|
insilmaril@502
|
229 |
} else
|
insilmaril@502
|
230 |
{
|
insilmaril@502
|
231 |
// zip could be started
|
insilmaril@502
|
232 |
cpProc->waitForFinished();
|
insilmaril@502
|
233 |
if (cpProc->exitStatus()!=QProcess::NormalExit )
|
insilmaril@502
|
234 |
{
|
insilmaril@502
|
235 |
QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
|
insilmaril@502
|
236 |
QObject::tr("cp didn't exit normally")+
|
insilmaril@502
|
237 |
"\n" + cpProc->getErrout());
|
insilmaril@502
|
238 |
err=aborted;
|
insilmaril@502
|
239 |
} else
|
insilmaril@502
|
240 |
{
|
insilmaril@502
|
241 |
if (cpProc->exitCode()>0)
|
insilmaril@502
|
242 |
{
|
insilmaril@502
|
243 |
QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
|
insilmaril@502
|
244 |
QString("cp exit code: %1").arg(cpProc->exitCode() )+
|
insilmaril@502
|
245 |
"\n" + cpProc->getErrout() );
|
insilmaril@502
|
246 |
err=aborted;
|
insilmaril@502
|
247 |
}
|
insilmaril@502
|
248 |
}
|
insilmaril@502
|
249 |
} // cp could be started
|
insilmaril@502
|
250 |
*/
|
insilmaril@502
|
251 |
}
|
insilmaril@502
|
252 |
|
insilmaril@195
|
253 |
void makeSubDirs (const QString &s)
|
insilmaril@195
|
254 |
{
|
insilmaril@195
|
255 |
QDir d(s);
|
insilmaril@195
|
256 |
d.mkdir(s);
|
insilmaril@195
|
257 |
d.mkdir ("images");
|
insilmaril@195
|
258 |
d.mkdir ("flags");
|
insilmaril@195
|
259 |
}
|
insilmaril@195
|
260 |
|
insilmaril@205
|
261 |
ErrorCode zipDir (const QDir &zipDir, const QString &zipName)
|
insilmaril@195
|
262 |
{
|
insilmaril@205
|
263 |
ErrorCode err=success;
|
insilmaril@195
|
264 |
|
insilmaril@195
|
265 |
// zip the temporary directory
|
insilmaril@408
|
266 |
QStringList args;
|
insilmaril@195
|
267 |
Process *zipProc=new Process ();
|
insilmaril@408
|
268 |
zipProc->setWorkingDirectory (zipDir.path());
|
insilmaril@408
|
269 |
args <<"-r";
|
insilmaril@408
|
270 |
args <<zipName;
|
insilmaril@408
|
271 |
args <<".";
|
insilmaril@195
|
272 |
|
insilmaril@408
|
273 |
zipProc->start ("zip",args);
|
insilmaril@408
|
274 |
if (!zipProc->waitForStarted() )
|
insilmaril@195
|
275 |
{
|
insilmaril@195
|
276 |
// zip could not be started
|
insilmaril@195
|
277 |
QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
|
insilmaril@195
|
278 |
QObject::tr("Couldn't start zip to compress data."));
|
insilmaril@195
|
279 |
err=aborted;
|
insilmaril@195
|
280 |
} else
|
insilmaril@195
|
281 |
{
|
insilmaril@195
|
282 |
// zip could be started
|
insilmaril@408
|
283 |
zipProc->waitForFinished();
|
insilmaril@408
|
284 |
if (zipProc->exitStatus()!=QProcess::NormalExit )
|
insilmaril@195
|
285 |
{
|
insilmaril@195
|
286 |
QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
|
insilmaril@195
|
287 |
QObject::tr("zip didn't exit normally")+
|
insilmaril@195
|
288 |
"\n" + zipProc->getErrout());
|
insilmaril@195
|
289 |
err=aborted;
|
insilmaril@195
|
290 |
} else
|
insilmaril@195
|
291 |
{
|
insilmaril@408
|
292 |
if (zipProc->exitCode()>0)
|
insilmaril@195
|
293 |
{
|
insilmaril@195
|
294 |
QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
|
insilmaril@408
|
295 |
QString("zip exit code: %1").arg(zipProc->exitCode() )+
|
insilmaril@195
|
296 |
"\n" + zipProc->getErrout() );
|
insilmaril@195
|
297 |
err=aborted;
|
insilmaril@195
|
298 |
}
|
insilmaril@195
|
299 |
}
|
insilmaril@195
|
300 |
} // zip could be started
|
insilmaril@195
|
301 |
return err;
|
insilmaril@195
|
302 |
}
|
insilmaril@195
|
303 |
|
insilmaril@205
|
304 |
ErrorCode unzipDir (const QDir &zipDir, const QString &zipName)
|
insilmaril@195
|
305 |
{
|
insilmaril@205
|
306 |
ErrorCode err=success;
|
insilmaril@195
|
307 |
|
insilmaril@195
|
308 |
// Try to unzip file
|
insilmaril@606
|
309 |
#if !defined(Q_OS_WIN32)
|
insilmaril@408
|
310 |
QStringList args;
|
insilmaril@195
|
311 |
Process *zipProc=new Process ();
|
insilmaril@408
|
312 |
zipProc->setWorkingDirectory (zipDir.path());
|
insilmaril@408
|
313 |
args << "-o"; // overwrite existing files!
|
insilmaril@408
|
314 |
args << zipName ;
|
insilmaril@408
|
315 |
args << "-d";
|
insilmaril@408
|
316 |
args << zipDir.path();
|
insilmaril@195
|
317 |
|
insilmaril@408
|
318 |
zipProc->start ("unzip",args);
|
insilmaril@408
|
319 |
if (!zipProc->waitForStarted() )
|
insilmaril@195
|
320 |
{
|
insilmaril@195
|
321 |
QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
|
insilmaril@195
|
322 |
QObject::tr("Couldn't start unzip to decompress data."));
|
insilmaril@195
|
323 |
err=aborted;
|
insilmaril@195
|
324 |
|
insilmaril@195
|
325 |
} else
|
insilmaril@195
|
326 |
{
|
insilmaril@408
|
327 |
zipProc->waitForFinished();
|
insilmaril@408
|
328 |
if (zipProc->exitStatus()!=QProcess::NormalExit )
|
insilmaril@195
|
329 |
{
|
insilmaril@195
|
330 |
QMessageBox::critical( 0,QObject::tr( "Critical Error" ),
|
insilmaril@195
|
331 |
QObject::tr("unzip didn't exit normally") +
|
insilmaril@195
|
332 |
zipProc->getErrout() );
|
insilmaril@195
|
333 |
err=aborted;
|
insilmaril@195
|
334 |
} else
|
insilmaril@195
|
335 |
{
|
insilmaril@408
|
336 |
if (zipProc->exitCode()>0)
|
insilmaril@195
|
337 |
{
|
insilmaril@428
|
338 |
if (zipProc->exitCode()==9)
|
insilmaril@195
|
339 |
// no zipped file, but maybe .xml or old version? Try again.
|
insilmaril@195
|
340 |
err=nozip;
|
insilmaril@195
|
341 |
else
|
insilmaril@195
|
342 |
{
|
insilmaril@195
|
343 |
QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
|
insilmaril@408
|
344 |
QString("unzip exit code: %1").arg(zipProc->exitCode() ) +
|
insilmaril@195
|
345 |
zipProc->getErrout() );
|
insilmaril@195
|
346 |
err=aborted;
|
insilmaril@195
|
347 |
}
|
insilmaril@195
|
348 |
}
|
insilmaril@195
|
349 |
}
|
insilmaril@195
|
350 |
}
|
insilmaril@606
|
351 |
#else
|
insilmaril@606
|
352 |
// Do this process creation using Win32 API.
|
insilmaril@606
|
353 |
//! Create process.
|
insilmaril@606
|
354 |
PROCESS_INFORMATION piProcInfo;
|
insilmaril@606
|
355 |
STARTUPINFO siStartInfo;
|
insilmaril@606
|
356 |
|
insilmaril@606
|
357 |
// Initialize members of the PROCESS_INFORMATION structure.
|
insilmaril@606
|
358 |
::ZeroMemory( &piProcInfo, sizeof(PROCESS_INFORMATION) );
|
insilmaril@606
|
359 |
|
insilmaril@606
|
360 |
// Set up members of the STARTUPINFO structure.
|
insilmaril@606
|
361 |
::ZeroMemory( &siStartInfo, sizeof(STARTUPINFO) );
|
insilmaril@606
|
362 |
siStartInfo.cb = sizeof(STARTUPINFO);
|
insilmaril@606
|
363 |
|
insilmaril@606
|
364 |
// Create command line.
|
insilmaril@606
|
365 |
QString argv("unzip -o ");
|
insilmaril@606
|
366 |
argv.append(QDir::convertSeparators(zipName));
|
insilmaril@606
|
367 |
argv.append(" -d ");
|
insilmaril@606
|
368 |
argv.append(QDir::convertSeparators(zipDir.path()));
|
insilmaril@606
|
369 |
|
insilmaril@606
|
370 |
// Create the child process.
|
insilmaril@606
|
371 |
if( !::CreateProcess(NULL,
|
insilmaril@606
|
372 |
(LPWSTR)argv.unicode(), // command line
|
insilmaril@606
|
373 |
NULL, // process security attributes
|
insilmaril@606
|
374 |
NULL, // primary thread security attributes
|
insilmaril@606
|
375 |
TRUE, // handles are inherited
|
insilmaril@606
|
376 |
0, // creation flags
|
insilmaril@606
|
377 |
NULL, // use parent's environment
|
insilmaril@606
|
378 |
NULL, // use parent's current directory
|
insilmaril@606
|
379 |
&siStartInfo, // STARTUPINFO pointer
|
insilmaril@606
|
380 |
&piProcInfo) ) // receives PROCESS_INFORMATION
|
insilmaril@606
|
381 |
{
|
insilmaril@606
|
382 |
err = aborted;
|
insilmaril@606
|
383 |
}
|
insilmaril@606
|
384 |
else
|
insilmaril@606
|
385 |
{
|
insilmaril@606
|
386 |
// Wait for it to finish.
|
insilmaril@606
|
387 |
::WaitForSingleObject( piProcInfo.hProcess, 10000 );
|
insilmaril@606
|
388 |
}
|
insilmaril@606
|
389 |
#endif
|
insilmaril@195
|
390 |
return err;
|
insilmaril@195
|
391 |
}
|
insilmaril@195
|
392 |
|
insilmaril@195
|
393 |
bool loadStringFromDisk (const QString &fname, QString &s)
|
insilmaril@195
|
394 |
{
|
insilmaril@195
|
395 |
s="";
|
insilmaril@195
|
396 |
QFile file ( fname);
|
insilmaril@366
|
397 |
if ( !file.open( QIODevice::ReadOnly ) ) return false;
|
insilmaril@195
|
398 |
|
insilmaril@195
|
399 |
QTextStream ts( &file );
|
insilmaril@195
|
400 |
ts.setEncoding (QTextStream::UnicodeUTF8);
|
insilmaril@195
|
401 |
while ( !ts.atEnd() )
|
insilmaril@195
|
402 |
s+=ts.readLine()+"\n";
|
insilmaril@195
|
403 |
file.close();
|
insilmaril@195
|
404 |
return true;
|
insilmaril@195
|
405 |
}
|
insilmaril@195
|
406 |
|
insilmaril@195
|
407 |
bool saveStringToDisk (const QString &fname, const QString &s)
|
insilmaril@195
|
408 |
{
|
insilmaril@195
|
409 |
QFile file( fname);
|
insilmaril@195
|
410 |
|
insilmaril@195
|
411 |
file.setName ( fname);
|
insilmaril@366
|
412 |
if ( !file.open( QIODevice::WriteOnly ) )
|
insilmaril@195
|
413 |
{
|
insilmaril@195
|
414 |
file.close();
|
insilmaril@195
|
415 |
return false;
|
insilmaril@195
|
416 |
}
|
insilmaril@195
|
417 |
|
insilmaril@195
|
418 |
// Write it finally, and write in UTF8, no matter what
|
insilmaril@195
|
419 |
QTextStream ts( &file );
|
insilmaril@195
|
420 |
ts.setEncoding (QTextStream::UnicodeUTF8);
|
insilmaril@195
|
421 |
ts << s;
|
insilmaril@195
|
422 |
file.close();
|
insilmaril@195
|
423 |
return true;
|
insilmaril@195
|
424 |
}
|
insilmaril@195
|
425 |
|
insilmaril@195
|
426 |
|
insilmaril@234
|
427 |
ImagePreview::ImagePreview (QWidget *par=0): QLabel (par)
|
insilmaril@195
|
428 |
{
|
insilmaril@366
|
429 |
fdia=(Q3FileDialog*)par;
|
insilmaril@195
|
430 |
}
|
insilmaril@195
|
431 |
|
insilmaril@366
|
432 |
void ImagePreview::previewUrl( const Q3Url &u )
|
insilmaril@195
|
433 |
{
|
insilmaril@195
|
434 |
QString path = u.path();
|
insilmaril@195
|
435 |
QPixmap pix( path );
|
insilmaril@195
|
436 |
if ( pix.isNull() )
|
insilmaril@234
|
437 |
{
|
insilmaril@234
|
438 |
// Strange: If we have fd->setMode (QFileDialog::ExistingFiles)
|
insilmaril@234
|
439 |
// in the filedialog, then there are 3 calls to previewURL
|
insilmaril@234
|
440 |
// for each selection. And only the first is the actual selected file
|
insilmaril@234
|
441 |
// while the following 2 point to the directory above the current one.
|
insilmaril@234
|
442 |
// So here's my workaround:
|
insilmaril@234
|
443 |
|
insilmaril@234
|
444 |
if (fdia && fdia->selectedFiles().count()==0)
|
insilmaril@234
|
445 |
setText( QObject::tr("This is not an image.") );
|
insilmaril@234
|
446 |
if (fdia &&fdia->selectedFiles().count()>1)
|
insilmaril@234
|
447 |
setText( QObject::tr("Sorry, no preview for\nmultiple selected files.") );
|
insilmaril@234
|
448 |
}
|
insilmaril@195
|
449 |
else
|
insilmaril@195
|
450 |
{
|
insilmaril@195
|
451 |
float max_w=300;
|
insilmaril@195
|
452 |
float max_h=300;
|
insilmaril@195
|
453 |
float r;
|
insilmaril@195
|
454 |
if (pix.width()>max_w)
|
insilmaril@195
|
455 |
{
|
insilmaril@195
|
456 |
r=max_w / pix.width();
|
insilmaril@195
|
457 |
pix.resize(qRound(pix.width()*r), qRound(pix.height()*r));
|
insilmaril@412
|
458 |
// TODO not a resize, but a shrink/enlarge is needed here...
|
insilmaril@195
|
459 |
}
|
insilmaril@195
|
460 |
if (pix.height()>max_h)
|
insilmaril@195
|
461 |
{
|
insilmaril@195
|
462 |
r=max_h / pix.height();
|
insilmaril@195
|
463 |
pix.resize(qRound(pix.width()*r), qRound(pix.height()*r));
|
insilmaril@412
|
464 |
// TODO not a resize, but a shrink/enlarge is needed here...
|
insilmaril@195
|
465 |
}
|
insilmaril@195
|
466 |
setPixmap( pix );
|
insilmaril@195
|
467 |
}
|
insilmaril@195
|
468 |
}
|
insilmaril@195
|
469 |
|
insilmaril@366
|
470 |
ImageIO::ImageIO ()
|
insilmaril@366
|
471 |
{
|
insilmaril@366
|
472 |
// Create list with supported image types
|
insilmaril@366
|
473 |
// foreach (QByteArray format, QImageWriter::supportedImageFormats())
|
insilmaril@366
|
474 |
// imageTypes.append( tr("%1...").arg(QString(format).toUpper()));
|
insilmaril@366
|
475 |
imageFilters.append ("Images (*.png *.jpg *.jpeg *.bmp *.bmp *.ppm *.xpm *.xbm)");
|
insilmaril@366
|
476 |
imageTypes.append ("PNG");
|
insilmaril@366
|
477 |
imageFilters.append ("Portable Network Graphics (*.png)");
|
insilmaril@366
|
478 |
imageTypes.append ("PNG");
|
insilmaril@366
|
479 |
imageFilters.append ("Joint Photographic Experts Group (*.jpg)");
|
insilmaril@366
|
480 |
imageTypes.append ("JPG");
|
insilmaril@366
|
481 |
imageFilters.append ("Joint Photographic Experts Group (*.jpeg)");
|
insilmaril@366
|
482 |
imageTypes.append ("JPG");
|
insilmaril@366
|
483 |
imageFilters.append ("Windows Bitmap (*.bmp)");
|
insilmaril@366
|
484 |
imageTypes.append ("BMP");
|
insilmaril@366
|
485 |
imageFilters.append ("Portable Pixmap (*.ppm)");
|
insilmaril@366
|
486 |
imageTypes.append ("PPM");
|
insilmaril@366
|
487 |
imageFilters.append ("X11 Bitmap (*.xpm)");
|
insilmaril@366
|
488 |
imageTypes.append ("XPM");
|
insilmaril@366
|
489 |
imageFilters.append ("X11 Bitmap (*.xbm)");
|
insilmaril@366
|
490 |
imageTypes.append ("XBM");
|
insilmaril@366
|
491 |
}
|
insilmaril@366
|
492 |
|
insilmaril@366
|
493 |
QStringList ImageIO::getFilters()
|
insilmaril@366
|
494 |
{
|
insilmaril@366
|
495 |
return imageFilters;
|
insilmaril@366
|
496 |
}
|
insilmaril@366
|
497 |
|
insilmaril@366
|
498 |
QString ImageIO::getType(QString filter)
|
insilmaril@366
|
499 |
{
|
insilmaril@366
|
500 |
for (int i=0;i<imageFilters.count()+1;i++)
|
insilmaril@366
|
501 |
if (imageFilters.at(i)==filter) return imageTypes.at(i);
|
insilmaril@366
|
502 |
return QString();
|
insilmaril@366
|
503 |
}
|
insilmaril@366
|
504 |
|
insilmaril@366
|
505 |
|