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

installdlg.cpp

Go to the documentation of this file.
00001 /*
00002                              This file is part of the Opie Project
00003 
00004                              Copyright (C)2004, 2005 Dan Williams <drw@handhelds.org>
00005               =.
00006             .=l.
00007            .>+-=
00008  _;:,     .>    :=|.         This program is free software; you can
00009 .> <`_,   >  .   <=          redistribute it and/or  modify it under
00010 :`=1 )Y*s>-.--   :           the terms of the GNU Library General Public
00011 .="- .-=="i,     .._         License as published by the Free Software
00012  - .   .-<_>     .<>         Foundation; either version 2 of the License,
00013      ._= =}       :          or (at your option) any later version.
00014     .%`+i>       _;_.
00015     .i_,=:_.      -<s.       This program is distributed in the hope that
00016      +  .  -:.       =       it will be useful,  but WITHOUT ANY WARRANTY;
00017     : ..    .:,     . . .    without even the implied warranty of
00018     =_        +     =;=|`    MERCHANTABILITY or FITNESS FOR A
00019   _.=:.       :    :=>`:     PARTICULAR PURPOSE. See the GNU
00020 ..}^=.=       =       ;      Library General Public License for more
00021 ++=   -.     .`     .:       details.
00022 :     =  ...= . :.=-
00023  -.   .:....=;==+<;          You should have received a copy of the GNU
00024   -_. . .   )=.  =           Library General Public License along with
00025     --        :-=`           this library; see the file COPYING.LIB.
00026                              If not, write to the Free Software Foundation,
00027                              Inc., 59 Temple Place - Suite 330,
00028                              Boston, MA 02111-1307, USA.
00029 */
00030 
00031 #include "installdlg.h"
00032 
00033 #include <opie2/ofiledialog.h>
00034 #include <opie2/oresource.h>
00035 
00036 #include <qpe/fileselector.h>
00037 #include <qpe/storage.h>
00038 
00039 #include <qapplication.h>
00040 #include <qcombobox.h>
00041 #include <qfileinfo.h>
00042 #include <qgroupbox.h>
00043 #include <qlabel.h>
00044 #include <qlayout.h>
00045 #include <qmap.h>
00046 #include <qmultilineedit.h>
00047 #include <qpushbutton.h>
00048 
00049 #include <sys/vfs.h>
00050 
00051 #include "opackagemanager.h"
00052 
00053 InstallDlg::InstallDlg( QWidget *parent, OPackageManager *pm, const QString &caption,
00054                         OPackage::Command command1, const QStringList &packages1,
00055                         OPackage::Command command2, const QStringList &packages2,
00056                         OPackage::Command command3, const QStringList &packages3 )
00057     : QWidget( 0l )
00058     , m_packman( pm )
00059     , m_installFound( false )
00060     , m_numCommands( 0 )
00061     , m_currCommand( 0 )
00062     , m_destItem( 0l )
00063 {
00064     // Save command/package list information
00065     if ( command1 != OPackage::NotDefined )
00066     {
00067         m_command[ m_numCommands ] = command1;
00068         m_packages[ m_numCommands ] = packages1;
00069         ++m_numCommands;
00070 
00071         if ( command1 == OPackage::Install )
00072             m_installFound = true;
00073     }
00074     if ( command2 != OPackage::NotDefined )
00075     {
00076         m_command[ m_numCommands ] = command2;
00077         m_packages[ m_numCommands ] = packages2;
00078         ++m_numCommands;
00079 
00080         if ( command2 == OPackage::Install )
00081             m_installFound = true;
00082     }
00083     if ( command3 != OPackage::NotDefined )
00084     {
00085         m_command[ m_numCommands ] = command3;
00086         m_packages[ m_numCommands ] = packages3;
00087         ++m_numCommands;
00088 
00089         if ( command3 == OPackage::Install )
00090             m_installFound = true;
00091     }
00092 
00093     // Initialize UI
00094     if ( parent )
00095         parent->setCaption( caption );
00096 
00097     QGridLayout *layout = new QGridLayout( this, 4, 2, 2, 4 );
00098 
00099     if ( m_installFound )
00100     {
00101         QLabel *label = new QLabel( tr( "Destination" ), this );
00102         layout->addWidget( label, 0, 0 );
00103         m_destination = new QComboBox( this );
00104         m_destination->insertStringList( m_packman->destinations() );
00105         layout->addWidget( m_destination, 0, 1 );
00106         connect( m_destination, SIGNAL(highlighted(const QString&)),
00107                 this, SLOT(slotDisplayAvailSpace(const QString&)) );
00108 
00109         label = new QLabel( tr( "Space Avail" ), this );
00110         layout->addWidget( label, 1, 0 );
00111         m_availSpace = new QLabel( this );
00112         layout->addWidget( m_availSpace, 1, 1 );
00113 
00114         // TODO - select correct destination
00115         slotDisplayAvailSpace( m_destination->currentText() );
00116     }
00117     else
00118     {
00119         m_destination = 0l;
00120         m_availSpace = 0l;
00121     }
00122 
00123     QGroupBox *groupBox = new QGroupBox( 0, Qt::Vertical, tr( "Output" ), this );
00124     groupBox->layout()->setSpacing( 0 );
00125     groupBox->layout()->setMargin( 4 );
00126 
00127     QVBoxLayout *groupBoxLayout = new QVBoxLayout( groupBox->layout() );
00128     m_output = new QMultiLineEdit( groupBox );
00129     m_output->setReadOnly( true );
00130     groupBoxLayout->addWidget( m_output );
00131     layout->addMultiCellWidget( groupBox, 2, 2, 0, 1 );
00132 
00133     m_btnStart = new QPushButton( Opie::Core::OResource::loadPixmap( "packagemanager/apply",
00134                                   Opie::Core::OResource::SmallIcon ), tr( "Start" ), this );
00135     m_btnStart->setMinimumHeight( AppLnk::smallIconSize() );
00136     layout->addWidget( m_btnStart, 3, 0 );
00137     connect( m_btnStart, SIGNAL(clicked()), this, SLOT(slotBtnStart()) );
00138 
00139     m_btnOptions = new QPushButton( Opie::Core::OResource::loadPixmap( "SettingsIcon", Opie::Core::OResource::SmallIcon ),
00140                                     tr( "Options" ), this );
00141     m_btnOptions->setMinimumHeight( AppLnk::smallIconSize() );
00142     layout->addWidget( m_btnOptions, 3, 1 );
00143     connect( m_btnOptions, SIGNAL( clicked() ), this, SLOT(slotBtnOptions()) );
00144 
00145     // Display packages being acted upon in output widget
00146     for( int i = 0; i < m_numCommands; i++ )
00147     {
00148         if ( !m_packages[ i ].isEmpty() )
00149         {
00150             QString lineStr = tr( "Packages to " );
00151 
00152             switch( m_command[ i ] )
00153             {
00154                 case OPackage::Install : lineStr.append( tr( "install" ) );
00155                     break;
00156                 case OPackage::Remove : lineStr.append( tr( "remove" ) );
00157                     break;
00158                 case OPackage::Upgrade : lineStr.append( tr( "upgrade" ) );
00159                     break;
00160                 case OPackage::Download : lineStr.append( tr( "download" ) );
00161                     break;
00162                 default :
00163                     break;
00164             };
00165             lineStr.append( ":\n" );
00166 
00167             QStringList tmpPackage = m_packages[ i ];
00168             for ( QStringList::Iterator it = tmpPackage.begin(); it != tmpPackage.end(); ++it )
00169             {
00170                 lineStr.append( QString( "\t%1\n" ).arg( ( *it ) ) );
00171             }
00172 
00173             m_output->append( lineStr );
00174         }
00175     }
00176 
00177     m_output->append( tr( "Press the start button to begin.\n" ) );
00178     m_output->setCursorPosition( m_output->numLines(), 0 );
00179 
00180 }
00181 
00182 void InstallDlg::slotDisplayAvailSpace( const QString &destination )
00183 {
00184     // If available space is not displayed, exit
00185     if ( !m_availSpace )
00186         return;
00187 
00188     QString space = tr( "Unknown" );
00189 
00190     // Get destination
00191     if ( !destination.isNull() )
00192         m_destItem = m_packman->findConfItem( OConfItem::Destination, destination );
00193 
00194     if ( m_destItem )
00195     {
00196         // Calculate available space
00197         struct statfs fs;
00198         if ( !statfs( m_destItem->value(), &fs ) )
00199         {
00200             long mult = fs.f_bsize / 1024;
00201             long div = 1024 / fs.f_bsize;
00202 
00203             if ( !mult ) mult = 1;
00204             if ( !div ) div = 1;
00205             long avail = fs.f_bavail * mult / div;
00206 
00207             space = tr( "%1 Kb" ).arg( avail );
00208         }
00209     }
00210 
00211     // Display available space
00212     m_availSpace->setText( space );
00213 }
00214 
00215 void InstallDlg::slotBtnStart()
00216 {
00217     QString btnText = m_btnStart->text();
00218     if ( btnText == tr( "Abort" ) )
00219     {
00220         // Prevent unexecuted commands from executing
00221         m_currCommand = 999;
00222 
00223         // Allow user to close dialog
00224         m_btnStart->setText( tr( "Close" ) );
00225         m_btnStart->setIconSet( Opie::Core::OResource::loadPixmap( "close", Opie::Core::OResource::SmallIcon ) );
00226         return;
00227     }
00228     else if ( btnText == tr( "Close" ) )
00229     {
00230         // TODO - force reload of package data
00231         emit closeInstallDlg();
00232         return;
00233     }
00234 
00235     // Start was clicked, start executing
00236     QString dest;
00237     if ( m_installFound )
00238     {
00239         dest = m_destination->currentText();
00240         m_destination->setEnabled( false );
00241     }
00242 
00243     m_btnOptions->setEnabled( false );
00244     if ( m_numCommands > 1 )
00245     {
00246         m_btnStart->setText( tr( "Abort" ) );
00247         m_btnStart->setIconSet( Opie::Core::OResource::loadPixmap( "reset", Opie::Core::OResource::SmallIcon ) );
00248     }
00249     else
00250     {
00251         m_btnStart->setEnabled( false );
00252     }
00253 
00254     for ( m_currCommand = 0; m_currCommand < m_numCommands; m_currCommand++ )
00255     {
00256         // Execute next command
00257         m_packman->executeCommand( m_command[ m_currCommand ], m_packages[ m_currCommand ], dest,
00258                                    this, SLOT(slotOutput(const QString &)), true );
00259     }
00260 
00261     // All commands executed, allow user to close dialog
00262     m_btnStart->setEnabled( true );
00263     m_btnStart->setText( tr( "Close" ) );
00264     m_btnStart->setIconSet( Opie::Core::OResource::loadPixmap( "close", Opie::Core::OResource::SmallIcon ) );
00265 
00266     m_btnOptions->setEnabled( true );
00267     m_btnOptions->setText( tr( "Save output" ) );
00268     m_btnOptions->setIconSet( Opie::Core::OResource::loadPixmap( "save", Opie::Core::OResource::SmallIcon ) );
00269 }
00270 
00271 void InstallDlg::slotBtnOptions()
00272 {
00273     QString btnText = m_btnOptions->text();
00274     if ( btnText == tr( "Options" ) )
00275     {
00276         // Display configuration dialog (only options tab is enabled)
00277         m_packman->configureDlg( true );
00278         return;
00279     }
00280 
00281     // Save output was clicked
00282     QMap<QString, QStringList> map;
00283     map.insert( tr( "All" ), QStringList() );
00284     QStringList text;
00285     text << "text/*";
00286     map.insert(tr( "Text" ), text );
00287     text << "*";
00288     map.insert( tr( "All" ), text );
00289 
00290     QString filename = Opie::Ui::OFileDialog::getSaveFileName( 2, "/", "ipkg-output", map );
00291     if( !filename.isEmpty() )
00292     {
00293         QString currentFileName = QFileInfo( filename ).fileName();
00294         DocLnk doc;
00295         doc.setType( "text/plain" );
00296         doc.setFile( filename );
00297         doc.setName( currentFileName );
00298         FileManager fm;
00299         fm.saveFile( doc, m_output->text() );
00300     }
00301 }
00302 
00303 void InstallDlg::slotOutput( const QString &msg )
00304 {
00305     // Allow processing of other events
00306     qApp->processEvents();
00307 
00308     QString lineStr = msg;
00309     if ( lineStr[lineStr.length()-1] == '\n' )
00310         lineStr.truncate( lineStr.length() - 1 );
00311     m_output->append( lineStr );
00312     m_output->setCursorPosition( m_output->numLines(), 0 );
00313 
00314     // Update available space
00315     slotDisplayAvailSpace( QString::null );
00316 }

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