QuaZip  quazip-1-0
quazip_qt_compat.h
1 #ifndef QUAZIP_QT_COMPAT_H
2 #define QUAZIP_QT_COMPAT_H
3 
4 /*
5  * For some reason, Qt 5.14 and 5.15 introduced a whole mess of seemingly random
6  * moves and deprecations. To avoid populating code with #ifs,
7  * we handle this stuff here, as well as some other compatibility issues.
8  *
9  * Some includes are repeated just in case we want to split this file later.
10  */
11 
12 #include <Qt>
13 #include <QtGlobal>
14 
15 // Legacy encodings are still everywhere, but the Qt team decided we
16 // don't need them anymore and moved them out of Core in Qt 6.
17 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
18 # include <QtCore5Compat/QTextCodec>
19 #else
20 # include <QtCore/QTextCodec>
21 #endif
22 
23 // QSaveFile terribly breaks the is-a idiom (Liskov substitution principle):
24 // QSaveFile is-a QIODevice, but it makes close() private and aborts
25 // if you call it through the base class. Hence this ugly hack:
26 #if (QT_VERSION >= 0x050100)
27 #include <QtCore/QSaveFile>
28 inline bool quazip_close(QIODevice *device) {
29  QSaveFile *file = qobject_cast<QSaveFile*>(device);
30  if (file != nullptr) {
31  // We have to call the ugly commit() instead:
32  return file->commit();
33  } else {
34  device->close();
35  return true;
36  }
37 }
38 #else
39 inline bool quazip_close(QIODevice *device) {
40  device->close();
41  return true;
42 }
43 #endif
44 
45 // this is yet another stupid move and deprecation
46 #if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
47 using Qt::SkipEmptyParts;
48 #else
49 #include <QString>
50 const auto SkipEmptyParts = QString::SplitBehavior::SkipEmptyParts;
51 #endif
52 
53 // and yet another... (why didn't they just make qSort delegate to std::sort?)
54 #include <QList>
55 #if (QT_VERSION >= QT_VERSION_CHECK(5, 2, 0))
56 #include <algorithm>
57 template<typename T, typename C>
58 inline void quazip_sort(T begin, T end, C comparator) {
59  std::sort(begin, end, comparator);
60 }
61 #else
62 #include <QtAlgorithms>
63 template<typename T, typename C>
64 inline void quazip_sort(T begin, T end, C comparator) {
65  qSort(begin, end, comparator);
66 }
67 #endif
68 
69 // this is a stupid rename...
70 #include <QDateTime>
71 #include <QFileInfo>
72 #if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0))
73 inline QDateTime quazip_ctime(const QFileInfo &fi) {
74  return fi.birthTime();
75 }
76 #else
77 inline QDateTime quazip_ctime(const QFileInfo &fi) {
78  return fi.created();
79 }
80 #endif
81 
82 // this is not a deprecation but an improvement, for a change
83 #include <QDateTime>
84 #if (QT_VERSION >= 0x040700)
85 inline quint64 quazip_ntfs_ticks(const QDateTime &time, int fineTicks) {
86  QDateTime base(QDate(1601, 1, 1), QTime(0, 0), Qt::UTC);
87  return base.msecsTo(time) * 10000 + fineTicks;
88 }
89 #else
90 inline quint64 quazip_ntfs_ticks(const QDateTime &time, int fineTicks) {
91  QDateTime base(QDate(1601, 1, 1), QTime(0, 0), Qt::UTC);
92  QDateTime utc = time.toUTC();
93  return (static_cast<qint64>(base.date().daysTo(utc.date()))
94  * Q_INT64_C(86400000)
95  + static_cast<qint64>(base.time().msecsTo(utc.time())))
96  * Q_INT64_C(10000) + fineTicks;
97 }
98 #endif
99 
100 // yet another improvement...
101 #include <QDateTime>
102 #if QT_VERSION >= QT_VERSION_CHECK(5, 8, 0) // Yay! Finally a way to get time as qint64!
103 inline qint64 quazip_to_time64_t(const QDateTime &time) {
104  return time.toSecsSinceEpoch();
105 }
106 #else
107 inline qint64 quazip_to_time64_t(const QDateTime &time) {
108  return static_cast<qint64>(time.toTime_t()); // 32 bits only, but better than nothing
109 }
110 #endif
111 
112 #include <QTextStream>
113 // and another stupid move
114 #if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
115 const auto quazip_endl = Qt::endl;
116 #else
117 const auto quazip_endl = endl;
118 #endif
119 
120 #endif // QUAZIP_QT_COMPAT_H