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
|
|
|
|
2013-05-29 18:13:38 +04:00
|
|
|
#include "mirall/version.h"
|
|
|
|
|
2013-05-28 21:35:30 +04:00
|
|
|
#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-07-04 21:59:40 +04:00
|
|
|
#include <QWidget>
|
2013-03-21 15:01:37 +04:00
|
|
|
#include <QDebug>
|
2013-10-02 13:55:36 +04:00
|
|
|
#include <QDesktopServices>
|
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-03-21 15:01:37 +04:00
|
|
|
|
2013-08-24 16:57:55 +04:00
|
|
|
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
|
|
|
|
#include <QDesktopServices>
|
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)
|
|
|
|
#include "mirall/utility_win.cpp"
|
|
|
|
#elif defined(Q_OS_MAC)
|
|
|
|
#include "mirall/utility_mac.cpp"
|
|
|
|
#else
|
|
|
|
#include "mirall/utility_unix.cpp"
|
2013-03-21 15:01:37 +04:00
|
|
|
#endif
|
|
|
|
|
2012-12-14 00:36:44 +04:00
|
|
|
namespace Mirall {
|
|
|
|
|
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 )
|
|
|
|
{
|
2013-09-13 17:00:49 +04: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) {
|
2013-08-29 14:06:41 +04:00
|
|
|
s = QCoreApplication::translate("Utility", "%L1 TB");
|
|
|
|
value /= tb;
|
2013-07-20 03:27:41 +04:00
|
|
|
} else if (octets >= gb) {
|
2013-08-29 14:06:41 +04:00
|
|
|
s = QCoreApplication::translate("Utility", "%L1 GB");
|
|
|
|
value /= gb;
|
2013-07-04 21:59:40 +04:00
|
|
|
} else if (octets >= mb) {
|
2013-08-29 14:06:41 +04:00
|
|
|
s = QCoreApplication::translate("Utility", "%L1 MB");
|
|
|
|
value /= mb;
|
2013-07-04 21:59:40 +04:00
|
|
|
} else if (octets >= kb) {
|
2013-08-29 14:06:41 +04:00
|
|
|
s = QCoreApplication::translate("Utility", "%L1 kB");
|
|
|
|
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");
|
2013-05-28 21:35:30 +04:00
|
|
|
#elif defined(Q_OS_FREEBSD)
|
|
|
|
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-04 21:59:40 +04:00
|
|
|
void Utility::raiseDialog( QWidget *raiseWidget )
|
|
|
|
{
|
|
|
|
// viel hilft viel ;-)
|
|
|
|
if( raiseWidget ) {
|
2013-09-09 15:31:39 +04:00
|
|
|
#if defined(Q_OS_WIN) || defined (Q_OS_MAC)
|
2013-07-04 21:59:40 +04:00
|
|
|
Qt::WindowFlags eFlags = raiseWidget->windowFlags();
|
2014-02-14 06:02:59 +04:00
|
|
|
if (!(eFlags & Qt::WindowStaysOnTopHint)) {
|
|
|
|
eFlags |= Qt::WindowStaysOnTopHint;
|
|
|
|
raiseWidget->setWindowFlags(eFlags);
|
|
|
|
raiseWidget->show();
|
|
|
|
eFlags &= ~Qt::WindowStaysOnTopHint;
|
|
|
|
raiseWidget->setWindowFlags(eFlags);
|
|
|
|
}
|
2013-07-04 21:59:40 +04:00
|
|
|
#endif
|
|
|
|
raiseWidget->show();
|
|
|
|
raiseWidget->raise();
|
|
|
|
raiseWidget->activateWindow();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2013-10-16 17:57:59 +04:00
|
|
|
#if defined(Q_OS_MAC) || defined(Q_OS_FREEBSD)
|
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;
|
|
|
|
QString drive = QDir().absoluteFilePath(path).left(2);
|
|
|
|
if( !GetDiskFreeSpaceEx( reinterpret_cast<const wchar_t *>(drive.utf16()), &freeBytes, NULL, NULL ) ) {
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Utility::dataLocation()
|
|
|
|
{
|
2014-04-09 16:59:08 +04:00
|
|
|
// Qt 5's QStandardPaths::writableLocation gives us wrong results (without /data/),
|
|
|
|
// so we'll have to use the deprecated version for now
|
2013-08-24 16:57:55 +04:00
|
|
|
return QDesktopServices::storageLocation(QDesktopServices::DataLocation);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-10-02 20:16:24 +04:00
|
|
|
// ### helper functions for showInFileManager() ###
|
|
|
|
|
|
|
|
// according to the QStandardDir impl from Qt5
|
|
|
|
static QStringList xdgDataDirs()
|
2013-10-02 13:55:36 +04:00
|
|
|
{
|
2013-10-02 20:16:24 +04:00
|
|
|
QStringList dirs;
|
|
|
|
// http://standards.freedesktop.org/basedir-spec/latest/
|
|
|
|
QString xdgDataDirsEnv = QFile::decodeName(qgetenv("XDG_DATA_DIRS"));
|
|
|
|
if (xdgDataDirsEnv.isEmpty()) {
|
|
|
|
dirs.append(QString::fromLatin1("/usr/local/share"));
|
|
|
|
dirs.append(QString::fromLatin1("/usr/share"));
|
|
|
|
} else {
|
|
|
|
dirs = xdgDataDirsEnv.split(QLatin1Char(':'));
|
2013-10-02 13:55:36 +04:00
|
|
|
}
|
2013-10-02 20:16:24 +04:00
|
|
|
// local location
|
|
|
|
QString xdgDataHome = QFile::decodeName(qgetenv("XDG_DATA_HOME"));
|
|
|
|
if (xdgDataHome.isEmpty()) {
|
|
|
|
xdgDataHome = QDir::homePath()+"/.local/share";
|
|
|
|
}
|
|
|
|
dirs.prepend(xdgDataHome);
|
|
|
|
return dirs;
|
|
|
|
}
|
2013-10-02 13:55:36 +04:00
|
|
|
|
2013-10-02 20:16:24 +04:00
|
|
|
// Linux impl only, make sure to process %u and %U which might be returned
|
|
|
|
static QString findDefaultFileManager()
|
|
|
|
{
|
|
|
|
QProcess p;
|
|
|
|
p.start("xdg-mime", QStringList() << "query" << "default" << "inode/directory", QFile::ReadOnly);
|
|
|
|
p.waitForFinished();
|
|
|
|
QString fileName = QString::fromUtf8(p.readAll().trimmed());
|
|
|
|
if (fileName.isEmpty())
|
|
|
|
return QString();
|
|
|
|
|
|
|
|
QFileInfo fi;
|
|
|
|
QStringList dirs = xdgDataDirs();
|
2013-10-02 21:22:52 +04:00
|
|
|
QStringList subdirs;
|
|
|
|
subdirs << "/applications/" << "/applications/kde4/";
|
2013-10-02 20:16:24 +04:00
|
|
|
foreach(QString dir, dirs) {
|
2013-10-02 21:22:52 +04:00
|
|
|
foreach(QString subdir, subdirs) {
|
|
|
|
fi.setFile(dir + subdir + fileName);
|
2013-10-03 14:04:08 +04:00
|
|
|
if (fi.exists()) {
|
|
|
|
return fi.absoluteFilePath();
|
2013-10-02 21:22:52 +04:00
|
|
|
}
|
2013-10-02 20:16:24 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
// early dolphin versions did not have --select
|
|
|
|
static bool checkDolphinCanSelect()
|
|
|
|
{
|
|
|
|
QProcess p;
|
|
|
|
p.start("dolphin", QStringList() << "--help", QFile::ReadOnly);
|
|
|
|
p.waitForFinished();
|
|
|
|
return p.readAll().contains("--select");
|
|
|
|
}
|
|
|
|
|
|
|
|
// inspired by Qt Creator's showInGraphicalShell();
|
|
|
|
void Utility::showInFileManager(const QString &localPath)
|
|
|
|
{
|
|
|
|
if (isWindows()) {
|
2014-02-13 20:21:15 +04:00
|
|
|
#ifdef Q_OS_WIN
|
|
|
|
if (QSysInfo::windowsVersion() <= QSysInfo::WV_2003) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
2013-12-17 13:16:25 +04:00
|
|
|
QString explorer = "explorer.exe "; // FIXME: we trust it's in PATH
|
|
|
|
|
|
|
|
if (!QFileInfo(localPath).isDir()) {
|
|
|
|
explorer += QLatin1String("/select,");
|
|
|
|
}
|
2014-01-24 23:28:17 +04:00
|
|
|
explorer += QLatin1Char('"');
|
2013-12-17 13:16:25 +04:00
|
|
|
explorer += QDir::toNativeSeparators(localPath);
|
2014-01-24 23:28:17 +04:00
|
|
|
explorer += QLatin1Char('"');
|
2013-12-17 13:16:25 +04:00
|
|
|
|
|
|
|
qDebug() << "OO Open explorer commandline:" << explorer;
|
|
|
|
QProcess p;
|
|
|
|
p.start(explorer);
|
|
|
|
p.waitForFinished(5000);
|
2013-10-02 20:16:24 +04:00
|
|
|
} else if (isMac()) {
|
|
|
|
QStringList scriptArgs;
|
|
|
|
scriptArgs << QLatin1String("-e")
|
|
|
|
<< QString::fromLatin1("tell application \"Finder\" to reveal POSIX file \"%1\"")
|
|
|
|
.arg(localPath);
|
|
|
|
QProcess::execute(QLatin1String("/usr/bin/osascript"), scriptArgs);
|
|
|
|
scriptArgs.clear();
|
|
|
|
scriptArgs << QLatin1String("-e")
|
|
|
|
<< QLatin1String("tell application \"Finder\" to activate");
|
|
|
|
QProcess::execute(QLatin1String("/usr/bin/osascript"), scriptArgs);
|
2013-10-02 13:55:36 +04:00
|
|
|
} else {
|
2013-10-02 20:16:24 +04:00
|
|
|
QString app;
|
|
|
|
QStringList args;
|
|
|
|
|
2013-10-03 14:04:08 +04:00
|
|
|
static QString defaultManager = findDefaultFileManager();
|
|
|
|
QSettings desktopFile(defaultManager, QSettings::IniFormat);
|
|
|
|
QString exec = desktopFile.value("Desktop Entry/Exec").toString();
|
|
|
|
|
2013-10-02 20:16:24 +04:00
|
|
|
QString fileToOpen = QFileInfo(localPath).absoluteFilePath();
|
|
|
|
QString pathToOpen = QFileInfo(localPath).absolutePath();
|
2013-10-03 14:04:08 +04:00
|
|
|
bool canHandleFile = false; // assume dumb fm
|
2013-10-02 20:16:24 +04:00
|
|
|
|
2013-10-03 14:04:08 +04:00
|
|
|
args = exec.split(' ');
|
2013-10-02 20:16:24 +04:00
|
|
|
if (args.count() > 0) app = args.takeFirst();
|
|
|
|
|
|
|
|
QString kdeSelectParam("--select");
|
|
|
|
|
|
|
|
if (app.contains("konqueror") && !args.contains(kdeSelectParam)) {
|
|
|
|
// konq needs '--select' in order not to launch the file
|
|
|
|
args.prepend(kdeSelectParam);
|
2013-10-03 14:04:08 +04:00
|
|
|
canHandleFile = true;
|
2013-10-02 20:16:24 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (app.contains("dolphin"))
|
|
|
|
{
|
|
|
|
static bool dolphinCanSelect = checkDolphinCanSelect();
|
|
|
|
if (dolphinCanSelect && !args.contains(kdeSelectParam)) {
|
|
|
|
args.prepend(kdeSelectParam);
|
2013-10-03 14:04:08 +04:00
|
|
|
canHandleFile = true;
|
2013-10-02 20:16:24 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-03 14:04:08 +04:00
|
|
|
// whitelist
|
|
|
|
if (app.contains("nautilus") || app.contains("nemo")) {
|
|
|
|
canHandleFile = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static QString name;
|
|
|
|
if (name.isEmpty()) {
|
|
|
|
name = desktopFile.value(QString::fromLatin1("Desktop Entry/Name[%1]").arg(qApp->property("ui_lang").toString())).toString();
|
|
|
|
if (name.isEmpty()) {
|
|
|
|
name = desktopFile.value(QString::fromLatin1("Desktop Entry/Name")).toString();
|
|
|
|
}
|
2013-10-02 20:16:24 +04:00
|
|
|
}
|
|
|
|
|
2013-10-03 14:04:08 +04:00
|
|
|
std::replace(args.begin(), args.end(), QString::fromLatin1("%c"), name);
|
|
|
|
std::replace(args.begin(), args.end(), QString::fromLatin1("%u"), fileToOpen);
|
|
|
|
std::replace(args.begin(), args.end(), QString::fromLatin1("%U"), fileToOpen);
|
|
|
|
std::replace(args.begin(), args.end(), QString::fromLatin1("%f"), fileToOpen);
|
|
|
|
std::replace(args.begin(), args.end(), QString::fromLatin1("%F"), fileToOpen);
|
|
|
|
|
|
|
|
// fixme: needs to append --icon, according to http://standards.freedesktop.org/desktop-entry-spec/desktop-entry-spec-latest.html#exec-variables
|
|
|
|
QStringList::iterator it = std::find(args.begin(), args.end(), QString::fromLatin1("%i"));
|
|
|
|
if (it != args.end()) {
|
|
|
|
(*it) = desktopFile.value("Desktop Entry/Icon").toString();
|
|
|
|
args.insert(it, QString::fromLatin1("--icon")); // before
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (args.count() == 0) args << fileToOpen;
|
|
|
|
|
|
|
|
if (app.isEmpty() || args.isEmpty() || !canHandleFile) {
|
2013-10-02 20:16:24 +04:00
|
|
|
// fall back: open the default file manager, without ever selecting the file
|
|
|
|
QDesktopServices::openUrl(QUrl::fromLocalFile(pathToOpen));
|
|
|
|
} else {
|
2014-01-21 15:52:25 +04:00
|
|
|
QProcess::startDetached(app, args);
|
2013-10-02 20:16:24 +04:00
|
|
|
}
|
2013-10-02 13:55:36 +04:00
|
|
|
}
|
2013-10-02 20:16:24 +04:00
|
|
|
}
|
|
|
|
|
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-14 11:55:04 +04:00
|
|
|
QString Utility::timeToDescriptiveString(quint64 msecs)
|
2014-04-25 02:08:25 +04:00
|
|
|
{
|
2014-05-12 18:36:27 +04:00
|
|
|
QList<QPair<QString,quint32> > timeMapping = QList<QPair<QString,quint32> >();
|
2014-05-14 11:55:04 +04:00
|
|
|
timeMapping.append(QPair<QString,quint32>("years",86400*365));
|
|
|
|
timeMapping.append(QPair<QString,quint32>("months",86400*30));
|
|
|
|
timeMapping.append(QPair<QString,quint32>("days",86400));
|
2014-05-12 18:36:27 +04:00
|
|
|
timeMapping.append(QPair<QString,quint32>("hours",3600));
|
|
|
|
timeMapping.append(QPair<QString,quint32>("minutes",60));
|
|
|
|
timeMapping.append(QPair<QString,quint32>("seconds",1));
|
|
|
|
|
2014-05-14 11:55:04 +04:00
|
|
|
return timeToDescriptiveString(timeMapping, msecs, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
QString Utility::timeToDescriptiveString(QList<QPair<QString,quint32> > &timeMapping, quint64 msecs, quint8 precision)
|
|
|
|
{
|
2014-05-12 18:36:27 +04:00
|
|
|
quint64 secs = msecs / 1000;
|
2014-05-14 11:55:04 +04:00
|
|
|
QString retStr = "0 seconds"; // default value in case theres no actual time in msecs.
|
|
|
|
qint64 values[6];
|
|
|
|
int idx = 0;
|
|
|
|
|
|
|
|
for(QList<QPair<QString,quint32> >::Iterator itr = timeMapping.begin(); itr != timeMapping.end() && idx <= precision; itr++) {
|
|
|
|
quint64 result = secs / itr->second;
|
|
|
|
if(idx == 0) {
|
|
|
|
if(result == 0 ) {
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
retStr = itr->first;
|
|
|
|
retStr.prepend(" ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
secs -= result * itr->second;
|
|
|
|
values[idx++] = result;
|
|
|
|
}
|
2014-05-12 18:36:27 +04:00
|
|
|
|
2014-05-14 11:55:04 +04:00
|
|
|
for(idx--; idx >= 0; idx--) {
|
|
|
|
retStr = retStr.prepend("%1").arg(values[idx], 2, 10, QChar('0'));
|
|
|
|
if(0 < idx) {
|
|
|
|
retStr.prepend(":");
|
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
|
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
#ifdef Q_OS_LINUX
|
|
|
|
return true;
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Utility::StopWatch::stop()
|
|
|
|
{
|
|
|
|
addLapTime(QLatin1String(STOPWATCH_END_TAG));
|
|
|
|
_timer.invalidate();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-29 18:13:38 +04:00
|
|
|
} // namespace Mirall
|