QuaZip  quazip-1-4
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 <QtCore/Qt>
13 #include <QtCore/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  }
34  device->close();
35  return true;
36 }
37 #else
38 inline bool quazip_close(QIODevice *device) {
39  device->close();
40  return true;
41 }
42 #endif
43 
44 // this is yet another stupid move and deprecation
45 #if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
46 using Qt::SkipEmptyParts;
47 #else
48 #include <QtCore/QString>
49 const auto SkipEmptyParts = QString::SplitBehavior::SkipEmptyParts;
50 #endif
51 
52 // and yet another... (why didn't they just make qSort delegate to std::sort?)
53 #include <QtCore/QList>
54 #if (QT_VERSION >= QT_VERSION_CHECK(5, 2, 0))
55 #include <algorithm>
56 template<typename T, typename C>
57 inline void quazip_sort(T begin, T end, C comparator) {
58  std::sort(begin, end, comparator);
59 }
60 #else
61 #include <QtCore/QtAlgorithms>
62 template<typename T, typename C>
63 inline void quazip_sort(T begin, T end, C comparator) {
64  qSort(begin, end, comparator);
65 }
66 #endif
67 
68 // this is a stupid rename...
69 #include <QtCore/QDateTime>
70 #include <QtCore/QFileInfo>
71 #if (QT_VERSION >= QT_VERSION_CHECK(5, 10, 0))
72 inline QDateTime quazip_ctime(const QFileInfo &fi) {
73  return fi.birthTime();
74 }
75 #else
76 inline QDateTime quazip_ctime(const QFileInfo &fi) {
77  return fi.created();
78 }
79 #endif
80 
81 // this is just a slightly better alternative
82 #include <QtCore/QFileInfo>
83 #if (QT_VERSION >= QT_VERSION_CHECK(5, 14, 0))
84 inline bool quazip_is_symlink(const QFileInfo &fi) {
85  return fi.isSymbolicLink();
86 }
87 #else
88 inline bool quazip_is_symlink(const QFileInfo &fi) {
89  // also detects *.lnk on Windows, but better than nothing
90  return fi.isSymLink();
91 }
92 #endif
93 
94 // I'm not even sure what this one is, but nevertheless
95 #include <QtCore/QFileInfo>
96 #if (QT_VERSION >= QT_VERSION_CHECK(5, 13, 0))
97 inline QString quazip_symlink_target(const QFileInfo &fi) {
98  return fi.symLinkTarget();
99 }
100 #else
101 inline QString quazip_symlink_target(const QFileInfo &fi) {
102  return fi.readLink(); // What's the difference? I've no idea.
103 }
104 #endif
105 
106 // this is not a deprecation but an improvement, for a change
107 #include <QtCore/QDateTime>
108 #if (QT_VERSION >= 0x040700)
109 inline quint64 quazip_ntfs_ticks(const QDateTime &time, int fineTicks) {
110  QDateTime base(QDate(1601, 1, 1), QTime(0, 0), Qt::UTC);
111  return base.msecsTo(time) * 10000 + fineTicks;
112 }
113 #else
114 inline quint64 quazip_ntfs_ticks(const QDateTime &time, int fineTicks) {
115  QDateTime base(QDate(1601, 1, 1), QTime(0, 0), Qt::UTC);
116  QDateTime utc = time.toUTC();
117  return (static_cast<qint64>(base.date().daysTo(utc.date()))
118  * Q_INT64_C(86400000)
119  + static_cast<qint64>(base.time().msecsTo(utc.time())))
120  * Q_INT64_C(10000) + fineTicks;
121 }
122 #endif
123 
124 // yet another improvement...
125 #include <QtCore/QDateTime>
126 #if QT_VERSION >= QT_VERSION_CHECK(5, 8, 0) // Yay! Finally a way to get time as qint64!
127 inline qint64 quazip_to_time64_t(const QDateTime &time) {
128  return time.toSecsSinceEpoch();
129 }
130 #else
131 inline qint64 quazip_to_time64_t(const QDateTime &time) {
132  return static_cast<qint64>(time.toTime_t()); // 32 bits only, but better than nothing
133 }
134 #endif
135 
136 #include <QtCore/QTextStream>
137 // and another stupid move
138 #if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
139 const auto quazip_endl = Qt::endl;
140 #else
141 const auto quazip_endl = endl;
142 #endif
143 
144 #endif // QUAZIP_QT_COMPAT_H