Skip to content

Commit

Permalink
added command "Troubleshooting -> Reset to Factory Defaults" to tray …
Browse files Browse the repository at this point in the history
…menu (Windows only)
  • Loading branch information
hugbug committed Mar 19, 2015
1 parent 449ea24 commit 5a4b503
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 13 deletions.
141 changes: 141 additions & 0 deletions daemon/windows/WinConsole.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,10 @@ void WinConsole::ShowMenu()
case ID_TROUBLESHOOTING_OPENCONFIG:
OpenConfigFileInTextEdit();
break;

case ID_TROUBLESHOOTING_FACTORYRESET:
ShowFactoryResetDialog();
break;
}

if (iItemID >= ID_SHOW_DESTDIR + 1000 && iItemID < ID_SHOW_DESTDIR + 2000)
Expand Down Expand Up @@ -1011,3 +1015,140 @@ void WinConsole::SetupScripts()
}
}
}

void WinConsole::ShowFactoryResetDialog()
{
HWND hTrayWindow = FindWindow("NZBGet tray window", NULL);
m_bRunning = true;

int iResult = DialogBox(m_hInstance, MAKEINTRESOURCE(IDD_FACTORYRESETDIALOG), m_hTrayWindow, FactoryResetDialogProcStat);

switch (iResult)
{
case IDC_FACTORYRESET_RESET:
ResetFactoryDefaults();
break;
}
}

BOOL CALLBACK WinConsole::FactoryResetDialogProcStat(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
return g_pWinConsole->FactoryResetDialogProc(hwndDlg, uMsg, wParam, lParam);
}

BOOL WinConsole::FactoryResetDialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
case WM_INITDIALOG:
SendDlgItemMessage(hwndDlg, IDC_FACTORYRESET_ICON, STM_SETICON, (WPARAM)m_hRunningIcon, 0);
SendDlgItemMessage(hwndDlg, IDC_FACTORYRESET_TITLE, WM_SETFONT, (WPARAM)m_hTitleFont, 0);
return FALSE;

case WM_CLOSE:
EndDialog(hwndDlg, 0);
return TRUE;

case WM_COMMAND:
EndDialog(hwndDlg, LOWORD(wParam));
return TRUE;

default:
return FALSE;
}
}

