29 |
#include <shellapi.h> |
#include <shellapi.h> |
30 |
#include <shlobj.h> |
#include <shlobj.h> |
31 |
#include <commctrl.h> |
#include <commctrl.h> |
32 |
|
#include <time.h> |
33 |
|
|
34 |
#include "wptNLS.h" |
#include "wptNLS.h" |
35 |
#include "wptW32API.h" |
#include "wptW32API.h" |
36 |
#include "wptErrors.h" |
#include "wptErrors.h" |
37 |
#include "wptVersion.h" |
#include "wptVersion.h" |
38 |
|
#include "wptUTF8.h" |
39 |
#include "wptTypes.h" |
#include "wptTypes.h" |
40 |
|
|
41 |
|
|
42 |
extern "C" void _SHFree (void *p); |
extern "C" void _SHFree (void *p); |
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
/* Insert a new item into the menu @hm at position @pos. */ |
47 |
|
void |
48 |
|
insert_menu_item (HMENU hm, int pos, UINT m_id, const char *text) |
49 |
|
{ |
50 |
|
MENUITEMINFO mi; |
51 |
|
|
52 |
|
memset (&mi, 0, sizeof mi); |
53 |
|
mi.cbSize = sizeof mi; |
54 |
|
mi.fType = MF_STRING; |
55 |
|
mi.dwTypeData = (char *)text; |
56 |
|
mi.cch = strlen (text); |
57 |
|
mi.wID = m_id; |
58 |
|
mi.fMask = MIIM_ID|MIIM_DATA| MIIM_TYPE; |
59 |
|
InsertMenuItem (hm, pos, FALSE, &mi); |
60 |
|
} |
61 |
|
|
62 |
|
|
63 |
static void |
static void |
64 |
set_menu_text_ext (HMENU menu, int by_pos, int m_uid, const char *text) |
set_menu_text_ext (HMENU menu, int by_pos, int m_uid, const char *text) |
65 |
{ |
{ |
76 |
|
|
77 |
/* Set the text of a menu item @m_uid to @text. */ |
/* Set the text of a menu item @m_uid to @text. */ |
78 |
void |
void |
79 |
set_menu_text (HMENU menu, int m_uid, const char *text) |
set_menu_text (HMENU menu, UINT m_uid, const char *text) |
80 |
{ |
{ |
81 |
set_menu_text_ext (menu, 0, m_uid, text); |
set_menu_text_ext (menu, 0, m_uid, text); |
82 |
} |
} |
84 |
|
|
85 |
/* Set the text of a menu item with the position @pos to @text. */ |
/* Set the text of a menu item with the position @pos to @text. */ |
86 |
void |
void |
87 |
set_menu_text_bypos (HMENU menu, int pos, const char *text) |
set_menu_text_bypos (HMENU menu, UINT pos, const char *text) |
88 |
{ |
{ |
89 |
set_menu_text_ext (menu, 1, pos, text); |
set_menu_text_ext (menu, 1, pos, text); |
90 |
} |
} |
92 |
|
|
93 |
/* Set the state of a menu item @m_uid to @state. */ |
/* Set the state of a menu item @m_uid to @state. */ |
94 |
void |
void |
95 |
set_menu_state (HMENU menu, int m_uid, int state) |
set_menu_state (HMENU menu, UINT m_uid, UINT state) |
96 |
{ |
{ |
97 |
MENUITEMINFO mii; |
MENUITEMINFO mii; |
98 |
|
|
104 |
} |
} |
105 |
|
|
106 |
|
|
107 |
|
/* Retrieve the state of the menu item @m_uid and return it. */ |
108 |
|
UINT |
109 |
|
get_menu_state (HMENU menu, UINT m_uid) |
110 |
|
{ |
111 |
|
MENUITEMINFO mii; |
112 |
|
|
113 |
|
memset (&mii, 0, sizeof (mii)); |
114 |
|
mii.cbSize = sizeof (mii); |
115 |
|
mii.fMask = MIIM_STATE; |
116 |
|
GetMenuItemInfo (menu, m_uid, FALSE, &mii); |
117 |
|
return mii.fState; |
118 |
|
} |
119 |
|
|
120 |
|
|
121 |
enum { |
enum { |
122 |
CDLG_FILE_OPEN = 0, |
CDLG_FILE_OPEN = 0, |
123 |
CDLG_FILE_SAVE = 1 |
CDLG_FILE_SAVE = 1 |
127 |
id can be either FILE_OPEN or FILE_SAVE. |
id can be either FILE_OPEN or FILE_SAVE. |
128 |
The return value is the file name or NULL if cancel was chosen. */ |
The return value is the file name or NULL if cancel was chosen. */ |
129 |
const char * |
const char * |
130 |
get_filename_dlg (HWND hwnd, int id, const char * title, |
get_filename_dlg (HWND hwnd, int id, const char *title, |
131 |
const char * filter, const char * name) |
const char *filter, const char *name) |
132 |
{ |
{ |
133 |
static char file[512] = ""; |
static char file[512] = ""; |
134 |
OPENFILENAME open; |
OPENFILENAME open; |
138 |
else |
else |
139 |
memset (file, 0, sizeof (file)); |
memset (file, 0, sizeof (file)); |
140 |
if (!filter) |
if (!filter) |
141 |
filter = _("All Files (*.*)\0*.*\0\0"); |
filter = "All Files (*.*)\0*.*\0\0"; |
142 |
/* XXX: problem with gettext because of the 'artificial' |
/* XXX: problem with gettext because of the 'artificial' |
143 |
double string termination!. */ |
double string termination!. */ |
144 |
memset (&open, 0, sizeof (open)); |
memset (&open, 0, sizeof (open)); |
257 |
BUG (NULL); |
BUG (NULL); |
258 |
p = (char *) GlobalLock (clipmem); |
p = (char *) GlobalLock (clipmem); |
259 |
if (p == NULL) { |
if (p == NULL) { |
260 |
rc = WPTERR_GENERAL;; |
CloseClipboard (); |
261 |
goto leave; |
GlobalFree (clipmem); |
262 |
|
return WPTERR_GENERAL; |
263 |
} |
} |
264 |
memcpy (p, text, nbytes); |
memcpy (p, text, nbytes); |
265 |
p[nbytes] = '\0'; |
p[nbytes] = '\0'; |
266 |
|
|
|
GlobalUnlock (clipmem); |
|
267 |
SetClipboardData (CF_TEXT, clipmem); |
SetClipboardData (CF_TEXT, clipmem); |
268 |
|
GlobalUnlock (clipmem); |
|
leave: |
|
269 |
CloseClipboard (); |
CloseClipboard (); |
270 |
|
GlobalFree (clipmem); |
271 |
|
|
272 |
return rc; |
return rc; |
273 |
} /* set_clip_text */ |
} |
274 |
|
|
275 |
|
|
276 |
/* Append or prepend some text to the clipboard contents. |
/* Append or prepend some text to the clipboard contents. |
329 |
} |
} |
330 |
|
|
331 |
|
|
332 |
|
/* Generate a file name from a special dirctory. */ |
333 |
|
char* |
334 |
|
make_special_filename (int folder, const char *file, const char *ext) |
335 |
|
{ |
336 |
|
BOOL ec; |
337 |
|
char path[MAX_PATH], *p; |
338 |
|
size_t n=0; |
339 |
|
|
340 |
|
/* MSDN: buf must be at least MAX_PATH=256 bytes */ |
341 |
|
ec = SHGetSpecialFolderPath (HWND_DESKTOP, path, folder, TRUE); |
342 |
|
if (ec != 1) { |
343 |
|
log_debug ("SHGetSpecialFolderPath() failed\r\n", (int)GetLastError ()); |
344 |
|
return NULL; |
345 |
|
} |
346 |
|
|
347 |
|
n = strlen (path)+1; |
348 |
|
if (file) |
349 |
|
n += strlen (file)+1; |
350 |
|
if (ext) |
351 |
|
n += strlen (ext)+1; |
352 |
|
p = new char[n+2]; |
353 |
|
if (!p) |
354 |
|
BUG (0); |
355 |
|
memset (p, 0, n+2); |
356 |
|
strcpy (p, path); |
357 |
|
if (file) { |
358 |
|
strcat (p, "\\"); |
359 |
|
strcat (p, file); |
360 |
|
} |
361 |
|
if (ext) |
362 |
|
strcat (p, ext); |
363 |
|
return p; |
364 |
|
} |
365 |
|
|
366 |
|
|
367 |
/* return 0 if the file @fname exists, otherwise >0. */ |
/* return 0 if the file @fname exists, otherwise >0. */ |
368 |
int |
int |
369 |
file_exist_check (const char *fname) |
file_exist_check (const char *fname) |
409 |
} |
} |
410 |
|
|
411 |
|
|
|
int |
|
|
init_file_lock( LOCK *ctx, const char *file ) |
|
|
{ |
|
|
|
|
|
ctx->size = get_file_size( file ); |
|
|
ctx->file = m_strdup( file ); |
|
|
ctx->fh = CreateFile( file, GENERIC_READ, FILE_SHARE_READ, NULL, |
|
|
OPEN_ALWAYS, 0, NULL ); |
|
|
if( ctx->fh == INVALID_HANDLE_VALUE ) |
|
|
return WPTERR_GENERAL; |
|
|
if( LockFile( ctx->fh, 0, 0, ctx->size, 0 ) == FALSE ) { |
|
|
CloseHandle( ctx->fh ); |
|
|
ctx->fh = INVALID_HANDLE_VALUE; |
|
|
ctx->size = 0; |
|
|
free( ctx->file ); |
|
|
return WPTERR_GENERAL; |
|
|
} |
|
|
return 0; |
|
|
} /* init_file_lock */ |
|
|
|
|
|
|
|
|
void |
|
|
release_file_lock( LOCK *ctx ) |
|
|
{ |
|
|
free_if_alloc( ctx->file ); |
|
|
ctx->file = NULL; |
|
|
ctx->size = 0; |
|
|
CloseHandle( ctx->fh ); |
|
|
} /* release_file_lock */ |
|
|
|
|
|
|
|
412 |
/* Start a dialog with the exception that before it is checked that the |
/* Start a dialog with the exception that before it is checked that the |
413 |
dialog is not already openened. */ |
dialog is not already openened. */ |
414 |
int |
int |
503 |
get_file_version (const char *fname, WORD *major, WORD *minor, |
get_file_version (const char *fname, WORD *major, WORD *minor, |
504 |
WORD *patch1, WORD *patch2) |
WORD *patch1, WORD *patch2) |
505 |
{ |
{ |
506 |
VS_FIXEDFILEINFO *inf = {0}; |
VS_FIXEDFILEINFO *inf; |
507 |
char file[MAX_PATH+1] = {0}; |
char file[MAX_PATH+1] = {0}; |
508 |
LPVOID buf, data; |
LPVOID buf, data; |
509 |
DWORD arg; |
DWORD arg; |
514 |
size = GetFileVersionInfoSize (file, &arg); |
size = GetFileVersionInfoSize (file, &arg); |
515 |
if (!size) |
if (!size) |
516 |
return -1; |
return -1; |
517 |
buf = (LPVOID)new CHAR[size]; |
buf = (LPVOID)new char[size]; |
518 |
if (!buf) |
if (!buf) |
519 |
BUG (NULL); |
BUG (NULL); |
520 |
GetFileVersionInfo (file, 0, size, buf); |
GetFileVersionInfo (file, 0, size, buf); |
522 |
qlen=0; |
qlen=0; |
523 |
VerQueryValue (buf, "\\", &data, &qlen); |
VerQueryValue (buf, "\\", &data, &qlen); |
524 |
if (!qlen) { |
if (!qlen) { |
525 |
delete [] (char*)buf; |
delete [](char*)buf; |
526 |
return -1; |
return -1; |
527 |
} |
} |
528 |
inf = (VS_FIXEDFILEINFO*)data; |
inf = (VS_FIXEDFILEINFO*)data; |
536 |
if (patch2) |
if (patch2) |
537 |
*patch2 = LOWORD (inf->dwProductVersionLS); |
*patch2 = LOWORD (inf->dwProductVersionLS); |
538 |
|
|
539 |
delete [] (char*)buf; |
delete [](char*)buf; |
540 |
return 0; |
return 0; |
541 |
} |
} |
542 |
|
|
543 |
|
|
544 |
void |
/* Return date in a format which complies with the |
545 |
set_active_window( HWND dlg) |
system locale settings. */ |
546 |
{ |
const char* |
547 |
activ_hwnd = dlg; |
get_locale_date (long tm_t, char *buf, DWORD buflen) |
548 |
} /* set_active_window */ |
{ |
549 |
|
SYSTEMTIME st; |
550 |
|
struct tm *ptm; |
551 |
|
|
552 |
|
ptm = localtime (&tm_t); |
553 |
|
st.wYear = (WORD)ptm->tm_year; |
554 |
|
st.wMonth = (WORD)ptm->tm_mon; |
555 |
|
st.wDay = (WORD)ptm->tm_mday; |
556 |
|
st.wYear += 1900; |
557 |
|
st.wMonth += 1; |
558 |
|
if (!GetDateFormat (LOCALE_USER_DEFAULT, DATE_SHORTDATE, &st, |
559 |
|
NULL, buf, buflen)) |
560 |
|
return NULL; |
561 |
|
return buf; |
562 |
|
} |
563 |
|
|
564 |
|
|
565 |
|
/* Generate a temporary file name by using the users |
566 |
|
temp path and optionally a name @name provided by the caller. |
567 |
|
Return value: 0 on success. */ |
568 |
|
int |
569 |
|
get_temp_name (char *buf, DWORD buflen, const char *name) |
570 |
|
{ |
571 |
|
char tmp[32]; |
572 |
|
|
573 |
|
if (!name) { |
574 |
|
sprintf (tmp, "%08lX", GetTickCount ()); |
575 |
|
name = tmp; |
576 |
|
} |
577 |
|
|
578 |
|
/* in the mobile mode we use a local temp folder |
579 |
|
with the fixed name 'temp'. */ |
580 |
|
if (mobile_mode_active) { |
581 |
|
_snprintf (buf, buflen-1, "temp\\%s", name); |
582 |
|
return 0; |
583 |
|
} |
584 |
|
|
585 |
|
if (!GetTempPath (buflen - strlen (name) -2, buf)) { |
586 |
|
log_debug ("GetTempPath() failed ec=%d\n", (int)GetLastError ()); |
587 |
|
return -1; |
588 |
|
} |
589 |
|
strcat (buf, name); |
590 |
|
return 0; |
591 |
|
} |
592 |
|
|
593 |
|
|
594 |
void |
void |
595 |
reset_active_window( void ) |
ListBox_AddString_utf8 (HWND lb, const char *txt) |
596 |
{ |
{ |
597 |
activ_hwnd = NULL; |
char *utf8_txt; |
598 |
} /* reset_active_window */ |
|
599 |
|
utf8_txt = utf8_to_native (txt); |
600 |
|
SendMessage (lb, LB_ADDSTRING, 0, (LPARAM)(LPCSTR)(utf8_txt)); |
601 |
|
safe_free (utf8_txt); |
602 |
|
} |
603 |
|
|
604 |
|
|
605 |
static DWORD CALLBACK |
void |
606 |
reminder_thread (void *ctx) |
ComboBox_AddString_utf8 (HWND cb, const char *txt) |
607 |
{ |
{ |
|
reminder_ctx_s *c = (reminder_ctx_s *)ctx; |
|
608 |
|
|
609 |
Sleep( c->msecs ); |
char *utf8_txt; |
|
SetForegroundWindow( activ_hwnd ); |
|
610 |
|
|
611 |
return 0; |
utf8_txt = utf8_to_native (txt); |
612 |
} /* reminder_thread */ |
SendMessage ((cb), CB_ADDSTRING, 0, (LPARAM)(LPCSTR)(utf8_txt)); |
613 |
|
safe_free (utf8_txt); |
614 |
|
} |
615 |
|
|
616 |
|
|
617 |
HANDLE |
/* GetDlgItemText replacement with UTF8 support. */ |
618 |
window_reminder( struct reminder_ctx_s *ctx ) |
int |
619 |
|
GetDlgItemText_utf8 (HWND dlg, int id, char **r_txt) |
620 |
{ |
{ |
621 |
DWORD tid = 0; |
int len = GetWindowTextLength (GetDlgItem (dlg, id)); |
622 |
|
char *txt; |
623 |
return CreateThread( NULL, 0, reminder_thread, ctx, 0, &tid ); |
|
624 |
} /* window_reminder */ |
*r_txt = NULL; |
625 |
|
if (len < 1) |
626 |
|
return 0; |
627 |
|
txt = new char[len+2]; |
628 |
|
if (!txt) |
629 |
|
BUG (NULL); |
630 |
|
GetDlgItemText (dlg, id, txt, len+1); |
631 |
|
*r_txt = native_to_utf8 (txt); |
632 |
|
free_if_alloc (txt); |
633 |
|
return len; |
634 |
|
} |
635 |
|
|
636 |
|
|
637 |
|
/* Return TRUE if the current user has admin privileges. */ |
638 |
|
BOOL |
639 |
|
user_is_admin (void) |
640 |
|
{ |
641 |
|
SID_IDENTIFIER_AUTHORITY SystemSidAuthority = SECURITY_NT_AUTHORITY; |
642 |
|
HANDLE hd; |
643 |
|
TOKEN_GROUPS *ptg = NULL; |
644 |
|
DWORD ngtoken; |
645 |
|
DWORD i; |
646 |
|
BOOL admin = FALSE; |
647 |
|
PSID psid = 0; |
648 |
|
|
649 |
|
if (GetVersion () & 0x80000000) /* Win9X */ |
650 |
|
return TRUE; |
651 |
|
|
652 |
|
if (!OpenThreadToken (GetCurrentThread (), TOKEN_QUERY, FALSE, &hd) && |
653 |
|
!OpenProcessToken (GetCurrentProcess (), TOKEN_QUERY, &hd)) |
654 |
|
return FALSE; |
655 |
|
|
656 |
|
if (!GetTokenInformation (hd, TokenGroups, NULL, 0, &ngtoken) && |
657 |
|
GetLastError() == ERROR_INSUFFICIENT_BUFFER) { |
658 |
|
ptg = (TOKEN_GROUPS*)GlobalAlloc (GPTR, ngtoken); |
659 |
|
if (!ptg) |
660 |
|
return FALSE; |
661 |
|
|
662 |
|
if (!GetTokenInformation (hd, TokenGroups, |
663 |
|
ptg, ngtoken, &ngtoken)) { |
664 |
|
GlobalFree (ptg); |
665 |
|
return FALSE; |
666 |
|
} |
667 |
|
AllocateAndInitializeSid (&SystemSidAuthority, |
668 |
|
2, SECURITY_BUILTIN_DOMAIN_RID, |
669 |
|
DOMAIN_ALIAS_RID_ADMINS, |
670 |
|
0, 0, 0, 0, 0, 0, |
671 |
|
&psid); |
672 |
|
for (i = 0; i < ptg->GroupCount; i++) { |
673 |
|
if (EqualSid (ptg->Groups[i].Sid, psid)) { |
674 |
|
admin = TRUE; |
675 |
|
break; |
676 |
|
} |
677 |
|
} |
678 |
|
FreeSid (psid); |
679 |
|
GlobalFree (ptg); |
680 |
|
} |
681 |
|
|
682 |
|
CloseHandle (hd); |
683 |
|
return admin; |
684 |
|
} |