Skip to content

Commit 67d4cba

Browse files
committed
Move GetStartOnSystemStartup and SetStartOnSystemStartup to GUI code
1 parent 82f6608 commit 67d4cba

File tree

7 files changed

+155
-154
lines changed

7 files changed

+155
-154
lines changed

src/init.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -656,11 +656,6 @@ bool AppInit2(int argc, char* argv[])
656656
if (fServer)
657657
CreateThread(ThreadRPCServer, NULL);
658658

659-
#ifdef QT_GUI
660-
if (GetStartOnSystemStartup())
661-
SetStartOnSystemStartup(true); // Remove startup links
662-
#endif
663-
664659
#if !defined(QT_GUI)
665660
while (1)
666661
Sleep(5000);

src/qt/bitcoin.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,10 @@ int main(int argc, char *argv[])
232232

233233
try
234234
{
235+
// Regenerate startup link, to fix links to old versions
236+
if (GUIUtil::GetStartOnSystemStartup())
237+
GUIUtil::SetStartOnSystemStartup(true);
238+
235239
BitcoinGUI window;
236240
guiref = &window;
237241
if(AppInit2(argc, argv))

src/qt/guiutil.cpp

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <QThread>
2020

2121
#include <boost/filesystem.hpp>
22+
#include <boost/filesystem/fstream.hpp>
2223

2324
#ifdef WIN32
2425
#ifdef _WIN32_WINNT
@@ -268,5 +269,149 @@ bool ToolTipToRichTextFilter::eventFilter(QObject *obj, QEvent *evt)
268269
return QObject::eventFilter(obj, evt);
269270
}
270271

