summaryrefslogtreecommitdiffstats
path: root/src/systeminfo/qsysteminfo_maemo.cpp
blob: ce7c7271206c07e6dc6f26973301662f066e5562 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
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
/****************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the Qt Mobility Components.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the Technology Preview License Agreement accompanying
** this package.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**
**
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qsysteminfocommon_p.h"
#include "qsysteminfo_maemo_p.h"
#include <QStringList>
#include <QSize>
#include <QFile>
#include <QTextStream>
#include <QLocale>
#include <QLibraryInfo>
//#include <QtGui>
#include <QDesktopWidget>
#include <QDebug>
#include <QTimer>
#include <QDir>
#include <QTimer>
#include <QMapIterator>

#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/input.h>

#if !defined(Q_WS_MAEMO_6)
#if !defined(SW_KEYPAD_SLIDE)
#define SW_KEYPAD_SLIDE 0x0a
#endif
#endif

#define BITS_PER_LONG (sizeof(long) * 8)
#define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
#define OFF(x)  ((x)%BITS_PER_LONG)
#define BIT(x)  (1UL<<OFF(x))
#define LONG(x) ((x)/BITS_PER_LONG)
#define test_bit(bit, array)    ((array[LONG(bit)] >> OFF(bit)) & 1)

#if !defined(QT_NO_DBUS)
#include "linux/gconfitem_p.h" // Temporarily here.
#endif

#ifdef Q_WS_X11
#include <QX11Info>
#include <X11/Xlib.h>

#endif

#include <QDBusInterface>
static QString sysinfodValueForKey(const QString& key)
{
    QString value = "";
#if !defined(QT_NO_DBUS)
    QDBusInterface connectionInterface("com.nokia.SystemInfo",
                                       "/com/nokia/SystemInfo",
                                       "com.nokia.SystemInfo",
                                       QDBusConnection::systemBus());

    QDBusReply<QByteArray> reply = connectionInterface.call("GetConfigValue", key);
    if (reply.isValid()) {
        /*
         * sysinfod automatically terminates after some idle time (no D-Bus traffic).
         * Therefore, we cannot use isServiceRegistered() to determine if sysinfod is available.
         *
         * Thus, make a query to sysinfod and if we got back a valid reply, sysinfod
         * is available.
         */
        value = reply.value();
    }
#endif
    return value;
}

#if !defined(QT_NO_DBUS)
QDBusArgument &operator<<(QDBusArgument &argument, const ProfileDataValue &value)
{
  argument.beginStructure();
  argument << value.key << value.val << value.type;
  argument.endStructure();
  return argument;
}

const QDBusArgument &operator>>(const QDBusArgument &argument, ProfileDataValue &value)
{
  argument.beginStructure();
  argument >> value.key >> value.val >> value.type;
  argument.endStructure();
  return argument;
}
#endif

QTM_BEGIN_NAMESPACE

QSystemInfoPrivate::QSystemInfoPrivate(QSystemInfoLinuxCommonPrivate *parent)
 : QSystemInfoLinuxCommonPrivate(parent)
{
}

QSystemInfoPrivate::~QSystemInfoPrivate()
{
}

QStringList QSystemInfoPrivate::availableLanguages() const
{
    QStringList languages;

#if defined(Q_WS_MAEMO_6)
    QDir langDir("/etc/meego-supported-languages");
    languages = langDir.entryList(QStringList() <<"??",QDir::Files | QDir::NoDotAndDotDot, QDir::Name);
#else
    GConfItem languagesItem("/meegotouch/inputmethods/languages");
    const QStringList locales = languagesItem.value().toStringList();

    foreach(const QString &locale, locales) {
        languages << locale.mid(0,2);
    }
    languages << currentLanguage();
    languages.removeDuplicates();
#endif

    return languages;
}

QString QSystemInfoPrivate::currentLanguage() const
{
#if defined(Q_WS_MAEMO_6)
    GConfItem langItem("/meegotouch/i18n/language");
    QString lang = langItem.value().toString();
    if(lang.count() > 2) lang = lang.left(2);
    if (lang.isEmpty()) {
        lang = QString::fromLocal8Bit(qgetenv("LANG")).left(2);
    }
    return lang;
#else
    return QSystemInfoLinuxCommonPrivate::currentLanguage();
#endif
}


QString QSystemInfoPrivate::currentCountryCode() const
{
#if defined(Q_WS_MAEMO_6)
    GConfItem langItem("/meegotouch/i18n/region");
     QString langCC = langItem.value().toString().section("_",1,1);
     if (langCC.isEmpty()) {
         langCC = QString::fromLocal8Bit(qgetenv("LANG")).section("_",1,1);
         langCC = langCC.remove(".UTF-8",Qt::CaseSensitive);
         return langCC;
     }
#endif
    return QSystemInfoLinuxCommonPrivate::currentCountryCode();
}

QString QSystemInfoPrivate::version(QSystemInfo::Version type,const QString &parameter)
{
    QString errorStr = "Not Available";

    switch(type) {
    case QSystemInfo::Os :
    {
        QString sysinfodValue = sysinfodValueForKey("/device/sw-release-ver");//("/device/content-ver");
        if (!sysinfodValue.isEmpty()) {
           sysinfodValue =  sysinfodValue.section("_",2,4);
            return sysinfodValue;
        }
    }
        break;
    case QSystemInfo::Firmware :
    {
        QString sysinfodValue = sysinfodValueForKey("/device/sw-release-ver");
        if (!sysinfodValue.isEmpty()) {
            return sysinfodValue;
        }
    }

    default:
        return QSystemInfoLinuxCommonPrivate::version(type, parameter);
        break;
    };
    return errorStr;
}

bool QSystemInfoPrivate::hasFeatureSupported(QSystemInfo::Feature feature)
{
    bool featureSupported = false;
    switch (feature) {
    case QSystemInfo::SimFeature :
        {
            QSystemDeviceInfoPrivate d;
            featureSupported = (d.simStatus() != QSystemDeviceInfo::SimNotAvailable);
        }
        break;
    case QSystemInfo::LocationFeature :
        {
#if defined(Q_WS_MAEMO_6)
            GConfItem satellitePositioning("/system/osso/location/settings/satellitePositioning");
            GConfItem networkPositioning("/system/osso/location/settings/networkPositioning");

            bool satellitePositioningAvailable = satellitePositioning.value(false).toBool();
            bool networkPositioningAvailable   = networkPositioning.value(false).toBool();

            featureSupported = (satellitePositioningAvailable || networkPositioningAvailable);
#else /* Maemo 5 */
            GConfItem locationValues("/system/nokia/location");
            const QStringList locationKeys = locationValues.listEntries();
            if(locationKeys.count()) {
                featureSupported = true;
            }
#endif /* Maemo 5 */
        }
        break;
    case QSystemInfo::HapticsFeature:
        {
           // if(halIsAvailable) {
                QHalInterface iface;
                const QStringList touchSupport =
                        iface.findDeviceByCapability("input.touchpad");
                if(touchSupport.count()) {
                    featureSupported = true;
                } else {
                    featureSupported = false;
                }
            }
      //  }
        break;
    default:
        featureSupported = QSystemInfoLinuxCommonPrivate::hasFeatureSupported(feature);
        break;
    };
    return featureSupported;
}

#if defined(Q_WS_MAEMO_6)
QMap<QString, int> QSystemNetworkInfoPrivate::CellularServiceStatus;
#endif // Q_WS_MAEMO_6

QSystemNetworkInfoPrivate::QSystemNetworkInfoPrivate(QSystemNetworkInfoLinuxCommonPrivate *parent)
    : QSystemNetworkInfoLinuxCommonPrivate(parent)
    , currentBluetoothNetworkStatus(QSystemNetworkInfo::UndefinedStatus)
    , currentCellNetworkStatus(-1)
    , currentWlanNetworkStatus(QSystemNetworkInfo::UndefinedStatus)
    , currentNetworkMode(QSystemNetworkInfo::UnknownMode)
    , currentCellSignalStrength(-1)
    , currentEthernetSignalStrength(-1)
    , currentWlanSignalStrength(-1)
    , currentCellId(-1)
    , currentLac(-1)
    , radioAccessTechnology(0)
    , currentCellDataTechnology(QSystemNetworkInfo::UnknownDataTechnology)
    , wlanSignalStrengthTimer(0)
{
#if defined(Q_WS_MAEMO_6)
    if (CellularServiceStatus.isEmpty()) {
        CellularServiceStatus["Unknown"]    = -1;  // Current registration status is unknown.
        CellularServiceStatus["Home"]       = 0;   // Registered with the home network.
        CellularServiceStatus["Roaming"]    = 1;   // Registered with a roaming network.
        CellularServiceStatus["Offline"]    = 3;   // Not registered.
        CellularServiceStatus["Searching"]  = 4;   // Offline, but currently searching for network.
        CellularServiceStatus["NoSim"]      = 6;   // Offline because no SIM is present.
        CellularServiceStatus["PowerOff"]   = 8;   // Offline because the CS is powered off.
        CellularServiceStatus["PowerSave"]  = 9;   // Offline and in power save mode.
        CellularServiceStatus["NoCoverage"] = 10;  // Offline and in power save mode because of poor coverage.
        CellularServiceStatus["Rejected"]   = 11;  // Offline because SIM was rejected by the network.
    }
#endif // Q_WS_MAEMO_6

    setupNetworkInfo();
}

QSystemNetworkInfoPrivate::~QSystemNetworkInfoPrivate()
{
    delete wlanSignalStrengthTimer;
}

