only include Applications by default on OS X
[ambit.git] / src / diskinformation.cpp
blobf572e5bdd1a5ab6c39f9080e266b8acbe42916fc
1 /**
2 * Copyright (C) 2007 Benjamin C. Meyer
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 #include "diskinformation.h"
21 #if defined(Q_OS_UNIX)
22 # include <sys/statvfs.h>
24 DiskInformation getDiskInformation(const QString &path)
26 struct statvfs vfs;
27 if (statvfs(path.toLocal8Bit().constData(), &vfs) < 0)
28 return DiskInformation();
29 DiskInformation result = {
30 vfs.f_blocks * (qint64)vfs.f_frsize,
31 vfs.f_bavail * (qint64)vfs.f_frsize
33 return result;
36 #endif
38 /****************************************************************************
40 ** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.
42 ** This file is part of the QtGui module of the Qt Toolkit.
44 ** This file may be used under the terms of the GNU General Public
45 ** License version 2.0 as published by the Free Software Foundation
46 ** and appearing in the file LICENSE.GPL included in the packaging of
47 ** this file. Please review the following information to ensure GNU
48 ** General Public Licensing requirements will be met:
49 ** http://www.trolltech.com/products/qt/opensource.html
51 ** If you are unsure which license is appropriate for your use, please
52 ** review the following information:
53 ** http://www.trolltech.com/products/qt/licensing.html or contact the
54 ** sales department at sales@trolltech.com.
56 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
57 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
59 ****************************************************************************/
61 #include <qlocale.h>
63 QString humanSize(qint64 bytes)
65 // According to the Si standard KB is 1000 bytes, KiB is 1024
66 // but on windows sizes are calculated by dividing by 1024 so we do what they do.
67 const qint64 kb = 1024;
68 const qint64 mb = 1024 * kb;
69 const qint64 gb = 1024 * mb;
70 const qint64 tb = 1024 * gb;
71 if (bytes >= tb)
72 return QString("%1 TB").arg(QLocale().toString(qreal(bytes) / tb, 'f', 3));
73 if (bytes >= gb)
74 return QString("%1 GB").arg(QLocale().toString(qreal(bytes) / gb, 'f', 2));
75 if (bytes >= mb)
76 return QString("%1 MB").arg(QLocale().toString(qreal(bytes) / mb, 'f', 1));
77 if (bytes >= kb)
78 return QString("%1 KB").arg(QLocale().toString(bytes / kb));
79 return QString("%1 bytes").arg(QLocale().toString(bytes));