/[winpt]/trunk/Src/wptW32API.cpp
ViewVC logotype

Diff of /trunk/Src/wptW32API.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 48 by werner, Mon Oct 31 21:14:11 2005 UTC revision 278 by twoaday, Mon Jan 15 22:02:04 2007 UTC
# Line 1  Line 1 
1  /* wptW32API.cpp - Common W32 API functions  /* wptW32API.cpp - Common W32 API functions
2   *      Copyright (C) 2001, 2002, 2003 Timo Schulz   *      Copyright (C) 2001, 2002, 2003, 2005, 2006 Timo Schulz
3   *   *
4   * This file is part of WinPT.   * This file is part of WinPT.
5   *   *
# Line 12  Line 12 
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.   * GNU General Public License for more details.
  *  
  * You should have received a copy of the GNU General Public License  
  * along with WinPT; if not, write to the Free Software Foundation,  
  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA  
15   */   */
   
16  #ifdef HAVE_CONFIG_H  #ifdef HAVE_CONFIG_H
17  #include <config.h>  #include <config.h>
18  #endif  #endif
# Line 29  Line 24 
24  #include <shellapi.h>  #include <shellapi.h>
25  #include <shlobj.h>  #include <shlobj.h>
26  #include <commctrl.h>  #include <commctrl.h>
27    #include <time.h>
28    
29  #include "wptNLS.h"  #include "wptNLS.h"
30  #include "wptW32API.h"  #include "wptW32API.h"
31  #include "wptErrors.h"  #include "wptErrors.h"
32  #include "wptVersion.h"  #include "wptVersion.h"
33    #include "wptUTF8.h"
34  #include "wptTypes.h"  #include "wptTypes.h"
35    
36    
37  extern "C" void _SHFree (void *p);  extern "C" void _SHFree (void *p);
38    
39    
40    /* Insert a new item into the menu @hm at position @pos. */
41    void
42    insert_menu_item (HMENU hm, int pos, UINT m_id, const char *text)
43    {
44        MENUITEMINFO mi;
45    
46        memset (&mi, 0, sizeof mi);
47        mi.cbSize = sizeof mi;
48        mi.fType = MF_STRING;
49        mi.dwTypeData = (char *)text;
50        mi.cch = strlen (text);
51        mi.wID = m_id;
52        mi.fMask = MIIM_ID|MIIM_DATA| MIIM_TYPE;
53        InsertMenuItem (hm, pos, FALSE, &mi);
54    }
55    
56    
57  static void  static void
58  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)
59  {  {
# Line 56  set_menu_text_ext (HMENU menu, int by_po Line 70  set_menu_text_ext (HMENU menu, int by_po
70    
71  /* Set the text of a menu item @m_uid to @text. */  /* Set the text of a menu item @m_uid to @text. */
72  void  void
73  set_menu_text (HMENU menu, int m_uid, const char *text)  set_menu_text (HMENU menu, UINT m_uid, const char *text)
74  {  {
75      set_menu_text_ext (menu, 0, m_uid, text);      set_menu_text_ext (menu, 0, m_uid, text);
76  }  }
# Line 64  set_menu_text (HMENU menu, int m_uid, co Line 78  set_menu_text (HMENU menu, int m_uid, co
78    
79  /* 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. */
80  void  void
81  set_menu_text_bypos (HMENU menu, int pos, const char *text)  set_menu_text_bypos (HMENU menu, UINT pos, const char *text)
82  {  {
83      set_menu_text_ext (menu, 1, pos, text);      set_menu_text_ext (menu, 1, pos, text);
84  }  }
# Line 72  set_menu_text_bypos (HMENU menu, int pos Line 86  set_menu_text_bypos (HMENU menu, int pos
86    
87  /* Set the state of a menu item @m_uid to @state. */  /* Set the state of a menu item @m_uid to @state. */
88  void  void
89  set_menu_state (HMENU menu, int m_uid, int state)  set_menu_state (HMENU menu, UINT m_uid, UINT state)
90  {        {      
91      MENUITEMINFO mii;      MENUITEMINFO mii;
92    
93      memset( &mii, 0, sizeof (mii) );      memset (&mii, 0, sizeof (mii));
94      mii.cbSize = sizeof (mii);      mii.cbSize = sizeof (mii);
95      mii.fMask = MIIM_STATE;      mii.fMask = MIIM_STATE;
96      mii.fState = state;      mii.fState = state;
# Line 84  set_menu_state (HMENU menu, int m_uid, i Line 98  set_menu_state (HMENU menu, int m_uid, i
98  }  }
99    
100    
101    /* Retrieve the state of the menu item @m_uid and return it. */
102    UINT
103    get_menu_state (HMENU menu, UINT m_uid)
104    {
105        MENUITEMINFO mii;
106    
107        memset (&mii, 0, sizeof (mii));
108        mii.cbSize = sizeof (mii);
109        mii.fMask = MIIM_STATE;
110        GetMenuItemInfo (menu, m_uid, FALSE, &mii);
111        return mii.fState;
112    }
113    
114    
115    enum {
116        CDLG_FILE_OPEN = 0,
117        CDLG_FILE_SAVE = 1
118    };
119    
120  /* Use the common dialog to request a file from the user.  /* Use the common dialog to request a file from the user.
121     id can be either FILE_OPEN or FILE_SAVE.     id can be either FILE_OPEN or FILE_SAVE.
122     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. */
123  const char *  const char *
124  get_filename_dlg (HWND hwnd, int id, const char * title,  get_filename_dlg (HWND hwnd, int id, const char *title,
125                    const char * filter, const char * name)                    const char *filter, const char *name)
126  {  {
127      static char file[512] = "";      static char file[2*MAX_PATH+1] = "";
128        char workdir[MAX_PATH+1];
129        char *sel_file = NULL;
130      OPENFILENAME open;      OPENFILENAME open;
131            
132      if (name && strlen (name) < (sizeof (file)-1))      if (name && strlen (name) < (DIM (file)-1))
133          strcpy (file, name);          strcpy (file, name);
134      else      else
135          memset (file, 0, sizeof (file));          memset (file, 0, sizeof (file));
136      if (!filter)      if (!filter)
137          filter = _("All Files (*.*)\0*.*\0\0");          filter = "All Files (*.*)\0*.*\0\0";
138      /* XXX: problem with gettext because of the 'artificial'  
139              double string termination!. */      GetCurrentDirectory (MAX_PATH, workdir);
140      memset (&open, 0, sizeof (open));      memset (&open, 0, sizeof (open));
141      open.lStructSize = sizeof (OPENFILENAME);      open.lStructSize = sizeof (OPENFILENAME);
142      open.hInstance = glob_hinst;      open.hInstance = glob_hinst;
# Line 110  get_filename_dlg (HWND hwnd, int id, con Line 144  get_filename_dlg (HWND hwnd, int id, con
144      open.lpstrFilter = filter;      open.lpstrFilter = filter;
145      open.hwndOwner = hwnd;      open.hwndOwner = hwnd;
146      open.lpstrFile = file;      open.lpstrFile = file;
147      open.nMaxFile = sizeof (file) - 1;      open.nMaxFile = DIM (file) - 1;
148      if (id == FILE_OPEN)      if (id == CDLG_FILE_OPEN)
149          open.Flags = OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST;          open.Flags = OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST;
150      else      else
151          open.Flags = OFN_OVERWRITEPROMPT;          open.Flags = OFN_OVERWRITEPROMPT;
152    
153      if (id == FILE_OPEN && GetOpenFileName (&open))      if (id == CDLG_FILE_OPEN && GetOpenFileName (&open))
154          return open.lpstrFile;          sel_file = open.lpstrFile;
155      else if (id == FILE_SAVE && GetSaveFileName (&open))      else if (id == CDLG_FILE_SAVE && GetSaveFileName (&open))
156          return open.lpstrFile;          sel_file = open.lpstrFile;
157        SetCurrentDirectory (workdir);
158            
159      return NULL;      return sel_file;
160  }  }
161    
162  const char*  const char*
163  get_filesave_dlg (HWND hwnd, const char *title,  get_filesave_dlg (HWND hwnd, const char *title,
164                    const char *filter, const char *name)                    const char *filter, const char *name)
165  {  {
166      return get_filename_dlg (hwnd, FILE_SAVE, title, filter, name);      return get_filename_dlg (hwnd, CDLG_FILE_SAVE, title, filter, name);
167  }  }
168    
169  const char *  const char*
170  get_fileopen_dlg (HWND hwnd, const char *title, const char *filter,  get_fileopen_dlg (HWND hwnd, const char *title, const char *filter,
171                    const char *name)                    const char *name)
172  {  {
173      return get_filename_dlg (hwnd, FILE_OPEN, title, filter, name);      return get_filename_dlg (hwnd, CDLG_FILE_OPEN, title, filter, name);
174  }  }
175    
176    
177  /* Use the common dialog to allow the user to select a folder.  /* Use the common dialog to allow the user to select a folder.
178     The return value is either the folder path or NULL if cancel was chosen. */     The return value is either the folder path or NULL if cancel was chosen. */
179  const char*  const char*
180  get_folder_dlg (HWND hwnd, const char * title, const char * name)  get_folder_dlg (HWND hwnd, const char *title, const char *name)
181  {  {
182      static char folder[MAX_PATH+1] = "";      static char folder[MAX_PATH+1] = "";
183      BROWSEINFO bi;      BROWSEINFO bi;
184      ITEMIDLIST * il;      ITEMIDLIST *il;
185    
186      memset (&bi, 0, sizeof (bi));      memset (&bi, 0, sizeof (bi));
187      bi.hwndOwner = hwnd;      bi.hwndOwner = hwnd;
# Line 157  get_folder_dlg (HWND hwnd, const char * Line 192  get_folder_dlg (HWND hwnd, const char *
192      else      else
193          memset (folder, 0, sizeof (folder));          memset (folder, 0, sizeof (folder));
194      il = SHBrowseForFolder (&bi);      il = SHBrowseForFolder (&bi);
195      if (il) {      if (il != NULL) {
196          SHGetPathFromIDList (il, folder);          SHGetPathFromIDList (il, folder);
197          _SHFree (il);          _SHFree (il);
198          return folder;          return folder;
# Line 219  set_clip_text (HWND hwnd, const char *te Line 254  set_clip_text (HWND hwnd, const char *te
254          BUG (NULL);          BUG (NULL);
255      p = (char *) GlobalLock (clipmem);      p = (char *) GlobalLock (clipmem);
256      if (p == NULL) {      if (p == NULL) {
257          rc = WPTERR_GENERAL;;          CloseClipboard ();
258          goto leave;              GlobalFree (clipmem);
259            return WPTERR_GENERAL;
260      }      }
261      memcpy (p, text, nbytes);      memcpy (p, text, nbytes);
262      p[nbytes] = '\0';      p[nbytes] = '\0';
263            
     GlobalUnlock (clipmem);  
264      SetClipboardData (CF_TEXT, clipmem);      SetClipboardData (CF_TEXT, clipmem);
265            GlobalUnlock (clipmem);
 leave:  
266      CloseClipboard ();      CloseClipboard ();
267        GlobalFree (clipmem);
268        
269      return rc;      return rc;
270  } /* set_clip_text */  }
271    
272    
273  /* Append or prepend some text to the clipboard contents.  /* Append or prepend some text to the clipboard contents.
# Line 239  leave: Line 275  leave:
275  int  int
276  set_clip_text2 (HWND hwnd, const char *text, int nbytes, int as_footer)  set_clip_text2 (HWND hwnd, const char *text, int nbytes, int as_footer)
277  {  {
278        const char *fmt;
279      char *p, *new_text;      char *p, *new_text;
280    
281      p = get_clip_text (hwnd);      p = get_clip_text (hwnd);
282      if (!p)      if (!p)
283          return WPTERR_CLIP_GET;          return WPTERR_CLIP_GET;
284      new_text = new char [strlen (p)+strlen (text)+8];      if (as_footer == 0)
285            fmt = "%s\r\n%s\r\n\r\n";
286        else
287            fmt = "%s\n%s\n\n";
288        new_text = new char [strlen (p)+strlen (text)+strlen (fmt)+2];
289      if (!new_text)      if (!new_text)
290          BUG (0);          BUG (0);
291      if (as_footer == 0)      if (as_footer == 0)
292          sprintf (new_text, "%s\r\n%s\r\n\r\n", text, p);          sprintf (new_text, fmt, text, p);
293      else      else
294          sprintf (new_text, "%s\n%s\n\n", p, text);          sprintf (new_text, fmt, p, text);
295      set_clip_text (hwnd, new_text, strlen (new_text)+1);      set_clip_text (hwnd, new_text, strlen (new_text)+1);
296      free_if_alloc (p);      free_if_alloc (p);
297      free_if_alloc (new_text);      free_if_alloc (new_text);
# Line 258  set_clip_text2 (HWND hwnd, const char *t Line 299  set_clip_text2 (HWND hwnd, const char *t
299  }  }
300    
301    
302  /* Make a file name out of the path, the file and an extension. */  /* Make a file name out of the path @path, the file @file and
303       an extension. @ext.
304       Return value: the full file name on success. */
305  char*  char*
306  make_filename (const char *path, const char *file, const char *ext)  make_filename (const char *path, const char *file, const char *ext)
307  {  {
308      char *p;      char *p;
309      size_t size = 0;      size_t size = 0;
310    
311      if( path && *path )      if (path && *path)
312          size += strlen( path );          size += strlen (path);
313      if( file && *file )      if (file && *file)
314          size += strlen( file );          size += strlen (file);
315      if( ext && *ext )      if (ext && *ext)
316          size += strlen( ext );          size += strlen (ext);
317      p = new char[size + 4];      p = new char[size + 4];
318      memset( p, 0, size );      if (!p)
319      if( path ) {          BUG (0);
320          strcat( p, path );      memset (p, 0, size);
321          if( path[strlen( path ) -1] != '\\' )      if (path) {
322              strcat( p, "\\" );          strcat (p, path);
323      }          if (path[strlen (path) -1] != '\\')
324      if( file )              strcat (p, "\\");
325          strcat( p, file );      }
326      if( ext ) {      if (file != NULL)
327          strcat( p, "." );          strcat (p, file);
328          strcat( p, ext );      if (ext != NULL) {
329            strcat (p, ".");
330            strcat (p, ext);
331        }
332        return p;
333    }
334    
335    
336    /* Generate a file name from a special dirctory. */
337    char*
338    make_special_filename (int folder, const char *file, const char *ext)
339    {
340        BOOL ec;
341        char path[MAX_PATH], *p;
342        size_t n=0;
343    
344        /* MSDN: buf must be at least MAX_PATH=256 bytes */
345        ec = SHGetSpecialFolderPath (HWND_DESKTOP, path, folder, TRUE);
346        if (ec != 1) {
347            log_debug ("SHGetSpecialFolderPath() failed\r\n", (int)GetLastError ());
348            return NULL;
349        }
350    
351        n = strlen (path)+1;
352        if (file)
353            n += strlen (file)+1;
354        if (ext)
355            n += strlen (ext)+1;
356        p = new char[n+2];
357        if (!p)
358            BUG (0);
359        memset (p, 0, n+2);
360        strcpy (p, path);    
361        if (file) {
362            strcat (p, "\\");
363            strcat (p, file);
364      }      }
365        if (ext)
366            strcat (p, ext);
367      return p;      return p;
368  } /* make_filename */  }
369    
370    
371  /* return 0 if it exists, otherwise >0. */  /* return 0 if the file @fname exists, otherwise >0. */
372  int  int
373  file_exist_check (const char * fname)  file_exist_check (const char *fname)
374  {  {
375      struct stat st;      struct stat st;
376      if (stat (fname, &st) == -1)      if (stat (fname, &st) == -1)
# Line 306  dir_exist_check (const char *dir) Line 386  dir_exist_check (const char *dir)
386  {  {
387      struct stat statbuf;          struct stat statbuf;    
388            
389      if( stat( dir, &statbuf ) == -1 )      if (stat (dir, &statbuf) == -1)
390          return WPTERR_GENERAL;          return WPTERR_GENERAL;
391      if( statbuf.st_mode & _S_IFDIR )      if (statbuf.st_mode & _S_IFDIR)
392          return 0;          return 0;
393      return WPTERR_GENERAL;      return WPTERR_GENERAL;
394  }  }
395    
396    
397  /* Return the file size of the given file. */  /* Return the file size of the given file @fname. */
398  size_t  DWORD
399  get_file_size (const char *fname)  get_file_size (const char *fname)
400  {  {
401      size_t fsize;      DWORD fsize;
402      HANDLE fh;      HANDLE fh;
403    
404      fh = CreateFile( fname, GENERIC_READ, FILE_SHARE_READ,      fh = CreateFile (fname, GENERIC_READ, FILE_SHARE_READ,
405                      NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );                       NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
406      if( fh == INVALID_HANDLE_VALUE )      if (fh == INVALID_HANDLE_VALUE)
407          return 0;          return 0;
408      fsize = GetFileSize( fh, NULL );      fsize = GetFileSize (fh, NULL);
409      if( fsize == 0xFFFFFFFF )      if (fsize == 0xFFFFFFFF)
410          fsize = 0;          fsize = 0;
411      CloseHandle( fh );      CloseHandle (fh);
412      return fsize;      return fsize;
413  } /* get_file_size */  }
   
   
 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 */  
414    
415    
416  /* 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
417     dialog is not already openened. */     dialog is not already openened. */
418  int  int
419  dialog_box_param( HINSTANCE hinst, LPCTSTR name, HWND parent, DLGPROC fnc,  dialog_box_param (HINSTANCE hinst, LPCTSTR name, HWND parent, DLGPROC fnc,
420                                    LPARAM param, LPCTSTR title, int title_id )                    LPARAM param, LPCTSTR title, int title_id)
421  {  {
422      #ifndef LANG_DE      if (FindWindowEx (GetDesktopWindow (), NULL, NULL, title))
     if( FindWindowEx( GetDesktopWindow(), NULL, NULL, title ) )  
         return -1;        
     #else  
     char strdesc[256];  
     LoadString( glob_hinst, title_id, strdesc, sizeof (strdesc) -1 );  
     if( FindWindowEx( GetDesktopWindow(), NULL, NULL, strdesc ) )  
423          return -1;          return -1;
424      #endif      return DialogBoxParam (hinst, name, parent, fnc, param);
425        }
     return DialogBoxParam( hinst, name, parent, fnc, param );  
 } /* dialog_box_param */  
426    
427    
428  /* Wrapper for message box which forces the message box into the  /* Wrapper for message box which forces the message box into the
# Line 396  msg_box (HWND hwnd, const char *text, co Line 437  msg_box (HWND hwnd, const char *text, co
437  }  }
438    
439    
 void  
 set_active_window( HWND dlg)  
 {        
     activ_hwnd = dlg;  
 } /* set_active_window */  
   
 void  
 reset_active_window( void )  
 {        
     activ_hwnd = NULL;  
 } /* reset_active_window */  
   
   
 static DWORD CALLBACK  
 reminder_thread (void *ctx)  
 {  
     reminder_ctx_s *c = (reminder_ctx_s *)ctx;  
   
     Sleep( c->msecs );  
     SetForegroundWindow( activ_hwnd );  
   
     return 0;  
 } /* reminder_thread */  
   
   
 HANDLE  
 window_reminder( struct reminder_ctx_s *ctx )  
 {  
     DWORD tid = 0;  
       
     return CreateThread( NULL, 0, reminder_thread, ctx, 0, &tid );  
 } /* window_reminder */  
   
   
 char*  
 m_strdup (const char *str)  
 {  
     char * p = new char[strlen (str) + 1];  
     if (p)  
         strcpy (p, str);  
     return p;  
 } /* m_strdup */  
   
   
440  /* Center the hwndChild relative to parent.  /* Center the hwndChild relative to parent.
441     The style param allows to specificy additional styles (like topmost). */     The style param allows to specificy additional styles (like topmost). */
442  void  void
# Line 485  center_window2 (HWND hwndChild, HWND par Line 482  center_window2 (HWND hwndChild, HWND par
482    
483  /* Center the given hwndChild window with no special style. */  /* Center the given hwndChild window with no special style. */
484  void  void
485  center_window (HWND hwndChild, HWND hwndParent)  center_window (HWND child, HWND parent)
486    {
487        center_window2 (child, parent, NULL);
488    }
489    
490    
491    /* Retrieve the product verion of the given file @fname.
492       Format: MAJOR.MINOR.PATCH1.PATCH2
493       Return value: 0 on success. */
494    int
495    get_file_version (const char *fname,
496                      WORD *major, WORD *minor, WORD *patch1, WORD *patch2)
497    {
498        VS_FIXEDFILEINFO *inf;
499        char file[MAX_PATH+1] = {0};
500        LPVOID buf, data;
501        DWORD arg;
502        DWORD size;
503        UINT qlen;
504    
505        strncpy (file, fname, MAX_PATH);
506        size = GetFileVersionInfoSize (file, &arg);
507        if (!size)
508            return -1;
509        buf = (LPVOID)new char[size];
510        if (!buf)
511            BUG (NULL);
512        GetFileVersionInfo (file, 0, size, buf);
513    
514        qlen=0;
515        VerQueryValue (buf, "\\", &data, &qlen);
516        if (!qlen) {
517            delete [](char*)buf;
518            return -1;
519        }
520        inf = (VS_FIXEDFILEINFO*)data;
521    
522        if (major)
523            *major = HIWORD (inf->dwProductVersionMS);
524        if (minor)
525            *minor = LOWORD (inf->dwProductVersionMS);
526        if (patch1)
527            *patch1 = HIWORD (inf->dwProductVersionLS);    
528        if (patch2)
529            *patch2 = LOWORD (inf->dwProductVersionLS);
530    
531        delete [](char*)buf;
532        return 0;
533    }
534    
535    
536    /* Return date in a format which complies with the
537       locale user settings. */
538    const char*
539    get_locale_date (long tm_t, char *buf, DWORD buflen)
540    {
541        SYSTEMTIME st;
542        struct tm *ptm;
543    
544        ptm = localtime (&tm_t);
545        memset (&st, 0, sizeof (st));
546        st.wYear = (WORD)ptm->tm_year+1900;
547        st.wMonth = (WORD)ptm->tm_mon+1;
548        st.wDay = (WORD)ptm->tm_mday;
549        if (!GetDateFormat (LOCALE_USER_DEFAULT, DATE_SHORTDATE, &st,
550                            NULL, buf, buflen))
551            return NULL;
552        return buf;
553    }
554    
555    /* Return the time in a format which complies with the locale user
556       settings. */
557    const char*
558    get_locale_time (long tm_t, char *buf, DWORD buflen)
559    {
560        SYSTEMTIME st;
561        struct tm *ptm;
562    
563        ptm = localtime (&tm_t);
564        memset (&st, 0, sizeof (st));
565        st.wMinute = (WORD)ptm->tm_min;
566        st.wHour = (WORD)ptm->tm_hour;
567        st.wSecond = (WORD)ptm->tm_sec;
568    
569        if (!GetTimeFormat (LOCALE_USER_DEFAULT, TIME_FORCE24HOURFORMAT, &st,
570                            NULL, buf, buflen))
571            return NULL;
572        return buf;
573    }
574    
575    
576    /* Return a combination of date+time which complies to the
577       local user settings. */
578    const char*
579    get_locale_timedate (long tm_t, char *buf, DWORD buflen)
580  {  {
581      center_window2 (hwndChild, hwndParent, NULL);      DWORD nleft = buflen;
582    
583        if (!get_locale_date (tm_t, buf, buflen))
584            return NULL;
585        nleft -= strlen (buf)+1;
586        if (nleft < 1)
587            return NULL;
588        strcat (buf, " ");
589        nleft--;
590        if (!get_locale_time (tm_t, buf+strlen (buf), nleft))
591            return NULL;
592        return buf;
593    }
594    
595    
596    /* Generate a temporary file name by using the users
597       temp path and optionally a name @name provided by the caller.
598       Return value: 0 on success. */
599    int
600    get_temp_name (char *buf, DWORD buflen, const char *name)
601    {
602        char tmp[32];
603    
604        
605        if (!name) {
606            _snprintf (tmp, DIM (tmp)-1, "%08lX", GetTickCount ());
607            name = tmp;
608        }
609    
610        /* In the mobile mode we need to use the current directory
611           as the temp path because no files should be stored outside
612           the USB disk drive. */
613    #ifdef WINPT_MOBILE
614        memset (buf, 0, buflen);
615        strncpy (buf, name, buflen-1);
616        return 0;
617    #endif
618    
619        if (!GetTempPath (buflen - strlen (name) -2, buf)) {
620            log_debug ("GetTempPath() failed ec=%d\n", (int)GetLastError ());
621            return -1;
622        }
623    
624        strcat (buf, name);
625        return 0;
626    }
627    
628    
629    void
630    ListBox_AddString_utf8 (HWND lb, const char *txt)
631    {
632        char *utf8_txt;
633    
634        utf8_txt = utf8_to_native (txt);
635        SendMessage (lb, LB_ADDSTRING, 0, (LPARAM)(LPCSTR)(utf8_txt));
636        safe_free (utf8_txt);
637    }
638    
639    
640    void
641    ComboBox_AddString_utf8 (HWND cb, const char *txt)
642    {
643        char *utf8_txt;
644    
645        utf8_txt = utf8_to_native (txt);
646        SendMessage ((cb), CB_ADDSTRING, 0, (LPARAM)(LPCSTR)(utf8_txt));
647        safe_free (utf8_txt);
648    }
649    
650    
651    /* GetDlgItemText replacement with UTF8 support. */
652    int
653    GetDlgItemText_utf8 (HWND dlg, int id, char **r_txt)
654    {
655        int len = GetWindowTextLength (GetDlgItem (dlg, id));
656        char *txt;
657    
658        *r_txt = NULL;
659        if (len < 1)
660            return 0;
661        txt = new char[len+2];
662        if (!txt)
663            BUG (NULL);
664        GetDlgItemText (dlg, id, txt, len+1);
665        *r_txt = native_to_utf8 (txt);
666        free_if_alloc (txt);
667        return len;
668    }
669    
670    
671    /* Return TRUE if the current user has admin privileges. */
672    BOOL
673    user_is_admin (void)
674    {
675        SID_IDENTIFIER_AUTHORITY SystemSidAuthority = SECURITY_NT_AUTHORITY;
676        HANDLE hd;
677        TOKEN_GROUPS  *ptg = NULL;
678        DWORD ngtoken;
679        DWORD i;
680        BOOL admin = FALSE;
681        PSID psid = 0;
682    
683        if (GetVersion () & 0x80000000) /* Win9X */
684            return TRUE;
685    
686        if (!OpenThreadToken (GetCurrentThread (), TOKEN_QUERY, FALSE, &hd) &&
687            !OpenProcessToken (GetCurrentProcess (), TOKEN_QUERY, &hd))
688            return FALSE;
689        
690        if (!GetTokenInformation (hd, TokenGroups, NULL, 0, &ngtoken) &&
691            GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
692            ptg = (TOKEN_GROUPS*)GlobalAlloc (GPTR, ngtoken);
693            if (!ptg)
694                return FALSE;
695    
696            if (!GetTokenInformation (hd, TokenGroups,
697                                      ptg, ngtoken, &ngtoken)) {
698                GlobalFree (ptg);
699                return FALSE;
700            }
701            AllocateAndInitializeSid (&SystemSidAuthority,
702                                      2, SECURITY_BUILTIN_DOMAIN_RID,
703                                      DOMAIN_ALIAS_RID_ADMINS,
704                                      0, 0, 0, 0, 0, 0,
705                                      &psid);
706            for (i = 0; i < ptg->GroupCount; i++) {
707                if (EqualSid (ptg->Groups[i].Sid, psid)) {
708                    admin = TRUE;
709                    break;
710                }
711            }
712            FreeSid (psid);
713            GlobalFree (ptg);
714        }
715    
716        CloseHandle (hd);
717        return admin;
718  }  }

Legend:
Removed from v.48  
changed lines
  Added in v.278

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26