File: wapi.cpp

package info (click to toggle)
x2goclient 3.99.2.1-5
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 6,596 kB
  • sloc: cpp: 21,625; perl: 512; makefile: 129; sh: 17
file content (500 lines) | stat: -rw-r--r-- 13,780 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
#ifndef _WIN32_WINDOWS
#define _WIN32_WINDOWS 0x0500
#define _WIN32_WINNT 0x0500
#define WINVER 0x0500
#endif
#include "x2goclientconfig.h"
#ifdef Q_OS_WIN
#include <windows.h>
#include <sddl.h>
#include "wapi.h"
#include "x2gologdebug.h"


long wapiSetFSWindow ( HWND hWnd, const QRect& desktopGeometry )
{
    long style=GetWindowLong ( hWnd,GWL_STYLE );
    SetWindowPos ( hWnd, HWND_TOP, desktopGeometry.x(),
                   desktopGeometry.y(),
                   desktopGeometry.width(),
                   desktopGeometry.height(),
                   SWP_FRAMECHANGED );
    SetWindowLong ( hWnd, GWL_STYLE,
                    WS_VISIBLE | WS_SYSMENU |
                    WS_CLIPCHILDREN | WS_CLIPSIBLINGS );

    SetWindowPos ( hWnd, HWND_TOP, desktopGeometry.x(),
                   desktopGeometry.y(),
                   desktopGeometry.width(),
                   desktopGeometry.height(),
                   SWP_FRAMECHANGED );

    SetForegroundWindow ( hWnd );
    return style;
}

void wapiRestoreWindow( HWND hWnd, long style, const QRect& desktopGeometry )
{
    SetWindowLong ( hWnd, GWL_STYLE,style);
    SetWindowPos ( hWnd, HWND_TOP, desktopGeometry.x(),
                   desktopGeometry.y(),
                   desktopGeometry.width(),
                   desktopGeometry.height(),
                   SWP_FRAMECHANGED );
}

void wapiHideFromTaskBar ( HWND wnd )
{
    ShowWindow ( wnd, SW_HIDE ) ;
    SetWindowLong ( wnd, GWL_EXSTYLE, GetWindowLong ( wnd, GWL_EXSTYLE )  &
                    ~WS_EX_APPWINDOW );
    SetWindowLong ( wnd, GWL_EXSTYLE, GetWindowLong ( wnd, GWL_EXSTYLE )  |
                    WS_EX_TOOLWINDOW );
    ShowWindow ( wnd, SW_SHOW ) ;
}

HWND wapiSetParent ( HWND child, HWND par )
{
    HWND wn=SetParent ( child,par );
    if ( par )
        SetWindowLong ( child, GWL_STYLE,
                        GetWindowLong ( child, GWL_STYLE ) | WS_CHILD );
    else
        SetWindowLong (
            child, GWL_STYLE,
            GetWindowLong ( child, GWL_STYLE ) &~ WS_CHILD );
    SetWindowLong ( child, GWL_STYLE,
                    GetWindowLong ( child, GWL_STYLE ) | WS_POPUP );
    return wn;
}

bool wapiClientRect ( HWND wnd, QRect& rect )
{
    RECT rcWindow;
    if ( GetClientRect ( wnd,&rcWindow ) )
    {
        rect.setCoords ( rcWindow.left,
                         rcWindow.top,
                         rcWindow.right,rcWindow.bottom );
        return true;
    }
    return false;
}

bool wapiWindowRectWithoutDecoration ( HWND wnd, QRect& rect )
{
    RECT rcWindow;
    if ( GetClientRect ( wnd,&rcWindow ) )
    {
        POINT pnt;
        pnt.x=0;
        pnt.y=0;
        ClientToScreen(wnd,&pnt);
        rect.setRect ( pnt.x,
                       pnt.y,
                       rcWindow.right-rcWindow.left,rcWindow.bottom-rcWindow.top );
        return true;
    }
    return false;
}


bool wapiWindowRect ( HWND wnd, QRect& rect )
{
    RECT rcWindow;
    if ( GetWindowRect ( wnd,&rcWindow ) )
    {
        rect.setCoords ( rcWindow.left,
                         rcWindow.top,
                         rcWindow.right,rcWindow.bottom );
        return true;
    }
    return false;
}

