30 #include <qlineedit.h>
31 #include <qvalidator.h>
32 #include <qmessagebox.h>
33 #include <qpushbutton.h>
34 #include <qscrollview.h>
35 #include <qradiobutton.h>
36 #include <qvbuttongroup.h>
40 : QDialog(App_, 0, TRUE, Qt::WDestructiveClose)
42 setCaption(tr(
"Manage User Libraries"));
44 Expr.setPattern(
"[\\w_]+");
45 Validator =
new QRegExpValidator(Expr,
this);
48 QVBoxLayout *all =
new QVBoxLayout(
this);
52 Group =
new QVButtonGroup(tr(
"Choose library:"),
this);
53 all->addWidget(Group);
55 QScrollView *Dia_Scroll =
new QScrollView(Group);
56 Dia_Scroll->setMargin(5);
57 Dia_Box =
new QVBox(Dia_Scroll->viewport());
58 Dia_Scroll->addChild(Dia_Box);
60 QHBox *h1 =
new QHBox(
this);
62 theLabel =
new QLabel(tr(
"New Name:"), h1);
63 NameEdit =
new QLineEdit(h1);
64 NameEdit->setValidator(Validator);
67 QHBox *h2 =
new QHBox(
this);
69 ButtDelete =
new QPushButton(tr(
"Delete"), h2);
70 connect(ButtDelete, SIGNAL(clicked()), SLOT(slotDelete()));
71 ButtRename =
new QPushButton(tr(
"Rename"), h2);
72 connect(ButtRename, SIGNAL(clicked()), SLOT(slotRename()));
73 ButtClose =
new QPushButton(tr(
"Close"), h2);
74 connect(ButtClose, SIGNAL(clicked()), SLOT(reject()));
75 ButtClose->setDefault(
true);
79 QStringList LibFiles =
UserLibDir.entryList(
"*.lib", QDir::Files, QDir::Name);
81 toggleGroup =
new QVButtonGroup();
84 QStringList::iterator it;
86 for(it = LibFiles.begin(); it != LibFiles.end(); it++)
87 toggleGroup->insert(
new QRadioButton((*it).left((*it).length()-4), Dia_Box));
90 QButton *rButton = toggleGroup->find(0);
92 theColor = rButton->paletteBackgroundColor();
94 ButtDelete->setEnabled(
false);
95 ButtRename->setEnabled(
false);
97 (
new QLabel(tr(
"No user library!"), Dia_Box))->paletteBackgroundColor();
99 Dia_Scroll->viewport()->setPaletteBackgroundColor(theColor);
109 void LibraryDialog::slotRename()
111 if(NameEdit->text().isEmpty()) {
112 QMessageBox::critical(
this, tr(
"Error"), tr(
"Please insert a new library name!"));
116 QRadioButton *rButton = (QRadioButton*)toggleGroup->selected();
118 QMessageBox::critical(
this, tr(
"Error"), tr(
"Please choose a library!"));
122 QString oldName =
UserLibDir.absFilePath(rButton->text());
123 QString newName =
UserLibDir.absFilePath(NameEdit->text());
126 if(NewLibFile.exists()) {
127 QMessageBox::critical(
this, tr(
"Error"), tr(
"A system library with this name already exists!"));
131 NewLibFile.setName(newName +
".lib");
132 if(NewLibFile.exists()) {
133 QMessageBox::critical(
this, tr(
"Error"), tr(
"A library with this name already exists!"));
137 QDir LibSubdir(oldName);
138 if(LibSubdir.exists()) {
139 if(!LibSubdir.rename(oldName, newName)) {
140 QMessageBox::critical(
this, tr(
"Error"), tr(
"Cannot rename library subdirectory!"));
144 QFile LibFile(oldName +
".lib");
145 if(!LibFile.open(IO_ReadOnly)) {
146 QMessageBox::critical(
this, tr(
"Error"), tr(
"Cannot open library!"));
150 QByteArray FileContent = LibFile.readAll();
155 char *Config = FileContent.data();
157 p = strstr(Config,
"<Qucs Library ");
159 Name = strstr(p,
" \"");
162 p = strstr(Name,
"\">");
165 if(!NewLibFile.open(IO_WriteOnly)) {
166 QMessageBox::critical(
this, tr(
"Error"), tr(
"No permission to modify library!"));
170 count += NewLibFile.writeBlock(Config, Name-Config);
171 count += NewLibFile.writeBlock(NameEdit->text().latin1(), NameEdit->text().length());
172 count += NewLibFile.writeBlock(p, FileContent.count() - (p-Config) );
174 count -= FileContent.count() + NameEdit->text().length() - (p-Name);
176 QMessageBox::critical(
this, tr(
"Error"), tr(
"Writing new library not successful!"));
180 if(!LibFile.remove()) {
181 QMessageBox::critical(
this, tr(
"Error"), tr(
"Cannot delete old library."));
182 toggleGroup->insert(
new QRadioButton(NameEdit->text(), Dia_Box));
187 rButton->setText(NameEdit->text());
192 QMessageBox::critical(
this, tr(
"Error"), tr(
"Library file is corrupt!"));
197 void LibraryDialog::slotDelete()
199 QRadioButton *rButton = (QRadioButton*)toggleGroup->selected();
201 QMessageBox::critical(
this, tr(
"Error"), tr(
"Please choose a library!"));
205 QString oldName =
UserLibDir.absFilePath(rButton->text());
206 QFile LibFile(oldName +
".lib");
207 if(!LibFile.remove()) {
208 QMessageBox::critical(
this, tr(
"Error"),
209 tr(
"No permission to delete library \"%1\".").
arg(rButton->text()));
213 QDir LibSubdir(oldName);
214 if(LibSubdir.exists()) {
215 QStringList DirFiles;
216 QStringList::iterator it;
217 DirFiles = LibSubdir.entryList(
"*", QDir::Files, QDir::Name);
218 for(it = DirFiles.begin(); it != DirFiles.end(); it++)
219 LibSubdir.remove(*it);
220 if(!LibSubdir.rmdir(oldName)) {
221 QMessageBox::critical(
this, tr(
"Error"),
222 tr(
"No permission to delete library subdirectory \"%1\".").
223 arg(rButton->text()));