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/private/qohosmemoizingjsthreadfetcher_p.h>
10#include <QtCore/qdir.h>
11#include <QtCore/qlogging.h>
12#include <filemanagement/environment/oh_environment.h>
13#include <optional>
14
15#ifndef QT_NO_STANDARDPATHS
16
17QT_BEGIN_NAMESPACE
18
19namespace {
20
21template<FileManagement_ErrCode (*ohEnvironmentDirGetter)(char **)>
23{
24 char *dirPath = nullptr;
25 FileManagement_ErrCode dirGetterRetVal = ohEnvironmentDirGetter(&dirPath);
26 if (dirGetterRetVal != FileManagement_ErrCode::ERR_OK || dirPath == nullptr) {
27 qOhosPrintfDebug("OH_Environment_GetUser* dir getter failed, retval: %d", static_cast<int>(dirGetterRetVal));
28 return {};
29 }
30
31 auto result = QString::fromUtf8(dirPath);
32
33 ::free(dirPath);
34
35 return result;
36}
37
39 tryGetUserDirFromOhEnvironment<OH_Environment_GetUserDesktopDir>, "OH_Environment_GetUserDesktopDir()");
40
42 tryGetUserDirFromOhEnvironment<OH_Environment_GetUserDocumentDir>, "OH_Environment_GetUserDocumentDir()");
43
45 tryGetUserDirFromOhEnvironment<OH_Environment_GetUserDownloadDir>, "OH_Environment_GetUserDownloadDir()");
46
47}
48
49QStringList QStandardPaths::standardLocations(StandardLocation type)
50{
51 return QStringList(writableLocation(type));
52}
53
54QString QStandardPaths::writableLocation(StandardLocation type)
55{
56 auto testDirSuffix = QStandardPaths::isTestModeEnabled() ? QLatin1String("/.qttest") : QLatin1String("");
57
58 switch (type) {
59 case DesktopLocation:
60 // NOTE: return empty string if the dir isn't available, even in the doc says otherwise
61 return userDesktopDirFetcher.optValue().value_or(QLatin1String());
62 case DocumentsLocation:
63 // NOTE: return empty string if the dir isn't available, even in the doc says otherwise
64 return userDocumentDirFetcher.optValue().value_or(QLatin1String());
65 case FontsLocation:
66 // NOT SUPPORTED
67 break;
68 case ApplicationsLocation:
69 // NOT SUPPORTED
70 break;
71 case MusicLocation:
72 // NOT SUPPORTED
73 break;
74 case MoviesLocation:
75 // NOT SUPPORTED
76 break;
77 case PicturesLocation:
78 // NOT SUPPORTED
79 break;
80 case DownloadLocation:
81 // NOTE: return empty string if the dir isn't available, even in the doc says otherwise
82 return userDownloadDirFetcher.optValue().value_or(QLatin1String());
83 case PublicShareLocation:
84 // NOT SUPPORTED
85 break;
86 case TemplatesLocation:
87 // NOT SUPPORTED
88 break;
89 case TempLocation:
90 return QDir::tempPath();
91 case HomeLocation:
92 return QDir::homePath();
93 case GenericDataLocation:
94 case AppDataLocation:
95 case StateLocation:
96 case GenericStateLocation:
97 return QOhosAppContext::getProperty(QOhosAppContext::Type::filesDir) + testDirSuffix;
98 case CacheLocation:
99 case GenericCacheLocation:
100 return QOhosAppContext::getProperty(QOhosAppContext::Type::cacheDir) + testDirSuffix;
101 case RuntimeLocation:
102 case ConfigLocation:
103 case GenericConfigLocation:
104 case AppConfigLocation:
105 case AppLocalDataLocation:
106 return QOhosAppContext::getProperty(QOhosAppContext::Type::preferencesDir) + testDirSuffix;
107 default:
108 break;
109 }
110
111 return QLatin1String();
112}
113
114QT_END_NAMESPACE
115
116#endif // QT_NO_STANDARDPATHS
QOhosMemoizingJsThreadFetcher< QString > userDesktopDirFetcher(tryGetUserDirFromOhEnvironment< OH_Environment_GetUserDesktopDir >, "OH_Environment_GetUserDesktopDir()")
QOhosMemoizingJsThreadFetcher< QString > userDocumentDirFetcher(tryGetUserDirFromOhEnvironment< OH_Environment_GetUserDocumentDir >, "OH_Environment_GetUserDocumentDir()")
std::optional< QString > tryGetUserDirFromOhEnvironment(QOhosJsState &)
QOhosMemoizingJsThreadFetcher< QString > userDownloadDirFetcher(tryGetUserDirFromOhEnvironment< OH_Environment_GetUserDownloadDir >, "OH_Environment_GetUserDownloadDir()")