private void RestoreFavorites(FavoritesFolder favoritesFolder, IEnumerable <Folder> folders, IEnumerable <Link> links) { foreach (var link in links) { if (link.MessageId != 0) { _favManager.AddMessageLink( link.MessageId, link.Comment, favoritesFolder); } else { _favManager.AddUrlLink( link.Url, link.Comment, favoritesFolder); } } foreach (var folder in folders) { _favManager.AddFolder(folder.Name, folder.Comment, favoritesFolder); var parentFolder = // ReSharper disable AccessToModifiedClosure favoritesFolder.SubFolders.FirstOrDefault(f => f.Name == folder.Name); // ReSharper restore AccessToModifiedClosure if (parentFolder != null) { RestoreFavorites(parentFolder, folder.Folders, folder.Links); } } }
private void GridDragDrop(object sender, DragEventArgs e) { var effect = DragDropEffects.None; if (sender == _grid) { int dummy; var point = _grid.PointToClient(new Point(e.X, e.Y)); ITreeNode activeNode; _grid.GetNodeAndCellIndexAt(point.X, point.Y, out activeNode, out dummy); if (activeNode == null) { activeNode = _favManager.RootFolder; } var activeEntry = activeNode as IFavoritesEntry; if (activeEntry != null) { var activeFolder = activeEntry.IsContainer ? (FavoritesFolder)activeEntry : (FavoritesFolder)activeEntry.Parent; if (e.Data.GetDataPresent(typeof(TreeNodeBag))) { var dropNodes = (TreeNodeBag)e.Data.GetData(typeof(TreeNodeBag)); if (dropNodes != null && dropNodes.Count > 0) { var select = LastActiveNode ?? _grid.ActiveNode; if (dropNodes.IsAssignableFrom(typeof(IFavoritesEntry))) { var copy = DragDropHelper.GetDropEffect(e.KeyState, true) == DragDropEffects.Copy; var failedItems = new List <IFavoritesEntry>(); ProgressWorker.Run(_serviceManager, false, pi => { pi.SetProgressText(SR.Favorites.MovingItems); // При одновременном перемещении контейнера и его элементов, // элементы оказываются не в "своём" контейнере, а в том, где отпустили мышь. // Так и задумано foreach (IFavoritesEntry entry in dropNodes) { if (copy) { Debug.Assert(entry is FavoritesLink, "entry is FavoritesLink"); var link = (FavoritesLink)entry; if (!_favManager.AddMessageLink(link.MessageId, link.Comment, activeFolder)) { failedItems.Add(entry); } } else if (!entry.Move(activeFolder)) { failedItems.Add(entry); } } }, () => { _favManager.Reload(); if (failedItems.Count > 0) { var failedMessages = new StringBuilder(); failedMessages.Append(Environment.NewLine); foreach (var entry in failedItems) { var itemText = string.IsNullOrEmpty(entry.Comment) ? (entry is FavoritesLink ? ((FavoritesLink)entry).Link : ((FavoritesFolder)entry).Name) : entry.Comment; failedMessages.AppendFormat(" \"{0}\"{1}", itemText, Environment.NewLine); } var message = String.Format(SR.Favorites.ItemsExists, failedMessages, activeFolder.Name); MessageBox.Show(this, message, FavoritesFeature.Instance.Info, MessageBoxButtons.OK, MessageBoxIcon.Warning); } effect = (dropNodes.Count > failedItems.Count) ? (copy ? DragDropEffects.Copy : DragDropEffects.Move) : DragDropEffects.None; }); } else if (dropNodes.IsAssignableFrom(typeof(IMsg))) { var failedItems = dropNodes .Cast <IMsg>() .Where( message => !_favManager.AddMessageLink(message.ID, message.Subject, activeFolder)) .ToList(); if (failedItems.Count > 0) { var failedMessages = new StringBuilder(); failedMessages.Append(Environment.NewLine); foreach (var message in failedItems) { failedMessages.AppendFormat("{0} - \"{1}\"{2}", message.ID, message.Subject, Environment.NewLine); } var messageText = String.Format(SR.Favorites.ItemsExists, failedMessages, activeFolder.Name); MessageBox.Show(this, messageText, FavoritesFeature.Instance.Info, MessageBoxButtons.OK, MessageBoxIcon.Warning); } //if effect = (dropNodes.Count > failedItems.Count) ? DragDropEffects.Link : DragDropEffects.None; } //if if (select != null) { _grid.EnsureVisible(select, false); if (_grid.ActiveNode != select) { _grid.ActiveNode = select; } } //if } //if } //if } //if } //if e.Effect = effect; }