summaryrefslogtreecommitdiffstats
path: root/doc/scripting.qdoc
blob: e7e398c61e3c5cc9c1a7b7c8accbcd11069a8f68 (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
/****************************************************************************
**
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the Qt Installer Framework.
**
** $QT_BEGIN_LICENSE:FDL$
** 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 The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** GNU Free Documentation License Usage
** Alternatively, this file may be used under the terms of the GNU Free
** Documentation License version 1.3 as published by the Free Software
** Foundation and appearing in the file included in the packaging of
** this file. Please review the following information to ensure
** the GNU Free Documentation License version 1.3 requirements
** will be met: http://www.gnu.org/copyleft/fdl.html.
** $QT_END_LICENSE$
**
****************************************************************************/

/*!
    \contentspage{index.html}{Qt Installer Framework}
    \previouspage noninteractive.html
    \page scripting.html
    \nextpage operations.html

    \title 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.

    \section1 Construction

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

    The following code snippet places the \c 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 \c false.

    \code
    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;
    }
    \endcode

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

    \section1 Installer Hooks

    You can add the following hook methods into your script:

    \table
        \header
            \li Method
            \li Description
        \row
            \li \c{Component.prototype.retranslateUi}
            \li Called when the language of the installer changes.
        \row
            \li \c{Component.prototype.createOperations}
            \li See \l component::createOperations().
        \row
            \li \c{Component.prototype.createOperationsForArchive}
            \li See \l component::createOperationsForArchive().
        \row
            \li \c{Component.prototype.createOperationsForPath}
            \li See \l component::createOperationsForPath().
    \endtable

    \section1 Global Variables

    The installer puts the following symbols into the script space:

    \table
        \header
            \li Symbol
            \li Description
        \row
            \li installer
            \li Reference to the \l QInstaller of the component
        \row
            \li component
            \li Reference to the \l Component of the component
    \endtable

    \section1 Message Boxes

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

    \list
    \li QMessageBox::critical()
    \li QMessageBox::information()
    \li QMessageBox::question()
    \li QMessageBox::warning()
    \endlist

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

    \section1 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
    \l 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 \l{Predefined Variables}.

    For a summary of all available operations, see \l{Operations}.

    \section1 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:

  \code
  #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;
    }
  };
  \endcode

    Finally, you need to register your custom operation class, as follows:
  \code
  #include <UpdateOperationFactory>

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

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

  \section1 Predefined Variables

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

    \table
        \header
            \li Symbol
            \li Description
        \row
            \li ProductName
            \li Name of the product to be installed, as defined in config.xml.
        \row
            \li ProductVersion
            \li Version number of the product to be installed, as defined in
                config.xml.
        \row
            \li Title
            \li Title of the installation program, as defined in config.xml.
        \row
            \li Publisher
            \li Publisher of the installation program, as defined in config.xml.
        \row
            \li Url
            \li Product URL, as defined in config.xml.
        \row
            \li StartMenuDir
            \li Start menu group, as defined in config.xml. Only available on Windows.
        \row
            \li TargetDir
            \li Target directory for installation, as selected by the user.
        \row
            \li DesktopDir
            \li Name of the directory that contains the user's desktop.

            Only available on Windows.
        \row
            \li os
            \li Current platform: \c "x11", \c "win", or \c "mac".

                This variable is deprecated: Use \l systemInfo instead.
        \row
            \li RootDir
            \li Root directory of the filesystem.
        \row
            \li HomeDir
            \li Home directory of the current user.
        \row
            \li ApplicationsDir
            \li Applications directory.

                For example, \c {C:\Program Files} on Windows,
                \c {/opt} on Linux and \c {/Applications} on OS X.

                See also the table that lists examples of \l {Applications-directory-on-Windows}
                {applications directories on Windows}.
        \row
            \li ApplicationsDirUser
            \li Applications directory for user-specific programs. This is useful on macOS,
                on other platforms it is the same as \c ApplicationsDir.

                For example, \c {$HOME/Applications} on macOS.
        \row
            \li ApplicationsDirX86
            \li Applications Directory for 32 bit programs. This is useful on Windows,
                on other platforms it is the same as \c ApplicationsDir.

                For example, \c {C:\Program Files (x86)} on Windows.

                See also the table that lists examples of \l {Applications-directory-on-Windows}
                {applications directories on Windows}.
        \row
            \li ApplicationsDirX64
            \li Applications Directory for 64 bit programs. This is useful on Windows,
                on other platforms it is the same as \c ApplicationsDir.

                For example, \c {C:\Program Files} on Windows.

                See also the table that lists examples of \l {Applications-directory-on-Windows}
                {applications directories on Windows}.
        \row
            \li InstallerDirPath
            \li The directory that contains the installer application executable.
        \row
            \li InstallerFilePath
            \li The file path of the installer application executable.
        \row
            \li UserStartMenuProgramsPath
            \li The path to the folder containing the items in the Start menu of the user.

                For example, \c {C:\Users\USERNAME\AppData\Roaming\Microsoft\Windows\Start Menu\Programs}

                Only available on Windows.
        \row
            \li AllUsersStartMenuProgramsPath
            \li The path to the folder containing the items in the Start menu for all users.

                For example, \c {C:\ProgramData\Microsoft\Windows\Start Menu\Programs}

                Only available on Windows.
    \endtable

    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:

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


    \target Applications-directory-on-Windows
    For example, applications directory on Windows:
    \table
        \header
            \li OS (Windows)
            \li Qt Installer Framework
            \li Variable
            \li Example Path
        \row
            \li {1, 3} 32bit
            \li {1, 3} 32bit
            \li ApplicationsDir
            \li \c {C:\Program Files}
        \row
            \li ApplicationsDirX86
            \li \c {C:\Program Files}
        \row
            \li ApplicationsDirX64
            \li \c {C:\Program Files}
        \row
            \li {1, 6} 64bit
            \li {1, 3} 32bit
            \li ApplicationsDir
            \li \c {C:\Program Files (x86)}
        \row
            \li ApplicationsDirX86
            \li \c {C:\Program Files (x86)}
        \row
            \li ApplicationsDirX64
            \li \c {C:\Program Files}
        \row
            \li {1, 3} 64bit
            \li ApplicationsDir
            \li \c {C:\Program Files}
        \row
            \li ApplicationsDirX86
            \li \c {C:\Program Files (x86)}
        \row
            \li ApplicationsDirX64
            \li \c {C:\Program Files}
    \endtable
*/