public static void Insert(string key, ClipboardItem value, int index) { LocalClipboard.MainWindow.ListBox.Items.Insert(index, key); LocalClipboard.Keys.Insert(index, key); LocalClipboard.Dict.Add(key, value); // create or overwrite CLIPBOARD file, then write all items' FileChars in the appropriate order using (StreamWriter sw = LocalClipboard.ClipboardFile.CreateText()) { // write items prior to index to CLIPBOARD file for (int i = LocalClipboard.Keys.Count - 1; i > index; i--) { sw.Write(LocalClipboard.Dict[LocalClipboard.Keys[i]].FileChars); } // write current item's FileChars to CLIPBOARD file sw.Write(LocalClipboard.Dict[LocalClipboard.Keys[index]].FileChars); // write items following index to CLIPBOARD file for (int i = index - 1; i >= 0; i--) { sw.Write(LocalClipboard.Dict[LocalClipboard.Keys[i]].FileChars); } } }
public static void AddWithFile(string key, ClipboardItem value) { // if CLIPBOARD file is missing or empty if (!LocalClipboard.ClipboardFile.Exists || LocalClipboard.ClipboardFile.Length == 0) { // create new file and prepare to write using (StreamWriter sw = LocalClipboard.ClipboardFile.CreateText()) { // write each item's FileChars to the CLIPBOARD file for (int i = LocalClipboard.Keys.Count - 1; i >= 0; i--) { sw.Write(LocalClipboard.Dict[LocalClipboard.Keys[i]].FileChars); } } } // append the item's FileChars to the CLIPBOARD file using (StreamWriter sw = LocalClipboard.ClipboardFile.AppendText()) { sw.Write(value.FileChars); } // add to data structures in the local clipboard LocalClipboard.Add(key, value); }
public static void Remove(int index) { // return if index is invalid if (index < 0) { return; } // remove the item at the index string key = (string)MainWindow.ListBox.Items[index]; ClipboardItem clipboardItem = LocalClipboard.Dict[key]; LocalClipboard.Remove(clipboardItem.KeyText, clipboardItem); // if there was an item located after the removed item, select that item if (LocalClipboard.MainWindow.ListBox.Items.Count > index) { LocalClipboard.MainWindow.ListBox.SelectedIndex = index; } // else select the item located before the removed item else { LocalClipboard.MainWindow.ListBox.SelectedIndex = index - 1; } // notify the user of the successful operation for 3 seconds MsgLabel.Normal("Item removed!"); }
public static void Add(string key, ClipboardItem value) { try { // add to the back-end dictionary LocalClipboard.Dict.Add(key, value); } // exception thrown on attempt to add a duplicate key to the dict catch (ArgumentException) { // store item with the same key ClipboardItem duplicateKeyItem = LocalClipboard.Dict[key]; // remove old item and prepare to replace with new item LocalClipboard.Remove(duplicateKeyItem.KeyText, duplicateKeyItem); // add new item LocalClipboard.Dict.Add(key, value); } // add to back-end string collection of keys LocalClipboard.Keys.Insert(0, key); // add to visual clipboard last LocalClipboard.MainWindow.ListBox.Items.Insert(0, key); }
protected bool SetKeyDiff() { // calculate KeyDiff so that we can differentiate the items with duplicate key text for (string key = this.KeyText; LocalClipboard.Dict.ContainsKey(key); key = this.KeyText + this.KeyDiff) { // store item with the same key ClipboardItem duplicateKeyItem = LocalClipboard.Dict[key]; // if keys are equivalent but the items are not, increment KeyDiff and continue loop if (!this.IsEquivalent(duplicateKeyItem)) { this.KeyDiff++; continue; } // if old item is at the target index, then there is no need to add this item if (LocalClipboard.Keys.IndexOf(key) == 0) { return(false); } // if this point is reached, remove the old item and break from the loop LocalClipboard.Remove(duplicateKeyItem.KeyText, duplicateKeyItem); break; } return(true); }
/// <summary> /// All child classes of ClipboardItem must implement IsEquivalent. /// /// This method determines if the param is functionally equivalent to /// the current ClipboardItem instance. /// </summary> /// <param name="duplicateKeyItem">ClipboardItem with the same key as this instance</param> /// <returns>whether or not the ClipboardItems are equivalent</returns> protected override bool IsEquivalent(ClipboardItem duplicateKeyItem) { // check for valid type if (duplicateKeyItem.Type != TypeEnum.FileDropList) { return(false); } // check for equivalent item count if (this.FileDropList.Count != (duplicateKeyItem as FileItem).FileDropList.Count) { return(false); } // check equivalency of each filename for (int i = 0; i < this.FileDropList.Count; i++) { if (this.FileDropList[i] != (duplicateKeyItem as FileItem).FileDropList[i]) { return(false); } } // test passed if this point is reached return(true); }
public static void Remove(string key, ClipboardItem value) { // first, remove the item's FileChars from the CLIPBOARD file LocalClipboard.RemoveFromFile(value.FileChars); // remove item by its key LocalClipboard.MainWindow.ListBox.Items.Remove(key); LocalClipboard.Keys.Remove(key); LocalClipboard.Dict.Remove(key); // determine if we need to delete any files along with the item DirectoryInfo folder = null; if (value is ImageItem) { folder = LocalClipboard.ImageFolder; } else if (value is AudioItem) { folder = LocalClipboard.AudioFolder; } else if (value is CustomItem) { folder = LocalClipboard.CustomFolder; } // nothing to do if folder is null or missing if (folder == null || !folder.Exists) { return; } // delete image/audio/custom file if it exists FileInfo fileToDelete = new FileInfo(Path.Combine(folder.FullName, key)); if (fileToDelete.Exists) { fileToDelete.Delete(); } // delete the folder if it's empty, i.e. contains 0 files FileInfo[] fileInfos = folder.GetFiles(); if (fileInfos.Length == 0) { folder.Delete(); } }
/// <summary> /// All child classes of ClipboardItem must implement IsEquivalent. /// /// This method determines if the param is functionally equivalent to /// the current ClipboardItem instance. /// </summary> /// <param name="duplicateKeyItem">ClipboardItem with the same key as this instance</param> /// <returns>whether or not the ClipboardItems are equivalent</returns> protected override bool IsEquivalent(ClipboardItem duplicateKeyItem) { // check for valid type if (duplicateKeyItem.Type != TypeEnum.Image) { return(false); } // check for equivalent image dimensions if (!this.Size.Equals((duplicateKeyItem as ImageItem).Size)) { return(false); } // TODO: finish algorithm to accurately compare images return(true); }
private void OnArrowKeyDown(KeyEventArgs e) { // vars regarding listbox data int total = this.listBox.Items.Count; int current = this.listBox.SelectedIndex; // base case 1: nothing in the clipboard if (total == 0) { return; } // base case 2: no item is selected, or only one item exists if (current < 0 || total == 1) { this.listBox.SelectedIndex = 0; return; } // store the item at current index ClipboardItem clipboardItem = LocalClipboard.Dict[(string)listBox.Items[current]]; // store new index for the item and/or index to be moved int newIndex; // base case 3: current is at the last index if (current == total - 1) { // if box isn't checked, no wrapping or moving is needed if (!this.wrapKeysItem.Checked) { return; } // we will be wrapping to the top, so newIndex is set to the top index, i.e. 0 newIndex = 0; // move current item to the top index if shift is pressed if (e.Shift) { LocalClipboard.Move(clipboardItem.KeyText, clipboardItem, newIndex); // wrap selected index to the top index only if the box is checked if (this.ChangeTopBottom.Checked) { this.listBox.SelectedIndex = newIndex; } else { this.listBox.SelectedIndex = current; } } // else don't move the current item, and wrap the selected index unconditionally else { this.listBox.SelectedIndex = newIndex; } return; } // store whether the selected index should be changed bool changeIndex = true; // if ctrl is being pressed, set newIndex to the bottom index if (e.Control) { newIndex = total - 1; // selected index should be unchanged if we're moving an item // to the top/bottom, but the box is unchecked if (e.Shift && !this.ChangeTopBottom.Checked) { changeIndex = false; } } // else set newIndex to current + 1 else { newIndex = current + 1; // changeIndex is unconditionally true if ctrl isn't being pressed } // move item to new index if shift is pressed if (e.Shift) { LocalClipboard.Move(clipboardItem.KeyText, clipboardItem, newIndex); } // move selected index to new index if bool is satisfied if (changeIndex) { this.listBox.SelectedIndex = newIndex; } else { this.listBox.SelectedIndex = current; } }
public static void Copy() { // check for valid SelectedIndex val before continuing if (LocalClipboard.MainWindow.ListBox.SelectedIndex < 0) { return; } // Windows clipboard will be changed in this method; we don't want it to be handled LocalClipboard.HandleClipboard = false; // store the selected item string key = (string)MainWindow.ListBox.SelectedItem; ClipboardItem clipboardItem = LocalClipboard.Dict[key]; // store an error msg string that will notify the user if an error occurred string errMsg = null; // what's copied to the clipboard depends on the item's type switch (clipboardItem.Type) { case ClipboardItem.TypeEnum.Text: // copy the item's text to the Windows clipboard Clipboard.SetText((clipboardItem as TextItem).Text); break; case ClipboardItem.TypeEnum.FileDropList: // copy the item's files to the Windows clipboard Clipboard.SetFileDropList((clipboardItem as FileItem).FileDropList); break; case ClipboardItem.TypeEnum.Image: // make sure the image file exists FileInfo imageFile = new FileInfo(Path.Combine(LocalClipboard.ImageFolder.FullName, clipboardItem.KeyText)); if (imageFile.Exists) { // get image from the file using (Image image = Image.FromFile(imageFile.FullName)) { // set image to the Windows clipboard Clipboard.SetImage(image); } } // else set errMsg to be reported else { errMsg = "Image file is missing!"; } break; case ClipboardItem.TypeEnum.Audio: // make sure the audio file exists FileInfo audioFile = new FileInfo(Path.Combine(LocalClipboard.AudioFolder.FullName, clipboardItem.KeyText)); if (audioFile.Exists) { // get audio stream from the file Stream audioStream = new MemoryStream(); using (FileStream fs = audioFile.OpenRead()) { fs.CopyTo(audioStream); } using (audioStream) { // set audio to the Windows clipboard Clipboard.SetAudio(audioStream); } } // else set errMsg to be reported else { errMsg = "Audio file is missing!"; } break; case ClipboardItem.TypeEnum.Custom: // make sure the custom file exists FileInfo customFile = new FileInfo(Path.Combine(LocalClipboard.CustomFolder.FullName, clipboardItem.KeyText)); if (customFile.Exists) { // store custom data from the serializable file object data; using (FileStream customStream = customFile.Open(FileMode.Open)) { BinaryFormatter formatter = new BinaryFormatter(); data = formatter.Deserialize(customStream); } // set custom data to the Windows clipboard Clipboard.SetData((clipboardItem as CustomItem).WritableFormat, data); } // else set errMsg to be reported else { errMsg = "Custom file is missing!"; } break; } // flip to true after algorithm is finished LocalClipboard.HandleClipboard = true; // notify to the user the results of the operation attempt if (errMsg == null) { MsgLabel.Normal("Copied to clipboard!"); } else { MsgLabel.Warn(errMsg); } }
/// <summary> /// Moves item to new index /// </summary> /// <param name="key"></param> /// <param name="value"></param> /// <param name="index"></param> public static void Move(string key, ClipboardItem value, int index) { LocalClipboard.Remove(key, value); LocalClipboard.Insert(key, value, index); }
/// <summary> /// All child classes of ClipboardItem must implement IsEquivalent. /// /// This method determines if the param is functionally equivalent to /// the current ClipboardItem instance. /// </summary> /// <param name="duplicateKeyItem">ClipboardItem with the same key as this instance</param> /// <returns>whether or not the ClipboardItems are equivalent</returns> protected override bool IsEquivalent(ClipboardItem duplicateKeyItem) { // TODO: implement method return(false); }
/// <summary> /// All child classes of ClipboardItem must implement IsEquivalent. /// /// This method determines if the param is functionally equivalent to /// the current ClipboardItem instance. /// </summary> /// <param name="duplicateKeyItem">ClipboardItem with the same key as this instance</param> /// <returns>whether or not the ClipboardItems are equivalent</returns> protected override bool IsEquivalent(ClipboardItem duplicateKeyItem) { return(duplicateKeyItem.Type == TypeEnum.Audio && this.ByteLength == (duplicateKeyItem as AudioItem).ByteLength); }
/// <summary> /// All child classes of ClipboardItem must implement IsEquivalent. /// /// This method determines if the param is functionally equivalent to /// the current ClipboardItem instance. /// </summary> /// <param name="duplicateKeyItem">ClipboardItem with the same key as this instance</param> /// <returns>whether or not the ClipboardItems are equivalent</returns> protected override bool IsEquivalent(ClipboardItem duplicateKeyItem) { return(duplicateKeyItem.Type == TypeEnum.Text && this.Text == (duplicateKeyItem as TextItem).Text); }
/// <summary> /// All child classes of ClipboardItem must implement IsEquivalent. /// /// This method determines if the param is functionally equivalent to /// the current ClipboardItem instance. /// </summary> /// <param name="duplicateKeyItem">ClipboardItem with the same key as this instance</param>LocalClipboard.AddWithFile /// <returns>whether or not the ClipboardItems are equivalent</returns> protected abstract bool IsEquivalent(ClipboardItem duplicateKeyItem);