2 * Implementation of the Local Printmonitor User Interface
4 * Copyright 2007 Detlef Riekenberg
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
30 #include "ddk/winsplp.h"
32 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(localui
);
37 /*****************************************************/
39 static HINSTANCE LOCALUI_hInstance
;
41 static const WCHAR cmd_AddPortW
[] = {'A','d','d','P','o','r','t',0};
42 static const WCHAR cmd_ConfigureLPTPortCommandOKW
[] = {'C','o','n','f','i','g','u','r','e',
43 'L','P','T','P','o','r','t',
44 'C','o','m','m','a','n','d','O','K',0};
45 static const WCHAR cmd_DeletePortW
[] = {'D','e','l','e','t','e','P','o','r','t',0};
46 static const WCHAR cmd_GetDefaultCommConfigW
[] = {'G','e','t',
47 'D','e','f','a','u','l','t',
48 'C','o','m','m','C','o','n','f','i','g',0};
49 static const WCHAR cmd_GetTransmissionRetryTimeoutW
[] = {'G','e','t',
50 'T','r','a','n','s','m','i','s','s','i','o','n',
51 'R','e','t','r','y','T','i','m','e','o','u','t',0};
52 static const WCHAR cmd_PortIsValidW
[] = {'P','o','r','t','I','s','V','a','l','i','d',0};
53 static const WCHAR cmd_SetDefaultCommConfigW
[] = {'S','e','t',
54 'D','e','f','a','u','l','t',
55 'C','o','m','m','C','o','n','f','i','g',0};
57 static const WCHAR fmt_uW
[] = {'%','u',0};
58 static const WCHAR portname_LPT
[] = {'L','P','T',0};
59 static const WCHAR portname_COM
[] = {'C','O','M',0};
60 static const WCHAR portname_FILE
[] = {'F','I','L','E',':',0};
61 static const WCHAR portname_CUPS
[] = {'C','U','P','S',':',0};
62 static const WCHAR portname_LPR
[] = {'L','P','R',':',0};
64 static const WCHAR XcvMonitorW
[] = {',','X','c','v','M','o','n','i','t','o','r',' ',0};
65 static const WCHAR XcvPortW
[] = {',','X','c','v','P','o','r','t',' ',0};
67 /*****************************************************/
69 typedef struct tag_addportui_t
{
74 typedef struct tag_lptconfig_t
{
80 static INT_PTR CALLBACK
dlgproc_lptconfig(HWND hwnd
, UINT msg
, WPARAM wparam
, LPARAM lparam
);
82 /*****************************************************
86 static LPWSTR
strdupWW(LPCWSTR pPrefix
, LPCWSTR pSuffix
)
91 len
= lstrlenW(pPrefix
) + (pSuffix
? lstrlenW(pSuffix
) : 0) + 1;
92 ptr
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
94 lstrcpyW(ptr
, pPrefix
);
95 if (pSuffix
) lstrcatW(ptr
, pSuffix
);
100 /*****************************************************
101 * dlg_configure_com [internal]
105 static BOOL
dlg_configure_com(HANDLE hXcv
, HWND hWnd
, PCWSTR pPortName
)
114 /* strip the colon (pPortName is never empty here) */
115 len
= lstrlenW(pPortName
);
116 shortname
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
118 memcpy(shortname
, pPortName
, (len
-1) * sizeof(WCHAR
));
119 shortname
[len
-1] = '\0';
121 /* get current settings */
122 len
= FIELD_OFFSET(COMMCONFIG
, wcProviderData
[1]);
123 status
= ERROR_SUCCESS
;
124 res
= XcvDataW( hXcv
, cmd_GetDefaultCommConfigW
,
126 (lstrlenW(shortname
) +1) * sizeof(WCHAR
),
127 (PBYTE
) &cfg
, len
, &len
, &status
);
129 if (res
&& (status
== ERROR_SUCCESS
)) {
130 /* display the Dialog */
131 res
= CommConfigDialogW(pPortName
, hWnd
, &cfg
);
133 status
= ERROR_SUCCESS
;
134 /* set new settings */
135 res
= XcvDataW(hXcv
, cmd_SetDefaultCommConfigW
,
137 (PBYTE
) &dummy
, 0, &len
, &status
);
140 HeapFree(GetProcessHeap(), 0, shortname
);
147 /*****************************************************
148 * dlg_configure_lpt [internal]
152 static BOOL
dlg_configure_lpt(HANDLE hXcv
, HWND hWnd
)
160 res
= DialogBoxParamW(LOCALUI_hInstance
, MAKEINTRESOURCEW(LPTCONFIG_DIALOG
), hWnd
,
161 dlgproc_lptconfig
, (LPARAM
) &data
);
163 TRACE("got %u with %u\n", res
, GetLastError());
165 if (!res
) SetLastError(ERROR_CANCELLED
);
169 /******************************************************************
170 * dlg_port_already_exists [internal]
173 static void dlg_port_already_exists(HWND hWnd
, LPCWSTR portname
)
175 WCHAR res_PortW
[IDS_LOCALPORT_MAXLEN
];
176 WCHAR res_PortExistsW
[IDS_PORTEXISTS_MAXLEN
];
181 res_PortExistsW
[0] = '\0';
182 LoadStringW(LOCALUI_hInstance
, IDS_LOCALPORT
, res_PortW
, IDS_LOCALPORT_MAXLEN
);
183 LoadStringW(LOCALUI_hInstance
, IDS_PORTEXISTS
, res_PortExistsW
, IDS_PORTEXISTS_MAXLEN
);
185 len
= lstrlenW(portname
) + IDS_PORTEXISTS_MAXLEN
+ 1;
186 message
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
189 swprintf(message
, len
, res_PortExistsW
, portname
);
190 MessageBoxW(hWnd
, message
, res_PortW
, MB_OK
| MB_ICONERROR
);
191 HeapFree(GetProcessHeap(), 0, message
);
195 /******************************************************************
196 * dlg_invalid_portname [internal]
199 static void dlg_invalid_portname(HWND hWnd
, LPCWSTR portname
)
201 WCHAR res_PortW
[IDS_LOCALPORT_MAXLEN
];
202 WCHAR res_InvalidNameW
[IDS_INVALIDNAME_MAXLEN
];
207 res_InvalidNameW
[0] = '\0';
208 LoadStringW(LOCALUI_hInstance
, IDS_LOCALPORT
, res_PortW
, IDS_LOCALPORT_MAXLEN
);
209 LoadStringW(LOCALUI_hInstance
, IDS_INVALIDNAME
, res_InvalidNameW
, IDS_INVALIDNAME_MAXLEN
);
211 len
= lstrlenW(portname
) + IDS_INVALIDNAME_MAXLEN
;
212 message
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
215 swprintf(message
, len
, res_InvalidNameW
, portname
);
216 MessageBoxW(hWnd
, message
, res_PortW
, MB_OK
| MB_ICONERROR
);
217 HeapFree(GetProcessHeap(), 0, message
);
221 /******************************************************************
222 * display the Dialog "Nothing to configure"
226 static void dlg_nothingtoconfig(HWND hWnd
)
228 WCHAR res_PortW
[IDS_LOCALPORT_MAXLEN
];
229 WCHAR res_nothingW
[IDS_NOTHINGTOCONFIG_MAXLEN
];
232 res_nothingW
[0] = '\0';
233 LoadStringW(LOCALUI_hInstance
, IDS_LOCALPORT
, res_PortW
, IDS_LOCALPORT_MAXLEN
);
234 LoadStringW(LOCALUI_hInstance
, IDS_NOTHINGTOCONFIG
, res_nothingW
, IDS_NOTHINGTOCONFIG_MAXLEN
);
236 MessageBoxW(hWnd
, res_nothingW
, res_PortW
, MB_OK
| MB_ICONINFORMATION
);
239 /******************************************************************
240 * dlg_win32error [internal]
243 static void dlg_win32error(HWND hWnd
, DWORD lasterror
)
245 WCHAR res_PortW
[IDS_LOCALPORT_MAXLEN
];
246 LPWSTR message
= NULL
;
250 LoadStringW(LOCALUI_hInstance
, IDS_LOCALPORT
, res_PortW
, IDS_LOCALPORT_MAXLEN
);
253 res
= FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM
,
254 NULL
, lasterror
, 0, (LPWSTR
) &message
, 0, NULL
);
257 MessageBoxW(hWnd
, message
, res_PortW
, MB_OK
| MB_ICONERROR
);
262 /*****************************************************************************
266 static INT_PTR CALLBACK
dlgproc_addport(HWND hwnd
, UINT msg
, WPARAM wparam
, LPARAM lparam
)
277 SetWindowLongPtrW(hwnd
, DWLP_USER
, lparam
);
281 if (wparam
== MAKEWPARAM(IDOK
, BN_CLICKED
))
283 data
= (addportui_t
*) GetWindowLongPtrW(hwnd
, DWLP_USER
);
284 /* length in WCHAR, without the '\0' */
285 len
= SendDlgItemMessageW(hwnd
, ADDPORT_EDIT
, WM_GETTEXTLENGTH
, 0, 0);
286 data
->portname
= HeapAlloc(GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
));
288 if (!data
->portname
) {
289 EndDialog(hwnd
, FALSE
);
292 /* length is in WCHAR, including the '\0' */
293 GetDlgItemTextW(hwnd
, ADDPORT_EDIT
, data
->portname
, len
+ 1);
294 status
= ERROR_SUCCESS
;
295 res
= XcvDataW( data
->hXcv
, cmd_PortIsValidW
, (PBYTE
) data
->portname
,
296 (lstrlenW(data
->portname
) + 1) * sizeof(WCHAR
),
297 (PBYTE
) &dummy
, 0, &len
, &status
);
299 TRACE("got %u with status %u\n", res
, status
);
300 if (res
&& (status
== ERROR_SUCCESS
)) {
301 /* The caller must free data->portname */
302 EndDialog(hwnd
, TRUE
);
306 if (res
&& (status
== ERROR_INVALID_NAME
)) {
307 dlg_invalid_portname(hwnd
, data
->portname
);
308 HeapFree(GetProcessHeap(), 0, data
->portname
);
309 data
->portname
= NULL
;
313 dlg_win32error(hwnd
, status
);
314 HeapFree(GetProcessHeap(), 0, data
->portname
);
315 data
->portname
= NULL
;
319 if (wparam
== MAKEWPARAM(IDCANCEL
, BN_CLICKED
))
321 EndDialog(hwnd
, FALSE
);
329 /*****************************************************************************
330 * dlgproc_lptconfig [internal]
332 * Our message-proc is simple, as the range-check is done only during the
333 * command "OK" and the dialog is set to the start-value at "out of range".
335 * Native localui.dll does the check during keyboard-input and set the dialog
336 * to the previous value.
340 static INT_PTR CALLBACK
dlgproc_lptconfig(HWND hwnd
, UINT msg
, WPARAM wparam
, LPARAM lparam
)
353 SetWindowLongPtrW(hwnd
, DWLP_USER
, lparam
);
354 data
= (lptconfig_t
*) lparam
;
356 /* Get current setting */
358 status
= ERROR_SUCCESS
;
359 res
= XcvDataW( data
->hXcv
, cmd_GetTransmissionRetryTimeoutW
,
361 (PBYTE
) &data
->value
, sizeof(data
->value
), &len
, &status
);
363 TRACE("got %u with status %u\n", res
, status
);
365 /* Set current setting as the initial value in the Dialog */
366 SetDlgItemInt(hwnd
, LPTCONFIG_EDIT
, data
->value
, FALSE
);
370 if (wparam
== MAKEWPARAM(IDOK
, BN_CLICKED
))
372 data
= (lptconfig_t
*) GetWindowLongPtrW(hwnd
, DWLP_USER
);
375 res
= GetDlgItemInt(hwnd
, LPTCONFIG_EDIT
, (BOOL
*) &status
, FALSE
);
376 /* length is in WCHAR, including the '\0' */
377 GetDlgItemTextW(hwnd
, LPTCONFIG_EDIT
, bufferW
, ARRAY_SIZE(bufferW
));
378 TRACE("got %s and %u (translated: %u)\n", debugstr_w(bufferW
), res
, status
);
380 /* native localui.dll use the same limits */
381 if ((res
> 0) && (res
< 1000000) && status
) {
382 swprintf(bufferW
, ARRAY_SIZE(bufferW
), fmt_uW
, res
);
383 res
= XcvDataW( data
->hXcv
, cmd_ConfigureLPTPortCommandOKW
,
385 (lstrlenW(bufferW
) +1) * sizeof(WCHAR
),
386 (PBYTE
) &dummy
, 0, &len
, &status
);
388 TRACE("got %u with status %u\n", res
, status
);
389 EndDialog(hwnd
, TRUE
);
393 /* Set initial value and rerun the Dialog */
394 SetDlgItemInt(hwnd
, LPTCONFIG_EDIT
, data
->value
, FALSE
);
398 if (wparam
== MAKEWPARAM(IDCANCEL
, BN_CLICKED
))
400 EndDialog(hwnd
, FALSE
);
409 /*****************************************************
410 * get_type_from_name (internal)
414 static DWORD
get_type_from_name(LPCWSTR name
)
418 if (!wcsnicmp(name
, portname_LPT
, ARRAY_SIZE(portname_LPT
) -1))
421 if (!wcsnicmp(name
, portname_COM
, ARRAY_SIZE(portname_COM
) -1))
424 if (!wcsicmp(name
, portname_FILE
))
428 return PORT_IS_UNIXNAME
;
433 if (!wcsncmp(name
, portname_CUPS
, ARRAY_SIZE(portname_CUPS
) -1))
436 if (!wcsncmp(name
, portname_LPR
, ARRAY_SIZE(portname_LPR
) -1))
439 /* Must be a file or a directory. Does the file exist ? */
440 hfile
= CreateFileW(name
, GENERIC_WRITE
, 0, NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, NULL
);
441 TRACE("%p for OPEN_EXISTING on %s\n", hfile
, debugstr_w(name
));
442 if (hfile
== INVALID_HANDLE_VALUE
) {
443 /* Can we create the file? */
444 hfile
= CreateFileW(name
, GENERIC_WRITE
, 0, NULL
, OPEN_ALWAYS
, FILE_FLAG_DELETE_ON_CLOSE
, NULL
);
445 TRACE("%p for OPEN_ALWAYS\n", hfile
);
447 if (hfile
!= INVALID_HANDLE_VALUE
) {
449 return PORT_IS_FILENAME
;
451 /* We can't use the name. use GetLastError() for the reason */
452 return PORT_IS_UNKNOWN
;
455 /*****************************************************
456 * open_monitor_by_name [internal]
459 static BOOL
open_monitor_by_name(LPCWSTR pPrefix
, LPCWSTR pPort
, HANDLE
* phandle
)
461 PRINTER_DEFAULTSW pd
;
466 TRACE("(%s,%s)\n", debugstr_w(pPrefix
),debugstr_w(pPort
) );
468 fullname
= strdupWW(pPrefix
, pPort
);
471 pd
.DesiredAccess
= SERVER_ACCESS_ADMINISTER
;
473 res
= OpenPrinterW(fullname
, phandle
, &pd
);
474 HeapFree(GetProcessHeap(), 0, fullname
);
478 /*****************************************************
479 * localui_AddPortUI [exported through MONITORUI]
481 * Display a Dialog to add a local Port
484 * pName [I] Servername or NULL (local Computer)
485 * hWnd [I] Handle to parent Window for the Dialog-Box or NULL
486 * pMonitorName[I] Name of the Monitor, that should be used to add a Port or NULL
487 * ppPortName [O] PTR to PTR of a buffer, that receive the Name of the new Port or NULL
494 * The caller must free the buffer (returned in ppPortName) with GlobalFree().
495 * Native localui.dll failed with ERROR_INVALID_PARAMETER, when the user tried
496 * to add a Port, that start with "COM" or "LPT".
499 static BOOL WINAPI
localui_AddPortUI(PCWSTR pName
, HWND hWnd
, PCWSTR pMonitorName
, PWSTR
*ppPortName
)
508 TRACE( "(%s, %p, %s, %p) (*ppPortName: %p)\n", debugstr_w(pName
), hWnd
,
509 debugstr_w(pMonitorName
), ppPortName
, ppPortName
? *ppPortName
: NULL
);
511 if (open_monitor_by_name(XcvMonitorW
, pMonitorName
, &hXcv
)) {
513 ZeroMemory(&data
, sizeof(addportui_t
));
515 res
= DialogBoxParamW(LOCALUI_hInstance
, MAKEINTRESOURCEW(ADDPORT_DIALOG
), hWnd
,
516 dlgproc_addport
, (LPARAM
) &data
);
518 TRACE("got %u with %u for %s\n", res
, GetLastError(), debugstr_w(data
.portname
));
520 if (ppPortName
) *ppPortName
= NULL
;
523 res
= XcvDataW(hXcv
, cmd_AddPortW
, (PBYTE
) data
.portname
,
524 (lstrlenW(data
.portname
)+1) * sizeof(WCHAR
),
525 (PBYTE
) &dummy
, 0, &needed
, &status
);
527 TRACE("got %u with status %u\n", res
, status
);
528 if (res
&& (status
== ERROR_SUCCESS
) && ppPortName
) {
529 /* Native localui uses GlobalAlloc also.
530 The caller must GlobalFree the buffer */
531 *ppPortName
= GlobalAlloc(GPTR
, (lstrlenW(data
.portname
)+1) * sizeof(WCHAR
));
532 if (*ppPortName
) lstrcpyW(*ppPortName
, data
.portname
);
535 if (res
&& (status
== ERROR_ALREADY_EXISTS
)) {
536 dlg_port_already_exists(hWnd
, data
.portname
);
537 /* Native localui also return "TRUE" from AddPortUI in this case */
540 HeapFree(GetProcessHeap(), 0, data
.portname
);
544 SetLastError(ERROR_CANCELLED
);
549 TRACE("=> %u with %u\n", res
, GetLastError());
554 /*****************************************************
555 * localui_ConfigurePortUI [exported through MONITORUI]
557 * Display the Configuration-Dialog for a specific Port
560 * pName [I] Servername or NULL (local Computer)
561 * hWnd [I] Handle to parent Window for the Dialog-Box or NULL
562 * pPortName [I] Name of the Port, that should be configured
569 static BOOL WINAPI
localui_ConfigurePortUI(PCWSTR pName
, HWND hWnd
, PCWSTR pPortName
)
574 TRACE("(%s, %p, %s)\n", debugstr_w(pName
), hWnd
, debugstr_w(pPortName
));
575 if (open_monitor_by_name(XcvPortW
, pPortName
, &hXcv
)) {
577 res
= get_type_from_name(pPortName
);
582 res
= dlg_configure_com(hXcv
, hWnd
, pPortName
);
586 res
= dlg_configure_lpt(hXcv
, hWnd
);
590 dlg_nothingtoconfig(hWnd
);
591 SetLastError(ERROR_CANCELLED
);
602 /*****************************************************
603 * localui_DeletePortUI [exported through MONITORUI]
605 * Delete a specific Port
608 * pName [I] Servername or NULL (local Computer)
609 * hWnd [I] Handle to parent Window
610 * pPortName [I] Name of the Port, that should be deleted
617 * Native localui does not allow deleting a COM/LPT port (ERROR_NOT_SUPPORTED)
620 static BOOL WINAPI
localui_DeletePortUI(PCWSTR pName
, HWND hWnd
, PCWSTR pPortName
)
627 TRACE("(%s, %p, %s)\n", debugstr_w(pName
), hWnd
, debugstr_w(pPortName
));
629 if ((!pPortName
) || (!pPortName
[0])) {
630 SetLastError(ERROR_INVALID_PARAMETER
);
634 if (open_monitor_by_name(XcvPortW
, pPortName
, &hXcv
)) {
635 /* native localui tests here for LPT / COM - Ports and failed with
636 ERROR_NOT_SUPPORTED. */
637 if (XcvDataW(hXcv
, cmd_DeletePortW
, (LPBYTE
) pPortName
,
638 (lstrlenW(pPortName
)+1) * sizeof(WCHAR
), (LPBYTE
) &dummy
, 0, &needed
, &status
)) {
641 if (status
!= ERROR_SUCCESS
) SetLastError(status
);
642 return (status
== ERROR_SUCCESS
);
647 SetLastError(ERROR_UNKNOWN_PORT
);
651 /*****************************************************
652 * InitializePrintMonitorUI (LOCALUI.@)
654 * Initialize the User-Interface for the Local Ports
657 * Success: Pointer to a MONITORUI Structure
662 PMONITORUI WINAPI
InitializePrintMonitorUI(void)
664 static MONITORUI mymonitorui
=
668 localui_ConfigurePortUI
,
672 TRACE("=> %p\n", &mymonitorui
);
676 /*****************************************************
679 BOOL WINAPI
DllMain(HINSTANCE hinstDLL
, DWORD fdwReason
, LPVOID lpvReserved
)
681 TRACE("(%p, %d, %p)\n",hinstDLL
, fdwReason
, lpvReserved
);
685 case DLL_PROCESS_ATTACH
:
686 DisableThreadLibraryCalls( hinstDLL
);
687 LOCALUI_hInstance
= hinstDLL
;