Exemple #1
0
 public void CopyItem(ItemVM copiedItemVM)
 {
     this.copiedItemVM = copiedItemVM;
     cutCopiedItemVM   = false;
     // Save to settings
     // No need to save here as there is no change to the data contex
 }
Exemple #2
0
        public void NewGroup(ObservableCollection <ItemVM> itemVMs)
        {
            // Create a new group
            ItemVM itemVM = ContextHelper.NewGroup();

            itemVMs.Add(itemVM);
            SaveData.SaveShortcuts(false);
        }
Exemple #3
0
        public void DeleteItem(ItemVM itemVMToDelete)
        {
            bool stop = false;

            foreach (WindowItemVM windowItemVM in WindowItemVMs)
            {
                if (windowItemVM.RootItemVMs.Contains(itemVMToDelete))
                {
                    // Delete physical file/s first
                    ShortcutUtils.DeleteShortcuts(itemVMToDelete);
                    // Delete the item
                    windowItemVM.RootItemVMs.Remove(itemVMToDelete);
                    stop = true;
                    break;
                }

                foreach (ItemVM rootItemVM in windowItemVM.RootItemVMs)
                {
                    IEnumerable <ItemVM> itemVMs = rootItemVM.FlattenItemVMs();
                    foreach (ItemVM itemVM in itemVMs)
                    {
                        if (itemVM.IsGroup)
                        {
                            if (itemVM.ItemVMs.Contains(itemVMToDelete))
                            {
                                // Delete physical file/s first
                                ShortcutUtils.DeleteShortcuts(itemVMToDelete);
                                // Delete the item
                                itemVM.ItemVMs.Remove(itemVMToDelete);
                                //
                                stop = true;
                                break;
                            }
                        }

                        if (stop)
                        {
                            break;
                        }
                    }

                    if (stop)
                    {
                        break;
                    }
                }

                if (stop)
                {
                    break;
                }
            }
            SaveData.SaveShortcuts(false);
        }
Exemple #4
0
 public void PasteItem(ObservableCollection <ItemVM> itemVMs)
 {
     if (copiedItemVM != null)
     {
         // Patste the copiedItemVM
         ItemVM clonedItemVM = ContextHelper.DeepCloneItemVM(copiedItemVM);
         itemVMs.Add(clonedItemVM);
         if (cutCopiedItemVM)
         {
             // Delete copiedItemVM
             DeleteItem(copiedItemVM);
             // Save to settings
             // No need to save here as DeleteItem has its own save statement
         }
         else
         {
             SaveData.SaveShortcuts(false);
         }
     }
 }
