-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add setDefaultProtocolHandler() to qx-system
Allows for registering an application to be the default handler for a given scheme-based protocol.
- Loading branch information
1 parent
13553a0
commit 3929b0d
Showing
7 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#ifndef QX_SYSTEM_P_H | ||
#define QX_SYSTEM_P_H | ||
|
||
// Qt Includes | ||
#include <QString> | ||
|
||
namespace Qx | ||
{ | ||
/*! @cond */ | ||
|
||
//-Component Private Functions-------------------------------------------------------------------- | ||
bool registerUriScheme(const QString& scheme, const QString& name, const QString& command); | ||
|
||
/*! @endcond */ | ||
} | ||
|
||
#endif // QX_SYSTEM_P_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Unit Includes | ||
#include "qx-system_p.h" | ||
|
||
// Qt Includes | ||
|
||
using namespace Qt::Literals::StringLiterals; | ||
|
||
namespace Qx | ||
{ | ||
/*! @cond */ | ||
|
||
bool registerUriScheme(const QString& scheme, const QString& name, const QString& command) | ||
{ | ||
// Get desktop entry path | ||
QString XDG_DATA_HOME = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation); | ||
QString dEntryFilename = scheme + u"-scheme-handler.desktop"_s; | ||
QString dEntryPath = XDG_DATA_HOME + u"/applications/"_s + dEntryFilename; | ||
QString xSchemeHandler = u"x-scheme-handler/"_s + scheme; | ||
|
||
// Create desktop entry | ||
QSettings de(dEntryPath, QSettings::IniFormat); | ||
de.beginGroup(u"Desktop Entry"_s); | ||
de.setValue(u"Type"_s, u"Application"_s); | ||
de.setValue(u"Name"_s, name); | ||
de.setValue(u"Exec"_s, command + u" %u"_s); // %u is already passed as single param, no need for quotes | ||
de.setValue(u"StartupNotify"_s, u"false"_s); | ||
de.setValue(u"MimeType"_s, xSchemeHandler); | ||
de.endGroup(); | ||
|
||
de.sync(); | ||
if(de.status() != QSettings::NoError) | ||
return false; | ||
|
||
// Register MIME type | ||
QProcess xdgMime; | ||
xdgMime.setProgram(u"xdg-mime"_s); | ||
xdgMime.setArguments({u"default"_s, dEntryFilename, xSchemeHandler}); | ||
xdgMime.setStandardOutputFile(QProcess::nullDevice()); | ||
xdgMime.setStandardErrorFile(QProcess::nullDevice()); | ||
xdgMime.start(); | ||
|
||
return xdgMime.waitForFinished(3000) && xdgMime.exitStatus() == xdgMime.NormalExit && xdgMime.exitCode() == 0; | ||
|
||
// Alternatively "xdg-settings set default-url-scheme-handler *scheme* *.desktop_file*" can be used | ||
} | ||
|
||
/*! @endcond */ | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Unit Includes | ||
#include "qx-system_p.h" | ||
|
||
// Qt Includes | ||
#include <QSettings> | ||
|
||
using namespace Qt::Literals::StringLiterals; | ||
|
||
namespace Qx | ||
{ | ||
/*! @cond */ | ||
|
||
bool registerUriScheme(const QString& scheme, const QString& name, const QString& command) | ||
{ | ||
/* Set registry keys | ||
* | ||
* The example registry key root used in the MS documentation is HKEY_CLASSES_ROOT, which is | ||
* a merged view of system-wide and user-specific settings, that defaults to updating the | ||
* system-wide settings when written to, and therefore requires admin priviledges. So instead | ||
* we use HKEY_CURRENT_USER\SOFTWARE\Classes which is just the user-specific section. | ||
*/ | ||
QSettings schemeKey(u"HKEY_CURRENT_USER\\SOFTWARE\\Classes"_s + scheme, QSettings::NativeFormat); | ||
schemeKey.setValue(u"."_s, name); | ||
schemeKey.setValue("URL Protocol", ""); | ||
schemeKey.setValue(u"shell/open/command/."_s, command + uR"( "%1")"_s); | ||
|
||
// Save and return status | ||
schemeKey.sync(); | ||
return schemeKey.status() == QSettings::NoError; | ||
|
||
/* NOTE: The Microsoft specification recommends adding a DefaultIcon key to these entries | ||
* with an executable based icon path, though I'm not sure if/how that's actually | ||
* used. If that is ever added, we should check if adding an icon to the desktop entry | ||
* of the Linux equivalent has an appreciable effect as well. | ||
*/ | ||
} | ||
|
||
/*! @endcond */ | ||
} | ||
|
||
|