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 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML
><HEAD
><TITLE
>Application Configuration Information</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.54"/><LINK
REL="HOME"
TITLE="KDE 2.0 Development"
HREF="index.html"/><LINK
REL="UP"
TITLE="Further KDE Compliance"
HREF="ch07.html"/><LINK
REL="PREVIOUS"
TITLE="Further KDE Compliance"
HREF="ch07.html"/><LINK
REL="NEXT"
TITLE="Session Management"
HREF="ch07lev1sec3.html"/><META
HTTP-EQUIV="Content-Style-Type"
CONTENT="text/css"/><LINK
REL="stylesheet"
HREF="kde-common.css"
TYPE="text/css"/><META
HTTP-EQUIV="Content-Type"
CONTENT="text/html; charset=iso-8859-1"/><META
HTTP-EQUIV="Content-Language"
CONTENT="en"/><LINK
REL="stylesheet"
HREF="kde-localised.css"
TYPE="text/css"
TITLE="KDE-English"/><LINK
REL="stylesheet"
HREF="kde-default.css"
TYPE="text/css"
TITLE="KDE-Default"/></HEAD
><BODY
CLASS="section"
LINK="#336699"
VLINK="#336699"
ALINK="#336699"
BGCOLOR="#FFFFFF"
><DIV
ALIGN="RIGHT"
CLASS="NAVBAR"
><P
><A
HREF="ch07.html"
>Prev</A
> <A
HREF="ch07lev1sec3.html"
>Next</A
> <A
HREF="index.html"
>Table of Contents</A
></P
></DIV
><DIV
CLASS="section"
><TABLE
WIDTH="100%"
CELLPADDING="0"
CELLSPACING="0"
BORDER="0"
ALIGN="CENTER"
><TR
><TD
WIDTH="90%"
><H1
CLASS="section"
><A
NAME="ch07lev1sec2"
>7.2. Application Configuration Information</A
></H1
><P
>Configuration information for KDE applications is stored in human-readable text files in a standard format. An example is shown in <A
HREF="ch07lev1sec2.html#ch07list07"
>Listing 7.7</A
>. Applications generally store user settings, such as the name of a mail server or the number of times to try connecting to a site before giving up and user preferences for things such as UI layout and colors. The files are stored as human-readable text so that users and administrators can read and modify these files with any text editor. These files offer advantages over the binary-format configuration files used on some other systems:</P
><UL
COMPACT="COMPACT"
><LI
STYLE="list-style-type: disc"
><P
>You can automate configuration and reconfiguration (from scripts, for example).</P
></LI
><LI
STYLE="list-style-type: disc"
><P
>The files are safer from corruption. Having a few small errors in a file such as the one in <A
HREF="ch07lev1sec2.html#ch07list07"
>Listing 7.7</A
> is unlikely to make it unreadable to a human. The human can correct these errors so that the application can read the file. Small errors in a compact, binary format may destroy information and make the file unusable by the application.</P
></LI
></UL
><P
>KDE configuration files store information as Key, Value pairs as shown in <A
HREF="ch07lev1sec2.html#ch07list07"
>Listing 7.7</A
> One pair appears per line.</P
><DIV
CLASS="example"
><HR/><A
NAME="ch07list07"
></A
><P
><B
>Example 7.7. A Sample KDE Application Configuration File</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="programlisting"
> 1
2 1: #KDE Config File
3 2: DefaultHeight=300
4 3: [Options]
5 4: BackgroundColor=255,255,255
6 5: ForegroundColor=100,100,255
7 </PRE
></TD
></TR
></TABLE
><HR/></DIV
><P
>The pairs can be grouped to help organize the information for the application or to make the file more readable. Group names appear in brackets alone on a line preceding the group they label (line 3). </P
><P
>The files can also include comments. Comments appear on lines starting with a <TT
CLASS="literal"
># (line 1)</TT
>.</P
><P
>KDE configuration files are stored in .kde/share/config in the user's home directory. Each application creates a configuration file called <I
CLASS="emphasis"
>kappnamerc</I
>, where <I
CLASS="emphasis"
>kappname</I
> is the name of the application. For example, knotes creates knotesrc.</P
><DIV
CLASS="section"
><TABLE
WIDTH="100%"
CELLPADDING="0"
CELLSPACING="0"
BORDER="0"
ALIGN="CENTER"
><TR
><TD
WIDTH="90%"
><H2
CLASS="section"
><A
NAME="ch07lev2sec3"
>7.2.1. Accessing Configuration Files</A
></H2
><P
>Configuration files are read and written with the <TT
CLASS="literal"
>KConfig</TT
> class. The default configuration file, <I
CLASS="emphasis"
>kappnamerc</I
>, can be accessed with the <TT
CLASS="literal"
>KConfig</TT
> object returned by the method <TT
CLASS="literal"
>KApplication::config()</TT
>. You typically access this object with <TT
CLASS="literal"
>kapp->config()</TT
>. (<TT
CLASS="literal"
>kapp</TT
> is a macro defined in kapp.h that returns a pointer to the current <TT
CLASS="literal"
>KApplication</TT
> object. A KDE application uses only one <TT
CLASS="literal"
>KApplication</TT
> object; therefore, <TT
CLASS="literal"
>kapp</TT
> can reliably be used from any source-code file that is part of your application.)</P
><P
><A
HREF="ch07lev1sec2.html#ch07list08"
>Listings 7.8</A
> and <A
HREF="ch07lev1sec2.html#ch07list09"
>7.9</A
> show the code for <TT
CLASS="literal"
>KConfigDemo</TT
>, a widget that demonstrates the use of <TT
CLASS="literal"
>KConfig</TT
>.</P
><DIV
CLASS="example"
><HR/><A
NAME="ch07list08"
></A
><P
><B
>Example 7.8. kconfigdemo.h: Class Declaration for <TT
CLASS="literal"
>KConfigDemo</TT
>, a widget that demonstrates <TT
CLASS="literal"
>KConfig</TT
></B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="programlisting"
> 1
2 1: #ifndef __KCONFIGDEMO_H__
3 2: #define __KCONFIGDEMO_H__
4 3:
5 4: #include <qlineedit.h>
6 5:
7 6: /**
8 7: * KConfigDemo
9 8: * Show how to access KDE configuration files with KConfig.
10 9: **/
11 10: class KConfigDemo : public QLineEdit
12 11: {
13 12: public:
14 13: KConfigDemo ();
15 14:
16 15: protected:
17 16: void closeEvent (QCloseEvent *qcloseevent);
18 17:
19 18: };
20 19:
21 20: #endif
22 </PRE
></TD
></TR
></TABLE
><HR/></DIV
><P
><TT
CLASS="literal"
>KConfigDemo</TT
> is a line editor widget that saves its text in a configuration file when the user closes the window, and it reloads it the next time the program is run.</P
><P
><TT
CLASS="literal"
>KConfigDemo</TT
> is subclassed from <TT
CLASS="literal"
>QLineEdit</TT
>, which does most of the work for you. You just need to add the configuration file access.</P
><DIV
CLASS="example"
><HR/><A
NAME="ch07list09"
></A
><P
><B
>Example 7.9. kconfigdemo.cpp: Class Definition for <TT
CLASS="literal"
>KConfigDemo</TT
></B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="programlisting"
> 1
2 1: #include <kapp.h>
3 2: #include <kconfig.h>
4 3:
5 4: #include "kconfigdemo.h"
6 5:
7 6: KConfigDemo::KConfigDemo () : QLineEdit (0)
8 7: {
9 8: kapp->config()->setGroup ("LineEditor");
10 9: setText (kapp->config()->readEntry ("Text", "Hello"));
11 10: }
12 11:
13 12: void
14 13: KConfigDemo::closeEvent (QCloseEvent *qcloseevent)
15 14: {
16 15: kapp->config()->setGroup ("LineEditor");
17 16: kapp->config()->writeEntry ("Text", text());
18 17: kapp->config()->sync();
19 18:
20 19: qcloseevent->accept();
21 20: }</PRE
></TD
></TR
></TABLE
><HR/></DIV
><P
>In the constructor, you look in the group <TT
CLASS="literal"
>LineEditor</TT
> (line 8) for the key <TT
CLASS="literal"
>Text</TT
> (line 9). (Note: If you had not specified a group with <TT
CLASS="literal"
>setGroup()</TT
>, the Key, Value pair would have been written to the default, unnamed group.) <TT
CLASS="literal"
>readEntry()</TT
> returns the value found to the right of <TT
CLASS="literal"
>Text=</TT
> in the configuration file as a <TT
CLASS="literal"
>QString</TT
>. In the event that the key <TT
CLASS="literal"
>Text</TT
> does not appear at all—as is the case when the program is run for the first time—<TT
CLASS="literal"
>readEntry()</TT
> returns <TT
CLASS="literal"
>Hello</TT
>, the value specified as the default (line 9).</P
><P
>You want to save the text when the user closes the window. To do this, reimplement the virtual method <TT
CLASS="literal"
>closeEvent()</TT
>. This method is called when a close request is made, but before the window is actually closed. In this method, place the text of the widget (returned by <TT
CLASS="literal"
>text()</TT
>) to the configuration file into the group <TT
CLASS="literal"
>LineEditor</TT
>. The call on line 17 is very important. This call actually writes the new configuration information to disk.</P
><DIV
CLASS="note"
><TABLE
CLASS="note"
WIDTH="100%"
BORDER="0"
><TR
><TD
WIDTH="25"
ALIGN="CENTER"
VALIGN="TOP"
><IMG
SRC="note.png"
HSPACE="5"
ALT="Note"/></TD
><TD
ALIGN="LEFT"
VALIGN="TOP"
><P
><TT
CLASS="literal"
>KConfig</TT
> caches the information in memory until the method <TT
CLASS="literal"
>KConfig::sync()</TT
> is called, so be sure to call it before your application exits or else your settings will be lost.</P
></TD
></TR
></TABLE
></DIV
><P
>Finally, call <TT
CLASS="literal"
>qcloseevent->accept()</TT
>, which lets Qt know that the widget is willing to grant the user's request and be closed.</P
><P
><A
HREF="ch07lev1sec2.html#ch07list10"
>Listing 7.10</A
> is for a <TT
CLASS="literal"
>main()</TT
> function that you can use to test <TT
CLASS="literal"
>KConfigDemo</TT
>. <A
HREF="ch07lev1sec2.html#ch07fig03"
>Figure 7.3</A
> shows the program that is created—kconfigdemotest—running.</P
><DIV
CLASS="example"
><HR/><A
NAME="ch07list10"
></A
><P
><B
>Example 7.10. main.cpp: A <TT
CLASS="literal"
>main()</TT
> Function Suitable for Testing <TT
CLASS="literal"
>KConfigDemo</TT
></B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><PRE
CLASS="programlisting"
> 1
2 1: #include <kapp.h>
3 2:
4 3: #include "kconfigdemo.h"
5 4:
6 5: int
7 6: main (int argc, char *argv[])
8 7: {
9 8: KApplication kapplication (argc, argv, "kconfigdemotest");
10 9:
11 10: KConfigDemo *kconfigdemo = new KConfigDemo;
12 11:
13 12: kapplication.setMainWidget (kconfigdemo);
14 13:
15 14: kconfigdemo->show();
16 15: return kapplication.exec();
17 16: } </PRE
></TD
></TR
></TABLE
><HR/></DIV
><P
><TT
CLASS="literal"
>KConfig</TT
> can read and write many types of data, not just strings. For example, in the next section you read and write a <TT
CLASS="literal"
>QFont</TT
>. The supported types are listed in the documentation (and header file) for <TT
CLASS="literal"
>KConfigBase</TT
>.</P
><DIV
CLASS="figure"
><HR/><A
NAME="ch07fig03"
></A
><P
><B
>Figure 7.3. KConfigDemo saves the text you enter in its configuration file.</B
></P
><DIV
CLASS="mediaobject"
><P
><IMG
SRC="graphics/07fig03.gif"
></IMG
></P
></DIV
><HR/></DIV
></TD
><TD
WIDTH="10%"
VALIGN="BOTTOM"
ALIGN="CENTER"
><ANNMARK
NAME="ch07lev2sec3"/></TD
></TR
><ANNOTATION
NAME="ch07lev2sec3"
TITLE="Accessing Configuration Files"/></TABLE
></DIV
></TD
><TD
WIDTH="10%"
VALIGN="BOTTOM"
ALIGN="CENTER"
><ANNMARK
NAME="ch07lev1sec2"/></TD
></TR
><ANNOTATION
NAME="ch07lev1sec2"
TITLE="Application Configuration Information"/></TABLE
></DIV
><DIV
ALIGN="RIGHT"
CLASS="NAVBAR"
><P
><A
HREF="ch07.html"
>Prev</A
> <A
HREF="ch07lev1sec3.html"
>Next</A
> <A
HREF="index.html"
>Table of Contents</A
></P
></DIV
><HR
WIDTH="100%"
SIZE="2"
ALIGN="CENTER"
NOSHADE="NOSHADE"/></BODY
></HTML
>
|