Component Scripting | Qt Installer Framework Manual

Qt Installer Framework Manual | Component Scripting

Controller Scripting ◦ Operations

# Component Scripting

For each component, you can specify one script that prepares the operations to be performed by the installer. The script format has to be compatible with QJSEngine.

## Construction

The script has to contain a `Component` object that the installer creates when it loads the script. Therefore, the script must contain at least the `Component()` function, which performs initialization, such as putting pages in the correct places or connecting signals and slots.

The following code snippet places the `ErrorPage` (which is the class name of the user interface file loaded from errorpage.ui) in front of the ready for installation page and sets its completeness to `false`.

      function Component()
      {
          // Add a user interface file called ErrorPage, which should not be complete
          installer.addWizardPage( component, "ErrorPage", QInstaller.ReadyForInstallation );
          component.userInterface( "ErrorPage" ).complete = false;
      }

For more information, see the documentation for installer::addWizardPage() and component::userInterface().

## Installer Hooks

You can add the following hook methods into your script:

Method | Description
--- | ---
`Component.prototype.retranslateUi` | Called when the language of the installer changes.
`Component.prototype.createOperations` | See component::createOperations().
`Component.prototype.createOperationsForArchive` | See component::createOperationsForArchive().
`Component.prototype.createOperationsForPath` | See component::createOperationsForPath().

## Global Variables

The installer puts the following symbols into the script space:

Symbol | Description
--- | ---
installer | Reference to the QInstaller of the component
component | Reference to the Component of the component

## Message Boxes

You can show a QMessageBox from within the script by using the following static members:

- QMessageBox::critical()
- QMessageBox::information()
- QMessageBox::question()
- QMessageBox::warning()

For your convenience, the values for QMessageBox::StandardButton are made available by using `QMessageBox.Ok`, `QMessageBox.Open`, and so on.

## Adding Operations to Components

You might want to add custom operations after extracting the content, when copying files or patching file content, for example. You can create and add update operations to the installation from within a script using component::addOperation(). If you need to run an operation that requires administrative rights, use component::addElevatedOperation() instead.

Operations need to be added before the actual installation step. Override component::createOperations() to register custom operations for a component.

Each operation has a unique key used for identification and can take up to five parameters. In the parameter values, you can use variables as set in installer::setValue(). For more information, see Predefined Variables.

For a summary of all available operations, see Operations.

## Registering Custom Operations

You can register custom installation operations in the installer by deriving the KDUpdater::UpdateOperation class. The following code displays the methods that you must implement:

      #include <UpdateOperation>

      class CustomOperation : public KDUpdater::UpdateOperation
      {
      public:
        CustomOperation()
        {
            setName( "CustomOperation" );
        }

        void backup()
        {
            // do whatever is needed to restore the state in undoOperation()
        }

        bool performOperation()
        {
            const QStringList args = arguments();
            // do whatever is needed to do for the given arguments

            bool success = ...;
            return success;
        }

        void undoOperation()
        {
            // restore the previous state, as saved in backup()
        }

        bool testOperation()
        {
            // currently unused
            return true;
        }

        CustomOperation* clone() const
        {
            return new CustomOperation;
        }

        QDomDocument toXml()
        {
            // automatically adds the operation's arguments and everything set via setValue
            QDomDocument doc = KDUpdater::UpdateOperation::toXml();

            // if you need any information to undo the operation you did,
            // add them to the doc here

            return doc;
        }

        bool fromXml( const QDomDocument& doc )
        {
            // automatically loads the operation's arguments and everything set via setValue
            if( !KDUpdater::UpdateOperation::fromXml( doc ) )
                return false;

            // if you need any information to undo the operation you did,
            // read them from the doc here

            return true;
        }
      };

Finally, you need to register your custom operation class, as follows:

      #include <UpdateOperationFactory>

      KDUpdater::UpdateOperationFactory::instance().registerUpdateOperation< CustomOperation >( "CustomOperation" );

Now you can use your operation in the installer in the same way as the predefined operations.

## Predefined Variables

You can use the following predefined variables in scripts to facilitate directory access:

Symbol | Description
--- | ---
ProductName | Name of the product to be installed, as defined in config.xml.
ProductVersion | Version number of the product to be installed, as defined in config.xml.
Title | Title of the installation program, as defined in config.xml.
Publisher | Publisher of the installation program, as defined in config.xml.
Url | Product URL, as defined in config.xml.
StartMenuDir | Start menu group, as defined in config.xml. Only available on Windows.
TargetDir | Target directory for installation, as selected by the user.
DesktopDir | Name of the directory that contains the user's desktop. Only available on Windows.
os | Current platform: `"x11"`, `"win"`, or `"mac"`. This variable is deprecated: Use systemInfo instead.
RootDir | Root directory of the filesystem.
HomeDir | Home directory of the current user.
ApplicationsDir | Applications directory. For example, `C:\Program Files` on Windows, `/opt` on Linux and `/Applications` on OS X. See also the table that lists examples of applications directories on Windows.
ApplicationsDirX86 | Applications Directory for 32 bit programs. This is useful on Windows, on other platforms it is the same as `ApplicationsDir`. For example, `C:\Program Files (x86)` on Windows. See also the table that lists examples of applications directories on Windows.
ApplicationsDirX64 | Applications Directory for 64 bit programs. This is useful on Windows, on other platforms it is the same as `ApplicationsDir`. For example, `C:\Program Files` on Windows. See also the table that lists examples of applications directories on Windows.
InstallerDirPath | The directory that contains the installer application executable.
InstallerFilePath | The file path of the installer application executable.
UserStartMenuProgramsPath | The path to the folder containing the items in the Start menu of the user. For example, `C:\Users\USERNAME\AppData\Roaming\Microsoft\Windows\Start Menu\Programs` Only available on Windows.
AllUsersStartMenuProgramsPath | The path to the folder containing the items in the Start menu for all users. For example, `C:\ProgramData\Microsoft\Windows\Start Menu\Programs` Only available on Windows.

The variables can be resolved by calls to installer::value(). If embedded in '@' they can also be part of strings passed as arguments to installation operations:

      if (installer.value("os") === "win") {
          component.addOperation("CreateShortcut", "@TargetDir@/MyApp.exe", "@StartMenuDir@/MyApp.lnk");
      }

For example, applications directory on Windows:

OS (Windows) | Qt Installer Framework | Variable | Example Path
--- | --- | --- | ---
32bit | 32bit | ApplicationsDir | `C:\Program Files`
ApplicationsDirX86 | `C:\Program Files`
ApplicationsDirX64 | `C:\Program Files`
64bit | 32bit | ApplicationsDir | `C:\Program Files (x86)`
ApplicationsDirX86 | `C:\Program Files (x86)`
ApplicationsDirX64 | `C:\Program Files`
64bit | ApplicationsDir | `C:\Program Files`
ApplicationsDirX86 | `C:\Program Files (x86)`
ApplicationsDirX64 | `C:\Program Files`

Controller Scripting ◦ Operations