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

configdialog.cpp

Go to the documentation of this file.
00001 /*
00002                              This file is part of the Opie Project
00003              =.             (C) 2005 Michael 'Mickey' Lauer <mickey@Vanille.de>
00004             .=l.
00005            .>+-=
00006  _;:,     .>    :=|.         This program is free software; you can
00007 .> <`_,   >  .   <=          redistribute it and/or  modify it under
00008 :`=1 )Y*s>-.--   :           the terms of the GNU Library General Public
00009 .="- .-=="i,     .._         License as published by the Free Software
00010  - .   .-<_>     .<>         Foundation; either version 2 of the License,
00011      ._= =}       :          or (at your option) any later version.
00012     .%`+i>       _;_.
00013     .i_,=:_.      -<s.       This program is distributed in the hope that
00014      +  .  -:.       =       it will be useful,  but WITHOUT ANY WARRANTY;
00015     : ..    .:,     . . .    without even the implied warranty of
00016     =_        +     =;=|`    MERCHANTABILITY or FITNESS FOR A
00017   _.=:.       :    :=>`:     PARTICULAR PURPOSE. See the GNU
00018 ..}^=.=       =       ;      Library General Public License for more
00019 ++=   -.     .`     .:       details.
00020  :     =  ...= . :.=-
00021  -.   .:....=;==+<;          You should have received a copy of the GNU
00022   -_. . .   )=.  =           Library General Public License along with
00023     --        :-=`           this library; see the file COPYING.LIB.
00024                              If not, write to the Free Software Foundation,
00025                              Inc., 59 Temple Place - Suite 330,
00026                              Boston, MA 02111-1307, USA.
00027 
00028 */
00029 
00030 #include "configdialog.h"
00031 
00032 /* OPIE */
00033 #include <opie2/oconfig.h>
00034 #include <opie2/odebug.h>
00035 #include <opie2/opcmciasystem.h>
00036 #include <qpe/global.h>
00037 using namespace Opie::Core;
00038 
00039 /* QT */
00040 #include <qcombobox.h>
00041 #include <qdir.h>
00042 #include <qfile.h>
00043 #include <qgroupbox.h>
00044 #include <qlabel.h>
00045 #include <qtextstream.h>
00046 
00047 /* STD */
00048 #include <errno.h>
00049 #include <string.h>
00050 
00051 ConfigDialog::ConfigDialog( const OPcmciaSocket* card, QWidget* parent )
00052              :ConfigDialogBase( parent, "pcmcia config dialog", true )
00053 {
00054     gbDetails->setTitle( QString( "Details for card in socket #%1" ).arg( card->number() ) );
00055     txtCardName->setText( card->productIdentity() );
00056     txtManfid->setText( card->manufacturerIdentity() );
00057     txtFunction->setText( card->function() );
00058 
00059     QString insertAction = preferredAction( card, "insert" );
00060     QString resumeAction = preferredAction( card, "resume" );
00061 
00062     odebug << "pcmcia: preferred insertion action for card '" << card->productIdentity() << "' seems to be '" << insertAction << "'" << oendl;
00063     odebug << "pcmcia: preferred resume    action for card '" << card->productIdentity() << "' seems to be '" << resumeAction << "'" << oendl;
00064 
00065     if ( !insertAction.isEmpty() )
00066     {
00067         for ( unsigned int i = 0; i < cbInsertAction->count(); ++i )
00068             if ( cbInsertAction->text( i ) == insertAction ) cbInsertAction->setCurrentItem( i );
00069     }
00070 
00071     if ( !resumeAction.isEmpty() )
00072     {
00073         for ( unsigned int i = 0; i < cbResumeAction->count(); ++i )
00074             if ( cbResumeAction->text( i ) == resumeAction ) cbResumeAction->setCurrentItem( i );
00075     }
00076 
00077     if ( !card->isUnsupported() )
00078     {
00079         odebug << "pcmcia: card is recognized - hiding bindings" << oendl;
00080         textInfo->hide();
00081         textBindTo->hide();
00082         cbBindTo->hide();
00083         return;
00084     }
00085     else
00086     {
00087         odebug << "card is unsupported yet - showing possible bindings" << oendl;
00088         textInfo->show();
00089         textBindTo->show();
00090         cbBindTo->show();
00091     }
00092 
00093     // parse possible bind entries out of /etc/pcmcia/*.conf
00094     QDir pcmciaconfdir( "/etc/pcmcia", "*.conf" );
00095 
00096     for ( unsigned int i = 0; i < pcmciaconfdir.count(); ++i )
00097     {
00098         odebug << "processing conf file '" << pcmciaconfdir[i] << "'" << oendl;
00099         QString conffilename = QString( "%1/%2" ).arg( pcmciaconfdir.absPath() ).arg( pcmciaconfdir[i] );
00100         QFile conffile( conffilename );
00101         if ( conffile.open( IO_ReadOnly ) )
00102         {
00103             QTextStream ts( &conffile );
00104             while ( !ts.atEnd() )
00105             {
00106                 QString word;
00107                 ts >> word;
00108                 if ( word == "bind" )
00109                 {
00110                     word = ts.readLine();
00111                     bindEntries[ word.stripWhiteSpace() ] = conffilename;
00112                     continue;
00113                 }
00114                 ts.readLine();
00115             }
00116         }
00117         else
00118         {
00119             owarn << "couldn't open '" << conffile.name() << "' for reading" << oendl;
00120             continue;
00121         }
00122     }
00123 
00124     for ( StringMap::Iterator it = bindEntries.begin(); it != bindEntries.end(); ++it )
00125     {
00126         odebug << "found binding '" << it.key() << "' defined in '" << it.data().latin1() << "'" << oendl;
00127         cbBindTo->insertItem( it.key() );
00128     }
00129 }
00130 
00131 ConfigDialog::~ConfigDialog()
00132 {
00133 }
00134 
00135 void ConfigDialog::writeConfigEntry( const OPcmciaSocket* card, const QString& key, const QString& value )
00136 {
00137     OConfig* cfg = cardConfig( card );
00138     if ( cfg )
00139     {
00140         cfg->writeEntry( key, value );
00141     }
00142     delete cfg; // deleting a 0 pointer is within spec.
00143 }
00144 
00145 QString ConfigDialog::readConfigEntry( const OPcmciaSocket* card, const QString& key, const QString& defaultValue )
00146 {
00147     QString value;
00148     OConfig* cfg = cardConfig( card );
00149     if ( cfg )
00150     {
00151         value = cfg->readEntry( key, defaultValue );
00152     }
00153     delete cfg; // deleting a 0 pointer is within spec.
00154     return value;
00155 }
00156 
00157 
00158 QString ConfigDialog::preferredAction( const OPcmciaSocket* card, const QString& type )
00159 {
00160     return ConfigDialog::readConfigEntry( card, QString( "%1Action" ).arg( type ), "suspend" );
00161 }
00162 
00163 
00164 OConfig* ConfigDialog::cardConfig( const OPcmciaSocket* card )
00165 {
00166     OConfig* cardcfg = 0;
00167     OConfig* cfg = new OConfig( "PCMCIA" );
00168     cfg->setGroup( "Global" );
00169     int nCards = cfg->readNumEntry( "nCards", 0 );
00170     QString cardName = card->productIdentity();
00171 
00172     for ( int i = 0; i < nCards; ++i )
00173     {
00174         QString cardSection = QString( "Card_%1" ).arg( i );
00175         cfg->setGroup( cardSection );
00176         QString name = cfg->readEntry( "name" );
00177         odebug << "comparing card '" << cardName << "' with known card '" << name << "'" << oendl;
00178         if ( cardName == name )
00179         {
00180             cardcfg = cfg;
00181             break;
00182         }
00183     }
00184     return cardcfg;
00185 }
00186 
00187 
00188 void ConfigDialog::writeConfiguration( const OPcmciaSocket* card )
00189 {
00190     odebug << "pcmcia: ConfigDialog::writeConfiguration()" << oendl;
00191     OConfig* cfg = cardConfig( card );
00192     if ( !cfg )
00193     {
00194         cfg = new OConfig( "PCMCIA" );
00195         cfg->setGroup( "Global" );
00196         int nCards = cfg->readNumEntry( "nCards", 0 );
00197         cfg->setGroup( QString( "Card_%1" ).arg( nCards ) );
00198         cfg->writeEntry( "name", card->productIdentity() );
00199         cfg->setGroup( "Global" );
00200         cfg->writeEntry( "nCards", nCards+1 );
00201         cfg->setGroup( QString( "Card_%1" ).arg( nCards ) );
00202     }
00203 
00204     cfg->writeEntry( "insertAction", cbInsertAction->currentText() );
00205     cfg->writeEntry( "resumeAction", cbResumeAction->currentText() );
00206     cfg->write();
00207 
00208     if ( cbBindTo->isVisible() && cbBindTo->currentText() != "<None>" )
00209     {
00210         QString driver = cbBindTo->currentText();
00211         QString conf = bindEntries[driver];
00212 
00213         // write binding
00214 
00215         QFile confFile( conf );
00216         if ( confFile.open( IO_ReadWrite | IO_Append ) )
00217         {
00218             QString entryCard = QString( "card \"%1\"" ).arg( card->productIdentity() );
00219             QString entryVersion( "    version " );
00220             for ( QStringList::Iterator it = card->productIdentityVector().begin(); it != card->productIdentityVector().end(); ++it )
00221             {
00222                 entryVersion += QString( "\"%1\", " ).arg( *it );
00223             }
00224             QString entryBind = QString( "    bind %1" ).arg( driver );
00225             QString entry = QString( "\n%1\n%2\n%3\n" ).arg( entryCard ).arg( entryVersion ).arg( entryBind );
00226             odebug << "pcmcia: writing entry...:" << entry << oendl;
00227 
00228             confFile.writeBlock( (const char*) entry, entry.length() );
00229             Global::statusMessage( "restarting pcmcia services..." );
00230             OPcmciaSystem::instance()->restart();
00231         }
00232         else
00233         {
00234             owarn << "pcmcia: couldn't write binding to '" << conf << "' ( " << strerror( errno ) << " )." << oendl;
00235         }
00236     }
00237 }
00238 

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