~ubuntu-branches/debian/lenny/italc/lenny

« back to all changes in this revision

Viewing changes to lib/src/local_system.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Patrick Winnertz
  • Date: 2008-06-17 13:46:54 UTC
  • mfrom: (1.2.1 upstream) (4.1.1 gutsy)
  • Revision ID: james.westby@ubuntu.com-20080617134654-cl0gi4u524cv1ici
Tags: 1:1.0.9~rc3-1
* Package new upstream version
  - upstream ported the code to qt4.4 (Closes: #481974)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * local_system.cpp - namespace localSystem, providing an interface for
 
3
 *                    transparent usage of operating-system-specific functions
 
4
 *
 
5
 * Copyright (c) 2006-2008 Tobias Doerffel <tobydox/at/users/dot/sf/dot/net>
 
6
 *
 
7
 * This file is part of iTALC - http://italc.sourceforge.net
 
8
 *
 
9
 * This program is free software; you can redistribute it and/or
 
10
 * modify it under the terms of the GNU General Public
 
11
 * License as published by the Free Software Foundation; either
 
12
 * version 2 of the License, or (at your option) any later version.
 
13
 *
 
14
 * This program is distributed in the hope that it will be useful,
 
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
17
 * General Public License for more details.
 
18
 *
 
19
 * You should have received a copy of the GNU General Public
 
20
 * License along with this program (see COPYING); if not, write to the
 
21
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
22
 * Boston, MA 02111-1307, USA.
 
23
 *
 
24
 */
 
25
 
 
26
 
 
27
#ifdef HAVE_CONFIG_H
 
28
#include <config.h>
 
29
#endif
 
30
 
 
31
 
 
32
#include <QtCore/QCoreApplication>
 
33
#include <QtCore/QDir>
 
34
#include <QtCore/QMutex>
 
35
#include <QtCore/QProcess>
 
36
#include <QtCore/QSettings>
 
37
#include <QtCore/QDateTime>
 
38
#include <QtGui/QWidget>
 
39
#include <QtNetwork/QTcpServer>
 
40
 
 
41
 
 
42
#ifdef BUILD_WIN32
 
43
 
 
44
#include <QtCore/QLibrary>
 
45
 
 
46
static const char * tr_accels = QT_TRANSLATE_NOOP(
 
47
                "QObject",
 
48
                "UPL (note for translators: the first three characters of "
 
49
                "this string are the accellerators (underlined characters) "
 
50
                "of the three input-fields in logon-dialog of windows - "
 
51
                "please keep this note as otherwise there are strange errors "
 
52
                                        "concerning logon-feature)" );
 
53
 
 
54
#define _WIN32_WINNT 0x0501
 
55
#include <windows.h>
 
56
#include <shlobj.h>
 
57
#include <psapi.h>
 
58
 
 
59
 
 
60
 
 
61
namespace localSystem
 
62
{
 
63
 
 
64
// taken from qt-win-opensource-src-4.2.2/src/corelib/io/qsettings.cpp
 
65
QString windowsConfigPath( int _type )
 
66
{
 
67
        QString result;
 
68
 
 
69
        QLibrary library( "shell32" );
 
70
        typedef BOOL( WINAPI* GetSpecialFolderPath )( HWND, char *, int, BOOL );
 
71
        GetSpecialFolderPath SHGetSpecialFolderPath = (GetSpecialFolderPath)
 
72
                                library.resolve( "SHGetSpecialFolderPathA" );
 
73
        if( SHGetSpecialFolderPath )
 
74
        {
 
75
            char path[MAX_PATH];
 
76
            SHGetSpecialFolderPath( 0, path, _type, FALSE );
 
77
            result = QString::fromLocal8Bit( path );
 
78
        }
 
79
        return( result );
 
80
}
 
81
 
 
82
}
 
83
 
 
84
#endif
 
85
 
 
86
 
 
87
#ifdef HAVE_PWD_H
 
88
#include <pwd.h>
 
89
#endif
 
90
 
 
91
#ifdef HAVE_SYS_SOCKET_H
 
92
#include <sys/socket.h>
 
93
#endif
 
94
 
 
95
#ifdef HAVE_ARPA_INET_H
 
96
#include <arpa/inet.h>
 
97
#endif
 
98
 
 
99
#ifdef HAVE_NETINET_IN_H
 
100
#include <netinet/in.h>
 
101
#endif
 
102
 
 
103
#ifdef HAVE_ERRNO_H
 
104
#include <errno.h>
 
105
#endif
 
106
 
 
107
#include "local_system.h"
 
108
 
 
109
 
 
110
 
 
111
static localSystem::p_pressKey __pressKey;
 
112
static QString __log_file;
 
113
static QFile * __debug_out = NULL;
 
114
 
 
115
 
 
116
 
 
117
static QString properLineEnding( QString _out )
 
118
{
 
119
        if( _out.right( 1 ) != "\012" )
 
120
        {
 
121
                _out += "\012";                         
 
122
        }
 
123
#ifdef BUILD_WIN32
 
124
        if( _out.right( 1 ) != "\015" )
 
125
        {
 
126
                _out.replace( QString( "\012" ), QString( "\015\012" ) );
 
127
        }
 
128
#elif BUILD_LINUX
 
129
#else
 
130
        if( _out.right( 1 ) != "\015" ) // MAC
 
131
        {
 
132
                _out.replace( QString( "\012" ), QString( "\015" ) );
 
133
        }
 
134
#endif                  
 
135
        return( _out );
 
136
}
 
137
 
 
138
 
 
139
 
 
140
void msgHandler( QtMsgType _type, const char * _msg )
 
141
{
 
142
        if( localSystem::logLevel == 0 )
 
143
        {
 
144
                return ;
 
145
        }
 
146
#ifdef BUILD_WIN32
 
147
        if( QString( _msg ).contains( "timers cannot be stopped",
 
148
                                                        Qt::CaseInsensitive ) )
 
149
        {
 
150
                exit( 0 );
 
151
        }
 
152
#endif
 
153
        if( __debug_out == NULL )
 
154
        {
 
155
                QString tmp_path = QDir::rootPath() +
 
156
#ifdef BUILD_WIN32
 
157
                                                "temp"
 
158
#else
 
159
                                                "tmp"
 
160
#endif
 
161
                                ;
 
162
                foreach( const QString s, QProcess::systemEnvironment() )
 
163
                {
 
164
                        if( s.toLower().left( 5 ) == "temp=" )
 
165
                        {
 
166
                                tmp_path = s.toLower().mid( 5 );
 
167
                                break;
 
168
                        }
 
169
                        else if( s.toLower().left( 4 ) == "tmp=" )
 
170
                        {
 
171
                                tmp_path = s.toLower().mid( 4 );
 
172
                                break;
 
173
                        }
 
174
                }
 
175
                if( !QDir( tmp_path ).exists() )
 
176
                {
 
177
                        if( QDir( QDir::rootPath() ).mkdir( tmp_path ) )
 
178
                        {
 
179
                                QFile::setPermissions( tmp_path,
 
180
                                                QFile::ReadOwner | QFile::WriteOwner | QFile::ExeOwner |
 
181
                                                QFile::ReadUser | QFile::WriteUser | QFile::ExeUser |
 
182
                                                QFile::ReadGroup | QFile::WriteGroup | QFile::ExeGroup |
 
183
                                                QFile::ReadOther | QFile::WriteOther | QFile::ExeOther );
 
184
                        }
 
185
                }
 
186
                const QString log_path = tmp_path + QDir::separator();
 
187
                __debug_out = new QFile( log_path + __log_file );
 
188
                __debug_out->open( QFile::WriteOnly | QFile::Append |
 
189
                                                        QFile::Unbuffered );
 
190
        }
 
191
 
 
192
        QString out;
 
193
        switch( _type )
 
194
        {
 
195
                case QtDebugMsg:
 
196
                        if( localSystem::logLevel > 8)
 
197
                        {
 
198
                                out = QDateTime::currentDateTime().toString() + QString( ": [debug] %1" ).arg( _msg ) + "\n";
 
199
                        }
 
200
                        break;
 
201
                case QtWarningMsg:
 
202
                        if( localSystem::logLevel > 5 )
 
203
                        {
 
204
                                out = QDateTime::currentDateTime().toString() + QString( ": [warning] %1" ).arg( _msg ) + "\n";
 
205
                        }
 
206
                        break;
 
207
                case QtCriticalMsg:
 
208
                        if( localSystem::logLevel > 3 )
 
209
                        {
 
210
                                out = QDateTime::currentDateTime().toString() + QString( ": [critical] %1" ).arg( _msg ) + "\n";
 
211
                        }
 
212
                        break;
 
213
                case QtFatalMsg:
 
214
                        if( localSystem::logLevel > 1 )
 
215
                        {
 
216
                                out = QDateTime::currentDateTime().toString() + QString( ": [fatal] %1" ).arg( _msg ) + "\n";
 
217
                        }
 
218
                default:
 
219
                        out = QDateTime::currentDateTime().toString() + QString( ": [unknown] %1" ).arg( _msg ) + "\n";
 
220
                        break;
 
221
        }
 
222
        if( out.trimmed().size() )
 
223
        {
 
224
                out = properLineEnding( out );
 
225
                __debug_out->write( out.toUtf8() );
 
226
                printf( "%s", out.toUtf8().constData() );
 
227
        }
 
228
}
 
229
 
 
230
 
 
231
void initResources( void )
 
232
{
 
233
        Q_INIT_RESOURCE(italc_core);
 
234
}
 
235
 
 
236
namespace localSystem
 
237
{
 
238
 
 
239
int IC_DllExport logLevel = 6;
 
240
 
 
241
 
 
242
void initialize( p_pressKey _pk, const QString & _log_file )
 
243
{
 
244
        __pressKey = _pk;
 
245
        __log_file = _log_file;
 
246
 
 
247
        QCoreApplication::setOrganizationName( "iTALC Solutions" );
 
248
        QCoreApplication::setOrganizationDomain( "italcsolutions.org" );
 
249
        QCoreApplication::setApplicationName( "iTALC" );
 
250
 
 
251
        QSettings settings( QSettings::SystemScope, "iTALC Solutions", "iTALC" );
 
252
        
 
253
        if( settings.contains( "settings/LogLevel" ) )
 
254
        {
 
255
                logLevel = settings.value( "settings/LogLevel" ).toInt();
 
256
        }
 
257
 
 
258
        qInstallMsgHandler( msgHandler );
 
259
 
 
260
        initResources();
 
261
}
 
262
 
 
263
 
 
264
 
 
265
 
 
266
int freePort( int _default_port )
 
267
{
 
268
        QTcpServer t;
 
269
        if( t.listen( QHostAddress::LocalHost, _default_port ) )
 
270
        {
 
271
                return( _default_port );
 
272
        }
 
273
        t.listen( QHostAddress::LocalHost );
 
274
        return( t.serverPort() );
 
275
}
 
276
 
 
277
 
 
278
 
 
279
 
 
280
void sleep( const int _ms )
 
281
{
 
282
#ifdef BUILD_WIN32
 
283
        Sleep( static_cast<unsigned int>( _ms ) );
 
284
#else
 
285
        struct timespec ts = { _ms / 1000, ( _ms % 1000 ) * 1000 * 1000 } ;
 
286
        nanosleep( &ts, NULL );
 
287
#endif
 
288
}
 
289
 
 
290
 
 
291
 
 
292
 
 
293
void execInTerminal( const QString & _cmds )
 
294
{
 
295
        QProcess::startDetached(
 
296
#ifdef BUILD_WIN32
 
297
                        "cmd " +
 
298
#else
 
299
                        "xterm -e " +
 
300
#endif
 
301
                        _cmds );
 
302
}
 
303
 
 
304
 
 
305
 
 
306
 
 
307
void broadcastWOLPacket( const QString & _mac )
 
308
{
 
309
        const int PORT_NUM = 65535;
 
310
        const int MAC_SIZE = 6;
 
311
        const int OUTBUF_SIZE = MAC_SIZE*17;
 
312
        unsigned char mac[MAC_SIZE];
 
313
        char out_buf[OUTBUF_SIZE];
 
314
 
 
315
        if( sscanf( _mac.toAscii().constData(),
 
316
                                "%2x:%2x:%2x:%2x:%2x:%2x",
 
317
                                (unsigned int *) &mac[0],
 
318
                                (unsigned int *) &mac[1],
 
319
                                (unsigned int *) &mac[2],
 
320
                                (unsigned int *) &mac[3],
 
321
                                (unsigned int *) &mac[4],
 
322
                                (unsigned int *) &mac[5] ) != MAC_SIZE )
 
323
        {
 
324
                qWarning( "invalid MAC-address" );
 
325
                return;
 
326
        }
 
327
 
 
328
        for( int i = 0; i < MAC_SIZE; ++i )
 
329
        {
 
330
                out_buf[i] = 0xff;        
 
331
        }
 
332
 
 
333
        for( int i = 1; i < 17; ++i )
 
334
        {
 
335
                for(int j = 0; j < MAC_SIZE; ++j )
 
336
                {
 
337
                        out_buf[i*MAC_SIZE+j] = mac[j];
 
338
                }
 
339
        }
 
340
 
 
341
#ifdef BUILD_WIN32
 
342
        WSADATA info;
 
343
        if( WSAStartup( MAKEWORD( 1, 1 ), &info ) != 0 )
 
344
        {
 
345
                qCritical( "cannot initialize WinSock!" );
 
346
                return;
 
347
        }
 
348
#endif
 
349
 
 
350
        // UDP-broadcast the MAC-address
 
351
        unsigned int sock = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP );
 
352
        struct sockaddr_in my_addr;
 
353
        my_addr.sin_family      = AF_INET;            // Address family to use
 
354
        my_addr.sin_port        = htons( PORT_NUM );    // Port number to use
 
355
        my_addr.sin_addr.s_addr = inet_addr( "255.255.255.255" ); // send to
 
356
                                                                  // IP_ADDR
 
357
 
 
358
        int optval = 1;
 
359
        if( setsockopt( sock, SOL_SOCKET, SO_BROADCAST, (char *) &optval,
 
360
                                                        sizeof( optval ) ) < 0 )
 
361
        {
 
362
                qCritical( "can't set sockopt (%d).", errno );
 
363
                return;
 
364
        }
 
365
 
 
366
        sendto( sock, out_buf, sizeof( out_buf ), 0,
 
367
                        (struct sockaddr*) &my_addr, sizeof( my_addr ) );
 
368
#ifdef BUILD_WIN32
 
369
        closesocket( sock );
 
370
        WSACleanup();
 
371
#else
 
372
        close( sock );
 
373
#endif
 
374
 
 
375
 
 
376
#if 0
 
377
#ifdef BUILD_LINUX
 
378
        QProcess::startDetached( "etherwake " + _mac );
 
379
#endif
 
380
#endif
 
381
}
 
382
 
 
383
 
 
384
 
 
385
static inline void pressAndReleaseKey( int _key )
 
386
{
 
387
        __pressKey( _key, TRUE );
 
388
        __pressKey( _key, FALSE );
 
389
}
 
390
 
 
391
 
 
392
void logonUser( const QString & _uname, const QString & _passwd,
 
393
                                                const QString & _domain )
 
394
{
 
395
#ifdef BUILD_WIN32
 
396
 
 
397
        // first check for process "explorer.exe" - if we find it, a user
 
398
        // is logged in and we do not send our key-sequences as it probably
 
399
        // disturbes user
 
400
        DWORD aProcesses[1024], cbNeeded;
 
401
 
 
402
        if( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
 
403
        {
 
404
                return;
 
405
        }
 
406
 
 
407
        DWORD cProcesses = cbNeeded / sizeof(DWORD);
 
408
 
 
409
        bool user_logged_on = FALSE;
 
410
        for( DWORD i = 0; i < cProcesses; i++ )
 
411
        {
 
412
                HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
 
413
                                                                PROCESS_VM_READ,
 
414
                                                        FALSE, aProcesses[i] );
 
415
                HMODULE hMod;
 
416
                if( hProcess == NULL ||
 
417
                        !EnumProcessModules( hProcess, &hMod, sizeof( hMod ),
 
418
                                                                &cbNeeded ) )
 
419
                {
 
420
                        continue;
 
421
                }
 
422
                TCHAR szProcessName[MAX_PATH];
 
423
                GetModuleBaseName( hProcess, hMod, szProcessName, 
 
424
                                          sizeof(szProcessName)/sizeof(TCHAR) );
 
425
                for( TCHAR * ptr = szProcessName; *ptr; ++ptr )
 
426
                {
 
427
                        *ptr = tolower( *ptr );
 
428
                }
 
429
                if( strcmp( szProcessName, "explorer.exe" ) == 0 )
 
430
                {
 
431
                        user_logged_on = TRUE;
 
432
                        break;
 
433
                }
 
434
        }
 
435
 
 
436
        if( user_logged_on )
 
437
        {
 
438
                return;
 
439
        }
 
440
 
 
441
        // disable caps lock
 
442
        if( GetKeyState( VK_CAPITAL ) & 1 )
 
443
        {
 
444
                INPUT input[2];
 
445
                ZeroMemory( input, sizeof( input ) );        
 
446
                input[0].type = input[1].type = INPUT_KEYBOARD;
 
447
                input[0].ki.wVk = input[1].ki.wVk = VK_CAPITAL;        
 
448
                input[1].ki.dwFlags = KEYEVENTF_KEYUP;
 
449
                SendInput( 2, input, sizeof( INPUT ) );
 
450
        }
 
451
 
 
452
        pressAndReleaseKey( XK_Escape );
 
453
        pressAndReleaseKey( XK_Escape );
 
454
 
 
455
        // send Secure Attention Sequence (SAS) for making sure we can enter
 
456
        // username and password
 
457
        __pressKey( XK_Alt_L, TRUE );
 
458
        __pressKey( XK_Control_L, TRUE );
 
459
        pressAndReleaseKey( XK_Delete );
 
460
        __pressKey( XK_Control_L, FALSE );
 
461
        __pressKey( XK_Alt_L, FALSE );
 
462
 
 
463
        const ushort * accels = QObject::tr( tr_accels ).utf16();
 
464
 
 
465
        /* Need to handle 2 cases here; if an interactive login message is
 
466
         * defined in policy, this window will be displayed with an "OK" button;
 
467
         * if not the login window will be displayed. Sending a space will
 
468
         * dismiss the message, but will also add a space to the currently
 
469
         * selected field if the login windows is active. The solution is:
 
470
         *  1. send the "username" field accelerator (which won't do anything
 
471
         *     to the message window, but will select the "username" field of
 
472
         *     the login window if it is active)
 
473
         *  2. Send a space keypress to dismiss the message. (adds a space
 
474
         *     to username field of login window if active)
 
475
         *  3. Send the "username" field accelerator again; which will select
 
476
         *     the username field for the case where the message was displayed
 
477
         *  4. Send a backspace keypress to remove any space that was added to
 
478
         *     the username field if there is no message.
 
479
         */
 
480
        __pressKey( XK_Alt_L, TRUE );
 
481
        pressAndReleaseKey( accels[0] );
 
482
        __pressKey( XK_Alt_L, FALSE );
 
483
 
 
484
        pressAndReleaseKey( XK_space );
 
485
 
 
486
        __pressKey( XK_Alt_L, TRUE );
 
487
        pressAndReleaseKey( accels[0] );
 
488
        __pressKey( XK_Alt_L, FALSE );
 
489
 
 
490
        pressAndReleaseKey( XK_BackSpace );
 
491
#endif
 
492
 
 
493
        for( int i = 0; i < _uname.size(); ++i )
 
494
        {
 
495
                pressAndReleaseKey( _uname.utf16()[i] );
 
496
        }
 
497
 
 
498
#ifdef BUILD_WIN32
 
499
        __pressKey( XK_Alt_L, TRUE );
 
500
        pressAndReleaseKey( accels[1] );
 
501
        __pressKey( XK_Alt_L, FALSE );
 
502
#else
 
503
        pressAndReleaseKey( XK_Tab );
 
504
#endif
 
505
 
 
506
        for( int i = 0; i < _passwd.size(); ++i )
 
507
        {
 
508
                pressAndReleaseKey( _passwd.utf16()[i] );
 
509
        }
 
510
 
 
511
#ifdef BUILD_WIN32
 
512
        if( !_domain.isEmpty() )
 
513
        {
 
514
                __pressKey( XK_Alt_L, TRUE );
 
515
                pressAndReleaseKey( accels[2] );
 
516
                __pressKey( XK_Alt_L, FALSE );
 
517
                for( int i = 0; i < _domain.size(); ++i )
 
518
                {
 
519
                        pressAndReleaseKey( _domain.utf16()[i] );
 
520
                }
 
521
        }
 
522
#endif
 
523
 
 
524
        pressAndReleaseKey( XK_Return );
 
525
}
 
526
 
 
527
 
 
528
 
 
529
 
 
530
static const QString userRoleNames[] =
 
531
{
 
532
        "none",
 
533
        "teacher",
 
534
        "admin",
 
535
        "supporter",
 
536
        "other"
 
537
} ;
 
538
 
 
539
 
 
540
QString userRoleName( const ISD::userRoles _role )
 
541
{
 
542
        return( userRoleNames[_role] );
 
543
}
 
544
 
 
545
 
 
546
inline QString keyPath( const ISD::userRoles _role, const QString _group,
 
547
                                                        bool _only_path )
 
548
{
 
549
        QSettings settings( QSettings::SystemScope, "iTALC Solutions",
 
550
                                                                "iTALC" );
 
551
        if( _role <= ISD::RoleNone || _role >= ISD::RoleCount )
 
552
        {
 
553
                qWarning( "invalid role" );
 
554
                return( "" );
 
555
        }
 
556
        const QString fallback_dir =
 
557
#ifdef BUILD_LINUX
 
558
                "/etc/italc/keys/"
 
559
#elif BUILD_WIN32
 
560
                "c:\\italc\\keys\\"
 
561
#endif
 
562
                + _group + QDir::separator() + userRoleNames[_role] +
 
563
                                                QDir::separator() +
 
564
                                                ( _only_path ? "" : "key" );
 
565
        const QString val = settings.value( "keypaths" + _group + "/" +
 
566
                                        userRoleNames[_role] ).toString();
 
567
        if( val.isEmpty() )
 
568
        {
 
569
                settings.setValue( "keypaths" + _group + "/" +
 
570
                                        userRoleNames[_role], fallback_dir );
 
571
                return( fallback_dir );
 
572
        }
 
573
        else
 
574
        {
 
575
                if( _only_path && val.right( 4 ) == "\\key" )
 
576
                {
 
577
                        return( val.left( val.size() - 4 ) );
 
578
                }
 
579
        }
 
580
        return( val );
 
581
}
 
582
 
 
583
 
 
584
QString privateKeyPath( const ISD::userRoles _role, bool _only_path )
 
585
{
 
586
        return( keyPath( _role, "private", _only_path ) );
 
587
}
 
588
 
 
589
 
 
590
QString publicKeyPath( const ISD::userRoles _role, bool _only_path )
 
591
{
 
592
        return( keyPath( _role, "public", _only_path ) );
 
593
}
 
594
 
 
595
 
 
596
 
 
597
 
 
598
void setKeyPath( QString _path, const ISD::userRoles _role,
 
599
                                                        const QString _group )
 
600
{
 
601
        _path = _path.left( 1 ) + _path.mid( 1 ).
 
602
                replace( QString( QDir::separator() ) + QDir::separator(),
 
603
                                                        QDir::separator() );
 
604
 
 
605
        QSettings settings( QSettings::SystemScope, "iTALC Solutions",
 
606
                                                                "iTALC" );
 
607
        if( _role <= ISD::RoleNone || _role >= ISD::RoleCount )
 
608
        {
 
609
                qWarning( "invalid role" );
 
610
                return;
 
611
        }
 
612
        settings.setValue( "keypaths" + _group + "/" +
 
613
                                                userRoleNames[_role], _path );
 
614
}
 
615
 
 
616
 
 
617
void setPrivateKeyPath( const QString & _path, const ISD::userRoles _role )
 
618
{
 
619
        setKeyPath( _path, _role, "private" );
 
620
}
 
621
 
 
622
 
 
623
 
 
624
 
 
625
void setPublicKeyPath( const QString & _path, const ISD::userRoles _role )
 
626
{
 
627
        setKeyPath( _path, _role, "public" );
 
628
}
 
629
 
 
630
 
 
631
 
 
632
 
 
633
QString snapshotDir( void )
 
634
{
 
635
        QSettings settings;
 
636
        return( settings.value( "paths/snapshots",
 
637
#ifdef BUILD_WIN32
 
638
                                windowsConfigPath( CSIDL_PERSONAL ) +
 
639
                                        QDir::separator() +
 
640
                                        QObject::tr( "iTALC-snapshots" )
 
641
#else
 
642
                                personalConfigDir() + "snapshots"
 
643
#endif
 
644
                                        ).toString() + QDir::separator() );
 
645
}
 
646
 
 
647
 
 
648
 
 
649
 
 
650
QString globalConfigPath( void )
 
651
{
 
652
        QSettings settings;
 
653
        return( settings.value( "paths/globalconfig", personalConfigDir() +
 
654
                                        "globalconfig.xml" ).toString() );
 
655
}
 
656
 
 
657
 
 
658
 
 
659
 
 
660
QString personalConfigDir( void )
 
661
{
 
662
        QSettings settings;
 
663
        const QString d = settings.value( "paths/personalconfig" ).toString();
 
664
        return( d.isEmpty() ?
 
665
#ifdef BUILD_WIN32
 
666
                                windowsConfigPath( CSIDL_APPDATA ) +
 
667
                                                QDir::separator() + "iTALC"
 
668
#else
 
669
                                QDir::homePath() + QDir::separator() +
 
670
                                                                ".italc"
 
671
#endif
 
672
                                + QDir::separator()
 
673
                :
 
674
                                d );
 
675
}
 
676
 
 
677
 
 
678
 
 
679
 
 
680
QString personalConfigPath( void )
 
681
{
 
682
        QSettings settings;
 
683
        const QString d = settings.value( "paths/personalconfig" ).toString();
 
684
        return( d.isEmpty() ?
 
685
                                personalConfigDir() + "personalconfig.xml"
 
686
                        :
 
687
                                d );
 
688
}
 
689
 
 
690
 
 
691
 
 
692
 
 
693
QString globalStartmenuDir( void )
 
694
{
 
695
#ifdef BUILD_WIN32
 
696
        return( windowsConfigPath( CSIDL_COMMON_STARTMENU ) +
 
697
                                                        QDir::separator() );
 
698
#else
 
699
        return( "/usr/share/applnk/Applications/" );
 
700
#endif
 
701
}
 
702
 
 
703
 
 
704
 
 
705
 
 
706
QString parameter( const QString & _name )
 
707
{
 
708
        return( QSettings().value( "parameters/" + _name ).toString() );
 
709
}
 
710
 
 
711
 
 
712
 
 
713
 
 
714
bool ensurePathExists( const QString & _path )
 
715
{
 
716
        if( _path.isEmpty() || QDir( _path ).exists() )
 
717
        {
 
718
                return( TRUE );
 
719
        }
 
720
 
 
721
        QString p = QDir( _path ).absolutePath();
 
722
        if( !QFileInfo( _path ).isDir() )
 
723
        {
 
724
                p = QFileInfo( _path ).absolutePath();
 
725
        }
 
726
        QStringList dirs;
 
727
        while( !QDir( p ).exists() && !p.isEmpty() )
 
728
        {
 
729
                dirs.push_front( QDir( p ).dirName() );
 
730
                p.chop( dirs.front().size() + 1 );
 
731
        }
 
732
        if( !p.isEmpty() )
 
733
        {
 
734
                return( QDir( p ).mkpath( dirs.join( QDir::separator() ) ) );
 
735
        }
 
736
        return( FALSE );
 
737
}
 
738
 
 
739
 
 
740
 
 
741
#ifdef BUILD_WIN32
 
742
BOOL enablePrivilege( LPCTSTR lpszPrivilegeName, BOOL bEnable )
 
743
{
 
744
        HANDLE                  hToken;
 
745
        TOKEN_PRIVILEGES        tp;
 
746
        LUID                    luid;
 
747
        BOOL                    ret;
 
748
 
 
749
        if( !OpenProcessToken( GetCurrentProcess(),
 
750
                TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY | TOKEN_READ, &hToken ) )
 
751
        {
 
752
                return FALSE;
 
753
        }
 
754
 
 
755
        if( !LookupPrivilegeValue( NULL, lpszPrivilegeName, &luid ) )
 
756
        {
 
757
                return FALSE;
 
758
        }
 
759
 
 
760
        tp.PrivilegeCount           = 1;
 
761
        tp.Privileges[0].Luid       = luid;
 
762
        tp.Privileges[0].Attributes = bEnable ? SE_PRIVILEGE_ENABLED : 0;
 
763
 
 
764
        ret = AdjustTokenPrivileges( hToken, FALSE, &tp, 0, NULL, NULL );
 
765
 
 
766
        CloseHandle( hToken );
 
767
 
 
768
        return ret;
 
769
}
 
770
#endif
 
771
 
 
772
 
 
773
 
 
774
void activateWindow( QWidget * _w )
 
775
{
 
776
        _w->activateWindow();
 
777
        _w->raise();
 
778
#ifdef BUILD_WIN32
 
779
        SetWindowPos( _w->winId(), HWND_TOPMOST, 0, 0, 0, 0,
 
780
                                                SWP_NOMOVE | SWP_NOSIZE );
 
781
        SetWindowPos( _w->winId(), HWND_NOTOPMOST, 0, 0, 0, 0,
 
782
                                                SWP_NOMOVE | SWP_NOSIZE );
 
783
#endif
 
784
}
 
785
 
 
786
 
 
787
} // end of namespace localSystem
 
788