Skip to content

Commit

Permalink
Share images instead of links
Browse files Browse the repository at this point in the history
  • Loading branch information
Bionus committed Jan 1, 2021
1 parent a350ea4 commit 3cd1d9d
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 12 deletions.
4 changes: 4 additions & 0 deletions src/dist/android/AndroidManifest.xml.in
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@
<!-- Extract android style -->
<meta-data android:name="android.app.extract_android_style" android:value="default"/>
</activity>

<provider android:name="android.support.v4.content.FileProvider" android:authorities="com.bionus.grabber.fileprovider" android:grantUriPermissions="true" android:exported="false">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/filepaths"/>
</provider>
</application>

<supports-screens android:anyDensity="true" android:normalScreens="true" android:smallScreens="true" android:largeScreens="true"/>
Expand Down
67 changes: 67 additions & 0 deletions src/dist/android/build.gradle.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
buildscript {
repositories {
google()
jcenter()
}

dependencies {
classpath 'com.android.tools.build:gradle:3.2.0'
}
}

repositories {
google()
jcenter()
}

apply plugin: 'com.android.application'

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar', '*.aar'])
compile 'com.android.support:support-v4:25.3.1'
}

android {
/*******************************************************
* The following variables:
* - androidBuildToolsVersion,
* - androidCompileSdkVersion
* - qt5AndroidDir - holds the path to qt android files
* needed to build any Qt application
* on Android.
*
* are defined in gradle.properties file. This file is
* updated by QtCreator and androiddeployqt tools.
* Changing them manually might break the compilation!
*******************************************************/

compileSdkVersion androidCompileSdkVersion.toInteger()

buildToolsVersion androidBuildToolsVersion

sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = [qt5AndroidDir + '/src', 'src', 'java']
aidl.srcDirs = [qt5AndroidDir + '/src', 'src', 'aidl']
res.srcDirs = [qt5AndroidDir + '/res', 'res']
resources.srcDirs = ['src']
renderscript.srcDirs = ['src']
assets.srcDirs = ['assets']
jniLibs.srcDirs = ['libs']
}
}

lintOptions {
abortOnError false
}

// Do not compress Qt binary resources file
aaptOptions {
noCompress 'rcc'
}

defaultConfig {
resConfigs "en"
}
}
5 changes: 5 additions & 0 deletions src/dist/android/res/xml/filepaths.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<cache-path name="cache" path="." />
<files-path name="files" path="." />
<external-path name="external" path="/" />
</paths>
20 changes: 19 additions & 1 deletion src/dist/android/src/com/bionus/grabber/ShareUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@

import org.qtproject.qt5.android.QtNative;

import java.io.File;
import java.lang.String;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;

import android.support.v4.content.FileProvider;

public class ShareUtils
{
private static String AUTHORITY = "com.bionus.grabber.fileprovider";

protected ShareUtils()
{}

Expand All @@ -32,10 +38,22 @@ public static boolean sendFile(String path, String mimeType, String title)
return false;
}

// Use a FileProvider to avoid "FileUriExposedException"
File imageFileToShare = new File(path);
Uri uri;
try {
uri = FileProvider.getUriForFile(QtNative.activity(), AUTHORITY, imageFileToShare);
} catch (IllegalArgumentException e) {
Log.d("com.bionus.grabber.ShareUtils.sendFile", "Cannot share file: " + e.toString());
return false;
}
Log.d("com.bionus.grabber.ShareUtils.sendFile", uri.toString());

Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(path));
sendIntent.putExtra(Intent.EXTRA_STREAM, uri);
sendIntent.setType(mimeType);
sendIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
QtNative.activity().startActivity(Intent.createChooser(sendIntent, title));

return true;
Expand Down
3 changes: 3 additions & 0 deletions src/gui-qml/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,7 @@ if(ANDROID)
KEYSTORE_PASSWORD ${KEYSTORE_PASSWORD}
DEPENDS ${ANDROID_EXTRA_LIBS})
add_dependencies("${PROJECT_NAME}_apk" android_copy_assets_qml)

