Changeset View
Changeset View
Standalone View
Standalone View
Modules/about-distro/src/MemoryEntry.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | Copyright (C) 2012-2020 Harald Sitter <sitter@kde.org> | ||||
3 | | ||||
4 | This program is free software; you can redistribute it and/or | ||||
5 | modify it under the terms of the GNU General Public License as | ||||
6 | published by the Free Software Foundation; either version 2 of | ||||
7 | the License or (at your option) version 3 or any later version | ||||
8 | accepted by the membership of KDE e.V. (or its successor approved | ||||
9 | by the membership of KDE e.V.), which shall act as a proxy | ||||
10 | defined in Section 14 of version 3 of the license. | ||||
11 | | ||||
12 | This program is distributed in the hope that it will be useful, | ||||
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
15 | GNU General Public License for more details. | ||||
16 | | ||||
17 | You should have received a copy of the GNU General Public License | ||||
18 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
19 | */ | ||||
20 | | ||||
21 | #include "MemoryEntry.h" | ||||
22 | | ||||
23 | #include <KFormat> | ||||
24 | | ||||
25 | #ifdef Q_OS_LINUX | ||||
26 | #include <sys/sysinfo.h> | ||||
27 | #elif defined(Q_OS_FREEBSD) | ||||
28 | #include <sys/types.h> | ||||
29 | #include <sys/sysctl.h> | ||||
30 | #endif | ||||
31 | | ||||
32 | MemoryEntry::MemoryEntry() : Entry(ki18n("Memory:"), text()) | ||||
33 | { | ||||
34 | } | ||||
35 | | ||||
36 | qlonglong MemoryEntry::calculateTotalRam() | ||||
37 | { | ||||
38 | qlonglong ret = -1; | ||||
39 | #ifdef Q_OS_LINUX | ||||
40 | struct sysinfo info; | ||||
41 | if (sysinfo(&info) == 0) | ||||
42 | // manpage "sizes are given as multiples of mem_unit bytes" | ||||
43 | ret = qlonglong(info.totalram) * info.mem_unit; | ||||
44 | #elif defined(Q_OS_FREEBSD) | ||||
45 | /* Stuff for sysctl */ | ||||
46 | size_t len; | ||||
47 | | ||||
48 | unsigned long memory; | ||||
49 | len = sizeof(memory); | ||||
50 | sysctlbyname("hw.physmem", &memory, &len, NULL, 0); | ||||
51 | | ||||
52 | ret = memory; | ||||
53 | #endif | ||||
54 | return ret; | ||||
55 | } | ||||
56 | | ||||
57 | QString MemoryEntry::text() | ||||
58 | { | ||||
59 | const qlonglong totalRam = calculateTotalRam(); | ||||
60 | if (totalRam > 0) { | ||||
61 | return i18nc("@label %1 is the formatted amount of system memory (e.g. 7,7 GiB)", | ||||
62 | "%1 of RAM", KFormat().formatByteSize(totalRam)); | ||||
63 | } | ||||
64 | return i18nc("Unknown amount of RAM", "Unknown"); | ||||
65 | } |