My Project  0.0.16
QUCS Mapping
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
librarydialog.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  librarydialog.cpp
3  -------------------
4  begin : Sun Jun 04 2006
5  copyright : (C) 2006 by Michael Margraf
6  email : michael.margraf@alumni.tu-berlin.de
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21 
22 #include "librarydialog.h"
23 #include "qucslib.h"
24 
25 #include <qhbox.h>
26 #include <qvbox.h>
27 #include <qfile.h>
28 #include <qlabel.h>
29 #include <qlayout.h>
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>
37 
38 
40  : QDialog(App_, 0, TRUE, Qt::WDestructiveClose)
41 {
42  setCaption(tr("Manage User Libraries"));
43 
44  Expr.setPattern("[\\w_]+");
45  Validator = new QRegExpValidator(Expr, this);
46 
47  // ...........................................................
48  QVBoxLayout *all = new QVBoxLayout(this);
49  all->setMargin(5);
50  all->setSpacing(6);
51 
52  Group = new QVButtonGroup(tr("Choose library:"), this);
53  all->addWidget(Group);
54 
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);
59 
60  QHBox *h1 = new QHBox(this);
61  all->addWidget(h1);
62  theLabel = new QLabel(tr("New Name:"), h1);
63  NameEdit = new QLineEdit(h1);
64  NameEdit->setValidator(Validator);
65 
66  // ...........................................................
67  QHBox *h2 = new QHBox(this);
68  all->addWidget(h2);
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);
76 
77  // ...........................................................
78  // insert all user libraries
79  QStringList LibFiles = UserLibDir.entryList("*.lib", QDir::Files, QDir::Name);
80 
81  toggleGroup = new QVButtonGroup(); // only to handle exclusive toggling
82 
83  previousLib = 0;
84  QStringList::iterator it;
85  // inserts all project directories
86  for(it = LibFiles.begin(); it != LibFiles.end(); it++)
87  toggleGroup->insert(new QRadioButton((*it).left((*it).length()-4), Dia_Box));
88 
89  QColor theColor;
90  QButton *rButton = toggleGroup->find(0);
91  if(rButton)
92  theColor = rButton->paletteBackgroundColor();
93  else {
94  ButtDelete->setEnabled(false);
95  ButtRename->setEnabled(false);
96  theColor =
97  (new QLabel(tr("No user library!"), Dia_Box))->paletteBackgroundColor();
98  }
99  Dia_Scroll->viewport()->setPaletteBackgroundColor(theColor);
100 }
101 
103 {
104  delete Validator;
105 }
106 
107 // ---------------------------------------------------------------
108 // Renames selected user library.
109 void LibraryDialog::slotRename()
110 {
111  if(NameEdit->text().isEmpty()) {
112  QMessageBox::critical(this, tr("Error"), tr("Please insert a new library name!"));
113  return;
114  }
115 
116  QRadioButton *rButton = (QRadioButton*)toggleGroup->selected();
117  if(rButton == 0) {
118  QMessageBox::critical(this, tr("Error"), tr("Please choose a library!"));
119  return;
120  }
121 
122  QString oldName = UserLibDir.absFilePath(rButton->text());
123  QString newName = UserLibDir.absFilePath(NameEdit->text());
124 
125  QFile NewLibFile(QucsSettings.LibDir + NameEdit->text() + ".lib");
126  if(NewLibFile.exists()) {
127  QMessageBox::critical(this, tr("Error"), tr("A system library with this name already exists!"));
128  return;
129  }
130 
131  NewLibFile.setName(newName + ".lib");
132  if(NewLibFile.exists()) {
133  QMessageBox::critical(this, tr("Error"), tr("A library with this name already exists!"));
134  return;
135  }
136 
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!"));
141  }
142  }
143 
144  QFile LibFile(oldName + ".lib");
145  if(!LibFile.open(IO_ReadOnly)) {
146  QMessageBox::critical(this, tr("Error"), tr("Cannot open library!"));
147  return;
148  }
149 
150  QByteArray FileContent = LibFile.readAll();
151 // LibFile.close();
152 
153  // rename library name within file
154  char *p, *Name;
155  char *Config = FileContent.data();
156  for(;;) {
157  p = strstr(Config, "<Qucs Library ");
158  if(p == 0) break;
159  Name = strstr(p, " \"");
160  if(Name == 0) break;
161  Name += 2;
162  p = strstr(Name, "\">");
163  if(p == 0) break;
164 
165  if(!NewLibFile.open(IO_WriteOnly)) {
166  QMessageBox::critical(this, tr("Error"), tr("No permission to modify library!"));
167  return;
168  }
169  int count = 0;
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) );
173  NewLibFile.close();
174  count -= FileContent.count() + NameEdit->text().length() - (p-Name);
175  if(count != 0) {
176  QMessageBox::critical(this, tr("Error"), tr("Writing new library not successful!"));
177  return;
178  }
179 
180  if(!LibFile.remove()) {
181  QMessageBox::critical(this, tr("Error"), tr("Cannot delete old library."));
182  toggleGroup->insert(new QRadioButton(NameEdit->text(), Dia_Box));
183  NameEdit->clear();
184  return;
185  }
186 
187  rButton->setText(NameEdit->text());
188  NameEdit->clear();
189  return;
190  }
191 
192  QMessageBox::critical(this, tr("Error"), tr("Library file is corrupt!"));
193 }
194 
195 // ---------------------------------------------------------------
196 // Deletes the selected user library.
197 void LibraryDialog::slotDelete()
198 {
199  QRadioButton *rButton = (QRadioButton*)toggleGroup->selected();
200  if(rButton == 0) {
201  QMessageBox::critical(this, tr("Error"), tr("Please choose a library!"));
202  return;
203  }
204 
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()));
210  return;
211  }
212 
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()));
224  return;
225  }
226  }
227 
228  delete rButton;
229 }