Skip to content

Commit ad625dc

Browse files
committed
build: attempt to fix kanzi windows builds with conditional compilation
1 parent 4935164 commit ad625dc

File tree

4 files changed

+100
-30
lines changed

4 files changed

+100
-30
lines changed

CMakeLists.txt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,25 @@ file(GLOB_RECURSE SRC_FILES
3232
"${SRC_DIR}/*.qss"
3333
)
3434

35+
set(COMPRESSION_SUPPORT ON)
36+
if (WIN32)
37+
set(COMPRESSION_SUPPORT OFF)
38+
endif()
39+
40+
# Exclude compression features for Windows (kanzi not supported)
41+
if(NOT COMPRESSION_SUPPORT)
42+
# list the files to exclude explicitly
43+
set(EXCLUDE_FILES
44+
"${SRC_DIR}/common/utils/compression.cpp"
45+
"${SRC_DIR}/common/utils/compression.hpp"
46+
)
47+
48+
# remove them from the main source list
49+
list(REMOVE_ITEM SRC_FILES ${EXCLUDE_FILES})
50+
endif()
51+
52+
list(REMOVE_ITEM SRC_FILES ${EXCLUDE_FILES})
53+
3554
# Combine translation files with the source files.
3655
set(PROJECT_SOURCES ${TS_FILES} ${SRC_FILES})
3756

@@ -74,3 +93,7 @@ if (UNIX AND NOT APPLE)
7493
install(FILES assets/logo.svg DESTINATION ${CMAKE_INSTALL_FULL_DATADIR}/icons/hicolor/scalable/apps RENAME io.github.prayag2.Drawy.svg)
7594
install(FILES deploy/linux/io.github.prayag2.Drawy.metainfo.xml.in DESTINATION ${CMAKE_INSTALL_FULL_DATADIR}/metainfo/ RENAME io.github.prayag2.Drawy.metainfo.xml)
7695
endif()
96+
97+
target_compile_definitions(${PROJECT_NAME} PRIVATE
98+
COMPRESSION_SUPPORT=$<BOOL:${COMPRESSION_SUPPORT}>
99+
)

deps/CMakeLists.txt

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
include(FetchContent)
22

3-
FetchContent_Declare(
4-
kanzi
5-
URL https://github.com/flanglet/kanzi-cpp/archive/refs/tags/2.4.0.zip
6-
URL_MD5 ec451cdab40362e9e4af8e5ca780a07d
7-
)
3+
# Kanzi compilation is currently broken on Windows.
4+
if(COMPRESSION_SUPPORT)
5+
FetchContent_Declare(
6+
kanzi
7+
URL https://github.com/flanglet/kanzi-cpp/archive/refs/tags/2.4.0.zip
8+
URL_MD5 ec451cdab40362e9e4af8e5ca780a07d
9+
)
810

9-
add_subdirectory(kanzi)
11+
add_subdirectory(kanzi)
12+
endif()
1013

1114
# find_package(tinyxml2 REQUIRED)
1215
# target_link_libraries(${PROJECT_NAME} PRIVATE tinyxml2::tinyxml2)

src/serializer/loader.cpp

Lines changed: 58 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626
#include <QString>
2727
#include <format>
2828
#include <memory>
29+
#include <optional>
2930

3031
#include "../common/constants.hpp"
31-
#include "../common/utils/compression.hpp"
3232
#include "../context/applicationcontext.hpp"
3333
#include "../context/renderingcontext.hpp"
3434
#include "../context/spatialcontext.hpp"
@@ -42,24 +42,12 @@
4242
#include "../item/rectangle.hpp"
4343
#include "../item/text.hpp"
4444

