28 #include <qlineedit.h>
29 #include <qtextedit.h>
30 #include <qcheckbox.h>
31 #include <qfiledialog.h>
32 #include <qmessagebox.h>
33 #include <qpushbutton.h>
34 #include <qscrollview.h>
35 #include <qdatastream.h>
36 #include <qvbuttongroup.h>
43 #define HEADER_LENGTH 32
44 #define CODE_ERROR 0x0000
45 #define CODE_DIR 0x0010
46 #define CODE_DIR_END 0x0018
47 #define CODE_FILE 0x0020
48 #define CODE_LIBRARY 0x0040
52 : QDialog(parent_, 0, TRUE, Qt::WDestructiveClose)
54 all =
new QVBoxLayout(
this);
58 QHBox *h2 =
new QHBox(
this);
61 setCaption(tr(
"Create Project Package"));
63 QHBox *h1 =
new QHBox(
this);
65 new QLabel(tr(
"Package:"), h1);
66 NameEdit =
new QLineEdit(h1);
67 QPushButton *ButtBrowse =
new QPushButton(tr(
"Browse"), h1);
68 connect(ButtBrowse, SIGNAL(clicked()), SLOT(slotBrowse()));
70 LibraryCheck =
new QCheckBox(tr(
"include user libraries"),
this);
71 all->addWidget(LibraryCheck);
73 Group =
new QVButtonGroup(tr(
"Choose projects:"),
this);
74 all->addWidget(Group);
76 QScrollView *Dia_Scroll =
new QScrollView(Group);
77 Dia_Scroll->setMargin(5);
78 QVBox *Dia_Box =
new QVBox(Dia_Scroll->viewport());
79 Dia_Scroll->addChild(Dia_Box);
83 QPushButton *ButtCreate =
new QPushButton(tr(
"Create"), h2);
84 connect(ButtCreate, SIGNAL(clicked()), SLOT(slotCreate()));
85 QPushButton *ButtCancel =
new QPushButton(tr(
"Cancel"), h2);
86 connect(ButtCancel, SIGNAL(clicked()), SLOT(reject()));
90 QStringList PrDirs =
QucsHomeDir.entryList(
"*", QDir::Dirs, QDir::Name);
91 QStringList::iterator it;
92 for(it = PrDirs.begin(); it != PrDirs.end(); it++)
93 if((*it).right(4) ==
"_prj")
94 BoxList.append(
new QCheckBox((*it).left((*it).length()-4), Dia_Box));
97 if(BoxList.isEmpty()) {
98 ButtCreate->setEnabled(
false);
100 (
new QLabel(tr(
"No projects!"), Dia_Box))->paletteBackgroundColor();
103 theColor = BoxList.current()->paletteBackgroundColor();
104 Dia_Scroll->viewport()->setPaletteBackgroundColor(theColor);
108 setCaption(tr(
"Extract Project Package"));
110 MsgText =
new QTextEdit(
this);
111 MsgText->setTextFormat(Qt::PlainText);
112 MsgText->setWordWrap(QTextEdit::NoWrap);
113 MsgText->setReadOnly(
true);
114 all->addWidget(MsgText);
117 h2->setStretchFactor(
new QWidget(h2), 5);
118 ButtClose =
new QPushButton(tr(
"Close"), h2);
119 ButtClose->setDisabled(
true);
120 connect(ButtClose, SIGNAL(clicked()), SLOT(accept()));
132 void PackageDialog::slotBrowse()
134 QString
s = QFileDialog::getSaveFileName(
136 tr(
"Qucs Packages")+
" (*.qucs);;"+
137 tr(
"Any File")+
" (*)",
138 this, 0, tr(
"Enter a Package File Name"));
139 if(s.isEmpty())
return;
144 if(Info.extension().isEmpty())
146 NameEdit->setText(s);
153 int PackageDialog::insertFile(
const QString& FileName, QFile&
File,
156 QByteArray FileContent;
158 if(!File.open(IO_ReadOnly)) {
159 QMessageBox::critical(
this, tr(
"Error"),
160 tr(
"Cannot open \"%1\"!").
arg(FileName));
164 Q_ULONG Count = File.size();
165 char *p = (
char*)malloc(Count+FileName.length()+2);
166 strcpy(p, FileName.latin1());
167 File.readBlock(p+FileName.length()+1, Count);
170 Count += FileName.length()+1;
171 FileContent = qCompress((
unsigned char*)p, Count);
174 Stream.writeBytes(FileContent.data(), FileContent.size());
179 int PackageDialog::insertDirectory(
const QString& DirName,
186 QStringList Entries = myDir.entryList(
"*", QDir::Files, QDir::Name);
187 QStringList::iterator it;
188 for(it = Entries.begin(); it != Entries.end(); ++it) {
189 File.setName(myDir.absFilePath(*it));
191 if(insertFile(*it, File, Stream) < 0)
196 Entries = myDir.entryList(
"*", QDir::Dirs, QDir::Name);
199 for(it = Entries.begin(); it != Entries.end(); ++it) {
200 Stream << Q_UINT32(
CODE_DIR) << (*it).latin1();
201 if(insertDirectory(myDir.absPath()+QDir::separator()+(*it), Stream) < 0)
209 int PackageDialog::insertLibraries(QDataStream& Stream)
212 QDir myDir(
QucsHomeDir.absPath() + QDir::separator() +
"user_lib");
213 QStringList Entries = myDir.entryList(
"*", QDir::Files, QDir::Name);
214 QStringList::iterator it;
215 for(it = Entries.begin(); it != Entries.end(); ++it) {
216 File.setName(myDir.absFilePath(*it));
218 if(insertFile(*it, File, Stream) < 0)
225 void PackageDialog::slotCreate()
227 if(NameEdit->text().isEmpty()) {
228 QMessageBox::critical(
this, tr(
"Error"), tr(
"Please insert a package name!"));
233 if(!LibraryCheck->isChecked()) {
235 for(p = BoxList.first(); p != 0; p = BoxList.next())
240 QMessageBox::critical(
this, tr(
"Error"), tr(
"Please choose at least one project!"));
245 QString
s(NameEdit->text());
247 if(Info.extension().isEmpty())
249 NameEdit->setText(s);
253 if(QMessageBox::information(
this, tr(
"Info"),
254 tr(
"Output file already exists!")+
"\n"+tr(
"Overwrite it?"),
255 tr(
"&Yes"), tr(
"&No"), 0,1,1))
258 if(!PkgFile.open(IO_ReadWrite)) {
259 QMessageBox::critical(
this, tr(
"Error"), tr(
"Cannot create package!"));
262 QDataStream Stream(&PkgFile);
267 strcpy(Header,
"Qucs package " PACKAGE_VERSION);
272 for(p = BoxList.first(); p != 0; p = BoxList.next())
274 s = p->text() +
"_prj";
275 Stream << Q_UINT32(
CODE_DIR) << s.latin1();
277 if(insertDirectory(s, Stream) < 0) {
286 if(LibraryCheck->isChecked())
287 if(insertLibraries(Stream) < 0) {
295 QByteArray Content = PkgFile.readAll();
296 Q_UINT16 Checksum = qChecksum(Content.data(), Content.size());
301 QMessageBox::information(
this, tr(
"Info"),
302 tr(
"Successfully created Qucs package!"));
314 QString s = QFileDialog::getOpenFileName(
316 tr(
"Qucs Packages")+
" (*.qucs);;"+
317 tr(
"Any File")+
" (*)",
318 this, 0, tr(
"Enter a Package File Name"));
329 if(!PkgFile.open(IO_ReadOnly)) {
330 if(Info.extension().isEmpty()) s +=
".qucs";
332 if(!PkgFile.open(IO_ReadOnly)) {
333 MsgText->append(tr(
"ERROR: Cannot open package!"));
334 ButtClose->setDisabled(
false);
338 QDataStream Stream(&PkgFile);
343 Q_UINT32 Code, Length;
346 QByteArray Content = PkgFile.readAll();
347 if(strncmp(Content.data(),
"Qucs package ", 13) != 0) {
348 MsgText->append(tr(
"ERROR: File contains wrong header!"));
352 Version = QString(Content.data()+13);
354 MsgText->append(tr(
"ERROR: Wrong version number!"));
362 if(Checksum != qChecksum(Content.data(), Content.size())) {
363 MsgText->append(tr(
"ERROR: Checksum mismatch!"));
371 if(PkgFile.atEnd())
break;
372 Stream >> Code >> Length;
376 if(extractDirectory(PkgFile, Length, currDir) > 0)
380 MsgText->append(tr(
"Leave directory \"%1\"").
arg(currDir.absPath()));
384 if(extractFile(PkgFile, Length, currDir) > 0)
388 if(extractLibrary(PkgFile, Length) > 0)
392 MsgText->append(tr(
"ERROR: Package is corrupt!"));
398 MsgText->append(
" ");
399 MsgText->append(tr(
"Successfully extracted package!"));
401 MsgText->append(
" ");
402 ButtClose->setDisabled(
false);
407 int PackageDialog::extractDirectory(QFile& PkgFile, Q_UINT32 Count, QDir& currDir)
409 char *p = (
char*)malloc(Count);
410 PkgFile.readBlock(p, Count);
412 if(currDir.cd(QString(p))) {
413 MsgText->append(tr(
"ERROR: Project directory \"%1\" already exists!").
arg(QString(p)));
417 if(!currDir.mkdir(QString(p))) {
418 MsgText->append(tr(
"ERROR: Cannot create directory \"%1\"!").
arg(QString(p)));
421 currDir.cd(QString(p));
422 MsgText->append(tr(
"Create and enter directory \"%1\"").
arg(currDir.absPath()));
429 int PackageDialog::extractFile(QFile& PkgFile, Q_UINT32 Count, QDir& currDir)
431 char *p = (
char*)malloc(Count);
432 PkgFile.readBlock(p, Count);
433 QByteArray Content = qUncompress((
unsigned char*)p, Count);
437 QFile
File(currDir.absFilePath(QString(p)));
438 if(!File.open(IO_WriteOnly)) {
439 MsgText->append(tr(
"ERROR: Cannot create file \"%1\"!").
arg(QString(p)));
443 File.writeBlock(p+strlen(p)+1, Content.size()-strlen(p)-1);
445 MsgText->append(tr(
"Create file \"%1\"").
arg(QString(p)));
450 int PackageDialog::extractLibrary(QFile& PkgFile, Q_UINT32 Count)
452 char *p = (
char*)malloc(Count);
453 PkgFile.readBlock(p, Count);
454 QByteArray Content = qUncompress((
unsigned char*)p, Count);
459 QDir::convertSeparators(
"/user_lib/") + QString(p));
461 MsgText->append(tr(
"ERROR: User library \"%1\" already exists!").
arg(QString(p)));
465 if(!File.open(IO_WriteOnly)) {
466 MsgText->append(tr(
"ERROR: Cannot create library \"%1\"!").
arg(QString(p)));
470 File.writeBlock(p+strlen(p)+1, Content.size()-strlen(p)-1);
472 MsgText->append(tr(
"Create library \"%1\"").
arg(QString(p)));