QSystemNetworkInfo::NetworkStatus QSystemNetworkInfoPrivate::networkStatus(QSystemNetworkInfo::NetworkMode mode)
{
    switch(mode) {
    case QSystemNetworkInfo::GsmMode:
    case QSystemNetworkInfo::CdmaMode:
    case QSystemNetworkInfo::WcdmaMode: {
        // radioAccessTechnology: 1 = GSM, 2 = WCDMA
        if ((radioAccessTechnology == 1 && mode != QSystemNetworkInfo::GsmMode)
            || (radioAccessTechnology == 2 && mode != QSystemNetworkInfo::WcdmaMode)) {
            return QSystemNetworkInfo::NoNetworkAvailable;
        }

#if defined(Q_WS_MAEMO_6)
        QVariant value = queryCsdProperties("com.nokia.csd.CSNet", "/com/nokia/csd/csnet", "com.nokia.csd.CSNet.NetworkRegistration").value("RegistrationStatus");
        if (value.isValid())
            currentCellNetworkStatus = CellularServiceStatus.value(value.toString(), -1);
#endif // Q_WS_MAEMO_6

        switch (currentCellNetworkStatus) {
        case  0: return QSystemNetworkInfo::HomeNetwork;        // CS is registered to home network
        case  1: return QSystemNetworkInfo::Roaming;            // CS is registered to some other network than home network
        case  2: return QSystemNetworkInfo::Roaming;            // CS is registered to non-home system in a non-home area
        case  3: return QSystemNetworkInfo::NoNetworkAvailable; // CS is not in service
        case  4: return QSystemNetworkInfo::Searching;          // CS is not in service, but is currently searching for service
        case  5: return QSystemNetworkInfo::NoNetworkAvailable; // CS is not in service and it is not currently searching for service
        case  6: return QSystemNetworkInfo::NoNetworkAvailable; // CS is not in service due to missing SIM or missing subscription
        case  8: return QSystemNetworkInfo::NoNetworkAvailable; // CS is in power off state
        case  9: return QSystemNetworkInfo::NoNetworkAvailable; // CS is in No Service Power Save State (currently not listening to any cell)
        case 10: return QSystemNetworkInfo::NoNetworkAvailable; // CS is in No Service Power Save State (CS is entered to this state
                                                                // because there is no network coverage)
        case 11: return QSystemNetworkInfo::Denied;             // CS is not in service due to missing subscription
        default:
            break;
        };
        break;
    }

    case QSystemNetworkInfo::EthernetMode:
        if (currentEthernetSignalStrength == -1)
            networkSignalStrength(mode);
        if (currentEthernetSignalStrength == 100)
            return QSystemNetworkInfo::Connected;
        else
            return QSystemNetworkInfo::NoNetworkAvailable;

    case QSystemNetworkInfo::WlanMode:
        currentWlanNetworkStatus = QSystemNetworkInfoLinuxCommonPrivate::networkStatus(mode);
        return currentWlanNetworkStatus;

    case QSystemNetworkInfo::BluetoothMode:
        currentBluetoothNetworkStatus = QSystemNetworkInfoLinuxCommonPrivate::networkStatus(mode);
        return currentBluetoothNetworkStatus;

//    case QSystemNetworkInfo::WimaxMode:
//    case QSystemNetworkInfo::LteMode:
    default:
        return QSystemNetworkInfoLinuxCommonPrivate::networkStatus(mode);
    };

    return QSystemNetworkInfo::UndefinedStatus;
}

int QSystemNetworkInfoPrivate::networkSignalStrength(QSystemNetworkInfo::NetworkMode mode)
{
    switch(mode) {
    case QSystemNetworkInfo::GsmMode:
    case QSystemNetworkInfo::CdmaMode:
    case QSystemNetworkInfo::WcdmaMode: {
        // radioAccessTechnology: 1 = GSM, 2 = WCDMA
        if ((radioAccessTechnology == 1 && mode != QSystemNetworkInfo::GsmMode)
            || (radioAccessTechnology == 2 && mode != QSystemNetworkInfo::WcdmaMode)) {
            return -1;
        }

#if !defined(QT_NO_DBUS)
#if defined(Q_WS_MAEMO_6)
        QVariant value = queryCsdProperties("com.nokia.csd.CSNet", "/com/nokia/csd/csnet", "com.nokia.csd.CSNet.SignalStrength").value("SignalPercent");
        if (value.isValid())
            currentCellSignalStrength = value.toInt();
#endif // Q_WS_MAEMO_6
#endif // QT_NO_DBUS

        return currentCellSignalStrength;
    }

    case QSystemNetworkInfo::EthernetMode:
        currentEthernetSignalStrength = QSystemNetworkInfoLinuxCommonPrivate::networkSignalStrength(mode);
        return currentEthernetSignalStrength;

    case QSystemNetworkInfo::WlanMode:
        currentWlanSignalStrength = QSystemNetworkInfoLinuxCommonPrivate::networkSignalStrength(mode);
        return currentWlanSignalStrength;

//    case QSystemNetworkInfo::BluetoothMode:
//    case QSystemNetworkInfo::WimaxMode:
//    case QSystemNetworkInfo::LteMode:
    default:
        return QSystemNetworkInfoLinuxCommonPrivate::networkSignalStrength(mode);
    };

    return -1;
}

int QSystemNetworkInfoPrivate::cellId()
{
#if !defined(QT_NO_DBUS)
#if defined(Q_WS_MAEMO_6)
    QVariant value = queryCsdProperties("com.nokia.csd.CSNet", "/com/nokia/csd/csnet", "com.nokia.csd.CSNet.NetworkCell").value("CellId");
    if (value.isValid())
        currentCellId = value.toInt();
#endif // Q_WS_MAEMO_6
#endif // QT_NO_DBUS

    return currentCellId;
}

int QSystemNetworkInfoPrivate::locationAreaCode()
{
#if !defined(QT_NO_DBUS)
#if defined(Q_WS_MAEMO_6)
    QVariant value = queryCsdProperties("com.nokia.csd.CSNet", "/com/nokia/csd/csnet", "com.nokia.csd.CSNet.NetworkCell").value("CellLac");
        if (value.isValid())
            currentLac = value.toInt();
#endif // Q_WS_MAEMO_6
#endif // QT_NO_DBUS

    return currentLac;
}

QString QSystemNetworkInfoPrivate::currentMobileCountryCode()
{
#if !defined(QT_NO_DBUS)
#if defined(Q_WS_MAEMO_6)
    QVariant value = queryCsdProperties("com.nokia.csd.CSNet", "/com/nokia/csd/csnet", "com.nokia.csd.CSNet.NetworkOperator").value("OperatorMCC");
    if (value.isValid())
        currentMCC = value.toString();
#endif // Q_WS_MAEMO_6
#endif // QT_NO_DBUS

    return currentMCC;
}

QString QSystemNetworkInfoPrivate::currentMobileNetworkCode()
{
#if !defined(QT_NO_DBUS)
#if defined(Q_WS_MAEMO_6)
    QVariant value = queryCsdProperties("com.nokia.csd.CSNet", "/com/nokia/csd/csnet", "com.nokia.csd.CSNet.NetworkOperator").value("OperatorMNC");
    if (value.isValid())
        currentMNC = value.toString();
#endif // Q_WS_MAEMO_6
#endif // QT_NO_DBUS

    return currentMNC;
}

QString QSystemNetworkInfoPrivate::homeMobileCountryCode()
{
    QSystemDeviceInfoPrivate d;
    QString imsi = d.imsi();
    if (imsi.length() >= 3)
        return imsi.left(3);

    return QString();
}

QString QSystemNetworkInfoPrivate::homeMobileNetworkCode()
{
#if !defined(QT_NO_DBUS)
#if defined(Q_WS_MAEMO_6)
    QDBusInterface connectionInterface("com.nokia.csd.SIM", "/com/nokia/csd/sim", "com.nokia.csd.SIM.Identity",
                                       QDBusConnection::systemBus(), this);
    QDBusMessage reply = connectionInterface.call(QLatin1String("GetHPLMN"));
    if (reply.errorName().isEmpty()) {
        QList<QVariant> args = reply.arguments();
        // The first attribute should be MCC and the 2nd one MNC
        if (args.size() == 2)
            return args.at(1).toString();
    }
#else // Q_WS_MAEMO_6
    QDBusInterface connectionInterface("com.nokia.phone.SIM",
                                       "/com/nokia/phone/SIM",
                                       "Phone.Sim",
                                       QDBusConnection::systemBus(), this);
    if (!connectionInterface.isValid()) {
        qDebug() << "interface not valid";
        return QString();
    }
    QDBusReply<QByteArray> reply = connectionInterface.call(QLatin1String("read_hplmn"));

    // The MNC and MCC are split into Hex numbers in the received byte array.
    // The MNC can be 2 or 3 digits long. If it is 2 digits long, it ends with 0xF.
    // The order of the Hex numbers in the reply is:
    // mcc2 mcc1 mnc3 mcc3 mnc2 mnc1
    QString homeMobileNetworkCode;
    if (reply.isValid()) {
        QString temp = reply.value().toHex();
        QString mnc1 = temp.right(1);
        temp.chop(1);
        QString mnc2 = temp.right(1);
        temp.chop(2);
        QString mnc3 = temp.right(1);
        if (mnc3 != "f")
            homeMobileNetworkCode.prepend(mnc3);
        homeMobileNetworkCode.prepend(mnc2);
        homeMobileNetworkCode.prepend(mnc1);
        return homeMobileNetworkCode;
    }
#endif // Q_WS_MAEMO_6
#endif // QT_NO_DBUS

    return QString();
}

QString QSystemNetworkInfoPrivate::networkName(QSystemNetworkInfo::NetworkMode mode)
{
    switch(mode) {
    case QSystemNetworkInfo::CdmaMode:
    case QSystemNetworkInfo::GsmMode:
    case QSystemNetworkInfo::WcdmaMode: {
        // radioAccessTechnology: 1 = GSM, 2 = WCDMA
        if ((radioAccessTechnology == 1 && mode != QSystemNetworkInfo::GsmMode)
            || (radioAccessTechnology == 2 && mode != QSystemNetworkInfo::WcdmaMode)) {
            break;
        }

#if !defined(QT_NO_DBUS)
#if defined(Q_WS_MAEMO_6)
        QVariant value = queryCsdProperties("com.nokia.csd.CSNet", "/com/nokia/csd/csnet", "com.nokia.csd.CSNet.NetworkOperator").value("OperatorName");
        if (value.isValid())
            currentOperatorName = value.toString();
#endif // Q_WS_MAEMO_6
#endif // QT_NO_DBUS

        return currentOperatorName;
    }

//    case QSystemNetworkInfo::WlanMode:
//    case QSystemNetworkInfo::EthernetMode:
//    case QSystemNetworkInfo::BluetoothMode:
//    case QSystemNetworkInfo::WimaxMode:
//    case QSystemNetworkInfo::LteMode:
    default:
        return QSystemNetworkInfoLinuxCommonPrivate::networkName(mode);
    };

    return QString();
}

