diff --git a/src/windowmessages.cpp b/src/windowmessages.cpp
index da6ee34..5508d91 100644
--- a/src/windowmessages.cpp
+++ b/src/windowmessages.cpp
@@ -1,277 +1,277 @@
/* @file windowmessages.h
* @brief Helper class to work with the windowmessage handler thread.
*
* Copyright (C) 2015, 2016 by Bundesamt für Sicherheit in der Informationstechnik
* Software engineering by Intevation GmbH
*
* This file is part of GpgOL.
*
* GpgOL is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* GpgOL 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, see .
*/
#include "windowmessages.h"
#include "common.h"
#include "oomhelp.h"
#include "mail.h"
#include "gpgoladdin.h"
#include
#define RESPONDER_CLASS_NAME "GpgOLResponder"
/* Singleton window */
static HWND g_responder_window = NULL;
LONG_PTR WINAPI
gpgol_window_proc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
- if (message == WM_USER + 1)
+ if (message == WM_USER + 42)
{
wm_ctx_t *ctx = (wm_ctx_t *) lParam;
log_debug ("%s:%s: Recieved user msg: %i",
SRCNAME, __func__, ctx->wmsg_type);
switch (ctx->wmsg_type)
{
case (PARSING_DONE):
{
auto mail = (Mail*) ctx->data;
if (!Mail::is_valid_ptr (mail))
{
log_debug ("%s:%s: Parsing done for mail which is gone.",
SRCNAME, __func__);
break;
}
mail->parsing_done();
break;
}
case (RECIPIENT_ADDED):
{
auto mail = (Mail*) ctx->data;
if (!Mail::is_valid_ptr (mail))
{
log_debug ("%s:%s: Recipient add for mail which is gone.",
SRCNAME, __func__);
break;
}
mail->locate_keys();
break;
}
case (INVALIDATE_UI):
{
log_debug ("%s:%s: Invalidating UI",
SRCNAME, __func__);
gpgoladdin_invalidate_ui();
log_debug ("%s:%s: Invalidation done",
SRCNAME, __func__);
break;
}
case (CLOSE):
{
auto mail = (Mail*) ctx->data;
if (!Mail::is_valid_ptr (mail))
{
log_debug ("%s:%s: Close for mail which is gone.",
SRCNAME, __func__);
break;
}
Mail::close (mail);
break;
}
default:
log_debug ("Unknown msg");
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
HWND
create_responder_window ()
{
size_t cls_name_len = strlen(RESPONDER_CLASS_NAME) + 1;
char cls_name[cls_name_len];
if (g_responder_window)
{
return g_responder_window;
}
/* Create Window wants a mutable string as the first parameter */
snprintf (cls_name, cls_name_len, "%s", RESPONDER_CLASS_NAME);
WNDCLASS windowClass;
windowClass.style = CS_GLOBALCLASS | CS_DBLCLKS;
windowClass.lpfnWndProc = gpgol_window_proc;
windowClass.cbClsExtra = 0;
windowClass.cbWndExtra = 0;
windowClass.hInstance = (HINSTANCE) GetModuleHandle(NULL);
windowClass.hIcon = 0;
windowClass.hCursor = 0;
windowClass.hbrBackground = 0;
windowClass.lpszMenuName = 0;
windowClass.lpszClassName = cls_name;
RegisterClass(&windowClass);
g_responder_window = CreateWindow (cls_name, RESPONDER_CLASS_NAME, 0, 0, 0,
0, 0, 0, (HMENU) 0,
(HINSTANCE) GetModuleHandle(NULL), 0);
return g_responder_window;
}
int
send_msg_to_ui_thread (wm_ctx_t *ctx)
{
size_t cls_name_len = strlen(RESPONDER_CLASS_NAME) + 1;
char cls_name[cls_name_len];
snprintf (cls_name, cls_name_len, "%s", RESPONDER_CLASS_NAME);
HWND responder = FindWindow (cls_name, RESPONDER_CLASS_NAME);
if (!responder)
{
log_error ("%s:%s: Failed to find responder window.",
SRCNAME, __func__);
return -1;
}
- SendMessage (responder, WM_USER + 1, 0, (LPARAM) ctx);
+ SendMessage (responder, WM_USER + 42, 0, (LPARAM) ctx);
return 0;
}
int
do_in_ui_thread (gpgol_wmsg_type type, void *data)
{
wm_ctx_t ctx = {NULL, UNKNOWN, 0};
ctx.wmsg_type = type;
ctx.data = data;
if (send_msg_to_ui_thread (&ctx))
{
return -1;
}
return ctx.err;
}
static std::vector explorers;
void
add_explorer (LPDISPATCH explorer)
{
explorers.push_back (explorer);
}
void remove_explorer (LPDISPATCH explorer)
{
explorers.erase(std::remove(explorers.begin(),
explorers.end(),
explorer),
explorers.end());
}
LRESULT CALLBACK
gpgol_hook(int code, WPARAM wParam, LPARAM lParam)
{
/* Once we are in the close events we don't have enough
control to revert all our changes so we have to do it
with this nice little hack by catching the WM_CLOSE message
before it reaches outlook. */
LPCWPSTRUCT cwp = (LPCWPSTRUCT) lParam;
switch (cwp->message)
{
case WM_CLOSE:
{
HWND lastChild = NULL;
for (const auto explorer: explorers)
{
/* Casting to LPOLEWINDOW and calling GetWindow
succeeded in Outlook 2016 but always returned
the number 1. So we need this hack. */
char *caption = get_oom_string (explorer, "Caption");
if (!caption)
{
log_debug ("%s:%s: No caption.",
SRCNAME, __func__);
continue;
}
/* rctrl_renwnd32 is the window class of outlook. */
HWND hwnd = FindWindowExA(NULL, lastChild, "rctrl_renwnd32",
caption);
xfree (caption);
lastChild = hwnd;
if (hwnd == cwp->hwnd)
{
log_debug ("%s:%s: WM_CLOSE windowmessage for explorer. "
"Closing all mails.",
SRCNAME, __func__);
Mail::close_all_mails();
break;
}
}
break;
}
case WM_SYSCOMMAND:
/*
This comes to often and when we are closed from the icon
we also get WM_CLOSE
if (cwp->wParam == SC_CLOSE)
{
log_debug ("%s:%s: SC_CLOSE syscommand. Closing all mails.",
SRCNAME, __func__);
Mail::close_all_mails();
} */
break;
default:
break;
}
return CallNextHookEx (NULL, code, wParam, lParam);
}
/* Create the message hook for outlook's windowmessages
we are especially interested in WM_QUIT to do cleanups
and prevent the "Item has changed" question. */
HHOOK
create_message_hook()
{
return SetWindowsHookEx (WH_CALLWNDPROC,
gpgol_hook,
NULL,
GetCurrentThreadId());
}
gpgrt_lock_t invalidate_lock = GPGRT_LOCK_INITIALIZER;
static bool invalidation_in_progress;
DWORD WINAPI
delayed_invalidate_ui (LPVOID)
{
if (invalidation_in_progress)
{
log_debug ("%s:%s: Invalidation canceled as it is in progress.",
SRCNAME, __func__);
return 0;
}
gpgrt_lock_lock(&invalidate_lock);
invalidation_in_progress = true;
/* We sleep here a bit to prevent invalidation immediately
after the selection change before we have started processing
the mail. */
Sleep (250);
do_in_ui_thread (INVALIDATE_UI, nullptr);
invalidation_in_progress = false;
gpgrt_lock_unlock(&invalidate_lock);
return 0;
}
DWORD WINAPI
close_mail (LPVOID mail)
{
do_in_ui_thread (CLOSE, mail);
return 0;
}
diff --git a/src/windowmessages.h b/src/windowmessages.h
index 584ff68..014c7d9 100644
--- a/src/windowmessages.h
+++ b/src/windowmessages.h
@@ -1,90 +1,90 @@
/* windowmessages.h - Helper functions for Window message exchange.
* Copyright (C) 2015 by Bundesamt für Sicherheit in der Informationstechnik
* Software engineering by Intevation GmbH
*
* This file is part of GpgOL.
*
* GpgOL is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* GpgOL 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, see .
*/
#ifndef WINDOWMESSAGES_H
#define WINDOWMESSAGES_H
#include
#include "config.h"
#include
/** Window Message handling for GpgOL.
In Outlook only one thread has access to the Outlook Object model
and this is the UI Thread. We can work in other threads but
to do something with outlooks data we neet to be in the UI Thread.
So we create a hidden Window in this thread and use the fact
that SendMessage handles Window messages in the thread where the
Window was created.
This way we can go back to interactct with the Outlook from another
thread without working with COM Multithreading / Marshaling.
The Responder Window should be initalized on startup.
*/
typedef enum _gpgol_wmsg_type
{
- UNKNOWN = 0,
- INVALIDATE_UI = 1, /* The UI should be invalidated. */
- PARSING_DONE = 2, /* A mail was parsed. Data should be a pointer
+ UNKNOWN = 1100, /* A large offset to avoid conflicts */
+ INVALIDATE_UI, /* The UI should be invalidated. */
+ PARSING_DONE, /* A mail was parsed. Data should be a pointer
to the mail object. */
- RECIPIENT_ADDED = 3, /* A recipient was added. Data should be ptr
+ RECIPIENT_ADDED, /* A recipient was added. Data should be ptr
to mail */
- CLOSE = 4, /* Send the message in the next event loop. */
+ CLOSE, /* Send the message in the next event loop. */
} gpgol_wmsg_type;
typedef struct
{
void *data; /* Pointer to arbitrary data depending on msg type */
gpgol_wmsg_type wmsg_type; /* Type of the msg. */
int err; /* Set to true on error */
} wm_ctx_t;
/** Create and register the responder window.
The responder window should be */
HWND
create_responder_window ();
/** Send a message to the UI thread through the responder Window.
Returns 0 on success. */
int
send_msg_to_ui_thread (wm_ctx_t *ctx);
/** Uses send_msg_to_ui_thread to execute the request
in the ui thread. Returns the result. */
int
do_in_ui_thread (gpgol_wmsg_type type, void *data);
/** Create our filter before outlook Window Messages. */
HHOOK
create_message_hook();
DWORD WINAPI
delayed_invalidate_ui (LPVOID);
DWORD WINAPI
close_mail (LPVOID);
void add_explorer (LPDISPATCH explorer);
void remove_explorer (LPDISPATCH explorer);
/* The lock to invalide the ui */
extern gpgrt_lock_t invalidate_lock;
#endif // WINDOWMESSAGES_H