PageRenderTime 14ms CodeModel.GetById 7ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

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