#if !defined(QT_NO_DBUS)
#if defined(Q_WS_MAEMO_6)
QMap<QString, QVariant> QSystemNetworkInfoPrivate::queryCsdProperties(const QString &service, const QString &path, const QString &interface)
{
    QMap<QString, QVariant> properties;

    QDBusMessage message = QDBusMessage::createMethodCall(service, path,
                                                          "org.freedesktop.DBus.Properties", "GetAll");
    message << interface;

    QDBusReply<QMap<QString, QVariant> > reply = QDBusConnection::systemBus().call(message);
    if (reply.isValid())
        properties = reply.value();

    return properties;
}
#endif // Q_WS_MAEMO_6
#endif // QT_NO_DBUS

void QSystemNetworkInfoPrivate::setupNetworkInfo()
{
#if !defined(QT_NO_DBUS)
#if defined(Q_WS_MAEMO_6)
    const QString service("com.nokia.csd.CSNet");
    const QString path("/com/nokia/csd/csnet");

    // CSD: network cell
    QMap<QString, QVariant> properties = queryCsdProperties(service, path, "com.nokia.csd.CSNet.NetworkCell");

    QVariant value = properties.value("CellType");
    if (value.isValid()) {
        QString currentCellType = value.toString();
        if (currentCellType == "GSM")
            radioAccessTechnology = 1;
        else if (currentCellType == "WCDMA")
            radioAccessTechnology = 2;
    }
#else // Q_WS_MAEMO_6
    QDBusConnection systemDbusConnection = QDBusConnection::systemBus();
    iWlanStrengthCheckEnabled = 0;

    QDBusInterface connectionInterface("com.nokia.phone.net",
                                       "/com/nokia/phone/net",
                                       "Phone.Net",
                                       systemDbusConnection);
    if (!connectionInterface.isValid()) {
        qDebug() << "setupNetworkInfo(): interface not valid";
        return;
    }

    QDBusMessage reply = connectionInterface.call(QLatin1String("get_registration_status"));
    if (reply.type() == QDBusMessage::ReplyMessage) {
        QList<QVariant> argList = reply.arguments();
        currentCellNetworkStatus = argList.at(STATUS_INDEX).toInt();
        currentLac = argList.at(LAC_INDEX).value<ushort>();
        currentCellId = argList.at(CELLID_INDEX).value<uint>();
        currentMCC.setNum(argList.at(MCC_INDEX).value<uint>());
        currentMNC.setNum(argList.at(MNC_INDEX).value<uint>());
    } else {
        qDebug() << reply.errorMessage();
    }

    if (!systemDbusConnection.connect("com.nokia.phone.net",
                                      "/com/nokia/phone/net",
                                      "Phone.Net",
                                      "registration_status_change",
                                      this, SLOT(registrationStatusChanged(uchar,ushort,uint,uint,uint,uchar,uchar)))) {
        qDebug() << "unable to connect to registration_status_change";
    }

    reply = connectionInterface.call(QLatin1String("get_signal_strength"));
    if (reply.type() == QDBusMessage::ReplyMessage) {
        QList<QVariant> argList = reply.arguments();
        currentCellSignalStrength = argList.at(0).toInt();
    } else {
        qDebug() << reply.errorMessage();
    }

    if (!systemDbusConnection.connect("com.nokia.phone.net",
                                      "/com/nokia/phone/net",
                                      "Phone.Net",
                                      "signal_strength_change",
                                      this, SLOT(cellNetworkSignalStrengthChanged(uchar,uchar)))) {
        qDebug() << "unable to connect to signal_strength_change";
    }

    uchar type = 0;
    QList<QVariant> argumentList;
    argumentList << qVariantFromValue(type) << qVariantFromValue(currentMNC.toUInt()) << qVariantFromValue(currentMCC.toUInt());

    reply = connectionInterface.callWithArgumentList(QDBus::Block, QLatin1String("get_operator_name"), argumentList);
    if (reply.type() == QDBusMessage::ReplyMessage) {
        QList<QVariant> argList = reply.arguments();
        currentOperatorName = argList.at(0).toString();
    } else {
        qDebug() << reply.errorMessage();
    }

    if (!systemDbusConnection.connect("com.nokia.phone.net",
                                      "/com/nokia/phone/net",
                                      "Phone.Net",
                                      "operator_name_change",
                                      this, SLOT(operatorNameChanged(uchar,QString,QString,uint,uint)))) {
        qDebug() << "unable to connect to operator_name_change";
    }

    reply = connectionInterface.call(QLatin1String("get_radio_access_technology"));
    if (reply.type() == QDBusMessage::ReplyMessage) {
        QList<QVariant> argList = reply.arguments();
        radioAccessTechnology = argList.at(0).toInt();
    } else {
        qDebug() << reply.errorMessage();
    }

    if (!systemDbusConnection.connect("com.nokia.phone.net",
                                      "/com/nokia/phone/net",
                                      "Phone.Net",
                                      "radio_access_technology_change",
                                      this, SLOT(networkModeChanged(int)))) {
        qDebug() << "unable to connect to radio_access_technology_change";
    }

    // TODO optimize the performance by enabling lazy loading (already done for M6)
    // here I just get the signal strength so we can handle the changes
    currentEthernetSignalStrength = networkSignalStrength(QSystemNetworkInfo::EthernetMode);
    currentBluetoothNetworkStatus = networkStatus(QSystemNetworkInfo::BluetoothMode);
    currentWlanNetworkStatus = networkStatus(QSystemNetworkInfo::WlanMode);
    currentWlanSignalStrength = networkSignalStrength(QSystemNetworkInfo::WlanMode);
    currentNetworkMode = currentMode();
    wlanSignalStrengthTimer = new QTimer(this);
    connect(wlanSignalStrengthTimer, SIGNAL(timeout()), this, SLOT(checkWlanSignalStrength()));

    if (!systemDbusConnection.connect("com.nokia.bme",
                                      "/com/nokia/bme/signal",
                                      "com.nokia.bme.signal",
                                      QLatin1String("charger_connected"),
                                      this, SLOT(updateUsbCableStatus()))) {
        qDebug() << "unable to connect to updateUsbCableStatus (connect)";
    }

    if (!systemDbusConnection.connect("com.nokia.bme",
                                      "/com/nokia/bme/signal",
                                      "com.nokia.bme.signal",
                                      QLatin1String("charger_disconnected"),
                                      this, SLOT(updateUsbCableStatus()))) {
        qDebug() << "unable to connect to updateUsbCableStatus (disconnect)";
    }

    if (!systemDbusConnection.connect("org.freedesktop.Hal",
                                      "/org/freedesktop/Hal/Manager",
                                      "org.freedesktop.Hal.Manager",
                                      QLatin1String("DeviceAdded"),
                                      this, SLOT(updateAttachedDevices(QString)))) {
        qDebug() << "unable to connect to updateAttachedDevices (1)";
    }

    if (!systemDbusConnection.connect("org.freedesktop.Hal",
                                      "/org/freedesktop/Hal/Manager",
                                      "org.freedesktop.Hal.Manager",
                                      QLatin1String("DeviceRemoved"),
                                      this, SLOT(updateAttachedDevices(QString)))) {
        qDebug() << "unable to connect to updateAttachedDevices (2)";
    }

    if (!systemDbusConnection.connect("com.nokia.icd",
                                      "/com/nokia/icd",
                                      "com.nokia.icd",
                                      QLatin1String("status_changed"),
                                      this, SLOT(icdStatusChanged(QString,QString,QString,QString)))) {
        qDebug() << "unable to connect to icdStatusChanged";
    }
#endif // Q_WS_MAEMO_6
#endif // QT_NO_DBUS
}

#if !defined(QT_NO_DBUS)
#if defined(Q_WS_MAEMO_6)
void QSystemNetworkInfoPrivate::connectNotify(const char *signal)
{
    const QString service("com.nokia.csd.CSNet");
    const QString path("/com/nokia/csd/csnet");

    if ((QLatin1String(signal) == SIGNAL(networkSignalStrengthChanged(QSystemNetworkInfo::NetworkMode,int)))) {
        networkSignalStrength(QSystemNetworkInfo::GsmMode);
        networkSignalStrength(QSystemNetworkInfo::EthernetMode);
        networkSignalStrength(QSystemNetworkInfo::WlanMode);
        wlanSignalStrengthTimer = new QTimer(this);
        wlanSignalStrengthTimer->start(5000);
        connect(wlanSignalStrengthTimer, SIGNAL(timeout()), this, SLOT(checkWlanSignalStrength()));

        if (!QDBusConnection::systemBus().connect(service, path, "com.nokia.csd.CSNet.SignalStrength", "SignalStrengthChanged",
                                                  this, SLOT(slotSignalStrengthChanged(int,int)))) {
            qDebug() << "unable to connect SignalStrengthChanged";
        }
    } else if ((QLatin1String(signal) == SIGNAL(currentMobileCountryCodeChanged(QString)))
               || (QLatin1String(signal) == SIGNAL(currentMobileNetworkCodeChanged(QString)))) {
        currentMobileCountryCode();

        if (!QDBusConnection::systemBus().connect(service, path, "com.nokia.csd.CSNet.NetworkOperator", "OperatorChanged",
                                                  this, SLOT(slotOperatorChanged(QString,QString)))) {
            qDebug() << "unable to connect (OperatorChanged";
        }
    } else if ((QLatin1String(signal) == SIGNAL(networkNameChanged(QSystemNetworkInfo::NetworkMode,QString)))) {
        networkName(QSystemNetworkInfo::GsmMode);

        if (!QDBusConnection::systemBus().connect(service, path, "com.nokia.csd.CSNet.NetworkOperator", "OperatorNameChanged",
                                                  this, SLOT(slotOperatorNameChanged(QString)))) {
            qDebug() << "unable to connect OperatorNameChanged";
        }
    } else if ((QLatin1String(signal) == SIGNAL(networkStatusChanged(QSystemNetworkInfo::NetworkMode,QSystemNetworkInfo::NetworkStatus)))) {
        networkSignalStrength(QSystemNetworkInfo::EthernetMode);
        networkStatus(QSystemNetworkInfo::BluetoothMode);
        networkStatus(QSystemNetworkInfo::GsmMode);
        networkStatus(QSystemNetworkInfo::WlanMode);
        currentMode();

        if (!QDBusConnection::systemBus().connect("com.nokia.icd", "/com/nokia/icd", "com.nokia.icd", QLatin1String("status_changed"),
                                                  this, SLOT(icdStatusChanged(QString,QString,QString,QString)))) {
            qDebug() << "unable to connect to icdStatusChanged";
        }

        if (!QDBusConnection::systemBus().connect("org.freedesktop.Hal", "/org/freedesktop/Hal/Manager", "org.freedesktop.Hal.Manager", QLatin1String("DeviceAdded"),
                                                  this, SLOT(updateAttachedDevices(QString)))) {
            qDebug() << "unable to connect to updateAttachedDevices (1)";
        }

        if (!QDBusConnection::systemBus().connect("org.freedesktop.Hal", "/org/freedesktop/Hal/Manager", "org.freedesktop.Hal.Manager", QLatin1String("DeviceRemoved"),
                                                  this, SLOT(updateAttachedDevices(QString)))) {
            qDebug() << "unable to connect to updateAttachedDevices (2)";
        }

        if (!QDBusConnection::systemBus().connect(service, path, "com.nokia.csd.CSNet.NetworkRegistration", "RegistrationChanged",
                                                  this, SLOT(slotRegistrationChanged(QString)))) {
            qDebug() << "unable to connect RegistrationChanged";
        }
    } else if ((QLatin1String(signal) == SIGNAL(cellIdChanged(int)))) {
        currentMode();
        cellId();

        if (!QDBusConnection::systemBus().connect(service, path, "com.nokia.csd.CSNet.NetworkCell", "CellChanged",
                                                  this, SLOT(slotCellChanged(QString,int,int)))) {
            qDebug() << "unable to connect CellChanged";
        }
    } else if ((QLatin1String(signal) == SIGNAL(cellDataTechnologyChanged(QSystemNetworkInfo::CellDataTechnology)))) {
        cellDataTechnology();

        if (!QDBusConnection::systemBus().connect(service, path, "com.nokia.csd.CSNet.RadioAccess", "DataTechnologyChanged",
                                                  this, SLOT(slotCellDataTechnologyChanged(QString)))) {
            qDebug() << "unable to connect DataTechnologyChanged";
        }
    }
}