bool wapiGetBorders ( HWND wnd, int& vBorder, int& hBorder, int& barHeight )
{
    WINDOWINFO wifo;
    wifo.cbSize=sizeof ( WINDOWINFO );
    if ( !GetWindowInfo ( wnd,&wifo ) )
        return false;
    vBorder=wifo.cxWindowBorders;
    hBorder=wifo.cyWindowBorders;
    TITLEBARINFO bifo;
    bifo.cbSize=sizeof ( TITLEBARINFO );
    if ( !GetTitleBarInfo ( wnd,&bifo ) )
        return false;
    barHeight=bifo.rcTitleBar.bottom-bifo.rcTitleBar.top;

    return true;

}

bool wapiShowWindow ( HWND wnd, wapiCmdShow nCmdShow )
{
    int cmd=WAPI_SHOWNORMAL;
    switch ( nCmdShow )
    {
    case WAPI_FORCEMINIMIZE:
        cmd=SW_FORCEMINIMIZE;
        break;
    case WAPI_HIDE:
        cmd=SW_HIDE;
        break;
    case WAPI_MAXIMIZE:
        cmd=SW_MAXIMIZE;
        break;
    case WAPI_MINIMIZE:
        cmd=SW_MINIMIZE;
        break;
    case WAPI_RESTORE:
        cmd=SW_RESTORE;
        break;
    case WAPI_SHOW:
        cmd=SW_SHOW;
        break;
    case WAPI_SHOWDEFAULT:
        cmd=SW_SHOWDEFAULT;
        break;
    case WAPI_SHOWMAXIMIZED:
        cmd=SW_SHOWMAXIMIZED;
        break;
    case WAPI_SHOWMINIMIZED:
        cmd=SW_SHOWMINIMIZED;
        break;
    case WAPI_SHOWMINNOACTIVE:
        cmd=SW_SHOWMINNOACTIVE;
        break;
    case WAPI_SHOWNA:
        cmd=SW_SHOWNA;
        break;
    case WAPI_SHOWNOACTIVATE:
        cmd=SW_SHOWNOACTIVATE;
        break;
    case WAPI_SHOWNORMAL:
        cmd=SW_SHOWNORMAL;
        break;
    }

    return ShowWindow ( wnd, cmd );
}

bool wapiUpdateWindow ( HWND wnd )
{
    return RedrawWindow ( wnd,0,0,RDW_INVALIDATE );
}

bool wapiMoveWindow ( HWND wnd, int x, int y, int width, int height,
                      bool repaint )
{
    return MoveWindow ( wnd, x, y, width, height, repaint );
}

HWND wapiFindWindow ( const ushort * className, const ushort * text )
{
    return FindWindowEx ( 0,0, ( LPCTSTR ) className, ( LPCTSTR ) text );
}

bool wapiSetWindowText( HWND wnd, const QString& text)
{
    return SetWindowText(wnd, (LPCTSTR)text.utf16() );
}

void wapiSetWindowIcon ( HWND wnd, const QPixmap& icon)
{
    int iconx=GetSystemMetrics(SM_CXICON);
    int icony=GetSystemMetrics(SM_CYICON);
    int smallx=GetSystemMetrics(SM_CXSMICON);
    int smally=GetSystemMetrics(SM_CXSMICON);

    HICON largeIcon=0;
    HICON smallIcon=0;

    largeIcon=icon.scaled(iconx,icony, Qt::IgnoreAspectRatio,Qt::SmoothTransformation).toWinHICON ();
    smallIcon=icon.scaled(smallx,smally, Qt::IgnoreAspectRatio,Qt::SmoothTransformation).toWinHICON ();

    x2goDebug<<"large icon: "<<largeIcon<<iconx<<"x"<<icony<<endl;
    x2goDebug<<"small icon: "<<smallIcon<<smallx<<"x"<<smally<<endl;
    int rez=SetClassLong(wnd,GCL_HICON, (LONG)largeIcon);
    if (!rez)
        x2goDebug<<"ERROR: "<<GetLastError()<<endl;
    rez=SetClassLong(wnd,GCL_HICONSM,(LONG)smallIcon);
    if (!rez)
        x2goDebug<<"ERROR: "<<GetLastError()<<endl;
    /*    ShowWindow(wnd, SW_HIDE);
        ShowWindow(wnd, SW_SHOW);*/
}

