Skip to content

Commit

Permalink
frontend/qt: use const char* over char* in the exported Go funcs
Browse files Browse the repository at this point in the history
`cchar_t` is a typedef for `const char`, which allows us to use `const
char*` instead of `char*` for the string input params in the Go
functions. Then we can can remove the ugly const casts in C++.

`toLocal8bit().constData()` is the documented way to get `const char*`
from q `QString`, see:

https://doc.qt.io/qt-6/qstring.html#qPrintable
  • Loading branch information
benma committed Oct 9, 2023
1 parent de15599 commit 390d3f7
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 10 deletions.
6 changes: 3 additions & 3 deletions frontends/qt/libserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ extern "C" {
#endif


extern void backendCall(int p0, char* p1);
extern void backendCall(int queryID, cchar_t* s);

extern void handleURI(char* p0);
extern void handleURI(cchar_t* uri);

extern void serve(
cppHeapFree cppHeapFreeFn,
Expand All @@ -52,7 +52,7 @@ extern void serve(
getSaveFilenameCallback getSaveFilenameFn
);

extern void systemOpen(char* p0);
extern void systemOpen(cchar_t* url);

extern void goLog(cchar_t* msg);

Expand Down
8 changes: 4 additions & 4 deletions frontends/qt/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class BitBoxApp : public SingleApplication
// if the BitBoxApp is launched and also when it is already running, in which case
// it is brought to the foreground automatically.

handleURI(const_cast<char*>(openEvent->url().toString().toStdString().c_str()));
handleURI(openEvent->url().toString().toLocal8Bit().constData());
}
}

Expand Down Expand Up @@ -124,7 +124,7 @@ class RequestInterceptor : public QWebEngineUrlRequestInterceptor {
if (onBuyPage) {
if (info.firstPartyUrl().toString() == info.requestUrl().toString()) {
// A link with target=_blank was clicked.
systemOpen(const_cast<char*>(info.requestUrl().toString().toStdString().c_str()));
systemOpen(info.requestUrl().toString().toLocal8Bit().constData());
// No need to also load it in our page.
info.block(true);
}
Expand Down Expand Up @@ -387,11 +387,11 @@ int main(int argc, char *argv[])
[&](int instanceId, QByteArray message) {
QString arg = QString::fromUtf8(message);
qDebug() << "Received arg from secondary instance:" << arg;
handleURI(const_cast<char*>(arg.toStdString().c_str()));
handleURI(arg.toLocal8Bit().constData());
});
// Handle URI which the app was launched with in the primary instance.
if (a.arguments().size() == 2) {
handleURI(const_cast<char*>(a.arguments()[1].toStdString().c_str()));
handleURI(a.arguments()[1].toLocal8Bit().constData());
}

return a.exec();
Expand Down
4 changes: 2 additions & 2 deletions frontends/qt/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func backendCall(queryID C.int, s *C.char) {
}

//export handleURI
func handleURI(uri *C.char) {
func handleURI(uri *C.cchar_t) {
bridgecommon.HandleURI(C.GoString(uri))
}

Expand Down Expand Up @@ -182,7 +182,7 @@ func serve(
}

//export systemOpen
func systemOpen(url *C.char) {
func systemOpen(url *C.cchar_t) {
goURL := C.GoString(url)
if err := system.Open(goURL); err != nil {
logging.Get().WithGroup("server").WithError(err).Errorf("systemOpen: error opening %v", goURL)
Expand Down
2 changes: 1 addition & 1 deletion frontends/qt/webclass.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class WebClass : public QObject
Q_OBJECT
public slots:
void call(int queryID, const QString& query) {
backendCall(queryID, const_cast<char*>(query.toStdString().c_str()));
backendCall(queryID, query.toLocal8Bit().constData());
}
signals:
void gotResponse(int queryID, QString response);
Expand Down

0 comments on commit 390d3f7

Please sign in to comment.