void QSystemNetworkInfoPrivate::disconnectNotify(const char *signal)
{
    const QString service("com.nokia.csd.CSNet");
    const QString path("/com/nokia/csd/csnet");

    if ((QLatin1String(signal) == SIGNAL(networkSignalStrengthChanged(QSystemNetworkInfo::NetworkMode,int)))) {
        wlanSignalStrengthTimer->stop();
        if (!QDBusConnection::systemBus().disconnect(service, path, "com.nokia.csd.CSNet.SignalStrength", "SignalStrengthChanged",
                                                     this, SLOT(slotSignalStrengthChanged(int,int)))) {
            qDebug() << "unable to disconnect SignalStrengthChanged";
        }
    } else if ((QLatin1String(signal) == SIGNAL(currentMobileCountryCodeChanged(QString)))
            || (QLatin1String(signal) == SIGNAL(currentMobileNetworkCodeChanged(QString)))) {
        if (!QDBusConnection::systemBus().disconnect(service, path, "com.nokia.csd.CSNet.NetworkOperator", "OperatorChanged",
                                                     this, SLOT(slotOperatorChanged(QString,QString)))) {
            qDebug() << "unable to disconnect (OperatorChanged";
        }
    } else if ((QLatin1String(signal) == SIGNAL(networkNameChanged(QSystemNetworkInfo::NetworkMode,QString)))) {
        if (!QDBusConnection::systemBus().disconnect(service, path, "com.nokia.csd.CSNet.NetworkOperator", "OperatorNameChanged",
                                                     this, SLOT(slotOperatorNameChanged(QString)))) {
            qDebug() << "unable to disconnect OperatorNameChanged";
        }
    } else if ((QLatin1String(signal) == SIGNAL(networkStatusChanged(QSystemNetworkInfo::NetworkMode,QSystemNetworkInfo::NetworkStatus)))) {
        if (!QDBusConnection::systemBus().disconnect(service, path, "com.nokia.csd.CSNet.NetworkRegistration", "RegistrationChanged",
                                                     this, SLOT(slotRegistrationChanged(QString)))) {
            qDebug() << "unable to disconnect RegistrationChanged";
        }
    } else if ((QLatin1String(signal) == SIGNAL(cellIdChanged(int)))) {
        if (!QDBusConnection::systemBus().disconnect(service, path, "com.nokia.csd.CSNet.NetworkCell", "CellChanged",
                                                     this, SLOT(slotCellChanged(QString,int,int)))) {
            qDebug() << "unable to disconnect CellChanged";
        }
    } else if ((QLatin1String(signal) == SIGNAL(cellDataTechnologyChanged(QSystemNetworkInfo::CellDataTechnology)))) {
        if (!QDBusConnection::systemBus().disconnect(service, path, "com.nokia.csd.CSNet", "ActivityChanged",
                                          this, SLOT(slotCellDataTechnologyChanged(QString)))) {
            qDebug() << "unable to disconnect DataTechnologyChanged";
        }
    }
}

void QSystemNetworkInfoPrivate::slotSignalStrengthChanged(int signalStrength, int /*dbm*/)
{
    currentCellSignalStrength = signalStrength;

    if (radioAccessTechnology == 1)
        Q_EMIT networkSignalStrengthChanged(QSystemNetworkInfo::GsmMode, currentCellSignalStrength);
    else if (radioAccessTechnology == 2)
        Q_EMIT networkSignalStrengthChanged(QSystemNetworkInfo::WcdmaMode, currentCellSignalStrength);
}

void QSystemNetworkInfoPrivate::slotOperatorChanged(const QString &mnc, const QString &mcc)
{
    if (currentMCC != mcc) {
        currentMCC = mcc;
        Q_EMIT currentMobileCountryCodeChanged(currentMCC);
    }

    if (currentMNC != mnc) {
        currentMNC = mnc;
        Q_EMIT currentMobileNetworkCodeChanged(currentMNC);
    }
}

void QSystemNetworkInfoPrivate::slotOperatorNameChanged(const QString &name)
{
    currentOperatorName = name;

    if (radioAccessTechnology == 1)
        Q_EMIT networkNameChanged(QSystemNetworkInfo::GsmMode, currentOperatorName);
    else if (radioAccessTechnology == 2)
        Q_EMIT networkNameChanged(QSystemNetworkInfo::WcdmaMode, currentOperatorName);
}

void QSystemNetworkInfoPrivate::slotRegistrationChanged(const QString &status)
{
    int newCellNetworkStatus = CellularServiceStatus.value(status, -1);

    if (currentCellNetworkStatus != newCellNetworkStatus) {
        currentCellNetworkStatus = newCellNetworkStatus;
        if (radioAccessTechnology == 1)
            Q_EMIT networkStatusChanged(QSystemNetworkInfo::GsmMode, networkStatus(QSystemNetworkInfo::GsmMode));
        else if (radioAccessTechnology == 2)
            Q_EMIT networkStatusChanged(QSystemNetworkInfo::WcdmaMode, networkStatus(QSystemNetworkInfo::WcdmaMode));
    }
}

void QSystemNetworkInfoPrivate::slotCellChanged(const QString &type, int id, int lac)
{
    int newRadioAccessTechnology = 0;
    if (type == "GSM")
        newRadioAccessTechnology = 1;
    else if (type == "WCDMA")
        newRadioAccessTechnology = 2;

    if (radioAccessTechnology != newRadioAccessTechnology) {
        radioAccessTechnology = newRadioAccessTechnology;
        checkNetworkMode();
    }

    if (currentCellId != id) {
        currentCellId = id;
        Q_EMIT cellIdChanged(currentCellId);
    }

    if (currentLac != lac) {
        currentLac = lac;
    }
}

void QSystemNetworkInfoPrivate::slotCellDataTechnologyChanged(const QString &tech)
{
    // TODO don't call cellDataTechnology() here
    Q_UNUSED(tech);

    QSystemNetworkInfo::CellDataTechnology oldTech = currentCellDataTechnology;

    if (oldTech != cellDataTechnology())
        Q_EMIT cellDataTechnologyChanged(currentCellDataTechnology);
}
#endif // Q_WS_MAEMO_6
#endif // QT_NO_DBUS

#if defined(Q_WS_MAEMO_5)
void QSystemNetworkInfoPrivate::cellNetworkSignalStrengthChanged(uchar var1, uchar)
{
    currentCellSignalStrength = var1;

    if (radioAccessTechnology == 1)
        Q_EMIT networkSignalStrengthChanged(QSystemNetworkInfo::GsmMode, currentCellSignalStrength);
    else if (radioAccessTechnology == 2)
        Q_EMIT networkSignalStrengthChanged(QSystemNetworkInfo::WcdmaMode, currentCellSignalStrength);
}

void QSystemNetworkInfoPrivate::networkModeChanged(int newRadioAccessTechnology)
{
    radioAccessTechnology = newRadioAccessTechnology;
    checkNetworkMode();
}

void QSystemNetworkInfoPrivate::operatorNameChanged(uchar, QString name, QString, uint, uint)
{
    currentOperatorName = name;

    if (radioAccessTechnology == 1)
        Q_EMIT networkNameChanged(QSystemNetworkInfo::GsmMode, currentOperatorName);
    else if (radioAccessTechnology == 2)
        Q_EMIT networkNameChanged(QSystemNetworkInfo::WcdmaMode, currentOperatorName);
}

void QSystemNetworkInfoPrivate::registrationStatusChanged(uchar var1, ushort var2, uint var3, uint var4, uint var5, uchar, uchar)
{
    int newCellNetworkStatus = var1;
    int newLac = var2;
    int newCellId = var3;
    QString newMobileCountryCode;
    QString newMobileNetworkCode;
    newMobileCountryCode.setNum(var5);
    newMobileNetworkCode.setNum(var4);

    if (currentCellNetworkStatus != newCellNetworkStatus) {
        currentCellNetworkStatus = newCellNetworkStatus;
        if (radioAccessTechnology == 1)
            Q_EMIT networkStatusChanged(QSystemNetworkInfo::GsmMode, networkStatus(QSystemNetworkInfo::GsmMode));
        else if (radioAccessTechnology == 2)
            Q_EMIT networkStatusChanged(QSystemNetworkInfo::WcdmaMode, networkStatus(QSystemNetworkInfo::WcdmaMode));
    }

    if (currentLac != newLac)
        currentLac = newLac;

    if (currentCellId != newCellId) {
        currentCellId = newCellId;
        Q_EMIT cellIdChanged(newCellId);
    }

    if (currentMCC != newMobileCountryCode) {
        currentMCC = newMobileCountryCode;
        Q_EMIT currentMobileCountryCodeChanged(currentMCC);
    }

    if (currentMNC != newMobileNetworkCode) {
        currentMNC = newMobileNetworkCode;
        Q_EMIT currentMobileNetworkCodeChanged(currentMNC);
    }
}
#endif // Q_WS_MAEMO_5

