public void On_FileDrop_format_and_one_file_with_directory_has_text_with_files_count_and_filename_and_folder() { // Arrange var file = "c:\\file1.ext"; var folder = "c:\\folder"; var data = new DataObject(DataFormats.FileDrop, new string[] { file, folder }); var viewModel = new BuferViewModel { Clip = data }; var fileStorage = new Fake <IFileStorage>(); fileStorage.CallsTo(s => s.GetFileAttributes(file)).Returns(FileAttributes.Normal); fileStorage.CallsTo(s => s.GetFileDirectory(file)).Returns("c:\\"); fileStorage.CallsTo(s => s.GetFileName(file)).Returns("file1.ext"); fileStorage.CallsTo(s => s.GetFileName(folder)).Returns("folder"); fileStorage.CallsTo(s => s.GetFileAttributes(folder)).Returns(FileAttributes.Directory); var sut = new DataObjectHandler( A.Fake <IClipboardBuferService>(), A.Fake <IProgramSettingsGetter>(), fileStorage.FakedObject); // Act sut.TryHandleDataObject(viewModel); // Assert Assert.AreEqual("c:\\" + Environment.NewLine + Environment.NewLine + "file1.ext" + Environment.NewLine + "folder\\", viewModel.Representation); }
public void On_Not_Empty_Unicode_Format_Handles_TextData_And_TextRepresentation() { // Arrange var originText = " Unicode"; var data = new DataObject(DataFormats.UnicodeText, originText); data.SetData(DataFormats.Text, " Text"); data.SetData(DataFormats.StringFormat, " String"); var viewModel = new BuferViewModel { Clip = data }; var sut = new DataObjectHandler( A.Fake <IClipboardBuferService>(), A.Fake <IProgramSettingsGetter>(), A.Fake <IFileStorage>()); // Act sut.TryHandleDataObject(viewModel); // Assert Assert.AreEqual(originText, viewModel.TextData); Assert.AreEqual(originText, viewModel.TextRepresentation); }
public void AddTemporaryClip(BuferViewModel bufer) { using (UndoableContext <ApplicationStateSnapshot> .Current.StartAction(Resource.BuferAdded)) { this._tempObjects.Add(bufer); } }
public void ProcessBuferItems(IEnumerable <BuferItem> bufers) { using (var action = UndoableContext <ApplicationStateSnapshot> .Current.StartAction(Resource.BufersLoaded)) { var loaded = false; foreach (var bufer in bufers) { var dataObject = this._buferItemDataObjectConverter.ToDataObject(bufer); var buferViewModel = new BuferViewModel { Clip = dataObject, Alias = bufer.Alias, CreatedAt = this._time.LocalTime, Pinned = bufer.Pinned }; var tempLoaded = this._dataObjectHandler.TryHandleDataObject(buferViewModel); loaded = tempLoaded || loaded; } if (!loaded) { action.Cancel(); } } }
public bool IsInTemporaryBufers(BuferViewModel bufer, out Guid viewId) { var tempBufer = this._tempObjects.FirstOrDefault(b => string.Equals(b.Alias, bufer.Alias, StringComparison.CurrentCulture) && this._comparer.Equals(bufer.Clip, b.Clip)); viewId = tempBufer != null ? tempBufer.ViewId : Guid.Empty; return(tempBufer != null); }
public bool IsLastTemporaryBufer(BuferViewModel bufer) { var lastTemporaryBufer = this.LastTemporaryBufer; return(lastTemporaryBufer != null && string.Equals(lastTemporaryBufer.Alias, bufer.Alias, StringComparison.CurrentCulture) && this._comparer.Equals(lastTemporaryBufer?.Clip, bufer.Clip)); }
private void _RemoveClipWithoutTrackingInUndoableContext(BuferViewModel bufer) { ApplicationStateSnapshot stateWithoutEmptyBufer; using (var action = UndoableContext <ApplicationStateSnapshot> .Current.StartAction()) { this._clipboardBuferService.RemoveBufer(bufer.ViewId); stateWithoutEmptyBufer = this._clipboardBuferService.UndoableState; action.Cancel(); } this._clipboardBuferService.UndoableState = stateWithoutEmptyBufer; }
public static BuferItem ToModel(this BuferViewModel buferViewModel) { var buferItem = new BuferItem() { Pinned = buferViewModel.Pinned, Alias = buferViewModel.Alias, Formats = buferViewModel.Clip .GetFormats() .ToDictionary( f => f, f => buferViewModel.Clip.GetData(f)) }; return(buferItem); }
public bool IsInPinnedBufers(BuferViewModel bufer, out Guid pinnedBuferViewId) { var pinnedBufer = this._pinnedObjects.FirstOrDefault(d => this._comparer.Equals(bufer.Clip, d.Clip) && string.Equals(bufer.Alias, d.Alias, StringComparison.CurrentCulture)); if (pinnedBufer != null) { pinnedBuferViewId = pinnedBufer.ViewId; return(true); } else { pinnedBuferViewId = Guid.Empty; return(false); } }
public void On_CUSTOM_IMAGE_FORMAT_button_has_image_bufer_in_text() { // Arrange var data = new DataObject(ClipboardFormats.CUSTOM_IMAGE_FORMAT, new object()); var viewModel = new BuferViewModel { Clip = data }; var sut = new DataObjectHandler( A.Fake <IClipboardBuferService>(), A.Fake <IProgramSettingsGetter>(), A.Fake <IFileStorage>()); // Act sut.TryHandleDataObject(viewModel); // Assert Assert.IsFalse(viewModel.IsChangeTextAvailable); Assert.AreEqual(Resource.ImageBufer, viewModel.Representation); Assert.AreEqual(Resource.ImageBufer, viewModel.TextRepresentation); }
public BuferFocusedEventArgs(BuferViewModel bufer) { this.Bufer = bufer; }
private void _ProcessCopyClipboardEvent(object sender, EventArgs e) { var currentTime = this._time.LocalTime; if (this._IsDuplicatedEvent(currentTime)) { return; } else { this._lastClipboardEventDateTime = currentTime; } try { var dataObject = this._clipboardWrapper.GetDataObject(); var copy = new DataObject(); foreach (var format in dataObject.GetFormats()) { if (format == "EnhancedMetafile")//Fixes bug with copy in Word { copy.SetData(format, "<< !!! EnhancedMetafile !!! >>"); } else { try { copy.SetData(format, dataObject.GetData(format)); } catch { // TODO (m) Log input parameters and other details. } } } if (this._clipboardWrapper.ContainsImage()) { copy.SetData(ClipboardFormats.CUSTOM_IMAGE_FORMAT, this._clipboardWrapper.GetImage()); } var buferViewModel = new BuferViewModel { Clip = copy, CreatedAt = this._time.LocalTime }; if (this.ShouldCatchCopies) { this._dataObjectHandler.TryHandleDataObject(buferViewModel); } else { using (var action = UndoableContext <ApplicationStateSnapshot> .Current.StartAction()) { this._dataObjectHandler.TryHandleDataObject(buferViewModel); action.Cancel(); }// TODO (m) Should be refactored } this.Host.SetStatusBarText(Resource.LastClipboardUpdate + currentTime.ToShortTimeString()); // TODO (m) Should be in separate strip label } catch (ExternalException exc) { Logger.WriteError("An error during get clipboard operation", exc); throw new ClipboardMessageException("An error occurred. See logs for more details.", exc); } }
// For unit tests: // not persistent: last temp ? <nothing> : // any temp ? <swap> : // pinned ? <nothing> : // (not exist) - <add temp> // persistent: pinned ? <nothing> : // any temp ? <remove from temp and add pinned> : // (not exist) <add pinned> public bool TryHandleDataObject(BuferViewModel buferViewModel) { buferViewModel.IsChangeTextAvailable = true; buferViewModel.TextData = this._GetNotEmptyStringData(buferViewModel.Clip, DataFormats.UnicodeText, DataFormats.StringFormat, DataFormats.Text); buferViewModel.TextRepresentation = buferViewModel.TextData; if (buferViewModel.Clip.GetData(DataFormats.StringFormat) as string == string.Empty) { // TODO (m) maybe set System.String data ? Can be implemented via setting. Such bufers can be marked and maybe suggest to user paste it as a usual text (Ctrl + A) } var files = buferViewModel.Clip.GetData(DataFormats.FileDrop) as string[]; if (files != null && files.Length > 0) { buferViewModel.IsChangeTextAvailable = false; var firstFile = files.First(); var onlyFolders = files.Select(f => this._fileStorage.GetFileAttributes(f).HasFlag(FileAttributes.Directory)) .All(f => f); if (files.Length == 1) { if (firstFile.Length < this._settings.MaxFilePathLengthForBuferTitle) { buferViewModel.TooltipTitle = $"<< {(onlyFolders ? Resource.FolderBufer : Resource.FileBufer)} >>"; } } var folder = this._fileStorage.GetFileDirectory(firstFile); buferViewModel.TextRepresentation += folder + Environment.NewLine + Environment.NewLine; buferViewModel.TextRepresentation += string.Join(Environment.NewLine, files.Select(f => this._fileStorage.GetFileName(f) + (this._fileStorage.GetFileAttributes(f).HasFlag(FileAttributes.Directory) ? Path.DirectorySeparatorChar.ToString() : string.Empty)).ToList()); } else { if (buferViewModel.Clip.GetFormats().Contains(ClipboardFormats.CUSTOM_IMAGE_FORMAT)) { buferViewModel.IsChangeTextAvailable = false; buferViewModel.TextRepresentation = Resource.ImageBufer; } else { if (buferViewModel.Clip.GetFormats().Contains(ClipboardFormats.FILE_CONTENTS_FORMAT)) { buferViewModel.IsChangeTextAvailable = false; buferViewModel.TextRepresentation = Resource.FileContentsBufer; } } } buferViewModel.Representation = buferViewModel.TextRepresentation;// TODO (m) Maybe store original presentation as well ? var isLastTempBufer = this._clipboardBuferService.IsLastTemporaryBufer(buferViewModel); if (!buferViewModel.Pinned && isLastTempBufer) // Repeated Ctrl + C operation { buferViewModel.ViewId = this._clipboardBuferService.LastTemporaryBufer.ViewId; this.Updated?.Invoke(this, new ClipboardUpdatedEventArgs(buferViewModel)); return(false); } if (this._clipboardBuferService.IsInPinnedBufers(buferViewModel, out Guid pinnedBuferViewId)) { buferViewModel.ViewId = pinnedBuferViewId; this.Updated?.Invoke(this, new ClipboardUpdatedEventArgs(buferViewModel)); return(false); } var alreadyInTempBufers = this._clipboardBuferService.IsInTemporaryBufers(buferViewModel, out Guid viewId); if (!alreadyInTempBufers && this._clipboardBuferService.GetPinnedBufers().Count() == this._settings.MaxBufersCount) { // Maybe we should not do any check if persistent clips count = max bufers count // Maybe all visible bufers can not be persistent (create a limit of persistent bufers)? this.Full?.Invoke(this, EventArgs.Empty); this.Updated?.Invoke(this, new ClipboardUpdatedEventArgs(buferViewModel)); return(false); } using (UndoableContext <ApplicationStateSnapshot> .Current.StartAction()) { if (alreadyInTempBufers) { this._clipboardBuferService.RemoveBufer(viewId); } else if (this._clipboardBuferService.BufersCount == this._settings.MaxBufersCount + this._settings.ExtraBufersCount) { this._clipboardBuferService.RemoveBufer(this._clipboardBuferService.FirstTemporaryBufer.ViewId); } buferViewModel.ViewId = Guid.NewGuid(); this._clipboardBuferService.AddTemporaryClip(buferViewModel); if (buferViewModel.Pinned) { this._clipboardBuferService.TryPinBufer(buferViewModel.ViewId); } } this.Updated?.Invoke(this, new ClipboardUpdatedEventArgs(buferViewModel)); return(true); }
public ClipboardUpdatedEventArgs(BuferViewModel bufer) { this.ViewModel = bufer; }