private int EventAdded(IntPtr wParam, IntPtr lParam) { IntPtr hContact = wParam; IntPtr hDBEvent = lParam; int blobSize = this.CallService("DB/Event/GetBlobSize", hDBEvent, IntPtr.Zero).ToInt32(); using (var pBlob = new AutoPtr(Marshal.AllocHGlobal(blobSize))) using (var pDBEventInfo = new AutoPtr( Marshal.AllocHGlobal(Marshal.SizeOf(typeof (DBEventInfo))))) { var eventInfo = new DBEventInfo(); eventInfo.pBlob = pBlob; eventInfo.cbBlob = (uint)blobSize; Marshal.StructureToPtr(eventInfo, pDBEventInfo, false); this.CallService("DB/Event/Get", hDBEvent, pDBEventInfo); eventInfo = (DBEventInfo)Marshal.PtrToStructure(pDBEventInfo, typeof (DBEventInfo)); if (eventInfo.eventType == DBEventInfo.EVENTTYPE_MESSAGE) { using (var pDBEventGetText = new AutoPtr( Marshal.AllocHGlobal(Marshal.SizeOf( typeof (DBEventGetText))))) { var getText = new DBEventGetText(); getText.dbei = pDBEventInfo; getText.datatype = Utils.DBVT_ASCIIZ; getText.codepage = 1251; Marshal.StructureToPtr(getText, pDBEventGetText, false); IntPtr pString = this.CallService( "DB/Event/GetText", IntPtr.Zero, pDBEventGetText); string message = Marshal.PtrToStringAnsi(pString); mmi.mmi_free(pString); var contact = new Contact(hContact); DateTime eventTime = new DateTime(1970, 1, 1). AddSeconds(eventInfo.timestamp).ToUniversalTime(); if ((eventInfo.flags & DBEventInfo.DBEF_SENT) != 0) { if (MessageSentEvent != null) MessageSentEvent(contact, eventTime, message); } else { if (MessageReceivedEvent != null) MessageReceivedEvent(contact, eventTime, message); } } } } return 0; }
/// <summary> /// Creates instance of history item and loads its content from /// database. /// </summary> /// <param name="contact"> /// Contact with whom history this item is associated. /// </param> /// <param name="hEvent"> /// Miranda event handle. /// </param> internal static HistoryItem Load(Contact contact, IntPtr hEvent) { if (hEvent == IntPtr.Zero) throw new ArgumentException("hEvent cannot be zero."); // Miranda interface for freeing strings: var mmi = MMInterface.GetMMI(); using (var pDbEventInfo = new AutoPtr(Marshal.AllocHGlobal( Marshal.SizeOf(typeof (DBEventInfo))))) { var result = Plugin.m_CallService( "DB/Event/Get", hEvent, pDbEventInfo); if (result != IntPtr.Zero) throw new DatabaseException(); var eventInfo = (DBEventInfo) Marshal.PtrToStructure(pDbEventInfo, typeof (DBEventInfo)); var type = (HistoryItemType)eventInfo.eventType; var direction = (eventInfo.flags & DBEventInfo.DBEF_SENT) != 0 ? HistoryItemDirection.Outgoing : HistoryItemDirection.Incoming; var historyItem = new HistoryItem(type, direction); if (type == HistoryItemType.Message) { // Get message text: using (var pDbEventGetText = new AutoPtr( Marshal.AllocHGlobal(Marshal.SizeOf( typeof (DBEventGetText))))) { var getText = new DBEventGetText(); getText.dbei = pDbEventInfo; getText.datatype = Utils.DBVT_WCHAR; Marshal.StructureToPtr(getText, pDbEventGetText, false); var pString = Plugin.m_CallService( "DB/Event/GetText", IntPtr.Zero, pDbEventGetText); mmi.mmi_free(pString); var message = Marshal.PtrToStringUni(pString); historyItem.MessageText = message; } } historyItem.Contact = contact; historyItem.DateTime = new DateTime(1970, 1, 1) .AddSeconds(eventInfo.timestamp); return historyItem; } }