void WinConsole::ResetFactoryDefaults()
{
char szPath[MAX_PATH + 100];
char szMessage[1024];

g_pOptions->SetPauseDownload(true);
g_pOptions->SetPausePostProcess(true);

char szCommonAppDataPath[MAX_PATH];
SHGetFolderPath(NULL, CSIDL_COMMON_APPDATA, NULL, 0, szCommonAppDataPath);

// delete default directories
const char* DefDirs[] = {"nzb", "tmp", "queue", "scripts"};
for (int i=0; i < 4; i++)
{
snprintf(szPath, sizeof(szPath), "%s\\NZBGet\\%s", szCommonAppDataPath, DefDirs[i]);
szPath[sizeof(szPath)-1] = '\0';

// try to delete the directory
char szErrBuf[200];
int iRetry = 10;
while (iRetry > 0 && Util::DirectoryExists(szPath) &&
!Util::DeleteDirectoryWithContent(szPath, szErrBuf, sizeof(szErrBuf)))
{
usleep(200 * 1000);
iRetry--;
}

if (Util::DirectoryExists(szPath))
{
snprintf(szMessage, 1024, "Could not delete directory %s.\nPlease delete the directory manually and try again.", szPath);
szMessage[1024-1] = '\0';
MessageBox(m_hTrayWindow, szMessage, "NZBGet", MB_ICONERROR);
return;
}
}

// delete old config file in the program's directory
GetModuleFileName(NULL, szPath, MAX_PATH + 1);
szPath[MAX_PATH] = '\0';
Util::NormalizePathSeparators(szPath);
char* end = strrchr(szPath, PATH_SEPARATOR);
if (end) end[1] = '\0';
strcat(szPath, "nzbget.conf");

remove(szPath);

if (Util::FileExists(szPath))
{
snprintf(szMessage, 1024, "Could not delete file %s.\nPlease delete the file manually and try again.", szPath);
szMessage[1024-1] = '\0';
MessageBox(m_hTrayWindow, szMessage, "NZBGet", MB_ICONERROR);
return;
}

// delete config file in default directory
snprintf(szPath, sizeof(szPath), "%s\\NZBGet\\nzbget.conf", szCommonAppDataPath);
szPath[sizeof(szPath)-1] = '\0';

remove(szPath);

if (Util::FileExists(szPath))
{
snprintf(szMessage, 1024, "Could not delete file %s.\nPlease delete the file manually and try again.", szPath);
szMessage[1024-1] = '\0';
MessageBox(m_hTrayWindow, szMessage, "NZBGet", MB_ICONERROR);
return;
}

// delete log files in default directory
snprintf(szPath, sizeof(szPath), "%s\\NZBGet", szCommonAppDataPath);
szPath[sizeof(szPath)-1] = '\0';

DirBrowser dir(szPath);
while (const char* szFilename = dir.Next())
{
if (Util::MatchFileExt(szFilename, ".log", ","))
{
char szFullFilename[1024];
snprintf(szFullFilename, 1024, "%s%c%s", szPath, PATH_SEPARATOR, szFilename);
szFullFilename[1024-1] = '\0';

remove(szFullFilename);

// ignore errors
}
}

MessageBox(m_hTrayWindow, "The program has been reset to factory defaults.",
"NZBGet", MB_ICONINFORMATION);

bMayStartBrowser = true;
Reload();
}
6 changes: 5 additions & 1 deletion daemon/windows/WinConsole.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* This file is part of nzbget
*
* Copyright (C) 2014 Andrey Prygunkov <[email protected]>
* Copyright (C) 2014-2015 Andrey Prygunkov <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -76,6 +76,8 @@ class WinConsole : public Thread
void ShowCategoryDir(int iCatIndex);
void SetupConfigFile();
void SetupScripts();
void ShowFactoryResetDialog();
void ResetFactoryDefaults();

