Respect users prefered text editor for shared text

Summary:
A user may have Kate installed but another editor set as default for text/plain. Respect that.

Also add .txt extension to the temp file name to make mime-type detection easier.

BUG: 399174

Test Plan:
Set default text editor to Kate -> Text opens in Kate
Set default text editor to Atom -> Text opens in Atom

Reviewers: #kde_connect, broulik, apol

Reviewed By: #kde_connect, broulik, apol

Subscribers: kdeconnect

Tags: #kde_connect

Differential Revision: https://phabricator.kde.org/D15813
This commit is contained in:
Nicolas Fella 2019-02-09 00:00:01 +01:00
parent e298b73f8e
commit 47749792f3
3 changed files with 10 additions and 3 deletions

View file

@ -24,7 +24,7 @@ if (SAILFISHOS)
else() else()
set(KF5_MIN_VERSION "5.42.0") set(KF5_MIN_VERSION "5.42.0")
set(QT_MIN_VERSION "5.7.0") set(QT_MIN_VERSION "5.7.0")
set(KF5_REQUIRED_COMPONENTS I18n ConfigWidgets DBusAddons IconThemes Notifications KIO KCMUtils) set(KF5_REQUIRED_COMPONENTS I18n ConfigWidgets DBusAddons IconThemes Notifications KIO KCMUtils Service)
set(KF5_OPTIONAL_COMPONENTS DocTools) set(KF5_OPTIONAL_COMPONENTS DocTools)
if(UNIX) if(UNIX)
set(KF5_OPTIONAL_COMPONENTS ${KF5_OPTIONAL_COMPONENTS} PulseAudioQt Runner) set(KF5_OPTIONAL_COMPONENTS ${KF5_OPTIONAL_COMPONENTS} PulseAudioQt Runner)

View file

@ -10,6 +10,7 @@ target_link_libraries(kdeconnect_share
KF5::Notifications KF5::Notifications
KF5::I18n KF5::I18n
KF5::KIOWidgets KF5::KIOWidgets
KF5::Service
) )
####################################### #######################################

View file

@ -33,6 +33,7 @@
#include <KJobTrackerInterface> #include <KJobTrackerInterface>
#include <KPluginFactory> #include <KPluginFactory>
#include <KIO/MkpathJob> #include <KIO/MkpathJob>
#include <KMimeTypeTrader>
#include "core/filetransferjob.h" #include "core/filetransferjob.h"
@ -121,14 +122,19 @@ bool SharePlugin::receivePacket(const NetworkPacket& np)
} }
} else if (np.has(QStringLiteral("text"))) { } else if (np.has(QStringLiteral("text"))) {
QString text = np.get<QString>(QStringLiteral("text")); QString text = np.get<QString>(QStringLiteral("text"));
if (!QStandardPaths::findExecutable(QStringLiteral("kate")).isEmpty()) {
KService::Ptr service = KMimeTypeTrader::self()->preferredService(QStringLiteral("text/plain"));
const QString defaultApp = service ? service->desktopEntryName() : QString();
if (defaultApp == QLatin1String("org.kde.kate") || defaultApp == QLatin1String("org.kde.kwrite")) {
QProcess* proc = new QProcess(); QProcess* proc = new QProcess();
connect(proc, SIGNAL(finished(int)), proc, SLOT(deleteLater())); connect(proc, SIGNAL(finished(int)), proc, SLOT(deleteLater()));
proc->start(QStringLiteral("kate"), QStringList(QStringLiteral("--stdin"))); proc->start(defaultApp.section('.', 2,2), QStringList(QStringLiteral("--stdin")));
proc->write(text.toUtf8()); proc->write(text.toUtf8());
proc->closeWriteChannel(); proc->closeWriteChannel();
} else { } else {
QTemporaryFile tmpFile; QTemporaryFile tmpFile;
tmpFile.setFileTemplate(QStringLiteral("kdeconnect-XXXXXX.txt"));
tmpFile.setAutoRemove(false); tmpFile.setAutoRemove(false);
tmpFile.open(); tmpFile.open();
tmpFile.write(text.toUtf8()); tmpFile.write(text.toUtf8());