Exemple #5
0
        public void Drop(IDropInfo dropInfo)
        {
            // There is nothing we can do here.
            if (dropInfo == null)
            {
                return;
            }
            // Data is populated from external and internal drag & drop. If it is null there is nothing we can do here.
            if (dropInfo.Data == null)
            {
                return;
            }
            // Check if target is not group, if yes, then there is nothing we can do here.
            bool targetIsTreeViewItem = dropInfo.InsertPosition.HasFlag(RelativeInsertPosition.TargetItemCenter) && dropInfo.VisualTargetItem is TreeViewItem;

            if (targetIsTreeViewItem)
            {
                TreeViewItem treeViewItem = (TreeViewItem)dropInfo.VisualTargetItem;
                ItemVM       itemVM       = (ItemVM)treeViewItem.DataContext;
                if (!itemVM.IsGroup)
                {
                    return;
                }
            }

            // Target is group proced with the other checks...

            var insertIndex  = dropInfo.InsertIndex != dropInfo.UnfilteredInsertIndex ? dropInfo.UnfilteredInsertIndex : dropInfo.InsertIndex;
            var itemsControl = dropInfo.VisualTarget as ItemsControl;

            if (itemsControl != null)
            {
                var editableItems = itemsControl.Items as IEditableCollectionView;
                if (editableItems != null)
                {
                    var newItemPlaceholderPosition = editableItems.NewItemPlaceholderPosition;
                    if (newItemPlaceholderPosition == NewItemPlaceholderPosition.AtBeginning && insertIndex == 0)
                    {
                        ++insertIndex;
                    }
                    else if (newItemPlaceholderPosition == NewItemPlaceholderPosition.AtEnd && insertIndex == itemsControl.Items.Count)
                    {
                        --insertIndex;
                    }
                }
            }

            var destinationList = dropInfo.TargetCollection.TryGetList();
            var data            = ExtractData(dropInfo.Data).OfType <object>().ToList();

            // If this is a move for sure it is from the internal Drag & Drop, so delete the source item.
            DragDropEffects dragDropEffects = GetDragDropEffects(dropInfo);

            if (dragDropEffects == DragDropEffects.Move)
            {
                if (dropInfo.DragInfo != null)
                {
                    // This is a drag & drop item from treeview its self
                    var sourceList = dropInfo.DragInfo.SourceCollection.TryGetList();
                    if (sourceList != null)
                    {
                        foreach (var o in data)
                        {
                            var index = sourceList.IndexOf(o);
                            if (index != -1)
                            {
                                sourceList.RemoveAt(index);
                                // so, is the source list the destination list too ?
                                if (destinationList != null && Equals(sourceList, destinationList) && index < insertIndex)
                                {
                                    --insertIndex;
                                }
                            }
                        }
                    }
                }
            }

            // Add item
            if (destinationList != null)
            {
                // check for cloning
                var cloneData = dropInfo.Effects.HasFlag(DragDropEffects.Copy) || dropInfo.Effects.HasFlag(DragDropEffects.Link);
                foreach (var o in data)
                {
                    // ----------------------------------------------
                    // This is a Drag & Drop from the external source
                    // ----------------------------------------------
                    if (dropInfo.Data is DataObject)
                    {
                        DataObject dataObject = o as DataObject;
                        if (dataObject != null)
                        {
                            // ******************************************************************************************************************************************
                            // FileDrop. Queries a data object for the presence of data in the System.Windows.DataFormats.FileDrop data format.
                            // ******************************************************************************************************************************************
                            if (dataObject.ContainsFileDropList() && dataObject.GetDataPresent(DataFormats.FileDrop, true))
                            {
                                // We can have more than one file dropped.
                                var droppedFilePaths = dataObject.GetFileDropList();
                                foreach (string droppedFilePath in droppedFilePaths)
                                {
                                    string        shortcutsDirectoryPath = Helper.GetShortcutsPath();
                                    string        guid          = NewData.NewGuid();
                                    string        name          = Path.GetFileName(droppedFilePath);
                                    DirectoryInfo directoryInfo = new DirectoryInfo(droppedFilePath);
                                    if (directoryInfo.Parent == null)
                                    {
                                        // This is the root. In that case name is empty so we need to set the name differently.
                                        DriveInfo driveInfo = new DriveInfo(droppedFilePath);
                                        // Something like this: Local Disk (C:)
                                        string volumeLabel = driveInfo.VolumeLabel;
                                        if (string.IsNullOrEmpty(volumeLabel))
                                        {
                                            volumeLabel = "Drive";
                                        }
                                        name = $"{volumeLabel} ({driveInfo.Name.Replace(@"\", "")})";
                                    }

                                    ItemVM obj2Insert = null;

                                    // Check if the dropped file is a .lnk file. We just copy the .lnk files to the sortcuts folder and use them from there.
                                    if (Path.GetExtension(droppedFilePath).Equals(Constants.EXT_LNK, StringComparison.InvariantCultureIgnoreCase))
                                    {
                                        // This is a .lnk file. In this case we copy this file to our shortcuts
                                        string shortcutLnkFilePath = Path.Combine(shortcutsDirectoryPath, guid + Constants.EXT_LNK);
                                        File.Copy(droppedFilePath, shortcutLnkFilePath);
                                        obj2Insert = new ItemVM(guid, false, false, name, shortcutLnkFilePath, Constants.EXT_LNK);
                                    }
                                    // Check if the dropped file is a .url file. We just copy the .url files to the sortcuts folder and use them from there.
                                    else if (Path.GetExtension(droppedFilePath).Equals(Constants.EXT_URL, StringComparison.InvariantCultureIgnoreCase))
                                    {
                                        // This is a .url file. In this case we copy this file to our shortcuts
                                        string shortcutUrlFilePath = Path.Combine(shortcutsDirectoryPath, guid + Constants.EXT_URL);
                                        File.Copy(droppedFilePath, shortcutUrlFilePath);
                                        obj2Insert = new ItemVM(guid, false, false, name, shortcutUrlFilePath, Constants.EXT_URL);
                                    }
                                    else
                                    {
                                        obj2Insert = new ItemVM(guid, false, false, name, droppedFilePath, Constants.EXT_LNK);
                                    }

                                    destinationList.Insert(insertIndex++, obj2Insert);
                                    SaveShortcutSettings();
                                }
                            }
                            // ******************************************************************************************************************************************
                            // AbsoluteUri. Queries a data object for the presence of data in the System.Windows.DataFormats.UnicodeText format.
                            // ******************************************************************************************************************************************
                            else if (dataObject.ContainsText() && dataObject.GetDataPresent(DataFormats.Text, true))
                            {
                                string dataText = (string)dataObject.GetData(DataFormats.Text, true);
                                Uri    uri;
                                bool   isAbsoluteUri = Uri.TryCreate(dataText, UriKind.Absolute, out uri);
                                if (isAbsoluteUri)
                                {
                                    ItemVM obj2Insert = new ItemVM(NewData.NewGuid(), false, false, uri.AbsoluteUri, uri.AbsoluteUri, Constants.EXT_URL);
                                    destinationList.Insert(insertIndex++, obj2Insert);
                                    // Save shortcut settings.
                                    SaveShortcutSettings();
                                }
                            }
                            else
                            {
                                // ******************************************************************************************************************************************
                                // ShellIDListArray. Perhaps data contain virtual files
                                // ******************************************************************************************************************************************
                                // Returns a list of formats in which the data in this data object is stored, or can be converted to.
                                string[] formats = dataObject.GetFormats();
                                foreach (string format in formats)
                                {
                                    if (format == "Shell IDList Array")
                                    {
                                        // Virtual file/s been draged.
                                        string message = "To create this kind of shortcut please do:\n";
                                        message += "1. Drag to create a desktop shortcut first,\n";
                                        message += "2. Drag the shortcut from desktop to MDSHO,\n";
                                        message += "3. Delete the shortcut from desktop if you want.";
                                        Window parent = Helper.GetWindowFromWindowItemVM(this);
                                        MessageBox.Show(parent, message, "Information", MessageBoxButton.OK, MessageBoxImage.Information);
                                    }
                                }
                            }
                        }
                    }
                    // ---------------------------------------------------------------------------
                    // This is a Drag & Drop from the internal source. Item from treeview its self
                    // ---------------------------------------------------------------------------
                    else
                    {
                        var obj2Insert = o;
                        if (cloneData)
                        {
                            var cloneable = o as ICloneable;
                            if (cloneable != null)
                            {
                                obj2Insert = cloneable.Clone();
                            }
                        }
                        destinationList.Insert(insertIndex++, obj2Insert);
                        // Save shortcut settings.
                        SaveShortcutSettings();
                    }
                }
            }
        }
