1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtCore module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39 
40 #include "qstandardpaths.h"
41 
42 #include <qdir.h>
43 #include <private/qsystemlibrary_p.h>
44 #include <qcoreapplication.h>
45 #include <qstringlist.h>
46 
47 #include <qt_windows.h>
48 
49 #include <wrl.h>
50 #include <windows.foundation.h>
51 #include <windows.storage.h>
52 #include <Windows.ApplicationModel.h>
53 
54 using namespace Microsoft::WRL;
55 using namespace Microsoft::WRL::Wrappers;
56 using namespace ABI::Windows::Foundation;
57 using namespace ABI::Windows::Storage;
58 using namespace ABI::Windows::ApplicationModel;
59 
60 #ifndef QT_NO_STANDARDPATHS
61 
62 QT_BEGIN_NAMESPACE
63 
convertCharArray(const wchar_t * path)64 static QString convertCharArray(const wchar_t *path)
65 {
66     return QDir::fromNativeSeparators(QString::fromWCharArray(path));
67 }
68 
writableLocation(StandardLocation type)69 QString QStandardPaths::writableLocation(StandardLocation type)
70 {
71     QString result;
72 
73     switch (type) {
74     case ConfigLocation: // same as AppLocalDataLocation, on Windows
75     case GenericConfigLocation: // same as GenericDataLocation, on Windows
76     case AppConfigLocation:
77     case AppDataLocation:
78     case AppLocalDataLocation:
79     case GenericDataLocation: {
80         ComPtr<IApplicationDataStatics> applicationDataStatics;
81         if (FAILED(GetActivationFactory(HString::MakeReference(RuntimeClass_Windows_Storage_ApplicationData).Get(), &applicationDataStatics)))
82             break;
83         ComPtr<IApplicationData> applicationData;
84         if (FAILED(applicationDataStatics->get_Current(&applicationData)))
85             break;
86         ComPtr<IStorageFolder> settingsFolder;
87         if (FAILED(applicationData->get_LocalFolder(&settingsFolder)))
88             break;
89         ComPtr<IStorageItem> settingsFolderItem;
90         if (FAILED(settingsFolder.As(&settingsFolderItem)))
91             break;
92         HString path;
93         if (FAILED(settingsFolderItem->get_Path(path.GetAddressOf())))
94             break;
95         result = convertCharArray(path.GetRawBuffer(nullptr));
96         if (isTestModeEnabled())
97             result += QLatin1String("/qttest");
98         break;
99     }
100     case CacheLocation:
101         return writableLocation(AppLocalDataLocation) + QLatin1String("/cache");
102 
103     case GenericCacheLocation:
104         return writableLocation(GenericDataLocation) + QLatin1String("/cache");
105 
106     case TempLocation:
107         result = QDir::tempPath();
108         break;
109 
110     case ApplicationsLocation:
111     case DesktopLocation:
112     case FontsLocation:
113     case HomeLocation:
114     case RuntimeLocation:
115         // these are read-only
116         break;
117 
118     case DocumentsLocation:
119     case MusicLocation:
120     case MoviesLocation:
121     case PicturesLocation:
122     case DownloadLocation:
123     default:
124         Q_UNIMPLEMENTED();
125     }
126     return result;
127 
128 }
129 
standardLocations(StandardLocation type)130 QStringList QStandardPaths::standardLocations(StandardLocation type)
131 {
132     const QString writable = writableLocation(type);
133     return writable.isEmpty() ? QStringList() : QStringList(writable);
134 }
135 
136 QT_END_NAMESPACE
137 
138 #endif // QT_NO_STANDARDPATHS
139