Fix for segfault when deleting MCO (invalid QModelIndex needs to be returned in index(TreeItem*) )
11 #if defined(Q_OS_WIN32)
16 QString maskPath(QString p)
18 // Change " " to "\ " to enable blanks in filenames
19 p=p.replace(QChar('&'),"\\&");
20 return p.replace(QChar(' '),"\\ ");
23 QString convertToRel (const QString &src, const QString &dst)
31 // Special case, we just need the name of the file,
32 // not the complete path
34 d=d.right (d.length()-i-1);
37 // Find relative path from src to dst
39 // Remove the first "/"
40 if (s.section ("/",0,0).isEmpty())
42 s=s.right (s.length()-1);
43 d=d.right (d.length()-1);
46 // remove identical left parts
47 while (s.section("/",0,0) == d.section("/",0,0) )
50 s=s.right (s.length()-i-1);
51 d=d.right (d.length()-i-1);
54 // Now take care of paths where we have to go back first
55 int srcsep=s.count("/");
56 int dstsep=d.count("/");
57 if (srcsep <= dstsep )
59 // find path to go up first and then back to dst
71 #include <QFileDialog>
72 extern QString vymName;
73 extern QDir lastFileDir;
75 QString browseDirectory (QWidget *parent,const QString &caption)
77 QFileDialog fd(parent,caption);
78 fd.setMode (QFileDialog::DirectoryOnly);
79 fd.setCaption(vymName+ " - "+caption);
80 fd.setDir (lastFileDir);
83 if ( fd.exec() == QDialog::Accepted )
84 return fd.selectedFile();
91 bool reallyWriteDirectory(const QString &dir)
93 QStringList eList = QDir(dir).entryList();
94 if (eList.first() ==".") eList.pop_front(); // remove "."
95 if (eList.first() =="..") eList.pop_front(); // remove "."
98 QMessageBox mb( vymName,
99 QObject::tr("The directory %1 is not empty.\nDo you risk to overwrite its contents?","write directory").arg(dir),
100 QMessageBox::Warning,
102 QMessageBox::Cancel | QMessageBox::Default,
103 QMessageBox::NoButton );
105 mb.setButtonText( QMessageBox::Yes, QObject::tr("Overwrite") );
106 mb.setButtonText( QMessageBox::No, QObject::tr("Cancel"));
109 case QMessageBox::Yes:
112 case QMessageBox::Cancel:
120 QString makeTmpDir (bool &ok, QString prefix)
123 QString path=makeUniqueDir (b,QDir::tempPath()+"/"+prefix+"-XXXXXX");
128 bool isInTmpDir(QString fn)
130 QString temp=QDir::tempPath();
132 return fn.left(l)==temp;
135 QString makeUniqueDir (bool &ok,QString s)
137 // Create unique directory e.g. for s="/tmp/vym-XXXXXX"
139 // Convert Separators
140 s=QDir::convertSeparators(s);
142 // Convert QString to string
145 int bytes=s.length();
146 p=(char*) malloc (bytes+1);
148 for (i=0;i<bytes;i++)
149 p[i]=s.at(i).latin1();
152 QString r=mkdtemp (p);
153 if (r.isEmpty()) ok=false;
158 void removeDir(QDir d)
160 // This check should_ not be necessary, but proved to be useful ;-)
161 if (!isInTmpDir(d.path()))
163 qWarning ("file.cpp::removeDir should remove "+d.path()+" - aborted.");
167 // Traverse directories
168 d.setFilter( QDir::Dirs| QDir::Hidden | QDir::NoSymLinks );
169 QFileInfoList list = d.entryInfoList();
172 for (int i = 0; i < list.size(); ++i)
175 if (fi.fileName() != "." && fi.fileName() != ".." )
177 if ( !d.cd(fi.fileName()) )
178 qWarning ("removeDir() cannot find the directory "+fi.fileName());
181 // Recursively remove subdirs
189 d.setFilter( QDir::Files| QDir::Hidden | QDir::NoSymLinks );
190 list = d.entryInfoList();
192 for (int i = 0; i < list.size(); ++i)
195 QFile (fi.filePath()).remove();
198 if (!d.rmdir(d.path()))
199 qWarning ("removeDir("+d.path()+") failed!");
202 void copyDir (QDir src, QDir dst)
204 system ("cp -r "+src.path()+"/* "+dst.path());
207 ErrorCode err=success;
209 Process *cpProc=new Process ();
211 cpProc->setWorkingDirectory (src.path());
216 cpProc->start ("cp",args);
217 if (!cpProc->waitForStarted() )
219 // zip could not be started
220 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
221 QObject::tr("Couldn't start zip to compress data."));
225 // zip could be started
226 cpProc->waitForFinished();
227 if (cpProc->exitStatus()!=QProcess::NormalExit )
229 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
230 QObject::tr("cp didn't exit normally")+
231 "\n" + cpProc->getErrout());
235 if (cpProc->exitCode()>0)
237 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
238 QString("cp exit code: %1").arg(cpProc->exitCode() )+
239 "\n" + cpProc->getErrout() );
243 } // cp could be started
247 void makeSubDirs (const QString &s)
255 ErrorCode zipDir (const QDir &zipDir, const QString &zipName)
257 ErrorCode err=success;
259 // zip the temporary directory
261 Process *zipProc=new Process ();
262 zipProc->setWorkingDirectory (zipDir.path());
267 zipProc->start ("zip",args);
268 if (!zipProc->waitForStarted() )
270 // zip could not be started
271 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
272 QObject::tr("Couldn't start zip to compress data."));
276 // zip could be started
277 zipProc->waitForFinished();
278 if (zipProc->exitStatus()!=QProcess::NormalExit )
280 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
281 QObject::tr("zip didn't exit normally")+
282 "\n" + zipProc->getErrout());
286 if (zipProc->exitCode()>0)
288 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
289 QString("zip exit code: %1").arg(zipProc->exitCode() )+
290 "\n" + zipProc->getErrout() );
294 } // zip could be started
298 ErrorCode unzipDir (const QDir &zipDir, const QString &zipName)
300 ErrorCode err=success;
303 #if !defined(Q_OS_WIN32)
305 Process *zipProc=new Process ();
306 zipProc->setWorkingDirectory (zipDir.path());
307 args << "-o"; // overwrite existing files!
310 args << zipDir.path();
312 zipProc->start ("unzip",args);
313 if (!zipProc->waitForStarted() )
315 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
316 QObject::tr("Couldn't start unzip to decompress data."));
321 zipProc->waitForFinished();
322 if (zipProc->exitStatus()!=QProcess::NormalExit )
324 QMessageBox::critical( 0,QObject::tr( "Critical Error" ),
325 QObject::tr("unzip didn't exit normally") +
326 zipProc->getErrout() );
330 if (zipProc->exitCode()>0)
332 if (zipProc->exitCode()==9)
333 // no zipped file, but maybe .xml or old version? Try again.
337 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
338 QString("unzip exit code: %1").arg(zipProc->exitCode() ) +
339 zipProc->getErrout() );
346 // Do this process creation using Win32 API.
348 PROCESS_INFORMATION piProcInfo;
349 STARTUPINFO siStartInfo;
351 // Initialize members of the PROCESS_INFORMATION structure.
352 ::ZeroMemory( &piProcInfo, sizeof(PROCESS_INFORMATION) );
354 // Set up members of the STARTUPINFO structure.
355 ::ZeroMemory( &siStartInfo, sizeof(STARTUPINFO) );
356 siStartInfo.cb = sizeof(STARTUPINFO);
358 // Create command line.
359 QString argv("unzip -o ");
360 argv.append(QDir::convertSeparators(zipName));
362 argv.append(QDir::convertSeparators(zipDir.path()));
364 // Create the child process.
365 if( !::CreateProcess(NULL,
366 (LPWSTR)argv.unicode(), // command line
367 NULL, // process security attributes
368 NULL, // primary thread security attributes
369 TRUE, // handles are inherited
371 NULL, // use parent's environment
372 NULL, // use parent's current directory
373 &siStartInfo, // STARTUPINFO pointer
374 &piProcInfo) ) // receives PROCESS_INFORMATION
380 // Wait for it to finish.
381 ::WaitForSingleObject( piProcInfo.hProcess, 10000 );
387 bool loadStringFromDisk (const QString &fname, QString &s)
391 if ( !file.open( QIODevice::ReadOnly ) ) return false;
393 QTextStream ts( &file );
394 ts.setEncoding (QTextStream::UnicodeUTF8);
395 while ( !ts.atEnd() )
396 s+=ts.readLine()+"\n";
401 bool saveStringToDisk (const QString &fname, const QString &s)
405 file.setName ( fname);
406 if ( !file.open( QIODevice::WriteOnly ) )
412 // Write it finally, and write in UTF8, no matter what
413 QTextStream ts( &file );
414 ts.setEncoding (QTextStream::UnicodeUTF8);
421 ImagePreview::ImagePreview (QWidget *par=0): QLabel (par)
423 fdia=(Q3FileDialog*)par;
426 void ImagePreview::previewUrl( const Q3Url &u )
428 QString path = u.path();
432 // Strange: If we have fd->setMode (QFileDialog::ExistingFiles)
433 // in the filedialog, then there are 3 calls to previewURL
434 // for each selection. And only the first is the actual selected file
435 // while the following 2 point to the directory above the current one.
436 // So here's my workaround:
438 if (fdia && fdia->selectedFiles().count()==0)
439 setText( QObject::tr("This is not an image.") );
440 if (fdia &&fdia->selectedFiles().count()>1)
441 setText( QObject::tr("Sorry, no preview for\nmultiple selected files.") );
448 if (pix.width()>max_w)
450 r=max_w / pix.width();
451 pix.resize(qRound(pix.width()*r), qRound(pix.height()*r));
452 // TODO not a resize, but a shrink/enlarge is needed here...
454 if (pix.height()>max_h)
456 r=max_h / pix.height();
457 pix.resize(qRound(pix.width()*r), qRound(pix.height()*r));
458 // TODO not a resize, but a shrink/enlarge is needed here...
466 // Create list with supported image types
467 // foreach (QByteArray format, QImageWriter::supportedImageFormats())
468 // imageTypes.append( tr("%1...").arg(QString(format).toUpper()));
469 imageFilters.append ("Images (*.png *.jpg *.jpeg *.bmp *.bmp *.ppm *.xpm *.xbm)");
470 imageTypes.append ("PNG");
471 imageFilters.append ("Portable Network Graphics (*.png)");
472 imageTypes.append ("PNG");
473 imageFilters.append ("Joint Photographic Experts Group (*.jpg)");
474 imageTypes.append ("JPG");
475 imageFilters.append ("Joint Photographic Experts Group (*.jpeg)");
476 imageTypes.append ("JPG");
477 imageFilters.append ("Windows Bitmap (*.bmp)");
478 imageTypes.append ("BMP");
479 imageFilters.append ("Portable Pixmap (*.ppm)");
480 imageTypes.append ("PPM");
481 imageFilters.append ("X11 Bitmap (*.xpm)");
482 imageTypes.append ("XPM");
483 imageFilters.append ("X11 Bitmap (*.xbm)");
484 imageTypes.append ("XBM");
487 QStringList ImageIO::getFilters()
492 QString ImageIO::getType(QString filter)
494 for (int i=0;i<imageFilters.count()+1;i++)
495 if (imageFilters.at(i)==filter) return imageTypes.at(i);