QString wapiShortFileName ( const QString& longName )
{
    long     length = 0;
    TCHAR*   buffer = NULL;

    length = GetShortPathName ( ( LPCTSTR ) longName.utf16(), NULL, 0 );
    if ( !length )
    {
        return QString::null;
    }

    buffer = new TCHAR[length];
    length = GetShortPathName ( ( LPCTSTR ) longName.utf16(),
                                buffer,length );
    if ( !length )
    {
        delete []buffer;
        return QString::null;
    }
    QString spath=QString::fromUtf16 ( ( const ushort* ) buffer );
    delete []buffer;
    return spath;
}


QString wapiGetDriveByLabel(const QString& label)
{
    int len=GetLogicalDriveStrings(0,0);
    if (len>0)
    {
        TCHAR* buf=new TCHAR[len+1];
        len=GetLogicalDriveStrings(len,buf);
        for (int i=0;i<len;i+=4)
        {
            QString drive=QString::fromUtf16 ( ( const ushort* ) buf+i );
            x2goDebug<<"drive:"<<drive;
            TCHAR vol[MAX_PATH+1];
            TCHAR fs[MAX_PATH+1];
            GetVolumeInformation(buf+i,vol,MAX_PATH,0,0,0,fs,MAX_PATH);
            QString volume=QString::fromUtf16 ( ( const ushort* ) vol );
            x2goDebug<<"vol:"<<volume<<
            "fs:"<<QString::fromUtf16 ( ( const ushort* ) fs );
            if (!volume.compare(label,Qt::CaseInsensitive))
            {
                x2goDebug<<"matched! ";

                delete []buf;
                return drive.replace(":\\","");
            }
        }
        delete []buf;
    }

    return label;
}


QString getNameFromSid ( PSID psid, QString* systemName )
{
    DWORD length=0;
    DWORD dlength=0;
    TCHAR* name=0;
    TCHAR* sysName=0;
    SID_NAME_USE eUse;

    LookupAccountSid ( 0,psid,
                       name,&length,sysName,&dlength,&eUse );
    if ( !length )
    {
        return QString::null;
    }

    name=new TCHAR[length];
    sysName=new TCHAR[dlength];

    if ( ! LookupAccountSid ( 0,psid,
                              name,&length,sysName,
                              &dlength,&eUse ) )
    {
        delete []name;
        delete []sysName;
        return QString::null;
    }

    QString strName=QString::fromUtf16 (
                        ( const ushort* ) name );
    if ( systemName )
        *systemName=QString::fromUtf16 (
                        ( const ushort* ) sysName );
    delete []sysName;
    delete []name;
    return strName;
}

QString getStringFromSid ( PSID psid )
{
    LPTSTR stringSid;
    ConvertSidToStringSid ( psid,
                            &stringSid );
    QString str=QString::fromUtf16 (
                    ( const ushort* ) stringSid );
    LocalFree ( stringSid );
    return str;
}

