summaryrefslogtreecommitdiff
path: root/shared/privilege.h
blob: 09fe0d08b442805ad09afdc8fa8909201c30cbb2 (plain)
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
#ifndef PRIVILEGE_H_INCLUDED
#define PRIVILEGE_H_INCLUDED

#include <map>
#include "zstring.h"
#include "file_error.h"
#include <wx/msw/wrapwin.h> //includes "windows.h"


namespace zen
{
#ifdef FFS_WIN
class Privileges
{
public:
    static Privileges& getInstance();

    void ensureActive(LPCTSTR privilege) //throw FileError
    {
        if (activePrivileges.find(privilege) != activePrivileges.end())
            return; //privilege already active

        if (privilegeIsActive(privilege)) //privilege was already active before starting this tool
            activePrivileges.insert(std::make_pair(privilege, false));
        else
        {
            setPrivilege(privilege, true);
            activePrivileges.insert(std::make_pair(privilege, true));
        }
    }

private:
    Privileges() {}
    Privileges(Privileges&);
    void operator=(Privileges&);

    ~Privileges() //clean up: deactivate all privileges that have been activated by this application
    {
        for (PrivBuffType::const_iterator i = activePrivileges.begin(); i != activePrivileges.end(); ++i)
            try
            {
                if (i->second)
                    Privileges::setPrivilege(i->first.c_str(), false);
            }
            catch (...) {}
    }

    static bool privilegeIsActive(LPCTSTR privilege); //throw FileError
    static void setPrivilege(LPCTSTR privilege, bool enable); //throw FileError

    typedef std::map<Zstring, bool> PrivBuffType; //bool: enabled by this application

    PrivBuffType activePrivileges;
};
#endif
}


#endif // PRIVILEGE_H_INCLUDED
bgstack15