void QSystemNetworkInfoPrivate::icdStatusChanged(QString, QString var2, QString, QString)
{
    if (var2 == "WLAN_INFRA") {
        // TODO don't call networkStatus() here
        QSystemNetworkInfo::NetworkStatus oldWlanNetworkStatus = currentWlanNetworkStatus;
        networkStatus(QSystemNetworkInfo::WlanMode);

        if (currentWlanNetworkStatus != oldWlanNetworkStatus) {
            Q_EMIT networkStatusChanged(QSystemNetworkInfo::WlanMode, currentWlanNetworkStatus);
            checkNetworkMode();
        }
    }
}

void QSystemNetworkInfoPrivate::updateUsbCableStatus()
{
    // TODO don't call networkSignalStrength() here
    int oldEthernetSignalStrength = currentEthernetSignalStrength;
    networkSignalStrength(QSystemNetworkInfo::EthernetMode);

    if (currentEthernetSignalStrength != oldEthernetSignalStrength) {
        Q_EMIT networkSignalStrengthChanged(QSystemNetworkInfo::EthernetMode, currentEthernetSignalStrength);
        Q_EMIT networkStatusChanged(QSystemNetworkInfo::EthernetMode, networkStatus(QSystemNetworkInfo::EthernetMode));
        checkNetworkMode();
    }
}

QSystemNetworkInfo::NetworkMode QSystemNetworkInfoPrivate::currentMode()
{
    if (networkStatus(QSystemNetworkInfo::EthernetMode) == QSystemNetworkInfo::Connected)
        currentNetworkMode = QSystemNetworkInfo::EthernetMode;
    else if (networkStatus(QSystemNetworkInfo::WlanMode) == QSystemNetworkInfo::Connected)
        currentNetworkMode = QSystemNetworkInfo::WlanMode;
    else if (radioAccessTechnology == 1)
        currentNetworkMode = QSystemNetworkInfo::GsmMode;
    else if (radioAccessTechnology == 2)
        currentNetworkMode = QSystemNetworkInfo::WcdmaMode;

    return currentNetworkMode;
}

void QSystemNetworkInfoPrivate::checkNetworkMode()
{
    QSystemNetworkInfo::NetworkMode oldNetworkMode = currentNetworkMode;
    currentMode();

    if (currentNetworkMode != oldNetworkMode)
        Q_EMIT networkModeChanged(currentNetworkMode);
}

void QSystemNetworkInfoPrivate::checkWlanSignalStrength()
{
    int oldWlanSignalStrength = currentWlanSignalStrength;
    networkSignalStrength(QSystemNetworkInfo::WlanMode);

    if (currentWlanSignalStrength != oldWlanSignalStrength)
        Q_EMIT networkSignalStrengthChanged(QSystemNetworkInfo::WlanMode, currentWlanSignalStrength);
}

void QSystemNetworkInfoPrivate::updateAttachedDevices(QString device)
{
    if (device == "/org/freedesktop/Hal/devices/net_1b") {
        QTimer::singleShot(500, this, SLOT(updateUsbCableStatus()));
    } else {
        QSystemNetworkInfo::NetworkStatus oldBluetoothNetworkStatus = currentBluetoothNetworkStatus;
        networkStatus(QSystemNetworkInfo::BluetoothMode);

        if (currentBluetoothNetworkStatus != oldBluetoothNetworkStatus)
            Q_EMIT networkStatusChanged(QSystemNetworkInfo::BluetoothMode, currentBluetoothNetworkStatus);
    }
}

#if defined(Q_WS_MAEMO_5)
void QSystemNetworkInfoPrivate::setWlanSignalStrengthCheckEnabled(bool enabled)
{
    if (enabled) {
        iWlanStrengthCheckEnabled++;
        if (!wlanSignalStrengthTimer->isActive())
            wlanSignalStrengthTimer->start(5000); //5 seconds interval
    } else {
        iWlanStrengthCheckEnabled--;
        if (iWlanStrengthCheckEnabled <= 0) {
            if (wlanSignalStrengthTimer->isActive())
                wlanSignalStrengthTimer->stop();
        }
    }
}
#endif // Q_WS_MAEMO_5

inline QSystemNetworkInfo::CellDataTechnology QSystemNetworkInfoPrivate::csdtToCellDataTechnology(const QString &tech)
{
    QSystemNetworkInfo::CellDataTechnology cdt = QSystemNetworkInfo::UnknownDataTechnology;
    if (tech == "GPRS")
        cdt = QSystemNetworkInfo::GprsDataTechnology;
    else if (tech == "EGPRS")
        cdt = QSystemNetworkInfo::EdgeDataTechnology;
    else if (tech == "UMTS")
        cdt = QSystemNetworkInfo::UmtsDataTechnology;
    else if (tech == "HSPA")
        cdt = QSystemNetworkInfo::HspaDataTechnology;
    return cdt;
}

QSystemNetworkInfo::CellDataTechnology QSystemNetworkInfoPrivate::cellDataTechnology()
{
#if !defined(QT_NO_DBUS)
    const QString service("com.nokia.csd.CSNet");
    const QString path("/com/nokia/csd/csnet");

    QDBusInterface csnetInterface(service, path, "com.nokia.csd.CSNet", QDBusConnection::systemBus());
    QVariant dataTechnology;
    QDBusInterface radioAccessInterface(service, path, "com.nokia.csd.CSNet.RadioAccess", QDBusConnection::systemBus());
    if (csnetInterface.property("Activity").toString() == "PacketData")
        dataTechnology = radioAccessInterface.property("DataTechnology");
    else
        dataTechnology = radioAccessInterface.property("Technology");

    if (dataTechnology.isValid())
        currentCellDataTechnology = csdtToCellDataTechnology(dataTechnology.toString());

    return currentCellDataTechnology;
#endif // QT_NO_DBUS
}

QSystemDisplayInfoPrivate::QSystemDisplayInfoPrivate(QSystemDisplayInfoLinuxCommonPrivate *parent)
        : QSystemDisplayInfoLinuxCommonPrivate(parent)
{
}

QSystemDisplayInfoPrivate::~QSystemDisplayInfoPrivate()
{
}

int QSystemDisplayInfoPrivate::displayBrightness(int screen)
{
    QDesktopWidget wid;
    if(wid.screenCount() - 1 < screen) {
        return -1;
    }
    GConfItem currentBrightness("/system/osso/dsm/display/display_brightness");
    GConfItem maxBrightness("/system/osso/dsm/display/max_display_brightness_levels");
    if(maxBrightness.value().toInt()) {
        float retVal = 100 * (currentBrightness.value().toFloat() /
                              maxBrightness.value().toFloat());
        return retVal;
    }

    return -1;
}

float QSystemDisplayInfoPrivate::contrast(int screen)
{
    Q_UNUSED(screen);

    return 0.0;
}

QSystemDisplayInfo::BacklightState QSystemDisplayInfoPrivate::backlightStatus(int screen)
{
    Q_UNUSED(screen)
    QSystemDisplayInfo::BacklightState backlightState = QSystemDisplayInfo::BacklightStateUnknown;

#if !defined(QT_NO_DBUS)
    QDBusReply<QString> reply = QDBusConnection::systemBus().call(
                                    QDBusMessage::createMethodCall("com.nokia.mce", "/com/nokia/mce/request",
                                                                   "com.nokia.mce.request", "get_display_status"));
    if (reply.isValid()) {
        QString displayStatus = reply.value();
        if (displayStatus == "off") {
            backlightState = QSystemDisplayInfo::BacklightStateOff;
        } else if (displayStatus == "dimmed") {
            backlightState = QSystemDisplayInfo::BacklightStateDimmed;
        } else if (displayStatus == "on") {
            backlightState = QSystemDisplayInfo::BacklightStateOn;
        }
    }
#endif
    return backlightState;
}

QSystemDeviceInfoPrivate::QSystemDeviceInfoPrivate(QSystemDeviceInfoLinuxCommonPrivate *parent)
        : QSystemDeviceInfoLinuxCommonPrivate(parent), gpioFD(-1)
{
    previousPowerState = QSystemDeviceInfo::UnknownPower;

    m_profileName                    = "";
    m_flightMode                     = false;
    m_silentProfile                  = false;
    m_vibratingAlertEnabled          = false;
    m_beepProfile                    = false;
    m_ringingAlertVolume             = 0;
    m_smsAlertVolume                 = 0;

    m_flightModeQueried              = false;
    m_profileNameQueried             = false;
    m_ringingAlertTypeQueried        = false;
    m_vibratingAlertEnabledQueried   = false;
    m_ringingAlertVolumeQueried      = false;
    m_smsAlertVolumeQueried          = false;

    m_profileDataMetaTypesRegistered = false;
}

QSystemDeviceInfoPrivate::~QSystemDeviceInfoPrivate()
{
    if (gpioFD == -1) {
        ::close(gpioFD);
        gpioFD = -1;
    }
}

void QSystemDeviceInfoPrivate::registerProfileDataMetaTypes()
{
    if (m_profileDataMetaTypesRegistered) {
        return;
    }

#if !defined(QT_NO_DBUS)
    qDBusRegisterMetaType<ProfileDataValue>();
    qDBusRegisterMetaType<QList<ProfileDataValue> >();
#endif

    m_profileDataMetaTypesRegistered = true;
}

