monodev master
authorMichael Wagner <michael@wagnertech.de>
Sat, 4 Jun 2016 19:43:07 +0000 (21:43 +0200)
committerMichael Wagner <michael@wagnertech.de>
Sat, 4 Jun 2016 19:43:07 +0000 (21:43 +0200)
GtkUtil/MessageBox.cs [new file with mode: 0644]
mCloudGui/DirectorySelectionDialog.cs [new file with mode: 0644]
mCloudGui/DirectorySelectionWin.cs [new file with mode: 0644]
mCloudGui/MainWindow.cs [new file with mode: 0644]
mCloudGui/Program.cs [new file with mode: 0644]
mCloudGui/gtk-gui/mCloudGui.MainWindow.cs [new file with mode: 0644]
util/Config.cs [new file with mode: 0644]

diff --git a/GtkUtil/MessageBox.cs b/GtkUtil/MessageBox.cs
new file mode 100644 (file)
index 0000000..dfb292f
--- /dev/null
@@ -0,0 +1,16 @@
+using System;
+using Gtk;
+
+namespace GtkUtil
+{
+       public class MessageBox
+       {
+               public static void Show(string Msg)
+               {
+                       MessageDialog md = new MessageDialog (null, DialogFlags.Modal, MessageType.Info, ButtonsType.Ok, Msg);
+                       md.Run ();
+                       md.Destroy();
+               }
+       }
+}
+
diff --git a/mCloudGui/DirectorySelectionDialog.cs b/mCloudGui/DirectorySelectionDialog.cs
new file mode 100644 (file)
index 0000000..bbcae6e
--- /dev/null
@@ -0,0 +1,13 @@
+using System;
+
+namespace mCloudGui
+{
+       public partial class DirectorySelectionDialog : Gtk.Dialog
+       {
+               public DirectorySelectionDialog ()
+               {
+                       this.Build ();
+               }
+       }
+}
+
diff --git a/mCloudGui/DirectorySelectionWin.cs b/mCloudGui/DirectorySelectionWin.cs
new file mode 100644 (file)
index 0000000..70d67b6
--- /dev/null
@@ -0,0 +1,27 @@
+using System;
+using Gtk;
+
+using mCloudAwk;
+
+namespace mCloudGui
+{
+       public partial class DirectorySelectionWin : Gtk.Window
+       {
+               private IMCloudAwk MCA;
+
+               public DirectorySelectionWin () : 
+                               base(Gtk.WindowType.Toplevel)
+               {
+                       this.Build ();
+                       this.MCA = new MCloudMgr ();
+               }
+               protected void OnDeleteEvent (object sender, DeleteEventArgs a)
+               {
+                       MCA.Dispose ();
+                       Application.Quit ();
+                       a.RetVal = true;
+               }
+
+       }
+}
+
diff --git a/mCloudGui/MainWindow.cs b/mCloudGui/MainWindow.cs
new file mode 100644 (file)
index 0000000..ea8a904
--- /dev/null
@@ -0,0 +1,119 @@
+using System;
+using System.Collections.Generic;
+using Gtk;
+
+using mCloudAwk;
+
+namespace mCloudGui {
+
+       public partial class MainWindow: Gtk.Window
+       {        
+               private const string new_text = "new ...";
+               private IMCloudAwk MCA;
+
+               public MainWindow (): base (Gtk.WindowType.Toplevel)
+               {
+                       Build ();
+                       this.MCA = new MCloudMgr ();
+                       TConnection[] Conns = MCA.GetConnections ();
+                       foreach (var Conn in Conns) {
+                               this.ConnectionSelection.AppendText (Conn.Name);
+                       }
+                       this.ConnectionSelection.AppendText (new_text);
+                       this.ConnectionSelection.Active = 0;
+               }
+
+               protected void OnDeleteEvent (object sender, DeleteEventArgs a)
+               {
+                       MCA.Dispose ();
+                       Application.Quit ();
+                       a.RetVal = true;
+               }
+
+               protected void TestButtonPressed (object sender, EventArgs e)
+               {
+                       // extract connection name
+                       string connection_name = this.ConnectionSelection.ActiveText;
+
+                       // TODO: implement ssh connection test
+                       int r = this.MCA.TestConnection (connection_name);
+
+                       if (r == MCloudMgr.SSH_NO_PW || r == MCloudMgr.SSH_WITH_PW) {
+                               // everything fine
+                       } else {
+                               GtkUtil.MessageBox.Show("Keine ssh-Verbindung möglich");
+                               return;
+                       }
+
+                       // if connection is new, add it to configuration
+                       if (connection_name == new_text) {
+
+                               // build connection object
+                               connection_name = this.UserField.Text + "@" + this.ServerField.Text;
+                               TConnection C = new TConnection {
+                                       Name = connection_name,
+                                       User = this.UserField.Text,
+                                       Server = this.ServerField.Text,
+                                       Password = this.PasswordField.Text
+                               };
+                               this.MCA.AddConnection (C);
+                       }
+
+                       // TODO: check login w/o password
+                       bool login_without_password_possible = false;
+
+                       if (!login_without_password_possible) {
+
+                               // check existence of public key file
+                               if (!System.IO.File.Exists ("/home/monodev/.ssh/id_dsa.pub")) {
+
+                                       // TODO: create public key file
+                                       using (System.IO.StreamWriter File = new System.IO.StreamWriter ("/home/monodev/.ssh/id_dsa.pub", false)) {
+                                               File.WriteLine ("Hallo");
+                                       }
+                               }
+                               // TODO: scp file to target
+                               // TODO: append it to .ssh/authorized_keys
+                               // TODO: test login w/o password again
+                       }
+                       // TODO:  next configution step
+                       this.Destroy ();
+                       MCA.Dispose ();
+                       DirectorySelectionWin DirWin = new DirectorySelectionWin ();
+                       DirWin.Show ();
+
+               }
+
+               protected void ConnectionSelectionChanged (object sender, EventArgs e)
+               {
+                       if (((Gtk.ComboBox)sender).ActiveText.IndexOf ('@') > 0) {
+                               // grey input fields
+                               this.ServerField.Sensitive = false;
+                               this.PasswordField.Sensitive = false;
+                               this.UserField.Sensitive = false;
+                               
+                               // activate delete button
+                               this.DeleteConnectionBtn.Sensitive = true;
+                       } else {
+                               // activate input fields
+                               this.ServerField.Sensitive = true;
+                               this.PasswordField.Sensitive = true;
+                               this.UserField.Sensitive = true;
+
+                               // deactivate delete button
+                               this.DeleteConnectionBtn.Sensitive = false;
+                       }
+               }
+
+               protected void DeleteConnectionPressed (object sender, EventArgs e)
+               {
+                       string connection = this.ConnectionSelection.ActiveText;
+                       if (this.ConnectionSelection.ActiveText.IndexOf ('@') == -1)
+                               throw new Exception ("Cannot remove default entry");
+                       int connPos = this.ConnectionSelection.Active;
+                       this.ConnectionSelection.Active = connPos+1;
+                       this.ConnectionSelection.RemoveText (connPos);
+                       this.MCA.DeleteConnection (connection);
+               }
+       }
+}
\ No newline at end of file
diff --git a/mCloudGui/Program.cs b/mCloudGui/Program.cs
new file mode 100644 (file)
index 0000000..02088ce
--- /dev/null
@@ -0,0 +1,16 @@
+using System;
+using Gtk;
+
+namespace mCloudGui
+{
+       class MainClass
+       {
+               public static void Main (string[] args)
+               {
+                       Application.Init ();
+                       MainWindow win = new MainWindow ();
+                       win.Show ();
+                       Application.Run ();
+               }
+       }
+}
diff --git a/mCloudGui/gtk-gui/mCloudGui.MainWindow.cs b/mCloudGui/gtk-gui/mCloudGui.MainWindow.cs
new file mode 100644 (file)
index 0000000..abd931c
--- /dev/null
@@ -0,0 +1,176 @@
+
+// This file has been generated by the GUI designer. Do not modify.
+namespace mCloudGui
+{
+       public partial class MainWindow
+       {
+               private global::Gtk.VBox vbox1;
+               private global::Gtk.Table table1;
+               private global::Gtk.ComboBox ConnectionSelection;
+               private global::Gtk.Label label10;
+               private global::Gtk.Label label11;
+               private global::Gtk.Label label12;
+               private global::Gtk.Label label9;
+               private global::Gtk.Entry PasswordField;
+               private global::Gtk.Entry ServerField;
+               private global::Gtk.Entry UserField;
+               private global::Gtk.HBox hbox2;
+               private global::Gtk.Button DeleteConnectionBtn;
+               private global::Gtk.Button button1;
+
+               protected virtual void Build ()
+               {
+                       global::Stetic.Gui.Initialize (this);
+                       // Widget mCloudGui.MainWindow
+                       this.Name = "mCloudGui.MainWindow";
+                       this.Title = global::Mono.Unix.Catalog.GetString ("mCloud Configuration Wizard");
+                       this.WindowPosition = ((global::Gtk.WindowPosition)(4));
+                       this.BorderWidth = ((uint)(3));
+                       // Container child mCloudGui.MainWindow.Gtk.Container+ContainerChild
+                       this.vbox1 = new global::Gtk.VBox ();
+                       this.vbox1.Name = "vbox1";
+                       this.vbox1.Spacing = 6;
+                       // Container child vbox1.Gtk.Box+BoxChild
+                       this.table1 = new global::Gtk.Table (((uint)(4)), ((uint)(2)), false);
+                       this.table1.Name = "table1";
+                       this.table1.RowSpacing = ((uint)(6));
+                       this.table1.ColumnSpacing = ((uint)(6));
+                       // Container child table1.Gtk.Table+TableChild
+                       this.ConnectionSelection = global::Gtk.ComboBox.NewText ();
+                       this.ConnectionSelection.Name = "ConnectionSelection";
+                       this.table1.Add (this.ConnectionSelection);
+                       global::Gtk.Table.TableChild w1 = ((global::Gtk.Table.TableChild)(this.table1 [this.ConnectionSelection]));
+                       w1.LeftAttach = ((uint)(1));
+                       w1.RightAttach = ((uint)(2));
+                       w1.XOptions = ((global::Gtk.AttachOptions)(4));
+                       w1.YOptions = ((global::Gtk.AttachOptions)(4));
+                       // Container child table1.Gtk.Table+TableChild
+                       this.label10 = new global::Gtk.Label ();
+                       this.label10.Name = "label10";
+                       this.label10.LabelProp = global::Mono.Unix.Catalog.GetString ("Server");
+                       this.table1.Add (this.label10);
+                       global::Gtk.Table.TableChild w2 = ((global::Gtk.Table.TableChild)(this.table1 [this.label10]));
+                       w2.TopAttach = ((uint)(1));
+                       w2.BottomAttach = ((uint)(2));
+                       w2.XOptions = ((global::Gtk.AttachOptions)(4));
+                       w2.YOptions = ((global::Gtk.AttachOptions)(4));
+                       // Container child table1.Gtk.Table+TableChild
+                       this.label11 = new global::Gtk.Label ();
+                       this.label11.Name = "label11";
+                       this.label11.LabelProp = global::Mono.Unix.Catalog.GetString ("User");
+                       this.table1.Add (this.label11);
+                       global::Gtk.Table.TableChild w3 = ((global::Gtk.Table.TableChild)(this.table1 [this.label11]));
+                       w3.TopAttach = ((uint)(2));
+                       w3.BottomAttach = ((uint)(3));
+                       w3.XOptions = ((global::Gtk.AttachOptions)(4));
+                       w3.YOptions = ((global::Gtk.AttachOptions)(4));
+                       // Container child table1.Gtk.Table+TableChild
+                       this.label12 = new global::Gtk.Label ();
+                       this.label12.Name = "label12";
+                       this.label12.LabelProp = global::Mono.Unix.Catalog.GetString ("Password");
+                       this.table1.Add (this.label12);
+                       global::Gtk.Table.TableChild w4 = ((global::Gtk.Table.TableChild)(this.table1 [this.label12]));
+                       w4.TopAttach = ((uint)(3));
+                       w4.BottomAttach = ((uint)(4));
+                       w4.XOptions = ((global::Gtk.AttachOptions)(4));
+                       w4.YOptions = ((global::Gtk.AttachOptions)(4));
+                       // Container child table1.Gtk.Table+TableChild
+                       this.label9 = new global::Gtk.Label ();
+                       this.label9.Name = "label9";
+                       this.label9.LabelProp = global::Mono.Unix.Catalog.GetString ("Connection Selection");
+                       this.table1.Add (this.label9);
+                       global::Gtk.Table.TableChild w5 = ((global::Gtk.Table.TableChild)(this.table1 [this.label9]));
+                       w5.XOptions = ((global::Gtk.AttachOptions)(4));
+                       w5.YOptions = ((global::Gtk.AttachOptions)(4));
+                       // Container child table1.Gtk.Table+TableChild
+                       this.PasswordField = new global::Gtk.Entry ();
+                       this.PasswordField.CanFocus = true;
+                       this.PasswordField.Name = "PasswordField";
+                       this.PasswordField.IsEditable = true;
+                       this.PasswordField.InvisibleChar = '●';
+                       this.table1.Add (this.PasswordField);
+                       global::Gtk.Table.TableChild w6 = ((global::Gtk.Table.TableChild)(this.table1 [this.PasswordField]));
+                       w6.TopAttach = ((uint)(3));
+                       w6.BottomAttach = ((uint)(4));
+                       w6.LeftAttach = ((uint)(1));
+                       w6.RightAttach = ((uint)(2));
+                       w6.XOptions = ((global::Gtk.AttachOptions)(4));
+                       w6.YOptions = ((global::Gtk.AttachOptions)(4));
+                       // Container child table1.Gtk.Table+TableChild
+                       this.ServerField = new global::Gtk.Entry ();
+                       this.ServerField.CanFocus = true;
+                       this.ServerField.Name = "ServerField";
+                       this.ServerField.IsEditable = true;
+                       this.ServerField.InvisibleChar = '●';
+                       this.table1.Add (this.ServerField);
+                       global::Gtk.Table.TableChild w7 = ((global::Gtk.Table.TableChild)(this.table1 [this.ServerField]));
+                       w7.TopAttach = ((uint)(1));
+                       w7.BottomAttach = ((uint)(2));
+                       w7.LeftAttach = ((uint)(1));
+                       w7.RightAttach = ((uint)(2));
+                       w7.XOptions = ((global::Gtk.AttachOptions)(4));
+                       w7.YOptions = ((global::Gtk.AttachOptions)(4));
+                       // Container child table1.Gtk.Table+TableChild
+                       this.UserField = new global::Gtk.Entry ();
+                       this.UserField.CanFocus = true;
+                       this.UserField.Name = "UserField";
+                       this.UserField.IsEditable = true;
+                       this.UserField.InvisibleChar = '●';
+                       this.table1.Add (this.UserField);
+                       global::Gtk.Table.TableChild w8 = ((global::Gtk.Table.TableChild)(this.table1 [this.UserField]));
+                       w8.TopAttach = ((uint)(2));
+                       w8.BottomAttach = ((uint)(3));
+                       w8.LeftAttach = ((uint)(1));
+                       w8.RightAttach = ((uint)(2));
+                       w8.XOptions = ((global::Gtk.AttachOptions)(4));
+                       w8.YOptions = ((global::Gtk.AttachOptions)(4));
+                       this.vbox1.Add (this.table1);
+                       global::Gtk.Box.BoxChild w9 = ((global::Gtk.Box.BoxChild)(this.vbox1 [this.table1]));
+                       w9.Position = 0;
+                       w9.Expand = false;
+                       w9.Fill = false;
+                       // Container child vbox1.Gtk.Box+BoxChild
+                       this.hbox2 = new global::Gtk.HBox ();
+                       this.hbox2.Name = "hbox2";
+                       this.hbox2.Spacing = 6;
+                       // Container child hbox2.Gtk.Box+BoxChild
+                       this.DeleteConnectionBtn = new global::Gtk.Button ();
+                       this.DeleteConnectionBtn.CanFocus = true;
+                       this.DeleteConnectionBtn.Name = "DeleteConnectionBtn";
+                       this.DeleteConnectionBtn.UseUnderline = true;
+                       this.DeleteConnectionBtn.Label = global::Mono.Unix.Catalog.GetString ("Delete Connection");
+                       this.hbox2.Add (this.DeleteConnectionBtn);
+                       global::Gtk.Box.BoxChild w10 = ((global::Gtk.Box.BoxChild)(this.hbox2 [this.DeleteConnectionBtn]));
+                       w10.Position = 0;
+                       w10.Expand = false;
+                       w10.Fill = false;
+                       // Container child hbox2.Gtk.Box+BoxChild
+                       this.button1 = new global::Gtk.Button ();
+                       this.button1.CanFocus = true;
+                       this.button1.Name = "button1";
+                       this.button1.UseUnderline = true;
+                       this.button1.Label = global::Mono.Unix.Catalog.GetString ("Weiter");
+                       this.hbox2.Add (this.button1);
+                       global::Gtk.Box.BoxChild w11 = ((global::Gtk.Box.BoxChild)(this.hbox2 [this.button1]));
+                       w11.Position = 1;
+                       w11.Expand = false;
+                       w11.Fill = false;
+                       this.vbox1.Add (this.hbox2);
+                       global::Gtk.Box.BoxChild w12 = ((global::Gtk.Box.BoxChild)(this.vbox1 [this.hbox2]));
+                       w12.Position = 1;
+                       w12.Expand = false;
+                       w12.Fill = false;
+                       this.Add (this.vbox1);
+                       if ((this.Child != null)) {
+                               this.Child.ShowAll ();
+                       }
+                       this.DefaultWidth = 400;
+                       this.DefaultHeight = 300;
+                       this.Show ();
+                       this.DeleteEvent += new global::Gtk.DeleteEventHandler (this.OnDeleteEvent);
+                       this.ConnectionSelection.Changed += new global::System.EventHandler (this.ConnectionSelectionChanged);
+                       this.button1.Pressed += new global::System.EventHandler (this.TestButtonPressed);
+                       this.DeleteConnectionBtn.Pressed += new global::System.EventHandler (this.DeleteConnectionPressed);
+               }
+       }
+}
diff --git a/util/Config.cs b/util/Config.cs
new file mode 100644 (file)
index 0000000..d9d2a43
--- /dev/null
@@ -0,0 +1,155 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+
+
+namespace util
+{
+       internal class ConfigEntry
+       {
+               public string value;
+               public ISet<string> list;
+               public ConfigEntry(string v)
+               {
+                       this.value = v;
+               }
+       }
+
+       public static class Config
+       {
+               private static Dictionary<String,Dictionary<string,ConfigEntry>> Data;
+               private static bool ready = false;
+               private static string file_name;
+
+               // return values for Load
+               public const int LOAD_OK = 0;
+               public const int LOAD_NO_INPUT_FILE = 1;
+               public static int Load (String FileName)
+               {
+                       Config.file_name = FileName;
+                       Data = new Dictionary<string, Dictionary<string,ConfigEntry>>();
+
+                       if (!System.IO.File.Exists (FileName)) {
+                               ready = true;
+                               return LOAD_NO_INPUT_FILE;
+                       }
+
+                       using (StreamReader File = new StreamReader (FileName)) {
+                               String Line;
+                               string section = null;
+                               string key = "no_key";
+                               Dictionary<string,ConfigEntry> SectionDic = null;
+                               bool listProcessing = false;
+                               while (File.Peek() != -1) {
+                                       Line = File.ReadLine ();
+                                       if (listProcessing) {
+                                               if (Line == "LIST")
+                                                       listProcessing = false;
+                                               else
+                                                       SectionDic [key].list.Add (Line);
+                                       } else if (Line.Length == 0 || Line.Substring (0, 1) == "#") {
+                                               ; // this is a comment line
+                                       } else if (Line.Substring (0, 1) == "[") {
+                                               // new section
+                                               if (section != null) {
+                                                       // append old section
+                                                       Data [section] = SectionDic;
+                                               }
+                                               // start new section
+                                               section = Line.Substring (1, Line.IndexOf (']') - 1);
+                                               SectionDic = new Dictionary<string, ConfigEntry> ();
+                                       } else {
+                                               key = Line.Substring (0, Line.IndexOf (':'));
+                                               String Value = Line.Substring (Line.IndexOf (':') + 1);
+                                               var ce = new ConfigEntry (Value);
+                                               if (Value == "LIST") {
+                                                       listProcessing = true;
+                                                       ce.list = new SortedSet<string> ();
+                                               }
+                                               SectionDic [key] = ce;
+                                       }
+                               }
+                               if (listProcessing)
+                                       throw new ApplicationException ("Configuration list has no end");
+                               if (SectionDic != null) Data [section] = SectionDic;
+                       }
+                       ready = true;
+                       return LOAD_OK;
+               }
+               /*
+               public static Config GetInstance()
+               {
+                       if (Config.Instance == null) Config.Instance = new Config ();
+                       return Config.Instance;
+               }*/
+               public static String GetConfig(string section, string key){
+                       Check ();
+                       if (Data.ContainsKey(section)){
+                               if (Data[section].ContainsKey(key)) return Data [section][key].value;
+                       }
+                       return null;
+               }
+               public static ISet<string> GetList(string section, string key){
+                       Check ();
+                       if (!Data.ContainsKey (section))
+                               return null;
+                       if (!Data [section].ContainsKey (key))
+                               return null;
+                       if (Data [section] [key].value != "LIST")
+                               throw new ApplicationException ("Required List-Config is not a list");
+                       return Data [section] [key].list;
+               }
+               public static ISet<string> RequireList(string section, string key){
+                       ISet<string> result = GetList (section, key);
+                       if (result == null)
+                               throw new ApplicationException ("Config missing:"+ section+ key);
+                       return result;
+               }
+               public static String RequireConfig(string section, string key){
+                       string value = GetConfig (section, key);
+                       if (value == null)
+                               throw new ApplicationException ("No Config Entry for " + section + "/"+key);
+                       return value;
+               }
+               public static void Set(string section, string key, string value){
+                       Check ();
+                       if (! Data.ContainsKey (section)) {
+                               Data [section] = new Dictionary<string, ConfigEntry> ();
+                       }
+                       Data [section] [key] = new ConfigEntry(value);
+               }
+               public static void Set(string section, string key, ISet<string> value){
+                       Set (section, key, "LIST");
+                       Data [section] [key].list = value;
+               }
+               public static void Write(){
+                       Check ();
+                       StreamWriter File = new StreamWriter (Config.file_name, false);
+                       foreach (var section in Data) {
+                               File.WriteLine ();
+                               File.WriteLine ("[" + section.Key + "]");
+                               foreach (var elem in section.Value) {
+                                       File.WriteLine (elem.Key + ":" + elem.Value.value);
+                                       if (elem.Value.value == "LIST") {
+                                               foreach (string list_ele in elem.Value.list) {
+                                                       File.WriteLine (list_ele);
+                                               }
+                                               File.WriteLine ("LIST");
+                                       }
+                               }
+                       }
+                       File.Close();
+               }
+               public static void Remove(string section)
+               {
+                       Check ();
+                       Data.Remove (section);
+               }
+               private static void Check()
+               {
+                       if (!ready)
+                               throw new ApplicationException ("Configuration data not loaded");
+               }
+       }
+}
+