New e-d-s backend which indexes all local addressbooks and calendars.
[beagle.git] / Util / GuidFu.cs
blobc2878872166377bc85bc867e544bf9509a8402e6
1 //
2 // GuidFu.cs
3 //
4 // Copyright (C) 2005 Novell, Inc.
5 //
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
15 // The above copyright notice and this permission notice shall be included in all
16 // copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 // SOFTWARE.
27 using System;
29 namespace Beagle.Util {
31 public class GuidFu {
33 static public string ToShortString (Guid uid)
35 // When converting 16 bytes to base64, the final two characters
36 // are always "==".
37 string str = Convert.ToBase64String (uid.ToByteArray (), 0, 16);
38 str = str.Substring (0, 22);
39 str = str.Replace ('/', '_'); // this avoids problems w/ Uris
40 return str;
43 static public Guid FromShortString (string str)
45 // We need to add back the padding before converting the
46 // string back to a byte array.
47 str = str.Replace ('_', '/') + "==";
48 return new Guid (Convert.FromBase64String (str));
51 static public Uri ToUri (Guid uid)
53 return new Uri ("uid:" + ToShortString (uid), true);
56 static public Uri FromShortStringToUri (string str)
58 return new Uri ("uid:" + str, true);
61 static public Guid FromUri (Uri uri)
63 return FromShortString (uri.LocalPath);