void QSystemDeviceInfoPrivate::connectNotify(const char *signal)
{
    if (QLatin1String(signal) == QLatin1String(QMetaObject::normalizedSignature(SIGNAL(lockStatusChanged(QSystemDeviceInfo::LockTypeFlags))))) {
        QDBusConnection::systemBus().connect("com.nokia.mce",
                                             "/com/nokia/mce/signal",
                                             "com.nokia.mce.signal",
                                             "tklock_mode_ind",
                                             this, SLOT(touchAndKeyboardStateChanged(const QString&)));
        QDBusConnection::systemBus().connect("com.nokia.devicelock",
                                             "/request",
                                             "com.nokia.devicelock",
                                             "stateChanged",
                                             this, SLOT(deviceStateChanged(int,int)));
    }
    if (QLatin1String(signal) == QLatin1String(QMetaObject::normalizedSignature(SIGNAL(currentProfileChanged(QSystemDeviceInfo::Profile))))) {
        registerProfileDataMetaTypes();

        if (! QDBusConnection::systemBus().connect("com.nokia.mce",
                               "/com/nokia/mce/signal",
                               "com.nokia.mce.signal",
                               "sig_device_mode_ind",
                               this, SLOT(deviceModeChanged(QString)))) {
            qDebug() << "unable to connect to sig_device_mode_ind";
        }
        if (!QDBusConnection::sessionBus().connect("com.nokia.profiled",
                               "/com/nokia/profiled",
                               "com.nokia.profiled",
                               "profile_changed",
                               this, SLOT(profileChanged(bool, bool, QString, QList<ProfileDataValue>)))) {
            qDebug() << "unable to connect to profile_changed";
        }
    }
    if (QLatin1String(signal) == QLatin1String(QMetaObject::normalizedSignature(SIGNAL(keyboardFlipped(bool))))) {
        if (gpioFD == -1) {
            gpioFD = ::open("/dev/input/gpio-keys", O_RDONLY | O_NONBLOCK);
        }

        if (gpioFD != -1) {
            notifier = new QSocketNotifier(gpioFD, QSocketNotifier::Read);
            connect(notifier, SIGNAL(activated(int)), this, SLOT(socketActivated(int)));
        } else {
            qDebug() << "Could not open gpiokeys";
            notifier = 0;
        }
    }
    QSystemDeviceInfoLinuxCommonPrivate::connectNotify(signal);
}

void QSystemDeviceInfoPrivate::disconnectNotify(const char *signal)
{
    if (QLatin1String(signal) == QLatin1String(QMetaObject::normalizedSignature(SIGNAL(lockStatusChanged(QSystemDeviceInfo::LockTypeFlags))))) {
        QDBusConnection::systemBus().disconnect("com.nokia.mce", "/com/nokia/mce/signal", "com.nokia.mce.signal", "tklock_mode_ind",
                                                this, SLOT(touchAndKeyboardStateChanged(const QString&)));
        QDBusConnection::systemBus().disconnect("com.nokia.devicelock", "/request", "com.nokia.devicelock", "stateChanged",
                                                this, SLOT(deviceStateChanged(int,int)));
    }
    if (QLatin1String(signal) == QLatin1String(QMetaObject::normalizedSignature(SIGNAL(currentProfileChanged(QSystemDeviceInfo::Profile))))) {
        QDBusConnection::systemBus().disconnect("com.nokia.mce",
                               "/com/nokia/mce/signal",
                               "com.nokia.mce.signal",
                               "sig_device_mode_ind",
                               this, SLOT(deviceModeChanged(QString)));
        QDBusConnection::sessionBus().disconnect("com.nokia.profiled",
                               "/com/nokia/profiled",
                               "com.nokia.profiled",
                               "profile_changed",
                               this, SLOT(profileChanged(bool, bool, QString, QList<ProfileDataValue>)));
    }
    if (QLatin1String(signal) == QLatin1String(QMetaObject::normalizedSignature(SIGNAL(keyboardFlipped(bool))))) {
        if (gpioFD != -1) {
            ::close(gpioFD);
            gpioFD = -1;
        }
    }
    QSystemDeviceInfoLinuxCommonPrivate::disconnectNotify(signal);
}

#if !defined(QT_NO_DBUS)
void QSystemDeviceInfoPrivate::halChanged(int,QVariantList map)
{
    for(int i=0; i < map.count(); i++) {
       if(map.at(i).toString() == "battery.charge_level.percentage") {
            int level = batteryLevel();
            if(currentBatteryLevel != level) {
                currentBatteryLevel = level;
                emit batteryLevelChanged(level);
            }
            QSystemDeviceInfo::BatteryStatus stat = QSystemDeviceInfo::NoBatteryLevel;

            if(level < 4) {
                stat = QSystemDeviceInfo::BatteryCritical;
            } else if(level < 11) {
                stat = QSystemDeviceInfo::BatteryVeryLow;
            } else if(level < 41) {
                stat = QSystemDeviceInfo::BatteryLow;
            } else if(level > 40) {
                stat = QSystemDeviceInfo::BatteryNormal;
            }
            if(currentBatStatus != stat) {
                currentBatStatus = stat;
                Q_EMIT batteryStatusChanged(stat);
            }
        }
        if((map.at(i).toString() == "maemo.charger.connection_status")
        || (map.at(i).toString() == "maemo.rechargeable.charging_status")) {
            QSystemDeviceInfo::PowerState state = currentPowerState();
            if (previousPowerState != state)
                emit powerStateChanged(state);
            previousPowerState = state;
       }
    } //end map
}
#endif

QSystemDeviceInfo::Profile QSystemDeviceInfoPrivate::currentProfile()
{
#if !defined(QT_NO_DBUS)
    if (flightMode())
        return QSystemDeviceInfo::OfflineProfile;

    if (silentProfile())
        return vibrationActive() ? QSystemDeviceInfo::VibProfile : QSystemDeviceInfo::SilentProfile;

    if (beepProfile())
        return QSystemDeviceInfo::BeepProfile;

    if (voiceRingtoneVolume() > 75)
        return QSystemDeviceInfo::LoudProfile;

    return QSystemDeviceInfo::NormalProfile;
#endif

    return QSystemDeviceInfo::UnknownProfile;
}

QString QSystemDeviceInfoPrivate::imei()
{
#if !defined(QT_NO_DBUS)
    #if defined(Q_WS_MAEMO_6)
        QString dBusService = "com.nokia.csd.Info";
    #else
        /* Maemo 5 */
        QString dBusService = "com.nokia.phone.SIM";
    #endif
    QDBusInterface connectionInterface(dBusService,
                                       "/com/nokia/csd/info",
                                       "com.nokia.csd.Info",
                                        QDBusConnection::systemBus(), this);
    QDBusReply< QString > reply = connectionInterface.call("GetIMEINumber");
    return reply.value();
#endif
    return "";
}

QString QSystemDeviceInfoPrivate::imsi()
{
#if defined(Q_WS_MAEMO_6)
    /* Maemo 6 */
    #if !defined(QT_NO_DBUS)
        QDBusInterface connectionInterface("com.nokia.csd.SIM",
                                           "/com/nokia/csd/sim",
                                           "com.nokia.csd.SIM.Identity",
                                           QDBusConnection::systemBus(), this);
        QDBusReply< QString > reply = connectionInterface.call("GetIMSI");
        return reply.value();
    #endif
    return "";
#else
    /* Maemo 5 */
    return GConfItem("/system/nokia/location/sim_imsi").value().toString();
#endif
}

QSystemDeviceInfo::SimStatus QSystemDeviceInfoPrivate::simStatus()
{
    QSystemDeviceInfo::SimStatus simStatus = QSystemDeviceInfo::SimNotAvailable;
    QString imsi = QSystemDeviceInfoPrivate::imsi();
    if (imsi.length() > 0) {
        simStatus = QSystemDeviceInfo::SingleSimAvailable;
    }
    return simStatus;
}

bool QSystemDeviceInfoPrivate::isDeviceLocked()
{
#if !defined(QT_NO_DBUS)
    QDBusConnection systemDbusConnection = QDBusConnection::systemBus();

    QDBusInterface mceConnectionInterface("com.nokia.mce",
                                      "/com/nokia/mce/request",
                                      "com.nokia.mce.request",
                                      systemDbusConnection, this);
    if (mceConnectionInterface.isValid()) {
        QDBusReply<QString> tkLockModeReply = mceConnectionInterface.call("get_tklock_mode");
        return tkLockModeReply.value() == "locked";
    }

    qDebug() << "mce interface not valid";
#endif
    return false;
}

QSystemDeviceInfo::PowerState QSystemDeviceInfoPrivate::currentPowerState()
{
#if !defined(QT_NO_DBUS)
        QHalInterface iface;
        const QStringList list = iface.findDeviceByCapability("battery");
        if(!list.isEmpty()) {
            foreach(const QString &dev, list) {
                QHalDeviceInterface ifaceDevice(dev);
                if (iface.isValid()) {
                    if (ifaceDevice.getPropertyString("maemo.charger.connection_status") == "connected") {
                        if (ifaceDevice.getPropertyString("maemo.rechargeable.charging_status") == "full")
                            return QSystemDeviceInfo::WallPower;
                        return QSystemDeviceInfo::WallPowerChargingBattery;
                    }
                    return QSystemDeviceInfo::BatteryPower;
                }
            }
        }
#endif
    return QSystemDeviceInfo::UnknownPower;
}

QSystemDeviceInfo::ThermalState QSystemDeviceInfoPrivate::currentThermalState()
{
#if !defined(QT_NO_DBUS)
    QString dBusService = "com.nokia.thermalmanager";
    QDBusReply<QString> thermalStateReply = QDBusConnection::systemBus().call
            (QDBusMessage::createMethodCall("com.nokia.thermalmanager",
                                            "/com/nokia/thermalmanager",
                                            "com.nokia.thermalmanager",
                                            "get_thermal_state"));
    if (thermalStateReply.isValid()){
        if (thermalStateReply.value() == "normal"){
            return QSystemDeviceInfo::NormalThermal;
        }
        if (thermalStateReply.value() == "warning"){
            return QSystemDeviceInfo::WarningThermal;
        }
        if (thermalStateReply.value() == "alert"){
            return QSystemDeviceInfo::AlertThermal;
        }
        if (thermalStateReply.value() == "unknown"){
            return QSystemDeviceInfo::UnknownThermal;
        } else {
            return QSystemDeviceInfo::ErrorThermal;
        }
    }
#endif
     return QSystemDeviceInfo::UnknownThermal;
}

