monodev
[mcloud.git] / mCloudGui / MainWindow.cs
1 using System;
2 using System.Collections.Generic;
3 using Gtk;
4
5 using mCloudAwk;
6
7 namespace mCloudGui {
8
9         public partial class MainWindow: Gtk.Window
10         {        
11                 private const string new_text = "new ...";
12                 private IMCloudAwk MCA;
13
14                 public MainWindow (): base (Gtk.WindowType.Toplevel)
15                 {
16                         Build ();
17                         this.MCA = new MCloudMgr ();
18                         TConnection[] Conns = MCA.GetConnections ();
19                         foreach (var Conn in Conns) {
20                                 this.ConnectionSelection.AppendText (Conn.Name);
21                         }
22                         this.ConnectionSelection.AppendText (new_text);
23                         this.ConnectionSelection.Active = 0;
24                 }
25
26                 protected void OnDeleteEvent (object sender, DeleteEventArgs a)
27                 {
28                         MCA.Dispose ();
29                         Application.Quit ();
30                         a.RetVal = true;
31                 }
32
33                 protected void TestButtonPressed (object sender, EventArgs e)
34                 {
35                         // extract connection name
36                         string connection_name = this.ConnectionSelection.ActiveText;
37
38                         // TODO: implement ssh connection test
39                         int r = this.MCA.TestConnection (connection_name);
40
41                         if (r == MCloudMgr.SSH_NO_PW || r == MCloudMgr.SSH_WITH_PW) {
42                                 // everything fine
43                         } else {
44                                 GtkUtil.MessageBox.Show("Keine ssh-Verbindung möglich");
45                                 return;
46                         }
47
48                         // if connection is new, add it to configuration
49                         if (connection_name == new_text) {
50
51                                 // build connection object
52                                 connection_name = this.UserField.Text + "@" + this.ServerField.Text;
53                                 TConnection C = new TConnection {
54                                         Name = connection_name,
55                                         User = this.UserField.Text,
56                                         Server = this.ServerField.Text,
57                                         Password = this.PasswordField.Text
58                                 };
59                                 this.MCA.AddConnection (C);
60                         }
61
62                         // TODO: check login w/o password
63                         bool login_without_password_possible = false;
64
65                         if (!login_without_password_possible) {
66
67                                 // check existence of public key file
68                                 if (!System.IO.File.Exists ("/home/monodev/.ssh/id_dsa.pub")) {
69
70                                         // TODO: create public key file
71                                         using (System.IO.StreamWriter File = new System.IO.StreamWriter ("/home/monodev/.ssh/id_dsa.pub", false)) {
72                                                 File.WriteLine ("Hallo");
73                                         }
74                                 }
75                                 // TODO: scp file to target
76                                 // TODO: append it to .ssh/authorized_keys
77                                 // TODO: test login w/o password again
78                         }
79                         // TODO:  next configution step
80                         this.Destroy ();
81                         MCA.Dispose ();
82                         DirectorySelectionWin DirWin = new DirectorySelectionWin ();
83                         DirWin.Show ();
84
85                 }
86
87                 protected void ConnectionSelectionChanged (object sender, EventArgs e)
88                 {
89                         if (((Gtk.ComboBox)sender).ActiveText.IndexOf ('@') > 0) {
90                                 // grey input fields
91                                 this.ServerField.Sensitive = false;
92                                 this.PasswordField.Sensitive = false;
93                                 this.UserField.Sensitive = false;
94                                 
95                                 // activate delete button
96                                 this.DeleteConnectionBtn.Sensitive = true;
97                         } else {
98                                 // activate input fields
99                                 this.ServerField.Sensitive = true;
100                                 this.PasswordField.Sensitive = true;
101                                 this.UserField.Sensitive = true;
102
103                                 // deactivate delete button
104                                 this.DeleteConnectionBtn.Sensitive = false;
105                         }
106                 }
107
108                 protected void DeleteConnectionPressed (object sender, EventArgs e)
109                 {
110                         string connection = this.ConnectionSelection.ActiveText;
111                         if (this.ConnectionSelection.ActiveText.IndexOf ('@') == -1)
112                                 throw new Exception ("Cannot remove default entry");
113                         int connPos = this.ConnectionSelection.Active;
114                         this.ConnectionSelection.Active = connPos+1;
115                         this.ConnectionSelection.RemoveText (connPos);
116                         this.MCA.DeleteConnection (connection);
117                 }
118         }
119 }