summaryrefslogtreecommitdiff
path: root/plugins/SessionLoader/Windows/sessionLoader.cpp
blob: 121bae0b45b96a893788c0f73da26e5104bee0a2 (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/** \file session-loader.cpp
\brief Define the session plugin loader test
\author alpha_one_x86 */

#include "sessionLoader.h"

#include <QCoreApplication>

#ifdef Q_OS_WIN32
    #ifndef NOMINMAX
        #define NOMINMAX
    #endif
    #include <windows.h>
#else
    #error "This plugin will only work under Windows"
#endif

void WindowsSessionLoader::setEnabled(const bool &newValue)
{
    ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"start, newValue: "+std::to_string(newValue));
    //set value into the variable
    HKEY    ultracopier_regkey;
    //for autostart
    QString runStringApp = "\"" + QCoreApplication::applicationFilePath() + "\"";
    runStringApp.replace( "/", "\\" );
    wchar_t windowsString[255];
    runStringApp.toWCharArray(windowsString);
    if(RegCreateKeyEx(HKEY_CURRENT_USER, TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Run"), 0, 0, REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS, 0, &ultracopier_regkey, 0)==ERROR_SUCCESS)
    {
        if(newValue)
        {
            if(RegSetValueEx(ultracopier_regkey, TEXT("ultracopier"), 0, REG_SZ, (BYTE*)windowsString, runStringApp.length()*2)!=ERROR_SUCCESS)
                ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"newValue: "+std::to_string(newValue)+" failed");
        }
        else
        {
            if(RegDeleteValue(ultracopier_regkey, TEXT("ultracopier"))!=ERROR_SUCCESS)
                ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"newValue: "+std::to_string(newValue)+" failed");
        }
        RegCloseKey(ultracopier_regkey);
    }
    else
        ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"RegCreateKeyEx: Software\\Microsoft\\Windows\\CurrentVersion\\Run failed");
}

bool WindowsSessionLoader::getEnabled() const
{
    //return the value into the variable
    HKEY    ultracopier_regkey;
    bool temp=false;
    if(RegCreateKeyEx(HKEY_CURRENT_USER, TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Run"), 0, 0, REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS, 0, &ultracopier_regkey, 0)==ERROR_SUCCESS)
    {
        DWORD kSize=254;
        if(RegQueryValueEx(ultracopier_regkey,TEXT("ultracopier"),NULL,NULL,(LPBYTE)0,&kSize) == ERROR_SUCCESS)
            temp=true;
        RegCloseKey(ultracopier_regkey);
    }
    else
        ULTRACOPIER_DEBUGCONSOLE(Ultracopier::DebugLevel_Notice,"RegCreateKeyEx: Software\\Microsoft\\Windows\\CurrentVersion\\Run failed");
    return temp;
}

void WindowsSessionLoader::setResources(OptionInterface * options,const std::string &writePath,const std::string &pluginPath,const bool &portableVersion)
{
    Q_UNUSED(options);
    Q_UNUSED(writePath);
    Q_UNUSED(pluginPath);
    Q_UNUSED(portableVersion);
}

/// \brief to get the options widget, NULL if not have
QWidget * WindowsSessionLoader::options()
{
    return NULL;
}

/// \brief to reload the translation, because the new language have been loaded
void WindowsSessionLoader::newLanguageLoaded()
{
}