sshmanagerplugin.cpp 2.7 KB
Newer Older
Tomaz  Canabrava's avatar
Tomaz Canabrava committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*  This file was part of the KDE libraries

    SPDX-FileCopyrightText: 2021 Tomaz Canabrava <tcanabrava@kde.org>

    SPDX-License-Identifier: GPL-2.0-or-later
*/

#include "sshmanagerplugin.h"

#include "sshmanagerpluginwidget.h"
#include "sshmanagermodel.h"

#include "session/SessionController.h"

#include <QMainWindow>
#include <QDockWidget>
#include <QListView>
#include <QTimer>
#include <QMenuBar>

#include <KLocalizedString>
#include <KActionCollection>

#include "MainWindow.h"

K_PLUGIN_CLASS_WITH_JSON(SSHManagerPlugin, "konsole_sshmanager.json")

struct SSHManagerPluginPrivate {
    SSHManagerModel model;

    QMap<Konsole::MainWindow*, SSHManagerTreeWidget *> widgetForWindow;
    QMap<Konsole::MainWindow*, QDockWidget *> dockForWindow;
};

SSHManagerPlugin::SSHManagerPlugin(QObject *object, const QVariantList &args)
: Konsole::IKonsolePlugin(object, args)
, d(std::make_unique<SSHManagerPluginPrivate>())
{
    setName(QStringLiteral("SshManager"));
}

SSHManagerPlugin::~SSHManagerPlugin() = default;

void SSHManagerPlugin::createWidgetsForMainWindow(Konsole::MainWindow *mainWindow)
{
    auto *sshDockWidget = new QDockWidget(mainWindow);
    auto *managerWidget = new SSHManagerTreeWidget();
    managerWidget->setModel(&d->model);
    sshDockWidget->setWidget(managerWidget);
    sshDockWidget->setWindowTitle(i18n("SSH Manager"));
    sshDockWidget->setObjectName(QStringLiteral("SSHManagerDock"));
    sshDockWidget->setVisible(false);

    mainWindow->addDockWidget(Qt::LeftDockWidgetArea, sshDockWidget);

    d->widgetForWindow[mainWindow] = managerWidget;
    d->dockForWindow[mainWindow] = sshDockWidget;

    connect(managerWidget, &SSHManagerTreeWidget::requestNewTab, this, [mainWindow]{
        mainWindow->newTab();
    });
}

QList<QAction *> SSHManagerPlugin::menuBarActions(Konsole::MainWindow* mainWindow) const
{
    Q_UNUSED(mainWindow);

68
    QAction *toggleVisibilityAction = new QAction(i18n("Show SSH Manager"), mainWindow);
Tomaz  Canabrava's avatar
Tomaz Canabrava committed
69
70
71
    toggleVisibilityAction->setCheckable(true);
    toggleVisibilityAction->setChecked(false);

72
73
    connect(toggleVisibilityAction, &QAction::triggered,
            d->dockForWindow[mainWindow], &QDockWidget::setVisible);
Tomaz  Canabrava's avatar
Tomaz Canabrava committed
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92

    return {toggleVisibilityAction};
}

void SSHManagerPlugin::activeViewChanged(Konsole::SessionController *controller)
{
    auto mainWindow = qobject_cast<Konsole::MainWindow*>(controller->view()->topLevelWidget());

    // if we don't get a mainwindow here this *might* be just opening, call it again
    // later on.
    if (!mainWindow) {
        QTimer::singleShot(500, this, [this, controller]{ activeViewChanged(controller); });
        return;
    }

    d->widgetForWindow[mainWindow]->setCurrentController(controller);
}

#include "sshmanagerplugin.moc"