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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
|
/****************************************************************************
**
** Implementation of TQSettings class
**
** Created : 000626
**
** Copyright (C) 2000-2008 Trolltech ASA. All rights reserved.
**
** This file is part of the tools module of the TQt GUI Toolkit.
**
** This file may be used under the terms of the GNU General
** Public License versions 2.0 or 3.0 as published by the Free
** Software Foundation and appearing in the files LICENSE.GPL2
** and LICENSE.GPL3 included in the packaging of this file.
** Alternatively you may (at your option) use any later version
** of the GNU General Public License if such license has been
** publicly approved by Trolltech ASA (or its successors, if any)
** and the KDE Free TQt Foundation.
**
** Please review the following information to ensure GNU General
** Public Licensing requirements will be met:
** http://trolltech.com/products/qt/licenses/licensing/opensource/.
** If you are unsure which license is appropriate for your use, please
** review the following information:
** http://trolltech.com/products/qt/licenses/licensing/licensingoverview
** or contact the sales department at [email protected].
**
** This file may be used under the terms of the Q Public License as
** defined by Trolltech ASA and appearing in the file LICENSE.TQPL
** included in the packaging of this file. Licensees holding valid TQt
** Commercial licenses may use this file in accordance with the TQt
** Commercial License Agreement provided with the Software.
**
** This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted
** herein.
**
**********************************************************************/
#include "qplatformdefs.h"
// POSIX Large File Support redefines open -> open64
static inline int qt_open( const char *pathname, int flags, mode_t mode )
{ return ::open( pathname, flags, mode ); }
#if defined(open)
# undef open
#endif
// POSIX Large File Support redefines truncate -> truncate64
#if defined(truncate)
# undef truncate
#endif
#include "ntqsettings.h"
#ifndef QT_NO_SETTINGS
#include "ntqdir.h"
#include "ntqfile.h"
#include "ntqfileinfo.h"
#include "ntqmap.h"
#include "ntqtextstream.h"
#include "ntqregexp.h"
#include <private/qsettings_p.h>
#ifndef NO_ERRNO_H
#include <errno.h>
#endif
/*!
\class TQSettings
\brief The TQSettings class provides persistent platform-independent application settings.
\ingroup io
\ingroup misc
\mainclass
On Unix systems, TQSettings uses text files to store settings. On Windows
systems, TQSettings uses the system registry. On Mac OS X, TQSettings uses
the Carbon preferences API.
Each setting comprises an identifying key and the data associated with
the key. A key is a unicode string which consists of \e two or more
subkeys. A subkey is a slash, '/', followed by one or more unicode
characters (excluding slashes, newlines, carriage returns and equals,
'=', signs). The associated data, called the entry or value, may be a
boolean, an integer, a double, a string or a list of strings. Entry
strings may contain any unicode characters.
If you want to save and restore the entire desktop's settings, i.e.
which applications are running, use TQSettings to save the settings
for each individual application and TQSessionManager to save the
desktop's session.
Example settings:
\code
/MyCompany/MyApplication/background color
/MyCompany/MyApplication/foreground color
/MyCompany/MyApplication/geometry/x
/MyCompany/MyApplication/geometry/y
/MyCompany/MyApplication/geometry/width
/MyCompany/MyApplication/geometry/height
/MyCompany/MyApplication/recent files/1
/MyCompany/MyApplication/recent files/2
/MyCompany/MyApplication/recent files/3
\endcode
Each line above is a complete key, made up of subkeys.
A typical usage pattern for reading settings at application
startup:
\code
TQSettings settings;
settings.setPath( "MyCompany.com", "MyApplication" );
TQString bgColor = settings.readEntry( "/colors/background", "white" );
int width = settings.readNumEntry( "/geometry/width", 640 );
// ...
\endcode
A typical usage pattern for saving settings at application exit or
'save preferences':
\code
TQSettings settings;
settings.setPath( "MyCompany.com", "MyApplication" );
settings.writeEntry( "/colors/background", bgColor );
settings.writeEntry( "/geometry/width", width );
// ...
\endcode
A key prefix can be prepended to all keys using beginGroup(). The
application of the prefix is stopped using endGroup(). For
example:
\code
TQSettings settings;
settings.beginGroup( "/MainWindow" );
settings.beginGroup( "/Geometry" );
int x = settings.readEntry( "/x" );
// ...
settings.endGroup();
settings.beginGroup( "/Toolbars" );
// ...
settings.endGroup();
settings.endGroup();
\endcode
You can get a list of entry-holding keys by calling entryList(), and
a list of key-holding keys using subkeyList().
\code
TQStringList keys = settings.entryList( "/MyApplication" );
// keys contains 'background color' and 'foreground color'.
TQStringList keys = settings.entryList( "/MyApplication/recent files" );
// keys contains '1', '2' and '3'.
TQStringList subkeys = settings.subkeyList( "/MyApplication" );
// subkeys contains 'geometry' and 'recent files'
TQStringList subkeys = settings.subkeyList( "/MyApplication/recent files" );
// subkeys is empty.
\endcode
Since settings for Windows are stored in the registry there are
some size limitations as follows:
\list
\i A subkey may not exceed 255 characters.
\i An entry's value may not exceed 16,300 characters.
\i All the values of a key (for example, all the 'recent files'
subkeys values), may not exceed 65,535 characters.
\endlist
These limitations are not enforced on Unix or Mac OS X.
\warning Creating multiple, simultaneous instances of TQSettings writing
to a text file may lead to data loss! This is a known issue which will
be fixed in a future release of TQt.
\section1 Notes for Mac OS X Applications
The location where settings are stored is not formally defined by
the CFPreferences API.
At the time of writing settings are stored (either on a global or
user basis, preferring locally) into a plist file in \c
$ROOT/System/Library/Preferences (in XML format). TQSettings will
create an appropriate plist file (\c{com.<first group name>.plist})
out of the full path to a key.
For further information on CFPreferences see
\link http://developer.apple.com/documentation/CoreFoundation/Conceptual/CFPreferences/index.html
Apple's Specifications\endlink
\section1 Notes for Unix Applications
There is no universally accepted place for storing application
settings under Unix. In the examples the settings file will be
searched for in the following directories:
\list 1
\i \c SYSCONF - the default value is \c INSTALL/etc/settings
\i \c /opt/MyCompany/share/etc
\i \c /opt/MyCompany/share/MyApplication/etc
\i \c $HOME/.qt
\endlist
When reading settings the files are searched in the order shown
above, with later settings overriding earlier settings. Files for
which the user doesn't have read permission are ignored. When saving
settings TQSettings works in the order shown above, writing
to the first settings file for which the user has write permission.
(\c INSTALL is the directory where TQt was installed. This can be
modified by using the configure script's -prefix argument )
If you want to put the settings in a particular place in the
filesystem you could do this:
\code
settings.insertSearchPath( TQSettings::Unix, "/opt/MyCompany/share" );
\endcode
But in practice you may prefer not to use a search path for Unix.
For example the following code:
\code
settings.writeEntry( "/MyApplication/geometry/width", width );
\endcode
will end up writing the "geometry/width" setting to the file
\c{$HOME/.qt/myapplicationrc} (assuming that the application is
being run by an ordinary user, i.e. not by root).
For cross-platform applications you should ensure that the
\link #sizelimit Windows size limitations \endlink are not exceeded.
\warning TQSettings doesn't write the settings until it is destroyed so
you should construct the TQSettings object on the stack.
*/
/*!
\enum TQSettings::System
\value Mac Macintosh execution environments
\value Unix Mac OS X, Unix, Linux and Unix-like execution environments
\value Windows Windows execution environments
*/
/*!
\enum TQSettings::Format
\value Native Store the settings in a platform dependent location
\value Ini Store the settings in a text file
*/
/*!
\enum TQSettings::Scope
\value Global Save settings as global as possible
\value User Save settings in user space
*/
#if defined(Q_OS_UNIX)
typedef int HANDLE;
#define Q_LOCKREAD F_RDLCK
#define Q_LOCKWRITE F_WRLCK
/*
Locks the file specified by name. The lockfile is created as a
hidden file in the same directory as the target file, with .lock
appended to the name. For example, "/etc/settings/onerc" uses a
lockfile named "/etc/settings/.onerc.lock". The type argument
controls the type of the lock, it can be either F_RDLCK for a read
lock, or F_WRLCK for a write lock.
A file descriptor for the lock file is returned, and should be
closed with closelock() when the lock is no longer needed.
*/
static HANDLE openlock( const TQString &name, int type )
{
TQFileInfo info( name );
// lockfile should be hidden, and never removed
TQString lockfile = info.dirPath() + "/." + info.fileName() + ".lock";
// open the lockfile
HANDLE fd = qt_open( TQFile::encodeName( lockfile ),
O_RDWR | O_CREAT, S_IRUSR | S_IWUSR );
if ( fd < 0 ) {
// failed to open the lock file, most likely because of permissions
return fd;
}
struct flock fl;
fl.l_type = type;
fl.l_whence = SEEK_SET;
fl.l_start = 0;
fl.l_len = 0;
if ( fcntl( fd, F_SETLKW, &fl ) == -1 ) {
// the lock failed, so we should fail silently, so that people
// using filesystems that do not support locking don't see
// numerous warnings about a failed lock
close( fd );
fd = -1;
}
return fd;
}
/*
Closes the lock file specified by fd. fd is the file descriptor
returned by the openlock() function.
*/
static void closelock( HANDLE fd )
{
if ( fd < 0 ) {
// the lock file is not open
return;
}
struct flock fl;
fl.l_type = F_UNLCK;
fl.l_whence = SEEK_SET;
fl.l_start = 0;
fl.l_len = 0;
// ignore the return value, so that the unlock fails silently
(void) fcntl( fd, F_SETLKW, &fl );
close( fd );
}
#endif
TQSettingsGroup::TQSettingsGroup()
: modified(FALSE)
{
}
void TQSettingsHeading::read(const TQString &filename)
{
if (! TQFileInfo(filename).exists())
return;
#ifndef Q_WS_WIN
HANDLE lockfd = openlock( filename, Q_LOCKREAD );
#endif
TQFile file(filename);
if (! file.open(IO_ReadOnly)) {
#if defined(QT_CHECK_STATE)
tqWarning("TQSettings: failed to open file '%s'", filename.latin1());
#endif
return;
}
git = end();
TQTextStream stream(&file);
stream.setEncoding(TQTextStream::UnicodeUTF8);
while (! stream.atEnd())
parseLine(stream);
git = end();
file.close();
#ifndef Q_WS_WIN
closelock( lockfd );
#endif
}
void TQSettingsHeading::parseLine(TQTextStream &stream)
{
TQString line = stream.readLine();
if (line.isEmpty())
// empty line... we'll allow it
return;
if (line[0] == TQChar('#'))
// commented line
return;
if (line[0] == TQChar('[')) {
TQString gname = line;
gname = gname.remove((uint)0, 1);
if (gname[(int)gname.length() - 1] == TQChar(']'))
gname = gname.remove(gname.length() - 1, 1);
git = find(gname);
if (git == end())
git = replace(gname, TQSettingsGroup());
} else {
if (git == end()) {
#if defined(QT_CHECK_STATE)
tqWarning("TQSettings: line '%s' out of group", line.latin1());
#endif
return;
}
int i = line.find('=');
if (i == -1) {
#if defined(QT_CHECK_STATE)
tqWarning("TQSettings: malformed line '%s' in group '%s'",
line.latin1(), git.key().latin1());
#endif
return;
} else {
TQString key, value;
key = line.left(i);
value = "";
bool esc=TRUE;
i++;
while (esc) {
esc = FALSE;
for ( ; i < (int)line.length(); i++ ) {
if ( esc ) {
if ( line[i] == 'n' )
value.append('\n'); // escaped newline
else if ( line[i] == '0' )
value = TQString::null; // escaped empty string
else
value.append(line[i]);
esc = FALSE;
} else if ( line[i] == '\\' )
esc = TRUE;
else
value.append(line[i]);
}
if ( esc ) {
// Backwards-compatiblity...
// still escaped at EOL - manually escaped "newline"
if (stream.atEnd()) {
#if defined(QT_CHECK_STATE)
tqWarning("TQSettings: reached end of file, expected continued line");
#endif
break;
}
value.append('\n');
line = stream.readLine();
i = 0;
}
}
(*git).insert(key, value);
}
}
}
#ifdef Q_WS_WIN // for homedirpath reading from registry
#include "qt_windows.h"
#include "ntqlibrary.h"
#ifndef CSIDL_APPDATA
#define CSIDL_APPDATA 0x001a // <user name>\Application Data
#endif
#ifndef CSIDL_COMMON_APPDATA
#define CSIDL_COMMON_APPDATA 0x0023 // All Users\Application Data
#endif
#endif
TQSettingsPrivate::TQSettingsPrivate( TQSettings::Format format )
: groupDirty( TRUE ), modified(FALSE), globalScope(TRUE)
{
#if !defined(TQWS) && (defined(Q_WS_WIN) || defined(Q_OS_MAC))
if ( format != TQSettings::Ini )
return;
#else
Q_UNUSED( format );
#endif
TQString appSettings(TQDir::homeDirPath() + "/.qt/");
TQString defPath;
#ifdef Q_WS_WIN
#ifdef Q_OS_TEMP
TCHAR path[MAX_PATH];
SHGetSpecialFolderPath( 0, path, CSIDL_APPDATA, FALSE );
appSettings = TQString::fromUcs2( path );
SHGetSpecialFolderPath( 0, path, CSIDL_COMMON_APPDATA, FALSE );
defPath = TQString::fromUcs2( path );
#else
TQLibrary library( "shell32" );
library.setAutoUnload( FALSE );
QT_WA( {
typedef BOOL (WINAPI*GetSpecialFolderPath)(HWND, LPTSTR, int, BOOL);
GetSpecialFolderPath SHGetSpecialFolderPath = (GetSpecialFolderPath)library.resolve( "SHGetSpecialFolderPathW" );
if ( SHGetSpecialFolderPath ) {
TCHAR path[MAX_PATH];
SHGetSpecialFolderPath( 0, path, CSIDL_APPDATA, FALSE );
appSettings = TQString::fromUcs2( (ushort*)path );
SHGetSpecialFolderPath( 0, path, CSIDL_COMMON_APPDATA, FALSE );
defPath = TQString::fromUcs2( (ushort*)path );
}
} , {
typedef BOOL (WINAPI*GetSpecialFolderPath)(HWND, char*, int, BOOL);
GetSpecialFolderPath SHGetSpecialFolderPath = (GetSpecialFolderPath)library.resolve( "SHGetSpecialFolderPathA" );
if ( SHGetSpecialFolderPath ) {
char path[MAX_PATH];
SHGetSpecialFolderPath( 0, path, CSIDL_APPDATA, FALSE );
appSettings = TQString::fromLocal8Bit( path );
SHGetSpecialFolderPath( 0, path, CSIDL_COMMON_APPDATA, FALSE );
defPath = TQString::fromLocal8Bit( path );
}
} );
#endif // Q_OS_TEMP
#else
defPath = tqInstallPathSysconf();
#endif
TQDir dir(appSettings);
if (! dir.exists()) {
if (! dir.mkdir(dir.path()))
#if defined(QT_CHECK_STATE)
tqWarning("TQSettings: error creating %s", dir.path().latin1());
#else
;
#endif
}
if ( !!defPath )
searchPaths.append(defPath);
searchPaths.append(dir.path());
}
TQSettingsPrivate::~TQSettingsPrivate()
{
}
TQSettingsGroup TQSettingsPrivate::readGroup()
{
TQSettingsHeading hd;
TQSettingsGroup grp;
TQMap<TQString,TQSettingsHeading>::Iterator headingsit = headings.find(heading);
if (headingsit != headings.end())
hd = *headingsit;
TQSettingsHeading::Iterator grpit = hd.find(group);
if (grpit == hd.end()) {
TQStringList::Iterator it = searchPaths.begin();
if ( !globalScope )
++it;
while (it != searchPaths.end()) {
TQString filebase = heading.lower().replace(TQRegExp(TQString::fromLatin1("\\s+")), "_");
TQString fn((*it++) + "/" + filebase + "rc");
if (! hd.contains(fn + "cached")) {
hd.read(fn);
hd.insert(fn + "cached", TQSettingsGroup());
}
}
headings.replace(heading, hd);
grpit = hd.find(group);
if (grpit != hd.end())
grp = *grpit;
} else if (hd.count() != 0)
grp = *grpit;
return grp;
}
void TQSettingsPrivate::removeGroup(const TQString &key)
{
TQSettingsHeading hd;
TQSettingsGroup grp;
bool found = FALSE;
TQMap<TQString,TQSettingsHeading>::Iterator headingsit = headings.find(heading);
if (headingsit != headings.end())
hd = *headingsit;
TQSettingsHeading::Iterator grpit = hd.find(group);
if (grpit == hd.end()) {
TQStringList::Iterator it = searchPaths.begin();
if ( !globalScope )
++it;
while (it != searchPaths.end()) {
TQString filebase = heading.lower().replace(TQRegExp(TQString::fromLatin1("\\s+")), "_");
TQString fn((*it++) + "/" + filebase + "rc");
if (! hd.contains(fn + "cached")) {
hd.read(fn);
hd.insert(fn + "cached", TQSettingsGroup());
}
}
headings.replace(heading, hd);
grpit = hd.find(group);
if (grpit != hd.end()) {
found = TRUE;
grp = *grpit;
}
} else if (hd.count() != 0) {
found = TRUE;
grp = *grpit;
}
if (found) {
grp.remove(key);
if (grp.count() > 0)
hd.replace(group, grp);
else
hd.remove(group);
if (hd.count() > 0)
headings.replace(heading, hd);
else
headings.remove(heading);
modified = TRUE;
}
}
void TQSettingsPrivate::writeGroup(const TQString &key, const TQString &value)
{
TQSettingsHeading hd;
TQSettingsGroup grp;
TQMap<TQString,TQSettingsHeading>::Iterator headingsit = headings.find(heading);
if (headingsit != headings.end())
hd = *headingsit;
TQSettingsHeading::Iterator grpit = hd.find(group);
if (grpit == hd.end()) {
TQStringList::Iterator it = searchPaths.begin();
if ( !globalScope )
++it;
while (it != searchPaths.end()) {
TQString filebase = heading.lower().replace(TQRegExp(TQString::fromLatin1("\\s+")), "_");
TQString fn((*it++) + "/" + filebase + "rc");
if (! hd.contains(fn + "cached")) {
hd.read(fn);
hd.insert(fn + "cached", TQSettingsGroup());
}
}
headings.replace(heading, hd);
grpit = hd.find(group);
if (grpit != hd.end())
grp = *grpit;
} else if (hd.count() != 0)
grp = *grpit;
grp.modified = TRUE;
grp.replace(key, value);
hd.replace(group, grp);
headings.replace(heading, hd);
modified = TRUE;
}
TQDateTime TQSettingsPrivate::modificationTime()
{
TQSettingsHeading hd = headings[heading];
TQSettingsGroup grp = hd[group];
TQDateTime datetime;
TQStringList::Iterator it = searchPaths.begin();
if ( !globalScope )
++it;
while (it != searchPaths.end()) {
TQFileInfo fi((*it++) + "/" + heading + "rc");
if (fi.exists() && fi.lastModified() > datetime)
datetime = fi.lastModified();
}
return datetime;
}
bool qt_verify_key( const TQString &key )
{
if ( key.isEmpty() || key[0] != '/' || key.contains( TQRegExp(TQString::fromLatin1("[=\\r\\n]")) ) )
return FALSE;
return TRUE;
}
static TQString groupKey( const TQString &group, const TQString &key )
{
TQString grp_key;
if ( group.isEmpty() || ( group.length() == 1 && group[0] == '/' ) ) {
// group is empty, or it contains a single '/', so we just return the key
if ( key.startsWith( "/" ) )
grp_key = key;
else
grp_key = "/" + key;
} else if ( group.endsWith( "/" ) || key.startsWith( "/" ) ) {
grp_key = group + key;
} else {
grp_key = group + "/" + key;
}
return grp_key;
}
/*!
Inserts \a path into the settings search path. The semantics of \a
path depends on the system \a s. It is usually easier and better to
use setPath() instead of this function.
When \a s is \e Windows and the execution environment is \e not
Windows the function does nothing. Similarly when \a s is \e Unix and
the execution environment is \e not Unix the function does nothing.
When \a s is \e Windows, and the execution environment is Windows, the
search path list will be used as the first subfolder of the "Software"
folder in the registry.
When reading settings the folders are searched forwards from the
first folder (listed below) to the last, returning the first
settings found, and ignoring any folders for which the user doesn't
have read permission.
\list 1
\i HKEY_CURRENT_USER/Software/MyCompany/MyApplication
\i HKEY_LOCAL_MACHINE/Software/MyCompany/MyApplication
\i HKEY_CURRENT_USER/Software/MyApplication
\i HKEY_LOCAL_MACHINE/Software/MyApplication
\endlist
\code
TQSettings settings;
settings.insertSearchPath( TQSettings::Windows, "/MyCompany" );
settings.writeEntry( "/MyApplication/Tip of the day", TRUE );
\endcode
The code above will write the subkey "Tip of the day" into the \e
first of the registry folders listed below that is found and for
which the user has write permission.
\list 1
\i HKEY_LOCAL_MACHINE/Software/MyCompany/MyApplication
\i HKEY_CURRENT_USER/Software/MyCompany/MyApplication
\i HKEY_LOCAL_MACHINE/Software/MyApplication
\i HKEY_CURRENT_USER/Software/MyApplication
\endlist
If a setting is found in the HKEY_CURRENT_USER space, this setting
is overwritten independently of write permissions in the
HKEY_LOCAL_MACHINE space.
When \a s is \e Unix, and the execution environment is Unix, the
search path list will be used when trying to determine a suitable
filename for reading and writing settings files. By default, there are
two entries in the search path:
\list 1
\i \c SYSCONF - where \c SYSCONF is a directory specified when
configuring TQt; by default it is INSTALL/etc/settings.
\i \c $HOME/.qt/ - where \c $HOME is the user's home directory.
\endlist
All insertions into the search path will go before $HOME/.qt/.
For example:
\code
TQSettings settings;
settings.insertSearchPath( TQSettings::Unix, "/opt/MyCompany/share/etc" );
settings.insertSearchPath( TQSettings::Unix, "/opt/MyCompany/share/MyApplication/etc" );
// ...
\endcode
Will result in a search path of:
\list 1
\i SYSCONF
\i /opt/MyCompany/share/etc
\i /opt/MyCompany/share/MyApplication/etc
\i $HOME/.qt
\endlist
When reading settings the files are searched in the order shown
above, with later settings overriding earlier settings. Files for
which the user doesn't have read permission are ignored. When saving
settings TQSettings works in the order shown above, writing
to the first settings file for which the user has write permission.
Note that paths in the file system are not created by this
function, so they must already exist to be useful.
Settings under Unix are stored in files whose names are based on the
first subkey of the key (not including the search path). The algorithm
for creating names is essentially: lowercase the first subkey, replace
spaces with underscores and add 'rc', e.g.
<tt>/MyCompany/MyApplication/background color</tt> will be stored in
<tt>myapplicationrc</tt> (assuming that <tt>/MyCompany</tt> is part of
the search path).
\sa removeSearchPath()
*/
void TQSettings::insertSearchPath( System s, const TQString &path)
{
#if !defined(TQWS) && (defined(Q_WS_WIN) || defined(Q_OS_MAC))
if ( d->sysd ) {
d->sysInsertSearchPath( s, path );
return;
}
#endif
#if !defined(Q_WS_WIN)
if ( s == Windows )
return;
#endif
#if !defined(Q_OS_MAC)
if ( s == Mac )
return;
#endif
if ( !qt_verify_key( path ) ) {
#if defined(QT_CHECK_STATE)
tqWarning( "TQSettings::insertSearchPath: Invalid key: '%s'", path.isNull() ? "(null)" : path.latin1() );
#endif
return;
}
#if !defined(TQWS) && (defined(Q_WS_WIN) || defined(Q_OS_MAC))
if ( d->sysd && s != Unix ) {
#else
if ( s != Unix ) {
#endif
#if !defined(TQWS) && defined(Q_OS_MAC)
if(s != Mac) //mac is respected on the mac as well
#endif
return;
}
TQString realPath = path;
#if defined(Q_WS_WIN)
TQString defPath = d->globalScope ? d->searchPaths.first() : d->searchPaths.last();
realPath = defPath + path;
#endif
TQStringList::Iterator it = d->searchPaths.find(d->searchPaths.last());
if (it != d->searchPaths.end()) {
d->searchPaths.insert(it, realPath);
}
}
/*!
Removes all occurrences of \a path (using exact matching) from the
settings search path for system \a s. Note that the default search
paths cannot be removed.
\sa insertSearchPath()
*/
void TQSettings::removeSearchPath( System s, const TQString &path)
{
if ( !qt_verify_key( path ) ) {
#if defined(QT_CHECK_STATE)
tqWarning( "TQSettings::insertSearchPath: Invalid key: '%s'", path.isNull() ? "(null)" : path.latin1() );
#endif
return;
}
#ifdef Q_WS_WIN
if ( d->sysd ) {
d->sysRemoveSearchPath( s, path );
return;
}
#endif
#if !defined(TQWS) && (defined(Q_WS_WIN) || defined(Q_OS_MAC))
if ( d->sysd && s != Unix ) {
#else
if ( s != Unix ) {
#endif
#if !defined(TQWS) && defined(Q_OS_MAC)
if(s != Mac) //mac is respected on the mac as well
#endif
return;
}
if (path == d->searchPaths.first() || path == d->searchPaths.last())
return;
d->searchPaths.remove(path);
}
/*!
Creates a settings object.
Be aware that you must call setPath() or insertSearchPath() before
you can use the TQSettings object.
*/
TQSettings::TQSettings()
{
d = new TQSettingsPrivate( Native );
TQ_CHECK_PTR(d);
#if !defined(TQWS) && (defined(Q_WS_WIN) || defined(Q_OS_MAC))
d->sysd = 0;
d->sysInit();
#endif
}
/*!
Creates a settings object. If \a format is 'Ini' the settings will
be stored in a text file, using the Unix strategy (see above). If \a format
is 'Native', the settings will be stored in a platform specific way
(ie. the Windows registry).
Be aware that you must call setPath() or insertSearchPath() before
you can use the TQSettings object.
*/
TQSettings::TQSettings( Format format )
{
d = new TQSettingsPrivate( format );
TQ_CHECK_PTR(d);
#if !defined(TQWS) && (defined(Q_WS_WIN) || defined(Q_OS_MAC))
d->sysd = 0;
if ( format == Native )
d->sysInit();
#else
Q_UNUSED(format);
#endif
}
/*!
Destroys the settings object. All modifications made to the settings
will automatically be saved.
*/
TQSettings::~TQSettings()
{
sync();
#if !defined(TQWS) && (defined(Q_WS_WIN) || defined(Q_OS_MAC))
if ( d->sysd )
d->sysClear();
#endif
delete d;
}
/*! \internal
Writes all modifications to the settings to disk. If any errors are
encountered, this function returns FALSE, otherwise it will return TRUE.
*/
bool TQSettings::sync()
{
#if !defined(TQWS) && (defined(Q_WS_WIN) || defined(Q_OS_MAC))
if ( d->sysd )
return d->sysSync();
#endif
if (! d->modified)
// fake success
return TRUE;
bool success = TRUE;
TQMap<TQString,TQSettingsHeading>::Iterator it = d->headings.begin();
while (it != d->headings.end()) {
// determine filename
TQSettingsHeading hd(*it);
TQSettingsHeading::Iterator hdit = hd.begin();
TQString filename;
TQStringList::Iterator pit = d->searchPaths.begin();
if ( !d->globalScope )
++pit;
while (pit != d->searchPaths.end()) {
TQString filebase = it.key().lower().replace(TQRegExp(TQString::fromLatin1("\\s+")), "_");
TQFileInfo di(*pit);
if ( !di.exists() ) {
TQDir dir;
dir.mkdir( *pit );
}
TQFileInfo fi((*pit++) + "/" + filebase + "rc");
if ((fi.exists() && fi.isFile() && fi.isWritable()) ||
(! fi.exists() && di.isDir()
#ifndef Q_WS_WIN
&& di.isWritable()
#else
&& ((qWinVersion()&TQt::WV_NT_based) > TQt::WV_2000 || di.isWritable())
#endif
)) {
filename = fi.filePath();
break;
}
}
++it;
if ( filename.isEmpty() ) {
#ifdef QT_CHECK_STATE
tqWarning("TQSettings::sync: filename is null/empty");
#endif // QT_CHECK_STATE
success = FALSE;
continue;
}
#ifndef Q_WS_WIN
HANDLE lockfd = openlock( filename, Q_LOCKWRITE );
#endif
TQFile file( filename + ".tmp" );
if (! file.open(IO_WriteOnly)) {
#ifdef QT_CHECK_STATE
tqWarning("TQSettings::sync: failed to open '%s' for writing",
file.name().latin1());
#endif // QT_CHECK_STATE
success = FALSE;
continue;
}
// spew to file
TQTextStream stream(&file);
stream.setEncoding(TQTextStream::UnicodeUTF8);
while (hdit != hd.end()) {
if ((*hdit).count() > 0) {
stream << "[" << hdit.key() << "]" << endl;
TQSettingsGroup grp(*hdit);
TQSettingsGroup::Iterator grpit = grp.begin();
while (grpit != grp.end()) {
TQString v = grpit.data();
if ( v.isNull() ) {
v = TQString::fromLatin1("\\0"); // escape null string
} else {
v.replace('\\', TQString::fromLatin1("\\\\")); // escape backslash
v.replace('\n', TQString::fromLatin1("\\n")); // escape newlines
}
stream << grpit.key() << "=" << v << endl;
++grpit;
}
stream << endl;
}
++hdit;
}
if (file.status() != IO_Ok) {
#ifdef QT_CHECK_STATE
tqWarning("TQSettings::sync: error at end of write");
#endif // QT_CHECK_STATE
success = FALSE;
}
file.close();
if ( success ) {
TQDir dir( TQFileInfo( file ).dir( TRUE ) );
if ( dir.exists( filename ) && !dir.remove( filename ) ||
!dir.rename( file.name(), filename, TRUE ) ) {
#ifdef QT_CHECK_STATE
tqWarning( "TQSettings::sync: error writing file '%s'",
TQFile::encodeName( filename ).data() );
#endif // QT_CHECK_STATE
success = FALSE;
}
}
// remove temporary file
file.remove();
#ifndef Q_WS_WIN
closelock( lockfd );
#endif
}
d->modified = FALSE;
return success;
}
/*!
\fn bool TQSettings::readBoolEntry(const TQString &key, bool def, bool *ok ) const
Reads the entry specified by \a key, and returns a bool, or the
default value, \a def, if the entry couldn't be read.
If \a ok is non-null, *ok is set to TRUE if the key was read, FALSE
otherwise.
\sa readEntry(), readNumEntry(), readDoubleEntry(), writeEntry(), removeEntry()
*/
/*!
\internal
*/
bool TQSettings::readBoolEntry(const TQString &key, bool def, bool *ok )
{
TQString grp_key( groupKey( group(), key ) );
if ( !qt_verify_key( grp_key ) ) {
#if defined(QT_CHECK_STATE)
tqWarning( "TQSettings::readBoolEntry: Invalid key: '%s'", grp_key.isNull() ? "(null)" : grp_key.latin1() );
#endif
if ( ok )
*ok = FALSE;
return def;
}
#if !defined(TQWS) && (defined(Q_WS_WIN) || defined(Q_OS_MAC))
if ( d->sysd )
return d->sysReadBoolEntry( grp_key, def, ok );
#endif
TQString value = readEntry( key, ( def ? "true" : "false" ), ok );
if (value.lower() == "true")
return TRUE;
else if (value.lower() == "false")
return FALSE;
else if (value == "1")
return TRUE;
else if (value == "0")
return FALSE;
if (! value.isEmpty())
tqWarning("TQSettings::readBoolEntry: '%s' is not 'true' or 'false'",
value.latin1());
if ( ok )
*ok = FALSE;
return def;
}
/*!
\fn double TQSettings::readDoubleEntry(const TQString &key, double def, bool *ok ) const
Reads the entry specified by \a key, and returns a double, or the
default value, \a def, if the entry couldn't be read.
If \a ok is non-null, *ok is set to TRUE if the key was read, FALSE
otherwise.
\sa readEntry(), readNumEntry(), readBoolEntry(), writeEntry(), removeEntry()
*/
/*!
\internal
*/
double TQSettings::readDoubleEntry(const TQString &key, double def, bool *ok )
{
TQString grp_key( groupKey( group(), key ) );
if ( !qt_verify_key( grp_key ) ) {
#if defined(QT_CHECK_STATE)
tqWarning( "TQSettings::readDoubleEntry: Invalid key: '%s'", grp_key.isNull() ? "(null)" : grp_key.latin1() );
#endif
if ( ok )
*ok = FALSE;
return def;
}
#if !defined(TQWS) && (defined(Q_WS_WIN) || defined(Q_OS_MAC))
if ( d->sysd )
return d->sysReadDoubleEntry( grp_key, def, ok );
#endif
TQString value = readEntry( key, TQString::number(def), ok );
bool conv_ok;
double retval = value.toDouble( &conv_ok );
if ( conv_ok )
return retval;
if ( ! value.isEmpty() )
tqWarning( "TQSettings::readDoubleEntry: '%s' is not a number",
value.latin1() );
if ( ok )
*ok = FALSE;
return def;
}
/*!
\fn int TQSettings::readNumEntry(const TQString &key, int def, bool *ok ) const
Reads the entry specified by \a key, and returns an integer, or the
default value, \a def, if the entry couldn't be read.
If \a ok is non-null, *ok is set to TRUE if the key was read, FALSE
otherwise.
\sa readEntry(), readDoubleEntry(), readBoolEntry(), writeEntry(), removeEntry()
*/
/*!
\internal
*/
int TQSettings::readNumEntry(const TQString &key, int def, bool *ok )
{
TQString grp_key( groupKey( group(), key ) );
if ( !qt_verify_key( grp_key ) ) {
#if defined(QT_CHECK_STATE)
tqWarning( "TQSettings::readNumEntry: Invalid key: '%s'", grp_key.isNull() ? "(null)" : grp_key.latin1() );
#endif
if ( ok )
*ok = FALSE;
return def;
}
#if !defined(TQWS) && (defined(Q_WS_WIN) || defined(Q_OS_MAC))
if ( d->sysd )
return d->sysReadNumEntry( grp_key, def, ok );
#endif
TQString value = readEntry( key, TQString::number( def ), ok );
bool conv_ok;
int retval = value.toInt( &conv_ok );
if ( conv_ok )
return retval;
if ( ! value.isEmpty() )
tqWarning( "TQSettings::readNumEntry: '%s' is not a number",
value.latin1() );
if ( ok )
*ok = FALSE;
return def;
}
/*!
\fn TQString TQSettings::readEntry(const TQString &key, const TQString &def, bool *ok ) const
Reads the entry specified by \a key, and returns a TQString, or the
default value, \a def, if the entry couldn't be read.
If \a ok is non-null, *ok is set to TRUE if the key was read, FALSE
otherwise.
\sa readListEntry(), readNumEntry(), readDoubleEntry(), readBoolEntry(), writeEntry(), removeEntry()
*/
/*!
\internal
*/
TQString TQSettings::readEntry(const TQString &key, const TQString &def, bool *ok )
{
TQString grp_key( groupKey( group(), key ) );
if ( !qt_verify_key( grp_key ) ) {
#if defined(QT_CHECK_STATE)
tqWarning( "TQSettings::readEntry: Invalid key: '%s'", grp_key.isNull() ? "(null)" : grp_key.latin1() );
#endif
if ( ok )
*ok = FALSE;
return def;
}
#if !defined(TQWS) && (defined(Q_WS_WIN) || defined(Q_OS_MAC))
if ( d->sysd )
return d->sysReadEntry( grp_key, def, ok );
#endif
if ( ok ) // no, everything is not ok
*ok = FALSE;
TQString realkey;
if (grp_key[0] == '/') {
// parse our key
TQStringList list(TQStringList::split('/', grp_key));
if (list.count() < 2) {
#ifdef QT_CHECK_STATE
tqWarning("TQSettings::readEntry: invalid key '%s'", grp_key.latin1());
#endif // QT_CHECK_STATE
if ( ok )
*ok = FALSE;
return def;
}
if (list.count() == 2) {
d->heading = list[0];
d->group = "General";
realkey = list[1];
} else {
d->heading = list[0];
d->group = list[1];
// remove the group from the list
list.remove(list.at(1));
// remove the heading from the list
list.remove(list.at(0));
realkey = list.join("/");
}
} else {
realkey = grp_key;
}
TQSettingsGroup grp = d->readGroup();
TQSettingsGroup::const_iterator it = grp.find( realkey ), end = grp.end();
TQString retval = def;
if ( it != end ) {
// found the value we needed
retval = *it;
if ( ok ) *ok = TRUE;
}
return retval;
}
#if !defined(Q_NO_BOOL_TYPE)
/*!
Writes the boolean entry \a value into key \a key. The \a key is
created if it doesn't exist. Any previous value is overwritten by \a
value.
If an error occurs the settings are left unchanged and FALSE is
returned; otherwise TRUE is returned.
\warning On certain platforms, keys are required to contain at least
two components (e.g., "/foo/bar"). This limitation does not apply to
TQt 4.
\sa readListEntry(), readNumEntry(), readDoubleEntry(), readBoolEntry(), removeEntry()
*/
bool TQSettings::writeEntry(const TQString &key, bool value)
{
TQString grp_key( groupKey( group(), key ) );
if ( !qt_verify_key( grp_key ) ) {
#if defined(QT_CHECK_STATE)
tqWarning( "TQSettings::writeEntry: Invalid key: '%s'", grp_key.isNull() ? "(null)" : grp_key.latin1() );
#endif
return FALSE;
}
#if !defined(TQWS) && (defined(Q_WS_WIN) || defined(Q_OS_MAC))
if ( d->sysd )
return d->sysWriteEntry( grp_key, value );
#endif
TQString s(value ? "true" : "false");
return writeEntry(key, s);
}
#endif
/*!
\overload
Writes the double entry \a value into key \a key. The \a key is
created if it doesn't exist. Any previous value is overwritten by \a
value.
If an error occurs the settings are left unchanged and FALSE is
returned; otherwise TRUE is returned.
\sa readListEntry(), readNumEntry(), readDoubleEntry(), readBoolEntry(), removeEntry()
*/
bool TQSettings::writeEntry(const TQString &key, double value)
{
TQString grp_key( groupKey( group(), key ) );
if ( !qt_verify_key( grp_key ) ) {
#if defined(QT_CHECK_STATE)
tqWarning( "TQSettings::writeEntry: Invalid key: '%s'", grp_key.isNull() ? "(null)" : grp_key.latin1() );
#endif
return FALSE;
}
#if !defined(TQWS) && (defined(Q_WS_WIN) || defined(Q_OS_MAC))
if ( d->sysd )
return d->sysWriteEntry( grp_key, value );
#endif
TQString s(TQString::number(value));
return writeEntry(key, s);
}
/*!
\overload
Writes the integer entry \a value into key \a key. The \a key is
created if it doesn't exist. Any previous value is overwritten by \a
value.
If an error occurs the settings are left unchanged and FALSE is
returned; otherwise TRUE is returned.
\sa readListEntry(), readNumEntry(), readDoubleEntry(), readBoolEntry(), removeEntry()
*/
bool TQSettings::writeEntry(const TQString &key, int value)
{
TQString grp_key( groupKey( group(), key ) );
if ( !qt_verify_key( grp_key ) ) {
#if defined(QT_CHECK_STATE)
tqWarning( "TQSettings::writeEntry: Invalid key: '%s'", grp_key.isNull() ? "(null)" : grp_key.latin1() );
#endif
return FALSE;
}
#if !defined(TQWS) && (defined(Q_WS_WIN) || defined(Q_OS_MAC))
if ( d->sysd )
return d->sysWriteEntry( grp_key, value );
#endif
TQString s(TQString::number(value));
return writeEntry(key, s);
}
/*!
\internal
Writes the entry specified by \a key with the string-literal \a value,
replacing any previous setting. If \a value is zero-length or null, the
entry is replaced by an empty setting.
\e NOTE: This function is provided because some compilers use the
writeEntry (const TQString &, bool) overload for this code:
writeEntry ("/foo/bar", "baz")
If an error occurs, this functions returns FALSE and the object is left
unchanged.
\sa readEntry(), removeEntry()
*/
bool TQSettings::writeEntry(const TQString &key, const char *value)
{
return writeEntry(key, TQString(value));
}
/*!
\overload
Writes the string entry \a value into key \a key. The \a key is
created if it doesn't exist. Any previous value is overwritten by \a
value. If \a value is an empty string or a null string the key's
value will be an empty string.
If an error occurs the settings are left unchanged and FALSE is
returned; otherwise TRUE is returned.
\sa readListEntry(), readNumEntry(), readDoubleEntry(), readBoolEntry(), removeEntry()
*/
bool TQSettings::writeEntry(const TQString &key, const TQString &value)
{
TQString grp_key( groupKey( group(), key ) );
if ( !qt_verify_key( grp_key ) ) {
#if defined(QT_CHECK_STATE)
tqWarning( "TQSettings::writeEntry: Invalid key: '%s'", grp_key.isNull() ? "(null)" : grp_key.latin1() );
#endif
return FALSE;
}
#if !defined(TQWS) && (defined(Q_WS_WIN) || defined(Q_OS_MAC))
if ( d->sysd )
return d->sysWriteEntry( grp_key, value );
#endif
// NOTE: we *do* allow value to be a null/empty string
TQString realkey;
if (grp_key[0] == '/') {
// parse our key
TQStringList list(TQStringList::split('/', grp_key));
if (list.count() < 2) {
#ifdef QT_CHECK_STATE
tqWarning("TQSettings::writeEntry: invalid key '%s'", grp_key.latin1());
#endif // QT_CHECK_STATE
return FALSE;
}
if (list.count() == 2) {
d->heading = list[0];
d->group = "General";
realkey = list[1];
} else {
d->heading = list[0];
d->group = list[1];
// remove the group from the list
list.remove(list.at(1));
// remove the heading from the list
list.remove(list.at(0));
realkey = list.join("/");
}
} else {
realkey = grp_key;
}
d->writeGroup(realkey, value);
return TRUE;
}
/*!
Removes the entry specified by \a key.
Returns true if the entry was successfully removed; otherwise
returns false. Note that removing the last entry in any given
folder, will also remove the folder.
\sa readEntry(), writeEntry()
*/
bool TQSettings::removeEntry(const TQString &key)
{
TQString grp_key( groupKey( group(), key ) );
if ( !qt_verify_key( grp_key ) ) {
#if defined(QT_CHECK_STATE)
tqWarning( "TQSettings::removeEntry: Invalid key: '%s'", grp_key.isNull() ? "(null)" : grp_key.latin1() );
#endif
return FALSE;
}
#if !defined(TQWS) && (defined(Q_WS_WIN) || defined(Q_OS_MAC))
if ( d->sysd )
return d->sysRemoveEntry( grp_key );
#endif
TQString realkey;
if (grp_key[0] == '/') {
// parse our key
TQStringList list(TQStringList::split('/', grp_key));
if (list.count() < 2) {
#ifdef QT_CHECK_STATE
tqWarning("TQSettings::removeEntry: invalid key '%s'", grp_key.latin1());
#endif // QT_CHECK_STATE
return FALSE;
}
if (list.count() == 2) {
d->heading = list[0];
d->group = "General";
realkey = list[1];
} else {
d->heading = list[0];
d->group = list[1];
// remove the group from the list
list.remove(list.at(1));
// remove the heading from the list
list.remove(list.at(0));
realkey = list.join("/");
}
} else {
realkey = grp_key;
}
d->removeGroup(realkey);
return TRUE;
}
/*!
Returns a list of the keys which contain entries under \a key. Does \e
not return any keys that contain subkeys.
Example settings:
\code
/MyCompany/MyApplication/background color
/MyCompany/MyApplication/foreground color
/MyCompany/MyApplication/geometry/x
/MyCompany/MyApplication/geometry/y
/MyCompany/MyApplication/geometry/width
/MyCompany/MyApplication/geometry/height
\endcode
\code
TQStringList keys = settings.entryList( "/MyCompany/MyApplication" );
\endcode
In the above example, \c keys will contain 'background color' and
'foreground color'. It will not contain 'geometry' because this key
contains subkeys not entries.
To access the geometry values, you could either use subkeyList()
to read the keys then read each entry, or simply read each entry
directly by specifying its full key, e.g.
"/MyCompany/MyApplication/geometry/y".
\sa subkeyList()
*/
TQStringList TQSettings::entryList(const TQString &key) const
{
TQString grp_key( groupKey( group(), key ) );
if ( !qt_verify_key( grp_key ) ) {
#if defined(QT_CHECK_STATE)
tqWarning( "TQSettings::entryList: Invalid key: %s", grp_key.isNull() ? "(null)" : grp_key.latin1() );
#endif
return TQStringList();
}
#if !defined(TQWS) && (defined(Q_WS_WIN) || defined(Q_OS_MAC))
if ( d->sysd )
return d->sysEntryList( grp_key );
#endif
TQString realkey;
if (grp_key[0] == '/') {
// parse our key
TQStringList list(TQStringList::split('/', grp_key));
if (list.count() < 1) {
#ifdef QT_CHECK_STATE
tqWarning("TQSettings::listEntries: invalid key '%s'", grp_key.latin1());
#endif // QT_CHECK_STATE
return TQStringList();
}
if (list.count() == 1) {
d->heading = list[0];
d->group = "General";
} else {
d->heading = list[0];
d->group = list[1];
// remove the group from the list
list.remove(list.at(1));
// remove the heading from the list
list.remove(list.at(0));
realkey = list.join("/");
}
} else
realkey = grp_key;
TQSettingsGroup grp = d->readGroup();
TQSettingsGroup::Iterator it = grp.begin();
TQStringList ret;
TQString itkey;
while (it != grp.end()) {
itkey = it.key();
++it;
if ( realkey.length() > 0 ) {
if ( itkey.left( realkey.length() ) != realkey )
continue;
else
itkey.remove( 0, realkey.length() + 1 );
}
if ( itkey.find( '/' ) != -1 )
continue;
ret << itkey;
}
return ret;
}
/*!
Returns a list of the keys which contain subkeys under \a key. Does \e
not return any keys that contain entries.
Example settings:
\code
/MyCompany/MyApplication/background color
/MyCompany/MyApplication/foreground color
/MyCompany/MyApplication/geometry/x
/MyCompany/MyApplication/geometry/y
/MyCompany/MyApplication/geometry/width
/MyCompany/MyApplication/geometry/height
/MyCompany/MyApplication/recent files/1
/MyCompany/MyApplication/recent files/2
/MyCompany/MyApplication/recent files/3
\endcode
\code
TQStringList keys = settings.subkeyList( "/MyCompany/MyApplication" );
\endcode
In the above example, \c keys will contain 'geometry' and
'recent files'. It will not contain 'background color' or
'foreground color' because those keys contain entries not
subkeys. To get a list of keys that contain entries rather than
subkeys use entryList() instead.
\warning In the above example, if TQSettings is writing to an Ini file,
then a call to
\code subkeyList("/MyCompany") \endcode
will return an empty list. This happens because a key like
\code /MyCompany/MyApplication/background color \endcode
is written to the file \e{"mycompanyrc"}, under the section \e{[MyApplication]}.
This call is therefore a request to list the sections in an ini file, which
is not supported in this version of TQSettings. This is a known issue which
will be fixed in TQt-4.
\sa entryList()
*/
TQStringList TQSettings::subkeyList(const TQString &key) const
{
TQString grp_key( groupKey( group(), key ) );
if ( !qt_verify_key( grp_key ) ) {
#if defined(QT_CHECK_STATE)
tqWarning( "TQSettings::subkeyList: Invalid key: %s", grp_key.isNull() ? "(null)" : grp_key.latin1() );
#endif
return TQStringList();
}
#if !defined(TQWS) && (defined(Q_WS_WIN) || defined(Q_OS_MAC))
if ( d->sysd )
return d->sysSubkeyList( grp_key );
#endif
TQString realkey;
int subkeycount = 2;
if (grp_key[0] == '/') {
// parse our key
TQStringList list(TQStringList::split('/', grp_key));
if (list.count() < 1) {
#ifdef QT_CHECK_STATE
tqWarning("TQSettings::subkeyList: invalid key '%s'", grp_key.latin1());
#endif // QT_CHECK_STATE
return TQStringList();
}
subkeycount = (int)list.count();
if (list.count() == 1) {
d->heading = list[0];
d->group = "General";
} else {
d->heading = list[0];
d->group = list[1];
// remove the group from the list
list.remove(list.at(1));
// remove the heading from the list
list.remove(list.at(0));
realkey = list.join("/");
}
} else
realkey = grp_key;
TQStringList ret;
if ( subkeycount == 1 ) {
TQMap<TQString,TQSettingsHeading>::Iterator it = d->headings.begin();
while ( it != d->headings.end() ) {
if ( it.key() != "General" && ! ret.contains( it.key() ) )
ret << it.key();
++it;
}
return ret;
}
TQSettingsGroup grp = d->readGroup();
TQSettingsGroup::Iterator it = grp.begin();
TQString itkey;
while (it != grp.end()) {
itkey = it.key();
++it;
if ( realkey.length() > 0 ) {
if ( itkey.left( realkey.length() ) != realkey
|| itkey.length()+1 < realkey.length()
|| itkey[(int)realkey.length()] != '/')
continue;
else
itkey.remove( 0, realkey.length() + 1 );
}
int slash = itkey.find( '/' );
if ( slash == -1 )
continue;
itkey.truncate( slash );
if ( ! ret.contains( itkey ) )
ret << itkey;
}
return ret;
}
/*!
\internal
This function returns the time of last modification for \a key.
*/
TQDateTime TQSettings::lastModificationTime( const TQString &key )
{
TQString grp_key( groupKey( group(), key ) );
if ( !qt_verify_key( grp_key ) ) {
#if defined(QT_CHECK_STATE)
tqWarning( "TQSettings::lastModificationTime: Invalid key '%s'", grp_key.isNull() ? "(null)" : grp_key.latin1() );
#endif
return TQDateTime();
}
#if !defined(TQWS) && (defined(Q_WS_WIN) || defined(Q_OS_MAC))
if ( d->sysd )
return TQDateTime();
#endif
if (grp_key[0] == '/') {
// parse our key
TQStringList list(TQStringList::split('/', grp_key));
if (list.count() < 2) {
#ifdef QT_CHECK_STATE
tqWarning("TQSettings::lastModificationTime: Invalid key '%s'", grp_key.latin1());
#endif // QT_CHECK_STATE
return TQDateTime();
}
if (list.count() == 2) {
d->heading = list[0];
d->group = "General";
} else {
d->heading = list[0];
d->group = list[1];
}
}
return d->modificationTime();
}
/*!
\overload
\obsolete
Writes the string list entry \a value into key \a key. The \a key
is created if it doesn't exist. Any previous value is overwritten
by \a value. The list is stored as a sequence of strings separated
by \a separator (using TQStringList::join()), so none of the
strings in the list should contain the separator. If the list is
empty or null the key's value will be an empty string.
\warning The list should not contain empty or null strings, as
readListEntry() will use TQStringList::split() to recreate the
list. As the documentation states, TQStringList::split() will omit
empty strings from the list. Because of this, it is impossible to
retrieve identical list data that is stored with this function.
We recommend using the writeEntry() and readListEntry() overloads
that do not take a \a separator argument.
If an error occurs the settings are left unchanged and FALSE is
returned; otherwise returns TRUE.
\sa readListEntry(), readNumEntry(), readDoubleEntry(), readBoolEntry(), removeEntry(), TQStringList::join()
*/
bool TQSettings::writeEntry(const TQString &key, const TQStringList &value,
const TQChar &separator)
{
TQString s(value.join(separator));
return writeEntry(key, s);
}
/*!
\overload
Writes the string list entry \a value into key \a key. The \a key
is created if it doesn't exist. Any previous value is overwritten
by \a value.
If an error occurs the settings are left unchanged and FALSE is
returned; otherwise returns TRUE.
\sa readListEntry(), readNumEntry(), readDoubleEntry(), readBoolEntry(), removeEntry()
*/
bool TQSettings::writeEntry(const TQString &key, const TQStringList &value)
{
TQString s;
for (TQStringList::ConstIterator it=value.begin(); it!=value.end(); ++it) {
TQString el = *it;
if ( el.isNull() ) {
el = "^0";
} else {
el.replace("^", "^^");
}
s+=el;
s+="^e"; // end of element
}
return writeEntry(key, s);
}
/*!
\overload TQStringList TQSettings::readListEntry(const TQString &key, const TQChar &separator, bool *ok ) const
\obsolete
Reads the entry specified by \a key as a string. The \a separator
is used to create a TQStringList by calling TQStringList::split(\a
separator, entry). If \a ok is not 0: \a *ok is set to TRUE
if the key was read, otherwise \a *ok is set to FALSE.
\warning As the documentation states, TQStringList::split() will
omit empty strings from the list. Because of this, it is
impossible to retrieve identical list data with this function. We
recommend using the readListEntry() and writeEntry() overloads
that do not take a \a separator argument.
Note that if you want to iterate over the list, you should iterate
over a copy, e.g.
\code
TQStringList list = mySettings.readListEntry( "size", " " );
TQStringList::Iterator it = list.begin();
while( it != list.end() ) {
myProcessing( *it );
++it;
}
\endcode
\sa readEntry(), readDoubleEntry(), readBoolEntry(), writeEntry(), removeEntry(), TQStringList::split()
*/
/*!
\internal
*/
TQStringList TQSettings::readListEntry(const TQString &key, const TQChar &separator, bool *ok )
{
TQString value = readEntry( key, TQString::null, ok );
if ( ok && !*ok )
return TQStringList();
return TQStringList::split(separator, value);
}
/*!
\fn TQStringList TQSettings::readListEntry(const TQString &key, bool *ok ) const
Reads the entry specified by \a key as a string. If \a ok is not
0, \a *ok is set to TRUE if the key was read, otherwise \a *ok is
set to FALSE.
Note that if you want to iterate over the list, you should iterate
over a copy, e.g.
\code
TQStringList list = mySettings.readListEntry( "recentfiles" );
TQStringList::Iterator it = list.begin();
while( it != list.end() ) {
myProcessing( *it );
++it;
}
\endcode
\sa readEntry(), readDoubleEntry(), readBoolEntry(), writeEntry(), removeEntry(), TQStringList::split()
*/
/*!
\internal
*/
TQStringList TQSettings::readListEntry(const TQString &key, bool *ok )
{
TQString value = readEntry( key, TQString::null, ok );
if ( ok && !*ok )
return TQStringList();
TQStringList l;
TQString s;
bool esc=FALSE;
for (int i=0; i<(int)value.length(); i++) {
if ( esc ) {
if ( value[i] == 'e' ) { // end-of-string
l.append(s);
s="";
} else if ( value[i] == '0' ) { // null string
s=TQString::null;
} else {
s.append(value[i]);
}
esc=FALSE;
} else if ( value[i] == '^' ) {
esc = TRUE;
} else {
s.append(value[i]);
if ( i == (int)value.length()-1 )
l.append(s);
}
}
return l;
}
#ifdef Q_OS_MAC
void qt_setSettingsBasePath(const TQString &); //qsettings_mac.cpp
#endif
/*!
Insert platform-dependent paths from platform-independent information.
The \a domain should be an Internet domain name
controlled by the producer of the software, eg. Trolltech products
use "trolltech.com".
The \a product should be the official name of the product.
The \a scope should be
TQSettings::User for user-specific settings, or
TQSettings::Global for system-wide settings (generally
these will be read-only to many users).
Not all information is relevant on all systems.
*/
void TQSettings::setPath( const TQString &domain, const TQString &product, Scope scope )
{
// On Windows, any trailing ".com(\..*)" is stripped from the domain. The
// Global scope corresponds to HKEY_LOCAL_MACHINE, and User corresponds to
// HKEY_CURRENT_USER. Note that on some installations, not all users can
// write to the Global scope. On UNIX, any trailing ".com(\..*)" is stripped
// from the domain. The Global scope corresponds to "/opt" (this would be
// configurable at library build time - eg. to "/usr/local" or "/usr"),
// while the User scope corresponds to $HOME/.*rc.
// Note that on most installations, not all users can write to the System
// scope.
//
// On MacOS X, if there is no "." in domain, append ".com", then reverse the
// order of the elements (Mac OS uses "com.apple.finder" as domain+product).
// The Global scope corresponds to /Library/Preferences/*.plist, while the
// User scope corresponds to ~/Library/Preferences/*.plist.
// Note that on most installations, not all users can write to the System
// scope.
d->globalScope = scope == Global;
TQString actualSearchPath;
int lastDot = domain.findRev( '.' );
#if defined(Q_WS_WIN)
actualSearchPath = "/" + domain.mid( 0, lastDot ) + "/" + product;
insertSearchPath( Windows, actualSearchPath );
#elif !defined(TQWS) && defined(Q_OS_MAC)
if(lastDot != -1) {
TQString topLevelDomain = domain.right( domain.length() - lastDot - 1 ) + ".";
if ( !topLevelDomain.isEmpty() )
qt_setSettingsBasePath( topLevelDomain );
}
actualSearchPath = "/" + domain.left( lastDot ) + "." + product;
insertSearchPath( Mac, actualSearchPath );
#else
if (scope == User)
actualSearchPath = TQDir::homeDirPath() + "/.";
else
actualSearchPath = TQString(tqInstallPathSysconf()) + "/";
actualSearchPath += domain.mid( 0, lastDot ) + "/" + product;
insertSearchPath( Unix, actualSearchPath );
#endif
}
/*!
Appends \a group to the current key prefix.
\code
TQSettings settings;
settings.beginGroup( "/MainWindow" );
// read values
settings.endGroup();
\endcode
*/
void TQSettings::beginGroup( const TQString &group )
{
d->groupStack.push( group );
d->groupDirty = TRUE;
}
/*!
Undo previous calls to beginGroup(). Note that a single beginGroup("a/b/c") is undone
by a single call to endGroup().
\code
TQSettings settings;
settings.beginGroup( "/MainWindow/Geometry" );
// read values
settings.endGroup();
\endcode
*/
void TQSettings::endGroup()
{
d->groupStack.pop();
d->groupDirty = TRUE;
}
/*!
Set the current key prefix to the empty string.
*/
void TQSettings::resetGroup()
{
d->groupStack.clear();
d->groupDirty = FALSE;
d->groupPrefix = TQString::null;
}
/*!
Returns the current key prefix, or a null string if there is no key prefix set.
\sa beginGroup();
*/
TQString TQSettings::group() const
{
if ( d->groupDirty ) {
d->groupDirty = FALSE;
d->groupPrefix = TQString::null;
TQValueStack<TQString>::Iterator it = d->groupStack.begin();
while ( it != d->groupStack.end() ) {
TQString group = *it;
++it;
if ( group[0] != '/' )
group.prepend( "/" );
d->groupPrefix += group;
}
}
return d->groupPrefix;
}
#endif
|