Diffstat (limited to 'noncore/tools/formatter/formatter.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/tools/formatter/formatter.cpp | 419 |
1 files changed, 419 insertions, 0 deletions
diff --git a/noncore/tools/formatter/formatter.cpp b/noncore/tools/formatter/formatter.cpp new file mode 100644 index 0000000..8c8ef2a --- a/dev/null +++ b/noncore/tools/formatter/formatter.cpp | |||
@@ -0,0 +1,419 @@ | |||
1 | /**************************************************************************** | ||
2 | ** formatter.cpp | ||
3 | ** | ||
4 | ** Copyright: Thu Apr 11 11:01:13 2002 | ||
5 | ** by: L. J. Potter | ||
6 | ** | ||
7 | ****************************************************************************/ | ||
8 | |||
9 | #include "formatter.h" | ||
10 | #include "inputDialog.h" | ||
11 | #include "output.h" | ||
12 | |||
13 | #include <qpe/qpemenubar.h> | ||
14 | #include <qpe/qpetoolbar.h> | ||
15 | #include <qpe/qpeapplication.h> | ||
16 | #include <qpe/resource.h> | ||
17 | #include <qpe/config.h> | ||
18 | #include <qpe/mimetype.h> | ||
19 | #include <qpe/qcopenvelope_qws.h> | ||
20 | #include <qpe/storage.h> | ||
21 | |||
22 | #include <qmultilineedit.h> | ||
23 | #include <qstring.h> | ||
24 | #include <qlist.h> | ||
25 | #include <qstringlist.h> | ||
26 | #include <qdir.h> | ||
27 | #include <qfile.h> | ||
28 | |||
29 | #include <qtstream.h> | ||
30 | |||
31 | #include <qcombobox.h> | ||
32 | #include <qpopupmenu.h> | ||
33 | #include <qmessagebox.h> | ||
34 | #include <qregexp.h> | ||
35 | |||
36 | #include <qlabel.h> | ||
37 | #include <qlineedit.h> | ||
38 | #include <qpushbutton.h> | ||
39 | #include <qtabwidget.h> | ||
40 | #include <qwidget.h> | ||
41 | #include <qlayout.h> | ||
42 | #include <qvariant.h> | ||
43 | |||
44 | #include <unistd.h> | ||
45 | #include <stdio.h> | ||
46 | #include <stdlib.h> | ||
47 | #include <sys/vfs.h> | ||
48 | #include <mntent.h> | ||
49 | |||
50 | #define BLANK ' ' | ||
51 | #define DELIMITER '#' | ||
52 | |||
53 | /* | ||
54 | Blah blah blah blah */ | ||
55 | FormatterApp::FormatterApp( QWidget* parent, const char* name, bool modal, WFlags fl ) | ||
56 | : QMainWindow( parent, name, fl ) | ||
57 | // : QDialog( parent, name, modal, fl ) | ||
58 | { | ||
59 | if ( !name ) | ||
60 | setName( "FormatterApp" ); | ||
61 | connect( qApp,SIGNAL( aboutToQuit()),SLOT( cleanUp()) ); | ||
62 | |||
63 | setCaption( tr( "Formatter" ) ); | ||
64 | FormatterAppLayout = new QGridLayout( this ); | ||
65 | FormatterAppLayout->setSpacing( 2); | ||
66 | FormatterAppLayout->setMargin( 2 ); | ||
67 | |||
68 | TabWidget = new QTabWidget( this, "TabWidget" ); | ||
69 | |||
70 | tab = new QWidget( TabWidget, "tab" ); | ||
71 | tabLayout = new QGridLayout( tab ); | ||
72 | tabLayout->setSpacing( 6); | ||
73 | tabLayout->setMargin( 11); | ||
74 | |||
75 | storageComboBox = new QComboBox( FALSE, tab, "storageComboBox" ); | ||
76 | |||
77 | tabLayout->addMultiCellWidget( storageComboBox, 0, 0, 0, 1 ); | ||
78 | |||
79 | TextLabel4 = new QLabel( tab, "TextLabel4" ); | ||
80 | TextLabel4->setText( tr( "Storage Type" ) ); | ||
81 | |||
82 | tabLayout->addMultiCellWidget( TextLabel4, 1, 1, 0, 1 ); | ||
83 | |||
84 | TextLabel2 = new QLabel( tab, "TextLabel2" ); | ||
85 | TextLabel2->setText( tr( "File Systems" ) ); | ||
86 | |||
87 | tabLayout->addMultiCellWidget( TextLabel2, 4, 4, 0, 1 ); | ||
88 | |||
89 | fileSystemsCombo = new QComboBox( FALSE, tab, "fileSystemsCombo" ); | ||
90 | |||
91 | tabLayout->addMultiCellWidget( fileSystemsCombo, 3, 3, 0, 1 ); | ||
92 | QSpacerItem* spacer = new QSpacerItem( 20, 20, QSizePolicy::Minimum, QSizePolicy::Expanding ); | ||
93 | tabLayout->addItem( spacer, 2, 1 ); | ||
94 | |||
95 | formatPushButton = new QPushButton( tab, "formatPushButton" ); | ||
96 | formatPushButton->setText( tr( "Format" ) ); | ||
97 | |||
98 | tabLayout->addMultiCellWidget( formatPushButton, 6, 6, 0, 1 ); | ||
99 | QSpacerItem* spacer_2 = new QSpacerItem( 20, 20, QSizePolicy::Minimum, QSizePolicy::Expanding ); | ||
100 | tabLayout->addItem( spacer_2, 5, 0 ); | ||
101 | TabWidget->insertTab( tab, tr( "Main" ) ); | ||
102 | |||
103 | tab_2 = new QWidget( TabWidget, "tab_2" ); | ||
104 | tabLayout_2 = new QGridLayout( tab_2 ); | ||
105 | tabLayout_2->setSpacing( 6); | ||
106 | tabLayout_2->setMargin( 11); | ||
107 | |||
108 | mountPointLineEdit = new QLineEdit( tab_2, "mountPointLineEdit" ); | ||
109 | |||
110 | tabLayout_2->addMultiCellWidget( mountPointLineEdit, 0, 0, 0, 1 ); | ||
111 | |||
112 | deviceComboBox = new QComboBox( FALSE, tab_2, "deviceComboBox" ); | ||
113 | |||
114 | tabLayout_2->addMultiCellWidget( deviceComboBox, 3, 3, 0, 1 ); | ||
115 | |||
116 | TextLabel5 = new QLabel( tab_2, "TextLabel5" ); | ||
117 | TextLabel5->setText( tr( "CAUTION:\n" | ||
118 | "Changing parameters on this \n" | ||
119 | "page may cause your system \n" | ||
120 | "to stop functioning properly!!" ) );//idiot message | ||
121 | |||
122 | tabLayout_2->addMultiCellWidget( TextLabel5, 6, 6, 0, 1 ); | ||
123 | |||
124 | editPushButton = new QPushButton( tab_2, "editPushButton" ); | ||
125 | editPushButton->setText( tr( "Edit fstab" ) ); | ||
126 | |||
127 | tabLayout_2->addMultiCellWidget( editPushButton, 7, 7, 0, 0 ); | ||
128 | |||
129 | fsckButton = new QPushButton( tab_2, "fsckPushButton" ); | ||
130 | fsckButton->setText( tr( "Check Disk" ) ); | ||
131 | |||
132 | tabLayout_2->addMultiCellWidget( fsckButton, 7, 7, 1, 1 ); | ||
133 | |||
134 | TextLabel3 = new QLabel( tab_2, "TextLabel3" ); | ||
135 | TextLabel3->setText( tr( "Device" ) ); | ||
136 | |||
137 | tabLayout_2->addMultiCellWidget( TextLabel3, 4, 4, 0, 1 ); | ||
138 | QSpacerItem* spacer_3 = new QSpacerItem( 20, 20, QSizePolicy::Minimum, QSizePolicy::Expanding ); | ||
139 | tabLayout_2->addItem( spacer_3, 5, 0 ); | ||
140 | |||
141 | TextLabel1 = new QLabel( tab_2, "TextLabel1" ); | ||
142 | TextLabel1->setText( tr( "Mount Point" ) ); | ||
143 | |||
144 | tabLayout_2->addMultiCellWidget( TextLabel1, 1, 1, 0, 1 ); | ||
145 | QSpacerItem* spacer_4 = new QSpacerItem( 20, 20, QSizePolicy::Minimum, QSizePolicy::Expanding ); | ||
146 | tabLayout_2->addItem( spacer_4, 2, 1 ); | ||
147 | TabWidget->insertTab( tab_2, tr( "Advanced" ) ); | ||
148 | |||
149 | FormatterAppLayout->addWidget( TabWidget, 0, 0 ); | ||
150 | |||
151 | connect( formatPushButton ,SIGNAL(released()),this,SLOT( doFormat()) ); | ||
152 | connect( editPushButton ,SIGNAL(released()),this,SLOT( editFstab()) ); | ||
153 | connect( fsckButton ,SIGNAL(released()),this,SLOT( doFsck()) ); | ||
154 | |||
155 | connect( fileSystemsCombo,SIGNAL(activated(int)),this,SLOT( fsComboSelected(int ) )); | ||
156 | connect( storageComboBox,SIGNAL(activated(int)),this,SLOT( storageComboSelected(int ) )); | ||
157 | connect( deviceComboBox,SIGNAL(activated(int)),this,SLOT( deviceComboSelected(int ) )); | ||
158 | |||
159 | |||
160 | fillCombos(); | ||
161 | } | ||
162 | |||
163 | /* | ||
164 | * Destroys the object and frees any allocated resources | ||
165 | frickin no duh?!? | ||
166 | */ | ||
167 | FormatterApp::~FormatterApp() | ||
168 | { | ||
169 | |||
170 | } | ||
171 | |||
172 | void FormatterApp::doFormat() { | ||
173 | int err=0; | ||
174 | Output *outDlg; | ||
175 | QString umountS, remountS; | ||
176 | QString text = storageComboBox->currentText(); | ||
177 | QString currentText = storageComboBox->currentText(); | ||
178 | QString cmd = currentText.right( currentText.length() - currentText.find(" -> ",0,TRUE) - 4); | ||
179 | QString fs = fileSystemsCombo->currentText(); | ||
180 | |||
181 | if( currentText.find("CF",0,TRUE) != -1) { | ||
182 | umountS = "/sbin/cardctl eject"; | ||
183 | remountS = "/sbin/cardctl insert"; | ||
184 | } | ||
185 | if( currentText.find("SD",0,TRUE) != -1) { | ||
186 | umountS = "/etc/sdcontrol compeject"; | ||
187 | remountS = "/etc/sdcontrol insert"; | ||
188 | } | ||
189 | |||
190 | switch ( QMessageBox::warning(this,tr("Format?!?"),tr("Really format\n") + currentText + | ||
191 | tr("\nwith ") + fs + tr(" filesystem?!?"),tr("Yes"),tr("No"),0,1,1) ) { | ||
192 | case 0: { | ||
193 | if(fs == "vfat") | ||
194 | cmd = "mkdosfs " + cmd; | ||
195 | else if(fs == "ext2") | ||
196 | cmd = "mke2fs " + cmd; | ||
197 | else { | ||
198 | QMessageBox::warning(this, tr("Formatter"),tr("Could not format.\nUnknown type"), tr("Ok")); | ||
199 | break; | ||
200 | } | ||
201 | // cmd = "ls -l"; | ||
202 | outDlg = new Output(this, tr("Formatter Output"),FALSE); | ||
203 | outDlg->showMaximized(); | ||
204 | outDlg->show(); | ||
205 | qApp->processEvents(); | ||
206 | FILE *fp; | ||
207 | char line[130]; | ||
208 | outDlg->OutputEdit->append( tr("Trying to eject.") + currentText ); | ||
209 | sleep(1); | ||
210 | fp = popen( (const char *) umountS, "r"); | ||
211 | if ( !fp ) { | ||
212 | qDebug("Could not execute '" + umountS + "'! err=%d", fp); | ||
213 | QMessageBox::warning( this, tr("CardMonitor"), tr("Card eject failed!"), tr("&OK") ); | ||
214 | pclose(fp); | ||
215 | return; | ||
216 | } else { | ||
217 | outDlg->OutputEdit->append( currentText + tr("\nhas been successfully ejected.")); | ||
218 | |||
219 | while ( fgets( line, sizeof line, fp)) { | ||
220 | outDlg->OutputEdit->append(line); | ||
221 | } | ||
222 | } | ||
223 | pclose(fp); | ||
224 | // err = system( (const char *) umountS); | ||
225 | |||
226 | qDebug("Command would be: "+cmd); | ||
227 | outDlg->OutputEdit->append( tr("Trying to format.") ); | ||
228 | |||
229 | fp = popen( (const char *) cmd, "r"); | ||
230 | while ( fgets( line, sizeof line, fp)) { | ||
231 | outDlg->OutputEdit->append(line); | ||
232 | } | ||
233 | outDlg->OutputEdit->append( currentText + tr("\nhas been successfully formatted.")); | ||
234 | pclose(fp); | ||
235 | |||
236 | outDlg->OutputEdit->append( tr("Trying to insert.") + currentText ); | ||
237 | fp = popen( (const char *) remountS, "r"); | ||
238 | if ( !fp) { | ||
239 | qDebug("Could not execute '" + remountS + "'! err=%d", err); | ||
240 | QMessageBox::warning( this, tr("Formatter"), tr("Card insert failed!"), tr("&OK") ); | ||
241 | |||
242 | } else { | ||
243 | outDlg->OutputEdit->append( currentText + tr("\nhas been successfully inserted.")); | ||
244 | while ( fgets( line, sizeof line, fp)) { | ||
245 | outDlg->OutputEdit->append(line); | ||
246 | } | ||
247 | } | ||
248 | pclose(fp); | ||
249 | sleep(1); | ||
250 | |||
251 | outDlg->OutputEdit->append(tr("You can now close the output window.")); | ||
252 | outDlg->close(); | ||
253 | if(outDlg) | ||
254 | delete outDlg; | ||
255 | } | ||
256 | break; | ||
257 | }; | ||
258 | } | ||
259 | |||
260 | void FormatterApp::fillCombos() { | ||
261 | |||
262 | StorageInfo storageInfo; | ||
263 | const QList<FileSystem> &fs = storageInfo.fileSystems(); | ||
264 | QListIterator<FileSystem> it ( fs ); | ||
265 | QString storage; | ||
266 | for( ; it.current(); ++it ){ | ||
267 | const QString name = (*it)->name(); | ||
268 | const QString path = (*it)->path(); | ||
269 | const QString disk = (*it)->disk(); | ||
270 | const QString options = (*it)->options(); | ||
271 | if( name.find( tr("Internal"),0,TRUE) == -1) { | ||
272 | storageComboBox->insertItem(name +" -> "+disk); | ||
273 | } | ||
274 | deviceComboBox->insertItem(disk); | ||
275 | } | ||
276 | parsetab(); | ||
277 | fileSystemsCombo->insertStringList( fsList,-1); | ||
278 | // for(int i = 0; i < fileSystemsCombo->count(); i++) { | ||
279 | // if( fsType == fileSystemsCombo->text(i)) | ||
280 | // fileSystemsCombo->setCurrentItem(i); | ||
281 | // } | ||
282 | storageComboSelected(0); | ||
283 | deviceComboSelected(0); | ||
284 | } | ||
285 | |||
286 | |||
287 | void FormatterApp::fsComboSelected(int index) { | ||
288 | |||
289 | } | ||
290 | |||
291 | void FormatterApp::storageComboSelected(int index ) { | ||
292 | |||
293 | QString currentText = storageComboBox->text(index); | ||
294 | QString nameS = currentText.left( currentText.find("->",0,TRUE)); | ||
295 | |||
296 | TextLabel4->setText( tr( "Storage Type : ") + nameS ); | ||
297 | currentText = currentText.right( currentText.length() - currentText.find(" -> ",0,TRUE) - 4); | ||
298 | |||
299 | QString fsType = getFileSystemType((const QString &) currentText); | ||
300 | // qDebug(fsType); | ||
301 | for(int i = 0; i < fileSystemsCombo->count(); i++) { | ||
302 | if( fsType == fileSystemsCombo->text(i)) | ||
303 | fileSystemsCombo->setCurrentItem(i); | ||
304 | } | ||
305 | // deviceComboSelected(index); | ||
306 | } | ||
307 | |||
308 | void FormatterApp::deviceComboSelected(int index) { | ||
309 | |||
310 | StorageInfo storageInfo; | ||
311 | QString totalS, usedS, avS, diskS, nameS, fsType, selectedText; | ||
312 | |||
313 | selectedText = deviceComboBox->text(index); | ||
314 | |||
315 | const QList<FileSystem> &fs = storageInfo.fileSystems(); | ||
316 | QListIterator<FileSystem> it ( fs ); | ||
317 | QString storage; | ||
318 | for( ; it.current(); ++it ){ | ||
319 | const QString name = (*it)->name(); | ||
320 | const QString path = (*it)->path(); | ||
321 | const QString disk = (*it)->disk(); | ||
322 | // const QString options = (*it)->options(); | ||
323 | if( selectedText == disk) { | ||
324 | diskS = disk; nameS= name; | ||
325 | mountPointLineEdit->setText(path); | ||
326 | long mult = (*it)->blockSize() / 1024; | ||
327 | long div = 1024 / (*it)->blockSize(); | ||
328 | if ( !mult ) mult = 1; | ||
329 | if ( !div ) div = 1; | ||
330 | long total = (*it)->totalBlocks() * mult / div; | ||
331 | long totalMb = total/1024; | ||
332 | long avail = (*it)->availBlocks() * mult / div; | ||
333 | long availMb = avail/1024; | ||
334 | long used = total - avail; | ||
335 | long usedMb = used/1024; | ||
336 | totalS.sprintf(tr("Total: %1 kB ( %d mB)\n").arg( total ), totalMb ); | ||
337 | usedS.sprintf(tr("Used: %1 kB ( %d mB)\n").arg(used) ,usedMb); | ||
338 | avS.sprintf( tr("Available: %1 kB ( %d mB)").arg(avail), availMb ); | ||
339 | } | ||
340 | } | ||
341 | fsType = getFileSystemType((const QString &)selectedText); | ||
342 | |||
343 | TextLabel5->setText("Type: "+ nameS+" Formatted with "+ fsType + " \n" + totalS + usedS + avS); | ||
344 | // storageComboSelected(0); | ||
345 | } | ||
346 | |||
347 | void FormatterApp::cleanUp() { | ||
348 | |||
349 | } | ||
350 | |||
351 | |||
352 | void FormatterApp::editFstab() { | ||
353 | QCopEnvelope e("QPE/Application/textedit","setDocument(QString)"); | ||
354 | e << (const QString &)"/etc/fstab"; | ||
355 | } | ||
356 | |||
357 | void FormatterApp::parsetab() { | ||
358 | |||
359 | fileSystemTypeList.clear(); | ||
360 | fsList.clear(); | ||
361 | struct mntent *me; | ||
362 | FILE *mntfp = setmntent( "/etc/mtab", "r" ); | ||
363 | if ( mntfp ) { | ||
364 | while ( (me = getmntent( mntfp )) != 0 ) { | ||
365 | QString deviceName = me->mnt_fsname; | ||
366 | QString filesystemType = me->mnt_type; | ||
367 | if(deviceName != "none") { | ||
368 | if( fsList.contains(filesystemType) == 0 | ||
369 | & filesystemType.find("proc",0,TRUE) == -1 | ||
370 | & filesystemType.find("cramfs",0,TRUE) == -1) | ||
371 | fsList << filesystemType; | ||
372 | fileSystemTypeList << deviceName+"::"+filesystemType; | ||
373 | } | ||
374 | } | ||
375 | } | ||
376 | endmntent( mntfp ); | ||
377 | |||
378 | // QFile f("/etc/fstab"); | ||
379 | // if ( f.open(IO_ReadOnly) ) { | ||
380 | // QTextStream t (&f); | ||
381 | // QString s; | ||
382 | // while (! t.eof()) { | ||
383 | // s=t.readLine(); | ||
384 | // s=s.simplifyWhiteSpace(); | ||
385 | // if ( (!s.isEmpty() ) && (s.find(" ")!=0) ) { | ||
386 | // s=s.remove(0,s.find(BLANK)+1 ); // devicename | ||
387 | // s=s.remove(0,s.find(BLANK)+1 ); // mountpoint | ||
388 | // s=s.remove(0,s.find(BLANK)+1 ); // fs | ||
389 | // } | ||
390 | // } | ||
391 | // } | ||
392 | // f.close(); | ||
393 | } | ||
394 | |||
395 | QString FormatterApp::getFileSystemType(const QString ¤tText) { | ||
396 | |||
397 | parsetab(); //why did TT forget filesystem type? | ||
398 | |||
399 | for ( QStringList::Iterator it = fileSystemTypeList.begin(); it != fileSystemTypeList.end(); ++it ) { | ||
400 | QString temp = (*it); | ||
401 | if( temp.find( currentText,0,TRUE) != -1) { | ||
402 | return temp.right( temp.length() - temp.find("::",0,TRUE) - 2); | ||
403 | // qDebug(fsType); | ||
404 | } | ||
405 | } | ||
406 | return ""; | ||
407 | } | ||
408 | |||
409 | void FormatterApp::doFsck() { | ||
410 | |||
411 | QString selectedDevice = deviceComboBox->currentText(); | ||
412 | QString fsType = getFileSystemType((const QString &)selectedDevice); | ||
413 | QString cmd; | ||
414 | qDebug( selectedDevice +" "+ fsType); | ||
415 | if(fsType == "vfat") cmd = "dosfsck -vy "; | ||
416 | if(fsType == "ext2") cmd = "e2fsck -cpvy "; | ||
417 | cmd += selectedDevice; | ||
418 | |||
419 | } | ||