Guitar
SshDialog.h
Go to the documentation of this file.
1 #ifndef SSHDIALOG_H
2 #define SSHDIALOG_H
3 #ifdef UNSAFE_ENABLED
4 
5 #include <QDialog>
6 
7 namespace Ui {
8 class Dialog;
9 }
10 
11 class QListWidgetItem;
12 
13 class SshConnection {
14 public:
15  struct FileItem {
16  std::string name;
17  bool isdir = false;
18  FileItem() = default;
19  FileItem(std::string const &n, bool d = false)
20  : name(n)
21  , isdir(d)
22  {
23  }
24  };
25 private:
26  struct Private;
27  Private *m;
28 
29  SshConnection(SshConnection const &) = delete;
30  SshConnection &operator=(SshConnection const &) = delete;
31  SshConnection(SshConnection &&) = delete;
32  SshConnection &operator=(SshConnection &&) = delete;
33 public:
34  SshConnection();
35  ~SshConnection();
36  bool connect(const std::string &host, int port, bool passwd, const std::string &uid = {}, const std::string &pwd = {});
37  void disconnect();
38  bool is_sftp_connected() const;
39  void add_allowed_command(const std::string &command);
40  std::optional<std::string> exec(char const *command);
41  std::optional<std::vector<FileItem>> list(const char *path);
42  std::optional<std::string> pull(char const *path);
43  static void sort(std::vector<FileItem> *files);
44  bool open_sftp();
45  void close_sftp();
46  bool open_channel();
47  void close_channel();
48  bool unlink(char const *path);
49 };
50 
51 class SshDialog : public QDialog {
52  Q_OBJECT
53 private:
54  struct Private;
55  Private *m;
56 
57  Ui::Dialog *ui;
58 
59  void onSelect();
60  void updateFiles();
61  void updateFileList();
62 public:
63  explicit SshDialog(QWidget *parent, SshConnection *ssh);
64  ~SshDialog();
65 
66  enum AuthMethod {
67  PublicKey,
68  Password
69  };
70 
71  void setHostName(QString const &host);
72  void setPort(int port);
73  void setAuthMethod(AuthMethod method);
74  void setUserName(QString const &uid);
75  void setPassword(QString const &pwd);
76 
77  QString hostName() const;
78  int port() const;
79  AuthMethod authMethod() const;
80  QString userName() const;
81  QString password() const;
82 
83  void updateUI();
84  void doConnect();
85  void doDisconnect();
86  bool isSftpConnected() const;
87 
88  QString selectedPath() const;
89 private slots:
90  void on_listWidget_itemDoubleClicked(QListWidgetItem *item);
91  void on_pushButton_connect_clicked();
92  void on_pushButton_disconnect_clicked();
93  void on_radioButton_key_clicked();
94  void on_radioButton_pwd_clicked();
95  void on_pushButton_ok_clicked();
96 protected:
97  void keyPressEvent(QKeyEvent *event);
98 
99  // QDialog interface
100 public slots:
101  int exec();
102 };
103 
104 #endif
105 #endif // SSHDIALOG_H
Definition: AboutDialog.h:6