public static void CreateJumpList(NoteManager note_manager) { ICustomDestinationList custom_destinationd_list = null; IObjectArray removed_objects = null; try { custom_destinationd_list = (ICustomDestinationList)Activator.CreateInstance(Type.GetTypeFromCLSID(CLSID.DestinationList)); uint slots; Guid riid = CLSID.IObjectArray; Logger.Debug("Windows Taskbar: Begin jump list"); custom_destinationd_list.BeginList(out slots, ref riid, out removed_objects); try { AddUserTasks(custom_destinationd_list); } catch (UnauthorizedAccessException uae) { Logger.Warn("Access denied adding user tasks to jump list: {0}\n{1}", uae.Message, uae.StackTrace); } try { AddRecentNotes(custom_destinationd_list, note_manager, slots); } catch (UnauthorizedAccessException uae) { Logger.Warn("Access denied adding recent notes to jump list: {0}\n{1}", uae.Message, uae.StackTrace); } Logger.Debug("Windows Taskbar: Commit jump list"); custom_destinationd_list.CommitList(); } catch (Exception e) { Logger.Error("Error creating jump list: {0}\n{1}", e.Message, e.StackTrace); if (custom_destinationd_list != null) { custom_destinationd_list.AbortList(); } } finally { if (removed_objects != null) { Marshal.FinalReleaseComObject(removed_objects); removed_objects = null; } if (custom_destinationd_list != null) { Marshal.FinalReleaseComObject(custom_destinationd_list); custom_destinationd_list = null; } } }
/// <summary> /// Commits the pending JumpList changes and refreshes the Taskbar. /// </summary> /// <exception cref="System.InvalidOperationException">Will throw if the type of the file being added to the JumpList is not registered with the application.</exception> /// <exception cref="System.UnauthorizedAccessException">Will throw if recent documents tracking is turned off by the user or via group policy.</exception> /// <exception cref="System.Runtime.InteropServices.COMException">Will throw if updating the JumpList fails for any other reason.</exception> public void Refresh() { // Let the taskbar know which specific jumplist we are updating if (!string.IsNullOrEmpty(ApplicationId)) { customDestinationList.SetAppID(ApplicationId); } // Begins rendering on the taskbar destination list BeginList(); Exception exception = null; try { // try to add the user tasks first AppendTaskList(); } catch (Exception e) { // If this fails, save the exception but don't throw it yet. // We need to continue to try and add the custom categories exception = e; } // Even it fails, continue appending the custom categories try { // Add custom categories AppendCustomCategories(); } finally { // End rendering of the taskbar destination list customDestinationList.CommitList(); } // If an exception was thrown while adding the user tasks or // custom categories, throw it. if (exception != null) { throw exception; } }
/// <summary> /// Commits the pending JumpList changes and refreshes the Taskbar. /// </summary> /// <exception cref="System.InvalidOperationException">Will throw if the type of the file being added to the JumpList is not registered with the application.</exception> /// <exception cref="System.UnauthorizedAccessException">Will throw if recent documents tracking is turned off by the user or via group policy.</exception> /// <exception cref="System.Runtime.InteropServices.COMException">Will throw if updating the JumpList fails for any other reason.</exception> public void Refresh() { // Let the taskbar know which specific jumplist we are updating if (!string.IsNullOrEmpty(ApplicationId)) { customDestinationList.SetAppID(ApplicationId); } // Begins rendering on the taskbar destination list BeginList(); // Even it fails, continue appending the custom categories try { // Add custom categories AppendCustomCategories(); } finally { // End rendering of the taskbar destination list customDestinationList.CommitList(); } }
public void Apply() { BeginList(); try { if (active == null) { active = new List <JumpItemTask>(); } if (active.Count > 0) { foreach (var t in active) { t.Dispose(); } active.Clear(); } var count = tasks.Count; if (count > 0) { IObjectCollection content = null; var categories = new Dictionary <string, IObjectCollection>(); for (var i = 0; i < count; ++i) { var t = tasks[i]; if (i > 0) { if (tasks[i - 1].CustomCategory != t.CustomCategory) { content = null; } } if (content == null) { var c = t.CustomCategory; if (c == null) { c = ""; } if (!categories.TryGetValue(c, out content)) { content = categories[c] = (IObjectCollection) new CEnumerableObjectCollection(); } } var jit = new JumpItemTask(t); active.Add(jit); content.AddObject(jit.NativeShellLink); } foreach (var k in categories.Keys) { if (k.Length == 0) { list.AddUserTasks((IObjectArray)categories[k]); } else { var r = list.AppendCategory(k, (IObjectArray)categories[k]); if (r < 0) { if (r == -2147024891) //denied (tracking of recent files is disabled) { throw new CustomCategoryException(); } throw Marshal.GetExceptionForHR(r); } } } } } finally { list.CommitList(); } }
private void _BuildShellLists(out List <JumpItem> successList, out List <JumpList._RejectedJumpItemPair> rejectedList, out List <JumpList._ShellObjectPair> removedList) { List <List <JumpList._ShellObjectPair> > list = null; removedList = null; ICustomDestinationList customDestinationList = CLSID.CoCreateInstance <ICustomDestinationList>("77f10cf0-3db5-4966-b520-b7c54fd35ed6"); try { string runtimeId = JumpList._RuntimeId; if (!string.IsNullOrEmpty(runtimeId)) { customDestinationList.SetAppID(runtimeId); } Guid guid = new Guid("92CA9DCD-5622-4bba-A805-5E9F541BD8C9"); uint num; IObjectArray shellObjects = (IObjectArray)customDestinationList.BeginList(out num, ref guid); removedList = JumpList.GenerateJumpItems(shellObjects); successList = new List <JumpItem>(this.JumpItems.Count); rejectedList = new List <JumpList._RejectedJumpItemPair>(this.JumpItems.Count); list = new List <List <JumpList._ShellObjectPair> > { new List <JumpList._ShellObjectPair>() }; foreach (JumpItem jumpItem in this.JumpItems) { if (jumpItem == null) { rejectedList.Add(new JumpList._RejectedJumpItemPair { JumpItem = jumpItem, Reason = JumpItemRejectionReason.InvalidItem }); } else { object obj = null; try { obj = JumpList.GetShellObjectForJumpItem(jumpItem); if (obj == null) { rejectedList.Add(new JumpList._RejectedJumpItemPair { Reason = JumpItemRejectionReason.InvalidItem, JumpItem = jumpItem }); } else if (JumpList.ListContainsShellObject(removedList, obj)) { rejectedList.Add(new JumpList._RejectedJumpItemPair { Reason = JumpItemRejectionReason.RemovedByUser, JumpItem = jumpItem }); } else { JumpList._ShellObjectPair item = new JumpList._ShellObjectPair { JumpItem = jumpItem, ShellObject = obj }; if (string.IsNullOrEmpty(jumpItem.CustomCategory)) { list[0].Add(item); } else { bool flag = false; foreach (List <JumpList._ShellObjectPair> list2 in list) { if (list2.Count > 0 && list2[0].JumpItem.CustomCategory == jumpItem.CustomCategory) { list2.Add(item); flag = true; break; } } if (!flag) { list.Add(new List <JumpList._ShellObjectPair> { item }); } } obj = null; } } finally { Utility.SafeRelease <object>(ref obj); } } } list.Reverse(); if (this.ShowFrequentCategory) { customDestinationList.AppendKnownCategory(KDC.FREQUENT); } if (this.ShowRecentCategory) { customDestinationList.AppendKnownCategory(KDC.RECENT); } foreach (List <JumpList._ShellObjectPair> list3 in list) { if (list3.Count > 0) { string customCategory = list3[0].JumpItem.CustomCategory; JumpList.AddCategory(customDestinationList, customCategory, list3, successList, rejectedList); } } customDestinationList.CommitList(); successList.Reverse(); } finally { Utility.SafeRelease <ICustomDestinationList>(ref customDestinationList); if (list != null) { foreach (List <JumpList._ShellObjectPair> list4 in list) { JumpList._ShellObjectPair.ReleaseShellObjects(list4); } } JumpList._ShellObjectPair.ReleaseShellObjects(removedList); } }
private void CommitList() { _customDestinationList.CommitList(); }
private void _BuildShellLists(out List <JumpItem> successList, out List <_RejectedJumpItemPair> rejectedList, out List <_ShellObjectPair> removedList) { // Declare these outside the try block so we can cleanup native resources in the _ShellObjectPairs. List <List <_ShellObjectPair> > categories = null; removedList = null; ICustomDestinationList destinationList = CLSID.CoCreateInstance <ICustomDestinationList>(CLSID.DestinationList); try { // Even though we're not exposing Shell's AppModelId concept, we'll still respect it // since it's easy to clients to p/invoke to set it for Application and Window, but not for JumpLists string appId = _RuntimeId; if (!string.IsNullOrEmpty(appId)) { destinationList.SetAppID(appId); } // The number ot items visible on a jump list is a user setting. Shell doesn't reject items based on overflow. // We don't bother checking against it because the app can query the setting and manage overflow based on it // if they really care. We'll happily add too many items with the hope that if the user changes the setting // items will be recovered from the overflow. uint slotsVisible; Guid removedIid = new Guid(IID.ObjectArray); var objectsRemoved = (IObjectArray)destinationList.BeginList(out slotsVisible, ref removedIid); // Keep track of the items that were previously removed by the user. // We don't want to pend any items that are contained in this list. // It's possible that this contains null JumpItems when we were unable to do the conversion. removedList = GenerateJumpItems(objectsRemoved); // Keep track of the items that have been successfully pended. // IMPORTANT: Ensure at the end of this that if the list is applied again that it would // result in items being added to the JumpList in the same order. // This doesn't mean that they'll be ordered the same as how the user added them // (e.g. categories will be coalesced), but the categories should appear in the same order. // Since when we call AddCategory we're doing it in reverse order, AddCategory augments // the items in the list in reverse as well. At the end the final list is reversed. successList = new List <JumpItem>(JumpItems.Count); // Keep track of the items that we couldn't pend, and why. rejectedList = new List <_RejectedJumpItemPair>(JumpItems.Count); // Need to group the JumpItems based on their categories. // The special "Tasks" category doesn't actually have a name and should be first so it's unconditionally added. categories = new List <List <_ShellObjectPair> >() { new List <_ShellObjectPair>() }; // This is not thread-safe. // We're traversing the original list so we're vulnerable to another thread modifying it during the enumeration. foreach (var jumpItem in JumpItems) { if (jumpItem == null) { // App added a null jump item? Just go through the normal failure mechanisms. rejectedList.Add(new _RejectedJumpItemPair { JumpItem = jumpItem, Reason = JumpItemRejectionReason.InvalidItem }); continue; } object shellObject = null; try { shellObject = GetShellObjectForJumpItem(jumpItem); // If for some reason we couldn't create the item add it to the rejected list. if (shellObject == null) { rejectedList.Add(new _RejectedJumpItemPair { Reason = JumpItemRejectionReason.InvalidItem, JumpItem = jumpItem }); continue; } // Don't add this item if it's in the list of items previously removed by the user. if (ListContainsShellObject(removedList, shellObject)) { rejectedList.Add(new _RejectedJumpItemPair { Reason = JumpItemRejectionReason.RemovedByUser, JumpItem = jumpItem }); continue; } var shellMap = new _ShellObjectPair { JumpItem = jumpItem, ShellObject = shellObject }; if (string.IsNullOrEmpty(jumpItem.CustomCategory)) { // No custom category, so add to the Tasks list. categories[0].Add(shellMap); } else { // Find the appropriate category and add to that list. // If it doesn't exist, add a new category for it. bool categoryExists = false; foreach (var list in categories) { // The first item in the category list can be used to check the name. if (list.Count > 0 && list[0].JumpItem.CustomCategory == jumpItem.CustomCategory) { list.Add(shellMap); categoryExists = true; break; } } if (!categoryExists) { categories.Add(new List <_ShellObjectPair>() { shellMap }); } } // Shell interface is now owned by the category list. shellObject = null; } finally { Utility.SafeRelease(ref shellObject); } } // Jump List categories get added top-down, except for "Tasks" which is special and always at the bottom. // We want the Recent/Frequent to always be at the top so they get added first. // Logically the categories are added bottom-up, but their contents are top-down, // so we reverse the order we add the categories to the destinationList. // To preserve the item ordering AddCategory also adds items in reverse. // We need to reverse the final list when everything is done. categories.Reverse(); if (ShowFrequentCategory) { destinationList.AppendKnownCategory(KDC.FREQUENT); } if (ShowRecentCategory) { destinationList.AppendKnownCategory(KDC.RECENT); } // Now that all the JumpItems are grouped add them to the custom destinations list. foreach (List <_ShellObjectPair> categoryList in categories) { if (categoryList.Count > 0) { string categoryHeader = categoryList[0].JumpItem.CustomCategory; AddCategory(destinationList, categoryHeader, categoryList, successList, rejectedList); } } destinationList.CommitList(); // Swap the current list with what we were able to successfully place into the JumpList. // Reverse it first to ensure that the items are in a repeatable order. successList.Reverse(); } finally { // Deterministically release native resources. Utility.SafeRelease(ref destinationList); if (categories != null) { foreach (List <_ShellObjectPair> list in categories) { _ShellObjectPair.ReleaseShellObjects(list); } } // Note that this only clears the ShellObjects, not the JumpItems. // We still need the JumpItems out of this list for the JumpItemsRemovedByUser event. _ShellObjectPair.ReleaseShellObjects(removedList); } }
private void ApplyList() { Verify.IsApartmentState(ApartmentState.STA); if (!Utilities.IsOSWindows7OrNewer) { this.RejectEverything(); return; } List <List <JumpList._ShellObjectPair> > list = null; List <JumpList._ShellObjectPair> list2 = null; ICustomDestinationList customDestinationList = (ICustomDestinationList)Activator.CreateInstance(Type.GetTypeFromCLSID(new Guid("77f10cf0-3db5-4966-b520-b7c54fd35ed6"))); List <JumpItem> list3; List <JumpList._RejectedJumpItemPair> list4; try { string runtimeId = JumpList._RuntimeId; if (!string.IsNullOrEmpty(runtimeId)) { customDestinationList.SetAppID(runtimeId); } Guid guid = new Guid("92CA9DCD-5622-4bba-A805-5E9F541BD8C9"); uint num; IObjectArray shellObjects = (IObjectArray)customDestinationList.BeginList(out num, ref guid); list2 = JumpList.GenerateJumpItems(shellObjects); list3 = new List <JumpItem>(this.JumpItems.Count); list4 = new List <JumpList._RejectedJumpItemPair>(this.JumpItems.Count); list = new List <List <JumpList._ShellObjectPair> > { new List <JumpList._ShellObjectPair>() }; foreach (JumpItem jumpItem in this.JumpItems) { if (jumpItem == null) { list4.Add(new JumpList._RejectedJumpItemPair { JumpItem = jumpItem, Reason = JumpItemRejectionReason.InvalidItem }); } else { object obj = null; try { obj = JumpList.GetShellObjectForJumpItem(jumpItem); if (obj == null) { list4.Add(new JumpList._RejectedJumpItemPair { Reason = JumpItemRejectionReason.InvalidItem, JumpItem = jumpItem }); } else if (JumpList.ListContainsShellObject(list2, obj)) { list4.Add(new JumpList._RejectedJumpItemPair { Reason = JumpItemRejectionReason.RemovedByUser, JumpItem = jumpItem }); } else { JumpList._ShellObjectPair item = new JumpList._ShellObjectPair { JumpItem = jumpItem, ShellObject = obj }; if (string.IsNullOrEmpty(jumpItem.CustomCategory)) { list[0].Add(item); } else { bool flag = false; foreach (List <JumpList._ShellObjectPair> list5 in list) { if (list5.Count > 0 && list5[0].JumpItem.CustomCategory == jumpItem.CustomCategory) { list5.Add(item); flag = true; break; } } if (!flag) { list.Add(new List <JumpList._ShellObjectPair> { item }); } } obj = null; } } finally { Utilities.SafeRelease <object>(ref obj); } } } list.Reverse(); if (this.ShowFrequentCategory) { customDestinationList.AppendKnownCategory(KDC.FREQUENT); } if (this.ShowRecentCategory) { customDestinationList.AppendKnownCategory(KDC.RECENT); } foreach (List <JumpList._ShellObjectPair> list6 in list) { if (list6.Count > 0) { string customCategory = list6[0].JumpItem.CustomCategory; JumpList.AddCategory(customDestinationList, customCategory, list6, list3, list4); } } customDestinationList.CommitList(); } catch { if (TraceShell.IsEnabled) { TraceShell.Trace(TraceEventType.Error, TraceShell.RejectingJumpItemsBecauseCatastrophicFailure); } this.RejectEverything(); return; } finally { Utilities.SafeRelease <ICustomDestinationList>(ref customDestinationList); if (list != null) { foreach (List <JumpList._ShellObjectPair> list7 in list) { JumpList._ShellObjectPair.ReleaseShellObjects(list7); } } JumpList._ShellObjectPair.ReleaseShellObjects(list2); } list3.Reverse(); this._jumpItems = list3; EventHandler <JumpItemsRejectedEventArgs> jumpItemsRejected = this.JumpItemsRejected; EventHandler <JumpItemsRemovedEventArgs> jumpItemsRemovedByUser = this.JumpItemsRemovedByUser; if (list4.Count > 0 && jumpItemsRejected != null) { List <JumpItem> list8 = new List <JumpItem>(list4.Count); List <JumpItemRejectionReason> list9 = new List <JumpItemRejectionReason>(list4.Count); foreach (JumpList._RejectedJumpItemPair rejectedJumpItemPair in list4) { list8.Add(rejectedJumpItemPair.JumpItem); list9.Add(rejectedJumpItemPair.Reason); } jumpItemsRejected(this, new JumpItemsRejectedEventArgs(list8, list9)); } if (list2.Count > 0 && jumpItemsRemovedByUser != null) { List <JumpItem> list10 = new List <JumpItem>(list2.Count); foreach (JumpList._ShellObjectPair shellObjectPair in list2) { if (shellObjectPair.JumpItem != null) { list10.Add(shellObjectPair.JumpItem); } } if (list10.Count > 0) { jumpItemsRemovedByUser(this, new JumpItemsRemovedEventArgs(list10)); } } }