monodev
[mcloud.git] / mCloudAwk / MCloudMgr.cs
1 using System;
2
3 using util;
4
5 namespace mCloudAwk
6 {
7         public class MCloudMgr : IMCloudAwk, IDisposable
8         {
9                 public TConnection[] GetConnections()
10                 {
11                         var Connections = util.Config.GetList ("General", "Connections");
12                         if (Connections == null)
13                                 return new TConnection[0];
14                         TConnection[] TConns = new TConnection[Connections.Count];
15                         int i = 0;
16                         foreach (var C in Connections) {
17                                 TConnection TC = new TConnection ();
18                                 TC.Name = C;
19                                 TC.User = util.Config.RequireConfig (TC.Name, "User");
20                                 TC.Server = util.Config.RequireConfig (TC.Name, "Server");
21                                 TC.Password = util.Config.RequireConfig (TC.Name, "Password");
22                                 TConns [i] = TC;
23                                 i++;
24                         }
25                         return TConns;
26                 }
27
28                 public void AddConnection(TConnection C) {
29                         var Connections = util.Config.GetList ("General", "Connections");
30                         if (Connections == null) {
31                                 Connections = new System.Collections.Generic.SortedSet<string> { C.Name };
32                         } else {
33                                 Connections.Add (C.Name);
34                         }
35                         Config.Set ("General", "Connections", Connections);
36                         Config.Set (C.Name, "User", C.User);
37                         Config.Set (C.Name, "Server", C.Server);
38                         Config.Set (C.Name, "Password", C.Password);
39                 }
40
41                 public void DeleteConnection(string name) {
42                         var Connections = Config.GetList ("General", "Connections");
43                         Connections.Remove (name);
44                         Config.Set("General", "Connections", Connections);
45                         Config.Remove(name);
46                 }
47
48                 // TestConnection results
49                 public const int SSH_NO_PW = 0; // connection without password possible
50                 public const int SSH_WITH_PW = 1; // connection only possible with password
51                 public const int SSH_NOK = 10; // so ssh connection possible
52                 public int TestConnection(string name){return 5;}
53
54                 public MCloudMgr(){
55                         int r = util.Config.Load (System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal)
56                                                                            +"/.mCloud.Config");
57                         if (r == util.Config.LOAD_OK || r == util.Config.LOAD_NO_INPUT_FILE) {
58                                 // everything fine
59                         } else {
60                                 throw new ApplicationException ("Config load failed");
61                         }
62                 }
63
64                 public void Dispose() {
65                         util.Config.Write ();
66                 }
67         }
68 }
69