#if !defined(QT_NO_DBUS)
 void QSystemDeviceInfoPrivate::setupBluetooth()
 {
     QDBusInterface *connectionInterface;
     connectionInterface = new QDBusInterface("org.bluez",
                                              "/",
                                              "org.bluez.Manager",
                                              QDBusConnection::systemBus(), this);
     if (connectionInterface->isValid()) {

         QDBusReply<  QDBusObjectPath > reply = connectionInterface->call("DefaultAdapter");
         if (reply.isValid()) {
             QDBusInterface *adapterInterface;
             adapterInterface = new QDBusInterface("org.bluez",
                                                   reply.value().path(),
                                                   "org.bluez.Adapter",
                                                   QDBusConnection::systemBus(), this);
             if (adapterInterface->isValid()) {
                 if (!QDBusConnection::systemBus().connect("org.bluez",
                                           reply.value().path(),
                                            "org.bluez.Adapter",
                                            "PropertyChanged",
                                            this,SLOT(bluezPropertyChanged(QString, QDBusVariant)))) {
                     qDebug() << "bluez could not connect signal";
                 }
             }
         }
     }
 }
#endif

#if !defined(QT_NO_DBUS)
void QSystemDeviceInfoPrivate::bluezPropertyChanged(const QString &name, QDBusVariant value)
{
    if (name == "Powered")
        emit bluetoothStateChanged(value.variant().toBool());
}
#endif

#if !defined(QT_NO_DBUS)

void QSystemDeviceInfoPrivate::queryRingingAlertType()
{
    if (m_ringingAlertTypeQueried) {
        return;
    }

    QDBusMessage ringingAlertTypeMsg = QDBusMessage::createMethodCall("com.nokia.profiled", "/com/nokia/profiled",
                                                                      "com.nokia.profiled", "get_value");
    ringingAlertTypeMsg << profileName();
    ringingAlertTypeMsg << "ringing.alert.type";

    QDBusReply<QString> ringingAlertTypeReply = QDBusConnection::sessionBus().call(ringingAlertTypeMsg);

    if (ringingAlertTypeReply.isValid()) {
        m_silentProfile = QString::compare(ringingAlertTypeReply.value(), "silent", Qt::CaseInsensitive) == 0;
        m_beepProfile = QString::compare(ringingAlertTypeReply.value(), "beep", Qt::CaseInsensitive) == 0;
        m_ringingAlertTypeQueried = true;
    }
}

bool QSystemDeviceInfoPrivate::flightMode()
{
   if (m_flightModeQueried) {
       return m_flightMode;
   }

   QDBusReply<quint32> radioStatesReply = QDBusConnection::systemBus().call(
                QDBusMessage::createMethodCall("com.nokia.mce",  "/com/nokia/mce/request",
                                               "com.nokia.mce.request", "get_radio_states"));
    if (radioStatesReply.isValid()) {
        quint32 radioStateFlags = radioStatesReply.value();
#define MCE_RADIO_STATE_WLAN            (1 << 2)
#define MCE_RADIO_STATE_BLUETOOTH       (1 << 3)

        m_flightMode = !(radioStateFlags & ~(MCE_RADIO_STATE_WLAN | MCE_RADIO_STATE_BLUETOOTH));
        m_flightModeQueried = true;
    }
    return m_flightMode;
}

QString QSystemDeviceInfoPrivate::profileName()
{
    if (m_profileNameQueried) {
        return m_profileName;
    }

    QDBusReply<QString> profileNameReply = QDBusConnection::sessionBus().call(
                QDBusMessage::createMethodCall("com.nokia.profiled", "/com/nokia/profiled",
                                               "com.nokia.profiled", "get_profile"));

    if (profileNameReply.isValid()) {
        m_profileName = profileNameReply.value();
        m_profileNameQueried = true;
    }
    return m_profileName;
}

bool QSystemDeviceInfoPrivate::silentProfile()
{
    if (!m_ringingAlertTypeQueried) {
        queryRingingAlertType();
    }
    return m_silentProfile;
}

bool QSystemDeviceInfoPrivate::beepProfile()
{
    if (!m_ringingAlertTypeQueried) {
        queryRingingAlertType();
    }
    return m_beepProfile;
}

void QSystemDeviceInfoPrivate::deviceModeChanged(QString newMode)
{
    bool previousFlightMode = m_flightMode;
    m_flightMode = newMode == "flight";
    m_flightModeQueried = true;
    if (previousFlightMode != m_flightMode)
        emit currentProfileChanged(currentProfile());
}

void QSystemDeviceInfoPrivate::profileChanged(bool changed, bool active, QString profile, QList<ProfileDataValue> values)
{
    if (active) {
        m_profileName = profile;
        m_profileNameQueried = true;

        foreach (const ProfileDataValue &value, values) {
            if (value.key == "ringing.alert.type") {
                m_silentProfile = QString::compare(value.val, "silent", Qt::CaseInsensitive) == 0;
                m_beepProfile = QString::compare(value.val, "beep", Qt::CaseInsensitive) == 0;
                m_ringingAlertTypeQueried = true;
            } else if (value.key == "vibrating.alert.enabled") {
                m_vibratingAlertEnabled = QString::compare(value.val, "On", Qt::CaseInsensitive) == 0;
                m_vibratingAlertEnabledQueried = true;
            } else if (value.key == "ringing.alert.volume") {
                m_ringingAlertVolume = value.val.toInt();
                m_ringingAlertVolumeQueried = true;
            } else if (value.key == "sms.alert.volume") {
                m_smsAlertVolume = value.val.toInt();
                m_smsAlertVolumeQueried = true;
            }
        }
        if (changed)
            emit currentProfileChanged(currentProfile());
    }
}

QString QSystemDeviceInfoPrivate::model()
{
#if !defined(QT_NO_DBUS)
    QString product = sysinfodValueForKey("/component/product");
    if (!product.isEmpty()) {
        return product;
    }
#endif
    return QString();
}

QString QSystemDeviceInfoPrivate::productName()
{
#if !defined(QT_NO_DBUS)
    QString productName = sysinfodValueForKey("/component/product-name");
    if (!productName.isEmpty()) {
        return productName;
    }
#endif
    return QString();
}

#endif

bool QSystemDeviceInfoPrivate::isKeyboardFlippedOpen()
{
    bool keyboardFlippedOpen = false;
    unsigned long bits[NBITS(KEY_MAX)] = {0}; /* switch state bits */
    int eventFd = ::open("/dev/input/gpio-keys", O_RDONLY | O_NONBLOCK);

    if ((eventFd != -1) && (ioctl(eventFd, EVIOCGSW(KEY_MAX), bits) != -1)) {
            keyboardFlippedOpen = (0 == test_bit(SW_KEYPAD_SLIDE, bits));
    }
    if (eventFd != -1) {
        ::close(eventFd);
    }
    return keyboardFlippedOpen;
}

void QSystemDeviceInfoPrivate::socketActivated(int fd)
{
    int result = 0;
     do {
        struct input_event inputEvent;
        result = read(fd, &inputEvent, sizeof(inputEvent));
        if (result == sizeof(inputEvent)) {
            if(inputEvent.type > 0 && inputEvent.code == SW_KEYPAD_SLIDE) {
                Q_EMIT keyboardFlipped(!inputEvent.value);
            }
        }
    } while (result > 0);
}


bool QSystemDeviceInfoPrivate::keypadLightOn(QSystemDeviceInfo::KeypadType type)
{
    bool lightOn = false;

    if (type != QSystemDeviceInfo::PrimaryKeypad) {
        return lightOn;
    }

#if !defined(QT_NO_DBUS)
    QDBusReply<bool> reply = QDBusConnection::systemBus().call(
                                 QDBusMessage::createMethodCall("com.nokia.mce", "/com/nokia/mce/request",
                                                                "com.nokia.mce.request", "get_key_backlight_state"));
    if (reply.isValid()) {
        lightOn = reply.value();
    }
#endif
    return lightOn;
}

int QSystemDeviceInfoPrivate::messageRingtoneVolume()
{
    if (m_smsAlertVolumeQueried) {
        return m_smsAlertVolume;
    }

    QDBusMessage smsAlertVolumeMsg = QDBusMessage::createMethodCall("com.nokia.profiled", "/com/nokia/profiled",
                                                                    "com.nokia.profiled", "get_value");
    smsAlertVolumeMsg << profileName();
    smsAlertVolumeMsg << "sms.alert.volume";

    QDBusReply<QString> smsAlertVolumeReply = QDBusConnection::sessionBus().call(smsAlertVolumeMsg);
    if (smsAlertVolumeReply.isValid()) {
        m_smsAlertVolume = smsAlertVolumeReply.value().toInt();
        m_smsAlertVolumeQueried = true;
    }
    return m_smsAlertVolume;
}

int QSystemDeviceInfoPrivate::voiceRingtoneVolume()
{
    if (m_ringingAlertVolumeQueried) {
        return m_ringingAlertVolume;
    }

    QDBusMessage ringingAlertVolumeMsg = QDBusMessage::createMethodCall("com.nokia.profiled", "/com/nokia/profiled",
                                                                        "com.nokia.profiled", "get_value");
    ringingAlertVolumeMsg << profileName();
    ringingAlertVolumeMsg << "ringing.alert.volume";

    QDBusReply<QString> ringingAlertVolumeReply = QDBusConnection::sessionBus().call(ringingAlertVolumeMsg);
    if (ringingAlertVolumeReply.isValid()) {
        m_ringingAlertVolume = ringingAlertVolumeReply.value().toInt();
        m_ringingAlertVolumeQueried = true;
    }
    return m_ringingAlertVolume;
}

bool QSystemDeviceInfoPrivate::vibrationActive()
{
    if (m_vibratingAlertEnabledQueried) {
        return m_vibratingAlertEnabled;
    }

    QDBusMessage vibratingAlertMsg = QDBusMessage::createMethodCall("com.nokia.profiled", "/com/nokia/profiled",
                                                                    "com.nokia.profiled", "get_value");
    vibratingAlertMsg << profileName();
    vibratingAlertMsg << "vibrating.alert.enabled";

    QDBusReply<QString> vibratingAlertEnabledReply = QDBusConnection::sessionBus().call(vibratingAlertMsg);
    if (vibratingAlertEnabledReply.isValid()) {
        m_vibratingAlertEnabled = QString::compare(vibratingAlertEnabledReply.value(), "On", Qt::CaseInsensitive) == 0;
        m_vibratingAlertEnabledQueried = true;
    }
    return m_vibratingAlertEnabled;
}

