-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainForm.cs
176 lines (151 loc) · 5.15 KB
/
MainForm.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace LinkHelper
{
public partial class MainForm : Form
{
[DllImport("User32.dll")]
protected static extern int
SetClipboardViewer(int hWndNewViewer);
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern bool
ChangeClipboardChain(IntPtr hWndRemove,
IntPtr hWndNewNext);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int SendMessage(IntPtr hwnd, int wMsg,
IntPtr wParam,
IntPtr lParam);
public static String version = "1.0.1";
public static String clipboard;
private bool ready = false;
MySettings settings = MySettings.Load();
IntPtr nextClipboardViewer;
public MainForm()
{
InitializeComponent();
nextClipboardViewer = (IntPtr)SetClipboardViewer((int)
this.Handle);
}
private void MainForm_Load(object sender, EventArgs e)
{
versionLabel.Text = versionLabel.Text.Replace("%s", version);
shortSize.Value = settings.maxlength;
ready = true;
}
private void onClose(object sender, FormClosingEventArgs e)
{
this.Hide();
trayIcon.Visible = true;
trayIcon.ShowBalloonTip(1000);
e.Cancel = true;
}
private void onClick(object sender, EventArgs e)
{
this.Show();
trayIcon.Visible = false;
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
System.Diagnostics.Process.Start("http://stefanarts.net");
}
private void button1_Click(object sender, EventArgs e)
{
Environment.Exit(0);
}
void checkClipboard()
{
if(ready)
{
try
{
IDataObject iData = new DataObject();
iData = Clipboard.GetDataObject();
if (iData.GetDataPresent(DataFormats.Text))
{
clipboard = (string)iData.GetData(DataFormats.Text);
if (Uri.IsWellFormedUriString(clipboard, UriKind.Absolute))
{
if (clipboard.Length > settings.maxlength)
{
if (isOnline())
{
System.Windows.Forms.Clipboard.SetText(shortUrlWithSLS(clipboard));
}
}
}
}
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}
}
private String shortUrlWithSLS(String url)
{
using (WebClient client = new WebClient())
{
string raw = client.DownloadString("https://s-ls.de/?url=" + url + "&linkhelper");
var link = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(raw);
return "https://s-ls.de/" + link.link.shortname;
}
}
protected override void
WndProc(ref System.Windows.Forms.Message m)
{
// defined in winuser.h
const int WM_DRAWCLIPBOARD = 0x308;
const int WM_CHANGECBCHAIN = 0x030D;
switch (m.Msg)
{
case WM_DRAWCLIPBOARD:
checkClipboard();
SendMessage(nextClipboardViewer, m.Msg, m.WParam,
m.LParam);
break;
case WM_CHANGECBCHAIN:
if (m.WParam == nextClipboardViewer)
nextClipboardViewer = m.LParam;
else
SendMessage(nextClipboardViewer, m.Msg, m.WParam,
m.LParam);
break;
default:
base.WndProc(ref m);
break;
}
}
public static bool isOnline()
{
try
{
using (var client = new WebClient())
using (client.OpenRead("http://google.com/generate_204"))
return true;
}
catch
{
return false;
}
}
private void shortSize_ValueChanged(object sender, EventArgs e)
{
settings.maxlength = (int)shortSize.Value;
settings.Save();
}
}
class MySettings : AppSettings<MySettings>
{
public int maxlength = 25;
}
}