source: filezilla/trunk/fuentes/src/interface/settings/optionspage_connection_sftp.cpp @ 3185

Last change on this file since 3185 was 3185, checked in by jrpelegrina, 3 years ago

Update new version: 3.15.02

File size: 4.0 KB
Line 
1#include <filezilla.h>
2#include "../Options.h"
3#include "settingsdialog.h"
4#include "optionspage.h"
5#include "optionspage_connection_sftp.h"
6#include "../filezillaapp.h"
7#include "../inputdialog.h"
8#include <wx/tokenzr.h>
9
10BEGIN_EVENT_TABLE(COptionsPageConnectionSFTP, COptionsPage)
11EVT_END_PROCESS(wxID_ANY, COptionsPageConnectionSFTP::OnEndProcess)
12EVT_BUTTON(XRCID("ID_ADDKEY"), COptionsPageConnectionSFTP::OnAdd)
13EVT_BUTTON(XRCID("ID_REMOVEKEY"), COptionsPageConnectionSFTP::OnRemove)
14EVT_LIST_ITEM_SELECTED(wxID_ANY, COptionsPageConnectionSFTP::OnSelChanged)
15EVT_LIST_ITEM_DESELECTED(wxID_ANY, COptionsPageConnectionSFTP::OnSelChanged)
16END_EVENT_TABLE()
17
18COptionsPageConnectionSFTP::COptionsPageConnectionSFTP()
19{
20        m_pFzpg = new CFZPuttyGenInterface(this);
21}
22
23COptionsPageConnectionSFTP::~COptionsPageConnectionSFTP()
24{
25        delete m_pFzpg;
26}
27
28bool COptionsPageConnectionSFTP::LoadPage()
29{
30        wxListCtrl* pKeys = XRCCTRL(*this, "ID_KEYS", wxListCtrl);
31        if (!pKeys)
32                return false;
33        pKeys->InsertColumn(0, _("Filename"), wxLIST_FORMAT_LEFT, 150);
34        pKeys->InsertColumn(1, _("Comment"), wxLIST_FORMAT_LEFT, 100);
35        pKeys->InsertColumn(2, _("Data"), wxLIST_FORMAT_LEFT, 350);
36
37        // Generic wxListCtrl has gross minsize
38        wxSize size = pKeys->GetMinSize();
39        size.x = 1;
40        pKeys->SetMinSize(size);
41
42        wxString keyFiles = m_pOptions->GetOption(OPTION_SFTP_KEYFILES);
43        wxStringTokenizer tokens(keyFiles, _T("\n"), wxTOKEN_DEFAULT);
44        while (tokens.HasMoreTokens())
45                AddKey(tokens.GetNextToken(), true);
46
47        bool failure = false;
48
49        SetCtrlState();
50
51        SetCheckFromOption(XRCID("ID_SFTP_COMPRESSION"), OPTION_SFTP_COMPRESSION, failure);
52
53        return !failure;
54}
55
56bool COptionsPageConnectionSFTP::SavePage()
57{
58        // Don't save keys on process error
59        if (!m_pFzpg->IsProcessStarted() || m_pFzpg->IsProcessCreated()) {
60                wxString keyFiles;
61                wxListCtrl* pKeys = XRCCTRL(*this, "ID_KEYS", wxListCtrl);
62                for (int i = 0; i < pKeys->GetItemCount(); i++) {
63                        if (!keyFiles.empty())
64                                keyFiles += _T("\n");
65                        keyFiles += pKeys->GetItemText(i);
66                }
67                m_pOptions->SetOption(OPTION_SFTP_KEYFILES, keyFiles);
68        }
69
70        if (m_pFzpg->IsProcessCreated())
71                m_pFzpg->EndProcess();
72
73        SetOptionFromCheck(XRCID("ID_SFTP_COMPRESSION"), OPTION_SFTP_COMPRESSION);
74
75        return true;
76}
77
78void COptionsPageConnectionSFTP::OnAdd(wxCommandEvent&)
79{
80        wxFileDialog dlg(this, _("Select file containing private key"), wxString(), wxString(), wxFileSelectorDefaultWildcardStr, wxFD_OPEN | wxFD_FILE_MUST_EXIST);
81        if (dlg.ShowModal() != wxID_OK)
82                return;
83
84        const wxString file = dlg.GetPath();
85
86        AddKey(dlg.GetPath(), false);
87}
88
89void COptionsPageConnectionSFTP::OnRemove(wxCommandEvent&)
90{
91        wxListCtrl* pKeys = XRCCTRL(*this, "ID_KEYS", wxListCtrl);
92
93        int index = pKeys->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
94        if (index == -1)
95                return;
96
97        pKeys->DeleteItem(index);
98}
99
100bool COptionsPageConnectionSFTP::AddKey(wxString keyFile, bool silent)
101{
102        wxString comment, data;
103        if (!m_pFzpg->LoadKeyFile(keyFile, silent, comment, data))
104                return false;
105
106        if (KeyFileExists(keyFile)) {
107                if (!silent)
108                        wxMessageBoxEx(_("Selected file is already loaded"), _("Cannot load key file"), wxICON_INFORMATION);
109                return false;
110        }
111
112        wxListCtrl* pKeys = XRCCTRL(*this, "ID_KEYS", wxListCtrl);
113        int index = pKeys->InsertItem(pKeys->GetItemCount(), keyFile);
114        pKeys->SetItem(index, 1, comment);
115        pKeys->SetItem(index, 2, data);
116
117        return true;
118}
119
120bool COptionsPageConnectionSFTP::KeyFileExists(const wxString& keyFile)
121{
122        wxListCtrl* pKeys = XRCCTRL(*this, "ID_KEYS", wxListCtrl);
123        for (int i = 0; i < pKeys->GetItemCount(); ++i) {
124                if (pKeys->GetItemText(i) == keyFile)
125                        return true;
126        }
127        return false;
128}
129
130void COptionsPageConnectionSFTP::SetCtrlState()
131{
132        wxListCtrl* pKeys = XRCCTRL(*this, "ID_KEYS", wxListCtrl);
133
134        int index = pKeys->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
135        XRCCTRL(*this, "ID_REMOVEKEY", wxButton)->Enable(index != -1);
136        return;
137}
138
139void COptionsPageConnectionSFTP::OnSelChanged(wxListEvent&)
140{
141        SetCtrlState();
142}
143
144void COptionsPageConnectionSFTP::OnEndProcess(wxProcessEvent&)
145{
146        m_pFzpg->DeleteProcess();
147}
Note: See TracBrowser for help on using the repository browser.