// // OpenVPN Administrator // // Author(s): Everaldo Canuto // // (C) 2006 Everaldo Canuto // (C) 2006 The Gang // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // using System; using System.Reflection; using Mono.Unix; using Gtk; using Glade; namespace OpenVPN.Admin { public class KeysForm { #region Fields [Widget] Dialog keysForm; [Widget] Image imageLogo; //[Widget] Button buttonAdd; [Widget] Button buttonDelete; [Widget] Button buttonPassphrase; [Widget] Button buttonClear; [Widget] TreeView listView; [Widget] Gtk.Table tableCert; [Widget] Gtk.Entry entryName; [Widget] Gtk.Entry entryMail; [Widget] Gtk.Entry entryCity; [Widget] Gtk.Entry entryState; [Widget] Gtk.Entry entryCountry; [Widget] Gtk.Entry entryOrganization; [Widget] Gtk.Entry entryOrganizationUnit; [Widget] Gtk.Entry entryExpiration; [Widget] Gtk.Entry entrySerial; private ListStore listStore; private string selectedKey; #endregion #region Constructors and destructors public KeysForm() { InitializeComponent(); RefreshEntries(); } private void InitializeComponent() { // keysForm Glade.XML gxml = new Glade.XML (null, "openvpn-admin.glade", "keysForm", null); gxml.Autoconnect (this); keysForm.Icon = Gdk.Pixbuf.LoadFromResource("openvpn-admin.png"); imageLogo.Pixbuf = Gdk.Pixbuf.LoadFromResource("certificate.png"); // listStore listStore = new ListStore (typeof (string)); // columnKeys listView.AppendColumn("Keys", new CellRendererText(), "text", 0);; // listView listView.Model = listStore; listView.EnableSearch = true; listView.Selection.Changed += new EventHandler(on_listView_selection_change); } #endregion #region Private methods public ResponseType QuestionBox(string message) { MessageDialog md = new MessageDialog(this.keysForm, DialogFlags.DestroyWithParent, MessageType.Question, ButtonsType.YesNo, message); ResponseType result = (ResponseType) md.Run(); md.Destroy(); return result; } public void MsgBox(string message) { MessageDialog md = new MessageDialog(this.keysForm, DialogFlags.DestroyWithParent, MessageType.Info, ButtonsType.Ok, message); md.Run(); md.Destroy(); } public void ErrorBox(string message) { MessageDialog md = new MessageDialog(this.keysForm, DialogFlags.DestroyWithParent, MessageType.Error, ButtonsType.Ok, message); md.Run(); md.Destroy(); } #endregion #region Public methods public Gtk.ResponseType ShowModal() { return (Gtk.ResponseType) this.keysForm.Run(); } public void RefreshEntries() { this.listStore.Clear(); foreach (string key in Configuration.Keys) { this.listStore.AppendValues(key); } listView.Selection.SelectPath(new TreePath("0")); } #endregion #region Private events protected void on_close_clicked(object o, EventArgs args) { keysForm.Hide(); } protected void on_add_clicked(object o, EventArgs args) { FileChooserDialog dialog = new FileChooserDialog(Catalog.GetString("Select Key File"), keysForm, FileChooserAction.Open); dialog.AddButton (Stock.Cancel, ResponseType.Cancel); dialog.AddButton (Stock.Open, ResponseType.Ok); if (dialog.Run() == (int) ResponseType.Ok) { Configuration.AddKey(dialog.Filename); this.RefreshEntries(); } dialog.Hide(); } protected void on_delete_clicked(object o, EventArgs args) { if (QuestionBox(Catalog.GetString("Are you sure you want to delete this key?")) == ResponseType.Yes) { try { Configuration.DeleteKey(this.selectedKey); this.RefreshEntries(); } catch (Exception e) { ErrorBox(e.Message); } } } protected void on_passphrase_clicked(object o, EventArgs args) { ChangeForm form = new ChangeForm(); if (form.ShowModal() == Gtk.ResponseType.Ok) { try { Configuration.ChangeKey(this.selectedKey, form.Password, form.NewPassword); } catch (Exception e) { ErrorBox(e.Message); } } } protected void on_clear_clicked(object o, EventArgs args) { PassForm form = new PassForm(false); if (form.Show() == Gtk.ResponseType.Ok) { try { Configuration.ClearKey(this.selectedKey, form.Password); } catch (Exception e) { ErrorBox(e.Message); } } } protected void on_listView_selection_change(object o, EventArgs args) { try { TreeModel model; TreeIter iter; this.listView.Selection.GetSelected(out model, out iter); this.selectedKey = (string) listView.Model.GetValue(iter, 0); this.buttonDelete.Sensitive = true; this.buttonPassphrase.Sensitive = true; this.buttonClear.Sensitive = true; RefreshPanel(); } catch {} } private void RefreshPanel() { CertificationInfo cert = Configuration.CertificateInfo(this.selectedKey); tableCert.Visible = ! cert.Empty; entryName.Text = cert.Name; entryMail.Text = cert.Mail; entryCity.Text = cert.City; entryState.Text = cert.State; entryCountry.Text = cert.Country; entryOrganization.Text = cert.Organization; entryOrganizationUnit.Text = cert.OrganizationUnit; entryExpiration.Text = cert.Expiration; entrySerial.Text = cert.Serial; } #endregion } }