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