PageRenderTime 18ms CodeModel.GetById 13ms app.highlight 2ms RepoModel.GetById 2ms app.codeStats 0ms

/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
17using Gtk;
18using SparkleLib;
19using System;
20using System.IO;
21using System.Net;
22using System.Security.Cryptography;
23using System.Text;
24
25namespace SparkleShare {
26
27    public static class SparkleUIHelpers {
28
29        // Creates an MD5 hash of input
30        public static string GetMD5 (string s)
31        {
32            MD5 md5 = new MD5CryptoServiceProvider ();
33            Byte[] bytes = ASCIIEncoding.Default.GetBytes (s);
34            Byte[] encodedBytes = md5.ComputeHash (bytes);
35            return BitConverter.ToString (encodedBytes).ToLower ().Replace ("-", "");
36        }
37
38
39        // Looks up an icon from the system's theme
40        public static Gdk.Pixbuf GetIcon (string name, int size)
41        {
42            IconTheme icon_theme = new IconTheme ();
43
44            icon_theme.AppendSearchPath (
45                Path.Combine (SparkleUI.AssetsPath, "icons")
46            );
47
48            icon_theme.AppendSearchPath (
49                Path.Combine (Path.GetDirectoryName (SparkleConfig.DefaultConfig.FullPath), "icons")
50            );
51
52            try {
53                return icon_theme.LoadIcon (name, size, IconLookupFlags.GenericFallback);
54
55            } catch {
56                try {
57                    return icon_theme.LoadIcon ("gtk-missing-image", size, IconLookupFlags.GenericFallback);
58
59                } catch {
60                    return null;
61                }
62            }
63        }
64
65
66        public static Image GetImage (string name)
67        {
68            string image_path = SparkleHelpers.CombineMore (Defines.DATAROOTDIR, "sparkleshare",
69                "pixmaps", name);
70
71            return new Image (image_path);
72        }
73
74
75        // Converts a Gdk RGB color to a hex value.
76        // Example: from "rgb:0,0,0" to "#000000"
77        public static string GdkColorToHex (Gdk.Color color)
78        {
79            return String.Format ("#{0:X2}{1:X2}{2:X2}",
80                (int) Math.Truncate (color.Red   / 256.00),
81                (int) Math.Truncate (color.Green / 256.00),
82                (int) Math.Truncate (color.Blue  / 256.00));
83        }
84    }
85}