bool wapiAccountInfo ( QString* retSid, QString* retUname,
                       QString* primaryGroupSID, QString* primaryGroupName,
                       QString* retSysName )
{
    HANDLE hToken;
    if ( !OpenProcessToken ( GetCurrentProcess(),
                             TOKEN_QUERY, &hToken ) )
    {
        return false;
    }
    if ( primaryGroupSID || primaryGroupName )
    {
        PTOKEN_PRIMARY_GROUP pGroupInfo=0;
        DWORD dwResult=0;
        DWORD dwSize=0;
        if ( !GetTokenInformation ( hToken, TokenPrimaryGroup,
                                    NULL, dwSize, &dwSize ) )
        {
            dwResult = GetLastError();
            if ( dwResult != ERROR_INSUFFICIENT_BUFFER )
            {
                CloseHandle ( hToken );
                return false;
            }
        }
        pGroupInfo = ( PTOKEN_PRIMARY_GROUP ) GlobalAlloc ( GPTR,
                     dwSize );

        if ( ! GetTokenInformation ( hToken, TokenPrimaryGroup,
                                     pGroupInfo,
                                     dwSize, &dwSize ) )
        {
            if ( pGroupInfo )
                GlobalFree ( pGroupInfo );
            CloseHandle ( hToken );
            return false;
        }

        if ( primaryGroupSID )
        {
            *primaryGroupSID=getStringFromSid (
                                 pGroupInfo->PrimaryGroup );
        }
        if ( primaryGroupName )
        {
            *primaryGroupName=getNameFromSid (
                                  pGroupInfo->PrimaryGroup,
                                  retSysName );
        }

        if ( pGroupInfo )
            GlobalFree ( pGroupInfo );
    }
    if ( retSid || retUname )
    {
        PTOKEN_USER pUserInfo=0;
        DWORD dwResult=0;
        DWORD dwSize=0;

        if ( !GetTokenInformation ( hToken, TokenUser,
                                    NULL, dwSize, &dwSize ) )
        {
            dwResult = GetLastError();
            if ( dwResult != ERROR_INSUFFICIENT_BUFFER )
            {
                CloseHandle ( hToken );
                return false;
            }
        }
        pUserInfo = ( PTOKEN_USER ) GlobalAlloc ( GPTR,
                    dwSize );

        if ( ! GetTokenInformation ( hToken, TokenUser,
                                     pUserInfo,
                                     dwSize, &dwSize ) )
        {
            if ( pUserInfo )
                GlobalFree ( pUserInfo );
            CloseHandle ( hToken );
            return false;
        }

        if ( retSid )
        {
            *retSid=getStringFromSid (
                        pUserInfo->User.Sid );
        }
        if ( retUname )
        {
            *retUname=getNameFromSid (
                          pUserInfo->User.Sid,
                          retSysName );
        }
        if ( pUserInfo )
            GlobalFree ( pUserInfo );
    }
    CloseHandle ( hToken );
    return true;
}

void wapiShellExecute ( const QString& operation, const QString& file,
                        const QString& parameters,
                        const QString& dir, HWND win )
{
    if ( parameters==QString::null )
        ShellExecute ( win, ( LPCTSTR ) ( operation.utf16() ),
                       ( LPCTSTR ) ( file.utf16() ),0,
                       ( LPCTSTR ) ( dir.utf16() ),SW_SHOWNORMAL );
    else
        ShellExecute ( win, ( LPCTSTR ) ( operation.utf16() ),
                       ( LPCTSTR ) ( file.utf16() ),
                       ( LPCTSTR ) ( parameters.utf16() ),
                       ( LPCTSTR ) ( dir.utf16() ),SW_SHOWNORMAL );
}

QString wapiGetDefaultPrinter()
{
    TCHAR *prName;
    DWORD length;
    GetDefaultPrinter ( 0,&length );
    if ( !length )
        return QString::null;
    prName=new TCHAR[length];
    GetDefaultPrinter ( prName,&length );
    if ( !length )
    {
        delete []prName;
        return QString::null;
    }
    QString printer=QString::fromUtf16 ( ( const ushort* ) prName );
    delete []prName;
    return printer;

}

QStringList wapiGetLocalPrinters()
{
    QStringList printers;
    PRINTER_INFO_4 *info_array;
    DWORD sizeOfArray;
    DWORD bufSize=0;
    DWORD sizeNeeded=0;
    EnumPrinters ( PRINTER_ENUM_LOCAL,0,4,NULL,bufSize,
                   &sizeNeeded,&sizeOfArray );
    if ( !sizeNeeded )
    {
        return printers;
    }
    info_array= ( PRINTER_INFO_4* ) new char[sizeNeeded];
    if ( !info_array )
        return printers;
    bufSize=sizeNeeded;
    EnumPrinters ( PRINTER_ENUM_LOCAL,0,4, ( LPBYTE ) info_array,bufSize,
                   &sizeNeeded,&sizeOfArray );
    if ( !sizeNeeded || !sizeOfArray )
    {
        delete []info_array;
        return printers;
    }
    for ( uint i=0;i<sizeOfArray;++i )
    {
        printers<<QString::fromUtf16 (
            ( const ushort* ) ( info_array[i].pPrinterName ) );
    }
    delete []info_array;
    return printers;
}
#endif