More documentation, fixed small bug where links of unselected objects where visible again
2 #include <qmessagebox.h>
9 QString maskPath(QString p)
11 // Change " " to "\ " to enable blanks in filenames
12 p=p.replace(QChar('&'),"\\&");
13 return p.replace(QChar(' '),"\\ ");
16 QString convertToRel (const QString &src, const QString &dst)
24 // Special case, we just need the name of the file,
25 // not the complete path
27 d=d.right (d.length()-i-1);
30 // Find relative path from src to dst
32 // Remove the first "/"
33 if (s.section ("/",0,0).isEmpty())
35 s=s.right (s.length()-1);
36 d=d.right (d.length()-1);
39 // remove identical left parts
40 while (s.section("/",0,0) == d.section("/",0,0) )
43 s=s.right (s.length()-i-1);
44 d=d.right (d.length()-i-1);
47 // Now take care of paths where we have to go back first
48 int srcsep=s.contains("/");
49 int dstsep=d.contains("/");
50 if (srcsep <= dstsep )
52 // find path to go up first and then back to dst
64 QString makeUniqueDir (QString s)
66 // Create unique directory e.g. s="/tmp/vym-XXXXXX"
68 // Convert QString to string first
71 p=(char*) malloc (bytes+1);
74 p[i]=s.at(i).latin1();
76 QString r=mkdtemp (p);
81 void removeDir(QDir d)
83 if (d.path().left(4)!="/tmp")
85 // This _should_ not be necessary, but proved to be useful ;-)
86 qWarning ("file.cpp::removeDir should remove "+d.path()+" - aborted.");
90 // Traverse directories
91 d.setFilter( QDir::Dirs| QDir::Hidden | QDir::NoSymLinks );
92 const QFileInfoList *dirlist = d.entryInfoList();
93 QFileInfoListIterator itdir( *dirlist );
96 while ( (fi = itdir.current()) != 0 )
98 if (fi->fileName() != "." && fi->fileName() != ".." )
100 if ( !d.cd(fi->fileName()) )
101 qWarning ("removeDir() cannot find the directory "+fi->fileName());
104 // Recursively remove subdirs
112 d.setFilter( QDir::Files| QDir::Hidden | QDir::NoSymLinks );
113 const QFileInfoList *filelist = d.entryInfoList();
114 QFileInfoListIterator itfile( *filelist );
116 while ( (fi = itfile.current()) != 0 )
118 QFile (fi->filePath()).remove();
123 if (!d.rmdir(d.path()))
124 qWarning ("removeDir("+d.path()+") failed!");
127 void makeSubDirs (const QString &s)
135 ErrorCode zipDir (const QDir &zipDir, const QString &zipName)
137 ErrorCode err=success;
139 // zip the temporary directory
140 Process *zipProc=new Process ();
141 zipProc->clearArguments();
142 zipProc->setWorkingDirectory (QDir(zipDir));
143 zipProc->addArgument ("zip");
144 zipProc->addArgument ("-r");
145 zipProc->addArgument (zipName);
146 zipProc->addArgument (".");
148 if (!zipProc->start() )
150 // zip could not be started
151 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
152 QObject::tr("Couldn't start zip to compress data."));
156 // zip could be started
157 zipProc->waitFinished();
158 if (!zipProc->normalExit() )
160 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
161 QObject::tr("zip didn't exit normally")+
162 "\n" + zipProc->getErrout());
166 if (zipProc->exitStatus()>0)
168 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
169 QString("zip exit code: %1").arg(zipProc->exitStatus() )+
170 "\n" + zipProc->getErrout() );
174 } // zip could be started
178 ErrorCode unzipDir (const QDir &zipDir, const QString &zipName)
180 ErrorCode err=success;
183 Process *zipProc=new Process ();
184 zipProc->clearArguments();
185 zipProc->setWorkingDirectory (zipDir);
186 zipProc->addArgument ("unzip");
187 zipProc->addArgument ("-o"); // overwrite existing files!
188 zipProc->addArgument (zipName );
189 zipProc->addArgument ("-d");
190 zipProc->addArgument (zipDir.path());
192 if (!zipProc->start() )
194 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
195 QObject::tr("Couldn't start unzip to decompress data."));
200 zipProc->waitFinished();
201 if (!zipProc->normalExit() )
203 QMessageBox::critical( 0,QObject::tr( "Critical Error" ),
204 QObject::tr("unzip didn't exit normally") +
205 zipProc->getErrout() );
209 if (zipProc->exitStatus()>0)
211 if (zipProc->exitStatus()==9)
212 // no zipped file, but maybe .xml or old version? Try again.
216 QMessageBox::critical( 0, QObject::tr( "Critical Error" ),
217 QString("unzip exit code: %1").arg(zipProc->exitStatus() ) +
218 zipProc->getErrout() );
227 bool loadStringFromDisk (const QString &fname, QString &s)
231 if ( !file.open( IO_ReadOnly ) ) return false;
233 QTextStream ts( &file );
234 ts.setEncoding (QTextStream::UnicodeUTF8);
235 while ( !ts.atEnd() )
236 s+=ts.readLine()+"\n";
241 bool saveStringToDisk (const QString &fname, const QString &s)
245 file.setName ( fname);
246 if ( !file.open( IO_WriteOnly ) )
252 // Write it finally, and write in UTF8, no matter what
253 QTextStream ts( &file );
254 ts.setEncoding (QTextStream::UnicodeUTF8);
261 ImagePreview::ImagePreview (QWidget *par=0): QLabel (par)
263 fdia=(QFileDialog*)par;
266 void ImagePreview::previewUrl( const QUrl &u )
268 QString path = u.path();
272 // Strange: If we have fd->setMode (QFileDialog::ExistingFiles)
273 // in the filedialog, then there are 3 calls to previewURL
274 // for each selection. And only the first is the actual selected file
275 // while the following 2 point to the directory above the current one.
276 // So here's my workaround:
278 if (fdia && fdia->selectedFiles().count()==0)
279 setText( QObject::tr("This is not an image.") );
280 if (fdia &&fdia->selectedFiles().count()>1)
281 setText( QObject::tr("Sorry, no preview for\nmultiple selected files.") );
288 if (pix.width()>max_w)
290 r=max_w / pix.width();
291 pix.resize(qRound(pix.width()*r), qRound(pix.height()*r));
292 // FIXME not a resize, but a shrink/enlarge is needed here...
294 if (pix.height()>max_h)
296 r=max_h / pix.height();
297 pix.resize(qRound(pix.width()*r), qRound(pix.height()*r));
298 // FIXME not a resize, but a shrink/enlarge is needed here...