From 5a284d5c6698bd7ee0420a80548a00557fb0e651 Mon Sep 17 00:00:00 2001 From: shenlebantongying Date: Sun, 27 Nov 2022 20:24:49 -0500 Subject: [PATCH] Implement "goldendict://nice" url scheme handler for windows and linux (#227) * windows: add url scheme goldendict:// * linux: add url scheme goldendict:// handler --- main.cc | 8 ++++++++ mainwindow.cc | 15 +++++++++++++++ redist/org.goldendict.GoldenDict.desktop | 3 ++- 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/main.cc b/main.cc index 28afaba6..a6fee949 100644 --- a/main.cc +++ b/main.cc @@ -183,6 +183,14 @@ logFile( false ) } else word = arguments[ i ]; +#if defined(Q_OS_LINUX) || defined (Q_OS_WIN) + // handle url scheme like "goldendict://" on windows + word.remove("goldendict://"); + // In microsoft Words, the / will be automatically appended + if(word.endsWith("/")){ + word.chop(1); + } +#endif } } } diff --git a/mainwindow.cc b/mainwindow.cc index 9dfe5fac..72530b33 100644 --- a/mainwindow.cc +++ b/mainwindow.cc @@ -912,6 +912,21 @@ MainWindow::MainWindow( Config::Class & cfg_ ): inspector.reset( new ArticleInspector( this )); connect( QApplication::clipboard(), &QClipboard::changed, this, &MainWindow::clipboardChange ); + +#ifdef Q_OS_WIN + // Regiser and update URL Scheme for windows + // https://learn.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/platform-apis/aa767914(v=vs.85) + + // Windows will automatically map registry key to Computer\HKEY_CLASSES_ROOT\ */ + QSettings urlRegistry(R"(HKEY_CURRENT_USER\Software\Classes)", QSettings::NativeFormat); + + urlRegistry.beginGroup("goldendict"); + urlRegistry.setValue("Default", "URL: goldendict Protocol"); + urlRegistry.setValue("URL Protocol", ""); + urlRegistry.setValue("shell/open/command/Default", + QString("\"%1\"").arg( QDir::toNativeSeparators(QApplication::applicationFilePath())) + " \"%1\""); + urlRegistry.endGroup(); +#endif } void MainWindow::clipboardChange( QClipboard::Mode m) diff --git a/redist/org.goldendict.GoldenDict.desktop b/redist/org.goldendict.GoldenDict.desktop index 2eb159b4..ce6059fd 100755 --- a/redist/org.goldendict.GoldenDict.desktop +++ b/redist/org.goldendict.GoldenDict.desktop @@ -6,4 +6,5 @@ Name=GoldenDict GenericName=Multiformat Dictionary Comment=A feature-rich dictionary lookup program Icon=goldendict -Exec=goldendict +Exec=goldendict %u +MimeType=x-scheme-handler/goldendict