OSDN Git Service

Port Growl.vb to C#
authorKimura Youichi <kim.upsilon@bucyou.net>
Sun, 4 Dec 2011 12:40:59 +0000 (21:40 +0900)
committerKimura Youichi <kim.upsilon@bucyou.net>
Wed, 22 Feb 2012 10:47:44 +0000 (19:47 +0900)
Tween/Growl.vb [deleted file]
Tween/Tween.vbproj
TweenCS/Growl.cs [new file with mode: 0644]
TweenCS/TweenCS.csproj

diff --git a/Tween/Growl.vb b/Tween/Growl.vb
deleted file mode 100644 (file)
index 7063575..0000000
+++ /dev/null
@@ -1,309 +0,0 @@
-Imports System.Reflection
-Imports System.IO
-Imports System.ComponentModel
-Imports System.Drawing.Imaging
-
-Public Class GrowlHelper
-
-    Private _connector As Assembly = Nothing
-    Private _core As Assembly = Nothing
-
-    Private _growlNTreply As Object
-    Private _growlNTdm As Object
-    Private _growlNTnew As Object
-    Private _growlNTusevent As Object
-    Private _growlApp As Object
-
-    Private _targetConnector As Object
-    Private _targetCore As Object
-
-    Private _appName As String = ""
-    Dim _initialized As Boolean = False
-
-    Public Class NotifyCallbackEventArgs
-        Inherits EventArgs
-        Public Property StatusId As Long
-        Public Property NotifyType As NotifyType
-        Public Sub New(ByVal notifyType As NotifyType, ByVal statusId As String)
-            If statusId.Length > 1 Then
-                Me.StatusId = CLng(statusId)
-                Me.NotifyType = notifyType
-            End If
-        End Sub
-    End Class
-
-    Public Event NotifyClicked(ByVal sender As Object, ByVal e As NotifyCallbackEventArgs)
-
-    Public ReadOnly Property AppName As String
-        Get
-            Return _appName
-        End Get
-    End Property
-
-    Public Enum NotifyType
-        Reply = 0
-        DirectMessage = 1
-        Notify = 2
-        UserStreamEvent = 3
-    End Enum
-
-    Public Sub New(ByVal appName As String)
-        _appName = appName
-    End Sub
-
-    Public ReadOnly Property IsAvailable As Boolean
-        Get
-            If _connector Is Nothing OrElse _core Is Nothing OrElse Not _initialized Then
-                Return False
-            Else
-                Return True
-            End If
-        End Get
-    End Property
-
-    Private Overloads Function IconToByteArray(ByVal filename As String) As Byte()
-        Return IconToByteArray(New Icon(filename))
-    End Function
-
-    Private Overloads Function IconToByteArray(ByVal icondata As Icon) As Byte()
-        Using ms As New MemoryStream
-            Dim ic As Icon = New Icon(icondata, 48, 48)
-            ic.ToBitmap.Save(ms, ImageFormat.Png)
-            Return ms.ToArray()
-        End Using
-    End Function
-
-    Public Shared ReadOnly Property IsDllExists As Boolean
-        Get
-            Dim dir As String = Application.StartupPath
-            Dim connectorPath As String = Path.Combine(dir, "Growl.Connector.dll")
-            Dim corePath As String = Path.Combine(dir, "Growl.CoreLibrary.dll")
-            If File.Exists(connectorPath) AndAlso File.Exists(corePath) Then
-                Return True
-            Else
-                Return False
-            End If
-        End Get
-    End Property
-
-    Public Function RegisterGrowl() As Boolean
-
-        _initialized = False
-        Dim dir As String = Application.StartupPath
-        Dim connectorPath As String = Path.Combine(dir, "Growl.Connector.dll")
-        Dim corePath As String = Path.Combine(dir, "Growl.CoreLibrary.dll")
-
-        Try
-            If Not IsDllExists Then Return False
-            _connector = Assembly.LoadFile(connectorPath)
-            _core = Assembly.LoadFile(corePath)
-        Catch ex As Exception
-            Return False
-        End Try
-
-        Try
-            _targetConnector = _connector.CreateInstance("Growl.Connector.GrowlConnector")
-            _targetCore = _core.CreateInstance("Growl.CoreLibrary")
-            Dim _t As Type = _connector.GetType("Growl.Connector.NotificationType")
-
-            _growlNTreply = _t.InvokeMember(Nothing,
-                BindingFlags.CreateInstance, Nothing, Nothing, New Object() {"REPLY", "Reply"})
-
-            _growlNTdm = _t.InvokeMember(Nothing,
-                BindingFlags.CreateInstance, Nothing, Nothing, New Object() {"DIRECT_MESSAGE", "DirectMessage"})
-
-            _growlNTnew = _t.InvokeMember(Nothing,
-                BindingFlags.CreateInstance, Nothing, Nothing, New Object() {"NOTIFY", "新着通知"})
-
-            _growlNTusevent = _t.InvokeMember(Nothing,
-                BindingFlags.CreateInstance, Nothing, Nothing, New Object() {"USERSTREAM_EVENT", "UserStream Event"})
-
-            Dim encryptType As Object =
-                    _connector.GetType("Growl.Connector.Cryptography+SymmetricAlgorithmType").InvokeMember(
-                        "PlainText", BindingFlags.GetField, Nothing, Nothing, Nothing)
-            _targetConnector.GetType.InvokeMember("EncryptionAlgorithm", BindingFlags.SetProperty, Nothing, _targetConnector, New Object() {encryptType})
-
-            _growlApp = _connector.CreateInstance(
-                "Growl.Connector.Application", False, BindingFlags.Default, Nothing, New Object() {_appName}, Nothing, Nothing)
-
-
-            If File.Exists(Path.Combine(Application.StartupPath, "Icons\Tween.png")) Then
-                ' Icons\Tween.pngを使用
-                Dim ci As ConstructorInfo = _core.GetType(
-                    "Growl.CoreLibrary.Resource").GetConstructor(
-                    BindingFlags.NonPublic Or BindingFlags.Instance,
-                    Nothing, New Type() {GetType(System.String)}, Nothing)
-
-                Dim data As Object = ci.Invoke(New Object() {Path.Combine(Application.StartupPath, "Icons\Tween.png")})
-                Dim pi As PropertyInfo = _growlApp.GetType.GetProperty("Icon")
-                pi.SetValue(_growlApp, data, Nothing)
-
-            ElseIf File.Exists(Path.Combine(Application.StartupPath, "Icons\MIcon.ico")) Then
-                ' アイコンセットにMIcon.icoが存在する場合それを使用
-                Dim cibd As ConstructorInfo = _core.GetType(
-                     "Growl.CoreLibrary.BinaryData").GetConstructor(
-                     BindingFlags.Public Or BindingFlags.Instance,
-                     Nothing, New Type() {GetType(Byte())}, Nothing)
-                Dim tc As New TypeConverter
-                Dim bdata As Object = cibd.Invoke(
-                    New Object() {IconToByteArray(Path.Combine(Application.StartupPath, "Icons\MIcon.ico"))})
-
-                Dim ciRes As ConstructorInfo = _core.GetType(
-                    "Growl.CoreLibrary.Resource").GetConstructor(
-                    BindingFlags.NonPublic Or BindingFlags.Instance,
-                    Nothing, New Type() {bdata.GetType()}, Nothing)
-
-                Dim data As Object = ciRes.Invoke(New Object() {bdata})
-                Dim pi As PropertyInfo = _growlApp.GetType.GetProperty("Icon")
-                pi.SetValue(_growlApp, data, Nothing)
-            Else
-                '内蔵アイコンリソースを使用
-                Dim cibd As ConstructorInfo = _core.GetType(
-                     "Growl.CoreLibrary.BinaryData").GetConstructor(
-                     BindingFlags.Public Or BindingFlags.Instance,
-                     Nothing, New Type() {GetType(Byte())}, Nothing)
-                Dim tc As New TypeConverter
-                Dim bdata As Object = cibd.Invoke(
-                    New Object() {IconToByteArray(My.Resources.MIcon)})
-
-                Dim ciRes As ConstructorInfo = _core.GetType(
-                    "Growl.CoreLibrary.Resource").GetConstructor(
-                    BindingFlags.NonPublic Or BindingFlags.Instance,
-                    Nothing, New Type() {bdata.GetType()}, Nothing)
-
-                Dim data As Object = ciRes.Invoke(New Object() {bdata})
-                Dim pi As PropertyInfo = _growlApp.GetType.GetProperty("Icon")
-                pi.SetValue(_growlApp, data, Nothing)
-            End If
-
-            Dim mi As MethodInfo = _targetConnector.GetType.GetMethod("Register", New Type() {_growlApp.GetType, _connector.GetType("Growl.Connector.NotificationType[]")})
-
-            _t = _connector.GetType("Growl.Connector.NotificationType")
-
-            Dim arglist As New ArrayList
-            arglist.Add(_growlNTreply)
-            arglist.Add(_growlNTdm)
-            arglist.Add(_growlNTnew)
-            arglist.Add(_growlNTusevent)
-
-            mi.Invoke(_targetConnector, New Object() {_growlApp, arglist.ToArray(_t)})
-
-            ' コールバックメソッドの登録
-            Dim tGrowlConnector As Type = _connector.GetType("Growl.Connector.GrowlConnector")
-            Dim evNotificationCallback As EventInfo = tGrowlConnector.GetEvent("NotificationCallback")
-            Dim tDelegate As Type = evNotificationCallback.EventHandlerType
-            Dim miHandler As MethodInfo = GetType(GrowlHelper).GetMethod("GrowlCallbackHandler", BindingFlags.NonPublic Or BindingFlags.Instance)
-            Dim d As [Delegate] = [Delegate].CreateDelegate(tDelegate, Me, miHandler)
-            Dim miAddHandler As MethodInfo = evNotificationCallback.GetAddMethod()
-            Dim addHandlerArgs() As Object = {d}
-            miAddHandler.Invoke(_targetConnector, addHandlerArgs)
-
-            _initialized = True
-
-        Catch ex As Exception
-            _initialized = False
-            Return False
-        End Try
-
-        Return True
-    End Function
-
-    Public Sub Notify(ByVal notificationType As NotifyType, ByVal id As String, ByVal title As String, ByVal text As String, Optional ByVal icon As Image = Nothing, Optional ByVal url As String = "")
-        If Not _initialized Then Return
-        Dim notificationName As String = ""
-        Select Case notificationType
-            Case NotifyType.Reply
-                notificationName = "REPLY"
-            Case NotifyType.DirectMessage
-                notificationName = "DIRECT_MESSAGE"
-            Case NotifyType.Notify
-                notificationName = "NOTIFY"
-            Case NotifyType.UserStreamEvent
-                notificationName = "USERSTREAM_EVENT"
-        End Select
-        Dim n As Object = Nothing
-        If icon IsNot Nothing OrElse Not String.IsNullOrEmpty(url) Then
-            Dim gCore As Type = _core.GetType("Growl.CoreLibrary.Resource")
-            Dim res As Object = Nothing
-            If icon IsNot Nothing Then
-                res = gCore.InvokeMember("op_Implicit",
-                                                   BindingFlags.Public Or BindingFlags.Static Or BindingFlags.InvokeMethod,
-                                                   Nothing,
-                                                   Nothing,
-                                                   New Object() {icon})
-            Else
-                res = gCore.InvokeMember("op_Implicit",
-                                                   BindingFlags.Public Or BindingFlags.Static Or BindingFlags.InvokeMethod,
-                                                   Nothing,
-                                                   Nothing,
-                                                   New Object() {url})
-            End If
-            Dim priority As Object =
-                    _connector.GetType("Growl.Connector.Priority").InvokeMember(
-                        "Normal", BindingFlags.GetField, Nothing, Nothing, Nothing)
-            n = _connector.GetType("Growl.Connector.Notification").InvokeMember(
-                    "Notification",
-                    BindingFlags.CreateInstance,
-                    Nothing,
-                    _connector,
-                    New Object() {_appName,
-                                  notificationName,
-                                  id,
-                                  title,
-                                  text,
-                                  res,
-                                  False,
-                                  priority,
-                                  "aaa"})
-        Else
-            n = _connector.GetType("Growl.Connector.Notification").InvokeMember(
-                    "Notification",
-                    BindingFlags.CreateInstance,
-                    Nothing,
-                    _connector,
-                    New Object() {_appName,
-                                    notificationName,
-                                    id,
-                                    title,
-                                    text})
-        End If
-        '_targetConnector.GetType.InvokeMember("Notify", BindingFlags.InvokeMethod, Nothing, _targetConnector, New Object() {n})
-        Dim cc As Object = _connector.GetType("Growl.Connector.CallbackContext").InvokeMember(
-            Nothing, BindingFlags.CreateInstance, Nothing, _connector,
-            New Object() {"some fake information", notificationName})
-        _targetConnector.GetType.InvokeMember("Notify", BindingFlags.InvokeMethod, Nothing, _targetConnector, New Object() {n, cc})
-    End Sub
-
-    Private Sub GrowlCallbackHandler(ByVal response As Object, ByVal callbackData As Object, ByVal state As Object)
-        Try
-            ' 定数取得
-            Dim vCLICK As Object =
-            _core.GetType("Growl.CoreLibrary.CallbackResult").GetField(
-                        "CLICK",
-                       BindingFlags.Public Or BindingFlags.Static).GetRawConstantValue()
-            ' 実際の値
-            Dim vResult As Object = callbackData.GetType.GetProperty(
-                        "Result",
-                        BindingFlags.Public Or BindingFlags.Instance).GetGetMethod.Invoke(callbackData, Nothing)
-            vResult = CType(vResult, Integer)
-            Dim notifyId As String = CStr(callbackData.GetType.GetProperty("NotificationID").GetGetMethod.Invoke(callbackData, Nothing))
-            Dim notifyName As String = CStr(callbackData.GetType.GetProperty("Type").GetGetMethod.Invoke(callbackData, Nothing))
-            If vCLICK.Equals(vResult) Then
-                Dim nt As NotifyType
-                Select Case notifyName
-                    Case "REPLY"
-                        nt = NotifyType.Reply
-                    Case "DIRECT_MESSAGE"
-                        nt = NotifyType.DirectMessage
-                    Case "NOTIFY"
-                        nt = NotifyType.Notify
-                    Case "USERSTREAM_EVENT"
-                        nt = NotifyType.UserStreamEvent
-                End Select
-                RaiseEvent NotifyClicked(Me, New NotifyCallbackEventArgs(nt, notifyId))
-            End If
-        Catch ex As Exception
-            Exit Sub
-        End Try
-    End Sub
-End Class
index f0ec1e9..b4f27f6 100644 (file)
     </Compile>
     <Compile Include="Foursquare.vb" />
     <Compile Include="Google.vb" />
