2012-12-14 00:36:44 +04:00
|
|
|
/*
|
|
|
|
* Copyright (C) by Klaas Freitag <freitag@owncloud.com>
|
2013-10-02 21:06:33 +04:00
|
|
|
* Copyright (C) by Daniel Molkentin <danimo@owncloud.com>
|
2012-12-14 00:36:44 +04:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; version 2 of the License.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "utility.h"
|
2013-03-21 15:01:37 +04:00
|
|
|
|
2014-07-11 02:31:24 +04:00
|
|
|
#include "version.h"
|
2013-05-29 18:13:38 +04:00
|
|
|
|
2015-02-16 12:24:42 +03:00
|
|
|
// Note: This file must compile without QtGui
|
|
|
|
#include <QCoreApplication>
|
2013-08-23 02:35:33 +04:00
|
|
|
#include <QSettings>
|
2013-08-21 13:09:18 +04:00
|
|
|
#include <QTextStream>
|
2013-03-21 15:01:37 +04:00
|
|
|
#include <QDir>
|
|
|
|
#include <QFile>
|
|
|
|
#include <QUrl>
|
|
|
|
#include <QDebug>
|
2013-10-02 20:16:24 +04:00
|
|
|
#include <QProcess>
|
2013-12-10 16:47:44 +04:00
|
|
|
#include <QThread>
|
2014-01-08 17:34:51 +04:00
|
|
|
#include <QDateTime>
|
2014-02-13 20:21:15 +04:00
|
|
|
#include <QSysInfo>
|
2013-08-24 16:57:55 +04:00
|
|
|
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
|
2013-08-28 22:14:40 +04:00
|
|
|
#include <QTextDocument>
|
2013-08-24 16:57:55 +04:00
|
|
|
#else
|
|
|
|
#include <QStandardPaths>
|
|
|
|
#endif
|
|
|
|
|
2013-07-07 03:23:25 +04:00
|
|
|
#ifdef Q_OS_UNIX
|
|
|
|
#include <sys/statvfs.h>
|
|
|
|
#include <sys/types.h>
|
2013-09-10 13:35:30 +04:00
|
|
|
#include <unistd.h>
|
2013-07-09 16:07:48 +04:00
|
|
|
#endif
|
|
|
|
|
2013-07-10 11:19:41 +04:00
|
|
|
#include <stdarg.h>
|
|
|
|
|
2013-10-02 21:06:33 +04:00
|
|
|
#if defined(Q_OS_WIN)
|
2014-07-11 02:31:24 +04:00
|
|
|
#include "utility_win.cpp"
|
2013-10-02 21:06:33 +04:00
|
|
|
#elif defined(Q_OS_MAC)
|
2014-07-11 02:31:24 +04:00
|
|
|
#include "utility_mac.cpp"
|
2013-10-02 21:06:33 +04:00
|
|
|
#else
|
2014-07-11 02:31:24 +04:00
|
|
|
#include "utility_unix.cpp"
|
2013-03-21 15:01:37 +04:00
|
|
|
#endif
|
|
|
|
|
2014-11-10 00:34:07 +03:00
|
|
|
namespace OCC {
|
2012-12-14 00:36:44 +04:00
|
|
|
|
2014-01-08 17:34:51 +04:00
|
|
|
bool Utility::writeRandomFile( const QString& fname, int size )
|
|
|
|
{
|
|
|
|
int maxSize = 10*10*1024;
|
|
|
|
qsrand(QDateTime::currentMSecsSinceEpoch());
|
|
|
|
|
|
|
|
if( size == -1 ) size = qrand() % maxSize;
|
|
|
|
|
|
|
|
QString randString;
|
|
|
|
for( int i = 0; i < size; i++ ) {
|
|
|
|
int r = qrand() % 128;
|
|
|
|
randString.append(QChar(r));
|
|
|
|
}
|
|
|
|
|
|
|
|
QFile file(fname);
|
|
|
|
if( file.open(QIODevice::WriteOnly | QIODevice::Text) ) {
|
|
|
|
QTextStream out(&file);
|
|
|
|
out << randString;
|
|
|
|
// optional, as QFile destructor will already do it:
|
|
|
|
file.close();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-04-09 16:30:43 +04:00
|
|
|
QString Utility::formatFingerprint( const QByteArray& fmhash, bool colonSeparated )
|
2012-12-14 00:36:44 +04:00
|
|
|
{
|
|
|
|
QByteArray hash;
|
|
|
|
int steps = fmhash.length()/2;
|
|
|
|
for (int i = 0; i < steps; i++) {
|
|
|
|
hash.append(fmhash[i*2]);
|
|
|
|
hash.append(fmhash[i*2+1]);
|
|
|
|
hash.append(' ');
|
|
|
|
}
|
|
|
|
|
2013-08-24 16:57:55 +04:00
|
|
|
QString fp = QString::fromLatin1( hash.trimmed() );
|
2014-04-09 16:30:43 +04:00
|
|
|
if (colonSeparated) {
|
|
|
|
fp.replace(QChar(' '), QChar(':'));
|
|
|
|
}
|
2012-12-19 21:05:58 +04:00
|
|
|
|
|
|
|
return fp;
|
2012-12-14 00:36:44 +04:00
|
|
|
}
|
|
|
|
|
2013-03-21 15:01:37 +04:00
|
|
|
void Utility::setupFavLink(const QString &folder)
|
|
|
|
{
|
2013-10-02 21:06:33 +04:00
|
|
|
setupFavLink_private(folder);
|
2013-03-21 15:01:37 +04:00
|
|
|
}
|
|
|
|
|
2013-07-04 21:59:40 +04:00
|
|
|
QString Utility::octetsToString( qint64 octets )
|
|
|
|
{
|
2015-01-15 15:54:39 +03:00
|
|
|
static const qint64 kb = 1024;
|
|
|
|
static const qint64 mb = 1024 * kb;
|
|
|
|
static const qint64 gb = 1024 * mb;
|
|
|
|
static const qint64 tb = 1024 * gb;
|
2013-07-20 03:27:41 +04:00
|
|
|
|
2013-08-29 14:06:41 +04:00
|
|
|
QString s;
|
|
|
|
qreal value = octets;
|
2013-07-20 03:27:41 +04:00
|
|
|
if (octets >= tb) {
|
2015-01-15 16:06:17 +03:00
|
|
|
s = QCoreApplication::translate("Utility", "%L1 TiB");
|
2013-08-29 14:06:41 +04:00
|
|
|
value /= tb;
|
2013-07-20 03:27:41 +04:00
|
|
|
} else if (octets >= gb) {
|
2015-01-15 16:06:17 +03:00
|
|
|
s = QCoreApplication::translate("Utility", "%L1 GiB");
|
2013-08-29 14:06:41 +04:00
|
|
|
value /= gb;
|
2013-07-04 21:59:40 +04:00
|
|
|
} else if (octets >= mb) {
|
2015-01-15 16:06:17 +03:00
|
|
|
s = QCoreApplication::translate("Utility", "%L1 MiB");
|
2013-08-29 14:06:41 +04:00
|
|
|
value /= mb;
|
2013-07-04 21:59:40 +04:00
|
|
|
} else if (octets >= kb) {
|
2015-01-15 16:06:17 +03:00
|
|
|
s = QCoreApplication::translate("Utility", "%L1 KiB");
|
2013-08-29 14:06:41 +04:00
|
|
|
value /= kb;
|
|
|
|
} else {
|
|
|
|
s = QCoreApplication::translate("Utility", "%L1 B");
|
2013-07-04 21:59:40 +04:00
|
|
|
}
|
2013-08-29 14:06:41 +04:00
|
|
|
|
|
|
|
return (value > 9.95) ? s.arg(qRound(value)) : s.arg(value, 0, 'g', 2);
|
2013-07-04 21:59:40 +04:00
|
|
|
}
|
|
|
|
|
2013-05-28 21:35:30 +04:00
|
|
|
// Qtified version of get_platforms() in csync_owncloud.c
|
|
|
|
QString Utility::platform()
|
|
|
|
{
|
2013-12-03 19:23:49 +04:00
|
|
|
#if defined(Q_OS_WIN)
|
2013-05-28 21:35:30 +04:00
|
|
|
return QLatin1String("Windows");
|
|
|
|
#elif defined(Q_OS_MAC)
|
|
|
|
return QLatin1String("Macintosh");
|
|
|
|
#elif defined(Q_OS_LINUX)
|
|
|
|
return QLatin1String("Linux");
|
|
|
|
#elif defined(__DragonFly__) // Q_OS_FREEBSD also defined
|
2013-10-23 02:59:29 +04:00
|
|
|
return QLatin1String("DragonFlyBSD");
|
2014-04-29 13:02:44 +04:00
|
|
|
#elif defined(Q_OS_FREEBSD) || defined(Q_OS_FREEBSD_KERNEL)
|
2013-05-28 21:35:30 +04:00
|
|
|
return QLatin1String("FreeBSD");
|
|
|
|
#elif defined(Q_OS_NETBSD)
|
|
|
|
return QLatin1String("NetBSD");
|
|
|
|
#elif defined(Q_OS_OPENBSD)
|
|
|
|
return QLatin1String("OpenBSD");
|
|
|
|
#elif defined(Q_OS_SOLARIS)
|
2013-10-23 02:59:29 +04:00
|
|
|
return QLatin1String("Solaris");
|
2013-05-28 21:35:30 +04:00
|
|
|
#else
|
2013-10-23 02:59:29 +04:00
|
|
|
return QLatin1String("Unknown OS");
|
2013-05-28 21:35:30 +04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-05-29 18:13:38 +04:00
|
|
|
QByteArray Utility::userAgentString()
|
|
|
|
{
|
|
|
|
return QString::fromLatin1("Mozilla/5.0 (%1) mirall/%2")
|
|
|
|
.arg(Utility::platform())
|
|
|
|
.arg(QLatin1String(MIRALL_STRINGIFY(MIRALL_VERSION)))
|
|
|
|
.toLatin1();
|
2012-12-14 00:36:44 +04:00
|
|
|
}
|
2013-05-29 18:13:38 +04:00
|
|
|
|
2013-07-07 00:38:33 +04:00
|
|
|
bool Utility::hasLaunchOnStartup(const QString &appName)
|
2013-07-05 02:25:19 +04:00
|
|
|
{
|
2013-10-02 21:06:33 +04:00
|
|
|
return hasLaunchOnStartup_private(appName);
|
2013-07-07 00:38:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Utility::setLaunchOnStartup(const QString &appName, const QString& guiName, bool enable)
|
2013-07-05 02:25:19 +04:00
|
|
|
{
|
2013-10-02 21:06:33 +04:00
|
|
|
setLaunchOnStartup_private(appName, guiName, enable);
|
2013-07-05 02:25:19 +04:00
|
|
|
}
|
|
|
|
|
2013-07-07 03:23:25 +04:00
|
|
|
qint64 Utility::freeDiskSpace(const QString &path, bool *ok)
|
|
|
|
{
|
2014-12-21 17:00:56 +03:00
|
|
|
#if defined(Q_OS_MAC) || defined(Q_OS_FREEBSD) || defined(Q_OS_FREEBSD_KERNEL) || defined(Q_OS_NETBSD)
|
2013-07-09 16:07:48 +04:00
|
|
|
struct statvfs stat;
|
|
|
|
statvfs(path.toUtf8().data(), &stat);
|
|
|
|
return (qint64) stat.f_bavail * stat.f_frsize;
|
2013-07-09 16:23:58 +04:00
|
|
|
#elif defined(Q_OS_UNIX)
|
2013-07-07 03:23:25 +04:00
|
|
|
Q_UNUSED(ok)
|
|
|
|
struct statvfs64 stat;
|
|
|
|
statvfs64(path.toUtf8().data(), &stat);
|
|
|
|
return (qint64) stat.f_bavail * stat.f_frsize;
|
|
|
|
#elif defined(Q_OS_WIN)
|
|
|
|
ULARGE_INTEGER freeBytes;
|
|
|
|
freeBytes.QuadPart = 0L;
|
2014-09-12 18:08:25 +04:00
|
|
|
if( !GetDiskFreeSpaceEx( reinterpret_cast<const wchar_t *>(path.utf16()), &freeBytes, NULL, NULL ) ) {
|
2013-07-07 03:23:25 +04:00
|
|
|
if (ok) *ok = false;
|
|
|
|
}
|
|
|
|
return freeBytes.QuadPart;
|
|
|
|
#else
|
|
|
|
if (ok) *ok = false;
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-07-24 16:36:38 +04:00
|
|
|
QString Utility::compactFormatDouble(double value, int prec, const QString& unit)
|
2013-07-10 11:19:41 +04:00
|
|
|
{
|
|
|
|
QLocale locale = QLocale::system();
|
|
|
|
QChar decPoint = locale.decimalPoint();
|
|
|
|
QString str = locale.toString(value, 'f', prec);
|
|
|
|
while (str.endsWith('0') || str.endsWith(decPoint)) {
|
|
|
|
if (str.endsWith(decPoint)) {
|
|
|
|
str.chop(1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
str.chop(1);
|
|
|
|
}
|
2013-07-24 16:36:38 +04:00
|
|
|
if( !unit.isEmpty() )
|
|
|
|
str += (QLatin1Char(' ')+unit);
|
2013-07-10 11:19:41 +04:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2013-08-05 21:45:16 +04:00
|
|
|
QString Utility::toCSyncScheme(const QString &urlStr)
|
|
|
|
{
|
|
|
|
|
|
|
|
QUrl url( urlStr );
|
|
|
|
if( url.scheme() == QLatin1String("http") ) {
|
|
|
|
url.setScheme( QLatin1String("owncloud") );
|
|
|
|
} else {
|
|
|
|
// connect SSL!
|
|
|
|
url.setScheme( QLatin1String("ownclouds") );
|
|
|
|
}
|
|
|
|
return url.toString();
|
|
|
|
}
|
|
|
|
|
2014-10-22 15:29:28 +04:00
|
|
|
bool Utility::doesSetContainPrefix(const QSet<QString> &l, const QString &p) {
|
2014-10-13 19:23:42 +04:00
|
|
|
|
|
|
|
Q_FOREACH (const QString &setPath, l) {
|
|
|
|
//qDebug() << Q_FUNC_INFO << p << setPath << setPath.startsWith(p);
|
|
|
|
if (setPath.startsWith(p)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//qDebug() << "-> NOOOOO!!!" << p << l.count();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-08-24 16:57:55 +04:00
|
|
|
QString Utility::escape(const QString &in)
|
|
|
|
{
|
|
|
|
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
|
|
|
|
return Qt::escape(in);
|
|
|
|
#else
|
|
|
|
return in.toHtmlEscaped();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-12-10 16:47:44 +04:00
|
|
|
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
|
|
|
|
// In Qt 4, QThread::sleep functions are protected.
|
|
|
|
// This is a hack to make them visible in this namespace.
|
|
|
|
struct QThread : ::QThread {
|
|
|
|
using ::QThread::sleep;
|
|
|
|
using ::QThread::usleep;
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2013-09-10 13:35:30 +04:00
|
|
|
void Utility::sleep(int sec)
|
|
|
|
{
|
2013-12-10 16:47:44 +04:00
|
|
|
QThread::sleep(sec);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Utility::usleep(int usec)
|
|
|
|
{
|
|
|
|
QThread::usleep(usec);
|
2013-09-10 13:35:30 +04:00
|
|
|
}
|
|
|
|
|
2014-05-23 17:59:29 +04:00
|
|
|
bool Utility::fsCasePreserving()
|
|
|
|
{
|
|
|
|
bool re = false;
|
|
|
|
if( isWindows() || isMac() ) {
|
|
|
|
re = true;
|
2014-07-07 15:00:38 +04:00
|
|
|
} else {
|
|
|
|
static bool isTest = qgetenv("OWNCLOUD_TEST_CASE_PRESERVING").toInt();
|
|
|
|
re = isTest;
|
2014-05-23 17:59:29 +04:00
|
|
|
}
|
|
|
|
return re;
|
|
|
|
}
|
|
|
|
|
2014-01-29 14:39:14 +04:00
|
|
|
QDateTime Utility::qDateTimeFromTime_t(qint64 t)
|
|
|
|
{
|
|
|
|
return QDateTime::fromMSecsSinceEpoch(t * 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
qint64 Utility::qDateTimeToTime_t(const QDateTime& t)
|
|
|
|
{
|
|
|
|
return t.toMSecsSinceEpoch() / 1000;
|
|
|
|
}
|
|
|
|
|
2014-05-25 23:43:41 +04:00
|
|
|
//TODO change to initializers list when possible.
|
|
|
|
static QList<QPair<QString,quint32> > timeMapping = QList<QPair<QString,quint32> >() <<
|
2014-05-28 14:09:30 +04:00
|
|
|
QPair<QString,quint32>("%1 years",86400*365) <<
|
|
|
|
QPair<QString,quint32>("%1 months",86400*30) <<
|
|
|
|
QPair<QString,quint32>("%1 days",86400) <<
|
|
|
|
QPair<QString,quint32>("%1h",3600) <<
|
|
|
|
QPair<QString,quint32>("%1m",60) <<
|
|
|
|
QPair<QString,quint32>("%1s",1);
|
2014-05-29 01:09:02 +04:00
|
|
|
|
|
|
|
|
2014-05-25 23:43:41 +04:00
|
|
|
QString Utility::timeToDescriptiveString(quint64 msecs, quint8 precision, QString separator, bool specific)
|
|
|
|
{
|
2014-05-29 01:09:02 +04:00
|
|
|
return timeToDescriptiveString( timeMapping , msecs, precision, separator, specific);
|
2014-05-14 11:55:04 +04:00
|
|
|
}
|
|
|
|
|
2014-05-25 23:43:41 +04:00
|
|
|
|
|
|
|
QString Utility::timeToDescriptiveString(QList<QPair<QString,quint32> > &timeMapping, quint64 msecs, quint8 precision, QString separator, bool specific)
|
2014-05-14 11:55:04 +04:00
|
|
|
{
|
2014-05-12 18:36:27 +04:00
|
|
|
quint64 secs = msecs / 1000;
|
2014-05-28 14:09:30 +04:00
|
|
|
QString retStr = QString(timeMapping.last().first).arg(0); // default value in case theres no actual time in msecs.
|
2014-05-25 23:43:41 +04:00
|
|
|
QList<QPair<QString,quint32> > values;
|
2014-05-20 17:56:39 +04:00
|
|
|
bool timeStartFound = false;
|
2014-05-14 11:55:04 +04:00
|
|
|
|
2014-05-20 17:56:39 +04:00
|
|
|
for(QList<QPair<QString,quint32> >::Iterator itr = timeMapping.begin(); itr != timeMapping.end() && precision > 0; itr++) {
|
2014-05-14 11:55:04 +04:00
|
|
|
quint64 result = secs / itr->second;
|
2014-05-20 17:56:39 +04:00
|
|
|
if(!timeStartFound) {
|
2014-05-14 11:55:04 +04:00
|
|
|
if(result == 0 ) {
|
|
|
|
continue;
|
2014-05-25 23:43:41 +04:00
|
|
|
}
|
|
|
|
retStr = "";
|
2014-05-20 17:56:39 +04:00
|
|
|
timeStartFound= true;
|
2014-05-14 11:55:04 +04:00
|
|
|
}
|
|
|
|
secs -= result * itr->second;
|
2014-05-25 23:43:41 +04:00
|
|
|
values.append(QPair<QString,quint32>(itr->first,result));
|
2014-05-20 17:56:39 +04:00
|
|
|
precision--;
|
2014-05-14 11:55:04 +04:00
|
|
|
}
|
2014-05-12 18:36:27 +04:00
|
|
|
|
2014-05-25 23:43:41 +04:00
|
|
|
for(QList<QPair<QString,quint32> >::Iterator itr = values.begin(); itr < values.end(); itr++) {
|
2014-05-28 14:09:30 +04:00
|
|
|
retStr = retStr.append((specific || itr == values.end()-1) ? itr->first : "%1").arg(itr->second, (itr == values.begin() ? 1 :2 ), 10, QChar('0'));
|
2014-05-25 23:43:41 +04:00
|
|
|
if(itr < values.end()-1) {
|
|
|
|
retStr.append(separator);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-12 18:36:27 +04:00
|
|
|
}
|
|
|
|
|
2014-05-14 11:55:04 +04:00
|
|
|
return retStr;
|
2014-04-25 02:08:25 +04:00
|
|
|
}
|
2014-01-29 14:39:14 +04:00
|
|
|
|
2014-10-24 23:52:41 +04:00
|
|
|
bool Utility::hasDarkSystray()
|
|
|
|
{
|
|
|
|
return hasDarkSystray_private();
|
|
|
|
}
|
|
|
|
|
2014-01-29 14:39:14 +04:00
|
|
|
|
2013-10-02 20:16:24 +04:00
|
|
|
bool Utility::isWindows()
|
|
|
|
{
|
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
return true;
|
|
|
|
#else
|
|
|
|
return false;
|
2013-10-02 13:55:36 +04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-10-02 20:16:24 +04:00
|
|
|
bool Utility::isMac()
|
|
|
|
{
|
|
|
|
#ifdef Q_OS_MAC
|
|
|
|
return true;
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Utility::isUnix()
|
|
|
|
{
|
|
|
|
#ifdef Q_OS_UNIX
|
|
|
|
return true;
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-01-15 14:08:42 +04:00
|
|
|
bool Utility::isLinux()
|
|
|
|
{
|
2015-01-09 00:40:47 +03:00
|
|
|
#if defined(Q_OS_LINUX)
|
2014-01-15 14:08:42 +04:00
|
|
|
return true;
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-01-09 00:40:47 +03:00
|
|
|
bool Utility::isBSD()
|
|
|
|
{
|
|
|
|
#if defined(Q_OS_FREEBSD) || defined(Q_OS_NETBSD)
|
|
|
|
return true;
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-08-26 17:46:24 +04:00
|
|
|
void Utility::crash()
|
|
|
|
{
|
|
|
|
volatile int* a = (int*)(NULL);
|
|
|
|
*a = 1;
|
|
|
|
}
|
2014-08-07 16:58:06 +04:00
|
|
|
|
2014-03-28 12:50:13 +04:00
|
|
|
static const char STOPWATCH_END_TAG[] = "_STOPWATCH_END";
|
2014-03-26 21:00:02 +04:00
|
|
|
|
|
|
|
void Utility::StopWatch::start()
|
|
|
|
{
|
|
|
|
_startTime = QDateTime::currentDateTime();
|
|
|
|
_timer.start();
|
|
|
|
}
|
|
|
|
|
2014-12-04 19:57:29 +03:00
|
|
|
quint64 Utility::StopWatch::stop()
|
2014-03-26 21:00:02 +04:00
|
|
|
{
|
|
|
|
addLapTime(QLatin1String(STOPWATCH_END_TAG));
|
2014-12-04 19:57:29 +03:00
|
|
|
quint64 duration = _timer.elapsed();
|
2014-03-26 21:00:02 +04:00
|
|
|
_timer.invalidate();
|
2014-12-04 19:57:29 +03:00
|
|
|
return duration;
|
2014-03-26 21:00:02 +04:00
|
|
|
}
|
|
|
|
|
2014-04-15 18:10:46 +04:00
|
|
|
void Utility::StopWatch::reset()
|
|
|
|
{
|
|
|
|
_timer.invalidate();
|
|
|
|
_startTime.setMSecsSinceEpoch(0);
|
|
|
|
_lapTimes.clear();
|
|
|
|
}
|
|
|
|
|
2014-03-26 21:00:02 +04:00
|
|
|
quint64 Utility::StopWatch::addLapTime( const QString& lapName )
|
|
|
|
{
|
|
|
|
if( !_timer.isValid() ) {
|
|
|
|
start();
|
|
|
|
}
|
|
|
|
quint64 re = _timer.elapsed();
|
2014-03-28 12:50:13 +04:00
|
|
|
_lapTimes[lapName] = re;
|
2014-03-26 21:00:02 +04:00
|
|
|
return re;
|
|
|
|
}
|
|
|
|
|
2014-03-28 12:39:32 +04:00
|
|
|
QDateTime Utility::StopWatch::startTime() const
|
2014-03-26 21:00:02 +04:00
|
|
|
{
|
|
|
|
return _startTime;
|
|
|
|
}
|
|
|
|
|
2014-03-28 12:39:32 +04:00
|
|
|
QDateTime Utility::StopWatch::timeOfLap( const QString& lapName ) const
|
2014-03-26 21:00:02 +04:00
|
|
|
{
|
|
|
|
quint64 t = durationOfLap(lapName);
|
|
|
|
if( t ) {
|
|
|
|
QDateTime re(_startTime);
|
|
|
|
return re.addMSecs(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
return QDateTime();
|
|
|
|
}
|
|
|
|
|
2014-03-28 12:39:32 +04:00
|
|
|
quint64 Utility::StopWatch::durationOfLap( const QString& lapName ) const
|
2014-03-26 21:00:02 +04:00
|
|
|
{
|
2014-03-28 12:50:13 +04:00
|
|
|
return _lapTimes.value(lapName, 0);
|
2014-03-26 21:00:02 +04:00
|
|
|
}
|
|
|
|
|
2014-11-10 00:34:07 +03:00
|
|
|
} // namespace OCC
|