Skip to content

Commit 561d90c

Browse files
committedNov 14, 2018
[FEATURE] Add native platform interface for opening a terminal window at
a specified path And implement for Linux. Add a new context menu entry to directories in browser to open a terminal window at that directory on supported platforms
1 parent 22772ea commit 561d90c

File tree

5 files changed

+67
-2
lines changed

5 files changed

+67
-2
lines changed
 

‎src/app/browser/qgsinbuiltdataitemproviders.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,17 @@ void QgsAppDirectoryItemGuiProvider::populateContextMenu( QgsDataItem *item, QMe
175175
} );
176176
menu->addAction( openFolder );
177177

178+
if ( QgsGui::instance()->nativePlatformInterface()->capabilities() & QgsNative::NativeOpenTerminalAtPath )
179+
{
180+
QAction *openTerminal = new QAction( tr( "Open in Terminal…" ), menu );
181+
connect( openTerminal, &QAction::triggered, this, [ = ]
182+
{
183+
QgsGui::instance()->nativePlatformInterface()->openTerminalAtPath( directoryItem->dirPath() );
184+
} );
185+
menu->addAction( openTerminal );
186+
menu->addSeparator();
187+
}
188+
178189
QAction *propertiesAction = new QAction( tr( "Properties…" ), menu );
179190
connect( propertiesAction, &QAction::triggered, this, [ = ]
180191
{

‎src/native/linux/qgslinuxnative.cpp

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@
2323
#include <QtDBus/QtDBus>
2424
#include <QtDebug>
2525
#include <QImage>
26+
#include <QProcess>
2627

2728
QgsNative::Capabilities QgsLinuxNative::capabilities() const
2829
{
29-
return NativeDesktopNotifications | NativeFilePropertiesDialog;
30+
return NativeDesktopNotifications | NativeFilePropertiesDialog | NativeOpenTerminalAtPath;
3031
}
3132

3233
void QgsLinuxNative::initializeMainWindow( QWindow *,
@@ -138,6 +139,38 @@ void QgsLinuxNative::setApplicationBadgeCount( int count )
138139
QDBusConnection::sessionBus().send( message );
139140
}
140141

142+
bool QgsLinuxNative::openTerminalAtPath( const QString &path )
143+
{
144+
// logic adapted from https://askubuntu.com/a/227669,
145+
// https://github.com/Microsoft/vscode/blob/fec1775aa52e2124d3f09c7b2ac8f69c57309549/src/vs/workbench/parts/execution/electron-browser/terminal.ts
146+
QString term = QStringLiteral( "xterm" );
147+
const QString desktopSession = qgetenv( "DESKTOP_SESSION" );
148+
const QString currentDesktop = qgetenv( "XDG_CURRENT_DESKTOP" );
149+
const QString gdmSession = qgetenv( "GDMSESSION" );
150+
const bool isDebian = QFile::exists( QStringLiteral( "/etc/debian_version" ) );
151+
if ( isDebian )
152+
{
153+
term = QStringLiteral( "x-terminal-emulator" );
154+
}
155+
else if ( desktopSession.contains( QLatin1String( "gnome" ), Qt::CaseInsensitive ) ||
156+
currentDesktop.contains( QLatin1String( "gnome" ), Qt::CaseInsensitive ) ||
157+
currentDesktop.contains( QLatin1String( "unity" ), Qt::CaseInsensitive ) )
158+
{
159+
term = QStringLiteral( "gnome-terminal" );
160+
}
161+
else if ( desktopSession.contains( QLatin1String( "kde" ), Qt::CaseInsensitive ) ||
162+
currentDesktop.contains( QLatin1String( "kde" ), Qt::CaseInsensitive ) ||
163+
gdmSession.contains( QLatin1String( "kde" ), Qt::CaseInsensitive ) )
164+
{
165+
term = QStringLiteral( "konsole" );
166+
}
167+
168+
QStringList arguments;
169+
arguments << QStringLiteral( "--working-directory" )
170+
<< path;
171+
return QProcess::startDetached( term, QStringList(), path );
172+
}
173+
141174
/**
142175
* Automatic marshaling of a QImage for org.freedesktop.Notifications.Notify
143176
*

‎src/native/linux/qgslinuxnative.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class NATIVE_EXPORT QgsLinuxNative : public QgsNative
4646
void setApplicationProgress( double progress ) override;
4747
void hideApplicationProgress() override;
4848
void setApplicationBadgeCount( int count ) override;
49+
bool openTerminalAtPath( const QString &path ) override;
4950
NotificationResult showDesktopNotification( const QString &summary, const QString &body, const NotificationSettings &settings = NotificationSettings() ) override;
5051
private:
5152
QString mDesktopFile;

‎src/native/qgsnative.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,16 @@ void QgsNative::setApplicationBadgeCount( int )
7373

7474
}
7575

76+
bool QgsNative::hasDarkTheme()
77+
{
78+
return false;
79+
}
80+
81+
bool QgsNative::openTerminalAtPath( const QString & )
82+
{
83+
return false;
84+
}
85+
7686
QgsNative::NotificationResult QgsNative::showDesktopNotification( const QString &, const QString &, const NotificationSettings & )
7787
{
7888
NotificationResult result;

‎src/native/qgsnative.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class NATIVE_EXPORT QgsNative : public QObject
4545
{
4646
NativeDesktopNotifications = 1 << 1, //!< Native desktop notifications are supported. See showDesktopNotification().
4747
NativeFilePropertiesDialog = 1 << 2, //!< Platform can show a native "file" (or folder) properties dialog.
48+
NativeOpenTerminalAtPath = 1 << 3, //!< Platform can open a terminal (command line) at a specific path
4849
};
4950
Q_DECLARE_FLAGS( Capabilities, Capability )
5051

@@ -150,7 +151,16 @@ class NATIVE_EXPORT QgsNative : public QObject
150151
* Returns true if the operating system is set to utilize a "dark" theme.
151152
* \since QGIS 3.4
152153
*/
153-
virtual bool hasDarkTheme() {return false;}
154+
virtual bool hasDarkTheme();
155+
156+
/**
157+
* Opens a terminal (command line) window at the given \a path.
158+
*
159+
* This method is only supported when the interface returns the NativeOpenTerminalAtPath flag for capabilities().
160+
*
161+
* Returns true if terminal was successfully opened.
162+
*/
163+
virtual bool openTerminalAtPath( const QString &path );
154164

155165
/**
156166
* Notification settings, for use with showDesktopNotification().

0 commit comments

Comments
 (0)