Skip to content

Commit bb22911

Browse files
committed
Add support for data interchange format
1 parent de46e5c commit bb22911

File tree

5 files changed

+70
-42
lines changed

5 files changed

+70
-42
lines changed

PasteIntoFile/ClipboardDataContainer.cs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
namespace PasteIntoFile {
1111

1212
public enum Type {
13-
IMAGE, HTML, CSV, SYLK, RTF, URL, FILES,
13+
IMAGE, HTML, CSV, SYLK, DIF, RTF, URL, FILES,
1414
TEXT // text is last since it catches all unknown extensions
1515
}
1616

@@ -19,7 +19,7 @@ public static class TypeMethods {
1919
/// Returns true if the format is text like, i.e. text from clipboard can be saved as this format
2020
/// </summary>
2121
public static bool IsLikeText(this Type f) {
22-
return new[] { Type.TEXT, Type.HTML, Type.CSV, Type.SYLK, Type.RTF}.Contains(f);
22+
return new[] { Type.TEXT, Type.HTML, Type.CSV, Type.SYLK, Type.DIF, Type.RTF}.Contains(f);
2323
}
2424

2525
public static string[] Extensions(this Type f) {
@@ -29,9 +29,11 @@ public static string[] Extensions(this Type f) {
2929
case Type.HTML:
3030
return new[] { "html", "htm" };
3131
case Type.CSV:
32-
return new[] { "csv" };
32+
return new[] { "csv", "tsv", "tab" };
3333
case Type.SYLK:
3434
return new[] { "slk" };
35+
case Type.DIF:
36+
return new[] { "dif" };
3537
case Type.RTF:
3638
return new[] { "rtf" };
3739
case Type.URL:
@@ -74,16 +76,19 @@ public class ClipboardDataContainer {
7476
public string Html => Data.ContainsKey(Type.HTML) ? Data[Type.HTML] as string : null;
7577
public string Csv => Data.ContainsKey(Type.CSV) ? Data[Type.CSV] as string : null;
7678
public string Sylk => Data.ContainsKey(Type.SYLK) ? Data[Type.SYLK] as string : null;
79+
public string Dif => Data.ContainsKey(Type.DIF) ? Data[Type.DIF] as string : null;
7780
public string Rtf => Data.ContainsKey(Type.RTF) ? Data[Type.RTF] as string : null;
7881
public Image Image => Data.ContainsKey(Type.IMAGE) ? Data[Type.IMAGE] as Image : null;
7982
public StringCollection Files => Data.ContainsKey(Type.FILES) ? Data[Type.FILES] as StringCollection : null;
8083
public string TextUrl => Text != null && Uri.IsWellFormedUriString(Text.Trim(), UriKind.RelativeOrAbsolute) ? Text.Trim() : null;
8184

8285
public bool Has(Type type) {
83-
return Data.ContainsKey(type) ||
84-
type == Type.URL && TextUrl != null;
86+
return this[type] != null;
8587
}
8688

89+
public object this[Type t] => Data.ContainsKey(t) ? Data[t] : t == Type.URL ? TextUrl : null;
90+
91+
8792
public bool HasDataThatCanBeSaveAs(Type type) {
8893
return Has(type) ||
8994
Has(Type.TEXT) && type.IsLikeText(); // allow to save text as html, csv, etc.
@@ -107,6 +112,8 @@ public bool HasDataThatCanBeSaveAs(Type type) {
107112
public static ClipboardDataContainer fromCliboardData() {
108113
var container = new ClipboardDataContainer();
109114
container.Timestamp = DateTime.Now;
115+
116+
// https://docs.microsoft.com/en-us/windows/win32/dataxchg/standard-clipboard-formats
110117
if (Clipboard.ContainsImage())
111118
container.Data.Add(Type.IMAGE, Clipboard.GetImage());
112119
if (Clipboard.ContainsData(DataFormats.Html))
@@ -121,6 +128,8 @@ public static ClipboardDataContainer fromCliboardData() {
121128
container.Data.Add(Type.SYLK, readClipboardString(DataFormats.SymbolicLink));
122129
if (Clipboard.ContainsData(DataFormats.Rtf))
123130
container.Data.Add(Type.RTF, readClipboardString(DataFormats.Rtf));
131+
if (Clipboard.ContainsData(DataFormats.Dif))
132+
container.Data.Add(Type.DIF, readClipboardString(DataFormats.Dif));
124133

125134
return container;
126135
}

PasteIntoFile/Dialog.cs

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Diagnostics;
34
using System.Drawing;
45
using System.Drawing.Imaging;
@@ -193,48 +194,48 @@ private void updateContents() {
193194

194195
Type saveAs = TypeMethods.FromExtension(comExt.Text);
195196

196-
if (clipData.HasDataThatCanBeSaveAs(saveAs)) {
197-
if (saveAs == Type.IMAGE && clipData.Image != null) {
198-
imgContent.BackgroundImage = clipData.Image;
199-
imgContent.Show();
200-
box.Text = string.Format(Resources.str_preview_image, clipData.Image.Width, clipData.Image.Height);
201-
}
202-
else if (saveAs == Type.HTML && clipData.Html != null) {
203-
htmlContent.DocumentText = clipData.Html;
204-
htmlContent.Show();
205-
box.Text = Resources.str_preview_html;
206-
}
207-
else if (saveAs == Type.URL && clipData.TextUrl != null) {
208-
txtContent.Text = clipData.TextUrl;
209-
txtContent.Show();
210-
box.Text = Resources.str_preview_url;
211-
}
212-
else if (saveAs == Type.CSV && clipData.Csv != null) {
213-
txtContent.Text = clipData.Csv;
214-
txtContent.Show();
215-
box.Text = Resources.str_preview_csv;
216-
}
217-
else if (saveAs == Type.SYLK && clipData.Sylk != null) {
218-
txtContent.Text = clipData.Sylk;
219-
txtContent.Show();
220-
box.Text = Resources.str_preview_sylk;
221-
}
222-
else if (saveAs == Type.RTF && clipData.Rtf != null) {
223-
txtContent.Rtf = clipData.Rtf;
224-
txtContent.Show();
225-
box.Text = Resources.str_preview_rtf;
226-
}
227-
else if (saveAs.IsLikeText()) {
228-
txtContent.Text = clipData.Text;
197+
if (saveAs == Type.IMAGE && clipData.Image != null) {
198+
imgContent.BackgroundImage = clipData.Image;
199+
imgContent.Show();
200+
box.Text = string.Format(Resources.str_preview_image, clipData.Image.Width, clipData.Image.Height);
201+
return;
202+
}
203+
204+
if (saveAs == Type.HTML && clipData.Html != null) {
205+
htmlContent.DocumentText = clipData.Html;
206+
htmlContent.Show();
207+
box.Text = Resources.str_preview_html;
208+
return;
209+
}
210+
211+
// text like formats which are show in txtContent preview
212+
foreach (var t in new Dictionary<Type, string> {
213+
{ Type.URL, Resources.str_preview_url },
214+
{ Type.CSV, Resources.str_preview_csv },
215+
{ Type.SYLK, Resources.str_preview_sylk },
216+
{ Type.DIF, Resources.str_preview_dif },
217+
{ Type.RTF, Resources.str_preview_rtf },
218+
})
219+
{
220+
if (saveAs == t.Key && clipData[t.Key] is string) {
221+
txtContent.Text = clipData[t.Key] as string;
229222
txtContent.Show();
230-
box.Text = string.Format(Resources.str_preview_text, clipData.Text.Length, clipData.Text.Split('\n').Length);
223+
box.Text = t.Value;
224+
return;
231225
}
232-
233226
}
234-
else {
235-
box.Text = Resources.str_error_cliboard_format_missmatch;
227+
228+
if (saveAs.IsLikeText() && clipData.Text != null) {
229+
txtContent.Text = clipData.Text;
230+
txtContent.Show();
231+
box.Text = string.Format(Resources.str_preview_text, clipData.Text.Length,
232+
clipData.Text.Split('\n').Length);
233+
return;
236234
}
237235

236+
// no matching data found
237+
box.Text = Resources.str_error_cliboard_format_missmatch;
238+
238239
}
239240
private void updateSavebutton()
240241
{
@@ -321,6 +322,9 @@ string save()
321322
else if (saveAs == Type.SYLK && clipData.Sylk != null) {
322323
File.WriteAllText(file, clipData.Sylk, Encoding.ASCII);
323324
}
325+
else if (saveAs == Type.DIF && clipData.Dif != null) {
326+
File.WriteAllText(file, clipData.Dif, Encoding.ASCII);
327+
}
324328
else if (saveAs == Type.RTF && clipData.Rtf != null) {
325329
File.WriteAllText(file, clipData.Rtf, Encoding.ASCII);
326330
}

PasteIntoFile/Properties/Resources.Designer.cs

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

PasteIntoFile/Properties/Resources.de.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,4 +176,7 @@ Für Details zur Formatierung &lt;a&gt;hier klicken&lt;/a&gt;.</value>
176176
<data name="str_preview_rtf" xml:space="preserve">
177177
<value>Rich Text Vorschau</value>
178178
</data>
179+
<data name="str_preview_dif" xml:space="preserve">
180+
<value>Data Interchange Vorschau</value>
181+
</data>
179182
</root>

PasteIntoFile/Properties/Resources.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,4 +286,7 @@ Save current clipboard contents now?</value>
286286
<data name="str_preview_rtf" xml:space="preserve">
287287
<value>Rich Text Preview</value>
288288
</data>
289+
<data name="str_preview_dif" xml:space="preserve">
290+
<value>Data Interchange preview</value>
291+
</data>
289292
</root>

0 commit comments

Comments
 (0)