/SparkleShare/Mac/SparkleBadger.cs

http://github.com/hbons/SparkleShare · C# · 91 lines · 54 code · 21 blank · 16 comment · 2 complexity · 385bb33aaf39a759cafa05f5b416759d 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 System;
  17. using System.Drawing;
  18. using System.IO;
  19. using System.Collections.Generic;
  20. using MonoMac.AppKit;
  21. using MonoMac.Foundation;
  22. using MonoMac.Growl;
  23. namespace SparkleShare {
  24. public class SparkleBadger {
  25. private Dictionary<string, NSImage> icons = new Dictionary<string, NSImage> ();
  26. private int [] sizes = new int [] {16, 32, 48, 128, 256, 512};
  27. private string [] paths;
  28. public SparkleBadger (string [] paths)
  29. {
  30. this.paths = paths;
  31. }
  32. public void Badge ()
  33. {
  34. using (NSAutoreleasePool a = new NSAutoreleasePool ()) {
  35. foreach (string path in this.paths) {
  36. string extension = Path.GetExtension (path.ToLower ());
  37. NSImage new_icon = new NSImage ();
  38. if (!this.icons.ContainsKey (extension)) {
  39. foreach (int size in this.sizes) {
  40. NSImage file_icon = NSWorkspace.SharedWorkspace.IconForFileType (extension);
  41. file_icon.Size = new SizeF (size, size);
  42. // TODO: replace this with the sync icon
  43. NSImage overlay_icon = NSWorkspace.SharedWorkspace.IconForFileType ("sln");
  44. overlay_icon.Size = new SizeF (size / 2, size / 2);
  45. file_icon.LockFocus ();
  46. NSGraphicsContext.CurrentContext.ImageInterpolation = NSImageInterpolation.High;
  47. overlay_icon.Draw (
  48. new RectangleF (0, 0, file_icon.Size.Width / 3, file_icon.Size.Width / 3),
  49. new RectangleF (), NSCompositingOperation.SourceOver, 1.0f);
  50. file_icon.UnlockFocus ();
  51. new_icon.AddRepresentation (file_icon.Representations () [0]);
  52. }
  53. this.icons.Add (extension, new_icon);
  54. } else {
  55. new_icon = this.icons [extension];
  56. }
  57. NSWorkspace.SharedWorkspace.SetIconforFile (new_icon, path, 0);
  58. }
  59. }
  60. }
  61. public void Clear ()
  62. {
  63. foreach (string path in this.paths) {
  64. string extension = Path.GetExtension (path.ToLower ());
  65. NSImage original_icon = NSWorkspace.SharedWorkspace.IconForFileType (extension);
  66. NSWorkspace.SharedWorkspace.SetIconforFile (original_icon, path, 0);
  67. }
  68. }
  69. }
  70. }