Ejemplo n.º 1
0
        private static void AddCategory(ICustomDestinationList cdl, string category, List <_ShellObjectPair> jumpItems, List <JumpItem> successList, List <_RejectedJumpItemPair> rejectionList, bool isHeterogenous)
        {
            Debug.Assert(jumpItems.Count != 0);
            Debug.Assert(cdl != null);

            HRESULT hr;
            var     shellObjectCollection = (IObjectCollection)Activator.CreateInstance(Type.GetTypeFromCLSID(new Guid(CLSID.EnumerableObjectCollection)));

            foreach (var itemMap in jumpItems)
            {
                shellObjectCollection.AddObject(itemMap.ShellObject);
            }

            if (string.IsNullOrEmpty(category))
            {
                hr = cdl.AddUserTasks((IObjectArray)shellObjectCollection);
            }
            else
            {
                hr = cdl.AppendCategory(category, (IObjectArray)shellObjectCollection);
            }

            if (hr.Succeeded)
            {
                // Woot! Add these items to the list.
                // Do it in reverse order so Apply has the items in the correct order.
                for (int i = jumpItems.Count; --i >= 0;)
                {
                    successList.Add(jumpItems[i].JumpItem);
                }
            }
            else
            {
                // If the list contained items that could not be added because this object isn't a handler
                // then drop all ShellItems and retry without them.
                if (isHeterogenous && hr == HRESULT.DESTS_E_NO_MATCHING_ASSOC_HANDLER)
                {
                    if (TraceShell.IsEnabled)
                    {
                        TraceShell.Trace(TraceEventType.Error, TraceShell.RejectingJumpListCategoryBecauseNoRegisteredHandler(category));
                    }

                    Utilities.SafeRelease(ref shellObjectCollection);
                    var linksOnlyList = new List <_ShellObjectPair>();
                    foreach (var itemMap in jumpItems)
                    {
                        if (itemMap.JumpItem is JumpPath)
                        {
                            rejectionList.Add(new _RejectedJumpItemPair {
                                JumpItem = itemMap.JumpItem, Reason = JumpItemRejectionReason.NoRegisteredHandler
                            });
                        }
                        else
                        {
                            linksOnlyList.Add(itemMap);
                        }
                    }
                    if (linksOnlyList.Count > 0)
                    {
                        // There's not a reason I know of that we should reject a list of only links...
                        Debug.Assert(jumpItems.Count != linksOnlyList.Count);
                        AddCategory(cdl, category, linksOnlyList, successList, rejectionList, false);
                    }
                }
                else
                {
                    Debug.Assert(HRESULT.DESTS_E_NO_MATCHING_ASSOC_HANDLER != hr);
                    // If we failed for some other reason, just reject everything.
                    foreach (var item in jumpItems)
                    {
                        rejectionList.Add(new _RejectedJumpItemPair {
                            JumpItem = item.JumpItem, Reason = JumpItemRejectionReason.InvalidItem
                        });
                    }
                }
            }
        }
        private static void AddCategory(ICustomDestinationList cdl, string category, List <JumpList._ShellObjectPair> jumpItems, List <JumpItem> successList, List <JumpList._RejectedJumpItemPair> rejectionList, bool isHeterogenous)
        {
            IObjectCollection objectCollection = (IObjectCollection)Activator.CreateInstance(Type.GetTypeFromCLSID(new Guid("2d3468c1-36a7-43b6-ac24-d3f02fd9607a")));

            foreach (JumpList._ShellObjectPair shellObjectPair in jumpItems)
            {
                objectCollection.AddObject(shellObjectPair.ShellObject);
            }
            HRESULT hrLeft;

            if (string.IsNullOrEmpty(category))
            {
                hrLeft = cdl.AddUserTasks(objectCollection);
            }
            else
            {
                hrLeft = cdl.AppendCategory(category, objectCollection);
            }
            if (hrLeft.Succeeded)
            {
                int num = jumpItems.Count;
                while (--num >= 0)
                {
                    successList.Add(jumpItems[num].JumpItem);
                }
                return;
            }
            if (isHeterogenous && hrLeft == HRESULT.DESTS_E_NO_MATCHING_ASSOC_HANDLER)
            {
                if (TraceShell.IsEnabled)
                {
                    TraceShell.Trace(TraceEventType.Error, TraceShell.RejectingJumpListCategoryBecauseNoRegisteredHandler(new object[]
                    {
                        category
                    }));
                }
                Utilities.SafeRelease <IObjectCollection>(ref objectCollection);
                List <JumpList._ShellObjectPair> list = new List <JumpList._ShellObjectPair>();
                foreach (JumpList._ShellObjectPair shellObjectPair2 in jumpItems)
                {
                    if (shellObjectPair2.JumpItem is JumpPath)
                    {
                        rejectionList.Add(new JumpList._RejectedJumpItemPair
                        {
                            JumpItem = shellObjectPair2.JumpItem,
                            Reason   = JumpItemRejectionReason.NoRegisteredHandler
                        });
                    }
                    else
                    {
                        list.Add(shellObjectPair2);
                    }
                }
                if (list.Count > 0)
                {
                    JumpList.AddCategory(cdl, category, list, successList, rejectionList, false);
                    return;
                }
            }
            else
            {
                foreach (JumpList._ShellObjectPair shellObjectPair3 in jumpItems)
                {
                    rejectionList.Add(new JumpList._RejectedJumpItemPair
                    {
                        JumpItem = shellObjectPair3.JumpItem,
                        Reason   = JumpItemRejectionReason.InvalidItem
                    });
                }
            }
        }
