libkipi from trunk (KDE 4.3) : add support of kipi host settings "file timestamp...
[kdegraphics.git] / gwenview / lib / memoryutils.cpp
blob0ee9ee225f1e885e8c46a79b4de29680b660778f
1 // vim: set tabstop=4 shiftwidth=4 noexpandtab:
2 /*
3 Gwenview: an image viewer
4 Copyright 2008 Aurélien Gâteau <aurelien.gateau@free.fr>
5 Copyright (C) 2004-2005 by Enrico Ros <eros.kde@email.it>
6 Copyright (C) 2004-2007 by Albert Astals Cid <aacid@kde.org>
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Cambridge, MA 02110-1301, USA.
23 // Self
24 #include "memoryutils.h"
26 // Qt
27 #include <QFile>
28 #include <QTextStream>
29 #include <QTime>
31 // System
32 #ifdef Q_OS_WIN
33 #define _WIN32_WINNT 0x0500
34 #include <windows.h>
35 #endif
37 namespace Gwenview {
40 namespace MemoryUtils {
42 // This code has been copied from okular/core/document.cpp
43 qulonglong getTotalMemory() {
44 static qulonglong cachedValue = 0;
45 if ( cachedValue )
46 return cachedValue;
48 #if defined(Q_OS_LINUX)
49 // if /proc/meminfo doesn't exist, return 128MB
50 QFile memFile( "/proc/meminfo" );
51 if ( !memFile.open( QIODevice::ReadOnly ) )
52 return (cachedValue = 134217728);
54 // read /proc/meminfo and sum up the contents of 'MemFree', 'Buffers'
55 // and 'Cached' fields. consider swapped memory as used memory.
56 QTextStream readStream( &memFile );
57 while ( true )
59 QString entry = readStream.readLine();
60 if ( entry.isNull() ) break;
61 if ( entry.startsWith( "MemTotal:" ) )
62 return (cachedValue = (1024 * entry.section( ' ', -2, -2 ).toInt()));
64 #elif defined(Q_OS_WIN)
65 MEMORYSTATUSEX stat;
67 GlobalMemoryStatusEx (&stat);
69 return ( cachedValue = stat.ullTotalPhys );
70 #endif
71 return (cachedValue = 134217728);
75 qulonglong getFreeMemory() {
76 static QTime lastUpdate = QTime::currentTime();
77 static qulonglong cachedValue = 0;
79 if ( lastUpdate.secsTo( QTime::currentTime() ) <= 2 )
80 return cachedValue;
82 #if defined(Q_OS_LINUX)
83 // if /proc/meminfo doesn't exist, return MEMORY FULL
84 QFile memFile( "/proc/meminfo" );
85 if ( !memFile.open( QIODevice::ReadOnly ) )
86 return 0;
88 // read /proc/meminfo and sum up the contents of 'MemFree', 'Buffers'
89 // and 'Cached' fields. consider swapped memory as used memory.
90 qulonglong memoryFree = 0;
91 QString entry;
92 QTextStream readStream( &memFile );
93 while ( true )
95 entry = readStream.readLine();
96 if ( entry.isNull() ) break;
97 if ( entry.startsWith( "MemFree:" ) ||
98 entry.startsWith( "Buffers:" ) ||
99 entry.startsWith( "Cached:" ) ||
100 entry.startsWith( "SwapFree:" ) )
101 memoryFree += entry.section( ' ', -2, -2 ).toInt();
102 if ( entry.startsWith( "SwapTotal:" ) )
103 memoryFree -= entry.section( ' ', -2, -2 ).toInt();
105 memFile.close();
107 lastUpdate = QTime::currentTime();
109 return ( cachedValue = (1024 * memoryFree) );
110 #elif defined(Q_OS_WIN)
111 MEMORYSTATUSEX stat;
113 GlobalMemoryStatusEx (&stat);
115 lastUpdate = QTime::currentTime();
117 return ( cachedValue = stat.ullAvailPhys );
118 #else
119 // tell the memory is full.. will act as in LOW profile
120 return 0;
121 #endif
124 } // MemoryUtils namespace
126 } // Gwenview namespace