qBittorrent
misc.h
Go to the documentation of this file.
1 /*
2  * Bittorrent Client using Qt and libtorrent.
3  * Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  *
19  * In addition, as a special exception, the copyright holders give permission to
20  * link this program with the OpenSSL project's "OpenSSL" library (or with
21  * modified versions of it that use the same license as the "OpenSSL" library),
22  * and distribute the linked executables. You must obey the GNU General Public
23  * License in all respects for all of the code used other than "OpenSSL". If you
24  * modify file(s), you may extend this exception to your version of the file(s),
25  * but you are not obligated to do so. If you do not wish to do so, delete this
26  * exception statement from your version.
27  */
28 
29 #pragma once
30 
31 #include <QtGlobal>
32 
33 #ifdef Q_OS_WIN
34 #include <memory>
35 #include <Windows.h>
36 #endif
37 
38 #include <QString>
39 
40 enum class ShutdownDialogAction;
41 
42 /* Miscellaneous functions that can be useful */
43 
44 namespace Utils::Misc
45 {
46  // use binary prefix standards from IEC 60027-2
47  // see http://en.wikipedia.org/wiki/Kilobyte
48  enum class SizeUnit
49  {
50  Byte, // 1024^0,
51  KibiByte, // 1024^1,
52  MebiByte, // 1024^2,
53  GibiByte, // 1024^3,
54  TebiByte, // 1024^4,
55  PebiByte, // 1024^5,
56  ExbiByte // 1024^6,
57  // int64 is used for sizes and thus the next units can not be handled
58  // ZebiByte, // 1024^7,
59  // YobiByte, // 1024^8
60  };
61 
62  QString parseHtmlLinks(const QString &rawText);
63 
65 
66  QString osName();
67  QString boostVersionString();
68  QString libtorrentVersionString();
69  QString opensslVersionString();
70  QString zlibVersionString();
71 
72  QString unitString(SizeUnit unit, bool isSpeed = false);
73 
74  // return the best user friendly storage unit (B, KiB, MiB, GiB, TiB)
75  // value must be given in bytes
76  QString friendlyUnit(qint64 bytes, bool isSpeed = false);
78  qint64 sizeInBytes(qreal size, SizeUnit unit);
79 
80  bool isPreviewable(const QString &filename);
81 
82  // Take a number of seconds and return a user-friendly
83  // time duration like "1d 2h 10m".
84  QString userFriendlyDuration(qlonglong seconds, qlonglong maxCap = -1);
85  QString getUserIDString();
86 
87 #ifdef Q_OS_WIN
88  QString windowsSystemPath();
89 
90  template <typename T>
91  T loadWinAPI(const QString &source, const char *funcName)
92  {
93  QString path = windowsSystemPath();
94  if (!path.endsWith('\\'))
95  path += '\\';
96 
97  path += source;
98 
99  auto pathWchar = std::make_unique<wchar_t[]>(path.length() + 1);
100  path.toWCharArray(pathWchar.get());
101 
102  return reinterpret_cast<T>(
103  ::GetProcAddress(::LoadLibraryW(pathWchar.get()), funcName));
104  }
105 #endif // Q_OS_WIN
106 }
QString osName()
Definition: misc.cpp:466
QString libtorrentVersionString()
Definition: misc.cpp:487
QString opensslVersionString()
Definition: misc.cpp:494
QString parseHtmlLinks(const QString &rawText)
Definition: misc.cpp:404
SizeUnit
Definition: misc.h:49
void shutdownComputer(const ShutdownDialogAction &action)
Definition: misc.cpp:110
QString unitString(SizeUnit unit, bool isSpeed=false)
Definition: misc.cpp:252
qint64 sizeInBytes(qreal size, SizeUnit unit)
Definition: misc.cpp:288
QString boostVersionString()
Definition: misc.cpp:477
bool isPreviewable(const QString &filename)
Definition: misc.cpp:295
int friendlyUnitPrecision(SizeUnit unit)
Definition: misc.cpp:271
QString zlibVersionString()
Definition: misc.cpp:504
QString userFriendlyDuration(qlonglong seconds, qlonglong maxCap=-1)
Definition: misc.cpp:353
QString friendlyUnit(qint64 bytes, bool isSpeed=false)
Definition: misc.cpp:261
QString getUserIDString()
Definition: misc.cpp:389
action
Definition: tstool.py:143
ShutdownDialogAction
Definition: types.h:36