Ejemplo n.º 3
0
        private void ApplyList()
        {
            Debug.Assert(_initializing == false);
            Verify.IsApartmentState(ApartmentState.STA);

            // We don't want to force applications to conditionally check this before constructing a JumpList,
            // but if we're not on 7 then this isn't going to work.  Fail fast.
            if (!Utilities.IsOSWindows7OrNewer)
            {
                RejectEverything();
                return;
            }

            List <JumpItem> successList;
            List <_RejectedJumpItemPair> rejectedList;

            // Declare these outside the try block so we can cleanup native resources in the _ShellObjectPairs.
            List <List <_ShellObjectPair> > categories  = null;
            List <_ShellObjectPair>         removedList = null;
            var destinationList = (ICustomDestinationList)Activator.CreateInstance(Type.GetTypeFromCLSID(new Guid(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
                    {
                        Utilities.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();
            }
            catch
            {
                // It's not okay to throw an exception here.  If Shell is rejecting the JumpList for some reason
                // we don't want to be responsible for the app throwing an exception in its startup path.
                // For common use patterns there isn't really any user code on the stack, so there isn't
                // an opportunity to catch this in the app.
                // We can instead handle this.

                // Try to notify the developer if they're hitting this.
                if (TraceShell.IsEnabled)
                {
                    TraceShell.Trace(TraceEventType.Error, TraceShell.RejectingJumpItemsBecauseCatastrophicFailure);
                }
                RejectEverything();
                return;
            }
            finally
            {
                // Deterministically release native resources.

                Utilities.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);
            }

            // 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();
            _jumpItems = successList;

            // Raise the events for rejected and removed.
            EventHandler <JumpItemsRejectedEventArgs> rejectedHandler = JumpItemsRejected;
            EventHandler <JumpItemsRemovedEventArgs>  removedHandler  = JumpItemsRemovedByUser;

            if (rejectedList.Count > 0 && rejectedHandler != null)
            {
                var items   = new List <JumpItem>(rejectedList.Count);
                var reasons = new List <JumpItemRejectionReason>(rejectedList.Count);

                foreach (_RejectedJumpItemPair rejectionPair in rejectedList)
                {
                    items.Add(rejectionPair.JumpItem);
                    reasons.Add(rejectionPair.Reason);
                }

                rejectedHandler(this, new JumpItemsRejectedEventArgs(items, reasons));
            }

            if (removedList.Count > 0 && removedHandler != null)
            {
                var items = new List <JumpItem>(removedList.Count);
                foreach (_ShellObjectPair shellMap in removedList)
                {
                    // It's possible that not every shell object could be converted to a JumpItem.
                    if (shellMap.JumpItem != null)
                    {
                        items.Add(shellMap.JumpItem);
                    }
                }

                if (items.Count > 0)
                {
                    removedHandler(this, new JumpItemsRemovedEventArgs(items));
                }
            }
        }
        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));
                }
            }
        }