/// <summary>
        /// Repopulates the application's jump list.
        /// Use this method after all current changes to
        /// the application's jump list have been introduced,
        /// and you want the list to be refreshed.
        /// </summary>
        /// <returns><b>true</b> if the list was refreshed; <b>false</b>
        /// if the operation was cancelled.  The operation might have
        /// been cancelled if the <see cref="UserRemovedItems"/> event
        /// handler instructed us to cancel the operation.</returns>
        /// <remarks>
        /// If the user removed items from the jump list between the
        /// last refresh operation and this one, then the
        /// <see cref="UserRemovedItems"/> event will be invoked.
        /// If the event handler for this event instructed us to cancel
        /// the operation, then the current transaction is aborted,
        /// no items are added, and this method returns <b>false</b>.
        /// Check the return value to determine whether the jump list
        /// needs to be changed and the operation attempted again.
        /// </remarks>
        public bool Refresh()
        {
            if (!BeginList())
            {
                return(false);   //Operation was cancelled
            }
            _tasks.RefreshTasks(_customDestinationList);
            _destinations.RefreshDestinations(_customDestinationList);

            switch (EnabledAutoDestinationType)
            {
            case ApplicationDestinationType.Frequent:
                HResult appendKnownCategoryFrequentResult = _customDestinationList.AppendKnownCategory(KnownDestCategory.FREQUENT);
                appendKnownCategoryFrequentResult.ThrowIf();
                break;

            case ApplicationDestinationType.Recent:
                HResult appendKnownCategoryRecentResult = _customDestinationList.AppendKnownCategory(KnownDestCategory.RECENT);
                appendKnownCategoryRecentResult.ThrowIf();
                break;
            }

            CommitList();
            return(true);
        }
Example #2
0
 private void AppendKnownCategories()
 {
     if (KnownCategoryToDisplay == JumpListKnownCategoryType.Recent)
     {
         customDestinationList.AppendKnownCategory(KnownDestinationCategory.Recent);
     }
     else if (KnownCategoryToDisplay == JumpListKnownCategoryType.Frequent)
     {
         customDestinationList.AppendKnownCategory(KnownDestinationCategory.Frequent);
     }
 }
 private void AppendKnownCategories()
 {
     if (KnownCategoryToDisplay == JumpListKnownCategoryType.Recent)
     {
         customDestinationList.AppendKnownCategory(Shell32.KNOWNDESTCATEGORY.KDC_RECENT);
     }
     else if (KnownCategoryToDisplay == JumpListKnownCategoryType.Frequent)
     {
         customDestinationList.AppendKnownCategory(Shell32.KNOWNDESTCATEGORY.KDC_FREQUENT);
     }
 }
Example #4
0
        /// <summary>
        /// Repopulates the application's jump list.
        /// Use this method after all current changes to
        /// the application's jump list have been introduced,
        /// and you want the list to be refreshed.
        /// </summary>
        /// <returns><b>true</b> if the list was refreshed; <b>false</b>
        /// if the operation was cancelled.  The operation might have
        /// been cancelled if the <see cref="UserRemovedItems"/> event
        /// handler instructed us to cancel the operation.</returns>
        /// <remarks>
        /// If the user removed items from the jump list between the
        /// last refresh operation and this one, then the
        /// <see cref="UserRemovedItems"/> event will be invoked.
        /// If the event handler for this event instructed us to cancel
        /// the operation, then the current transaction is aborted,
        /// no items are added, and this method returns <b>false</b>.
        /// Check the return value to determine whether the jump list
        /// needs to be changed and the operation attempted again.
        /// </remarks>
        public bool Refresh()
        {
            if (!BeginList())
            {
                return(false);   //Operation was cancelled
            }
            _tasks.RefreshTasks(_customDestinationList);
            _destinations.RefreshDestinations(_customDestinationList);

            switch (EnabledAutoDestinationType)
            {
            case ApplicationDestinationType.Frequent:
                _customDestinationList.AppendKnownCategory(KNOWNDESTCATEGORY.KDC_FREQUENT);
                break;

            case ApplicationDestinationType.Recent:
                _customDestinationList.AppendKnownCategory(KNOWNDESTCATEGORY.KDC_RECENT);
                break;
            }

            CommitList();
            return(true);
        }
Example #5
0
        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);
            }
        }
Example #6
0
        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));
                }
            }
        }