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
|
|
|
*
|
2017-08-16 09:36:52 +03:00
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2012-12-14 00:36:44 +04:00
|
|
|
*
|
2017-08-16 09:36:52 +03:00
|
|
|
* This library 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2012-12-14 00:36:44 +04:00
|
|
|
*/
|
2015-07-30 17:38:22 +03:00
|
|
|
#include "config.h"
|
2012-12-14 00:36:44 +04:00
|
|
|
|
2017-08-16 09:36:52 +03:00
|
|
|
#include "common/utility.h"
|
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>
|
2013-10-02 20:16:24 +04:00
|
|
|
#include <QProcess>
|
2015-11-16 20:08:25 +03:00
|
|
|
#include <QObject>
|
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
|
2016-05-19 16:36:46 +03:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 2, 0)
|
|
|
|
#include <QCollator>
|
|
|
|
#endif
|
2016-07-28 17:30:40 +03:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 4, 0)
|
|
|
|
#include <QSysInfo>
|
|
|
|
#endif
|
2016-05-19 16:36:46 +03:00
|
|
|
|
2013-08-24 16:57:55 +04:00
|
|
|
|
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
|
|
|
|
|
2015-12-02 19:56:31 +03:00
|
|
|
#include <math.h>
|
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
|
|
|
|
2017-05-09 15:24:11 +03:00
|
|
|
Q_LOGGING_CATEGORY(lcUtility, "sync.utility", QtInfoMsg)
|
|
|
|
|
2017-05-17 11:55:42 +03:00
|
|
|
bool Utility::writeRandomFile(const QString &fname, int size)
|
2014-01-08 17:34:51 +04:00
|
|
|
{
|
2017-05-17 11:55:42 +03:00
|
|
|
int maxSize = 10 * 10 * 1024;
|
2014-01-08 17:34:51 +04:00
|
|
|
qsrand(QDateTime::currentMSecsSinceEpoch());
|
|
|
|
|
2017-05-17 11:55:42 +03:00
|
|
|
if (size == -1)
|
|
|
|
size = qrand() % maxSize;
|
2014-01-08 17:34:51 +04:00
|
|
|
|
|
|
|
QString randString;
|
2017-05-17 11:55:42 +03:00
|
|
|
for (int i = 0; i < size; i++) {
|
2014-01-08 17:34:51 +04:00
|
|
|
int r = qrand() % 128;
|
|
|
|
randString.append(QChar(r));
|
|
|
|
}
|
|
|
|
|
|
|
|
QFile file(fname);
|
2017-05-17 11:55:42 +03:00
|
|
|
if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
|
2014-01-08 17:34:51 +04:00
|
|
|
QTextStream out(&file);
|
|
|
|
out << randString;
|
2017-05-17 11:55:42 +03:00
|
|
|
// optional, as QFile destructor will already do it:
|
2014-01-08 17:34:51 +04:00
|
|
|
file.close();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:55:42 +03:00
|
|
|
QString Utility::formatFingerprint(const QByteArray &fmhash, bool colonSeparated)
|
2012-12-14 00:36:44 +04:00
|
|
|
{
|
|
|
|
QByteArray hash;
|
2017-05-17 11:55:42 +03:00
|
|
|
int steps = fmhash.length() / 2;
|
2012-12-14 00:36:44 +04:00
|
|
|
for (int i = 0; i < steps; i++) {
|
2017-05-17 11:55:42 +03:00
|
|
|
hash.append(fmhash[i * 2]);
|
|
|
|
hash.append(fmhash[i * 2 + 1]);
|
2012-12-14 00:36:44 +04:00
|
|
|
hash.append(' ');
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:55:42 +03: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
|
|
|
}
|
|
|
|
|
2017-05-17 11:55:42 +03:00
|
|
|
QString Utility::octetsToString(qint64 octets)
|
2013-07-04 21:59:40 +04:00
|
|
|
{
|
2015-09-10 13:08:20 +03:00
|
|
|
#define THE_FACTOR 1024
|
|
|
|
static const qint64 kb = THE_FACTOR;
|
|
|
|
static const qint64 mb = THE_FACTOR * kb;
|
|
|
|
static const qint64 gb = THE_FACTOR * mb;
|
2013-07-20 03:27:41 +04:00
|
|
|
|
2013-08-29 14:06:41 +04:00
|
|
|
QString s;
|
|
|
|
qreal value = octets;
|
2015-09-10 18:31:44 +03:00
|
|
|
|
2017-04-04 10:54:06 +03:00
|
|
|
// Whether we care about decimals: only for GB/MB and only
|
|
|
|
// if it's less than 10 units.
|
2016-01-21 11:50:32 +03:00
|
|
|
bool round = true;
|
|
|
|
|
2015-09-10 18:31:44 +03:00
|
|
|
// do not display terra byte with the current units, as when
|
|
|
|
// the MB, GB and KB units were made, there was no TB,
|
|
|
|
// see the JEDEC standard
|
|
|
|
// https://en.wikipedia.org/wiki/JEDEC_memory_standards
|
|
|
|
if (octets >= gb) {
|
2015-08-05 17:22:09 +03:00
|
|
|
s = QCoreApplication::translate("Utility", "%L1 GB");
|
2013-08-29 14:06:41 +04:00
|
|
|
value /= gb;
|
2016-01-21 11:50:32 +03:00
|
|
|
round = false;
|
2013-07-04 21:59:40 +04:00
|
|
|
} else if (octets >= mb) {
|
2015-08-05 17:22:09 +03:00
|
|
|
s = QCoreApplication::translate("Utility", "%L1 MB");
|
2013-08-29 14:06:41 +04:00
|
|
|
value /= mb;
|
2017-04-04 10:54:06 +03:00
|
|
|
round = false;
|
2013-07-04 21:59:40 +04:00
|
|
|
} else if (octets >= kb) {
|
2015-09-10 18:31:44 +03:00
|
|
|
s = QCoreApplication::translate("Utility", "%L1 KB");
|
2013-08-29 14:06:41 +04:00
|
|
|
value /= kb;
|
2017-05-17 11:55:42 +03:00
|
|
|
} else {
|
2013-08-29 14:06:41 +04:00
|
|
|
s = QCoreApplication::translate("Utility", "%L1 B");
|
2013-07-04 21:59:40 +04:00
|
|
|
}
|
2013-08-29 14:06:41 +04:00
|
|
|
|
2016-01-21 11:50:32 +03:00
|
|
|
if (value > 9.95)
|
|
|
|
round = true;
|
|
|
|
|
|
|
|
if (round)
|
|
|
|
return s.arg(qRound(value));
|
|
|
|
|
|
|
|
return 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
|
2015-06-15 18:38:12 +03:00
|
|
|
static QLatin1String platform()
|
2013-05-28 21:35:30 +04:00
|
|
|
{
|
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()
|
|
|
|
{
|
2015-05-04 18:42:31 +03:00
|
|
|
QString re = QString::fromLatin1("Mozilla/5.0 (%1) mirall/%2")
|
2017-02-23 16:54:17 +03:00
|
|
|
.arg(platform(), QLatin1String(MIRALL_VERSION_STRING));
|
2015-05-04 18:42:31 +03:00
|
|
|
|
2015-06-15 18:38:12 +03:00
|
|
|
QLatin1String appName(APPLICATION_SHORTNAME);
|
2015-05-04 18:42:31 +03:00
|
|
|
|
|
|
|
// this constant "ownCloud" is defined in the default OEM theming
|
|
|
|
// that is used for the standard client. If it is changed there,
|
|
|
|
// it needs to be adjusted here.
|
2017-05-17 11:55:42 +03:00
|
|
|
if (appName != QLatin1String("ownCloud")) {
|
2015-05-04 18:42:31 +03:00
|
|
|
re += QString(" (%1)").arg(appName);
|
|
|
|
}
|
|
|
|
return re.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
|
|
|
}
|
|
|
|
|
2017-05-17 11:55:42 +03: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
|
|
|
}
|
|
|
|
|
2015-09-30 16:07:45 +03:00
|
|
|
qint64 Utility::freeDiskSpace(const QString &path)
|
2013-07-07 03:23:25 +04:00
|
|
|
{
|
2015-08-13 09:54:53 +03:00
|
|
|
#if defined(Q_OS_MAC) || defined(Q_OS_FREEBSD) || defined(Q_OS_FREEBSD_KERNEL) || defined(Q_OS_NETBSD) || defined(Q_OS_OPENBSD)
|
2013-07-09 16:07:48 +04:00
|
|
|
struct statvfs stat;
|
2015-09-30 16:07:45 +03:00
|
|
|
if (statvfs(path.toLocal8Bit().data(), &stat) == 0) {
|
2017-05-17 11:55:42 +03:00
|
|
|
return (qint64)stat.f_bavail * stat.f_frsize;
|
2015-09-30 16:07:45 +03:00
|
|
|
}
|
2013-07-09 16:23:58 +04:00
|
|
|
#elif defined(Q_OS_UNIX)
|
2013-07-07 03:23:25 +04:00
|
|
|
struct statvfs64 stat;
|
2015-09-30 16:07:45 +03:00
|
|
|
if (statvfs64(path.toLocal8Bit().data(), &stat) == 0) {
|
2017-05-17 11:55:42 +03:00
|
|
|
return (qint64)stat.f_bavail * stat.f_frsize;
|
2015-09-30 16:07:45 +03:00
|
|
|
}
|
2013-07-07 03:23:25 +04:00
|
|
|
#elif defined(Q_OS_WIN)
|
|
|
|
ULARGE_INTEGER freeBytes;
|
|
|
|
freeBytes.QuadPart = 0L;
|
2017-05-17 11:55:42 +03:00
|
|
|
if (GetDiskFreeSpaceEx(reinterpret_cast<const wchar_t *>(path.utf16()), &freeBytes, NULL, NULL)) {
|
2015-09-30 16:07:45 +03:00
|
|
|
return freeBytes.QuadPart;
|
2013-07-07 03:23:25 +04:00
|
|
|
}
|
|
|
|
#endif
|
2015-09-30 16:07:45 +03:00
|
|
|
return -1;
|
2013-07-07 03:23:25 +04:00
|
|
|
}
|
|
|
|
|
2017-05-17 11:55:42 +03: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);
|
|
|
|
}
|
2017-05-17 11:55:42 +03:00
|
|
|
if (!unit.isEmpty())
|
|
|
|
str += (QLatin1Char(' ') + unit);
|
2013-07-10 11:19:41 +04:00
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
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.
|
2017-05-17 11:55:42 +03:00
|
|
|
struct QThread : ::QThread
|
|
|
|
{
|
2013-12-10 16:47:44 +04:00
|
|
|
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()
|
|
|
|
{
|
2017-08-14 19:49:44 +03:00
|
|
|
#ifdef WITH_TESTING
|
2017-01-27 20:12:18 +03:00
|
|
|
QByteArray env = qgetenv("OWNCLOUD_TEST_CASE_PRESERVING");
|
|
|
|
if (!env.isEmpty())
|
|
|
|
return env.toInt();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return isWindows() || isMac();
|
2014-05-23 17:59:29 +04:00
|
|
|
}
|
|
|
|
|
2017-05-17 11:55:42 +03:00
|
|
|
bool Utility::fileNamesEqual(const QString &fn1, const QString &fn2)
|
2016-10-12 15:48:00 +03:00
|
|
|
{
|
|
|
|
const QDir fd1(fn1);
|
|
|
|
const QDir fd2(fn2);
|
|
|
|
|
|
|
|
// Attention: If the path does not exist, canonicalPath returns ""
|
|
|
|
// ONLY use this function with existing pathes.
|
|
|
|
const QString a = fd1.canonicalPath();
|
2017-05-17 11:55:42 +03:00
|
|
|
const QString b = fd2.canonicalPath();
|
|
|
|
bool re = !a.isEmpty() && QString::compare(a, b, fsCasePreserving() ? Qt::CaseInsensitive : Qt::CaseSensitive) == 0;
|
2016-10-12 15:48:00 +03:00
|
|
|
return re;
|
|
|
|
}
|
|
|
|
|
2014-01-29 14:39:14 +04:00
|
|
|
QDateTime Utility::qDateTimeFromTime_t(qint64 t)
|
|
|
|
{
|
|
|
|
return QDateTime::fromMSecsSinceEpoch(t * 1000);
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:55:42 +03:00
|
|
|
qint64 Utility::qDateTimeToTime_t(const QDateTime &t)
|
2014-01-29 14:39:14 +04:00
|
|
|
{
|
|
|
|
return t.toMSecsSinceEpoch() / 1000;
|
|
|
|
}
|
|
|
|
|
2016-01-21 11:53:58 +03:00
|
|
|
namespace {
|
|
|
|
struct Period
|
|
|
|
{
|
|
|
|
const char *name;
|
|
|
|
quint64 msec;
|
|
|
|
|
|
|
|
QString description(quint64 value) const
|
|
|
|
{
|
|
|
|
return QCoreApplication::translate(
|
2017-05-17 11:55:42 +03:00
|
|
|
"Utility", name, 0, QCoreApplication::UnicodeUTF8,
|
|
|
|
value);
|
2016-01-21 11:53:58 +03:00
|
|
|
}
|
|
|
|
};
|
2016-05-26 13:15:53 +03:00
|
|
|
// QTBUG-3945 and issue #4855: QT_TRANSLATE_NOOP does not work with plural form because lupdate
|
|
|
|
// limitation unless we fake more arguments
|
|
|
|
// (it must be in the form ("context", "source", "comment", n)
|
|
|
|
#undef QT_TRANSLATE_NOOP
|
|
|
|
#define QT_TRANSLATE_NOOP(ctx, str, ...) str
|
2015-07-02 12:40:16 +03:00
|
|
|
Q_DECL_CONSTEXPR Period periods[] = {
|
2017-05-17 11:55:42 +03:00
|
|
|
{ QT_TRANSLATE_NOOP("Utility", "%n year(s)", 0, _), 365 * 24 * 3600 * 1000LL },
|
|
|
|
{ QT_TRANSLATE_NOOP("Utility", "%n month(s)", 0, _), 30 * 24 * 3600 * 1000LL },
|
|
|
|
{ QT_TRANSLATE_NOOP("Utility", "%n day(s)", 0, _), 24 * 3600 * 1000LL },
|
|
|
|
{ QT_TRANSLATE_NOOP("Utility", "%n hour(s)", 0, _), 3600 * 1000LL },
|
|
|
|
{ QT_TRANSLATE_NOOP("Utility", "%n minute(s)", 0, _), 60 * 1000LL },
|
|
|
|
{ QT_TRANSLATE_NOOP("Utility", "%n second(s)", 0, _), 1000LL },
|
2015-06-29 15:53:37 +03:00
|
|
|
{ 0, 0 }
|
|
|
|
};
|
2016-01-21 11:53:58 +03:00
|
|
|
} // anonymous namespace
|
2015-06-29 15:53:37 +03:00
|
|
|
|
2016-01-21 11:53:58 +03:00
|
|
|
QString Utility::durationToDescriptiveString2(quint64 msecs)
|
|
|
|
{
|
2015-06-29 15:53:37 +03:00
|
|
|
int p = 0;
|
2017-05-17 11:55:42 +03:00
|
|
|
while (periods[p + 1].name && msecs < periods[p].msec) {
|
2015-06-29 15:53:37 +03:00
|
|
|
p++;
|
|
|
|
}
|
2014-05-25 23:43:41 +04:00
|
|
|
|
2016-01-21 11:53:58 +03:00
|
|
|
auto firstPart = periods[p].description(int(msecs / periods[p].msec));
|
2015-06-29 15:53:37 +03:00
|
|
|
|
2017-05-17 11:55:42 +03:00
|
|
|
if (!periods[p + 1].name) {
|
2015-06-29 15:53:37 +03:00
|
|
|
return firstPart;
|
2014-05-12 18:36:27 +04:00
|
|
|
}
|
2015-06-29 15:53:37 +03:00
|
|
|
|
2017-05-17 11:55:42 +03:00
|
|
|
quint64 secondPartNum = qRound(double(msecs % periods[p].msec) / periods[p + 1].msec);
|
2015-06-29 15:53:37 +03:00
|
|
|
|
|
|
|
if (secondPartNum == 0) {
|
|
|
|
return firstPart;
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:55:42 +03:00
|
|
|
return QCoreApplication::translate("Utility", "%1 %2").arg(firstPart, periods[p + 1].description(secondPartNum));
|
2016-01-21 11:53:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QString Utility::durationToDescriptiveString1(quint64 msecs)
|
|
|
|
{
|
|
|
|
int p = 0;
|
2017-05-17 11:55:42 +03:00
|
|
|
while (periods[p + 1].name && msecs < periods[p].msec) {
|
2016-01-21 11:53:58 +03:00
|
|
|
p++;
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:55:42 +03:00
|
|
|
quint64 amount = qRound(double(msecs) / periods[p].msec);
|
2016-01-21 11:53:58 +03:00
|
|
|
return periods[p].description(amount);
|
2014-04-25 02:08:25 +04:00
|
|
|
}
|
2014-01-29 14:39:14 +04:00
|
|
|
|
2017-05-17 11:55:42 +03:00
|
|
|
QString Utility::fileNameForGuiUse(const QString &fName)
|
2015-10-19 15:36:55 +03:00
|
|
|
{
|
2017-05-17 11:55:42 +03:00
|
|
|
if (isMac()) {
|
2015-10-19 15:36:55 +03:00
|
|
|
QString n(fName);
|
|
|
|
return n.replace(QChar(':'), QChar('/'));
|
|
|
|
}
|
|
|
|
return fName;
|
|
|
|
}
|
|
|
|
|
2017-09-05 17:12:32 +03:00
|
|
|
QByteArray Utility::normalizeEtag(QByteArray etag)
|
|
|
|
{
|
|
|
|
/* strip "XXXX-gzip" */
|
|
|
|
if(etag.startsWith('"') && etag.endsWith("-gzip\"")) {
|
|
|
|
etag.chop(6);
|
|
|
|
etag.remove(0, 1);
|
|
|
|
}
|
|
|
|
/* strip trailing -gzip */
|
|
|
|
if(etag.endsWith("-gzip")) {
|
|
|
|
etag.chop(5);
|
|
|
|
}
|
|
|
|
/* strip normal quotes */
|
|
|
|
if (etag.startsWith('"') && etag.endsWith('"')) {
|
|
|
|
etag.chop(1);
|
|
|
|
etag.remove(0, 1);
|
|
|
|
}
|
|
|
|
etag.squeeze();
|
|
|
|
return etag;
|
|
|
|
}
|
|
|
|
|
2014-10-24 23:52:41 +04:00
|
|
|
bool Utility::hasDarkSystray()
|
|
|
|
{
|
|
|
|
return hasDarkSystray_private();
|
|
|
|
}
|
|
|
|
|
2014-01-29 14:39:14 +04:00
|
|
|
|
2016-07-28 17:30:40 +03:00
|
|
|
QString Utility::platformName()
|
|
|
|
{
|
|
|
|
QString re("Windows");
|
|
|
|
|
|
|
|
#if QT_VERSION < QT_VERSION_CHECK(5, 4, 0)
|
2017-05-17 11:55:42 +03:00
|
|
|
if (isMac()) {
|
2016-07-28 17:30:40 +03:00
|
|
|
re = QLatin1String("MacOSX");
|
2017-05-17 11:55:42 +03:00
|
|
|
} else if (isLinux()) {
|
2016-07-28 17:30:40 +03:00
|
|
|
re = QLatin1String("Linux");
|
2017-05-17 11:55:42 +03:00
|
|
|
} else if (isBSD()) {
|
2016-07-28 17:30:40 +03:00
|
|
|
re = QLatin1String("BSD");
|
2017-05-17 11:55:42 +03:00
|
|
|
} else if (isUnix()) {
|
2016-07-28 17:30:40 +03:00
|
|
|
re = QLatin1String("Unix");
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
re = QSysInfo::prettyProductName();
|
|
|
|
#endif
|
|
|
|
return re;
|
|
|
|
}
|
2015-01-09 00:40:47 +03:00
|
|
|
|
2014-08-26 17:46:24 +04:00
|
|
|
void Utility::crash()
|
|
|
|
{
|
2017-05-17 11:55:42 +03:00
|
|
|
volatile int *a = (int *)(NULL);
|
2014-08-26 17:46:24 +04:00
|
|
|
*a = 1;
|
|
|
|
}
|
2014-08-07 16:58:06 +04:00
|
|
|
|
2015-07-16 19:16:16 +03:00
|
|
|
// read the output of the owncloud --version command from the owncloud
|
|
|
|
// version that is on disk. This works for most versions of the client,
|
|
|
|
// because clients that do not yet know the --version flag return the
|
|
|
|
// version in the first line of the help output :-)
|
|
|
|
//
|
|
|
|
// This version only delivers output on linux, as Mac and Win get their
|
|
|
|
// restarting from the installer.
|
2017-05-17 11:55:42 +03:00
|
|
|
QByteArray Utility::versionOfInstalledBinary(const QString &command)
|
2015-07-16 19:16:16 +03:00
|
|
|
{
|
|
|
|
QByteArray re;
|
2017-05-17 11:55:42 +03:00
|
|
|
if (isLinux()) {
|
2015-07-16 19:16:16 +03:00
|
|
|
QString binary(command);
|
2017-05-17 11:55:42 +03:00
|
|
|
if (binary.isEmpty()) {
|
2015-07-16 19:16:16 +03:00
|
|
|
binary = qApp->arguments()[0];
|
|
|
|
}
|
|
|
|
QStringList params;
|
|
|
|
params << QLatin1String("--version");
|
|
|
|
QProcess process;
|
|
|
|
process.start(binary, params);
|
|
|
|
process.waitForFinished(); // sets current thread to sleep and waits for pingProcess end
|
|
|
|
re = process.readAllStandardOutput();
|
|
|
|
int newline = re.indexOf(QChar('\n'));
|
2017-05-17 11:55:42 +03:00
|
|
|
if (newline > 0) {
|
|
|
|
re.truncate(newline);
|
2015-07-16 19:16:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return re;
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:55:42 +03:00
|
|
|
QString Utility::timeAgoInWords(const QDateTime &dt, const QDateTime &from)
|
2015-11-16 20:08:25 +03:00
|
|
|
{
|
2016-03-18 10:20:23 +03:00
|
|
|
QDateTime now = QDateTime::currentDateTimeUtc();
|
2015-11-16 20:08:25 +03:00
|
|
|
|
2017-05-17 11:55:42 +03:00
|
|
|
if (from.isValid()) {
|
2015-11-16 20:08:25 +03:00
|
|
|
now = from;
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:55:42 +03:00
|
|
|
if (dt.daysTo(now) > 0) {
|
2015-11-16 20:08:25 +03:00
|
|
|
int dtn = dt.daysTo(now);
|
2016-03-18 17:28:00 +03:00
|
|
|
return QObject::tr("%n day(s) ago", "", dtn);
|
2015-11-16 20:08:25 +03:00
|
|
|
} else {
|
|
|
|
qint64 secs = dt.secsTo(now);
|
2017-05-17 11:55:42 +03:00
|
|
|
if (secs < 0) {
|
2016-02-15 19:58:32 +03:00
|
|
|
return QObject::tr("in the future");
|
|
|
|
}
|
2017-05-17 11:55:42 +03:00
|
|
|
if (floor(secs / 3600.0) > 0) {
|
|
|
|
int hours = floor(secs / 3600.0);
|
|
|
|
return (QObject::tr("%n hour(s) ago", "", hours));
|
2015-11-16 20:08:25 +03:00
|
|
|
} else {
|
2017-05-17 11:55:42 +03:00
|
|
|
int minutes = qRound(secs / 60.0);
|
|
|
|
if (minutes == 0) {
|
|
|
|
if (secs < 5) {
|
2016-02-15 19:58:32 +03:00
|
|
|
return QObject::tr("now");
|
|
|
|
} else {
|
|
|
|
return QObject::tr("Less than a minute ago");
|
|
|
|
}
|
2015-11-16 20:08:25 +03:00
|
|
|
}
|
2017-05-17 11:55:42 +03:00
|
|
|
return (QObject::tr("%n minute(s) ago", "", minutes));
|
2015-11-16 20:08:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return QObject::tr("Some time ago");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------- */
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:55:42 +03:00
|
|
|
quint64 Utility::StopWatch::addLapTime(const QString &lapName)
|
2014-03-26 21:00:02 +04:00
|
|
|
{
|
2017-05-17 11:55:42 +03:00
|
|
|
if (!_timer.isValid()) {
|
2014-03-26 21:00:02 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:55:42 +03:00
|
|
|
QDateTime Utility::StopWatch::timeOfLap(const QString &lapName) const
|
2014-03-26 21:00:02 +04:00
|
|
|
{
|
|
|
|
quint64 t = durationOfLap(lapName);
|
2017-05-17 11:55:42 +03:00
|
|
|
if (t) {
|
2014-03-26 21:00:02 +04:00
|
|
|
QDateTime re(_startTime);
|
|
|
|
return re.addMSecs(t);
|
|
|
|
}
|
|
|
|
|
|
|
|
return QDateTime();
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:55:42 +03: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
|
|
|
}
|
|
|
|
|
2017-05-17 11:55:42 +03:00
|
|
|
void Utility::sortFilenames(QStringList &fileNames)
|
2016-05-19 16:36:46 +03:00
|
|
|
{
|
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 2, 0)
|
|
|
|
QCollator collator;
|
|
|
|
collator.setNumericMode(true);
|
|
|
|
collator.setCaseSensitivity(Qt::CaseInsensitive);
|
|
|
|
qSort(fileNames.begin(), fileNames.end(), collator);
|
2016-06-07 11:05:57 +03:00
|
|
|
#elif QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
|
2016-05-19 16:36:46 +03:00
|
|
|
fileNames.sort(Qt::CaseInsensitive);
|
2016-06-07 11:05:57 +03:00
|
|
|
#else
|
|
|
|
fileNames.sort();
|
2016-05-19 16:36:46 +03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-10-25 13:04:22 +03:00
|
|
|
QUrl Utility::concatUrlPath(const QUrl &url, const QString &concatPath,
|
2017-05-17 11:55:42 +03:00
|
|
|
const QList<QPair<QString, QString>> &queryItems)
|
2016-10-25 13:04:22 +03:00
|
|
|
{
|
|
|
|
QString path = url.path();
|
2017-05-17 11:55:42 +03:00
|
|
|
if (!concatPath.isEmpty()) {
|
2016-10-25 13:04:22 +03:00
|
|
|
// avoid '//'
|
|
|
|
if (path.endsWith('/') && concatPath.startsWith('/')) {
|
|
|
|
path.chop(1);
|
|
|
|
} // avoid missing '/'
|
|
|
|
else if (!path.endsWith('/') && !concatPath.startsWith('/')) {
|
|
|
|
path += QLatin1Char('/');
|
|
|
|
}
|
|
|
|
path += concatPath; // put the complete path together
|
|
|
|
}
|
|
|
|
|
|
|
|
QUrl tmpUrl = url;
|
|
|
|
tmpUrl.setPath(path);
|
2017-05-17 11:55:42 +03:00
|
|
|
if (queryItems.size() > 0) {
|
2016-10-25 13:04:22 +03:00
|
|
|
tmpUrl.setQueryItems(queryItems);
|
|
|
|
}
|
|
|
|
return tmpUrl;
|
|
|
|
}
|
|
|
|
|
2014-11-10 00:34:07 +03:00
|
|
|
} // namespace OCC
|