# Override build.gradle
configure_file("${CMAKE_CURRENT_LIST_DIR}/../dist/android/build.gradle.in" "${QT_ANDROID_APP_BINARY_DIR}/build.gradle" @ONLY)
endif()
7 changes: 1 addition & 6 deletions src/gui-qml/src/components/ImageScreen.qml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import Grabber 1.0
import QtMultimedia 5.12
import QtQuick 2.12
import QtQuick.Controls 2.5
Expand All @@ -16,10 +15,6 @@ Page {
property bool showHd: false
property bool showTags: false

ShareUtils {
id: shareUtils
}

Component {
id: actionButtons

Expand All @@ -31,7 +26,7 @@ Page {

ToolButton {
icon.source: "/images/icons/share.png"
onClicked: shareUtils.share("Image", image.fileUrl)
onClicked: backend.shareImage(image.image)
}

ToolButton {
Expand Down
23 changes: 21 additions & 2 deletions src/gui-qml/src/main-screen.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "main-screen.h"
#include <QDir>
#include <QEventLoop>
#include <QMimeDatabase>
#include <QSettings>
#include <QSslSocket>
#include <QStandardPaths>
Expand All @@ -14,15 +15,16 @@
#include "models/site.h"
#include "models/source.h"
#include "settings.h"
#include "share/share-utils.h"
#include "utils/logging.h"
#include "models/qml-image.h"
#include "models/qml-site.h"

#define IMAGES_PER_PAGE 20


MainScreen::MainScreen(Profile *profile, QObject *parent)
: QObject(parent), m_profile(profile)
MainScreen::MainScreen(Profile *profile, ShareUtils *shareUtils, QObject *parent)
: QObject(parent), m_profile(profile), m_shareUtils(shareUtils)
{
connect(&Logger::getInstance(), &Logger::newLog, this, &MainScreen::newLog);
logSystemInformation(m_profile);
Expand Down Expand Up @@ -117,6 +119,23 @@ void MainScreen::downloadImage(const QSharedPointer<Image> &image)
downloader->save();
}

void MainScreen::shareImage(const QSharedPointer<Image> &image)
{
const QString filename = "image_to_share.%ext%";
const QString path = QStandardPaths::writableLocation(QStandardPaths::CacheLocation);

ImageDownloader downloader(m_profile, image, filename, path, 0, false, false, this);

QEventLoop loop;
QObject::connect(&downloader, &ImageDownloader::saved, &loop, &QEventLoop::quit, Qt::QueuedConnection);
downloader.save();
loop.exec();

const QString savePath = image->savePath();
const QString mimeType = QMimeDatabase().mimeTypeForFile(savePath).name();
m_shareUtils->sendFile(savePath, mimeType, "Share image");
}

QString MainScreen::addSite(const QString &type, const QString &host, bool https)
{
const auto sources = m_profile->getSources().values();
Expand Down
5 changes: 4 additions & 1 deletion src/gui-qml/src/main-screen.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Profile;
class QmlImage;
class QmlSite;
class Settings;
class ShareUtils;

class MainScreen : public QObject
{
Expand All @@ -27,7 +28,7 @@ class MainScreen : public QObject
Q_PROPERTY(QString settingsFileName READ settingsFileName CONSTANT)

public:
explicit MainScreen(Profile *profile, QObject *parent = nullptr);
explicit MainScreen(Profile *profile, ShareUtils *shareUtils, QObject *parent = nullptr);
const QString &query() const { return m_query; }
const QList<QmlImage*> &results() const { return m_results; }
const QString &log() const { return m_log; }
Expand All @@ -41,6 +42,7 @@ class MainScreen : public QObject
void search(const QString &site, const QString &query, int page, const QString &postFilter);
void newLog(const QString &message);
void downloadImage(const QSharedPointer<Image> &image);
void shareImage(const QSharedPointer<Image> &image);
QString addSite(const QString &type, const QString &host, bool https);
QString toLocalFile(const QString &url);
QString getBlacklist();
Expand Down Expand Up @@ -71,6 +73,7 @@ class MainScreen : public QObject

private:
Profile *m_profile;
ShareUtils *m_shareUtils;
QString m_query;
QList<QmlImage*> m_results;
QString m_log;
Expand Down
6 changes: 4 additions & 2 deletions src/gui-qml/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ int main(int argc, char *argv[])
app.setOrganizationDomain("bionus.fr.cr");

qmlRegisterType<StatusBar>("StatusBar", 0, 1, "StatusBar");
qmlRegisterType<ShareUtils>("Grabber", 1, 0, "ShareUtils");
qmlRegisterType<SyntaxHighlighterHelper>("Grabber", 1, 0, "SyntaxHighlighterHelper");
qRegisterMetaType<QSharedPointer<Image>>("QSharedPointer<Image>");
qRegisterMetaType<Settings*>("Settings*");
Expand Down Expand Up @@ -95,8 +94,11 @@ int main(int argc, char *argv[])
engine.rootContext()->setContextProperty("NIGHTLY_COMMIT", QString());
#endif

ShareUtils shareUtils(nullptr);
engine.rootContext()->setContextProperty("shareUtils", &shareUtils);

Profile profile(savePath());
MainScreen mainScreen(&profile, &engine);
MainScreen mainScreen(&profile, &shareUtils, &engine);
engine.setObjectOwnership(&mainScreen, QQmlEngine::CppOwnership);
engine.rootContext()->setContextProperty("backend", &mainScreen);

Expand Down

0 comments on commit 3cd1d9d

Please sign in to comment.