/SparkleShare/SparkleUIHelpers.cs

http://github.com/hbons/SparkleShare · C# · 85 lines · 50 code · 16 blank · 19 comment · 0 complexity · 64ae0fdc93f655988ee050970b2404a5 MD5 · raw file

  1. // SparkleShare, a collaboration and sharing tool.
  2. // Copyright (C) 2010 Hylke Bons <hylkebons@gmail.com>
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. using Gtk;
  17. using SparkleLib;
  18. using System;
  19. using System.IO;
  20. using System.Net;
  21. using System.Security.Cryptography;
  22. using System.Text;
  23. namespace SparkleShare {
  24. public static class SparkleUIHelpers {
  25. // Creates an MD5 hash of input
  26. public static string GetMD5 (string s)
  27. {
  28. MD5 md5 = new MD5CryptoServiceProvider ();
  29. Byte[] bytes = ASCIIEncoding.Default.GetBytes (s);
  30. Byte[] encodedBytes = md5.ComputeHash (bytes);
  31. return BitConverter.ToString (encodedBytes).ToLower ().Replace ("-", "");
  32. }
  33. // Looks up an icon from the system's theme
  34. public static Gdk.Pixbuf GetIcon (string name, int size)
  35. {
  36. IconTheme icon_theme = new IconTheme ();
  37. icon_theme.AppendSearchPath (
  38. Path.Combine (SparkleUI.AssetsPath, "icons")
  39. );
  40. icon_theme.AppendSearchPath (
  41. Path.Combine (Path.GetDirectoryName (SparkleConfig.DefaultConfig.FullPath), "icons")
  42. );
  43. try {
  44. return icon_theme.LoadIcon (name, size, IconLookupFlags.GenericFallback);
  45. } catch {
  46. try {
  47. return icon_theme.LoadIcon ("gtk-missing-image", size, IconLookupFlags.GenericFallback);
  48. } catch {
  49. return null;
  50. }
  51. }
  52. }
  53. public static Image GetImage (string name)
  54. {
  55. string image_path = SparkleHelpers.CombineMore (Defines.DATAROOTDIR, "sparkleshare",
  56. "pixmaps", name);
  57. return new Image (image_path);
  58. }
  59. // Converts a Gdk RGB color to a hex value.
  60. // Example: from "rgb:0,0,0" to "#000000"
  61. public static string GdkColorToHex (Gdk.Color color)
  62. {
  63. return String.Format ("#{0:X2}{1:X2}{2:X2}",
  64. (int) Math.Truncate (color.Red / 256.00),
  65. (int) Math.Truncate (color.Green / 256.00),
  66. (int) Math.Truncate (color.Blue / 256.00));
  67. }
  68. }
  69. }