System Information Example | Qt Installer Framework Manual

Qt Installer Framework Manual | System Information Example

# System Information Example

The System Information Example demonstrates how to use the systemInfo API to detect details about the target operating system.

The example installer consists of three packages: `root`, `root.i386` and `root.x86_64`. The `root.i386` and `root.x86_64` packages are assumed to contain binaries specific to the architecture. The `root` package contains logic to check for minimum operating system version. It also hides either the `root.i386` or `root.x86_64` package, based on the operating system architecture.

The logic to detect the operating system features is scripted in the `root`'s installscript.qs file.

## Helper Functions

The installscript.qs file first declares two helper functions: `cancelInstaller()` and `majorVersion()`.

      function cancelInstaller(message)
      {
          installer.setDefaultPageVisible(QInstaller.Introduction, false);
          installer.setDefaultPageVisible(QInstaller.TargetDirectory, false);
          installer.setDefaultPageVisible(QInstaller.ComponentSelection, false);
          installer.setDefaultPageVisible(QInstaller.ReadyForInstallation, false);
          installer.setDefaultPageVisible(QInstaller.StartMenuSelection, false);
          installer.setDefaultPageVisible(QInstaller.PerformInstallation, false);
          installer.setDefaultPageVisible(QInstaller.LicenseCheck, false);

          var abortText = "<font color='red'>" + message +"</font>";
          installer.setValue("FinishedText", abortText);
      }

`cancelInstaller()` sets all except the last installer page to invisible, and shows an error message on the last one. This is a technique also demonstrated in the componenterror and quitinstaller examples.

      function majorVersion(str)
      {
          return parseInt(str.split(".", 1));
      }

`majorVersion()` takes a string of the format `<number>.<number>.<number>.[...]`. It uses the built-in JavaScript functions `string.split()` and `parseInt()` to return the first `<number>` as an integer.

## Checking the Operating System Type

The actual checks are executed as soon as the package is loaded, in the `Component` constructor function.

      function Component()
      {

The function uses the built-in systemInfo.kernelType, systemInfo.kernelVersion, systemInfo::productType, and systemInfo.productVersion properties to check the minimum system requirements.

          var validOs = false;

          if (systemInfo.kernelType === "winnt") {
              if (majorVersion(systemInfo.kernelVersion) >= 6)
                  validOs = true;
          } else if (systemInfo.kernelType === "darwin") {
              if (majorVersion(systemInfo.kernelVersion) >= 11)
                  validOs = true;
          } else {
              if (systemInfo.productType !== "opensuse"
                      || systemInfo.productVersion !== "13.2") {
                  QMessageBox["warning"]("os.warning", "Installer",
                                         "Note that the binaries are only tested on OpenSUSE 13.2.",
                                         QMessageBox.Ok);
              }
              validOs = true;
          }

The script uses systemInfo.productType to differentiate between Windows, OS X, and individual Linux distributions.

For OS X and Windows, the script checks the operating system kernel version. For a mapping of kernel to operating system versions, see Darwin and Windows NT.

In the case of Linux, it checks the distribution name and version. If it does not match the specific distribution and version the binaries are presumably built for, the installer shows a warning in a modal dialog, but allows installation.

If the Windows or OS X version is too old, though, the script calls the `cancelInstaller()` helper function to prevent an actual installation:

          if (!validOs) {
              cancelInstaller("Installation on " + systemInfo.prettyProductName + " is not supported");
              return;
          }

## Checking the Operating System Architecture

The next section demonstrates the use of systemInfo.currentCpuArchitecture to choose the appropriate sub-package for a particular architecture:

          installer.componentByName("root.i386").setValue("Virtual", "true");
          installer.componentByName("root.x86_64").setValue("Virtual", "true");

          if ( systemInfo.currentCpuArchitecture === "i386") {
              installer.componentByName("root.i386").setValue("Virtual", "false");
              installer.componentByName("root.i386").setValue("Default", "true");
          }
          if ( systemInfo.currentCpuArchitecture === "x86_64") {
              installer.componentByName("root.x86_64").setValue("Virtual", "false");
              installer.componentByName("root.x86_64").setValue("Default", "true");
          }
      }

Depending on the operating system architecture, either the package `root.i386` or `root.x86_64` is marked `Virtual`, hiding it from the user. For the package that matches the local architecture, the `Default` property is set to check the package by default.

Files:

- systeminfo/config/config.xml
- systeminfo/packages/root/meta/installscript.qs
- systeminfo/packages/root/meta/package.xml
- systeminfo/packages/root.i386/meta/package.xml
- systeminfo/packages/root.x86_64/meta/package.xml
- systeminfo/systeminfo.pro