Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qstandardpaths_ohos.cpp
Go to the documentation of this file.
1// Copyright (C) 2025 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5
6#include <QtCore/private/qcore_ohos_p.h>
7#include <QtCore/private/qohosappcontext_p.h>
8#include <QtCore/private/qohoscommon_p.h>
9#include <QtCore/qdir.h>
10#include <QtCore/qlogging.h>
11#include <filemanagement/environment/oh_environment.h>
12#include <optional>
13
14#ifndef QT_NO_STANDARDPATHS
15
16QT_BEGIN_NAMESPACE
17
18namespace {
19
20std::optional<QString> tryGetUserDirFromOhEnvironment(FileManagement_ErrCode (*ohEnvironmentDirGetter)(char **))
21{
22 char *dirPath = nullptr;
23 FileManagement_ErrCode dirGetterRetVal = QOhosJsThreadGateway::eval(
24 [&](QOhosJsState &) {
25 return ohEnvironmentDirGetter(&dirPath);
26 });
27 if (dirGetterRetVal != FileManagement_ErrCode::ERR_OK || dirPath == nullptr) {
28 qOhosPrintfDebug("OH_Environment_GetUser* dir getter failed, retval: %d", static_cast<int>(dirGetterRetVal));
29 return {};
30 }
31
32 auto result = QString::fromUtf8(dirPath);
33
34 ::free(dirPath);
35
36 return result;
37}
38
39template<FileManagement_ErrCode (*ohEnvironmentDirGetter)(char **)>
41{
42 static std::optional<QString> cachedDirPath;
43
44 if (!cachedDirPath.has_value())
45 cachedDirPath = tryGetUserDirFromOhEnvironment(ohEnvironmentDirGetter);
46
47 return cachedDirPath;
48}
49
50}
51
52QStringList QStandardPaths::standardLocations(StandardLocation type)
53{
54 return QStringList(writableLocation(type));
55}
56
57QString QStandardPaths::writableLocation(StandardLocation type)
58{
59 auto testDirSuffix = QStandardPaths::isTestModeEnabled() ? QLatin1String("/.qttest") : QLatin1String("");
60
61 switch (type) {
62 case DesktopLocation:
63 // NOTE: return empty string if the dir isn't available, even in the doc says otherwise
64 return tryGetCachedUserDirFromOhEnvironment<OH_Environment_GetUserDesktopDir>().value_or(QLatin1String());
65 case DocumentsLocation:
66 // NOTE: return empty string if the dir isn't available, even in the doc says otherwise
67 return tryGetCachedUserDirFromOhEnvironment<OH_Environment_GetUserDocumentDir>().value_or(QLatin1String());
68 case FontsLocation:
69 // NOT SUPPORTED
70 break;
71 case ApplicationsLocation:
72 // NOT SUPPORTED
73 break;
74 case MusicLocation:
75 // NOT SUPPORTED
76 break;
77 case MoviesLocation:
78 // NOT SUPPORTED
79 break;
80 case PicturesLocation:
81 // NOT SUPPORTED
82 break;
83 case DownloadLocation:
84 // NOTE: return empty string if the dir isn't available, even in the doc says otherwise
85 return tryGetCachedUserDirFromOhEnvironment<OH_Environment_GetUserDownloadDir>().value_or(QLatin1String());
86 case PublicShareLocation:
87 // NOT SUPPORTED
88 break;
89 case TemplatesLocation:
90 // NOT SUPPORTED
91 break;
92 case TempLocation:
93 return QDir::tempPath();
94 case HomeLocation:
95 return QDir::homePath();
96 case GenericDataLocation:
97 case AppDataLocation:
98 case StateLocation:
99 case GenericStateLocation:
100 return QOhosAppContext::getProperty(QOhosAppContext::Type::filesDir) + testDirSuffix;
101 case CacheLocation:
102 case GenericCacheLocation:
103 return QOhosAppContext::getProperty(QOhosAppContext::Type::cacheDir) + testDirSuffix;
104 case RuntimeLocation:
105 case ConfigLocation:
106 case GenericConfigLocation:
107 case AppConfigLocation:
108 case AppLocalDataLocation:
109 return QOhosAppContext::getProperty(QOhosAppContext::Type::preferencesDir) + testDirSuffix;
110 default:
111 break;
112 }
113
114 return QLatin1String();
115}
116
117QT_END_NAMESPACE
118
119#endif // QT_NO_STANDARDPATHS
std::optional< QString > tryGetCachedUserDirFromOhEnvironment()
std::optional< QString > tryGetUserDirFromOhEnvironment(FileManagement_ErrCode(*ohEnvironmentDirGetter)(char **))