Compute lucene-style scores for our hits.
[beagle.git] / images / Images.cs
blob4112ae43ffc1133e02ec379c2030e1659f3f1bb4
1 //
2 // Images.cs
3 //
4 // Copyright (C) 2004 Novell, Inc.
5 //
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a
9 // copy of this software and associated documentation files (the "Software"),
10 // to deal in the Software without restriction, including without limitation
11 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 // and/or sell copies of the Software, and to permit persons to whom the
13 // Software is furnished to do so, subject to the following conditions:
15 // The above copyright notice and this permission notice shall be included in
16 // all 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
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 // DEALINGS IN THE SOFTWARE.
27 using System;
28 using System.IO;
29 using System.Reflection;
30 using Beagle.Util;
32 using Gnome;
34 namespace Beagle {
36 public class Images {
38 // This class is fully static.
39 private Images () { }
41 static private Stream GetStreamInner (string name)
43 Stream stream = null;
45 if (name.StartsWith ("file://")) {
46 name = name.Substring ("file://".Length);
47 if (File.Exists (name))
48 stream = File.OpenRead (name);
49 } else if (name.StartsWith ("/")) {
50 if (File.Exists (name))
51 stream = File.OpenRead (name);
52 } else {
53 Assembly assembly = Assembly.GetExecutingAssembly ();
54 stream = assembly.GetManifestResourceStream (name);
57 return stream;
60 static public Stream GetStream (string name)
62 Stream stream;
64 if (name == null || name.Length == 0)
65 return null;
67 stream = GetStreamInner (name);
68 if (stream == null)
69 stream = GetStreamInner (name + ".png");
70 if (stream == null)
71 stream = GetStreamInner (name + ".jpg");
72 if (stream == null)
73 Console.WriteLine ("Couldn't get stream for image '{0}'", name);
74 return stream;
77 static public Gdk.Pixbuf GetPixbuf (string name)
79 Stream s = GetStream (name);
80 return s != null ? new Gdk.Pixbuf (s) : null;
83 static public Gdk.Pixbuf GetPixbuf (string name, int maxWidth, int maxHeight)
85 Gdk.Pixbuf pixbuf = GetPixbuf (name);
86 if (pixbuf == null)
87 return null;
89 double scaleWidth = maxWidth / (double)pixbuf.Width;
90 double scaleHeight = maxHeight / (double)pixbuf.Height;
92 double s = Math.Min (scaleWidth, scaleHeight);
93 if (s >= 1.0)
94 return pixbuf;
96 int w = (int) Math.Round (s * pixbuf.Width);
97 int h = (int) Math.Round (s * pixbuf.Height);
99 return pixbuf.ScaleSimple (w, h, Gdk.InterpType.Bilinear);
102 static public Gtk.Widget GetWidget (string name)
104 Gdk.Pixbuf pixbuf = GetPixbuf (name);
105 return pixbuf != null ? new Gtk.Image (pixbuf) : null;
108 static public Gtk.Widget GetWidget (string name, int maxWidth, int maxHeight)
110 Gdk.Pixbuf pixbuf = GetPixbuf (name, maxWidth, maxHeight);
111 return pixbuf != null ? new Gtk.Image (pixbuf) : null;
114 static public string GetHtmlSourceForStock (string stockid,
115 int size)
117 int base_size;
118 IconTheme icon_theme = new IconTheme ();
119 string path = icon_theme.LookupIcon (stockid, size, IconData.Zero, out base_size);
121 if (path != null && path != "") {
122 return "file://" + path;
124 return null;
127 static public string GetHtmlSource (byte[] binary_data,
128 string mime_type)
130 string base64_string =
131 System.Convert.ToBase64String(binary_data,
133 binary_data.Length);
135 string data = "data:" + mime_type + ";base64," + base64_string;
137 return data;
140 static public string GetHtmlSource (string name,
141 string mime_type)
143 if (name == null || name.Length == 0) {
144 return null;
145 } else if (name.StartsWith ("file://")) {
146 return name;
147 } else if (name.StartsWith ("/")) {
148 return StringFu.PathToQuotedFileUri (name);
149 } else {
150 Stream stream;
152 if (mime_type == null || mime_type == "")
153 throw new ArgumentException ();
155 // FIXME: it's probably worth caching these,
156 // since they'll probably be repeated a lot
158 stream = GetStream (name);
159 if (stream == null)
160 return null;
162 byte[] binary_data = new Byte[stream.Length];
163 stream.Read(binary_data, 0, (int) stream.Length);
164 stream.Close ();
165 return GetHtmlSource (binary_data, mime_type);