File: utils.h

package info (click to toggle)
cantata 3.3.1.ds1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 38,300 kB
  • sloc: cpp: 105,773; perl: 1,366; xml: 723; python: 139; lex: 110; sh: 105; yacc: 78; makefile: 8
file content (138 lines) | stat: -rw-r--r-- 5,264 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
 * Cantata
 *
 * Copyright (c) 2011-2022 Craig Drummond <craig.p.drummond@gmail.com>
 *
 * ----
 *
 * 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; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#ifndef UTILS_H
#define UTILS_H

#include "thread.h"
#include <QDir>
#include <QFont>
#include <QLatin1Char>
#include <QPainterPath>
#include <QString>
#include <QtGlobal>
#include <math.h>
#include <stdlib.h>
#include <sys/types.h>

class QString;
class QWidget;
class QUrl;
class QRectF;

namespace Utils {
extern const QLatin1Char constDirSep;
extern const QLatin1String constDirSepStr;
extern const char* constDirSepCharStr;

inline bool equal(double d1, double d2, double precision = 0.0001)
{
	return (fabs(d1 - d2) < precision);
}

#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
template<typename T>
QSet<T> listToSet(const QList<T>& list) { return QSet<T>(list.cbegin(), list.cend()); }
#else
template<typename T>
static inline QSet<T> listToSet(const QList<T>& list) { return list.toSet(); }
#endif

extern QString fixPath(const QString& d, bool ensureEndsInSlash = true);
#ifdef Q_OS_WIN
inline QString homeToTilda(const QString& s) { return s; }
inline QString tildaToHome(const QString& s) { return s; }
#else
extern QString homeToTilda(const QString& s);
extern QString tildaToHome(const QString& s);
#endif

extern QString getDir(const QString& file, bool addSlash = true);
extern QString getFile(const QString& file);
extern QString getExtension(const QString& file);
extern QString changeExtension(const QString& file, const QString& extension);
extern bool isDirReadable(const QString& dir);

inline void msleep(int msecs) { Thread::msleep(msecs); }
inline void sleep() { msleep(100); }

extern QString strippedText(QString s);
extern QString stripAcceleratorMarkers(QString label);
extern QMap<QString, QString> hashParams(const QString& url);
extern QString addHashParam(const QString& url, const QString& key, const QString& val);
extern QString removeHash(const QString& url);

// Convert path to a format suitable fo rUI - e.g. use native separators, and remove any trailing separator
extern QString convertPathForDisplay(const QString& dir, bool isFolder = true);
// Convert path from a UI field - convert to / separators, and add a trailing separator (if isFolder)
extern QString convertPathFromDisplay(const QString& dir, bool isFolder = true);
#ifndef Q_OS_WIN
extern gid_t getGroupId(const char* groupName = "users");// Return 0 if user is not in group, otherwise returns group ID
#endif
extern void setFilePerms(const QString& file, const char* groupName = "users");
extern bool makeDir(const QString& dir, int mode);
extern bool createWorldReadableDir(const QString& dir, const QString& base, const char* groupName = "users");

extern QString findExe(const QString& appname, const QString& pathstr = QString());
extern QString formatByteSize(double size);
inline QString formatNumber(double number, int precision) { return QString::number(number, 'f', precision); }
extern QString formatDuration(const quint32 totalseconds);
extern QString formatTime(const quint32 seconds, bool zeroIsUnknown = false);
inline QString escapeActionText(const QString& text) { return QString(text).replace(QLatin1String("&"), QLatin1String("&&")); }

extern QString cleanPath(const QString& p);
extern QString dataDir(const QString& sub = QString(), bool create = false);
extern QString cacheDir(const QString& sub = QString(), bool create = true);
extern QString systemDir(const QString& sub);
extern QString helper(const QString& app);
extern bool moveFile(const QString& from, const QString& to);
extern void moveDir(const QString& from, const QString& to);
extern void clearOldCache(const QString& sub, int maxAge);
extern void touchFile(const QString& fileName);

extern double smallFontFactor(const QFont& f);
extern QFont smallFont(QFont f);
extern int layoutSpacing(QWidget* w);
extern double screenDpiScale();
inline bool isHighDpi() { return screenDpiScale() > 1.35; }
inline int scaleForDpi(int v) { return qRound(screenDpiScale() * v); }
extern bool limitedHeight(QWidget* w);
extern void resizeWindow(QWidget* w, bool preserveWidth = true, bool preserveHeight = true);
extern void raiseWindow(QWidget* w);
extern QSize minSize(const QSize& fst, const QSize& snd);

enum Desktop {
	KDE,
	Gnome,
	Unity,
	Other
};
extern Desktop currentDe();
extern bool useSystemTray();
extern QPainterPath buildPath(const QRectF& r, double radius);
extern QColor clampColor(const QColor& col);
extern QColor monoIconColor();
extern int compare(const QString& a, const QString& b);
}// namespace Utils

#endif