272+
#ifdef WIN32
273+
boost::filesystem::path static StartupShortcutPath()
274+
{
275+
return GetSpecialFolderPath(CSIDL_STARTUP) / "Bitcoin.lnk";
276+
}
277+
278+
bool GetStartOnSystemStartup()
279+
{
280+
// check for Bitcoin.lnk
281+
return boost::filesystem::exists(StartupShortcutPath());
282+
}
283+
284+
bool SetStartOnSystemStartup(bool fAutoStart)
285+
{
286+
// If the shortcut exists already, remove it for updating
287+
boost::filesystem::remove(StartupShortcutPath());
288+
289+
if (fAutoStart)
290+
{
291+
CoInitialize(NULL);
292+
293+
// Get a pointer to the IShellLink interface.
294+
IShellLink* psl = NULL;
295+
HRESULT hres = CoCreateInstance(CLSID_ShellLink, NULL,
296+
CLSCTX_INPROC_SERVER, IID_IShellLink,
297+
reinterpret_cast<void**>(&psl));
298+
299+
if (SUCCEEDED(hres))
300+
{
301+
// Get the current executable path
302+
TCHAR pszExePath[MAX_PATH];
303+
GetModuleFileName(NULL, pszExePath, sizeof(pszExePath));
304+
305+
TCHAR pszArgs[5] = TEXT("-min");
306+
307+
// Set the path to the shortcut target
308+
psl->SetPath(pszExePath);
309+
PathRemoveFileSpec(pszExePath);
310+
psl->SetWorkingDirectory(pszExePath);
311+
psl->SetShowCmd(SW_SHOWMINNOACTIVE);
312+
psl->SetArguments(pszArgs);
313+
314+
// Query IShellLink for the IPersistFile interface for
315+
// saving the shortcut in persistent storage.
316+
IPersistFile* ppf = NULL;
317+
hres = psl->QueryInterface(IID_IPersistFile,
318+
reinterpret_cast<void**>(&ppf));
319+
if (SUCCEEDED(hres))
320+
{
321+
WCHAR pwsz[MAX_PATH];
322+
// Ensure that the string is ANSI.
323+
MultiByteToWideChar(CP_ACP, 0, StartupShortcutPath().string().c_str(), -1, pwsz, MAX_PATH);
324+
// Save the link by calling IPersistFile::Save.
325+
hres = ppf->Save(pwsz, TRUE);
326+
ppf->Release();
327+
psl->Release();
328+
CoUninitialize();
329+
return true;
330+
}
331+
psl->Release();
332+
}
333+
CoUninitialize();
334+
return false;
335+
}
336+
return true;
337+
}
338+
339+
#elif defined(LINUX)
340+
341+
// Follow the Desktop Application Autostart Spec:
342+
// http://standards.freedesktop.org/autostart-spec/autostart-spec-latest.html
343+
344+
boost::filesystem::path static GetAutostartDir()
345+
{
346+
namespace fs = boost::filesystem;
347+
348+
char* pszConfigHome = getenv("XDG_CONFIG_HOME");
349+
if (pszConfigHome) return fs::path(pszConfigHome) / "autostart";
350+
char* pszHome = getenv("HOME");
351+
if (pszHome) return fs::path(pszHome) / ".config" / "autostart";
352+
return fs::path();
353+
}
354+
355+
boost::filesystem::path static GetAutostartFilePath()
356+
{
357+
return GetAutostartDir() / "bitcoin.desktop";
358+
}
359+
360+
bool GetStartOnSystemStartup()
361+
{
362+
boost::filesystem::ifstream optionFile(GetAutostartFilePath());
363+
if (!optionFile.good())
364+
return false;
365+
// Scan through file for "Hidden=true":
366+
std::string line;
367+
while (!optionFile.eof())
368+
{
369+
getline(optionFile, line);
370+
if (line.find("Hidden") != std::string::npos &&
371+
line.find("true") != std::string::npos)
372+
return false;
373+
}
374+
optionFile.close();
375+
376+
return true;
377+
}
378+
379+
bool SetStartOnSystemStartup(bool fAutoStart)
380+
{
381+
if (!fAutoStart)
382+
boost::filesystem::remove(GetAutostartFilePath());
383+
else
384+
{
385+
char pszExePath[MAX_PATH+1];
386+
memset(pszExePath, 0, sizeof(pszExePath));
387+
if (readlink("/proc/self/exe", pszExePath, sizeof(pszExePath)-1) == -1)
388+
return false;
389+
390+
boost::filesystem::create_directories(GetAutostartDir());
391+
392+
boost::filesystem::ofstream optionFile(GetAutostartFilePath(), std::ios_base::out|std::ios_base::trunc);
393+
if (!optionFile.good())
394+
return false;
395+
// Write a bitcoin.desktop file to the autostart directory:
396+
optionFile << "[Desktop Entry]\n";
397+
optionFile << "Type=Application\n";
398+
optionFile << "Name=Bitcoin\n";
399+
optionFile << "Exec=" << pszExePath << " -min\n";
400+
optionFile << "Terminal=false\n";
401+
optionFile << "Hidden=false\n";
402+
optionFile.close();
403+
}
404+
return true;
405+
}
406+
#else
407+
408+
// TODO: OSX startup stuff; see:
409+
// http://developer.apple.com/mac/library/documentation/MacOSX/Conceptual/BPSystemStartup/Articles/CustomLogin.html
410+
411+
bool GetStartOnSystemStartup() { return false; }
412+
bool SetStartOnSystemStartup(bool fAutoStart) { return false; }
413+
414+
#endif
415+
271416
} // namespace GUIUtil
272417

src/qt/guiutil.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ namespace GUIUtil
9090
int size_threshold;
9191
};
9292

93+
bool GetStartOnSystemStartup();
94+
bool SetStartOnSystemStartup(bool fAutoStart);
95+
9396
} // namespace GUIUtil
9497

9598
#endif // GUIUTIL_H

src/qt/optionsmodel.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "init.h"
66
#include "walletdb.h"
7+
#include "guiutil.h"
78

