1 // OpenTween - Client of Twitter
2 // Copyright (c) 2007-2011 kiri_feather (@kiri_feather) <kiri.feather@gmail.com>
3 // (c) 2008-2011 Moz (@syo68k)
4 // (c) 2008-2011 takeshik (@takeshik) <http://www.takeshik.org/>
5 // (c) 2010-2011 anis774 (@anis774) <http://d.hatena.ne.jp/anis774/>
6 // (c) 2010-2011 fantasticswallow (@f_swallow) <http://twitter.com/f_swallow>
7 // (c) 2011 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
8 // All rights reserved.
10 // This file is part of OpenTween.
12 // This program is free software; you can redistribute it and/or modify it
13 // under the terms of the GNU General public License as published by the Free
14 // Software Foundation; either version 3 of the License, or (at your option)
17 // This program is distributed in the hope that it will be useful, but
18 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General public License
22 // You should have received a copy of the GNU General public License along
23 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
24 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
25 // Boston, MA 02110-1301, USA.
28 using System.Collections.Generic;
31 using System.Reflection;
34 using System.Drawing.Imaging;
35 using System.Windows.Forms;
36 using System.ComponentModel;
37 using System.Collections;
38 using System.Globalization;
42 public class GrowlHelper
44 private Assembly _connector = null;
45 private Assembly _core = null;
47 private object _growlNTreply;
48 private object _growlNTdm;
49 private object _growlNTnew;
50 private object _growlNTusevent;
51 private object _growlApp;
53 private object _targetConnector;
54 bool _initialized = false;
56 public class NotifyCallbackEventArgs : EventArgs
58 public long StatusId { get; set; }
59 public NotifyType NotifyType { get; set; }
60 public NotifyCallbackEventArgs(NotifyType notifyType, string statusId)
62 if (statusId.Length > 1)
64 this.StatusId = long.Parse(statusId);
65 this.NotifyType = notifyType;
70 public event EventHandler<NotifyCallbackEventArgs> NotifyClicked;
72 public string AppName { get; }
74 public enum NotifyType
82 public GrowlHelper(string appName)
83 => this.AppName = appName;
85 public bool IsAvailable
89 if (_connector == null || _core == null || !_initialized)
96 private byte[] IconToByteArray(string filename)
98 using (Icon ic = new Icon(filename))
100 return IconToByteArray(ic);
104 private byte[] IconToByteArray(Icon icondata)
106 using (MemoryStream ms = new MemoryStream())
108 using (Icon ic = new Icon(icondata, 48, 48))
110 ic.ToBitmap().Save(ms, ImageFormat.Png);
116 public static bool IsDllExists
120 string dir = Application.StartupPath;
121 string connectorPath = Path.Combine(dir, "Growl.Connector.dll");
122 string corePath = Path.Combine(dir, "Growl.CoreLibrary.dll");
123 if (File.Exists(connectorPath) && File.Exists(corePath))
130 public bool RegisterGrowl()
132 _initialized = false;
133 string dir = Application.StartupPath;
134 string connectorPath = Path.Combine(dir, "Growl.Connector.dll");
135 string corePath = Path.Combine(dir, "Growl.CoreLibrary.dll");
139 if (!IsDllExists) return false;
140 _connector = Assembly.LoadFile(connectorPath);
141 _core = Assembly.LoadFile(corePath);
150 _targetConnector = _connector.CreateInstance("Growl.Connector.GrowlConnector");
151 Type _t = _connector.GetType("Growl.Connector.NotificationType");
153 _growlNTreply = _t.InvokeMember(null,
154 BindingFlags.CreateInstance, null, null, new object[] { "REPLY", "Reply" }, CultureInfo.InvariantCulture);
156 _growlNTdm = _t.InvokeMember(null,
157 BindingFlags.CreateInstance, null, null, new object[] { "DIRECT_MESSAGE", "DirectMessage" }, CultureInfo.InvariantCulture);
159 _growlNTnew = _t.InvokeMember(null,
160 BindingFlags.CreateInstance, null, null, new object[] { "NOTIFY", "新着通知" }, CultureInfo.InvariantCulture);
162 _growlNTusevent = _t.InvokeMember(null,
163 BindingFlags.CreateInstance, null, null, new object[] { "USERSTREAM_EVENT", "UserStream Event" }, CultureInfo.InvariantCulture);
166 _connector.GetType("Growl.Connector.Cryptography+SymmetricAlgorithmType").InvokeMember(
167 "PlainText", BindingFlags.GetField, null, null, null, CultureInfo.InvariantCulture);
168 _targetConnector.GetType().InvokeMember("EncryptionAlgorithm", BindingFlags.SetProperty, null, _targetConnector, new object[] { encryptType }, CultureInfo.InvariantCulture);
170 _growlApp = _connector.CreateInstance(
171 "Growl.Connector.Application", false, BindingFlags.Default, null, new object[] { AppName }, null, null);
174 if (File.Exists(Path.Combine(Application.StartupPath, "Icons\\Tween.png")))
176 // Icons\Tween.pngを使用
177 ConstructorInfo ci = _core.GetType(
178 "Growl.CoreLibrary.Resource").GetConstructor(
179 BindingFlags.NonPublic | BindingFlags.Instance,
180 null, new Type[] { typeof(string) }, null);
182 object data = ci.Invoke(new object[] { Path.Combine(Application.StartupPath, "Icons\\Tween.png") });
183 PropertyInfo pi = _growlApp.GetType().GetProperty("Icon");
184 pi.SetValue(_growlApp, data, null);
187 else if (File.Exists(Path.Combine(Application.StartupPath, "Icons\\MIcon.ico")))
189 // アイコンセットにMIcon.icoが存在する場合それを使用
190 ConstructorInfo cibd = _core.GetType(
191 "Growl.CoreLibrary.BinaryData").GetConstructor(
192 BindingFlags.Public | BindingFlags.Instance,
193 null, new Type[] { typeof(byte[]) }, null);
194 object bdata = cibd.Invoke(
195 new object[] { IconToByteArray(Path.Combine(Application.StartupPath, "Icons\\MIcon.ico")) });
197 ConstructorInfo ciRes = _core.GetType(
198 "Growl.CoreLibrary.Resource").GetConstructor(
199 BindingFlags.NonPublic | BindingFlags.Instance,
200 null, new Type[] { bdata.GetType() }, null);
202 object data = ciRes.Invoke(new object[] { bdata });
203 PropertyInfo pi = _growlApp.GetType().GetProperty("Icon");
204 pi.SetValue(_growlApp, data, null);
209 ConstructorInfo cibd = _core.GetType(
210 "Growl.CoreLibrary.BinaryData").GetConstructor(
211 BindingFlags.Public | BindingFlags.Instance,
212 null, new Type[] { typeof(byte[]) }, null);
213 object bdata = cibd.Invoke(
214 new object[] { IconToByteArray(Properties.Resources.MIcon) });
216 ConstructorInfo ciRes = _core.GetType(
217 "Growl.CoreLibrary.Resource").GetConstructor(
218 BindingFlags.NonPublic | BindingFlags.Instance,
219 null, new Type[] { bdata.GetType() }, null);
221 object data = ciRes.Invoke(new object[] { bdata });
222 PropertyInfo pi = _growlApp.GetType().GetProperty("Icon");
223 pi.SetValue(_growlApp, data, null);
226 MethodInfo mi = _targetConnector.GetType().GetMethod("Register", new Type[] { _growlApp.GetType(), _connector.GetType("Growl.Connector.NotificationType[]") });
228 _t = _connector.GetType("Growl.Connector.NotificationType");
230 var arglist = new ArrayList
238 mi.Invoke(_targetConnector, new object[] { _growlApp, arglist.ToArray(_t) });
241 Type tGrowlConnector = _connector.GetType("Growl.Connector.GrowlConnector");
242 EventInfo evNotificationCallback = tGrowlConnector.GetEvent("NotificationCallback");
243 Type tDelegate = evNotificationCallback.EventHandlerType;
244 MethodInfo miHandler = typeof(GrowlHelper).GetMethod("GrowlCallbackHandler", BindingFlags.NonPublic | BindingFlags.Instance);
245 Delegate d = Delegate.CreateDelegate(tDelegate, this, miHandler);
246 MethodInfo miAddHandler = evNotificationCallback.GetAddMethod();
247 object[] addHandlerArgs = { d };
248 miAddHandler.Invoke(_targetConnector, addHandlerArgs);
254 _initialized = false;
261 public void Notify(NotifyType notificationType, string id, string title, string text, Image icon = null, string url = "")
263 if (!_initialized) return;
264 string notificationName = "";
265 switch (notificationType)
267 case NotifyType.Reply:
268 notificationName = "REPLY";
270 case NotifyType.DirectMessage:
271 notificationName = "DIRECT_MESSAGE";
273 case NotifyType.Notify:
274 notificationName = "NOTIFY";
276 case NotifyType.UserStreamEvent:
277 notificationName = "USERSTREAM_EVENT";
281 if (icon != null || !string.IsNullOrEmpty(url))
283 Type gCore = _core.GetType("Growl.CoreLibrary.Resource");
287 res = gCore.InvokeMember("op_Implicit",
288 BindingFlags.Public | BindingFlags.Static | BindingFlags.InvokeMethod,
291 new object[] { icon },
292 CultureInfo.InvariantCulture);
296 res = gCore.InvokeMember("op_Implicit",
297 BindingFlags.Public | BindingFlags.Static | BindingFlags.InvokeMethod,
300 new object[] { url },
301 CultureInfo.InvariantCulture);
304 _connector.GetType("Growl.Connector.Priority").InvokeMember(
305 "Normal", BindingFlags.GetField, null, null, null, CultureInfo.InvariantCulture);
306 n = _connector.GetType("Growl.Connector.Notification").InvokeMember(
308 BindingFlags.CreateInstance,
311 new object[] {AppName,
320 CultureInfo.InvariantCulture);
324 n = _connector.GetType("Growl.Connector.Notification").InvokeMember(
326 BindingFlags.CreateInstance,
329 new object[] {AppName,
334 CultureInfo.InvariantCulture);
336 //_targetConnector.GetType.InvokeMember("Notify", BindingFlags.InvokeMethod, null, _targetConnector, new object[] {n});
337 object cc = _connector.GetType("Growl.Connector.CallbackContext").InvokeMember(
338 null, BindingFlags.CreateInstance, null, _connector,
339 new object[] { "some fake information", notificationName },
340 CultureInfo.InvariantCulture);
341 _targetConnector.GetType().InvokeMember("Notify", BindingFlags.InvokeMethod, null, _targetConnector, new object[] { n, cc }, CultureInfo.InvariantCulture);
344 private void GrowlCallbackHandler(object response, object callbackData, object state)
350 _core.GetType("Growl.CoreLibrary.CallbackResult").GetField(
352 BindingFlags.Public | BindingFlags.Static).GetRawConstantValue();
354 object vResult = callbackData.GetType().GetProperty(
356 BindingFlags.Public | BindingFlags.Instance).GetGetMethod().Invoke(callbackData, null);
357 vResult = (int)vResult;
358 string notifyId = (string)callbackData.GetType().GetProperty("NotificationID").GetGetMethod().Invoke(callbackData, null);
359 string notifyName = (string)callbackData.GetType().GetProperty("Type").GetGetMethod().Invoke(callbackData, null);
360 if (vCLICK.Equals(vResult))
366 nt = NotifyType.Reply;
368 case "DIRECT_MESSAGE":
369 nt = NotifyType.DirectMessage;
372 nt = NotifyType.Notify;
374 case "USERSTREAM_EVENT":
375 nt = NotifyType.UserStreamEvent;
381 NotifyClicked?.Invoke(this, new NotifyCallbackEventArgs(nt, notifyId));