File indexing completed on 2024-05-19 05:54:12

0001 /*  This file was part of the KDE libraries
0002 
0003     SPDX-FileCopyrightText: 2021 Tomaz Canabrava <tcanabrava@kde.org>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #ifndef SSHMANAGERMODEL_H
0009 #define SSHMANAGERMODEL_H
0010 
0011 #include <QFileSystemWatcher>
0012 #include <QStandardItemModel>
0013 
0014 #include <memory>
0015 #include <optional>
0016 
0017 namespace Konsole
0018 {
0019 class SessionController;
0020 class Session;
0021 }
0022 
0023 class SSHConfigurationData;
0024 
0025 class SSHManagerModel : public QStandardItemModel
0026 {
0027     Q_OBJECT
0028 public:
0029     enum Roles {
0030         SSHRole = Qt::UserRole + 1,
0031     };
0032 
0033     explicit SSHManagerModel(QObject *parent = nullptr);
0034     ~SSHManagerModel() override;
0035 
0036     void setSessionController(Konsole::SessionController *controller);
0037 
0038     /** Connected to Session::hostnameChanged, tries to set the profile to
0039      * the current configured profile for the specified SSH host
0040      */
0041     Q_SLOT void triggerProfileChange(const QString &sshHost);
0042 
0043     QStandardItem *addTopLevelItem(const QString &toplevel);
0044     void addChildItem(const SSHConfigurationData &config, const QString &parentName);
0045     void editChildItem(const SSHConfigurationData &config, const QModelIndex &idx);
0046     void removeIndex(const QModelIndex &idx);
0047     bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;
0048     QStringList folders() const;
0049 
0050     Qt::ItemFlags flags(const QModelIndex &index) const override;
0051 
0052     void startImportFromSshConfig();
0053     void importFromSshConfigFile(const QString &file);
0054     void load();
0055     void save();
0056 
0057     bool hasHost(const QString &hostName) const;
0058     std::optional<QString> profileForHost(const QString &host) const;
0059     void setManageProfile(bool manage);
0060     bool getManageProfile();
0061 
0062 private:
0063     QStandardItem *m_sshConfigTopLevelItem = nullptr;
0064     QFileSystemWatcher m_sshConfigWatcher;
0065     Konsole::Session *m_session = nullptr;
0066 
0067     QHash<Konsole::Session *, QString> m_sessionToProfileName;
0068 
0069     bool manageProfile = false;
0070 };
0071 
0072 #endif