-    <Compile Include="Growl.vb" />
     <Compile Include="HashtagManage.Designer.vb">
       <DependentUpon>HashtagManage.vb</DependentUpon>
     </Compile>
diff --git a/TweenCS/Growl.cs b/TweenCS/Growl.cs
new file mode 100644 (file)
index 0000000..1c03dae
--- /dev/null
@@ -0,0 +1,390 @@
+// Tween - Client of Twitter
+// Copyright (c) 2007-2011 kiri_feather (@kiri_feather) <kiri.feather@gmail.com>
+//           (c) 2008-2011 Moz (@syo68k)
+//           (c) 2008-2011 takeshik (@takeshik) <http://www.takeshik.org/>
+//           (c) 2010-2011 anis774 (@anis774) <http://d.hatena.ne.jp/anis774/>
+//           (c) 2010-2011 fantasticswallow (@f_swallow) <http://twitter.com/f_swallow>
+//           (c) 2011      kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
+// All rights reserved.
+// 
+// This file is part of Tween.
+// 
+// This program is free software; you can redistribute it and/or modify it
+// under the terms of the GNU General public License as published by the Free
+// Software Foundation; either version 3 of the License, or (at your option)
+// any later version.
+// 
+// This program is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General public License
+// for more details. 
+// 
+// You should have received a copy of the GNU General public License along
+// with this program. if (not, see <http://www.gnu.org/licenses/>, or write to
+// the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
+// Boston, MA 02110-1301, USA.
+
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Reflection;
+using System.IO;
+using System.Drawing;
+using System.Drawing.Imaging;
+using System.Windows.Forms;
+using System.ComponentModel;
+using System.Collections;
+
+namespace Tween
+{
+    public class GrowlHelper
+    {
+        private Assembly _connector = null;
+        private Assembly _core = null;
+
+        private object _growlNTreply;
+        private object _growlNTdm;
+        private object _growlNTnew;
+        private object _growlNTusevent;
+        private object _growlApp;
+
+        private object _targetConnector;
+        private object _targetCore;
+
+        private string _appName = "";
+        bool _initialized = false;
+
+        public class NotifyCallbackEventArgs : EventArgs
+        {
+            public long StatusId { get; set; }
+            public NotifyType NotifyType { get; set; }
+            public NotifyCallbackEventArgs(NotifyType notifyType, string statusId)
+            {
+                if (statusId.Length > 1)
+                {
+                    this.StatusId = long.Parse(statusId);
+                    this.NotifyType = notifyType;
+                }
+            }
+        }
+
+        public delegate void NotifyClickedEventHandler(object sender, NotifyCallbackEventArgs e);
+        public event NotifyClickedEventHandler NotifyClicked;
+
+        public string AppName
+        {
+            get { return _appName; }
+        }
+
+        public enum NotifyType
+        {
+            Reply = 0,
+            DirectMessage = 1,
+            Notify = 2,
+            UserStreamEvent = 3,
+        }
+
+        public GrowlHelper(string appName)
+        {
+            _appName = appName;
+        }
+
+        public bool IsAvailable
+        {
+            get
+            {
+                if (_connector == null || _core == null || !_initialized)
+                    return false;
+                else
+                    return true;
+            }
+        }
+
+        private byte[] IconToByteArray(string filename)
+        {
+            return IconToByteArray(new Icon(filename));
+        }
+
+        private byte[] IconToByteArray(Icon icondata)
+        {
+            using (MemoryStream ms = new MemoryStream())
+            {
+                Icon ic = new Icon(icondata, 48, 48);
+                ic.ToBitmap().Save(ms, ImageFormat.Png);
+                return ms.ToArray();
+            }
+        }
+
+        public static bool IsDllExists
+        {
+            get
+            {
+                string dir = Application.StartupPath;
+                string connectorPath = Path.Combine(dir, "Growl.Connector.dll");
+                string corePath = Path.Combine(dir, "Growl.CoreLibrary.dll");
+                if (File.Exists(connectorPath) && File.Exists(corePath))
+                    return true;
+                else
+                    return false;
+            }
+        }
+
+        public bool RegisterGrowl()
+        {
+            _initialized = false;
+            string dir = Application.StartupPath;
+            string connectorPath = Path.Combine(dir, "Growl.Connector.dll");
+            string corePath = Path.Combine(dir, "Growl.CoreLibrary.dll");
+
+            try
+            {
+                if (!IsDllExists) return false;
+                _connector = Assembly.LoadFile(connectorPath);
+                _core = Assembly.LoadFile(corePath);
+            }
+            catch (Exception)
+            {
+                return false;
+            }
+
+            try
+            {
+                _targetConnector = _connector.CreateInstance("Growl.Connector.GrowlConnector");
+                _targetCore = _core.CreateInstance("Growl.CoreLibrary");
+                Type _t = _connector.GetType("Growl.Connector.NotificationType");
+
+                _growlNTreply = _t.InvokeMember(null,
+                    BindingFlags.CreateInstance, null, null, new object[] { "REPLY", "Reply" });
+
+                _growlNTdm = _t.InvokeMember(null,
+                    BindingFlags.CreateInstance, null, null, new object[] { "DIRECT_MESSAGE", "DirectMessage" });
+
+                _growlNTnew = _t.InvokeMember(null,
+                    BindingFlags.CreateInstance, null, null, new object[] { "NOTIFY", "新着通知" });
+
+                _growlNTusevent = _t.InvokeMember(null,
+                    BindingFlags.CreateInstance, null, null, new object[] { "USERSTREAM_EVENT", "UserStream Event" });
+
+                object encryptType =
+                        _connector.GetType("Growl.Connector.Cryptography+SymmetricAlgorithmType").InvokeMember(
+                            "PlainText", BindingFlags.GetField, null, null, null);
+                _targetConnector.GetType().InvokeMember("EncryptionAlgorithm", BindingFlags.SetProperty, null, _targetConnector, new object[] { encryptType });
+
+                _growlApp = _connector.CreateInstance(
+                    "Growl.Connector.Application", false, BindingFlags.Default, null, new object[] { _appName }, null, null);
+
+
+                if (File.Exists(Path.Combine(Application.StartupPath, "Icons\\Tween.png")))
+                {
+                    // Icons\Tween.pngを使用
+                    ConstructorInfo ci = _core.GetType(
+                        "Growl.CoreLibrary.Resource").GetConstructor(
+                        BindingFlags.NonPublic | BindingFlags.Instance,
+                        null, new Type[] { typeof(string) }, null);
+
+                    object data = ci.Invoke(new object[] { Path.Combine(Application.StartupPath, "Icons\\Tween.png") });
+                    PropertyInfo pi = _growlApp.GetType().GetProperty("Icon");
+                    pi.SetValue(_growlApp, data, null);
+
+                }
+                else if (File.Exists(Path.Combine(Application.StartupPath, "Icons\\MIcon.ico")))
+                {
+                    // アイコンセットにMIcon.icoが存在する場合それを使用
+                    ConstructorInfo cibd = _core.GetType(
+                        "Growl.CoreLibrary.BinaryData").GetConstructor(
+                        BindingFlags.Public | BindingFlags.Instance,
+                        null, new Type[] { typeof(byte[]) }, null);
+                    TypeConverter tc = new TypeConverter();
+                    object bdata = cibd.Invoke(
+                        new object[] { IconToByteArray(Path.Combine(Application.StartupPath, "Icons\\MIcon.ico")) });
+
+                    ConstructorInfo ciRes = _core.GetType(
+                        "Growl.CoreLibrary.Resource").GetConstructor(
+                        BindingFlags.NonPublic | BindingFlags.Instance,
+                        null, new Type[] { bdata.GetType() }, null);
+
+                    object data = ciRes.Invoke(new object[] { bdata });
+                    PropertyInfo pi = _growlApp.GetType().GetProperty("Icon");
+                    pi.SetValue(_growlApp, data, null);
+                }
+                else
+                {
+                    //内蔵アイコンリソースを使用
+                    ConstructorInfo cibd = _core.GetType(
+                        "Growl.CoreLibrary.BinaryData").GetConstructor(
+                        BindingFlags.Public | BindingFlags.Instance,
+                        null, new Type[] { typeof(byte[]) }, null);
+                    TypeConverter tc = new TypeConverter();
+                    object bdata = cibd.Invoke(
+                        new object[] { IconToByteArray(Properties.Resources.MIcon) });
+
+                    ConstructorInfo ciRes = _core.GetType(
+                        "Growl.CoreLibrary.Resource").GetConstructor(
+                        BindingFlags.NonPublic | BindingFlags.Instance,
+                        null, new Type[] { bdata.GetType() }, null);
+
+                    object data = ciRes.Invoke(new object[] { bdata });
+                    PropertyInfo pi = _growlApp.GetType().GetProperty("Icon");
+                    pi.SetValue(_growlApp, data, null);
+                }
+
+                MethodInfo mi = _targetConnector.GetType().GetMethod("Register", new Type[] { _growlApp.GetType(), _connector.GetType("Growl.Connector.NotificationType[]") });
+
+                _t = _connector.GetType("Growl.Connector.NotificationType");
+
+                ArrayList arglist = new ArrayList();
+                arglist.Add(_growlNTreply);
+                arglist.Add(_growlNTdm);
+                arglist.Add(_growlNTnew);
+                arglist.Add(_growlNTusevent);
+
+                mi.Invoke(_targetConnector, new object[] { _growlApp, arglist.ToArray(_t) });
+
+                // コールバックメソッドの登録
+                Type tGrowlConnector = _connector.GetType("Growl.Connector.GrowlConnector");
+                EventInfo evNotificationCallback = tGrowlConnector.GetEvent("NotificationCallback");
+                Type tDelegate = evNotificationCallback.EventHandlerType;
+                MethodInfo miHandler = typeof(GrowlHelper).GetMethod("GrowlCallbackHandler", BindingFlags.NonPublic | BindingFlags.Instance);
+                Delegate d = Delegate.CreateDelegate(tDelegate, this, miHandler);
+                MethodInfo miAddHandler = evNotificationCallback.GetAddMethod();
+                object[] addHandlerArgs = { d };
+                miAddHandler.Invoke(_targetConnector, addHandlerArgs);
+
+                _initialized = true;
+            }
+            catch (Exception)
+            {
+                _initialized = false;
+                return false;
+            }
+
+            return true;
+        }
+
+        public void Notify(NotifyType notificationType, string id, string title, string text, Image icon = null, string url = "")
+        {
+            if (!_initialized) return;
+            string notificationName = "";
+            switch (notificationType)
+            {
+                case NotifyType.Reply:
+                    notificationName = "REPLY";
+                    break;
+                case NotifyType.DirectMessage:
+                    notificationName = "DIRECT_MESSAGE";
+                    break;
+                case NotifyType.Notify:
+                    notificationName = "NOTIFY";
+                    break;
+                case NotifyType.UserStreamEvent:
+                    notificationName = "USERSTREAM_EVENT";
+                    break;
+            }
+            object n = null;
+            if (icon != null || !string.IsNullOrEmpty(url))
+            {
+                Type gCore = _core.GetType("Growl.CoreLibrary.Resource");
+                object res = null;
+                if (icon != null)
+                {
+                    res = gCore.InvokeMember("op_Implicit",
+                                             BindingFlags.Public | BindingFlags.Static | BindingFlags.InvokeMethod,
+                                             null,
+                                             null,
+                                             new object[] { icon });
+                }
+                else
+                {
+                    res = gCore.InvokeMember("op_Implicit",
+                                             BindingFlags.Public | BindingFlags.Static | BindingFlags.InvokeMethod,
+                                             null,
+                                             null,
+                                             new object[] { url });
+                }
+                object priority =
+                        _connector.GetType("Growl.Connector.Priority").InvokeMember(
+                            "Normal", BindingFlags.GetField, null, null, null);
+                n = _connector.GetType("Growl.Connector.Notification").InvokeMember(
+                        "Notification",
+                        BindingFlags.CreateInstance,
+                        null,
+                        _connector,
+                        new object[] {_appName,
+                                      notificationName,
+                                      id,
+                                      title,
+                                      text,
+                                      res,
+                                      false,
+                                      priority,
+                                      "aaa"});
+            }
+            else
+            {
+                n = _connector.GetType("Growl.Connector.Notification").InvokeMember(
+                        "Notification",
+                        BindingFlags.CreateInstance,
+                        null,
+                        _connector,
+                        new object[] {_appName,
+                                      notificationName,
+                                      id,
+                                      title,
+                                      text});
+            }
+            //_targetConnector.GetType.InvokeMember("Notify", BindingFlags.InvokeMethod, null, _targetConnector, new object[] {n});
+            object cc = _connector.GetType("Growl.Connector.CallbackContext").InvokeMember(
+                null, BindingFlags.CreateInstance, null, _connector,
+                new object[] { "some fake information", notificationName });
+            _targetConnector.GetType().InvokeMember("Notify", BindingFlags.InvokeMethod, null, _targetConnector, new object[] { n, cc });
+        }
+
+        private void GrowlCallbackHandler(object response, object callbackData, object state)
+        {
+            try
+            {
+                // 定数取得
+                object vCLICK =
+                _core.GetType("Growl.CoreLibrary.CallbackResult").GetField(
+                            "CLICK",
+                           BindingFlags.Public | BindingFlags.Static).GetRawConstantValue();
+                // 実際の値
+                object vResult = callbackData.GetType().GetProperty(
+                            "Result",
+                            BindingFlags.Public | BindingFlags.Instance).GetGetMethod().Invoke(callbackData, null);
+                vResult = (int)vResult;
+                string notifyId = (string)callbackData.GetType().GetProperty("NotificationID").GetGetMethod().Invoke(callbackData, null);
+                string notifyName = (string)callbackData.GetType().GetProperty("Type").GetGetMethod().Invoke(callbackData, null);
+                if (vCLICK.Equals(vResult))
+                {
+                    NotifyType nt;
+                    switch (notifyName)
+                    {
+                        case "REPLY":
+                            nt = NotifyType.Reply;
+                            break;
+                        case "DIRECT_MESSAGE":
+                            nt = NotifyType.DirectMessage;
+                            break;
+                        case "NOTIFY":
+                            nt = NotifyType.Notify;
+                            break;
+                        case "USERSTREAM_EVENT":
+                            nt = NotifyType.UserStreamEvent;
+                            break;
+                        default:
+                            return;
+                    }
+
+                    if (NotifyClicked != null)
+                        NotifyClicked(this, new NotifyCallbackEventArgs(nt, notifyId));
+                }
+            }
+            catch (Exception)
+            {
+                return;
+            }
+        }
+    }
+}
index 5cce143..6e7ee27 100644 (file)
@@ -69,6 +69,7 @@
     <Compile Include="DetailsListView.cs">
       <SubType>Component</SubType>
     </Compile>
+    <Compile Include="Growl.cs" />
     <Compile Include="MyCommon.cs" />
     <Compile Include="OpenURL.cs">
       <SubType>Form</SubType>