Skip to content

Commit f34ece1

Browse files
committed
A hell lot of ifdefs to make file picker work in wasm
1 parent 480b8dd commit f34ece1

4 files changed

Lines changed: 43 additions & 9 deletions

File tree

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
1010
if(NOT EMSCRIPTEN)
1111
find_package(Qt6 REQUIRED COMPONENTS Quick NetworkAuth)
1212
else()
13-
find_package(Qt6 REQUIRED COMPONENTS Quick)
13+
find_package(Qt6 REQUIRED COMPONENTS Quick Widgets)
1414
endif()
1515

1616
set(
@@ -128,7 +128,7 @@ if(NOT EMSCRIPTEN)
128128
)
129129
else()
130130
target_link_libraries(TwitterClone
131-
PRIVATE Qt6::Quick
131+
PRIVATE Qt6::Quick Qt6::Widgets
132132
)
133133
endif()
134134

qml/Profile.qml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,12 @@ ColumnLayout {
170170
MouseArea {
171171
anchors.fill: parent
172172
enabled: root.userProfile.id === root.currentUserId
173-
onClicked: fileDialog.open()
173+
onClicked: {
174+
if(Qt.platform.os === "wasm")
175+
Api.users.uploadAvatar("")
176+
else
177+
fileDialog.open()
178+
}
174179
}
175180
}
176181

src/Api/UsersApi.cpp

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
#include <QFileInfo>
44
#include <QHttpMultiPart>
55

6+
#ifdef Q_OS_WASM
7+
#include <QFileDialog>
8+
#endif
9+
610
using namespace Qt::Literals::StringLiterals;
711

812
UsersApi::UsersApi(
@@ -57,20 +61,34 @@ void UsersApi::updateMe(const QString& displayName, const QString& username)
5761

5862
void UsersApi::uploadAvatar(const QUrl& path)
5963
{
64+
#ifdef Q_OS_WASM
65+
if(path.isEmpty())
66+
QFileDialog::getOpenFileContent("Images (*.png *.jpg *.jpeg)",
67+
[this](const QString& fileName, const QByteArray& fileContent)
68+
{
69+
#endif
6070
const auto multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType);
6171

6272
QHttpPart imagePart;
6373
imagePart.setHeader(
6474
QNetworkRequest::ContentDispositionHeader,
6575
{ u"form-data; name=\"avatar\"; filename=\"%1\""_s
66-
.arg(QFileInfo(path.toString()).fileName()) }
76+
.arg(
77+
#ifdef Q_OS_WASM
78+
fileName
79+
#else
80+
QFileInfo(path.toString()).fileName()
81+
#endif
82+
) }
6783
);
6884

69-
#ifndef Q_OS_ANDROID
85+
#if !defined(Q_OS_ANDROID) && !defined(Q_OS_WASM)
7086
QFile file(path.toLocalFile());
71-
#else
87+
#elif !defined(Q_OS_WASM)
7288
QFile file(path.toString());
7389
#endif
90+
91+
#ifndef Q_OS_WASM
7492
if(!file.open(QFile::ReadOnly))
7593
{
7694
multiPart->deleteLater();
@@ -80,7 +98,9 @@ void UsersApi::uploadAvatar(const QUrl& path)
8098
}
8199

82100
imagePart.setBody(file.readAll());
83-
101+
#else
102+
imagePart.setBody(fileContent);
103+
#endif
84104
multiPart->append(imagePart);
85105

86106
auto req = requestFactory.createRequest("/users/me/avatar");
@@ -96,5 +116,9 @@ void UsersApi::uploadAvatar(const QUrl& path)
96116
emit userReceived(obj);
97117
emit profileReceived(obj);
98118
});
119+
120+
#ifdef Q_OS_WASM
121+
});
122+
#endif
99123
}
100124

src/Auth/AuthManagerWASM.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ void AuthManagerWASM::refresh()
5151

5252
void AuthManagerWASM::handleCode()
5353
{
54-
em::val location = em::val::global("location");
55-
54+
const auto location = em::val::global("location");
5655
const auto search = QString::fromStdString(location["search"].as<std::string>());
5756

5857
if(search.isEmpty())
@@ -146,6 +145,12 @@ void AuthManagerWASM::acceptTokens(QNetworkReply *reply)
146145
storage.saveTokens({ access, refresh });
147146

148147
emit loginCompleted();
148+
149+
const auto location = em::val::global("location");
150+
const auto search = QString::fromStdString(location["search"].as<std::string>());
151+
152+
if(!search.isEmpty())
153+
em::val::global("eval")(u"window.location.href = window.location.origin + window.location.pathname;"_s.toStdString());
149154
}
150155
else
151156
qDebug() << "Token request error:" << reply->errorString();

0 commit comments

Comments
 (0)