summaryrefslogtreecommitdiff
path: root/noncore/settings/aqpkg/networkpkgmgr.cpp
blob: dee834ed263cb4d9d8a93efd99fbbb0215735977 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
/***************************************************************************
                          networkpkgmgr.cpp  -  description
                             -------------------
    begin                : Mon Aug 26 13:32:30 BST 2002
    copyright            : (C) 2002 by Andy Qua
    email                : andy.qua@blueyonder.co.uk
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include <fstream>
#include <iostream>
using namespace std;

#include <unistd.h>
#include <stdlib.h>
#include <linux/limits.h>

#ifdef QWS
#include <qpe/qpeapplication.h>
#include <qpe/qcopenvelope_qws.h>
#include <qpe/config.h>
#else
#include <qapplication.h>
#endif
#include <qlabel.h>
#include <qfile.h>
#include <qmessagebox.h>

#include "datamgr.h"
#include "networkpkgmgr.h"
#include "installdlgimpl.h"
#include "ipkg.h"
#include "inputdlg.h"
#include "letterpushbutton.h"

#include "global.h"

extern int compareVersions( const char *v1, const char *v2 );

NetworkPackageManager::NetworkPackageManager( DataManager *dataManager, QWidget *parent, const char *name)
  : QWidget(parent, name)
{
    dataMgr = dataManager;

#ifdef QWS
        // read download directory from config file
        Config cfg( "aqpkg" );
        cfg.setGroup( "settings" );
        currentlySelectedServer = cfg.readEntry( "selectedServer", "local" );
        showJumpTo = cfg.readBoolEntry( "showJumpTo", "true" );
#endif
    

    initGui();
    setupConnections();

    updateData();
//    progressDlg = 0;
//    timerId = startTimer( 100 );
}

NetworkPackageManager::~NetworkPackageManager()
{
}

void NetworkPackageManager :: timerEvent ( QTimerEvent * )
{
    killTimer( timerId );

//    showProgressDialog();
    // Add server names to listbox
    updateData();

//    progressDlg->hide();
}

void NetworkPackageManager :: updateData()
{
	serversList->clear();
    packagesList->clear();

    
    vector<Server>::iterator it;
    int activeItem = -1;
    int i;
    for ( i = 0, it = dataMgr->getServerList().begin() ; it != dataMgr->getServerList().end() ; ++it, ++i )
    {
        if ( !it->isServerActive() )
        {
            i--;
            continue;
        }
        serversList->insertItem( it->getServerName() );
        if ( it->getServerName() == currentlySelectedServer )
        	activeItem = i;
	}

	// set selected server to be active server
	if ( activeItem != -1 )
		serversList->setCurrentItem( activeItem );
    serverSelected( 0 );
}

void NetworkPackageManager :: selectLocalPackage( const QString &pkg )
{
    // First select local server
    for ( int i = 0 ; i < serversList->count() ; ++i )
    {
        if ( serversList->text( i ) == LOCAL_IPKGS )
        {
            serversList->setCurrentItem( i );
            break;
        }
    }
    serverSelected( 0 );

    // Now set the check box of the selected package
    for ( QCheckListItem *item = (QCheckListItem *)packagesList->firstChild();
          item != 0 ;
          item = (QCheckListItem *)item->nextSibling() )
    {
        if ( item->text().startsWith( pkg ) )
        {
            item->setOn( true );
            break;
        }
    }
}


void NetworkPackageManager :: initGui()
{
    QLabel *l = new QLabel( "Servers", this );
    serversList = new QComboBox( this );
    packagesList = new QListView( this );
    update = new QPushButton( "Refresh List", this );
    download = new QPushButton( "Download", this );
    upgrade = new QPushButton( "Upgrade", this );
    apply = new QPushButton( "Apply", this );

    QVBoxLayout *vbox = new QVBoxLayout( this, 0, -1, "VBox" );
    QHBoxLayout *hbox1 = new QHBoxLayout( vbox, -1, "HBox1" );
    hbox1->addWidget( l );
    hbox1->addWidget( serversList );

    QHBoxLayout *hbox3 = new QHBoxLayout( vbox, -1, "HBox1" );
    QHBoxLayout *hbox4 = new QHBoxLayout( vbox, -1, "HBox1" );


    if ( showJumpTo )
    {    
        char text[2];
        text[1] = '\0';
        for ( int i = 0 ; i < 26 ; ++i )
        {
                text[0] = 'A' + i;
                LetterPushButton *b = new LetterPushButton( text, this );
                connect( b, SIGNAL( released( QString ) ), this, SLOT( letterPushed( QString ) ) );
                if ( i < 13 )
                hbox3->addWidget( b );
                else
                hbox4->addWidget( b );
        }
    }

    vbox->addWidget( packagesList );
    packagesList->addColumn( "Packages" );

    QHBoxLayout *hbox2 = new QHBoxLayout( vbox, -1, "HBox2" );
    hbox2->addWidget( update );
    hbox2->addWidget( download );
    hbox2->addWidget( upgrade );
    hbox2->addWidget( apply );
}

void NetworkPackageManager :: setupConnections()
{
    connect( serversList, SIGNAL(activated( int )), this, SLOT(serverSelected( int )));
    connect( apply, SIGNAL(released()), this, SLOT(applyChanges()) );
    connect( download, SIGNAL(released()), this, SLOT(downloadPackage()) );
    connect( upgrade, SIGNAL( released()), this, SLOT(upgradePackages()) );
    connect( update, SIGNAL(released()), this, SLOT(updateServer()) );
}

void NetworkPackageManager :: showProgressDialog( char *initialText )
{
    if ( !progressDlg )
        progressDlg = new ProgressDlg( this, "Progress", false );
    progressDlg->setText( initialText );
    progressDlg->show();
}


void NetworkPackageManager :: serverSelected( int )
{
    packagesList->clear();

    // display packages
    QString serverName = serversList->currentText();
    currentlySelectedServer = serverName;

#ifdef QWS
        // read download directory from config file
        Config cfg( "aqpkg" );
        cfg.setGroup( "settings" );
        cfg.writeEntry( "selectedServer", currentlySelectedServer );
#endif

    Server *s = dataMgr->getServer( serverName );

    vector<Package> &list = s->getPackageList();
    vector<Package>::iterator it;
    for ( it = list.begin() ; it != list.end() ; ++it )
    {

        QString text = "";

		// If the local server, only display installed packages
		if ( serverName == LOCAL_SERVER && !it->isInstalled() )
			continue;

        text += it->getPackageName();
        if ( it->isInstalled() )
        {
            text += " (installed)";

            // If a different version of package is available, postfix it with an *
            if ( it->getVersion() != it->getInstalledVersion() )
            {

                if ( compareVersions( it->getInstalledVersion(), it->getVersion() ) == 1 )
                    text += "*";
            }
        }

        QCheckListItem *item = new QCheckListItem( packagesList, text, QCheckListItem::CheckBox );

        if ( it->isInstalled() )
        {
            QString destName = "";
            if ( it->getLocalPackage() )
            {
                if ( it->getLocalPackage()->getInstalledTo() )
                    destName = it->getLocalPackage()->getInstalledTo()->getDestinationName();
            }
            else
            {
                if ( it->getInstalledTo() )
                    destName = it->getInstalledTo()->getDestinationName();
            }
            if ( destName != "" )
                new QCheckListItem( item, QString( "Installed To - " ) + destName );
        }
        
        if ( !it->isPackageStoredLocally() )
            new QCheckListItem( item, QString( "Description - " ) + it->getDescription() );
        else
            new QCheckListItem( item, QString( "Filename - " ) + it->getFilename() );
        
		if ( serverName == LOCAL_SERVER )
		{
        	new QCheckListItem( item, QString( "V. Installed - " ) + it->getVersion() );
		}
		else
		{
        	new QCheckListItem( item, QString( "V. Available - " ) + it->getVersion() );
        	if ( it->getLocalPackage() )
        	{
				if ( it->isInstalled() )
	            	new QCheckListItem( item, QString( "V. Installed - " ) + it->getInstalledVersion() );
			}
		}
        packagesList->insertItem( item );
    }

    // If the local server or the local ipkgs server disable the download button
    if ( serverName == LOCAL_SERVER )
    {
        upgrade->setEnabled( false );
        download->setText( "Download" );
        download->setEnabled( false );
    }
    else if ( serverName == LOCAL_IPKGS )
    {
        upgrade->setEnabled( false );
        download->setEnabled( true );
        download->setText( "Remove" );
    }
    else
    {
        upgrade->setEnabled( true );
        download->setEnabled( true );
        download->setText( "Download" );
    }
}

void NetworkPackageManager :: updateServer()
{
    QString serverName = serversList->currentText();

    // Update the current server
    // Display dialog
//  ProgressDlg *progDlg = new ProgressDlg( this );
//  QString status = "Updating package lists...";
//  progDlg->show();
//  progDlg->setText( status );

    // Disable buttons to stop silly people clicking lots on them :)

    // First, write out ipkg_conf file so that ipkg can use it
    dataMgr->writeOutIpkgConf();

    Ipkg ipkg;
    ipkg.setOption( "update" );

    InstallDlgImpl dlg( &ipkg, "Refreshing server package lists", this, "Upgrade", true );
    dlg.showDlg();

    // Reload data
    dataMgr->reloadServerData();
    serverSelected(-1);
//  delete progDlg;
}

void NetworkPackageManager :: upgradePackages()
{
    // We're gonna do an upgrade of all packages
    // First warn user that this isn't recommended
    QString text = "WARNING: Upgrading while\nOpie/Qtopia is running\nis NOT recommended!\n\nAre you sure?\n";
    QMessageBox warn("Warning", text, QMessageBox::Warning,
                        QMessageBox::Yes,
                        QMessageBox::No | QMessageBox::Escape | QMessageBox::Default ,
                        0, this );
    warn.adjustSize();
                        
    if ( warn.exec() == QMessageBox::Yes )
    {
        // First, write out ipkg_conf file so that ipkg can use it
        dataMgr->writeOutIpkgConf();

        // Now run upgrade
        Ipkg ipkg;
        ipkg.setOption( "upgrade" );
        
        InstallDlgImpl dlg( &ipkg, "Upgrading installed packages", this, "Upgrade", true );
        dlg.showDlg();

        // Reload data
        dataMgr->reloadServerData();
        serverSelected(-1);
    }
}


void NetworkPackageManager :: downloadPackage()
{
    if ( download->text() == "Download" )
    {
        // First, write out ipkg_conf file so that ipkg can use it
        dataMgr->writeOutIpkgConf();
        
        // Display dialog to user asking where to download the files to
        bool ok = FALSE;
        QString dir = "";
#ifdef QWS
        // read download directory from config file
        Config cfg( "aqpkg" );
        cfg.setGroup( "settings" );
        dir = cfg.readEntry( "downloadDir", "/home/root/Documents/application/ipkg" );
#endif

        QString text = InputDialog::getText( tr( "Download to where" ), tr( "Enter path to download to" ), dir, &ok, this );
        if ( ok && !text.isEmpty() )
            dir = text;   // user entered something and pressed ok
        else
            return; // user entered nothing or pressed cancel

#ifdef QWS
        // Store download directory in config file
        cfg.writeEntry( "downloadDir", dir );
#endif

        // Get starting directory
        char initDir[PATH_MAX];
        getcwd( initDir, PATH_MAX );

        // Download each package
        Ipkg ipkg;
        connect( &ipkg, SIGNAL(outputText(const QString &)), this, SLOT(displayText(const QString &)));

        ipkg.setOption( "download" );
        ipkg.setRuntimeDirectory( dir );
        for ( QCheckListItem *item = (QCheckListItem *)packagesList->firstChild();
              item != 0 ;
              item = (QCheckListItem *)item->nextSibling() )
        {
            if ( item->isOn() )
            {
                QString name = item->text();
                int pos = name.find( "*" );
                name.truncate( pos );

                // if (there is a (installed), remove it
                pos = name.find( "(installed)" );
                if ( pos > 0 )
                    name.truncate( pos - 1 );

                ipkg.setPackage( name );
                ipkg.runIpkg( );
            }
        }
    }
    else if ( download->text() == "Remove" )
    {
        for ( QCheckListItem *item = (QCheckListItem *)packagesList->firstChild();
              item != 0 ;
              item = (QCheckListItem *)item->nextSibling() )
        {
            if ( item->isOn() )
            {
                QString name = item->text();
                int pos = name.find( "*" );
                name.truncate( pos );

                // if (there is a (installed), remove it
                pos = name.find( "(installed)" );
                if ( pos > 0 )
                    name.truncate( pos - 1 );
               
                Package *p = dataMgr->getServer( serversList->currentText() )->getPackage( name );
                QFile f( p->getFilename() );
                f.remove();
            }
        }
    }

    dataMgr->reloadServerData();
    serverSelected( -1 );
}


void NetworkPackageManager :: applyChanges()
{
    stickyOption = "";
    
    // First, write out ipkg_conf file so that ipkg can use it
    dataMgr->writeOutIpkgConf();

    // Now for each selected item
    // deal with it

	vector<InstallData> workingPackages;
    for ( QCheckListItem *item = (QCheckListItem *)packagesList->firstChild();
          item != 0 ;
          item = (QCheckListItem *)item->nextSibling() )
    {
        if ( item->isOn() )
        {
            InstallData data = dealWithItem( item );
            workingPackages.push_back( data );
		}
    }

    if ( workingPackages.size() == 0 )
    {
        // Nothing to do
        QMessageBox::information( this, "Nothing to do",
                             "No packages selected", "OK" );
        
        return;
    }
    
    // do the stuff
    InstallDlgImpl dlg( workingPackages, dataMgr, this, "Install", true );
    dlg.showDlg();

    // Reload data
    dataMgr->reloadServerData();
    serverSelected(-1);

#ifdef QWS
    // Finally let the main system update itself
    QCopEnvelope e("QPE/System", "linkChanged(QString)");
    QString lf = QString::null;
    e << lf;
#endif
}

// decide what to do - either remove, upgrade or install
// Current rules:
//    If not installed - install
//    If installed and different version available - upgrade
//    If installed and version up to date - remove
InstallData NetworkPackageManager :: dealWithItem( QCheckListItem *item )
{
    QString name = item->text();
    int pos = name.find( "*" );
    name.truncate( pos );

    // if (there is a (installed), remove it
    pos = name.find( "(installed)" );
    if ( pos > 0 )
        name.truncate( pos - 1 );

    // Get package
    Server *s = dataMgr->getServer( serversList->currentText() );
    Package *p = s->getPackage( name );

    // If the package has a filename then it is a local file
    if ( p->isPackageStoredLocally() )
        name = p->getFilename();

    QString option;
    QString dest = "root";
    if ( !p->isInstalled() )
    {
        InstallData item;
        item.option = "I";
        item.packageName = name;
        return item;
    }
    else
    {
        InstallData item;
        item.option = "D";
        if ( !p->isPackageStoredLocally() )
            item.packageName = p->getInstalledPackageName();
        else
            item.packageName = name;
        
        if ( p->getInstalledTo() )
        {
            item.destination = p->getInstalledTo();
            cout << "dest - " << p->getInstalledTo()->getDestinationName() << endl;
            cout << "dest - " << p->getInstalledTo()->getDestinationPath() << endl;
        }
        else
        {
            item.destination = p->getLocalPackage()->getInstalledTo();
        }

        // Now see if version is newer or not
        int val = compareVersions( p->getInstalledVersion(), p->getVersion() );

        // If the version requested is older and user selected a local ipk file, then reinstall the file
        if ( p->isPackageStoredLocally() && val == -1 )
            val = 0;
            
        if ( val == -2 )
        {
            // Error - should handle
        }
        else if ( val == -1 )
        {
            // Version available is older - remove only
            item.option = "D";
        }
        else
        {
            QString caption;
            QString text;
            QString secondButton;
            QString secondOption;
            if ( val == 0 )
            {
                // Version available is the same - option to remove or reinstall
                caption = "Do you wish to remove or reinstall\n%s?";
                text = "Remove or ReInstall";
                secondButton = "ReInstall";
                secondOption = "R";
            }
            else if ( val == 1 )
            {
                // Version available is newer - option to remove or upgrade
                caption = "Do you wish to remove or upgrade\n%s?";
                text = "Remove or Upgrade";
                secondButton = "Upgrade";
                secondOption = "U";
            }

            // Sticky option not implemented yet, but will eventually allow
            // the user to say something like 'remove all'
            if ( stickyOption == "" )
            {
                QString msgtext;
                msgtext.sprintf( caption, (const char *)name );
                switch( QMessageBox::information( this, text,
                                    msgtext, "Remove", secondButton ) )
                {
                    case 0: // Try again or Enter
						// option 0 = Remove
                        item.option = "D";
                        break;
                    case 1: // Quit or Escape
                        item.option = secondOption;
                        break;
                }
            }
            else
            {
//                item.option = stickyOption;
            }
        }

        
        // Check if we are reinstalling the same version
        if ( item.option != "R" )
           item.recreateLinks = true;
        else
           item.recreateLinks = false;

        // User hit cancel (on dlg - assume remove)
        return item;
    }
}

void NetworkPackageManager :: displayText( const QString &t )
{
    cout << t << endl;
}


void NetworkPackageManager :: letterPushed( QString t )
{
    QCheckListItem *top = (QCheckListItem *)packagesList->firstChild();
    QCheckListItem *start = (QCheckListItem *)packagesList->currentItem();
    if ( packagesList->firstChild() == 0 )
        return;

    QCheckListItem *item;
    if ( start == 0 )
    {
        item = (QCheckListItem *)packagesList->firstChild();
        start = top;
    }
    else
        item = (QCheckListItem *)start->nextSibling();

    if ( item == 0 )
        item = (QCheckListItem *)packagesList->firstChild();
    do
    {
        if ( item->text().lower().startsWith( t.lower() ) )
        {
            packagesList->setSelected( item, true );
            packagesList->ensureItemVisible( item );
            break;
		}

        item = (QCheckListItem *)item->nextSibling();
        if ( !item )
            item = (QCheckListItem *)packagesList->firstChild();        
    } while ( item != start);
}