summaryrefslogtreecommitdiffstats
path: root/doc/html/distributor-example.html
blob: 8a02555aa055ca65e07e1b421106209ff3c387d1 (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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/examples/distributor/distributor.doc:1 -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>TQt Distribution Example</title>
<style type="text/css"><!--
fn { margin-left: 1cm; text-indent: -1cm; }
a:link { color: #004faf; text-decoration: none }
a:visited { color: #672967; text-decoration: none }
body { background: #ffffff; color: black; }
--></style>
</head>
<body>

<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr bgcolor="#E5E5E5">
<td valign=center>
 <a href="index.html">
<font color="#004faf">Home</font></a>
 | <a href="classes.html">
<font color="#004faf">All&nbsp;Classes</font></a>
 | <a href="mainclasses.html">
<font color="#004faf">Main&nbsp;Classes</font></a>
 | <a href="annotated.html">
<font color="#004faf">Annotated</font></a>
 | <a href="groups.html">
<font color="#004faf">Grouped&nbsp;Classes</font></a>
 | <a href="functions.html">
<font color="#004faf">Functions</font></a>
</td>
<td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>TQt Distribution Example</h1>

 

<p> This example program modifies the hard-coded paths that are
compiled into the TQt library.
<p> When distributing the TQt library, the final installation prefix is
very rarely the same as the prefix used when doing development.
You can use the code from this example to modify the following
hard-coded paths in TQt library:
<p> <ul>
<p> <li> Prefix - Normally, all other paths are relative to the <em>Prefix</em>.
<p> <li> Binaries - Location of binaries distributed with TQt (for
example, <em>TQt Assistant</em>).
<p> <li> Documentation - Location of the TQt documentation.
<p> <li> Headers - Location of the TQt headers.
<p> <li> Libraries - Location of addition libraries distributed with TQt
(for example, the <em>tqui</em> library).
<p> <li> Plugins - Location of the TQt plugins.
<p> <li> Data - Location of applicaton specific data for all programs
distributed with TQt.
<p> </ul>
<p> <hr>
<p> Implementation:
<p> <pre>/****************************************************************************
** ui.h extension file, included from the uic-generated form implementation.
**
** If you wish to add, delete or rename functions or slots use
** TQt Designer which will update this file, preserving your code. Create an
** init() function in place of a constructor, and a destroy() function in
** place of a destructor.
*****************************************************************************/

#include &lt;<a href="qapplication-h.html">ntqapplication.h</a>&gt;
#include &lt;<a href="qcursor-h.html">ntqcursor.h</a>&gt;
#include &lt;<a href="qeventloop-h.html">ntqeventloop.h</a>&gt;
#include &lt;<a href="qfile-h.html">ntqfile.h</a>&gt;
#include &lt;<a href="qfiledialog-h.html">ntqfiledialog.h</a>&gt;
#include &lt;<a href="qfileinfo-h.html">ntqfileinfo.h</a>&gt;
#include &lt;<a href="qlineedit-h.html">ntqlineedit.h</a>&gt;
#include &lt;<a href="qmessagebox-h.html">ntqmessagebox.h</a>&gt;
#include &lt;<a href="qpushbutton-h.html">ntqpushbutton.h</a>&gt;
#include &lt;<a href="qtimer-h.html">ntqtimer.h</a>&gt;


void Distributor::init()
{
    timer = new <a href="ntqtimer.html">TQTimer</a>( this );
<a name="x2672"></a>    connect( timer, TQ_SIGNAL(<a href="ntqtimer.html#timeout">timeout</a>()), TQ_SLOT(checkLibData()) );

    cancelButton()-&gt;setAutoDefault( FALSE );
    backButton()-&gt;setAutoDefault( FALSE );

    setNextEnabled( selectLibrary, FALSE );

    setHelpEnabled( selectLibrary, FALSE );
    setHelpEnabled( modifyPaths, FALSE );
    setHelpEnabled( verifyMods, FALSE );

    setFinishEnabled( verifyMods, TRUE );
}

void Distributor::showPage( <a href="ntqwidget.html">TQWidget</a> *page )
{
    if ( page == selectLibrary ) {
        nextButton()-&gt;setDefault( TRUE );
        libFilename-&gt;setFocus();
    } else if ( page == modifyPaths ) {
        nextButton()-&gt;setDefault( TRUE );
        prefixPath-&gt;selectAll();
        prefixPath-&gt;setFocus();
    } else if ( page == verifyMods ) {
        finishButton()-&gt;setDefault( TRUE );
        finishButton()-&gt;setFocus();

        <a href="ntqstring.html">TQString</a> labeltext =
            tr("&lt;p&gt;&lt;b&gt;Current Library File:&lt;/b&gt; %1&lt;/p&gt;"
               "&lt;table border=0&gt;"
               "&lt;tr&gt;&lt;td&gt;&lt;b&gt;New Installation Prefix:&lt;/b&gt;&lt;/td&gt;&lt;td&gt;%2&lt;/td&gt;&lt;/tr&gt;"
               "&lt;tr&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;"
               "&lt;tr&gt;&lt;td&gt;&lt;b&gt;Binaries Path:&lt;/b&gt;&lt;/td&gt;&lt;td&gt;%3&lt;/td&gt;&lt;/tr&gt;"
               "&lt;tr&gt;&lt;td&gt;&lt;b&gt;Documentation Path:&lt;/b&gt;&lt;/td&gt;&lt;td&gt;%4&lt;/td&gt;&lt;/tr&gt;"
               "&lt;tr&gt;&lt;td&gt;&lt;b&gt;Headers Path:&lt;/b&gt;&lt;/td&gt;&lt;td&gt;%5&lt;/td&gt;&lt;/tr&gt;"
               "&lt;tr&gt;&lt;td&gt;&lt;b&gt;Libraries Path:&lt;/b&gt;&lt;/td&gt;&lt;td&gt;%6&lt;/td&gt;&lt;/tr&gt;"
               "&lt;tr&gt;&lt;td&gt;&lt;b&gt;Plugins Path:&lt;/b&gt;&lt;/td&gt;&lt;td&gt;%7&lt;/td&gt;&lt;/tr&gt;"
               "&lt;tr&gt;&lt;td&gt;&lt;b&gt;Data Path:&lt;/b&gt;&lt;/td&gt;&lt;td&gt;%8&lt;/td&gt;&lt;/tr&gt;"
               "&lt;/table&gt;"
               "&lt;p&gt;Please verify that these options are correct.  Press the "
               "&lt;i&gt;Finish&lt;/i&gt; button to apply these modifications to the TQt "
               "library.  Use the &lt;i&gt;Back&lt;/i&gt; button to make corrections.  Use "
               "the &lt;i&gt;Cancel&lt;/i&gt; button to abort.&lt;/p&gt;")
            .arg( libFilename-&gt;text() )
            .arg( prefixPath-&gt;text() )
            .arg( binPath-&gt;text() )
            .arg( docPath-&gt;text() )
            .arg( hdrPath-&gt;text() )
            .arg( libPath-&gt;text() )
            .arg( plgPath-&gt;text() )
            .arg( datPath-&gt;text() );
        textLabel4-&gt;setText( labeltext );
    }

<a name="x2673"></a>    TQWizard::<a href="ntqwizard.html#showPage">showPage</a>( page );
}

void Distributor::checkLibFilename( const <a href="ntqstring.html">TQString</a> &amp;filename )
{
    setNextEnabled( selectLibrary, FALSE );

    <a href="ntqfileinfo.html">TQFileInfo</a> fileinfo( filename );
<a name="x2661"></a>    if ( ! filename.<a href="ntqstring.html#isEmpty">isEmpty</a>() &amp;&amp; fileinfo.<a href="ntqfileinfo.html#exists">exists</a>() &amp;&amp;
<a name="x2665"></a><a name="x2663"></a>         fileinfo.<a href="ntqfileinfo.html#isReadable">isReadable</a>() &amp;&amp; fileinfo.<a href="ntqfileinfo.html#isWritable">isWritable</a>() &amp;&amp;
<a name="x2664"></a><a name="x2662"></a>         fileinfo.<a href="ntqfileinfo.html#isFile">isFile</a>() &amp;&amp; !fileinfo.<a href="ntqfileinfo.html#isSymLink">isSymLink</a>() )
<a name="x2671"></a>        timer-&gt;<a href="ntqtimer.html#start">start</a>( 500, TRUE );
}

void Distributor::browseLibFilename()
{
    <a href="ntqstring.html">TQString</a> filename =
<a name="x2660"></a>        TQFileDialog::<a href="ntqfiledialog.html#getOpenFileName">getOpenFileName</a>( <a href="ntqstring.html#TQString-null">TQString::null</a>, TQString::null, this );
    libFilename-&gt;setText( filename );
}

static char *find_pattern( char *h, const char *n, ulong hlen )
{
    if ( ! h || ! n || hlen == 0 )
        return 0;

#ifdef Q_OS_UNIX
    size_t nlen;
#else
    ulong nlen;
#endif

    char nc = *n++;
    nlen = strlen( n );
    char hc;

    do {
        do {
            hc = *h++;
            if ( hlen-- &lt; 1 )
                return 0;
        } while ( hc != nc );

        if ( nlen &gt; hlen )
            return 0;
    } while ( <a href="ntqcstring.html#qstrncmp">tqstrncmp</a>( h, n, nlen ) != 0 );
    return h + nlen;
}

void Distributor::checkLibData()
{
    struct step {
        const char *key;
        <a href="ntqcstring.html">TQCString</a> value;
        bool done;
    } steps[7];

    steps[0].key = "qt_nstpath=";
    steps[0].done = FALSE;

    steps[1].key = "qt_binpath=";
    steps[1].done = FALSE;

    steps[2].key = "qt_docpath=";
    steps[2].done = FALSE;

    steps[3].key = "qt_hdrpath=";
    steps[3].done = FALSE;

    steps[4].key = "qt_libpath=";
    steps[4].done = FALSE;

    steps[5].key = "qt_plgpath=";
    steps[5].done = FALSE;

    steps[6].key = "qt_datpath=";
    steps[6].done = FALSE;

    uint completed = 0;
    uint total_steps = sizeof(steps) / sizeof(step);

    <a href="ntqfile.html">TQFile</a> file( libFilename-&gt;text() );
<a name="x2659"></a>    if ( file.<a href="ntqfile.html#open">open</a>( <a href="ntqfile.html#open">IO_ReadOnly</a> ) ) {
<a name="x2653"></a>        TQApplication::<a href="ntqapplication.html#setOverrideCursor">setOverrideCursor</a>( WaitCursor );

        // instead of reading in the entire file, do the search in chunks
        char data[60000];
        ulong offset = 0;

<a name="x2655"></a>        while ( ! file.<a href="ntqfile.html#atEnd">atEnd</a>() &amp;&amp; completed &lt; total_steps ) {
<a name="x2651"></a>            TQApplication::<a href="ntqapplication.html#eventLoop">eventLoop</a>()-&gt;processEvents( TQEventLoop::ExcludeUserInput );

<a name="x2667"></a>            ulong len = file.<a href="ntqiodevice.html#readBlock">readBlock</a>( data, sizeof(data) );
            if ( len &lt; 267 ) {
                // not enough room to make any modifications... stop
                break;
            }

            for ( uint x = 0; x &lt; total_steps; ++x ) {
                if ( steps[x].done ) continue;

                char *s = find_pattern( data, steps[x].key, len );
                if ( s ) {
                    ulong where = s - data;
                    if ( len - where &lt; 256 ) {
                        // not enough space left to write the full
                        // path... move the file pointer back to just
                        // before the pattern and continue
                        offset += where - 11;
<a name="x2666"></a>                        file.<a href="ntqiodevice.html#at">at</a>( offset );
                        len = file.<a href="ntqiodevice.html#readBlock">readBlock</a>( data, sizeof(data) );
                        --x; // retry the current step
                        continue;
                    }

                    steps[x].value = s;
                    steps[x].done = TRUE;

                    ++completed;
                }
            }

            // move to the new read position
            offset += len - 11;
            file.<a href="ntqiodevice.html#at">at</a>( offset );
        }

<a name="x2656"></a>        file.<a href="ntqfile.html#close">close</a>();

<a name="x2652"></a>        TQApplication::<a href="ntqapplication.html#restoreOverrideCursor">restoreOverrideCursor</a>();
    }

    if ( completed == total_steps ) {
        setNextEnabled( selectLibrary, TRUE );

<a name="x2657"></a>        <a href="ntqstring.html">TQString</a> prefix = TQFile::<a href="ntqfile.html#decodeName">decodeName</a>( steps[0].value );
        prefixPath-&gt;setText( prefix );

        <a href="ntqstring.html">TQString</a> def_bin = prefix + TQString::fromLatin1( "/bin" );
        <a href="ntqstring.html">TQString</a> def_doc = prefix + TQString::fromLatin1( "/doc" );
        <a href="ntqstring.html">TQString</a> def_hdr = prefix + TQString::fromLatin1( "/include" );
        <a href="ntqstring.html">TQString</a> def_lib = prefix + TQString::fromLatin1( "/lib" );
        <a href="ntqstring.html">TQString</a> def_plg = prefix + TQString::fromLatin1( "/plugins" );
        <a href="ntqstring.html">TQString</a> def_dat = prefix;

        <a href="ntqstring.html">TQString</a> bin = TQFile::<a href="ntqfile.html#decodeName">decodeName</a>( steps[1].value );
        <a href="ntqstring.html">TQString</a> doc = TQFile::<a href="ntqfile.html#decodeName">decodeName</a>( steps[2].value );
        <a href="ntqstring.html">TQString</a> hdr = TQFile::<a href="ntqfile.html#decodeName">decodeName</a>( steps[3].value );
        <a href="ntqstring.html">TQString</a> lib = TQFile::<a href="ntqfile.html#decodeName">decodeName</a>( steps[4].value );
        <a href="ntqstring.html">TQString</a> plg = TQFile::<a href="ntqfile.html#decodeName">decodeName</a>( steps[5].value );
        <a href="ntqstring.html">TQString</a> dat = TQFile::<a href="ntqfile.html#decodeName">decodeName</a>( steps[6].value );

        autoSet-&gt;setChecked( def_bin == bin &amp;&amp;
                             def_doc == doc &amp;&amp;
                             def_hdr == hdr &amp;&amp;
                             def_lib == lib &amp;&amp;
                             def_plg == plg &amp;&amp;
                             def_dat == dat );

        if ( ! autoSet-&gt;isChecked() ) {
            binPath-&gt;setText( bin );
            docPath-&gt;setText( doc );
            hdrPath-&gt;setText( hdr );
            libPath-&gt;setText( lib );
            plgPath-&gt;setText( plg );
            datPath-&gt;setText( dat );
        }
    }
}

void Distributor::checkInstallationPrefix( const <a href="ntqstring.html">TQString</a> &amp;prefix )
{
    if ( autoSet-&gt;isChecked() ) {
        binPath-&gt;setText( prefix + TQString::fromLatin1( "/bin" ) );
        docPath-&gt;setText( prefix + TQString::fromLatin1( "/doc" ) );
        hdrPath-&gt;setText( prefix + TQString::fromLatin1( "/include" ) );
        libPath-&gt;setText( prefix + TQString::fromLatin1( "/lib" ) );
        plgPath-&gt;setText( prefix + TQString::fromLatin1( "/plugins" ) );
        datPath-&gt;setText( prefix );
    }
}

void Distributor::browseInstallationPrefix()
{
    <a href="ntqstring.html">TQString</a> prefix =
        TQFileDialog::<a href="ntqfiledialog.html#getOpenFileName">getOpenFileName</a>( TQString::null, TQString::null, this );
    prefixPath-&gt;setText( prefix );
}


void Distributor::toggleAutoSet( bool autoset )
{
    if ( autoset ) checkInstallationPrefix( prefixPath-&gt;text() );
}

void Distributor::accept()
{
    struct step {
        const char *key;
        <a href="ntqcstring.html">TQCString</a> value;
        bool done;
    } steps[7];

    steps[0].key = "qt_nstpath=";
<a name="x2658"></a>    steps[0].value = TQFile::<a href="ntqfile.html#encodeName">encodeName</a>( prefixPath-&gt;text() );
    steps[0].done = FALSE;

    steps[1].key = "qt_binpath=";
    steps[1].value = TQFile::<a href="ntqfile.html#encodeName">encodeName</a>( binPath-&gt;text() );
    steps[1].done = FALSE;

    steps[2].key = "qt_docpath=";
    steps[2].value = TQFile::<a href="ntqfile.html#encodeName">encodeName</a>( docPath-&gt;text() );
    steps[2].done = FALSE;

    steps[3].key = "qt_hdrpath=";
    steps[3].value = TQFile::<a href="ntqfile.html#encodeName">encodeName</a>( hdrPath-&gt;text() );
    steps[3].done = FALSE;

    steps[4].key = "qt_libpath=";
    steps[4].value = TQFile::<a href="ntqfile.html#encodeName">encodeName</a>( libPath-&gt;text() );
    steps[4].done = FALSE;

    steps[5].key = "qt_plgpath=";
    steps[5].value = TQFile::<a href="ntqfile.html#encodeName">encodeName</a>( plgPath-&gt;text() );
    steps[5].done = FALSE;

    steps[6].key = "qt_datpath=";
    steps[6].value = TQFile::<a href="ntqfile.html#encodeName">encodeName</a>( datPath-&gt;text() );
    steps[6].done = FALSE;

    uint completed = 0;
    uint total_steps = sizeof(steps) / sizeof(step);

    <a href="ntqfile.html">TQFile</a> file( libFilename-&gt;text() );
    if ( file.<a href="ntqfile.html#open">open</a>( <a href="ntqfile.html#open">IO_ReadWrite</a> ) ) {
        TQApplication::<a href="ntqapplication.html#setOverrideCursor">setOverrideCursor</a>( WaitCursor );

        // instead of reading in the entire file, do the search in chunks
        char data[60000];
        ulong offset = 0;

        while ( ! file.<a href="ntqfile.html#atEnd">atEnd</a>() &amp;&amp; completed &lt; total_steps ) {
            TQApplication::<a href="ntqapplication.html#eventLoop">eventLoop</a>()-&gt;processEvents( TQEventLoop::ExcludeUserInput );

            ulong len = file.<a href="ntqiodevice.html#readBlock">readBlock</a>( data, sizeof(data) );
            if ( len &lt; 267 ) {
                // not enough room to make any modifications... stop
                break;
            }

            uint completed_save = completed;
            for ( uint x = 0; x &lt; total_steps; ++x ) {
                if ( steps[x].done ) continue;

                char *s = find_pattern( data, steps[x].key, len );
                if ( s ) {
                    ulong where = s - data;
                    if ( len - where &lt; 256 ) {
                        // not enough space left to write the full
                        // path... move the file pointer back to just
                        // before the pattern and continue
                        offset += where - 11;
                        file.<a href="ntqiodevice.html#at">at</a>( offset );
                        len = file.<a href="ntqiodevice.html#readBlock">readBlock</a>( data, sizeof(data) );
                        --x; // retry the current step
                        continue;
                    }

                    <a href="ntqcstring.html#qstrcpy">qstrcpy</a>( s, steps[x].value );
                    steps[x].done = TRUE;

                    ++completed;
                }
            }

            if ( completed != completed_save ) {
                // something changed...  move file pointer back to
                // where the data was read and write the new data
                file.<a href="ntqiodevice.html#at">at</a>( offset );
<a name="x2668"></a>                file.<a href="ntqiodevice.html#writeBlock">writeBlock</a>( data, len );
            }

            // move to the new read position
            offset += len - 11;
            file.<a href="ntqiodevice.html#at">at</a>( offset );
        }

        file.<a href="ntqfile.html#close">close</a>();

        TQApplication::<a href="ntqapplication.html#restoreOverrideCursor">restoreOverrideCursor</a>();
    }

    if ( completed != total_steps ) {
        TQMessageBox::<a href="ntqmessagebox.html#information">information</a>( this,
                                  tr("TQt Distribution Wizard"),
                                  tr("&lt;p&gt;&lt;h3&gt;Modifications failed.&lt;/h3&gt;&lt;/p&gt;"
                                     "&lt;p&gt;Please make sure that you have permission "
                                     "to write the selected file, and that the library "
                                     "is properly built.&lt;/p&gt;") );
        return;
    }

<a name="x2654"></a>    TQWizard::<a href="ntqdialog.html#accept">accept</a>();
}
</pre>

<p>See also <a href="examples.html">Examples</a>.

<!-- eof -->
<p><address><hr><div align=center>
<table width=100% cellspacing=0 border=0><tr>
<td>Copyright &copy; 2007
<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
<td align=right><div align=right>TQt 3.3.8</div>
</table></div></address></body>
</html>