QSystemDeviceInfo::LockTypeFlags QSystemDeviceInfoPrivate::lockStatus()
{
    QSystemDeviceInfo::LockTypeFlags lockFlags; /* no active locks */
#if !defined(QT_NO_DBUS)
    /* Check the PIN lock status from devicelock */
    QDBusMessage lockStateCall = QDBusMessage::createMethodCall("com.nokia.devicelock", "/request",
                                                                "com.nokia.devicelock", "getState");
    /* getState argument 0: LockType_t, where 1 = the device lock */
    lockStateCall << QVariant::fromValue(1);

    QDBusReply<int> deviceLockReply = QDBusConnection::systemBus().call(lockStateCall);
    if (deviceLockReply.isValid()) {
        int lockState = deviceLockReply.value();
        if (lockState != 0) {
            /* 0 == unlocked, if we get any other state back, we are locked */
            lockFlags |= QSystemDeviceInfo::PinLocked;
        }
    }

    /* Check the touch screen/keypad lock status from MCE */
    QDBusReply<QString> mceReply = QDBusConnection::systemBus().call(
                                       QDBusMessage::createMethodCall("com.nokia.mce", "/com/nokia/mce/request",
                                                                      "com.nokia.mce.request", "get_tklock_mode"));
    if (mceReply.isValid()) {
        QString tkLockMode = mceReply.value();
        if (tkLockMode != "unlocked" && tkLockMode != "silent-unlocked") {
            lockFlags |= QSystemDeviceInfo::TouchAndKeyboardLocked;
             currentLockType = lockFlags;
        }
    }
#endif

    return lockFlags;
}

QSystemDeviceInfo::KeyboardTypeFlags QSystemDeviceInfoPrivate::keyboardTypes()
{
    QSystemDeviceInfo::KeyboardTypeFlags keyboardFlags;
    keyboardFlags = QSystemDeviceInfoLinuxCommonPrivate::keyboardTypes();
    keyboardFlags = (keyboardFlags | QSystemDeviceInfo::SoftwareKeyboard);
    if(model() == "RX-51") //fixme detect flip keyboard
        keyboardFlags = (keyboardFlags | QSystemDeviceInfo::FlipKeyboard);
    return keyboardFlags;
}

void QSystemDeviceInfoPrivate::deviceStateChanged(int device, int state)
{
    QSystemDeviceInfo::LockTypeFlags lockFlags;
    if (device == 1 && state != 0) {
        lockFlags |= QSystemDeviceInfo::PinLocked;
        currentLockType |= lockFlags;
        emit lockStatusChanged(lockFlags);
    } else {
        if (currentLockType & QSystemDeviceInfo::PinLocked) {
            currentLockType &= ~QSystemDeviceInfo::PinLocked;
        }
        lockFlags |= QSystemDeviceInfo::UnknownLock;
        currentLockType |= lockFlags;
        emit lockStatusChanged(lockFlags);
    }
}

void QSystemDeviceInfoPrivate::touchAndKeyboardStateChanged(const QString& state)
{
    QSystemDeviceInfo::LockTypeFlags lockFlags;
    if (state != "unlocked" && state != "silent-unlocked") {
        lockFlags |= QSystemDeviceInfo::TouchAndKeyboardLocked;
        currentLockType |= lockFlags;
        emit lockStatusChanged(lockFlags);
    } else {
        if (currentLockType & QSystemDeviceInfo::TouchAndKeyboardLocked) {
            currentLockType &= ~QSystemDeviceInfo::TouchAndKeyboardLocked;
        }
        lockFlags |= QSystemDeviceInfo::UnknownLock;
        currentLockType |= lockFlags;
        emit lockStatusChanged(lockFlags);
    }
}

QByteArray QSystemDeviceInfoPrivate::uniqueDeviceID()
{
#if defined(Q_WS_MAEMO_6)
    // create one from imei and mac addersses of bt and wlan interfaces
    QSystemNetworkInfo netinfo;
    QString wlanmac = netinfo.macAddress(QSystemNetworkInfo::WlanMode);
    QString btmac = netinfo.macAddress(QSystemNetworkInfo::BluetoothMode);

    QByteArray bytes = imei().toLocal8Bit();
    QCryptographicHash hash(QCryptographicHash::Sha1);

    hash.addData(bytes);
    hash.addData(wlanmac.toLocal8Bit());
    hash.addData(btmac.toLocal8Bit());
    qDebug() << Q_FUNC_INFO << hash.result().toHex();

    return hash.result().toHex();
#endif
}

QSystemScreenSaverPrivate::QSystemScreenSaverPrivate(QObject *parent)
    : QObject(parent)
    , isInhibited(false)
    , ssTimer(0)
{
#if !defined(QT_NO_DBUS)
    mceConnectionInterface = new QDBusInterface("com.nokia.mce",
                                                "/com/nokia/mce/request",
                                                "com.nokia.mce.request",
                                                QDBusConnection::systemBus(), this);
#endif // QT_NO_DBUS
}

QSystemScreenSaverPrivate::~QSystemScreenSaverPrivate()
{
    setScreenSaverInhibited(false);
}

bool QSystemScreenSaverPrivate::setScreenSaverInhibit()
{
    wakeUpDisplay();

    if (!ssTimer)
        ssTimer = new QTimer(this);

    if (!ssTimer->isActive()) {
        connect(ssTimer, SIGNAL(timeout()), this, SLOT(wakeUpDisplay()));
        // Set a wake up interval of 30 seconds.
        // The reason for this is to avoid the situation where
        // a crashed/hung application keeps the display on.
        ssTimer->start(30000);
        isInhibited = true;
    } else {
        isInhibited = false;
    }

    return screenSaverInhibited();
}

void QSystemScreenSaverPrivate::wakeUpDisplay()
{
#if !defined(QT_NO_DBUS)
    if (mceConnectionInterface->isValid()) {
        QDBusMessage msg = mceConnectionInterface->call("req_tklock_mode_change", "unlocked");
        qDebug() << msg.errorName() << msg.errorMessage();
        msg = mceConnectionInterface->call("req_display_blanking_pause");
        qDebug() << msg.errorName() << msg.errorMessage();
    }
#endif // QT_NO_DBUS
}

bool QSystemScreenSaverPrivate::screenSaverInhibited()
{
    bool displayOn = false;
    GConfItem screenBlankItem("/system/osso/dsm/display/inhibit_blank_mode");
    /* 0 - no inhibit
       1 - inhibit dim with charger
       2 - inhibit blank with charger (display still dims)
       3 - inhibit dim (always)
       4 - inhibit blank (always; display still dims)
    */
    int blankingItem = screenBlankItem.value().toInt();

    bool isBlankingInhibited = false;
    QSystemDeviceInfo devInfo(this);
    QSystemDeviceInfo::PowerState batState = devInfo.currentPowerState();

    if (((batState == QSystemDeviceInfo::WallPower || batState == QSystemDeviceInfo::WallPowerChargingBattery) && blankingItem == 2)
        || blankingItem == 4) {
        isBlankingInhibited = true;
    }

#if !defined(QT_NO_DBUS)
    if (mceConnectionInterface->isValid()) {
        // The most educated guess for the screen saver being inhibited is to determine
        // whether the display is on. That is because the QSystemScreenSaver cannot
        // prevent other processes from blanking the screen (like, if
        // MCE decides to blank the screen for some reason).
        // but that means it reports to be inhibited when display is on. meaning
        // effectly always inhibited by default. so we try a bit harder
        QDBusReply<QString> reply = mceConnectionInterface->call("get_display_status");
        displayOn = ("on" == reply.value());
    }
#endif // QT_NO_DBUS

    return ((displayOn && isBlankingInhibited) || (displayOn && isInhibited));
}

void QSystemScreenSaverPrivate::setScreenSaverInhibited(bool on)
{
    if (on) {
        setScreenSaverInhibit();
    } else {
        if (ssTimer && ssTimer->isActive()) {
            ssTimer->stop();
            isInhibited = false;
        }
    }
}

QSystemBatteryInfoPrivate::QSystemBatteryInfoPrivate(QSystemBatteryInfoLinuxCommonPrivate *parent)
    : QSystemBatteryInfoLinuxCommonPrivate(parent)
{
#if !defined(QT_NO_DBUS)
    QHalInterface iface;
    QStringList list = iface.findDeviceByCapability("battery");
    if (!list.isEmpty()) {
        foreach (const QString &dev, list) {
            halIfaceDevice = new QHalDeviceInterface(dev);
            if (halIfaceDevice->isValid()) {
                if (halIfaceDevice->setConnections()) {
                    if (!connect(halIfaceDevice,SIGNAL(propertyModified(int, QVariantList)),
                                 this,SLOT(halChangedMaemo(int,QVariantList)))) {
                        qDebug() << "connection malfunction";
                    }
                }
                return;
            }
        }
    }
#endif
}

QSystemBatteryInfoPrivate::~QSystemBatteryInfoPrivate()
{

}

#if !defined(QT_NO_DBUS)
void QSystemBatteryInfoPrivate::halChangedMaemo(int count,QVariantList map)
{
    QHalInterface iface;
    QStringList list = iface.findDeviceByCapability("battery");
    QHalDeviceInterface ifaceDevice(list.at(0)); //default battery
    if (ifaceDevice.isValid()) {
        for(int i=0; i < count; i++) {
            QString mapS = map.at(i).toString();
            qDebug() << __FUNCTION__ << mapS;
            QSystemBatteryInfo::ChargerType chargerType = QSystemBatteryInfo::UnknownCharger;
             if (  mapS == "maemo.charger.connection_status" || mapS == "maemo.charger.type") {
                const QString chargeType = ifaceDevice.getPropertyString("maemo.charger.type");
                if(chargeType == "host 500 mA") {
                    chargerType = QSystemBatteryInfo::USB_500mACharger;
                }
                if(chargeType == "host 100 mA") {
                    chargerType = QSystemBatteryInfo::USB_100mACharger;
                }
                chargerType = QSystemBatteryInfoLinuxCommonPrivate::currentChargerType();
                if (chargerType == QSystemBatteryInfo::UnknownCharger) {
                    chargerType = QSystemBatteryInfo::WallCharger;
                }

                if(chargerType != curChargeType) {
                    curChargeType = chargerType;
                    Q_EMIT chargerTypeChanged(curChargeType);
                }
            }
         }
    }
}
#endif

#include "moc_qsysteminfo_maemo_p.cpp"

QTM_END_NAMESPACE