// SPDX-FileCopyrightText: 2026 Matvey Shcherbinin // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL pragma Singleton import QtQuick // A FileDialog hands the pick over as a file:// URL. Depending on the platform // theme that arrives as a plain QUrl or as a ready-made string, and the two do // not share a method set - reading .toLocalFile() off the value crashes where // it is already a string. These helpers accept either and return what the // caller needs: a plain path for file APIs, or the URL text a QML Image and // Media can load directly. QtObject { function toLocalPath(url) { const text = String(url) if (!text.startsWith("file://")) return text // QUrl::toString() can still leave percent-encoding behind; undo it, // guarding against a stray '%' in a real file name. try { return decodeURIComponent(text.slice("file://".length)) } catch (_) { return text.slice("file://".length) } } function toUrlText(url) { return String(url) } // Settings keep avatar/banner/badge as a plain filesystem path because the // upload side reads it with QFile. An Image inside a qrc-loaded component, // given such a bare path, resolves it as a resource name - qrc:/home/... - // and fails to load, so display sites call this first: empty stays empty, // URLs and qrc: pass through, a bare path becomes a real file:// URL. function toSourceUrl(url) { const text = String(url) if (text.length === 0) return "" if (text.startsWith("file://") || text.startsWith("qrc:") || text.startsWith("image://") || text.startsWith("mxc://") || text.startsWith("http://") || text.startsWith("https://")) return text return "file://" + encodeURI(text) } }