Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

modulesinfo.cpp

Go to the documentation of this file.
00001 /**********************************************************************
00002 ** ModulesInfo
00003 **
00004 ** Display Modules information
00005 **
00006 ** Copyright (C) 2002, Michael Lauer
00007 **                    mickey@tm.informatik.uni-frankfurt.de
00008 **                    http://www.Vanille.de
00009 **
00010 ** Based on ProcessInfo by Dan Williams <williamsdr@acm.org>
00011 **
00012 ** This file may be distributed and/or modified under the terms of the
00013 ** GNU General Public License version 2 as published by the Free Software
00014 ** Foundation and appearing in the file LICENSE.GPL included in the
00015 ** packaging of this file.
00016 **
00017 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00018 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00019 **
00020 **********************************************************************/
00021 
00022 #include "modulesinfo.h"
00023 #include "detail.h"
00024 
00025 /* OPIE */
00026 #include <opie2/olistview.h>
00027 #include <qpe/qpeapplication.h>
00028 
00029 /* QT */
00030 #include <qcombobox.h>
00031 #include <qfile.h>
00032 #include <qlayout.h>
00033 #include <qmessagebox.h>
00034 #include <qpushbutton.h>
00035 #include <qtextview.h>
00036 #include <qtimer.h>
00037 #include <qwhatsthis.h>
00038 
00039 using namespace Opie::Ui;
00040 ModulesInfo::ModulesInfo( QWidget* parent,  const char* name, WFlags fl )
00041         : QWidget( parent, name, fl )
00042 {
00043     QGridLayout *layout = new QGridLayout( this );
00044     layout->setSpacing( 4 );
00045     layout->setMargin( 4 );
00046 
00047     ModulesView = new OListView( this );
00048     int colnum = ModulesView->addColumn( tr( "Module" ) );
00049     colnum = ModulesView->addColumn( tr( "Size" ) );
00050     ModulesView->setColumnAlignment( colnum, Qt::AlignRight );
00051     colnum = ModulesView->addColumn( tr( "Use#" ) );
00052     ModulesView->setColumnAlignment( colnum, Qt::AlignRight );
00053     colnum = ModulesView->addColumn( tr( "Used by" ) );
00054     ModulesView->setAllColumnsShowFocus( TRUE );
00055     layout->addMultiCellWidget( ModulesView, 0, 0, 0, 1 );
00056     QWhatsThis::add( ModulesView, tr( "This is a list of all the kernel modules currently loaded on this handheld device.\n\nClick and hold on a module to see additional information about the module, or to unload it." ) );
00057 
00058     // Test if we have /sbin/modinfo, and if so, allow module detail window
00059     if ( QFile::exists( "/sbin/modinfo" ) )
00060     {
00061         QPEApplication::setStylusOperation( ModulesView->viewport(), QPEApplication::RightOnHold );
00062         connect( ModulesView, SIGNAL( rightButtonPressed(QListViewItem*,const QPoint&,int) ),
00063                  this, SLOT( viewModules(QListViewItem*) ) );
00064     }
00065 
00066     CommandCB = new QComboBox( FALSE, this );
00067     CommandCB->insertItem( "modprobe -r" );
00068     CommandCB->insertItem( "rmmod" );
00069     // I can't think of other useful commands yet. Anyone?
00070     layout->addWidget( CommandCB, 1, 0 );
00071     QWhatsThis::add( CommandCB, tr( "Select a command here and then click the Send button to the right to send the command to module selected above." ) );
00072 
00073     QPushButton *btn = new QPushButton( this );
00074     btn->setMinimumSize( QSize( 50, 24 ) );
00075     btn->setMaximumSize( QSize( 50, 24 ) );
00076     btn->setText( tr( "Send" ) );
00077     connect( btn, SIGNAL( clicked() ), this, SLOT( slotSendClicked() ) );
00078     layout->addWidget( btn, 1, 1 );
00079     QWhatsThis::add( btn, tr( "Click here to send the selected command to the module selected above." ) );
00080 
00081     QTimer *t = new QTimer( this );
00082     connect( t, SIGNAL( timeout() ), this, SLOT( updateData() ) );
00083     t->start( 5000 );
00084 
00085     updateData();
00086 
00087     ModulesDtl = new Detail();
00088     QWhatsThis::add( ModulesDtl->detailView, tr( "This area shows detailed information about this module." ) );
00089 }
00090 
00091 ModulesInfo::~ModulesInfo()
00092 {}
00093 
00094 void ModulesInfo::updateData()
00095 {
00096     char modname[64];
00097     char usage[200];
00098     int modsize, usecount;
00099 
00100     QString selectedmod;
00101     OListViewItem *curritem = static_cast<OListViewItem*>( ModulesView->currentItem() );
00102     if ( curritem )
00103     {
00104         selectedmod = curritem->text( 0 );
00105     }
00106 
00107     ModulesView->clear();
00108 
00109     FILE *procfile = fopen( ( QString ) ( "/proc/modules"), "r");
00110 
00111     if ( procfile )
00112     {
00113         OListViewItem *newitem;
00114         OListViewItem *selecteditem = 0x0;
00115         while ( true )
00116         {
00117             modname[0] = '\0';
00118             usage[0] = '\0';
00119             int success = fscanf( procfile, "%s%d%d%[^\n]", modname, &modsize, &usecount, usage );
00120 
00121             if ( success == EOF )
00122                 break;
00123 
00124             QString qmodname = QString( modname );
00125             QString qmodsize = QString::number( modsize ).rightJustify( 6, ' ' );
00126             QString qusecount = QString::number( usecount ).rightJustify( 2, ' ' );
00127             QString qusage = QString( usage );
00128 
00129             newitem = new OListViewItem( ModulesView, qmodname, qmodsize, qusecount, qusage );
00130             if ( qmodname == selectedmod )
00131             {
00132                 selecteditem = newitem;
00133             }
00134         }
00135         ModulesView->setCurrentItem( selecteditem );
00136 
00137         fclose( procfile );
00138     }
00139 }
00140 
00141 void ModulesInfo::slotSendClicked()
00142 {
00143     if ( !ModulesView->currentItem() )
00144     {
00145         return;
00146     }
00147 
00148     QString capstr = tr( "You really want to execute\n%1 for this module?" ).arg( CommandCB->currentText() );
00149 
00150     QString modname = ModulesView->currentItem()->text( 0 );
00151 
00152     if ( QMessageBox::warning( this, modname, capstr,
00153                                QMessageBox::Yes | QMessageBox::Default, QMessageBox::No | QMessageBox::Escape ) == QMessageBox::Yes )
00154     {
00155         QString command = "/sbin/";
00156         command.append( CommandCB->currentText() );
00157         command.append( " " );
00158         command.append( modname );
00159 
00160         FILE* stream = popen( command, "r" );
00161         if ( stream )
00162             pclose( stream );
00163     }
00164 
00165 }
00166 
00167 void ModulesInfo::viewModules( QListViewItem *module ) {
00168     if ( !module )
00169         return;
00170     viewModules(  static_cast<OListViewItem*>( module ) );
00171 }
00172 void ModulesInfo::viewModules( OListViewItem *modules )
00173 {
00174     QString modname = modules->text( 0 );
00175     QString capstr = "Module: ";
00176     capstr.append( modname );
00177     ModulesDtl->setCaption( capstr );
00178     QString command = "/sbin/modinfo ";
00179     command.append( modname );
00180     FILE* modinfo = popen( command, "r" );
00181 
00182     if ( modinfo )
00183     {
00184         char line[200];
00185         ModulesDtl->detailView->setText( " Details:\n------------\n" );
00186 
00187         while( true )
00188         {
00189             int success = fscanf( modinfo, "%[^\n]\n", line );
00190             if ( success == EOF )
00191                 break;
00192             ModulesDtl->detailView->append( line );
00193         }
00194 
00195         pclose( modinfo );
00196     }
00197 
00198     QPEApplication::showWidget( ModulesDtl );
00199 }
00200 

Generated on Sat Nov 5 16:17:58 2005 for OPIE by  doxygen 1.4.2