source: trunk/doc/html/distributor-example.html@ 208

Last change on this file since 208 was 190, checked in by rudi, 14 years ago

reference documentation added

File size: 19.0 KB
Line 
1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2<!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/examples/distributor/distributor.doc:1 -->
3<html>
4<head>
5<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
6<title>Qt Distribution Example</title>
7<style type="text/css"><!--
8fn { margin-left: 1cm; text-indent: -1cm; }
9a:link { color: #004faf; text-decoration: none }
10a:visited { color: #672967; text-decoration: none }
11body { background: #ffffff; color: black; }
12--></style>
13</head>
14<body>
15
16<table border="0" cellpadding="0" cellspacing="0" width="100%">
17<tr bgcolor="#E5E5E5">
18<td valign=center>
19 <a href="index.html">
20<font color="#004faf">Home</font></a>
21 | <a href="classes.html">
22<font color="#004faf">All&nbsp;Classes</font></a>
23 | <a href="mainclasses.html">
24<font color="#004faf">Main&nbsp;Classes</font></a>
25 | <a href="annotated.html">
26<font color="#004faf">Annotated</font></a>
27 | <a href="groups.html">
28<font color="#004faf">Grouped&nbsp;Classes</font></a>
29 | <a href="functions.html">
30<font color="#004faf">Functions</font></a>
31</td>
32<td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>Qt Distribution Example</h1>
33
34
35
36<p> This example program modifies the hard-coded paths that are
37compiled into the Qt library.
38<p> When distributing the Qt library, the final installation prefix is
39very rarely the same as the prefix used when doing development.
40You can use the code from this example to modify the following
41hard-coded paths in Qt library:
42<p> <ul>
43<p> <li> Prefix - Normally, all other paths are relative to the <em>Prefix</em>.
44<p> <li> Binaries - Location of binaries distributed with Qt (for
45example, <em>Qt Assistant</em>).
46<p> <li> Documentation - Location of the Qt documentation.
47<p> <li> Headers - Location of the Qt headers.
48<p> <li> Libraries - Location of addition libraries distributed with Qt
49(for example, the <em>qui</em> library).
50<p> <li> Plugins - Location of the Qt plugins.
51<p> <li> Data - Location of applicaton specific data for all programs
52distributed with Qt.
53<p> </ul>
54<p> <hr>
55<p> Implementation:
56<p> <pre>/****************************************************************************
57** ui.h extension file, included from the uic-generated form implementation.
58**
59** If you wish to add, delete or rename functions or slots use
60** Qt Designer which will update this file, preserving your code. Create an
61** init() function in place of a constructor, and a destroy() function in
62** place of a destructor.
63*****************************************************************************/
64
65#include &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;
66#include &lt;<a href="qcursor-h.html">qcursor.h</a>&gt;
67#include &lt;<a href="qeventloop-h.html">qeventloop.h</a>&gt;
68#include &lt;<a href="qfile-h.html">qfile.h</a>&gt;
69#include &lt;<a href="qfiledialog-h.html">qfiledialog.h</a>&gt;
70#include &lt;<a href="qfileinfo-h.html">qfileinfo.h</a>&gt;
71#include &lt;<a href="qlineedit-h.html">qlineedit.h</a>&gt;
72#include &lt;<a href="qmessagebox-h.html">qmessagebox.h</a>&gt;
73#include &lt;<a href="qpushbutton-h.html">qpushbutton.h</a>&gt;
74#include &lt;<a href="qtimer-h.html">qtimer.h</a>&gt;
75
76
77void Distributor::init()
78{
79 timer = new <a href="qtimer.html">QTimer</a>( this );
80<a name="x2672"></a> connect( timer, SIGNAL(<a href="qtimer.html#timeout">timeout</a>()), SLOT(checkLibData()) );
81
82 cancelButton()-&gt;setAutoDefault( FALSE );
83 backButton()-&gt;setAutoDefault( FALSE );
84
85 setNextEnabled( selectLibrary, FALSE );
86
87 setHelpEnabled( selectLibrary, FALSE );
88 setHelpEnabled( modifyPaths, FALSE );
89 setHelpEnabled( verifyMods, FALSE );
90
91 setFinishEnabled( verifyMods, TRUE );
92}
93
94void Distributor::showPage( <a href="qwidget.html">QWidget</a> *page )
95{
96 if ( page == selectLibrary ) {
97 nextButton()-&gt;setDefault( TRUE );
98 libFilename-&gt;setFocus();
99 } else if ( page == modifyPaths ) {
100 nextButton()-&gt;setDefault( TRUE );
101 prefixPath-&gt;selectAll();
102 prefixPath-&gt;setFocus();
103 } else if ( page == verifyMods ) {
104 finishButton()-&gt;setDefault( TRUE );
105 finishButton()-&gt;setFocus();
106
107 <a href="qstring.html">QString</a> labeltext =
108 tr("&lt;p&gt;&lt;b&gt;Current Library File:&lt;/b&gt; %1&lt;/p&gt;"
109 "&lt;table border=0&gt;"
110 "&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;"
111 "&lt;tr&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;"
112 "&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;"
113 "&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;"
114 "&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;"
115 "&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;"
116 "&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;"
117 "&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;"
118 "&lt;/table&gt;"
119 "&lt;p&gt;Please verify that these options are correct. Press the "
120 "&lt;i&gt;Finish&lt;/i&gt; button to apply these modifications to the Qt "
121 "library. Use the &lt;i&gt;Back&lt;/i&gt; button to make corrections. Use "
122 "the &lt;i&gt;Cancel&lt;/i&gt; button to abort.&lt;/p&gt;")
123 .arg( libFilename-&gt;text() )
124 .arg( prefixPath-&gt;text() )
125 .arg( binPath-&gt;text() )
126 .arg( docPath-&gt;text() )
127 .arg( hdrPath-&gt;text() )
128 .arg( libPath-&gt;text() )
129 .arg( plgPath-&gt;text() )
130 .arg( datPath-&gt;text() );
131 textLabel4-&gt;setText( labeltext );
132 }
133
134<a name="x2673"></a> QWizard::<a href="qwizard.html#showPage">showPage</a>( page );
135}
136
137void Distributor::checkLibFilename( const <a href="qstring.html">QString</a> &amp;filename )
138{
139 setNextEnabled( selectLibrary, FALSE );
140
141 <a href="qfileinfo.html">QFileInfo</a> fileinfo( filename );
142<a name="x2661"></a> if ( ! filename.<a href="qstring.html#isEmpty">isEmpty</a>() &amp;&amp; fileinfo.<a href="qfileinfo.html#exists">exists</a>() &amp;&amp;
143<a name="x2665"></a><a name="x2663"></a> fileinfo.<a href="qfileinfo.html#isReadable">isReadable</a>() &amp;&amp; fileinfo.<a href="qfileinfo.html#isWritable">isWritable</a>() &amp;&amp;
144<a name="x2664"></a><a name="x2662"></a> fileinfo.<a href="qfileinfo.html#isFile">isFile</a>() &amp;&amp; !fileinfo.<a href="qfileinfo.html#isSymLink">isSymLink</a>() )
145<a name="x2671"></a> timer-&gt;<a href="qtimer.html#start">start</a>( 500, TRUE );
146}
147
148void Distributor::browseLibFilename()
149{
150 <a href="qstring.html">QString</a> filename =
151<a name="x2660"></a> QFileDialog::<a href="qfiledialog.html#getOpenFileName">getOpenFileName</a>( <a href="qstring.html#QString-null">QString::null</a>, QString::null, this );
152 libFilename-&gt;setText( filename );
153}
154
155static char *find_pattern( char *h, const char *n, ulong hlen )
156{
157 if ( ! h || ! n || hlen == 0 )
158 return 0;
159
160#ifdef Q_OS_UNIX
161 size_t nlen;
162#else
163 ulong nlen;
164#endif
165
166 char nc = *n++;
167 nlen = strlen( n );
168 char hc;
169
170 do {
171 do {
172 hc = *h++;
173 if ( hlen-- &lt; 1 )
174 return 0;
175 } while ( hc != nc );
176
177 if ( nlen &gt; hlen )
178 return 0;
179 } while ( <a href="qcstring.html#qstrncmp">qstrncmp</a>( h, n, nlen ) != 0 );
180 return h + nlen;
181}
182
183void Distributor::checkLibData()
184{
185 struct step {
186 const char *key;
187 <a href="qcstring.html">QCString</a> value;
188 bool done;
189 } steps[7];
190
191 steps[0].key = "qt_nstpath=";
192 steps[0].done = FALSE;
193
194 steps[1].key = "qt_binpath=";
195 steps[1].done = FALSE;
196
197 steps[2].key = "qt_docpath=";
198 steps[2].done = FALSE;
199
200 steps[3].key = "qt_hdrpath=";
201 steps[3].done = FALSE;
202
203 steps[4].key = "qt_libpath=";
204 steps[4].done = FALSE;
205
206 steps[5].key = "qt_plgpath=";
207 steps[5].done = FALSE;
208
209 steps[6].key = "qt_datpath=";
210 steps[6].done = FALSE;
211
212 uint completed = 0;
213 uint total_steps = sizeof(steps) / sizeof(step);
214
215 <a href="qfile.html">QFile</a> file( libFilename-&gt;text() );
216<a name="x2659"></a> if ( file.<a href="qfile.html#open">open</a>( <a href="qfile.html#open">IO_ReadOnly</a> ) ) {
217<a name="x2653"></a> QApplication::<a href="qapplication.html#setOverrideCursor">setOverrideCursor</a>( WaitCursor );
218
219 // instead of reading in the entire file, do the search in chunks
220 char data[60000];
221 ulong offset = 0;
222
223<a name="x2655"></a> while ( ! file.<a href="qfile.html#atEnd">atEnd</a>() &amp;&amp; completed &lt; total_steps ) {
224<a name="x2651"></a> QApplication::<a href="qapplication.html#eventLoop">eventLoop</a>()-&gt;processEvents( QEventLoop::ExcludeUserInput );
225
226<a name="x2667"></a> ulong len = file.<a href="qiodevice.html#readBlock">readBlock</a>( data, sizeof(data) );
227 if ( len &lt; 267 ) {
228 // not enough room to make any modifications... stop
229 break;
230 }
231
232 for ( uint x = 0; x &lt; total_steps; ++x ) {
233 if ( steps[x].done ) continue;
234
235 char *s = find_pattern( data, steps[x].key, len );
236 if ( s ) {
237 ulong where = s - data;
238 if ( len - where &lt; 256 ) {
239 // not enough space left to write the full
240 // path... move the file pointer back to just
241 // before the pattern and continue
242 offset += where - 11;
243<a name="x2666"></a> file.<a href="qiodevice.html#at">at</a>( offset );
244 len = file.<a href="qiodevice.html#readBlock">readBlock</a>( data, sizeof(data) );
245 --x; // retry the current step
246 continue;
247 }
248
249 steps[x].value = s;
250 steps[x].done = TRUE;
251
252 ++completed;
253 }
254 }
255
256 // move to the new read position
257 offset += len - 11;
258 file.<a href="qiodevice.html#at">at</a>( offset );
259 }
260
261<a name="x2656"></a> file.<a href="qfile.html#close">close</a>();
262
263<a name="x2652"></a> QApplication::<a href="qapplication.html#restoreOverrideCursor">restoreOverrideCursor</a>();
264 }
265
266 if ( completed == total_steps ) {
267 setNextEnabled( selectLibrary, TRUE );
268
269<a name="x2657"></a> <a href="qstring.html">QString</a> prefix = QFile::<a href="qfile.html#decodeName">decodeName</a>( steps[0].value );
270 prefixPath-&gt;setText( prefix );
271
272 <a href="qstring.html">QString</a> def_bin = prefix + QString::fromLatin1( "/bin" );
273 <a href="qstring.html">QString</a> def_doc = prefix + QString::fromLatin1( "/doc" );
274 <a href="qstring.html">QString</a> def_hdr = prefix + QString::fromLatin1( "/include" );
275 <a href="qstring.html">QString</a> def_lib = prefix + QString::fromLatin1( "/lib" );
276 <a href="qstring.html">QString</a> def_plg = prefix + QString::fromLatin1( "/plugins" );
277 <a href="qstring.html">QString</a> def_dat = prefix;
278
279 <a href="qstring.html">QString</a> bin = QFile::<a href="qfile.html#decodeName">decodeName</a>( steps[1].value );
280 <a href="qstring.html">QString</a> doc = QFile::<a href="qfile.html#decodeName">decodeName</a>( steps[2].value );
281 <a href="qstring.html">QString</a> hdr = QFile::<a href="qfile.html#decodeName">decodeName</a>( steps[3].value );
282 <a href="qstring.html">QString</a> lib = QFile::<a href="qfile.html#decodeName">decodeName</a>( steps[4].value );
283 <a href="qstring.html">QString</a> plg = QFile::<a href="qfile.html#decodeName">decodeName</a>( steps[5].value );
284 <a href="qstring.html">QString</a> dat = QFile::<a href="qfile.html#decodeName">decodeName</a>( steps[6].value );
285
286 autoSet-&gt;setChecked( def_bin == bin &amp;&amp;
287 def_doc == doc &amp;&amp;
288 def_hdr == hdr &amp;&amp;
289 def_lib == lib &amp;&amp;
290 def_plg == plg &amp;&amp;
291 def_dat == dat );
292
293 if ( ! autoSet-&gt;isChecked() ) {
294 binPath-&gt;setText( bin );
295 docPath-&gt;setText( doc );
296 hdrPath-&gt;setText( hdr );
297 libPath-&gt;setText( lib );
298 plgPath-&gt;setText( plg );
299 datPath-&gt;setText( dat );
300 }
301 }
302}
303
304void Distributor::checkInstallationPrefix( const <a href="qstring.html">QString</a> &amp;prefix )
305{
306 if ( autoSet-&gt;isChecked() ) {
307 binPath-&gt;setText( prefix + QString::fromLatin1( "/bin" ) );
308 docPath-&gt;setText( prefix + QString::fromLatin1( "/doc" ) );
309 hdrPath-&gt;setText( prefix + QString::fromLatin1( "/include" ) );
310 libPath-&gt;setText( prefix + QString::fromLatin1( "/lib" ) );
311 plgPath-&gt;setText( prefix + QString::fromLatin1( "/plugins" ) );
312 datPath-&gt;setText( prefix );
313 }
314}
315
316void Distributor::browseInstallationPrefix()
317{
318 <a href="qstring.html">QString</a> prefix =
319 QFileDialog::<a href="qfiledialog.html#getOpenFileName">getOpenFileName</a>( QString::null, QString::null, this );
320 prefixPath-&gt;setText( prefix );
321}
322
323
324void Distributor::toggleAutoSet( bool autoset )
325{
326 if ( autoset ) checkInstallationPrefix( prefixPath-&gt;text() );
327}
328
329void Distributor::accept()
330{
331 struct step {
332 const char *key;
333 <a href="qcstring.html">QCString</a> value;
334 bool done;
335 } steps[7];
336
337 steps[0].key = "qt_nstpath=";
338<a name="x2658"></a> steps[0].value = QFile::<a href="qfile.html#encodeName">encodeName</a>( prefixPath-&gt;text() );
339 steps[0].done = FALSE;
340
341 steps[1].key = "qt_binpath=";
342 steps[1].value = QFile::<a href="qfile.html#encodeName">encodeName</a>( binPath-&gt;text() );
343 steps[1].done = FALSE;
344
345 steps[2].key = "qt_docpath=";
346 steps[2].value = QFile::<a href="qfile.html#encodeName">encodeName</a>( docPath-&gt;text() );
347 steps[2].done = FALSE;
348
349 steps[3].key = "qt_hdrpath=";
350 steps[3].value = QFile::<a href="qfile.html#encodeName">encodeName</a>( hdrPath-&gt;text() );
351 steps[3].done = FALSE;
352
353 steps[4].key = "qt_libpath=";
354 steps[4].value = QFile::<a href="qfile.html#encodeName">encodeName</a>( libPath-&gt;text() );
355 steps[4].done = FALSE;
356
357 steps[5].key = "qt_plgpath=";
358 steps[5].value = QFile::<a href="qfile.html#encodeName">encodeName</a>( plgPath-&gt;text() );
359 steps[5].done = FALSE;
360
361 steps[6].key = "qt_datpath=";
362 steps[6].value = QFile::<a href="qfile.html#encodeName">encodeName</a>( datPath-&gt;text() );
363 steps[6].done = FALSE;
364
365 uint completed = 0;
366 uint total_steps = sizeof(steps) / sizeof(step);
367
368 <a href="qfile.html">QFile</a> file( libFilename-&gt;text() );
369 if ( file.<a href="qfile.html#open">open</a>( <a href="qfile.html#open">IO_ReadWrite</a> ) ) {
370 QApplication::<a href="qapplication.html#setOverrideCursor">setOverrideCursor</a>( WaitCursor );
371
372 // instead of reading in the entire file, do the search in chunks
373 char data[60000];
374 ulong offset = 0;
375
376 while ( ! file.<a href="qfile.html#atEnd">atEnd</a>() &amp;&amp; completed &lt; total_steps ) {
377 QApplication::<a href="qapplication.html#eventLoop">eventLoop</a>()-&gt;processEvents( QEventLoop::ExcludeUserInput );
378
379 ulong len = file.<a href="qiodevice.html#readBlock">readBlock</a>( data, sizeof(data) );
380 if ( len &lt; 267 ) {
381 // not enough room to make any modifications... stop
382 break;
383 }
384
385 uint completed_save = completed;
386 for ( uint x = 0; x &lt; total_steps; ++x ) {
387 if ( steps[x].done ) continue;
388
389 char *s = find_pattern( data, steps[x].key, len );
390 if ( s ) {
391 ulong where = s - data;
392 if ( len - where &lt; 256 ) {
393 // not enough space left to write the full
394 // path... move the file pointer back to just
395 // before the pattern and continue
396 offset += where - 11;
397 file.<a href="qiodevice.html#at">at</a>( offset );
398 len = file.<a href="qiodevice.html#readBlock">readBlock</a>( data, sizeof(data) );
399 --x; // retry the current step
400 continue;
401 }
402
403 <a href="qcstring.html#qstrcpy">qstrcpy</a>( s, steps[x].value );
404 steps[x].done = TRUE;
405
406 ++completed;
407 }
408 }
409
410 if ( completed != completed_save ) {
411 // something changed... move file pointer back to
412 // where the data was read and write the new data
413 file.<a href="qiodevice.html#at">at</a>( offset );
414<a name="x2668"></a> file.<a href="qiodevice.html#writeBlock">writeBlock</a>( data, len );
415 }
416
417 // move to the new read position
418 offset += len - 11;
419 file.<a href="qiodevice.html#at">at</a>( offset );
420 }
421
422 file.<a href="qfile.html#close">close</a>();
423
424 QApplication::<a href="qapplication.html#restoreOverrideCursor">restoreOverrideCursor</a>();
425 }
426
427 if ( completed != total_steps ) {
428 QMessageBox::<a href="qmessagebox.html#information">information</a>( this,
429 tr("Qt Distribution Wizard"),
430 tr("&lt;p&gt;&lt;h3&gt;Modifications failed.&lt;/h3&gt;&lt;/p&gt;"
431 "&lt;p&gt;Please make sure that you have permission "
432 "to write the selected file, and that the library "
433 "is properly built.&lt;/p&gt;") );
434 return;
435 }
436
437<a name="x2654"></a> QWizard::<a href="qdialog.html#accept">accept</a>();
438}
439</pre>
440
441<p>See also <a href="examples.html">Examples</a>.
442
443<!-- eof -->
444<p><address><hr><div align=center>
445<table width=100% cellspacing=0 border=0><tr>
446<td>Copyright &copy; 2007
447<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
448<td align=right><div align=right>Qt 3.3.8</div>
449</table></div></address></body>
450</html>
Note: See TracBrowser for help on using the repository browser.