private void comExt_Update(object sender, EventArgs e) { updateContentPreview(); // remember user selected defaults for certain content types BaseContent content = clipData.ForExtension(comExt.Text); if (content is TextLikeContent) { Settings.Default.extensionText = comExt.Text; } if (content is ImageContent) { Settings.Default.extensionImage = comExt.Text; } Settings.Default.Save(); }
/// <summary> /// Update content preview depending on available clipboard data and selected file extension /// </summary> private void updateContentPreview() { textPreview.Hide(); htmlPreview.Hide(); imagePreview.Hide(); BaseContent content = clipData.ForExtension(comExt.Text); if (content != null) { box.Text = content.Description; if (content is ImageContent imageContent) { imagePreview.BackgroundImage = imageContent.Image; imagePreview.Show(); return; } if (content is HtmlContent htmlContent) { htmlPreview.DocumentText = htmlContent.Text; htmlPreview.Show(); return; } if (content is TextLikeContent textLikeContent) { if (content is RtfContent) { textPreview.Rtf = textLikeContent.Text; } else { textPreview.Text = textLikeContent.Text; } textPreview.Show(); return; } } // no matching data found box.Text = Resources.str_error_cliboard_format_missmatch; }
/// <summary> /// Read the clipboard and update the UI /// </summary> /// <returns></returns> private bool readClipboard() { clipData = ClipboardContents.FromClipboard(); // Update extension dropdown list comExt.Items.Clear(); foreach (var content in clipData.Contents) { comExt.AddWithSeparator(content.Extensions.Except(comExt.ItemArray())); } // if selected extension does not match available contents, adjust it if (comExt.Text == "*" || comExt.Text == null || clipData.ForExtension(comExt.Text) == null) { // chose file extension based on available contents in this order BaseContent content = clipData.PrimaryContent; if (content is ImageContent) { comExt.Text = content.Extensions.Contains(Settings.Default.extensionImage) ? Settings.Default.extensionImage : content.DefaultExtension; } else if (content is TextContent) { comExt.Text = Settings.Default.extensionText == null ? content.DefaultExtension : Settings.Default.extensionText; } else if (content != null) { comExt.Text = content.DefaultExtension; } else { comExt.Text = ""; } } if (comExt.Items.Count > 0) { updateContentPreview(); return(true); } MessageBox.Show(Resources.str_noclip_text, Resources.str_main_window_title, MessageBoxButtons.OK, MessageBoxIcon.Warning); return(false); }
string save() { try { string dirname = Path.GetFullPath(txtCurrentLocation.Text); string ext = comExt.Text.ToLowerInvariant().Trim(); if (ext.StartsWith(".")) { ext = ext.Substring(1); } string filename = txtFilename.Text; if (!string.IsNullOrWhiteSpace(ext) && !filename.EndsWith("." + ext)) { filename += "." + ext; } string file = Path.Combine(dirname, filename); // check if file exists if (File.Exists(file)) { var result = MessageBox.Show(string.Format(Resources.str_file_exists, file), Resources.str_main_window_title, MessageBoxButtons.YesNo, MessageBoxIcon.Warning); if (result != DialogResult.Yes) { return(null); } } else if (Directory.Exists(file)) { MessageBox.Show(string.Format(Resources.str_file_exists_directory, file), Resources.str_main_window_title, MessageBoxButtons.OK, MessageBoxIcon.Error); return(null); } // create folders if required Directory.CreateDirectory(dirname); BaseContent contentToSave = clipData.ForExtension(comExt.Text); if (contentToSave != null) { contentToSave.SaveAs(file, ext); } /* * else if (saveAs == Type.URL) { * if (clipData.TextUrl == null) { * MessageBox.Show(Resources.str_error_save_no_uri, Resources.str_main_window_title, * MessageBoxButtons.OK, MessageBoxIcon.Error); * return null; * } * * File.WriteAllLines(file, new[] { * @"[InternetShortcut]", * @"URL=" + clipData.TextUrl * }, Encoding.UTF8); * * } * else if (saveAs == Type.SYLK && clipData.Sylk != null) { * File.WriteAllText(file, clipData.Sylk, Encoding.ASCII); * } * else if (saveAs == Type.DIF && clipData.Dif != null) { * File.WriteAllText(file, clipData.Dif, Encoding.ASCII); * } * else if (saveAs == Type.RTF && clipData.Rtf != null) { * File.WriteAllText(file, clipData.Rtf, Encoding.ASCII); * } * else if (saveAs == Type.CSV && clipData.Csv != null) { * File.WriteAllText(file, clipData.Csv, Encoding.UTF8); * } * else if (saveAs == Type.HTML && clipData.Html != null) { * var data = clipData.Html; * if (!data.StartsWith("<!DOCTYPE html>")) * data = "<!DOCTYPE html>\n" + data; * File.WriteAllText(file, data, Encoding.UTF8); * * } * else if (saveAs.IsLikeText() && clipData.Text != null) // text like, e.g. allow to save text as html * { * File.WriteAllText(file, clipData.Text, Encoding.UTF8); * }*/ else { return(null); } if (Settings.Default.clrClipboard) { Clipboard.Clear(); } saveCount++; return(file); } catch (UnauthorizedAccessException ex) { MessageBox.Show(ex.Message + "\n" + Resources.str_message_run_as_admin, Resources.str_main_window_title, MessageBoxButtons.OK, MessageBoxIcon.Error); Program.RestartAppElevated(txtCurrentLocation.Text); } catch (Exception ex) { MessageBox.Show(ex.Message, Resources.str_main_window_title, MessageBoxButtons.OK, MessageBoxIcon.Error); } return(null); }