Commit a7dd47d1 authored by Sharaf Zaman's avatar Sharaf Zaman
Browse files

Bugfix: "Also Save as Kra" doesn't work on Android

when alsoAsKra is checked we use file picker to ask for the location to
save Kra. Picker will try to find the parent location of the current file.

BUG:424612
parent 194bc293
From dfe4cf315522dc0efb3d74a79d660ce6581118e2 Mon Sep 17 00:00:00 2001
From: Sharaf Zaman <sharafzaz121@gmail.com>
Date: Mon, 9 Nov 2020 08:40:57 +0000
Subject: [PATCH] Android: use EXTRA_INITIAL_URI as the defaultDir
on API Level >= 26, Storage Access Framework attempts to use the URI to
save a new file at its parent location (i.e the parent directory).
---
.../qandroidplatformfiledialoghelper.cpp | 20 +++++++++++++++++++
1 file changed, 20 insertions(+)
diff --git a/src/plugins/platforms/android/qandroidplatformfiledialoghelper.cpp b/src/plugins/platforms/android/qandroidplatformfiledialoghelper.cpp
index 05e4ccbbee..00b5b0887c 100644
--- a/src/plugins/platforms/android/qandroidplatformfiledialoghelper.cpp
+++ b/src/plugins/platforms/android/qandroidplatformfiledialoghelper.cpp
@@ -61,6 +61,11 @@ QAndroidPlatformFileDialogHelper::QAndroidPlatformFileDialogHelper()
{
}
+void QAndroidPlatformFileDialogHelper::setDirectory(const QUrl &directory)
+{
+ m_directory = directory;
+}
+
bool QAndroidPlatformFileDialogHelper::handleActivityResult(jint requestCode, jint resultCode, jobject data)
{
if (requestCode != REQUEST_CODE)
@@ -125,6 +130,20 @@ void QAndroidPlatformFileDialogHelper::setIntentTitle(const QString &title)
extraTitle.object(), QJNIObjectPrivate::fromString(title).object());
}
+void QAndroidPlatformFileDialogHelper::setInitialUri()
+{
+ if (QtAndroidPrivate::androidSdkVersion() >= 26) {
+ const QJNIObjectPrivate extraInitialUri = QJNIObjectPrivate::getStaticObjectField(
+ "android/provider/DocumentsContract",
+ "EXTRA_INITIAL_URI",
+ "Ljava/lang/String;");
+ m_intent.callObjectMethod("putExtra",
+ "(Ljava/lang/String;Ljava/lang/String;)Landroid/content/Intent;",
+ extraInitialUri.object(),
+ QJNIObjectPrivate::fromString(m_directory.toString()).object());
+ }
+}
+
void QAndroidPlatformFileDialogHelper::setOpenableCategory()
{
const QJNIObjectPrivate CATEGORY_OPENABLE = QJNIObjectPrivate::getStaticObjectField(
@@ -229,6 +248,7 @@ bool QAndroidPlatformFileDialogHelper::show(Qt::WindowFlags windowFlags, Qt::Win
}
setIntentTitle(options()->windowTitle());
+ setInitialUri();
QtAndroidPrivate::registerActivityResultListener(this);
m_activity.callMethod<void>("startActivityForResult", "(Landroid/content/Intent;I)V",
--
2.28.0
......@@ -141,6 +141,7 @@ elseif (ANDROID)
COMMAND ${PATCH_COMMAND} -p1 -d qtbase -i ${CMAKE_CURRENT_SOURCE_DIR}/0051-Android-Add-clipboard-buffer-for-mimeTypes-that-aren.patch
COMMAND ${PATCH_COMMAND} -p1 -d qtbase -i ${CMAKE_CURRENT_SOURCE_DIR}/0052-Android-Move-events-handling-to-QtLayout.patch
COMMAND ${PATCH_COMMAND} -p1 -d qtbase -i ${CMAKE_CURRENT_SOURCE_DIR}/0053-Android-Use-processOpenModeFlags-to-get-right-OpenMo.patch
COMMAND ${PATCH_COMMAND} -p1 -d qtbase -i ${CMAKE_CURRENT_SOURCE_DIR}/0054-Android-use-EXTRA_INITIAL_URI-as-the-defaultDir.patch
COMMAND ${PATCH_COMMAND} -p1 -d qtbase -i ${CMAKE_CURRENT_SOURCE_DIR}/0105-Android-Don-t-hide-Menubar-even-if-MenuKey-is-presen.patch
COMMAND ${PATCH_COMMAND} -p1 -d qtbase -i ${CMAKE_CURRENT_SOURCE_DIR}/106-Android-don-t-use-getExternalStorageDirectory-and-ge.patch
......
......@@ -275,6 +275,18 @@ QString KisImportExportManager::askForAudioFileName(const QString &defaultDir, Q
return dialog.filename();
}
QString KisImportExportManager::getUriForAdditionalFile(const QString &defaultUri, QWidget *parent)
{
KoFileDialog dialog(parent, KoFileDialog::SaveFile, "Save Kra");
KIS_SAFE_ASSERT_RECOVER_NOOP(!defaultUri.isEmpty());
dialog.setDirectoryUrl(QUrl(defaultUri));
dialog.setMimeTypeFilters(QStringList("application/x-krita"));
return dialog.filename();
}
KisImportExportManager::ConversionResult KisImportExportManager::convert(KisImportExportManager::Direction direction, const QString &location, const QString& realLocation, const QString &mimeType, bool showWarnings, KisPropertiesConfigurationSP exportConfiguration, bool isAsync)
{
// export configuration is supported for export only
......@@ -637,7 +649,11 @@ KisImportExportErrorCode KisImportExportManager::doExport(const QString &locatio
doExportImpl(location, filter, exportConfiguration);
if (alsoAsKra && status.isOk()) {
#ifdef Q_OS_ANDROID
QString kraLocation = getUriForAdditionalFile(location, nullptr);
#else
QString kraLocation = location + ".kra";
#endif
QByteArray mime = m_document->nativeFormatMimeType();
QSharedPointer<KisImportExportFilter> filter(
filterForMimeType(QString::fromLatin1(mime), Export));
......
......@@ -126,6 +126,8 @@ public:
static QString askForAudioFileName(const QString &defaultDir, QWidget *parent);
static QString getUriForAdditionalFile(const QString &defaultUri, QWidget *parent);
private:
......
......@@ -60,6 +60,7 @@ public:
QString caption;
QString defaultDirectory;
QString proposedFileName;
QUrl defaultUri;
QStringList filterList;
QString defaultFilter;
QScopedPointer<QFileDialog> fileDialog;
......@@ -102,6 +103,11 @@ void KoFileDialog::setDefaultDir(const QString &defaultDir, bool force)
}
}
void KoFileDialog::setDirectoryUrl(const QUrl &defaultUri)
{
d->defaultUri = defaultUri;
}
void KoFileDialog::setImageFilters()
{
QStringList imageFilters;
......@@ -147,6 +153,9 @@ QString KoFileDialog::selectedMimeType() const
void KoFileDialog::createFileDialog()
{
d->fileDialog.reset(new QFileDialog(d->parent, d->caption, d->defaultDirectory + "/" + d->proposedFileName));
if (!d->defaultUri.isEmpty()) {
d->fileDialog->setDirectoryUrl(d->defaultUri);
}
KConfigGroup group = KSharedConfig::openConfig()->group("File Dialogs");
bool dontUseNative = true;
......
......@@ -72,6 +72,14 @@ public:
*/
void setDefaultDir(const QString &defaultDir, bool force = false);
/**
* @brief setDirectoryUrl set the default URI to defaultUri.
* @param defaultUri a Uri to a file from some ContentProvider
*
* Used only on Android.
*/
void setDirectoryUrl(const QUrl &defaultUri);
/**
* @brief setImageFilters sets the name filters for the file dialog to all
* image formats Qt's QImageReader supports.
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment