summaryrefslogtreecommitdiffstats
path: root/khtml/misc/knsplugininstaller.cpp
blob: 4cfbf3339549e4f52c43576c1989b1e016fcb874 (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
663
664
665
666
667
668
669
670
671
672
/*
 * This file is part of the KDE project.
 *
 * Copyright (C) 2005 Benoit Canet <[email protected]>
 *           with some advices from Aurelien Gateau <[email protected]>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 */


#include "knsplugininstaller.moc"

#include <kdebug.h>
#include <kdiroperator.h> 
#include <klistview.h>
#include <klocale.h>
#include <kstddirs.h>
#include <ktempfile.h> 
#include <netaccess.h>

#include <qbuttongroup.h>
#include <qdir.h>
#include <qiodevice.h>
#include <qfile.h>
#include <qlabel.h>
#include <qlayout.h> 
#include <qmap.h>
#include <qstringlist.h>
#include <qtextstream.h>

#include <sys/utsname.h>

// Use 6031 for debugging (render_frame)
#define DEBUG_NUMBER 6031 

/*
 * Utility class to associate a list item with a pluginInfo object
 */
class PluginListItem : public QListViewItem
{

public:
    PluginListItem(KNSPluginInfo pluginInfo, QListView *parent)
    : QListViewItem(parent, pluginInfo.pluginName())
    , m_pluginInfo(pluginInfo) {}
    KNSPluginInfo pluginInfo() const { return m_pluginInfo; }

private:
    KNSPluginInfo m_pluginInfo;

};


// public methods 

KNSPluginInstallEngine::KNSPluginInstallEngine(KMimeType::Ptr mime) : QObject()
{
    m_mime = mime;

    // Fill the architecture map 
    m_archMap["i386"] = "ia32";
    m_archMap["i486"] = "ia32";
    m_archMap["i586"] = "ia32";
    m_archMap["i686"] = "ia32";
}

KNSPluginInstallEngine::~KNSPluginInstallEngine()
{
}

bool KNSPluginInstallEngine::pluginAvailable()
{

    if(m_pluginList.count())
        return true;
    
    // check if pluginsListFile is present in kde config
    if(!loadConfig())
        return false;

    // load the xml configuration file 
    if(!loadXmlConfig())
        return false;
    
    return findPlugin();
}

bool KNSPluginInstallEngine::isActive() 
{
    // check if we have a configuration key in the kde registry
    QString pluginsListFile;
    KConfig cfg("kcmnspluginrc", true);
    cfg.setGroup("Misc");
    pluginsListFile = cfg.readPathEntry("PluginsListFile");
    return !pluginsListFile.isEmpty();
}

const QValueList<KNSPluginInfo>& KNSPluginInstallEngine::pluginList() const
{
    return m_pluginList;
}

// private methods 
bool KNSPluginInstallEngine::loadConfig()
{
    QString pluginsListFile;
    KConfig cfg("kcmnspluginrc", true);
    cfg.setGroup("Misc");
    pluginsListFile = cfg.readPathEntry("PluginsListFile");
    if(!pluginsListFile.isEmpty())
    {
        m_pluginsListFileURL = KURL(pluginsListFile);
        kdDebug(DEBUG_NUMBER) << "config loaded "<<endl;
        return true;
    }
    return false;
}

bool KNSPluginInstallEngine::loadXmlConfig()
{

    // load the Xml configuration file 
    if(m_pluginsXmlConfig.isEmpty())
    {
        QString tmpFile;
        if(KIO::NetAccess::download(m_pluginsListFileURL, tmpFile, NULL)) {
            QFile f(tmpFile);
            if(!f.open(IO_ReadOnly))
                return false;
            QTextStream stream(&f);
            stream.setEncoding(QTextStream::UnicodeUTF8);
            m_pluginsXmlConfig = stream.read();
            f.close();
            KIO::NetAccess::removeTempFile(tmpFile);
        } else
            return false;
    }
    kdDebug(DEBUG_NUMBER) << "xml config loaded :" << endl;
    return true;
}

bool KNSPluginInstallEngine::findPlugin()
{

    // get system infos
    // TODO/FIX : correct this to work with x86-64 machines
    utsname sysinfo;
    if(uname(&sysinfo))
        return false;
    QString sysname(sysinfo.sysname);
    QString machine(sysinfo.machine);
    QString arch = m_archMap[machine]; 
    
    // Parse the document
    QDomDocument doc("xmlConfig");
    doc.setContent(m_pluginsXmlConfig);
    QDomNodeList archList = doc.elementsByTagName(QString("arch"));
    QDomNode archNode, osNode , pluginNode, node;
    QDomElement e;

    // look for the correct architecture
    bool found = false;
    unsigned int i;
    for(i=0; i < archList.count() ; i++) {
        archNode = archList.item(i);
        e = archNode.toElement();
        if( e.attribute("architecture") == arch) {
            kdDebug(DEBUG_NUMBER) << "found correct architecture :" << arch << endl;
            found = true;
            break;
        }
    }

    if(!found)
        return false;
    
    // look for the correct os 
    found = false;
    osNode = archNode.firstChild();
    while(!osNode.isNull()) {
        e = osNode.toElement();
        if( e.tagName() == "os" && e.attribute("name") == sysname) {
            kdDebug(DEBUG_NUMBER) << "found correct os :" << sysname << endl;
            found = true;
            break;
        }
        osNode=osNode.nextSibling();
    }

    if(!found)
        return false;
   
    // Look for a plugin with the given mimetype
    pluginNode = osNode.firstChild();
    while(!pluginNode.isNull()) {
        e = pluginNode.toElement();
        if( e.tagName() == "plugin" && m_mime->is(e.attribute("mimetype")) ) {
            kdDebug(DEBUG_NUMBER) << "found correct plugin :" << e.attribute("mimetype") << endl;
            KNSPluginInfo pluginInfo(pluginNode);
                if(pluginInfo.isValid())
                    m_pluginList.append(pluginInfo);
        }
        pluginNode=pluginNode.nextSibling();
    }

    if(m_pluginList.count())
        return true;
    else
        return false;
}


void KNSPluginInstallEngine::startInstall(KNSPluginInfo info)
{
    m_toInstallPluginInfo = info;
    // create a temporary dowload file 
    KTempFile tempFile(locateLocal("tmp", "plugin") , QString(".tar.gz"));
    m_tmpPluginFileName = tempFile.name();
    tempFile.unlink();
    tempFile.close();
    // start the download job
    m_downloadJob  = KIO::copy(info.pluginURL(), "file://"+m_tmpPluginFileName, false );
    // connect signals 
    connect(m_downloadJob, SIGNAL(percent (KIO::Job *, unsigned long)), this , SLOT(slotDownLoadProgress(KIO::Job *, unsigned long)));
    connect(m_downloadJob, SIGNAL(result(KIO::Job *)), this, SLOT(slotDownloadResult(KIO::Job *)) );
    kdDebug(DEBUG_NUMBER) << "download plugin " << m_tmpPluginFileName << endl;
} 

void KNSPluginInstallEngine::slotDownLoadProgress(KIO::Job *, unsigned long percent)
{ 
    // propagate the download progression
    emit installProgress( ((int)percent)/3 );
}

void KNSPluginInstallEngine::slotDownloadResult(KIO::Job *job)
{ 
    // test if the download job suceed
    if(job->error()) {
        kdDebug(DEBUG_NUMBER) << "download error" << m_tmpPluginFileName << endl;
        emit installFailed();
     }
     else {
        kdDebug(DEBUG_NUMBER) << "download completed" << m_tmpPluginFileName << endl;
        // the download succeed copy the plugins files 
        
       // test the existance of the homedir
       QDir dir(QDir::homeDirPath());
       if(!dir.exists()) {
           emit installFailed();
           return;
        }

       // test and create firefox plugins directory
       if(!dir.exists(".mozilla"))
           dir.mkdir(".mozilla");
       if(!dir.exists(".mozilla/plugins"))
           dir.mkdir(".mozilla/plugins");
       // destination kurl
       KURL destURL("file://"+QDir::homeDirPath()+"/.mozilla/plugins");

       // construct the source kurlList
       KURL::List urlList;
       QStringList pluginFileList = m_toInstallPluginInfo.pluginFileList();

       QStringList::iterator it;
       for( it = pluginFileList.begin(); it != pluginFileList.end(); ++it ) {
           urlList.append( KURL("tar://"+m_tmpPluginFileName+"/"+(*it)) );
        }
       m_installFileJob  = KIO::copy(urlList , destURL, false );
       connect(m_installFileJob, SIGNAL(percent (KIO::Job *, unsigned long)), this , SLOT(slotCopyProgress(KIO::Job *, unsigned long)));
       connect(m_installFileJob, SIGNAL(result(KIO::Job *)), this, SLOT(slotCopyResult(KIO::Job *)) );
    }
    kdDebug(DEBUG_NUMBER) << "COPY FILE " << m_tmpPluginFileName << endl;

    // zero the download job pointer
    m_downloadJob = NULL;
}

void KNSPluginInstallEngine::slotCopyProgress(KIO::Job *, unsigned long percent)
{
    // propagate the download progression
    emit installProgress( ((int)percent)/3 + 33 );
} 
 
void KNSPluginInstallEngine::slotCopyResult(KIO::Job *job)
{
    // test if the download job suceed
    if(job->error()) {
        kdDebug(DEBUG_NUMBER) << "download error" << m_tmpPluginFileName << endl;
        emit installFailed();
    }
    else {
       // start the plugins scan
       m_scanProc = new QProcess( this );
       m_scanProc->addArgument( "nspluginscan" );
       m_scanProc->addArgument( "--verbose" );

       connect( m_scanProc, SIGNAL(readyReadStdout()),
               this, SLOT(readScanProcFromStdout()) );
       connect( m_scanProc, SIGNAL(processExited()),
               this, SLOT(endScanProc()) );
       if ( !m_scanProc->start() ) {
            emit installFailed();
       }
    }
}

void KNSPluginInstallEngine::readScanProcFromStdout()
{
    // Monitor the scan progress
    QString progress = m_scanProc->readLineStdout();
    int percent;
    bool ok;
    percent = progress.toInt(&ok);
    if(!ok)
        emit installFailed();
    emit installProgress( (percent)/3 + 66 );
}


void KNSPluginInstallEngine::endScanProc()
{
    // end of scan
    if(m_scanProc->normalExit()) {
        emit installProgress( 100 );
        emit installCompleted();
    } else
        emit installFailed();
}

KNSPluginWizard::KNSPluginWizard(QWidget *parent, const char *name, KMimeType::Ptr mime)
: KWizard(parent, name, true)
, m_installEngine(mime)
{
    setCaption(i18n("KDE plugin wizard"));
    setModal(true);

    // read the plugin installer configuration
    m_installEngine.pluginAvailable();

    // init the wizzard Pages
    initConfirmationPage();
    initLicencePage();
    initInstallationProgressPage();
    initFinishPage();
    initPagesButtonStates();

    // connect signals and slots
    connectSignals();

    //set correct default installation status
    m_installationComplete = false;

};


KNSPluginWizard::~KNSPluginWizard()
{
};

void KNSPluginWizard::initConfirmationPage() 
{

        m_confirmationVBox = new QVBox(this);
        new QLabel(i18n("The following plugins are available."), m_confirmationVBox);
        m_pluginListView = new KListView(m_confirmationVBox);        
        m_pluginListView->addColumn(i18n("Name"));
        m_pluginListView->setSelectionMode(QListView::Single);
        new QLabel(i18n("Click on next to install the selected plugin."), m_confirmationVBox);
        addPage (m_confirmationVBox, i18n("Plugin installation confirmation"));
        
        bool selected = false;
  
        // Fill the plugin list
        QValueList<KNSPluginInfo>::iterator it;
        QValueList<KNSPluginInfo> pluginList = m_installEngine.pluginList();
        for( it = pluginList.begin(); it != pluginList.end(); ++it ) {
            PluginListItem *item = new PluginListItem((*it) , m_pluginListView);
            if(!selected) {
                selected = true;
                m_pluginListView->setSelected(item, true);
            }
            kdDebug(DEBUG_NUMBER) << "New Plugin List item"<< endl;
            setNextEnabled(m_confirmationVBox, true);
        }
}

void KNSPluginWizard::initLicencePage()
{
    m_licenceVBox = new QVBox(this);
    m_licencePageLabel = new QLabel(m_licenceVBox);
    m_licencePageText = new KTextEdit(m_licenceVBox);
    m_licencePageText->setReadOnly(true);
    
    // invisible buttonGroup
    QButtonGroup *buttonGroup = new QButtonGroup(this);
    m_agreementButtonGroup = buttonGroup;
    buttonGroup->hide();
    buttonGroup->setExclusive(true);
    
    m_licencePageAgree = new QRadioButton ( i18n("I agree."), m_licenceVBox);
    
    m_licencePageDisagree = new QRadioButton ( i18n("I do not agree (plugin will not be installed)."), m_licenceVBox);

    buttonGroup->insert(m_licencePageAgree);
    buttonGroup->insert(m_licencePageDisagree);
    m_licencePageDisagree->setChecked(true);
    
    addPage (m_licenceVBox, i18n("Plugin licence"));


    connect(buttonGroup, SIGNAL(clicked(int)), this, SLOT(slotAgreementClicked(int)));
}

void KNSPluginWizard::initInstallationProgressPage() {

    m_installationProgressWidget = new QWidget(this);
    QVBoxLayout *layout = new QVBoxLayout(m_installationProgressWidget);
    layout->addWidget(new QLabel(i18n("Installation in progress."), m_installationProgressWidget));
    layout->addItem(new QSpacerItem(40,20,QSizePolicy::Expanding,QSizePolicy::Expanding ));
    m_installationProgressBar = new KProgress(m_installationProgressWidget);
    m_installationProgressBar->setTotalSteps(100);
    layout->addWidget(m_installationProgressBar);
    
    addPage( m_installationProgressWidget, i18n("Plugin installation"));
    
}

void KNSPluginWizard::initFinishPage()
{
    m_finishWidget = new QWidget(this);
    QVBoxLayout *layout = new QVBoxLayout(m_finishWidget);
    layout->addItem(new QSpacerItem(40,20,QSizePolicy::Expanding,QSizePolicy::Expanding ));
    m_finishLabel = new QLabel(m_finishWidget);
    layout->addWidget(m_finishLabel);
    layout->addItem(new QSpacerItem(40,20,QSizePolicy::Expanding,QSizePolicy::Expanding ));
    
    addPage(m_finishWidget, i18n("Installation status"));
    
}


void KNSPluginWizard::initPagesButtonStates()
{
    // set buttons states for the confirmation page
    setNextEnabled(m_confirmationVBox, true);
    setFinishEnabled(m_confirmationVBox, false);
    setHelpEnabled(m_confirmationVBox, false);

    // set buttons states for the licence page
    setNextEnabled(m_licenceVBox    , false);
    setBackEnabled(m_licenceVBox  , false);
    setFinishEnabled(m_licenceVBox  , false);
    setHelpEnabled(m_licenceVBox    , false);
    
    // for the installation page
    setNextEnabled(m_installationProgressWidget     , false);
    setBackEnabled(m_installationProgressWidget     , false);
    setFinishEnabled(m_installationProgressWidget   , false);
    setHelpEnabled(m_installationProgressWidget     , false);

    // for the finish page 
    setNextEnabled(m_finishWidget     , false);
    setBackEnabled(m_finishWidget     , false);
    setFinishEnabled(m_finishWidget   , true);
    setHelpEnabled(m_finishWidget     , false);
}



void KNSPluginWizard::connectSignals() {
    connect(&m_installEngine, SIGNAL(installProgress(int)), m_installationProgressBar, SLOT(setProgress(int)) );
    connect(&m_installEngine, SIGNAL(installCompleted()), this, SLOT(slotInstallationCompleted()) );
     connect(&m_installEngine, SIGNAL(installFailed()), this, SLOT(slotInstallationFailed()) );
     

}

void KNSPluginWizard::showPage(QWidget *page)
{

    // if the licence page is shown set the label and the licence content
    if(page == m_licenceVBox && m_licencePageLabel->text().isEmpty()) {
        KNSPluginInfo info =  static_cast<PluginListItem *>(m_pluginListView->selectedItem())->pluginInfo();
        m_licencePageLabel->setText(i18n("To install ")+info.pluginName()+i18n(" you need to agree to the following"));
        QString licence;
        licence = info.licence();
        QString tmpFile;
        if(info.licenceURL().isValid()) 
            // retrieve the licence if we have an url
            if(KIO::NetAccess::download(info.licenceURL(), tmpFile, NULL)) {
                    QFile f(tmpFile);
                    if(f.open(IO_ReadOnly)) {
                        QTextStream stream(&f);
                        stream.setEncoding(QTextStream::UnicodeUTF8);
                        licence  = stream.read();
                        f.close();
                        KIO::NetAccess::removeTempFile(tmpFile);
                    }
             } 
        // else display the licence found in the xml config
        m_licencePageText->setText(licence);

    }
    
    // if the installation page is shown start the download
    if(page == m_installationProgressWidget) {
        KNSPluginInfo info =  static_cast<PluginListItem *>(m_pluginListView->selectedItem())->pluginInfo();
        m_installEngine.startInstall(info);

    }

    // If we must display the finish page
    if(page == m_finishWidget) {
        if(m_installationComplete)  {
            m_finishLabel->setText(i18n("Installation completed. Reload the page."));

        } else
            m_finishLabel->setText(i18n("Installation failed"));

    }

    
    KWizard::showPage(page);
}

int KNSPluginWizard::exec()
{
    if(!m_installEngine.pluginList().count())
        return QDialog::Rejected;

    return KWizard::exec();
}


bool KNSPluginWizard::pluginAvailable()
{
    return m_installEngine.pluginAvailable();
}

void KNSPluginWizard::slotAgreementClicked(int id)
{
    if( id == m_agreementButtonGroup->id(m_licencePageAgree) ) {
        setNextEnabled(m_licenceVBox, true);
   
    } else {
        setNextEnabled(m_licenceVBox, false);
    }
       
}

void KNSPluginWizard::slotInstallationCompleted() 
{
    m_installationComplete = true;
    // enable next button 
    setNextEnabled(m_installationProgressWidget, true);
    next();
}
void KNSPluginWizard::slotInstallationFailed()
{
    m_installationComplete = false;
    showPage(m_finishWidget);
}


// KNSPlugin info copy constructor

KNSPluginInfo::KNSPluginInfo()
{

}

// KNSPlugin info constructor par an xml dom fragment
KNSPluginInfo::KNSPluginInfo(QDomNode pluginNode)
{
    QDomElement e;
    QDomNode node;

    // Read plugin informations
    node = pluginNode.firstChild();
    while(!node.isNull()) {
        e = node.toElement();
        if( e.tagName() == "pluginname") {
            kdDebug(DEBUG_NUMBER) << "found name " << e.text() << endl;
            m_pluginName = e.text();
        }
        
        if( e.tagName() == "pluginurl") {
            kdDebug(DEBUG_NUMBER) << "found plugin url " << e.text() << endl;
            m_pluginURL = KURL(e.text());
        }

        if( e.tagName() == "licence") {
            kdDebug(DEBUG_NUMBER) << "found licence " << e.text() << endl;
            m_licence = e.text();
        }            
        
        if( e.tagName() == "licenceurl") {
            kdDebug(DEBUG_NUMBER) << "found licenceurl " << e.text() << endl;
            m_licenceURL = KURL(e.text());
        }            
        
        if( e.tagName() == "pluginfile") {
            kdDebug(DEBUG_NUMBER) << "found pluginfile " << e.text() << endl;
            m_pluginFileList.append(e.text());
        }
        node = node.nextSibling();
    }
}


KNSPluginInfo::~KNSPluginInfo()
{

}


bool KNSPluginInfo::isValid() const
{
    // tell if the pluginInfo is a valid One
    if( m_pluginName.isEmpty() || ( m_licence.isEmpty() && !m_licenceURL.isValid() ) || !m_pluginURL.isValid() || m_pluginFileList.empty() ) {
        kdDebug(DEBUG_NUMBER) << "invalid plugin info" << endl;
        return false;

    }
    
    else {

        kdDebug(DEBUG_NUMBER) << "valid plugin info" << endl;
        return true;
    }
}

// Accesors
QString KNSPluginInfo::pluginName() const
{
    return m_pluginName;
}

QString KNSPluginInfo::licence() const
{
    return m_licence;
}

KURL KNSPluginInfo::licenceURL() const
{
    return m_licenceURL;
}

KURL KNSPluginInfo::pluginURL() const
{
    return m_pluginURL;
}

const QStringList& KNSPluginInfo::pluginFileList() const
{
    return m_pluginFileList;
}