static BOOL WINAPI ConsoleCtrlHandler(DWORD dwCtrlType);
static LRESULT CALLBACK TrayWndProcStat(HWND hwndWin, UINT uMsg, WPARAM wParam, LPARAM lParam);
Expand All @@ -86,6 +88,8 @@ class WinConsole : public Thread
BOOL PrefsDialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
static BOOL CALLBACK RunningDialogProcStat(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
BOOL RunningDialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
static BOOL CALLBACK FactoryResetDialogProcStat(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
BOOL FactoryResetDialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);

protected:
virtual void Run();
Expand Down
21 changes: 17 additions & 4 deletions windows/resources/nzbget.rc
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ BEGIN
POPUP "Troubleshooting"
BEGIN
MENUITEM "Restart", ID_TROUBLESHOOTING_RESTART
MENUITEM SEPARATOR
MENUITEM "Open Config-File in a Text Editor", ID_TROUBLESHOOTING_OPENCONFIG
MENUITEM SEPARATOR
MENUITEM "Reset to Factory Defaults", ID_TROUBLESHOOTING_FACTORYRESET
END
MENUITEM SEPARATOR
MENUITEM "About...", ID_ABOUT
Expand Down Expand Up @@ -114,6 +115,18 @@ BEGIN
LTEXT "NZBGet can also run as Windows service. See README-WINDOWS.txt for details.",IDC_STATIC,21,68,172,17
END

IDD_FACTORYRESETDIALOG DIALOGEX 0, 0, 297, 76
STYLE DS_SETFONT | DS_MODALFRAME | DS_CENTER | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "NZBGet"
FONT 8, "Tahoma", 400, 0, 0x0
BEGIN
PUSHBUTTON "Erase and Reset",IDC_FACTORYRESET_RESET,131,55,90,14
LTEXT "Reset to factory defaults?",IDC_FACTORYRESET_TITLE,54,7,236,13
ICON "",IDC_FACTORYRESET_ICON,7,7,20,20
LTEXT "All settings will be reset to defaults. The download queue, history, statistics, log-file, default incoming nzb-directory and default scripts-directory will be erased.",IDC_FACTORYRESET_TEXT,54,21,236,27
DEFPUSHBUTTON "Cancel",IDC_FACTORYRESET_CANCEL,227,55,63,14
END

IDD_RUNNINGDIALOG DIALOGEX 0, 0, 342, 69
STYLE DS_SETFONT | DS_MODALFRAME | DS_CENTER | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "NZBGet"
Expand Down Expand Up @@ -154,13 +167,13 @@ BEGIN
BOTTOMMARGIN, 130
END

IDD_RUNNINGDIALOG, DIALOG
IDD_FACTORYRESETDIALOG, DIALOG
BEGIN
LEFTMARGIN, 7
RIGHTMARGIN, 335
RIGHTMARGIN, 290
VERTGUIDE, 54
TOPMARGIN, 7
BOTTOMMARGIN, 62
BOTTOMMARGIN, 69
END
END
#endif // APSTUDIO_INVOKED
Expand Down
17 changes: 9 additions & 8 deletions windows/resources/resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#define IDI_TRAYICON_WORKING 104
#define IDD_PREFDIALOG 105
#define IDD_RUNNINGDIALOG 106
#define IDD_FACTORYRESETDIALOG 107
#define IDC_ABOUT_ICON 1001
#define IDC_ABOUT_VERSION 1002
#define IDC_ABOUT_HOMEPAGE 1003
Expand All @@ -21,13 +22,17 @@
#define IDC_PREF_WEBUI 1008
#define IDC_RUNNING_ICON 1009
#define IDC_RUNNING_TITLE 1010
#define IDC_RUNNING_TITLE2 1011
#define IDC_RUNNING_TEXT 1011
#define IDC_QUIT 1013
#define IDC_RUNNING_QUIT 1013
#define IDC_RUNNING_PREFS 1014
#define IDC_RUNNING_WEBUI 1015
#define IDC_RUNNING_OK 1016
#define IDC_FACTORYRESET_CANCEL 1016
#define IDC_FACTORYRESET_RESET 1017
#define IDC_FACTORYRESET_TEXT 1018
#define IDC_FACTORYRESET_TITLE 1019
#define IDC_FACTORYRESET_ICON 1020
#define ID_SHOWWEBUI 40004
#define ID_ABOUT 40005
#define ID_EXIT 40006
Expand All @@ -45,20 +50,16 @@
#define ID_SHOW_SCRIPTDIR 40025
#define ID_MENU_TROUBLESHOOTING 40026
#define ID_TROUBLESHOOTING_RESTART 40027
#define ID_TROUBLESHOOTING_RESTARTINRECOVERYMODE 40028
#define ID_TROUBLESHOOTING_RESETTOFACTORYDEFAULTS 40029
#define ID_TROUBLESHOOTING_RESTART_RECOVERY 40030
#define ID_TROUBLESHOOTING_RESET_TO_DEFAULTS 40031
#define ID_TROUBLESHOOTING_OPENCONFIG 40032
#define ID_Menu40033 40033
#define ID_TROUBLESHOOTING_FACTORYRESET 40036

// Next default values for new objects
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE 106
#define _APS_NEXT_COMMAND_VALUE 40034
#define _APS_NEXT_CONTROL_VALUE 1016
#define _APS_NEXT_COMMAND_VALUE 40037
#define _APS_NEXT_CONTROL_VALUE 1021
#define _APS_NEXT_SYMED_VALUE 101
#endif
#endif

0 comments on commit 5a4b503

Please sign in to comment.