45-
void Loader::loadFromFile(ApplicationContext *context) {
46-
// file filter
47-
QString filter = QString::fromUtf8("Drawy (*.%1)").arg(Common::drawyFileExt);
48-
49-
// ask for file (handle cancel)
50-
QDir homeDir{QDir::home()};
51-
QString fileName = QFileDialog::getOpenFileName(nullptr, "Open File", homeDir.path(), filter);
52-
if (fileName.isEmpty())
53-
return;
54-
55-
QFile file{fileName};
56-
if (!file.open(QIODevice::ReadOnly)) {
57-
qWarning() << "Failed to open file:" << file.errorString();
58-
return;
59-
}
45+
#ifdef COMPRESSION_SUPPORT
46+
#include "../common/utils/compression.hpp"
6047

48+
std::optional<QJsonDocument> loadCompressedSave(QFile &file) {
6149
QByteArray compressedByteArray = file.readAll();
62-
file.close();
50+
QJsonParseError parseError;
6351

6452
QByteArray byteArray;
6553
try {
@@ -73,21 +61,71 @@ void Loader::loadFromFile(ApplicationContext *context) {
7361
byteArray = Common::Utils::Compression::decompressData(decoded);
7462
} catch (const std::exception &ex2) {
7563
qWarning() << "Base64-decode fallback also failed:" << ex2.what();
76-
return;
64+
return std::nullopt;
7765
}
7866
} else {
79-
return;
67+
return std::nullopt;
8068
}
8169
}
8270

83-
QJsonParseError parseError;
8471
QJsonDocument doc = QJsonDocument::fromJson(byteArray, &parseError);
8572
if (doc.isNull() || !doc.isObject()) {
8673
qWarning() << "JSON parse failed:" << parseError.errorString()
8774
<< "offset:" << parseError.offset;
75+
return std::nullopt;
76+
}
77+
78+
return doc;
79+
}
80+
#endif
81+
82+
void Loader::loadFromFile(ApplicationContext *context) {
83+
// file filter
84+
QString filter = QString::fromUtf8("Drawy (*.%1)").arg(Common::drawyFileExt);
85+
86+
// ask for file (handle cancel)
87+
QDir homeDir{QDir::home()};
88+
QString fileName = QFileDialog::getOpenFileName(nullptr, "Open File", homeDir.path(), filter);
89+
if (fileName.isEmpty())
90+
return;
91+
92+
QFile file{fileName};
93+
if (!file.open(QIODevice::ReadOnly)) {
94+
qWarning() << "Failed to open file:" << file.errorString();
95+
return;
96+
}
97+
98+
bool jsonLoadFailed = false;
99+
100+
QByteArray byteArray = file.readAll();
101+
QJsonParseError parseError;
102+
QJsonDocument doc = QJsonDocument::fromJson(byteArray, &parseError);
103+
if (!doc.isNull() && doc.isObject()) {
104+
jsonLoadFailed = true;
105+
qWarning() << "JSON parse failed:" << parseError.errorString()
106+
<< "offset:" << parseError.offset;
107+
}
108+
109+
#ifdef COMPRESSION_SUPPORT
110+
if (jsonLoadFailed) {
111+
auto maybeDoc = loadCompressedSave(file);
112+
113+
if (maybeDoc.has_value()) {
114+
doc = maybeDoc.value();
115+
jsonLoadFailed = false;
116+
} else {
117+
file.close();
118+
return;
119+
}
120+
}
121+
#endif
122+
123+
if (jsonLoadFailed) {
88124
return;
89125
}
90126

127+
file.close();
128+
91129
QJsonObject docObj = doc.object();
92130

93131
context->reset();

src/serializer/serializer.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
#include <memory>
2727

2828
#include "../common/constants.hpp"
29-
#include "../common/utils/compression.hpp"
3029
#include "../context/applicationcontext.hpp"
3130
#include "../context/renderingcontext.hpp"
3231
#include "../context/spatialcontext.hpp"
@@ -36,6 +35,10 @@
3635
#include "../item/polygon.hpp"
3736
#include "../item/text.hpp"
3837

38+
#ifdef COMPRESSION_SUPPORT
39+
#include "../common/utils/compression.hpp"
40+
#endif
41+
3942
Serializer::Serializer() {
4043
}
4144

@@ -131,14 +134,17 @@ void Serializer::saveToFile() {
131134
QFileDialog::getSaveFileName(nullptr, "Save File", defaultFilePath, text.data())};
132135

133136
auto data{doc.toJson(QJsonDocument::Compact)};
134-
auto compressedData{Common::Utils::Compression::compressData(data)};
137+
138+
#ifdef COMPRESSION_SUPPORT
139+
data = Common::Utils::Compression::compressData(data);
140+
#endif
135141

136142
QFile file{fileName};
137143
file.open(QIODevice::WriteOnly);
138-
qint64 written = file.write(compressedData);
144+
qint64 written = file.write(data);
139145
file.close();
140146

141-
if (written != compressedData.size()) {
147+
if (written != data.size()) {
142148
qWarning() << "Warning: not all bytes were written";
143149
}
144150

0 commit comments

Comments
 (0)