1 //
2 // secret.cs
3 //
4 // Authors:
5 //	Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //
7 // (C) Copyright 2006 Novell, Inc. (http://www.novell.com)
8 //
9 
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 //
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 //
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29 
30 using System;
31 using System.Collections;
32 using Gnome.Keyring;
33 
34 namespace Gnome.Keyring {
35 	public class Test {
Main()36 		static void Main ()
37 		{
38 			if (!Ring.Available) {
39 				Console.WriteLine ("The gnome-keyring-daemon cannot be reached.");
40 				return;
41 			}
42 
43 			string deflt = Ring.GetDefaultKeyring ();
44 			Console.WriteLine ("The default keyring is '{0}'", deflt);
45 			Console.Write ("Other rings available: ");
46 
47 			foreach (string s in Ring.GetKeyrings ()) {
48 				if (s != deflt)
49 					Console.Write ("'{0}' ", s);
50 			}
51 			Console.WriteLine ();
52 
53 			// This is equivalent to...
54 			foreach (ItemData s in Ring.FindNetworkPassword ("gonzalo", null, null, null, null, null, 0)) {
55 				Console.WriteLine ("HERE");
56 				Console.WriteLine (s);
57 			}
58 
59 			// ... this other search.
60 			Hashtable tbl = new Hashtable ();
61 			tbl ["user"] = "gonzalo";
62 			foreach (ItemData s in Ring.Find (ItemType.NetworkPassword, tbl)) {
63 				Console.WriteLine (s);
64 			}
65 
66 			tbl = new Hashtable ();
67 			tbl ["user"] = "lalalito";
68 			tbl ["domain"] = "MiDomain";
69 			Console.WriteLine ("Creating item");
70 			int i = Ring.CreateItem (null, ItemType.NetworkPassword, "lala@pepe.com", tbl, "laclave", true);
71 			ItemData d2 = Ring.GetItemInfo (deflt, i);
72 			Ring.SetItemInfo (deflt, d2.ItemID, ItemType.NetworkPassword, "cambioesto@lalala", "otraclave");
73 			Hashtable atts = Ring.GetItemAttributes (deflt, i);
74 			foreach (string key in atts.Keys) {
75 				Console.WriteLine ("{0}: {1}", key, atts [key]);
76 			}
77 
78 			atts ["object"] = "new attributes";
79 			Ring.SetItemAttributes (deflt, i, atts);
80 			Console.WriteLine ("Press any key to continue...");
81 			Console.ReadLine ();
82 
83 			Console.WriteLine ("Deleting it (ID = {0})", i);
84 			Ring.DeleteItem (Ring.GetDefaultKeyring (), i);
85 			Console.WriteLine ("Existing IDs...");
86 			foreach (int nn in Ring.ListItemIDs (deflt)) {
87 				Console.WriteLine (nn);
88 			}
89 
90 			KeyringInfo info = new KeyringInfo (true, 15);
91 			Ring.SetKeyringInfo (deflt, info);
92 
93 			info = Ring.GetKeyringInfo (deflt);
94 			Console.WriteLine (info);
95 			ArrayList acl_list = Ring.GetItemACL (deflt, 3);
96 			foreach (ItemACL acl in acl_list)
97 				Console.WriteLine (acl);
98 
99 			ArrayList list2 = new ArrayList (acl_list);
100 			list2.Add (new ItemACL ("test", "/test", AccessRights.Read));
101 			Ring.SetItemACL (deflt, 3, list2);
102 			Console.WriteLine ("Press any key to continue...");
103 			Console.ReadLine ();
104 			Ring.SetItemACL (deflt, 3, acl_list);
105 		}
106 	}
107 }
108 
109