qTox  Version: nightly | Commit: bc751c8e1cac455f9690654fcfe0f560d2d7dfdd
autorun_win.cpp
Go to the documentation of this file.
1 /*
2  Copyright © 2014-2019 by The qTox Project Contributors
3 
4  This file is part of qTox, a Qt-based graphical interface for Tox.
5 
6  qTox is libre software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  qTox is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with qTox. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #include <QApplication>
22 #include "src/platform/autorun.h"
23 #include <string>
24 #include <windows.h>
25 
26 #ifdef UNICODE
27 
32 using tstring = std::wstring;
33 static inline tstring toTString(QString s)
34 {
35  return s.toStdWString();
36 }
37 #else
38 using tstring = std::string;
39 static inline tstring toTString(QString s)
40 {
41  return s.toStdString();
42 }
43 #endif
44 
45 namespace Platform {
47 {
48  return toTString("\"" + QApplication::applicationFilePath().replace('/', '\\') + "\" -p \""
49  + Settings::getInstance().getCurrentProfile() + "\"");
50 }
51 
53 {
54  return toTString("qTox - " + Settings::getInstance().getCurrentProfile());
55 }
56 }
57 
58 bool Platform::setAutorun(bool on)
59 {
60  HKEY key = nullptr;
61  if (RegOpenKeyEx(HKEY_CURRENT_USER, TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Run"),
62  0, KEY_ALL_ACCESS, &key)
63  != ERROR_SUCCESS)
64  return false;
65 
66  bool result = false;
67  tstring keyName = currentRegistryKeyName();
68 
69  if (on) {
70  tstring path = currentCommandLine();
71  result = RegSetValueEx(key, keyName.c_str(), 0, REG_SZ, const_cast<PBYTE>(reinterpret_cast<const unsigned char*>(path.c_str())),
72  path.length() * sizeof(TCHAR))
73  == ERROR_SUCCESS;
74  } else
75  result = RegDeleteValue(key, keyName.c_str()) == ERROR_SUCCESS;
76 
77  RegCloseKey(key);
78  return result;
79 }
80 
81 bool Platform::getAutorun()
82 {
83  HKEY key = nullptr;
84  if (RegOpenKeyEx(HKEY_CURRENT_USER, TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Run"),
85  0, KEY_ALL_ACCESS, &key)
86  != ERROR_SUCCESS)
87  return false;
88 
89  tstring keyName = currentRegistryKeyName();
90 
91  TCHAR path[MAX_PATH] = {0};
92  DWORD length = sizeof(path);
93  DWORD type = REG_SZ;
94  bool result = false;
95 
96  if (RegQueryValueEx(key, keyName.c_str(), nullptr, &type, const_cast<PBYTE>(reinterpret_cast<const unsigned char*>(path)), &length) == ERROR_SUCCESS
97  && type == REG_SZ)
98  result = true;
99 
100  RegCloseKey(key);
101  return result;
102 }
settings.h
Platform::currentCommandLine
tstring currentCommandLine()
Definition: autorun_win.cpp:46
Settings::getInstance
static Settings & getInstance()
Returns the singleton instance.
Definition: settings.cpp:88
Platform
Definition: autorun_win.cpp:45
autorun.h
Platform::currentRegistryKeyName
tstring currentRegistryKeyName()
Definition: autorun_win.cpp:52
tstring
std::string tstring
Definition: autorun_win.cpp:38