summaryrefslogtreecommitdiff
path: root/noncore/settings/packagemanager/installdlg.cpp
Unidiff
Diffstat (limited to 'noncore/settings/packagemanager/installdlg.cpp') (more/less context) (show whitespace changes)
-rw-r--r--noncore/settings/packagemanager/installdlg.cpp306
1 files changed, 306 insertions, 0 deletions
diff --git a/noncore/settings/packagemanager/installdlg.cpp b/noncore/settings/packagemanager/installdlg.cpp
new file mode 100644
index 0000000..c0c03fc
--- a/dev/null
+++ b/noncore/settings/packagemanager/installdlg.cpp
@@ -0,0 +1,306 @@
1/*
2                This file is part of the OPIE Project
3
4 =. Copyright (c) 2003 Dan Williams <drw@handhelds.org>
5             .=l.
6           .>+-=
7 _;:,     .>    :=|. This file is free software; you can
8.> <`_,   >  .   <= redistribute it and/or modify it under
9:`=1 )Y*s>-.--   : the terms of the GNU General Public
10.="- .-=="i,     .._ License as published by the Free Software
11 - .   .-<_>     .<> Foundation; either version 2 of the License,
12     ._= =}       : or (at your option) any later version.
13    .%`+i>       _;_.
14    .i_,=:_.      -<s. This file is distributed in the hope that
15     +  .  -:.       = it will be useful, but WITHOUT ANY WARRANTY;
16    : ..    .:,     . . . without even the implied warranty of
17    =_        +     =;=|` MERCHANTABILITY or FITNESS FOR A
18  _.=:.       :    :=>`: PARTICULAR PURPOSE. See the GNU General
19..}^=.=       =       ; Public License for more details.
20++=   -.     .`     .:
21 :     =  ...= . :.=- You should have received a copy of the GNU
22 -.   .:....=;==+<; General Public License along with this file;
23  -_. . .   )=.  = see the file COPYING. If not, write to the
24    --        :-=` Free Software Foundation, Inc.,
25 59 Temple Place - Suite 330,
26 Boston, MA 02111-1307, USA.
27
28*/
29
30#include "installdlg.h"
31
32#include <sys/vfs.h>
33
34#include <qapplication.h>
35#include <qcombobox.h>
36#include <qfileinfo.h>
37#include <qgroupbox.h>
38#include <qlabel.h>
39#include <qlayout.h>
40#include <qmap.h>
41#include <qmultilineedit.h>
42#include <qpushbutton.h>
43
44#include <qpe/fileselector.h>
45#include <qpe/resource.h>
46#include <qpe/storage.h>
47
48#include <opie/ofiledialog.h>
49
50#include "opackagemanager.h"
51
52InstallDlg::InstallDlg( QWidget *parent, OPackageManager *pm, const QString &caption, bool showDestInfo,
53 OPackage::Command command1, QStringList *packages1,
54 OPackage::Command command2, QStringList *packages2,
55 OPackage::Command command3, QStringList *packages3 )
56 : QWidget( 0x0 )
57 , m_packman( pm )
58 , m_numCommands( 0 )
59 , m_currCommand( 0 )
60{
61 // Save command/package list information
62 if ( command1 != OPackage::NotDefined )
63 {
64 m_command[ m_numCommands ] = command1;
65 m_packages[ m_numCommands ] = packages1;
66 ++m_numCommands;
67 }
68 if ( command2 != OPackage::NotDefined )
69 {
70 m_command[ m_numCommands ] = command2;
71 m_packages[ m_numCommands ] = packages2;
72 ++m_numCommands;
73 }
74 if ( command3 != OPackage::NotDefined )
75 {
76 m_command[ m_numCommands ] = command3;
77 m_packages[ m_numCommands ] = packages3;
78 ++m_numCommands;
79 }
80
81 // Initialize UI
82 if ( parent )
83 parent->setCaption( caption );
84
85 QGridLayout *layout = new QGridLayout( this, 4, 2, 2, 4 );
86
87 if ( showDestInfo )
88 {
89 QLabel *label = new QLabel( tr( "Destination" ), this );
90 layout->addWidget( label, 0, 0 );
91 m_destination = new QComboBox( this );
92 m_destination->insertStringList( *(m_packman->destinations()) );
93 layout->addWidget( m_destination, 0, 1 );
94 connect( m_destination, SIGNAL(highlighted(const QString&)),
95 this, SLOT(slotDisplayAvailSpace(const QString&)) );
96
97 label = new QLabel( tr( "Space Avail" ), this );
98 layout->addWidget( label, 1, 0 );
99 m_availSpace = new QLabel( this );
100 layout->addWidget( m_availSpace, 1, 1 );
101
102 // TODO - select correct destination
103 slotDisplayAvailSpace( m_destination->currentText() );
104 }
105 else
106 {
107 m_destination = 0x0;
108 m_availSpace = 0x0;
109 }
110
111 QGroupBox *groupBox = new QGroupBox( 0, Qt::Vertical, tr( "Output" ), this );
112 groupBox->layout()->setSpacing( 0 );
113 groupBox->layout()->setMargin( 4 );
114
115 QVBoxLayout *groupBoxLayout = new QVBoxLayout( groupBox->layout() );
116 m_output = new QMultiLineEdit( groupBox );
117 m_output->setReadOnly( true );
118 groupBoxLayout->addWidget( m_output );
119 layout->addMultiCellWidget( groupBox, 2, 2, 0, 1 );
120
121 m_btnStart = new QPushButton( Resource::loadPixmap( "packagemanager/apply" ), tr( "Start" ), this );
122 layout->addWidget( m_btnStart, 3, 0 );
123 connect( m_btnStart, SIGNAL(clicked()), this, SLOT(slotBtnStart()) );
124
125 m_btnOptions = new QPushButton( Resource::loadPixmap( "SettingsIcon" ), tr( "Options" ), this );
126 layout->addWidget( m_btnOptions, 3, 1 );
127 connect( m_btnOptions, SIGNAL( clicked() ), this, SLOT(slotBtnOptions()) );
128
129 // Display packages being acted upon in output widget
130 for( int i = 0; i < m_numCommands; i++ )
131 {
132 if ( m_packages[ i ] )
133 {
134 QString lineStr = tr( "Packages to " );
135
136 switch( m_command[ i ] )
137 {
138 case OPackage::Install : lineStr.append( tr( "install" ) );
139 break;
140 case OPackage::Remove : lineStr.append( tr( "remove" ) );
141 break;
142 case OPackage::Upgrade : lineStr.append( tr( "upgrade" ) );
143 break;
144 default :
145 break;
146 };
147 lineStr.append( ":\n" );
148
149 for ( QStringList::Iterator it = m_packages[ i ]->begin(); it != m_packages[ i ]->end(); ++it )
150 {
151 lineStr.append( QString( "\t%1\n" ).arg( ( *it ) ) );
152 }
153
154 m_output->append( lineStr );
155 }
156 }
157
158 m_output->append( tr( "Press the start button to begin.\n" ) );
159 m_output->setCursorPosition( m_output->numLines(), 0 );
160
161}
162
163InstallDlg::~InstallDlg()
164{
165 for( int i = 0; i < m_numCommands; i++ )
166 {
167 if ( m_packages[ i ] )
168 delete m_packages[ i ];
169 }
170}
171
172void InstallDlg::slotDisplayAvailSpace( const QString &destination )
173{
174 // If available space is not displayed, exit
175 if ( !m_availSpace )
176 return;
177
178 QString space = tr( "Unknown" );
179
180 // Get destination
181 OConfItem *dest = m_packman->findConfItem( OConfItem::Destination, destination );
182
183 if ( dest )
184 {
185 // Calculate available space
186 struct statfs fs;
187 if ( !statfs( dest->value(), &fs ) )
188 {
189 long mult = fs.f_bsize / 1024;
190 long div = 1024 / fs.f_bsize;
191
192 if ( !mult ) mult = 1;
193 if ( !div ) div = 1;
194 long avail = fs.f_bavail * mult / div;
195
196 space = tr( "%1 Kb" ).arg( avail );
197 }
198 }
199
200 // Display available space (if known)
201 m_availSpace->setText( space );
202}
203
204void InstallDlg::slotBtnStart()
205{
206 QString btnText = m_btnStart->text();
207 if ( btnText == tr( "Abort" ) )
208 {
209 // Stop execution of current command and prevent any others from executing
210 m_packman->abortCommand();
211 m_currCommand = 999;
212
213 // Allow user to close dialog
214 m_btnStart->setText( tr( "Close" ) );
215 m_btnStart->setIconSet( Resource::loadPixmap( "enter" ) );
216 return;
217 }
218 else if ( btnText == tr( "Close" ) )
219 {
220 // TODO - force reload of package data
221 emit closeInstallDlg();
222 return;
223 }
224
225 // Start was clicked, execute first command
226 m_btnOptions->setEnabled( false );
227 m_btnStart->setText( tr( "Abort" ) );
228 m_btnStart->setIconSet( Resource::loadPixmap( "close" ) );
229
230 m_packman->executeCommand( m_command[ 0 ], m_packages[ 0 ], m_destination->currentText(), this,
231 SLOT(slotOutput(OProcess*,char*,int)), SLOT(slotErrors(OProcess*,char*,int)),
232 SLOT(slotFinished(OProcess*)), true );
233}
234
235void InstallDlg::slotBtnOptions()
236{
237 QString btnText = m_btnOptions->text();
238 if ( btnText == tr( "Options" ) )
239 {
240 // Display configuration dialog (only options tab is enabled)
241 m_packman->configureDlg( true );
242 return;
243 }
244
245 // Save output was clicked
246 QMap<QString, QStringList> map;
247 map.insert( tr( "All" ), QStringList() );
248 QStringList text;
249 text << "text/*";
250 map.insert(tr( "Text" ), text );
251 text << "*";
252 map.insert( tr( "All" ), text );
253
254 QString filename = OFileDialog::getSaveFileName( 2, "/", "ipkg-output", map );
255 if( !filename.isEmpty() )
256 {
257 QString currentFileName = QFileInfo( filename ).fileName();
258 DocLnk doc;
259 doc.setType( "text/plain" );
260 doc.setFile( filename );
261 doc.setName( currentFileName );
262 FileManager fm;
263 fm.saveFile( doc, m_output->text() );
264 }
265}
266
267void InstallDlg::slotOutput( OProcess */*process*/, char *buffer, int buffLen )
268{
269 QString lineStr = buffer;
270 if ( lineStr[buffLen-1] == '\n' )
271 lineStr.truncate( buffLen - 1 );
272 m_output->append( lineStr );
273 m_output->setCursorPosition( m_output->numLines(), 0 );
274}
275
276void InstallDlg::slotErrors( OProcess */*process*/, char *buffer, int buffLen )
277{
278 QString lineStr = buffer;
279 if ( lineStr[buffLen-1] == '\n' )
280 lineStr.truncate( buffLen - 1 );
281 m_output->append( lineStr );
282 m_output->setCursorPosition( m_output->numLines(), 0 );
283}
284
285void InstallDlg::slotFinished( OProcess */*process*/ )
286{
287 ++m_currCommand;
288 if ( m_currCommand < m_numCommands )
289 {
290 // More commands left, execute next one
291 m_packman->executeCommand( m_command[ m_currCommand ], m_packages[ m_currCommand ], m_destination->currentText(),
292 this, SLOT(slotOutput(OProcess*,char*,int)),
293 SLOT(slotErrors(OProcess*,char*,int)),
294 SLOT(slotFinished(OProcess*)), true );
295 }
296 else
297 {
298 // All commands executed, allow user to close dialog
299 m_btnStart->setText( tr( "Close" ) );
300 m_btnStart->setIconSet( Resource::loadPixmap( "enter" ) );
301
302 m_btnOptions->setEnabled( true );
303 m_btnOptions->setText( tr( "Save output" ) );
304 m_btnOptions->setIconSet( Resource::loadPixmap( "save" ) );
305 }
306}