Exemple #6
0
        public static bool CanAcceptData(IDropInfo dropInfo)
        {
            // There is nothing we can do here.
            if (dropInfo == null)
            {
                return(false);
            }
            // Data is populated from external and internal drag & drop. If it is null there is nothing we can do here.
            if (dropInfo.Data == null)
            {
                return(false);
            }
            // Check if target is not group, then there is nothing we can do here.
            bool targetIsTreeViewItem = dropInfo.InsertPosition.HasFlag(RelativeInsertPosition.TargetItemCenter) && dropInfo.VisualTargetItem is TreeViewItem;

            if (targetIsTreeViewItem)
            {
                TreeViewItem treeViewItem = (TreeViewItem)dropInfo.VisualTargetItem;
                ItemVM       itemVM       = (ItemVM)treeViewItem.DataContext;
                if (!itemVM.IsGroup)
                {
                    return(false);
                }
            }

            // Target is group proced with the other checks...

            // ------------------------------------------------------------------
            // Check if Drag & Drop is from external source
            // ------------------------------------------------------------------
            if (dropInfo.Data is DataObject)
            {
                return(true);
            }

            // ------------------------------------------------------------------
            // Drag & Drop is from internal source
            // ------------------------------------------------------------------
            // Prevent target is in the same context as the source
            if (!dropInfo.IsSameDragDropContextAsSource)
            {
                return(false);
            }

            // do not drop on itself
            if (targetIsTreeViewItem && dropInfo.VisualTargetItem == dropInfo.DragInfo.VisualSourceItem)
            {
                return(false);
            }

            //
            if (dropInfo.DragInfo.SourceCollection == dropInfo.TargetCollection)
            {
                var targetList = dropInfo.TargetCollection.TryGetList();
                return(targetList != null);
            }
            //else if (dropInfo.DragInfo.SourceCollection is ItemCollection)
            //{
            //    return false;
            //}
            else if (dropInfo.TargetCollection == null)
            {
                return(false);
            }
            else
            {
                if (TestCompatibleTypes(dropInfo.TargetCollection, dropInfo.Data))
                {
                    var isChildOf = IsChildOf(dropInfo.VisualTargetItem, dropInfo.DragInfo.VisualSourceItem);
                    return(!isChildOf);
                }
                else
                {
                    return(false);
                }
            }
        }