summaryrefslogtreecommitdiffstats
path: root/src/libs/installer/scriptengine.cpp
blob: 057ce9c85134158dfb26995e4f457e50ae3c32f9 (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
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
/**************************************************************************
**
** Copyright (C) 2012-2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the Qt Installer Framework.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
**
** $QT_END_LICENSE$
**
**************************************************************************/
#include "scriptengine.h"

#include "messageboxhandler.h"
#include "errors.h"
#include "scriptengine_p.h"
#include "systeminfo.h"

#include <QMetaEnum>
#include <QQmlEngine>
#include <QUuid>
#include <QWizard>

namespace QInstaller {

/*!
    \class QInstaller::ScriptEngine
    \inmodule QtInstallerFramework
    \brief The ScriptEngine class is used to prepare and run the component scripts.
*/

/*!
    \qmltype console
    \inqmlmodule scripting
    \brief Provides methods for logging and debugging.

    You can use the \c console object to print log information about installer
    functions to the console. The following example uses the \c console object
    \l{console::log()}{log} method and \l installer object
    \l{installer::isUpdater()}, \l{installer::isUninstaller()}, and
    \l{installer::isPackageManager()} methods to display a message that
    indicates whether the maintenance tool is currently being used to update,
    remove, or add components.

    \code
    onPackageManagerCoreTypeChanged = function()
    {
        console.log("Is Updater: " + installer.isUpdater());
        console.log("Is Uninstaller: " + installer.isUninstaller());
        console.log("Is Package Manager: " + installer.isPackageManager());
    }
    \endcode
*/

/*!
    \qmlmethod void console::log(string value)

    Prints the string specified by \a value to the console.
*/

/*!
    \qmltype QFileDialog
    \inqmlmodule scripting
    \brief Provides a dialog that allows users to select files or directories.

    Use the QFileDialog::getExistingDirectory() method to create a modal dialog
    that displays an existing directory selected by the user. Use the
    QFileDialog::getOpenFileName() method to create a dialog that displays
    matching files in the directory selected by the user.
*/

/*!
    \qmlmethod string QFileDialog::getExistingDirectory(string caption, string dir)

    Returns an existing directory selected by the user.

    The dialog's working directory is set to \a dir, and the caption is set to
    \a caption. Either of these may be an empty string, in which case the
    current directory and a default caption will be used, respectively.
*/

/*!
    \qmlmethod string QFileDialog::getOpenFileName(string caption, string dir, string filter)

    Returns an existing file selected by the user. If the user selects
    \uicontrol Cancel, returns a null string.

    The file dialog's caption is set to \a caption. If \c caption is not
    specified, a default caption is used.

    The file dialog's working directory is set to \a dir. If \c dir includes a
    file name, the file will be selected. Only files that match the specified
    \a filter are shown. Either of these may be an empty string.

    To specify multiple filters, separate them with two semicolons (;;). For
    example:

    \code
    "Images (*.png *.xpm *.jpg);;Text files (*.txt);;XML files (*.xml)"
    \endcode

    On Windows, and OS X, this static function will use the native file dialog
    and not a QFileDialog.
*/

/*!
    \qmltype print
    \inqmlmodule scripting
*/

/*!
    \qmltype buttons
    \inqmlmodule scripting

    \brief Provides buttons that can be used on installer pages.

    You can use a set of standard buttons and some custom buttons on the
    installer pages. For more information about the buttons used by default on
    each installer page, see \l {Controller Scripting}.
*/

/*!
    \qmlproperty enumeration buttons::QWizard

    Specifies the buttons on an installer page.

    \value  buttons.BackButton
            The \uicontrol Back button (\uicontrol {Go Back} on OS X.)
    \value  buttons.NextButton
            The \uicontrol Next button (\uicontrol Continue on OS X.)
    \value  buttons.CommitButton
            The \uicontrol Commit button.
    \value  buttons.FinishButton
            The \uicontrol Finish button (\uicontrol Done on OS X.)
    \value  buttons.CancelButton
            The \uicontrol Cancel button.
    \value  buttons.HelpButton
            The \uicontrol Help button.
    \value  buttons.CustomButton1
            A custom button.
    \value  buttons.CustomButton2
            A custom button.
    \value  buttons.CustomButton3
            A custom button.
*/

/*!
    \qmltype QDesktopServices
    \inqmlmodule scripting

    \brief Provides methods for accessing common desktop services.

    Many desktop environments provide services that can be used by applications
    to perform common tasks, such as opening a file, in a way that is both
    consistent and takes into account the user's application preferences.

    This object contains methods that provide simple interfaces to these
    services that indicate whether they succeeded or failed.

    The openUrl() method is used to open files located at arbitrary URLs in
    external applications. For URLs that correspond to resources on the local
    filing system (where the URL scheme is "file"), a suitable application is
    used to open the file.

    The displayName() and storageLocation() methods take one of the following
    enums as an argument:

    \list
        \li DesktopServices.DesktopLocation
        \li DesktopServices.DocumentsLocation
        \li DesktopServices.FontsLocation
        \li DesktopServices.ApplicationsLocation
        \li DesktopServices.MusicLocation
        \li DesktopServices.MoviesLocation
        \li DesktopServices.PicturesLocation
        \li DesktopServices.TempLocation
        \li DesktopServices.HomeLocation
        \li DesktopServices.DataLocation
        \li DesktopServices.CacheLocation
        \li DesktopServices.GenericDataLocation
        \li DesktopServices.RuntimeLocation
        \li DesktopServices.ConfigLocation
        \li DesktopServices.DownloadLocation
        \li DesktopServices.GenericCacheLocation
        \li DesktopServices.GenericConfigLocation
    \endlist

    The enum values correspond to the values of the
    \l{QStandardPaths::StandardLocation} enum with the same names.
*/

/*!
    \qmlproperty enumeration QDesktopServices::QStandardPaths
    \internal
*/

/*!
    \qmlmethod boolean QDesktopServices::openUrl(string url)

    Uses the URL scheme \c file to open the specified \a url with a suitable
    application.
*/

/*!
    \qmlmethod string QDesktopServices::displayName(int location)

    Returns a localized display name for the specified \a location or an empty
    QString if no relevant location can be found.
*/

/*!
    \qmlmethod string QDesktopServices::storageLocation(int location)

    Returns the specified \a location.
*/

/*!
    \qmltype QInstaller
    \inqmlmodule scripting

    \brief Provides access to the installer status and pages from Qt Script.

    For more information about using the \c QInstaller object in control
    scripts, see \l{Controller Scripting}.

    For examples of using the pages to support end user workflows, see
    \l{End User Workflows}.

*/

/*!
   \qmlproperty enumeration QInstaller::WizardPage

    The installer has various pre-defined pages that can be used to for example insert pages
    in a certain place:

    \value  QInstaller.Introduction
            \l{Introduction Page}
    \value  QInstaller.TargetDirectory
            \l{Target Directory Page}
    \value  QInstaller.ComponentSelection
            \l{Component Selection Page}
    \value  QInstaller.LicenseCheck
            \l{License Agreement Page}
    \value  QInstaller.StartMenuSelection
            \l{Start Menu Directory Page}
    \value  QInstaller.ReadyForInstallation
            \l{Ready for Installation Page}
    \value  QInstaller.PerformInstallation
            \l{Perform Installation Page}
    \value  QInstaller.InstallationFinished
            \l{Finished Page}

    \omitvalue  QInstaller.End
*/


/*!
    \qmlproperty enumeration QInstaller::status

    Status of the installer.

    Possible values are:

    \value  QInstaller.Success
            Installation was successful.
    \value  QInstaller.Failure
            Installation failed.
    \value  QInstaller.Running
            Installation is in progress.
    \value  QInstaller.Canceled
            Installation was canceled.
    \value  QInstaller.Unfinished
            Installation was not completed.
    \value  QInstaller.ForceUpdate
*/

/*!
    \qmltype gui
    \inqmlmodule scripting
    \brief Enables interaction with the installer UI.
*/

/*!
    \qmlsignal gui::interrupted()
*/

/*!
    \qmlsignal gui::languageChanged()
*/

/*!
    \qmlsignal gui::finishButtonClicked()
*/

/*!
    \qmlsignal gui::gotRestarted()
*/

/*!
    \qmlsignal gui::settingsButtonClicked();
*/

QJSValue InstallerProxy::componentByName(const QString &componentName)
{
    if (m_core)
        return m_engine->newQObject(m_core->componentByName(componentName));
    return QJSValue();
}

GuiProxy::GuiProxy(ScriptEngine *engine, QObject *parent) :
    QObject(parent),
    m_engine(engine),
    m_gui(0)
{
}

void GuiProxy::setPackageManagerGui(PackageManagerGui *gui)
{
    if (m_gui) {
        disconnect(m_gui, &PackageManagerGui::interrupted, this, &GuiProxy::interrupted);
        disconnect(m_gui, &PackageManagerGui::languageChanged, this, &GuiProxy::languageChanged);
        disconnect(m_gui, &PackageManagerGui::finishButtonClicked, this, &GuiProxy::finishButtonClicked);
        disconnect(m_gui, &PackageManagerGui::gotRestarted, this, &GuiProxy::gotRestarted);
        disconnect(m_gui, &PackageManagerGui::settingsButtonClicked, this, &GuiProxy::settingsButtonClicked);
    }

    m_gui = gui;

    if (m_gui) {
        connect(m_gui, &PackageManagerGui::interrupted, this, &GuiProxy::interrupted);
        connect(m_gui, &PackageManagerGui::languageChanged, this, &GuiProxy::languageChanged);
        connect(m_gui, &PackageManagerGui::finishButtonClicked, this, &GuiProxy::finishButtonClicked);
        connect(m_gui, &PackageManagerGui::gotRestarted, this, &GuiProxy::gotRestarted);
        connect(m_gui, &PackageManagerGui::settingsButtonClicked, this, &GuiProxy::settingsButtonClicked);
    }
}

/*!
    \qmlmethod object gui::pageById(int id)

    Returns the installer page specified by \a id. The values of \c id for the
    available installer pages are provided by QInstaller::WizardPage.
*/
QJSValue GuiProxy::pageById(int id) const
{
    if (!m_gui)
        return QJSValue();
    return m_engine->newQObject(m_gui->pageById(id));
}

/*!
    \qmlmethod object gui::pageByObjectName(string name)

    Returns the installer page specified by \a name. The value of \c name is the
    object name set in the UI file that defines the installer page.
*/
QJSValue GuiProxy::pageByObjectName(const QString &name) const
{
    if (!m_gui)
        return QJSValue();
    return m_engine->newQObject(m_gui->pageByObjectName(name));
}

/*!
    \qmlmethod object gui::currentPageWidget()

    Returns the current wizard page.
*/
QJSValue GuiProxy::currentPageWidget() const
{
    if (!m_gui)
        return QJSValue();
    return m_engine->newQObject(m_gui->currentPageWidget());
}

/*!
    \qmlmethod object gui::pageWidgetByObjectName(string name)
*/
QJSValue GuiProxy::pageWidgetByObjectName(const QString &name) const
{
    if (!m_gui)
        return QJSValue();
    return m_engine->newQObject(m_gui->pageWidgetByObjectName(name));
}

/*!
    \qmlmethod string gui::defaultButtonText(int wizardButton)
*/
QString GuiProxy::defaultButtonText(int wizardButton) const
{
    if (!m_gui)
        return QString();
    return m_gui->defaultButtonText(wizardButton);
}

/*!
    \qmlmethod void gui::clickButton(int wizardButton, int delayInMs)

    Automatically clicks the button specified by \a wizardButton after a delay
    in milliseconds specified by \a delayInMs.
*/
void GuiProxy::clickButton(int wizardButton, int delayInMs)
{
    if (m_gui)
        m_gui->clickButton(wizardButton, delayInMs);
}

/*!
    \qmlmethod boolean gui::isButtonEnabled(int wizardButton)
*/
bool GuiProxy::isButtonEnabled(int wizardButton)
{
    if (!m_gui)
        return false;
    return m_gui->isButtonEnabled(wizardButton);
}

/*!
    \qmlmethod void gui::showSettingsButton(boolean show)
*/
void GuiProxy::showSettingsButton(bool show)
{
    if (m_gui)
        m_gui->showSettingsButton(show);
}

/*!
    \qmlmethod void gui::setSettingsButtonEnabled(boolean enable)
*/
void GuiProxy::setSettingsButtonEnabled(bool enable)
{
    if (m_gui)
        m_gui->setSettingsButtonEnabled(enable);
}

/*!
    \qmlmethod object gui::findChild(object parent, string objectName)

    Returns the first descendant of \a parent that has \a objectName as name.

    \sa QObject::findChild
*/
QJSValue GuiProxy::findChild(QObject *parent, const QString &objectName)
{
    return m_engine->newQObject(parent->findChild<QObject*>(objectName));
}

/*!
    \qmlmethod object[] gui::findChildren(object parent, string objectName)

    Returns all descendants of \a parent that have \a objectName as name.

    \sa QObject::findChildren
*/
QList<QJSValue> GuiProxy::findChildren(QObject *parent, const QString &objectName)
{
    QList<QJSValue> children;
    foreach (QObject *child, parent->findChildren<QObject*>(objectName))
        children.append(m_engine->newQObject(child));
    return children;
}

/*!
    \qmlmethod void gui::cancelButtonClicked()
*/
void GuiProxy::cancelButtonClicked()
{
    if (m_gui)
        m_gui->cancelButtonClicked();
}

/*!
    \qmlmethod void gui::reject()
*/
void GuiProxy::reject()
{
    if (m_gui)
        m_gui->reject();
}

/*!
    \qmlmethod void gui::rejectWithoutPrompt()
*/
void GuiProxy::rejectWithoutPrompt()
{
    if (m_gui)
        m_gui->rejectWithoutPrompt();
}

/*!
    \qmlmethod void gui::showFinishedPage()
*/
void GuiProxy::showFinishedPage()
{
    if (m_gui)
        m_gui->showFinishedPage();
}

/*!
    \qmlmethod void gui::setModified(boolean value)
*/
void GuiProxy::setModified(bool value)
{
    if (m_gui)
        m_gui->setModified(value);
}


/*!
    Constructs a script engine with \a core as parent.
*/
ScriptEngine::ScriptEngine(PackageManagerCore *core) :
    QObject(core),
    m_guiProxy(new GuiProxy(this, this))
{
    QJSValue global = m_engine.globalObject();
    global.setProperty(QLatin1String("console"), m_engine.newQObject(new ConsoleProxy));
    global.setProperty(QLatin1String("QFileDialog"), m_engine.newQObject(new QFileDialogProxy));
    const QJSValue proxy = m_engine.newQObject(new InstallerProxy(this, core));
    global.setProperty(QLatin1String("InstallerProxy"), proxy);
    global.setProperty(QLatin1String("print"), m_engine.newQObject(new ConsoleProxy)
        .property(QLatin1String("log")));
#if QT_VERSION < 0x050400
    global.setProperty(QLatin1String("qsTr"), m_engine.newQObject(new QCoreApplicationProxy)
        .property(QStringLiteral("qsTr")));
#else
    m_engine.installTranslatorFunctions();
#endif
    global.setProperty(QLatin1String("systemInfo"), m_engine.newQObject(new SystemInfo));

    global.setProperty(QLatin1String("QInstaller"), generateQInstallerObject());
    global.setProperty(QLatin1String("buttons"), generateWizardButtonsObject());
    global.setProperty(QLatin1String("QMessageBox"), generateMessageBoxObject());
    global.setProperty(QLatin1String("QDesktopServices"), generateDesktopServicesObject());

    if (core) {
        setGuiQObject(core->guiObject());
        QQmlEngine::setObjectOwnership(core, QQmlEngine::CppOwnership);
        global.setProperty(QLatin1String("installer"), m_engine.newQObject(core));
        connect(core, SIGNAL(guiObjectChanged(QObject*)), this, SLOT(setGuiQObject(QObject*)));

        const QList<Component*> all = core->components(PackageManagerCore::ComponentType::All);
        QJSValue scriptComponentsObject = m_engine.newArray(all.count());
        for (int i = 0; i < all.count(); ++i) {
            Component *const component = all.at(i);
            QQmlEngine::setObjectOwnership(component, QQmlEngine::CppOwnership);
            scriptComponentsObject.setProperty(i, newQObject(component));
        }
        global.property(QLatin1String("installer")).setProperty(QLatin1String("components"),
            scriptComponentsObject);
    } else {
        global.setProperty(QLatin1String("installer"), m_engine.newQObject(new QObject));
    }
    global.setProperty(QLatin1String("gui"), m_engine.newQObject(m_guiProxy));

    global.property(QLatin1String("installer")).setProperty(QLatin1String("componentByName"),
        proxy.property(QLatin1String("componentByName")));
}

/*!
    Creates a JavaScript object that wraps the given QObject \a object.

    Signals and slots, properties and children of \a object are
    available as properties of the created QJSValue. In addition some helper methods and properties
    are added:

    findChild(), findChildren() recursively search for child objects with the given object name.

    Direct child objects are made accessible as properties under their respective object names.
 */
QJSValue ScriptEngine::newQObject(QObject *object)
{
    QJSValue jsValue = m_engine.newQObject(object);
    if (!jsValue.isQObject())
        return jsValue;

    QQmlEngine::setObjectOwnership(object, QQmlEngine::CppOwnership);

    // add findChild(), findChildren() methods known from QtScript
    QJSValue findChild = m_engine.evaluate(
                QLatin1String("(function() { return gui.findChild(this, arguments[0]); })"));
    QJSValue findChildren = m_engine.evaluate(
                QLatin1String("(function() { return gui.findChildren(this, arguments[0]); })"));
    jsValue.setProperty(QLatin1String("findChild"), findChild);
    jsValue.setProperty(QLatin1String("findChildren"), findChildren);

    // add all named children as properties
    foreach (QObject *const child, object->children()) {
        if (child->objectName().isEmpty())
            continue;
        jsValue.setProperty(child->objectName(), m_engine.newQObject(child));
        newQObject(child);
    }

    return jsValue;
}

QJSValue ScriptEngine::evaluate(const QString &program, const QString &fileName, int lineNumber)
{
    return m_engine.evaluate(program, fileName, lineNumber);
}

/*!
    Registers QObject \a object in the engine, and makes it globally accessible under its object name.
 */
void ScriptEngine::addToGlobalObject(QObject *object)
{
    if (!object || object->objectName().isEmpty())
        return;

    QJSValue value = newQObject(object);
    globalObject().setProperty(object->objectName(), value);
}

/*!
    Removes the \a object name from the global object.
 */
void ScriptEngine::removeFromGlobalObject(QObject *object)
{
    globalObject().deleteProperty(object->objectName());
}

/*!
    Loads a script into the given \a context at \a fileName inside the ScriptEngine.

    The installer and all its components as well as other useful stuff are being exported into the
    script. Read \link componentscripting Component Scripting \endlink for details.
    Throws Error when either the script at \a fileName could not be opened, or the QScriptEngine
    couldn't evaluate the script.
*/
QJSValue ScriptEngine::loadInContext(const QString &context, const QString &fileName,
    const QString &scriptInjection)
{
    QFile file(fileName);
    if (!file.open(QIODevice::ReadOnly)) {
        throw Error(tr("Could not open the requested script file at %1: %2.")
            .arg(fileName, file.errorString()));
    }

    // Create a closure. Put the content in the first line to keep line number order in case of an
    // exception. Script content will be added as the last argument to the command to prevent wrong
    // replacements of %1, %2 or %3 inside the javascript code.
    const QString scriptContent = QLatin1String("(function() {")
        + scriptInjection + QString::fromUtf8(file.readAll())
        + QString::fromLatin1(";"
        "    if (typeof %1 != \"undefined\")"
        "        return new %1;"
        "    else"
        "        throw \"Missing Component constructor. Please check your script.\";"
        "})();").arg(context);
    QJSValue scriptContext = evaluate(scriptContent, fileName);
    scriptContext.setProperty(QLatin1String("Uuid"), QUuid::createUuid().toString());
    if (scriptContext.isError()) {
        throw Error(tr("Exception while loading the component script '%1'. (%2)").arg(
            QFileInfo(file).absoluteFilePath(), scriptContext.toString().isEmpty() ?
            QString::fromLatin1("Unknown error.") : scriptContext.toString()));
    }
    return scriptContext;
}

/*!
    Tries to call the method specified by \a methodName with the arguments specified by
    \a arguments within the script and returns the result. If the method does not exist or
    is not callable, an undefined result is returned. If the call to the method
    succeeds and the return value is still undefined, a null value will be returned instead.
    If the method call has an exception, its string representation is thrown as an Error exception.

    \note The method is not called if \a scriptContext is the same method, to avoid
    infinite recursion.
*/
QJSValue ScriptEngine::callScriptMethod(const QJSValue &scriptContext, const QString &methodName,
    const QJSValueList &arguments)
{
    // don't allow a recursion
    const QString key = scriptContext.property(QLatin1String("Uuid")).toString();
    QStringList stack = m_callstack.value(key);
    if (m_callstack.contains(key) && stack.value(stack.size() - 1).startsWith(methodName))
        return QJSValue(QJSValue::UndefinedValue);

    stack.append(methodName);
    m_callstack.insert(key, stack);

    QJSValue method = scriptContext.property(methodName);
    if (!method.isCallable())
        return QJSValue(QJSValue::UndefinedValue);
    if (method.isError()) {
        throw Error(method.toString().isEmpty() ? QString::fromLatin1("Unknown error.")
            : method.toString());
    }

    const QJSValue result = method.call(arguments);
    if (result.isError()) {
        throw Error(result.toString().isEmpty() ? QString::fromLatin1("Unknown error.")
            : result.toString());
    }

    stack.removeLast();
    m_callstack.insert(key, stack);
    return result.isUndefined() ? QJSValue(QJSValue::NullValue) : result;
}


// -- private slots

void ScriptEngine::setGuiQObject(QObject *guiQObject)
{
    m_guiProxy->setPackageManagerGui(qobject_cast<PackageManagerGui*>(guiQObject));
}


// -- private

#undef SETPROPERTY
#define SETPROPERTY(a, x, t) a.setProperty(QLatin1String(#x), QJSValue(t::x));

QJSValue ScriptEngine::generateWizardButtonsObject()
{
    QJSValue buttons = m_engine.newArray();
    SETPROPERTY(buttons, BackButton, QWizard)
    SETPROPERTY(buttons, NextButton, QWizard)
    SETPROPERTY(buttons, CommitButton, QWizard)
    SETPROPERTY(buttons, FinishButton, QWizard)
    SETPROPERTY(buttons, CancelButton, QWizard)
    SETPROPERTY(buttons, HelpButton, QWizard)
    SETPROPERTY(buttons, CustomButton1, QWizard)
    SETPROPERTY(buttons, CustomButton2, QWizard)
    SETPROPERTY(buttons, CustomButton3, QWizard)
    return buttons;
}

/*!
    \internal
    generates QMessageBox::StandardButton enum as an QScriptValue array
*/
QJSValue ScriptEngine::generateMessageBoxObject()
{
    QJSValue messageBox = m_engine.newQObject(MessageBoxHandler::instance());

    const QMetaObject &messageBoxMetaObject = QMessageBox::staticMetaObject;
    const int index = messageBoxMetaObject.indexOfEnumerator("StandardButtons");

    QJSValue value = m_engine.newArray();
    value.setProperty(QLatin1String("NoButton"), QJSValue(QMessageBox::NoButton));

    const QMetaEnum metaEnum = messageBoxMetaObject.enumerator(index);
    for (int i = 0; i < metaEnum.keyCount(); i++) {
        const int enumValue = metaEnum.value(i);
        if (enumValue >= QMessageBox::FirstButton)
            value.setProperty(QLatin1String(metaEnum.valueToKey(enumValue)), QJSValue(enumValue));
        if (enumValue == QMessageBox::LastButton)
            break;
    }

    messageBox.setPrototype(value);
    return messageBox;
}

QJSValue ScriptEngine::generateDesktopServicesObject()
{
    QJSValue desktopServices = m_engine.newArray();
    SETPROPERTY(desktopServices, DesktopLocation, QStandardPaths)
    SETPROPERTY(desktopServices, DocumentsLocation, QStandardPaths)
    SETPROPERTY(desktopServices, FontsLocation, QStandardPaths)
    SETPROPERTY(desktopServices, ApplicationsLocation, QStandardPaths)
    SETPROPERTY(desktopServices, MusicLocation, QStandardPaths)
    SETPROPERTY(desktopServices, MoviesLocation, QStandardPaths)
    SETPROPERTY(desktopServices, PicturesLocation, QStandardPaths)
    SETPROPERTY(desktopServices, TempLocation, QStandardPaths)
    SETPROPERTY(desktopServices, HomeLocation, QStandardPaths)
    SETPROPERTY(desktopServices, DataLocation, QStandardPaths)
    SETPROPERTY(desktopServices, CacheLocation, QStandardPaths)
    SETPROPERTY(desktopServices, GenericDataLocation, QStandardPaths)
    SETPROPERTY(desktopServices, RuntimeLocation, QStandardPaths)
    SETPROPERTY(desktopServices, ConfigLocation, QStandardPaths)
    SETPROPERTY(desktopServices, DownloadLocation, QStandardPaths)
    SETPROPERTY(desktopServices, GenericCacheLocation, QStandardPaths)
    SETPROPERTY(desktopServices, GenericConfigLocation, QStandardPaths)

    QJSValue object = m_engine.newQObject(new QDesktopServicesProxy);
    object.setPrototype(desktopServices);   // attach the properties
    return object;
}

QJSValue ScriptEngine::generateQInstallerObject()
{
    // register ::WizardPage enum in the script connection
    QJSValue qinstaller = m_engine.newArray();
    SETPROPERTY(qinstaller, Introduction, PackageManagerCore)
    SETPROPERTY(qinstaller, LicenseCheck, PackageManagerCore)
    SETPROPERTY(qinstaller, TargetDirectory, PackageManagerCore)
    SETPROPERTY(qinstaller, ComponentSelection, PackageManagerCore)
    SETPROPERTY(qinstaller, StartMenuSelection, PackageManagerCore)
    SETPROPERTY(qinstaller, ReadyForInstallation, PackageManagerCore)
    SETPROPERTY(qinstaller, PerformInstallation, PackageManagerCore)
    SETPROPERTY(qinstaller, InstallationFinished, PackageManagerCore)
    SETPROPERTY(qinstaller, End, PackageManagerCore)

    // register ::Status enum in the script connection
    SETPROPERTY(qinstaller, Success, PackageManagerCore)
    SETPROPERTY(qinstaller, Failure, PackageManagerCore)
    SETPROPERTY(qinstaller, Running, PackageManagerCore)
    SETPROPERTY(qinstaller, Canceled, PackageManagerCore)
    SETPROPERTY(qinstaller, Unfinished, PackageManagerCore)
    SETPROPERTY(qinstaller, ForceUpdate, PackageManagerCore)
    return qinstaller;
}

#undef SETPROPERTY

}   // namespace QInstaller