Welcome to mirror list, hosted at ThFree Co, Russian Federation.

JumpList.cs « WindowsPlatform « WindowsPlatform « addins « src « main - github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d2baeebf3e69da3f4b5affe6bcb800cad31dcb6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// 
// JumpList.cs
//  
// Author:
//       Steven Schermerhorn <stevens+monoaddins@ischyrus.com>
//

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Timers;
using Microsoft.Win32;
using MonoDevelop.Components.Commands;
using MonoDevelop.Ide;
using MonoDevelop.Ide.Desktop;
using Taskbar = Microsoft.WindowsAPICodePack.Taskbar;

namespace MonoDevelop.Platform
{
	public class JumpList : CommandHandler
	{
		private IList<string> supportedExtensions;
		private RecentFiles recentFiles;
		private Timer updateTimer;

		protected override void Run ()
		{
			bool isWindows7 = Taskbar.TaskbarManager.IsPlatformSupported;
			if (!isWindows7) {
				return;
			}
			
			bool areFileExtensionsRegistered = this.Initialize ();
			
			if (!areFileExtensionsRegistered) {
				return;
			}
			
			this.updateTimer = new Timer (1000);
			this.updateTimer.Elapsed += this.OnUpdateTimerEllapsed;
			this.updateTimer.AutoReset = false;
			
			this.recentFiles = DesktopService.RecentFiles;
			this.recentFiles.Changed += this.OnRecentFilesChanged;

			try {
				UpdateJumpList();
			} catch (Exception ex) {
				MonoDevelop.Core.LoggingService.LogError ("Could not update jumplists", ex);
			}
		}

		private void OnRecentFilesChanged (object sender, EventArgs args)
		{
			// This event fires several times for a single change. Rather than performing the update
			// several times we will restart the timer which has a 1 second delay on it. 
			// While this means the update won't make it to the JumpList immediately it is significantly
			// better for performance.
			this.updateTimer.Stop ();
			this.updateTimer.Start ();
		}

		private void OnUpdateTimerEllapsed (object sender, EventArgs args)
		{
			try {
				UpdateJumpList();
			} catch (Exception ex) {
				MonoDevelop.Core.LoggingService.LogError ("Could not update jumplists", ex);
			}
		}

		private void UpdateJumpList ()
		{
			Taskbar.JumpList jumplist = Taskbar.JumpList.CreateJumpList ();
			jumplist.KnownCategoryToDisplay = Taskbar.JumpListKnownCategoryType.Neither;
			
			Taskbar.JumpListCustomCategory recentProjectsCategory = new Taskbar.JumpListCustomCategory ("Recent Solutions");
			Taskbar.JumpListCustomCategory recentFilesCategory = new Taskbar.JumpListCustomCategory ("Recent Files");
			
			jumplist.AddCustomCategories (recentProjectsCategory, recentFilesCategory);
			jumplist.KnownCategoryOrdinalPosition = 0;
			
			foreach (RecentFile recentProject in recentFiles.GetProjects ()) {
				// Windows is picky about files that are added to the jumplist. Only files that MonoDevelop
				// has been registered as supported in the registry can be added.
				bool isSupportedFileExtension = this.supportedExtensions.Contains (Path.GetExtension (recentProject.FileName));
				if (isSupportedFileExtension) {
					recentProjectsCategory.AddJumpListItems (new Taskbar.JumpListItem (recentProject.FileName));
				}
			}
			
			foreach (RecentFile recentFile in recentFiles.GetFiles ()) {
				if (this.supportedExtensions.Contains (Path.GetExtension (recentFile.FileName)))
					recentFilesCategory.AddJumpListItems (new Taskbar.JumpListItem (recentFile.FileName));
			}
			
			jumplist.Refresh ();
		}

		private bool Initialize ()
		{
			this.supportedExtensions = new List<string> ();
			
			// Determine the correct value for /HKCR/MonoDevelop[version]/shell/Open/Command
			ProcessModule monoDevelopAssembly = Process.GetCurrentProcess ().MainModule;
			string exePath = monoDevelopAssembly.FileName;
			string executeString = exePath + " %1";
			string progId = MonoDevelop.Core.BrandingService.ProfileDirectoryName + "." + IdeApp.Version;
			string appId = progId;
			
			Taskbar.TaskbarManager.Instance.ApplicationId = progId;
			
			RegistryKey progIdKey = Registry.ClassesRoot.OpenSubKey (progId + @"\shell\Open\Command", false);
			if (progIdKey == null) {
				return false;
			}
			
			object path = progIdKey.GetValue (String.Empty);
			bool isProgIdRegistered = String.Equals (executeString, path as string, StringComparison.OrdinalIgnoreCase);
			if (!isProgIdRegistered) {
				return false;
			}
			
			string[] subkeyNames = Registry.ClassesRoot.GetSubKeyNames ();
			foreach (string subkey in subkeyNames) {
				if (subkey[0] != '.') {
					continue;
				}
				
				RegistryKey openWithKey = Registry.ClassesRoot.OpenSubKey (Path.Combine (subkey, "OpenWithProgids"));
				if (openWithKey == null) {
					continue;
				}
				
				string progIdValue = openWithKey.GetValue (progId, null) as string;
				if (progIdValue == null) {
					continue;
				}
				
				this.supportedExtensions.Add (subkey);
			}
			
			bool atLeastOneFileTypeRegistered = this.supportedExtensions.Count > 0;
			return atLeastOneFileTypeRegistered;
		}
	}
}