89
OptionsModel::OptionsModel(QObject *parent) :
910
QAbstractListModel(parent)
@@ -107,7 +108,7 @@ QVariant OptionsModel::data(const QModelIndex & index, int role) const
107108
switch(index.row())
108109
{
109110
case StartAtStartup:
110-
return QVariant(GetStartOnSystemStartup());
111+
return QVariant(GUIUtil::GetStartOnSystemStartup());
111112
case MinimizeToTray:
112113
return QVariant(fMinimizeToTray);
113114
case MapPortUPnP:
@@ -146,7 +147,7 @@ bool OptionsModel::setData(const QModelIndex & index, const QVariant & value, in
146147
switch(index.row())
147148
{
148149
case StartAtStartup:
149-
successful = SetStartOnSystemStartup(value.toBool());
150+
successful = GUIUtil::SetStartOnSystemStartup(value.toBool());
150151
break;
151152
case MinimizeToTray:
152153
fMinimizeToTray = value.toBool();

src/util.cpp

Lines changed: 0 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ namespace boost {
4848
#define NOMINMAX
4949
#endif
5050
#include "shlobj.h"
51-
#include "shlwapi.h"
5251
#endif
5352

5453
using namespace std;
@@ -1078,146 +1077,4 @@ boost::filesystem::path GetSpecialFolderPath(int nFolder, bool fCreate)
10781077
printf("SHGetSpecialFolderPathA() failed, could not obtain requested path.\n");
10791078
return fs::path("");
10801079
}
1081-
1082-
boost::filesystem::path static StartupShortcutPath()
1083-
{
1084-
return GetSpecialFolderPath(CSIDL_STARTUP) / "Bitcoin.lnk";
1085-
}
1086-
1087-
bool GetStartOnSystemStartup()
1088-
{
1089-
// check for Bitcoin.lnk
1090-
return boost::filesystem::exists(StartupShortcutPath());
1091-
}
1092-
1093-
bool SetStartOnSystemStartup(bool fAutoStart)
1094-
{
1095-
// If the shortcut exists already, remove it for updating
1096-
boost::filesystem::remove(StartupShortcutPath());
1097-
1098-
if (fAutoStart)
1099-
{
1100-
CoInitialize(NULL);
1101-
1102-
// Get a pointer to the IShellLink interface.
1103-
IShellLink* psl = NULL;
1104-
HRESULT hres = CoCreateInstance(CLSID_ShellLink, NULL,
1105-
CLSCTX_INPROC_SERVER, IID_IShellLink,
1106-
reinterpret_cast<void**>(&psl));
1107-
1108-
if (SUCCEEDED(hres))
1109-
{
1110-
// Get the current executable path
1111-
TCHAR pszExePath[MAX_PATH];
1112-
GetModuleFileName(NULL, pszExePath, sizeof(pszExePath));
1113-
1114-
TCHAR pszArgs[5] = TEXT("-min");
1115-
1116-
// Set the path to the shortcut target
1117-
psl->SetPath(pszExePath);
1118-
PathRemoveFileSpec(pszExePath);
1119-
psl->SetWorkingDirectory(pszExePath);
1120-
psl->SetShowCmd(SW_SHOWMINNOACTIVE);
1121-
psl->SetArguments(pszArgs);
1122-
1123-
// Query IShellLink for the IPersistFile interface for
1124-
// saving the shortcut in persistent storage.
1125-
IPersistFile* ppf = NULL;
1126-
hres = psl->QueryInterface(IID_IPersistFile,
1127-
reinterpret_cast<void**>(&ppf));
1128-
if (SUCCEEDED(hres))
1129-
{
1130-
WCHAR pwsz[MAX_PATH];
1131-
// Ensure that the string is ANSI.
1132-
MultiByteToWideChar(CP_ACP, 0, StartupShortcutPath().string().c_str(), -1, pwsz, MAX_PATH);
1133-
// Save the link by calling IPersistFile::Save.
1134-
hres = ppf->Save(pwsz, TRUE);
1135-
ppf->Release();
1136-
psl->Release();
1137-
CoUninitialize();
1138-
return true;
1139-
}
1140-
psl->Release();
1141-
}
1142-
CoUninitialize();
1143-
return false;
1144-
}
1145-
return true;
1146-
}
1147-
1148-
#elif defined(LINUX)
1149-
1150-
// Follow the Desktop Application Autostart Spec:
1151-
// http://standards.freedesktop.org/autostart-spec/autostart-spec-latest.html
1152-
1153-
boost::filesystem::path static GetAutostartDir()
1154-
{
1155-
namespace fs = boost::filesystem;
1156-
1157-
char* pszConfigHome = getenv("XDG_CONFIG_HOME");
1158-
if (pszConfigHome) return fs::path(pszConfigHome) / "autostart";
1159-
char* pszHome = getenv("HOME");
1160-
if (pszHome) return fs::path(pszHome) / ".config" / "autostart";
1161-
return fs::path();
1162-
}
1163-
1164-
boost::filesystem::path static GetAutostartFilePath()
1165-
{
1166-
return GetAutostartDir() / "bitcoin.desktop";
1167-
}
1168-
1169-
bool GetStartOnSystemStartup()
1170-
{
1171-
boost::filesystem::ifstream optionFile(GetAutostartFilePath());
1172-
if (!optionFile.good())
1173-
return false;
1174-
// Scan through file for "Hidden=true":
1175-
string line;
1176-
while (!optionFile.eof())
1177-
{
1178-
getline(optionFile, line);
1179-
if (line.find("Hidden") != string::npos &&
1180-
line.find("true") != string::npos)
1181-
return false;
1182-
}
1183-
optionFile.close();
1184-
1185-
return true;
1186-
}
1187-
1188-
bool SetStartOnSystemStartup(bool fAutoStart)
1189-
{
1190-
if (!fAutoStart)
1191-
boost::filesystem::remove(GetAutostartFilePath());
1192-
else
1193-
{
1194-
char pszExePath[MAX_PATH+1];
1195-
memset(pszExePath, 0, sizeof(pszExePath));
1196-
if (readlink("/proc/self/exe", pszExePath, sizeof(pszExePath)-1) == -1)
1197-
return false;
1198-
1199-
boost::filesystem::create_directories(GetAutostartDir());
1200-
1201-
boost::filesystem::ofstream optionFile(GetAutostartFilePath(), ios_base::out|ios_base::trunc);
1202-
if (!optionFile.good())
1203-
return false;
1204-
// Write a bitcoin.desktop file to the autostart directory:
1205-
optionFile << "[Desktop Entry]\n";
1206-
optionFile << "Type=Application\n";
1207-
optionFile << "Name=Bitcoin\n";
1208-
optionFile << "Exec=" << pszExePath << " -min\n";
1209-
optionFile << "Terminal=false\n";
1210-
optionFile << "Hidden=false\n";
1211-
optionFile.close();
1212-
}
1213-
return true;
1214-
}
1215-
#else
1216-
1217-
// TODO: OSX startup stuff; see:
1218-
// http://developer.apple.com/mac/library/documentation/MacOSX/Conceptual/BPSystemStartup/Articles/CustomLogin.html
1219-
1220-
bool GetStartOnSystemStartup() { return false; }
1221-
bool SetStartOnSystemStartup(bool fAutoStart) { return false; }
1222-
12231080
#endif

src/util.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,6 @@ void ReadConfigFile(std::map<std::string, std::string>& mapSettingsRet, std::map
162162
#ifdef WIN32
163163
boost::filesystem::path GetSpecialFolderPath(int nFolder, bool fCreate = true);
164164
#endif
165-
bool GetStartOnSystemStartup();
166-
bool SetStartOnSystemStartup(bool fAutoStart);
167165
void ShrinkDebugFile();
168166
int GetRandInt(int nMax);
169167
uint64 GetRand(uint64 nMax);
@@ -182,8 +180,6 @@ void AddTimeData(const CNetAddr& ip, int64 nTime);
182180

183181

184182

185-
186-
187183
inline std::string i64tostr(int64 n)
188184
{
189185
return strprintf("%"PRI64d, n);

0 commit comments

Comments
 (0)