1 /****************************************************************************
2 **
3 ** Copyright (C) 2019 Volker Krause <vkrause@kde.org>
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the plugins 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 "androidcontentfileengine.h"
41 
42 #include <private/qjni_p.h>
43 #include <private/qjnihelpers_p.h>
44 
45 #include <QDebug>
46 
AndroidContentFileEngine(const QString & f)47 AndroidContentFileEngine::AndroidContentFileEngine(const QString &f)
48     : m_file(f)
49 {
50     setFileName(f);
51 }
52 
open(QIODevice::OpenMode openMode)53 bool AndroidContentFileEngine::open(QIODevice::OpenMode openMode)
54 {
55     QString openModeStr;
56     if (openMode & QFileDevice::ReadOnly) {
57         openModeStr += QLatin1Char('r');
58     }
59     if (openMode & QFileDevice::WriteOnly) {
60         openModeStr += QLatin1Char('w');
61     }
62     if (openMode & QFileDevice::Truncate) {
63         openModeStr += QLatin1Char('t');
64     } else if (openMode & QFileDevice::Append) {
65         openModeStr += QLatin1Char('a');
66     }
67 
68     const auto fd = QJNIObjectPrivate::callStaticMethod<jint>("org/qtproject/qt5/android/QtNative",
69         "openFdForContentUrl",
70         "(Landroid/content/Context;Ljava/lang/String;Ljava/lang/String;)I",
71         QtAndroidPrivate::context(),
72         QJNIObjectPrivate::fromString(fileName(DefaultName)).object(),
73         QJNIObjectPrivate::fromString(openModeStr).object());
74 
75     if (fd < 0) {
76         return false;
77     }
78 
79     return QFSFileEngine::open(openMode, fd, QFile::AutoCloseHandle);
80 }
81 
size() const82 qint64 AndroidContentFileEngine::size() const
83 {
84     const jlong size = QJNIObjectPrivate::callStaticMethod<jlong>(
85             "org/qtproject/qt5/android/QtNative", "getSize",
86             "(Landroid/content/Context;Ljava/lang/String;)J", QtAndroidPrivate::context(),
87             QJNIObjectPrivate::fromString(fileName(DefaultName)).object());
88     return (qint64)size;
89 }
90 
fileFlags(FileFlags type) const91 AndroidContentFileEngine::FileFlags AndroidContentFileEngine::fileFlags(FileFlags type) const
92 {
93     FileFlags commonFlags(ReadOwnerPerm|ReadUserPerm|ReadGroupPerm|ReadOtherPerm|ExistsFlag);
94     FileFlags flags;
95     const bool isDir = QJNIObjectPrivate::callStaticMethod<jboolean>(
96             "org/qtproject/qt5/android/QtNative", "checkIfDir",
97             "(Landroid/content/Context;Ljava/lang/String;)Z", QtAndroidPrivate::context(),
98             QJNIObjectPrivate::fromString(fileName(DefaultName)).object());
99     // If it is a directory then we know it exists so there is no reason to explicitly check
100     const bool exists = isDir ? true : QJNIObjectPrivate::callStaticMethod<jboolean>(
101             "org/qtproject/qt5/android/QtNative", "checkFileExists",
102             "(Landroid/content/Context;Ljava/lang/String;)Z", QtAndroidPrivate::context(),
103             QJNIObjectPrivate::fromString(fileName(DefaultName)).object());
104     if (!exists && !isDir)
105         return flags;
106     if (isDir) {
107         flags = DirectoryType | commonFlags;
108     } else {
109         flags = FileType | commonFlags;
110         const bool writable = QJNIObjectPrivate::callStaticMethod<jboolean>(
111             "org/qtproject/qt5/android/QtNative", "checkIfWritable",
112             "(Landroid/content/Context;Ljava/lang/String;)Z", QtAndroidPrivate::context(),
113             QJNIObjectPrivate::fromString(fileName(DefaultName)).object());
114         if (writable)
115             flags |= WriteOwnerPerm|WriteUserPerm|WriteGroupPerm|WriteOtherPerm;
116     }
117     return type & flags;
118 }
119 
fileName(FileName f) const120 QString AndroidContentFileEngine::fileName(FileName f) const
121 {
122     switch (f) {
123         case PathName:
124         case AbsolutePathName:
125         case CanonicalPathName:
126         case DefaultName:
127         case AbsoluteName:
128         case CanonicalName:
129             return m_file;
130         case BaseName:
131         {
132             const int pos = m_file.lastIndexOf(QChar(QLatin1Char('/')));
133             return m_file.mid(pos);
134         }
135         default:
136             return QString();
137     }
138 }
139 
beginEntryList(QDir::Filters filters,const QStringList & filterNames)140 QAbstractFileEngine::Iterator *AndroidContentFileEngine::beginEntryList(QDir::Filters filters, const QStringList &filterNames)
141 {
142     return new AndroidContentFileEngineIterator(filters, filterNames);
143 }
144 
endEntryList()145 QAbstractFileEngine::Iterator *AndroidContentFileEngine::endEntryList()
146 {
147     return nullptr;
148 }
149 
150 AndroidContentFileEngineHandler::AndroidContentFileEngineHandler() = default;
151 AndroidContentFileEngineHandler::~AndroidContentFileEngineHandler() = default;
152 
create(const QString & fileName) const153 QAbstractFileEngine* AndroidContentFileEngineHandler::create(const QString &fileName) const
154 {
155     if (!fileName.startsWith(QLatin1String("content"))) {
156         return nullptr;
157     }
158 
159     return new AndroidContentFileEngine(fileName);
160 }
161 
AndroidContentFileEngineIterator(QDir::Filters filters,const QStringList & filterNames)162 AndroidContentFileEngineIterator::AndroidContentFileEngineIterator(QDir::Filters filters,
163                                                                    const QStringList &filterNames)
164     : QAbstractFileEngineIterator(filters, filterNames)
165 {
166 }
167 
~AndroidContentFileEngineIterator()168 AndroidContentFileEngineIterator::~AndroidContentFileEngineIterator()
169 {
170 }
171 
next()172 QString AndroidContentFileEngineIterator::next()
173 {
174     if (!hasNext())
175         return QString();
176     ++m_index;
177     return currentFilePath();
178 }
179 
hasNext() const180 bool AndroidContentFileEngineIterator::hasNext() const
181 {
182     if (m_index == -1) {
183         if (path().isEmpty())
184             return false;
185         const bool isDir = QJNIObjectPrivate::callStaticMethod<jboolean>(
186                              "org/qtproject/qt5/android/QtNative", "checkIfDir",
187                              "(Landroid/content/Context;Ljava/lang/String;)Z",
188                              QtAndroidPrivate::context(),
189                              QJNIObjectPrivate::fromString(path()).object());
190         if (isDir) {
191             QJNIObjectPrivate objArray = QJNIObjectPrivate::callStaticObjectMethod("org/qtproject/qt5/android/QtNative",
192                                            "listContentsFromTreeUri",
193                                            "(Landroid/content/Context;Ljava/lang/String;)[Ljava/lang/String;",
194                                            QtAndroidPrivate::context(),
195                                            QJNIObjectPrivate::fromString(path()).object());
196             if (objArray.isValid()) {
197                 QJNIEnvironmentPrivate env;
198                 const jsize length = env->GetArrayLength(static_cast<jarray>(objArray.object()));
199                 for (int i = 0; i != length; ++i) {
200                     m_entries << QJNIObjectPrivate(env->GetObjectArrayElement(
201                                 static_cast<jobjectArray>(objArray.object()), i)).toString();
202                 }
203             }
204         }
205         m_index = 0;
206     }
207     return m_index < m_entries.size();
208 }
209 
currentFileName() const210 QString AndroidContentFileEngineIterator::currentFileName() const
211 {
212     if (m_index <= 0 || m_index > m_entries.size())
213         return QString();
214     return m_entries.at(m_index - 1);
215 }
216