1 // $Id: KeyDialog.cpp 683 2012-02-09 23:37:19Z felfert $
2 //
3 // Copyright (C) 2006 The OpenNX team
4 // Author: Fritz Elfert
5 //
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU Library General Public License as
8 // published by the Free Software Foundation; either version 2 of the
9 // License, or (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU Library General Public
17 // License along with this program; if not, write to the
18 // Free Software Foundation, Inc.,
19 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 //
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
27 #pragma implementation "KeyDialog.h"
28 #endif
29
30 // For compilers that support precompilation, includes "wx/wx.h".
31 #include "wx/wxprec.h"
32
33 #ifdef __BORLANDC__
34 #pragma hdrstop
35 #endif
36
37 #ifndef WX_PRECOMP
38 #include "wx/wx.h"
39 #endif
40
41 ////@begin includes
42 ////@end includes
43
44 #include "KeyDialog.h"
45 #include "Icon.h"
46 #include "opennxApp.h"
47
48 #include <wx/config.h>
49 #include <wx/cshelp.h>
50
51 ////@begin XPM images
52 ////@end XPM images
53
54 /*!
55 * KeyDialog type definition
56 */
57
IMPLEMENT_DYNAMIC_CLASS(KeyDialog,wxDialog)58 IMPLEMENT_DYNAMIC_CLASS( KeyDialog, wxDialog )
59
60 /*!
61 * KeyDialog event table definition
62 */
63
64 BEGIN_EVENT_TABLE( KeyDialog, wxDialog )
65
66 ////@begin KeyDialog event table entries
67 EVT_TEXT( XRCID("ID_TEXTCTRL_SSHKEY"), KeyDialog::OnTextctrlSshkeyUpdated )
68
69 EVT_BUTTON( XRCID("ID_BUTTON_IMPORT"), KeyDialog::OnButtonImportClick )
70
71 EVT_BUTTON( wxID_DEFAULT, KeyDialog::OnDEFAULTClick )
72
73 EVT_BUTTON( wxID_SAVE, KeyDialog::OnSAVEClick )
74
75 ////@end KeyDialog event table entries
76
77 EVT_MENU(wxID_CONTEXT_HELP, KeyDialog::OnContextHelp)
78
79 END_EVENT_TABLE()
80
81 /*!
82 * KeyDialog constructors
83 */
84
85 KeyDialog::KeyDialog( )
86 {
87 Init();
88 }
89
KeyDialog(wxWindow * parent,wxWindowID id,const wxString & caption,const wxPoint & pos,const wxSize & size,long style)90 KeyDialog::KeyDialog( wxWindow* parent, wxWindowID id, const wxString& caption, const wxPoint& pos, const wxSize& size, long style )
91 {
92 Init();
93 Create(parent, id, caption, pos, size, style);
94 }
95
96 /*!
97 * KeyDialog creator
98 */
99
Create(wxWindow * parent,wxWindowID id,const wxString & caption,const wxPoint & pos,const wxSize & size,long style)100 bool KeyDialog::Create( wxWindow* parent, wxWindowID id, const wxString& caption, const wxPoint& pos, const wxSize& size, long style )
101 {
102 ////@begin KeyDialog creation
103 SetExtraStyle(wxWS_EX_BLOCK_EVENTS|wxDIALOG_EX_CONTEXTHELP);
104 SetParent(parent);
105 CreateControls();
106 SetIcon(GetIconResource(wxT("res/nx.png")));
107 if (GetSizer())
108 {
109 GetSizer()->SetSizeHints(this);
110 }
111 Centre();
112 ////@end KeyDialog creation
113 wxUnusedVar(style);
114 wxUnusedVar(size);
115 wxUnusedVar(pos);
116 wxUnusedVar(caption);
117 wxUnusedVar(id);
118 ::wxGetApp().EnableContextHelp(this);
119 return true;
120 }
121
122 /*!
123 * Member initialisation
124 */
125
Init()126 void KeyDialog::Init()
127 {
128 ////@begin KeyDialog member initialisation
129 m_sSshKey = wxT("");
130 m_pCtrlSshKey = NULL;
131 m_pCtrlSave = NULL;
132 ////@end KeyDialog member initialisation
133 }
134 /*!
135 * Control creation for KeyDialog
136 */
137
CreateControls()138 void KeyDialog::CreateControls()
139 {
140 ////@begin KeyDialog content construction
141 if (!wxXmlResource::Get()->LoadDialog(this, GetParent(), _T("ID_KEYDIALOG")))
142 wxLogError(wxT("Missing wxXmlResource::Get()->Load() in OnInit()?"));
143 m_pCtrlSshKey = XRCCTRL(*this, "ID_TEXTCTRL_SSHKEY", wxTextCtrl);
144 m_pCtrlSave = XRCCTRL(*this, "wxID_SAVE", wxButton);
145 // Set validators
146 if (FindWindow(XRCID("ID_TEXTCTRL_SSHKEY")))
147 FindWindow(XRCID("ID_TEXTCTRL_SSHKEY"))->SetValidator( wxGenericValidator(& m_sSshKey) );
148 ////@end KeyDialog content construction
149 // Create custom windows not generated automatically here.
150 ////@begin KeyDialog content initialisation
151 ////@end KeyDialog content initialisation
152 }
153
154 /*!
155 * Should we show tooltips?
156 */
157
ShowToolTips()158 bool KeyDialog::ShowToolTips()
159 {
160 return true;
161 }
162
163 /*!
164 * Get bitmap resources
165 */
166
GetBitmapResource(const wxString & name)167 wxBitmap KeyDialog::GetBitmapResource( const wxString& name )
168 {
169 // Bitmap retrieval
170 return CreateBitmapFromFile(name);
171 }
172
173 /*!
174 * Get icon resources
175 */
176
GetIconResource(const wxString & name)177 wxIcon KeyDialog::GetIconResource( const wxString& name )
178 {
179 // Icon retrieval
180 ////@begin KeyDialog icon retrieval
181 wxUnusedVar(name);
182 return wxNullIcon;
183 ////@end KeyDialog icon retrieval
184 }
185
CheckChanged()186 void KeyDialog::CheckChanged()
187 {
188 m_pCtrlSave->Enable(m_pCtrlSshKey->GetValue() != m_sSshKey);
189 }
190
OnContextHelp(wxCommandEvent &)191 void KeyDialog::OnContextHelp(wxCommandEvent &)
192 {
193 wxContextHelp contextHelp(this);
194 }
195
196 /*!
197 * wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_BUTTON_IMPORT
198 */
199
OnButtonImportClick(wxCommandEvent & event)200 void KeyDialog::OnButtonImportClick( wxCommandEvent& event )
201 {
202 wxString keyDir;
203 if (!wxConfigBase::Get()->Read(wxT("Recent/KeyImport"), &keyDir)) {
204 keyDir = ::wxGetHomeDir() + wxFileName::GetPathSeparator() + wxT(".ssh");
205 if (!wxFileName(keyDir).IsDirReadable())
206 keyDir = ::wxGetHomeDir();
207 }
208 wxFileDialog d(this, _("Select key to import"), keyDir, wxEmptyString,
209 _("SSh key files (*.key)|*.key|All files (*)|*"), wxFD_OPEN|wxFD_FILE_MUST_EXIST);
210 d.SetDirectory(keyDir);
211 if (d.ShowModal() == wxID_OK) {
212 m_pCtrlSshKey->LoadFile(d.GetPath());
213 wxConfigBase::Get()->Write(wxT("Recent/KeyImport"), d.GetDirectory());
214 CheckChanged();
215 }
216 event.Skip();
217 }
218
219 /*!
220 * wxEVT_COMMAND_BUTTON_CLICKED event handler for wxID_DEFAULT
221 */
222
OnDEFAULTClick(wxCommandEvent & event)223 void KeyDialog::OnDEFAULTClick( wxCommandEvent& event )
224 {
225 wxString fn;
226 wxConfigBase::Get()->Read(wxT("Config/SystemNxDir"), &fn);
227 fn << wxFileName::GetPathSeparator() << wxT("share")
228 << wxFileName::GetPathSeparator() << wxT("keys")
229 << wxFileName::GetPathSeparator() << wxT("server.id_dsa.key");
230 m_pCtrlSshKey->LoadFile(fn);
231 CheckChanged();
232 event.Skip();
233 }
234
235 /*!
236 * wxEVT_COMMAND_BUTTON_CLICKED event handler for wxID_SAVE
237 */
238
OnSAVEClick(wxCommandEvent & event)239 void KeyDialog::OnSAVEClick( wxCommandEvent& event )
240 {
241 wxUnusedVar(event);
242 TransferDataFromWindow();
243 CheckChanged();
244 EndModal(wxID_OK);
245 }
246
247
248 /*!
249 * wxEVT_COMMAND_TEXT_UPDATED event handler for ID_TEXTCTRL_SSHKEY
250 */
251
OnTextctrlSshkeyUpdated(wxCommandEvent & event)252 void KeyDialog::OnTextctrlSshkeyUpdated( wxCommandEvent& event )
253 {
254 CheckChanged();
255 event.Skip();
256 }
257
258
259
260