Index: src/ch/MainWnd.cpp =================================================================== diff -u -rc17c4aa7bb4c96219af4191623abe40d18e02713 -r8068e0c351055554340ac9755d1bc846893bf2b8 --- src/ch/MainWnd.cpp (.../MainWnd.cpp) (revision c17c4aa7bb4c96219af4191623abe40d18e02713) +++ src/ch/MainWnd.cpp (.../MainWnd.cpp) (revision 8068e0c351055554340ac9755d1bc846893bf2b8) @@ -44,6 +44,11 @@ #include "TRecentPathsTools.h" #include "DirectoryChooser.h" #include "FeedbackHandlerFactory.h" +#include "../libchcore/TTask.h" +#include "TTaskManagerWrapper.h" +#include "shortcuts.h" +#include "CfgProperties.h" +#include "resource.h" #ifdef _DEBUG #define new DEBUG_NEW @@ -75,10 +80,6 @@ ///////////////////////////////////////////////////////////////////////////// // CMainWnd construction/destruction CMainWnd::CMainWnd() : - m_pdlgStatus(NULL), - m_pdlgMiniView(NULL), - m_dwLastTime(0), - m_spTasks(), m_spTaskMgrStats(new chcore::TTaskManagerStatsSnapshot) { } @@ -97,10 +98,10 @@ wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = AfxGetInstanceHandle(); - wc.hIcon = ::LoadIcon(NULL, MAKEINTRESOURCE(AFX_IDI_STD_FRAME)); - wc.hCursor = ::LoadCursor(NULL, IDC_ARROW); + wc.hIcon = ::LoadIcon(nullptr, MAKEINTRESOURCE(AFX_IDI_STD_FRAME)); + wc.hCursor = ::LoadCursor(nullptr, IDC_ARROW); wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); - wc.lpszMenuName = NULL; + wc.lpszMenuName = nullptr; wc.lpszClassName = CH_WNDCLASS_NAME; return ::AfxRegisterClass(&wc); @@ -113,7 +114,7 @@ if(!bReg) return FALSE; - return CreateEx(WS_EX_TOOLWINDOW, CH_WNDCLASS_NAME, _T("Copy Handler"), WS_OVERLAPPED, 10, 10, 10, 10, NULL, (HMENU)NULL, NULL); + return CreateEx(WS_EX_TOOLWINDOW, CH_WNDCLASS_NAME, _T("Copy Handler"), WS_OVERLAPPED, 10, 10, 10, 10, nullptr, (HMENU)nullptr, nullptr); } int CMainWnd::ShowTrayIcon() @@ -200,7 +201,7 @@ // start clipboard monitoring LOG_INFO(_T("Starting clipboard monitor...")); - CClipboardMonitor::StartMonitor(m_spTasks.get()); + CClipboardMonitor::StartMonitor(m_spTasks); CheckForUpdates(); @@ -291,7 +292,7 @@ if (!mMenu.Attach(hMenu)) return (LRESULT)FALSE; - if ((pSubMenu = mMenu.GetSubMenu(0)) == NULL) + if ((pSubMenu = mMenu.GetSubMenu(0)) == nullptr) return (LRESULT)FALSE; // double click received, the default action is to execute first menu item @@ -310,7 +311,7 @@ if (!mMenu.Attach(hMenu)) return (LRESULT)FALSE; - if ((pSubMenu = mMenu.GetSubMenu(0)) == NULL) + if ((pSubMenu = mMenu.GetSubMenu(0)) == nullptr) return (LRESULT)FALSE; // set menu default item @@ -370,7 +371,7 @@ m_pdlgStatus->Create(); // hide miniview if showing status - if (m_pdlgMiniView != NULL && m_pdlgMiniView->m_bLock) + if (m_pdlgMiniView != nullptr && m_pdlgMiniView->m_bLock) { if (::IsWindow(m_pdlgMiniView->m_hWnd)) m_pdlgMiniView->HideWindow(); @@ -384,7 +385,35 @@ void CMainWnd::OnClose() { - PrepareToExit(); + CString strMessage; + try + { + PrepareToExit(); + } + catch(const chcore::TBaseException& e) + { + const size_t stMaxError = 1024; + wchar_t szError[ stMaxError ]; + e.GetErrorInfo(szError, stMaxError); + + strMessage = szError; + } + catch(const std::exception& e) + { + strMessage = e.what(); + } + + if(!strMessage.IsEmpty()) + { + LOG_ERROR(L"Failed to finalize tasks before exiting Copy Handler. Error: " + strMessage); + + ictranslate::CFormat fmt; + + fmt.SetFormat(GetResManager().LoadString(IDS_FINALIZE_CH_ERROR)); + fmt.SetParam(_T("%reason"), strMessage); + AfxMessageBox(fmt, MB_OK | MB_ICONERROR); + } + CWnd::OnClose(); } @@ -393,12 +422,27 @@ switch (nIDEvent) { case 1023: - // autosave timer - KillTimer(1023); - m_spTasks->Store(false); - SetTimer(1023, GetPropValue(GetConfig()), NULL); - break; + { + // autosave timer + KillTimer(1023); + try + { + m_spTasks->Store(false); + } + catch(const std::exception& e) + { + CString strError = e.what(); + ictranslate::CFormat fmt; + fmt.SetFormat(_T("Failed to autosave task. Error: %err.")); + fmt.SetParam(_T("%err"), (PCTSTR)strError); + + LOG_ERROR(fmt); + } + + SetTimer(1023, GetPropValue(GetConfig()), nullptr); + break; + } case 3245: // auto-delete finished tasks timer KillTimer(3245); @@ -410,7 +454,7 @@ m_pdlgStatus->SendMessage(WM_UPDATESTATUS); } - SetTimer(3245, TM_AUTOREMOVE, NULL); + SetTimer(3245, TM_AUTOREMOVE, nullptr); break; case 8743: @@ -515,16 +559,9 @@ break; } - // load resource strings - chcore::SetTaskPropValue(tTaskDefinition.GetConfiguration(), GetResManager().LoadString(IDS_FIRSTCOPY_STRING)); - chcore::SetTaskPropValue(tTaskDefinition.GetConfiguration(), GetResManager().LoadString(IDS_NEXTCOPY_STRING)); + TTaskManagerWrapper tTaskManager(m_spTasks); + tTaskManager.CreateTask(tTaskDefinition); - // create task with the above definition - chcore::TTaskPtr spTask = m_spTasks->CreateTask(tTaskDefinition); - - // add to task list and start processing - spTask->BeginProcessing(); - break; } case eCDType_CommandLineArguments: @@ -625,15 +662,8 @@ chcore::TTaskDefinition tTaskDefinition = dlg.m_tTaskDefinition; - // load resource strings - chcore::SetTaskPropValue(tTaskDefinition.GetConfiguration(), GetResManager().LoadString(IDS_FIRSTCOPY_STRING)); - chcore::SetTaskPropValue(tTaskDefinition.GetConfiguration(), GetResManager().LoadString(IDS_NEXTCOPY_STRING)); - - // new task - chcore::TTaskPtr spTask = m_spTasks->CreateTask(tTaskDefinition); - - // start - spTask->BeginProcessing(); + TTaskManagerWrapper tTaskManager(m_spTasks); + tTaskManager.CreateTask(tTaskDefinition); } } @@ -651,7 +681,7 @@ { OnShowMiniView(); return static_cast(0); - break; + } case WM_CONFIGNOTIFY: @@ -691,7 +721,7 @@ bool bAddedAnyOption = false; if(GetPropValue(rConfig)) { - spRootItem->AddChild(boost::make_shared(rResManager.LoadString(IDS_MENUCOPY_STRING), rResManager.LoadString(IDS_MENUTIPCOPY_STRING), + spRootItem->AddChild(std::make_shared(rResManager.LoadString(IDS_MENUCOPY_STRING), rResManager.LoadString(IDS_MENUTIPCOPY_STRING), TOperationTypeInfo(TOperationTypeInfo::eOpType_Specified, chcore::eOperation_Copy), TSourcePathsInfo(TSourcePathsInfo::eSrcType_InitializeIDataObject), TDestinationPathInfo(TDestinationPathInfo::eDstType_InitializePidlFolder, chcore::TSmartPath()), false, chcore::eOperation_Copy)); @@ -700,7 +730,7 @@ if(GetPropValue(rConfig)) { - spRootItem->AddChild(boost::make_shared(rResManager.LoadString(IDS_MENUMOVE_STRING), rResManager.LoadString(IDS_MENUTIPMOVE_STRING), + spRootItem->AddChild(std::make_shared(rResManager.LoadString(IDS_MENUMOVE_STRING), rResManager.LoadString(IDS_MENUTIPMOVE_STRING), TOperationTypeInfo(TOperationTypeInfo::eOpType_Specified, chcore::eOperation_Move), TSourcePathsInfo(TSourcePathsInfo::eSrcType_InitializeIDataObject), TDestinationPathInfo(TDestinationPathInfo::eDstType_InitializePidlFolder, chcore::TSmartPath()), false, chcore::eOperation_Move)); @@ -709,7 +739,7 @@ if(GetPropValue(rConfig)) { - spRootItem->AddChild(boost::make_shared(rResManager.LoadString(IDS_MENUCOPYMOVESPECIAL_STRING), rResManager.LoadString(IDS_MENUTIPCOPYMOVESPECIAL_STRING), + spRootItem->AddChild(std::make_shared(rResManager.LoadString(IDS_MENUCOPYMOVESPECIAL_STRING), rResManager.LoadString(IDS_MENUTIPCOPYMOVESPECIAL_STRING), TOperationTypeInfo(TOperationTypeInfo::eOpType_Autodetect, chcore::eOperation_Copy), TSourcePathsInfo(TSourcePathsInfo::eSrcType_InitializeIDataObject), TDestinationPathInfo(TDestinationPathInfo::eDstType_InitializePidlFolder, chcore::TSmartPath()), true)); @@ -719,7 +749,7 @@ if(bAddedAnyOption) { // insert separator as an addition to other items - spRootItem->AddChild(boost::make_shared()); + spRootItem->AddChild(std::make_shared()); } break; } @@ -728,15 +758,15 @@ { if(GetPropValue(rConfig)) { - spRootItem->AddChild(boost::make_shared(rResManager.LoadString(IDS_MENUPASTE_STRING), rResManager.LoadString(IDS_MENUTIPPASTE_STRING), + spRootItem->AddChild(std::make_shared(rResManager.LoadString(IDS_MENUPASTE_STRING), rResManager.LoadString(IDS_MENUTIPPASTE_STRING), TOperationTypeInfo(TOperationTypeInfo::eOpType_Autodetect, chcore::eOperation_Copy), TSourcePathsInfo(TSourcePathsInfo::eSrcType_Clipboard), TDestinationPathInfo(TDestinationPathInfo::eDstType_InitializeAuto, chcore::TSmartPath()), false)); } if(GetPropValue(rConfig)) { - spRootItem->AddChild(boost::make_shared(rResManager.LoadString(IDS_MENUPASTESPECIAL_STRING), rResManager.LoadString(IDS_MENUTIPPASTESPECIAL_STRING), + spRootItem->AddChild(std::make_shared(rResManager.LoadString(IDS_MENUPASTESPECIAL_STRING), rResManager.LoadString(IDS_MENUTIPPASTESPECIAL_STRING), TOperationTypeInfo(TOperationTypeInfo::eOpType_Autodetect, chcore::eOperation_Copy), TSourcePathsInfo(TSourcePathsInfo::eSrcType_Clipboard), TDestinationPathInfo(TDestinationPathInfo::eDstType_InitializeAuto, chcore::TSmartPath()), true)); @@ -760,7 +790,7 @@ unsigned long long ullSize = 0; // retrieving free space might fail, but it's not critical - we just won't show the free space - if(bRetrieveFreeSpace && GetDynamicFreeSpace(tShortcut.m_strPath, &ullSize, NULL)) + if(bRetrieveFreeSpace && GetDynamicFreeSpace(tShortcut.m_strPath, &ullSize, nullptr)) { CString strNameFormat; strNameFormat.Format(_T("%s (%s)"), tShortcut.m_strName, GetSizeString(ullSize)); @@ -776,10 +806,10 @@ if(GetPropValue(rConfig)) { - boost::shared_ptr menuItem(boost::make_shared(rResManager.LoadString(IDS_MENUCOPYTO_STRING), rResManager.LoadString(IDS_MENUTIPCOPYTO_STRING))); + std::shared_ptr menuItem(std::make_shared(rResManager.LoadString(IDS_MENUCOPYTO_STRING), rResManager.LoadString(IDS_MENUTIPCOPYTO_STRING))); BOOST_FOREACH(const CShortcut& tShortcut, vShortcuts) { - menuItem->AddChild(boost::make_shared((PCTSTR)tShortcut.m_strName, (PCTSTR)tShortcut.m_strPath, + menuItem->AddChild(std::make_shared((PCTSTR)tShortcut.m_strName, (PCTSTR)tShortcut.m_strPath, TOperationTypeInfo(TOperationTypeInfo::eOpType_Specified, chcore::eOperation_Copy), TSourcePathsInfo(TSourcePathsInfo::eSrcType_InitializeAuto), TDestinationPathInfo(TDestinationPathInfo::eDstType_Specified, chcore::PathFromString((PCTSTR)tShortcut.m_strPath)), false)); @@ -789,21 +819,21 @@ // optionally separator if(!vShortcuts.empty()) - menuItem->AddChild(boost::make_shared()); + menuItem->AddChild(std::make_shared()); // "Choose" menu option - menuItem->AddChild(boost::make_shared(rResManager.LoadString(IDS_SHELLEXT_CHOOSE_DIR_STRING), rResManager.LoadString(IDS_SHELLEXT_CHOOSE_DIR_TOOLTIP_STRING), + menuItem->AddChild(std::make_shared(rResManager.LoadString(IDS_SHELLEXT_CHOOSE_DIR_STRING), rResManager.LoadString(IDS_SHELLEXT_CHOOSE_DIR_TOOLTIP_STRING), TOperationTypeInfo(TOperationTypeInfo::eOpType_Specified, chcore::eOperation_Copy), TSourcePathsInfo(TSourcePathsInfo::eSrcType_InitializeAuto), TDestinationPathInfo(TDestinationPathInfo::eDstType_Choose, chcore::TSmartPath()), false)); } if(GetPropValue(rConfig)) { - boost::shared_ptr menuItem(boost::make_shared(rResManager.LoadString(IDS_MENUMOVETO_STRING), rResManager.LoadString(IDS_MENUTIPMOVETO_STRING))); + std::shared_ptr menuItem(std::make_shared(rResManager.LoadString(IDS_MENUMOVETO_STRING), rResManager.LoadString(IDS_MENUTIPMOVETO_STRING))); BOOST_FOREACH(const CShortcut& tShortcut, vShortcuts) { - menuItem->AddChild(boost::make_shared((PCTSTR)tShortcut.m_strName, (PCTSTR)tShortcut.m_strPath, + menuItem->AddChild(std::make_shared((PCTSTR)tShortcut.m_strName, (PCTSTR)tShortcut.m_strPath, TOperationTypeInfo(TOperationTypeInfo::eOpType_Specified, chcore::eOperation_Move), TSourcePathsInfo(TSourcePathsInfo::eSrcType_InitializeAuto), TDestinationPathInfo(TDestinationPathInfo::eDstType_Specified, chcore::PathFromString((PCTSTR)tShortcut.m_strPath)), false)); @@ -813,21 +843,21 @@ // optionally separator if(!vShortcuts.empty()) - menuItem->AddChild(boost::make_shared()); + menuItem->AddChild(std::make_shared()); // "Choose" menu option - menuItem->AddChild(boost::make_shared(rResManager.LoadString(IDS_SHELLEXT_CHOOSE_DIR_STRING), rResManager.LoadString(IDS_SHELLEXT_CHOOSE_DIR_TOOLTIP_STRING), + menuItem->AddChild(std::make_shared(rResManager.LoadString(IDS_SHELLEXT_CHOOSE_DIR_STRING), rResManager.LoadString(IDS_SHELLEXT_CHOOSE_DIR_TOOLTIP_STRING), TOperationTypeInfo(TOperationTypeInfo::eOpType_Specified, chcore::eOperation_Move), TSourcePathsInfo(TSourcePathsInfo::eSrcType_InitializeAuto), TDestinationPathInfo(TDestinationPathInfo::eDstType_Choose, chcore::TSmartPath()), false)); } if(GetPropValue(rConfig)) { - boost::shared_ptr menuItem(boost::make_shared(rResManager.LoadString(IDS_MENUCOPYMOVETOSPECIAL_STRING), rResManager.LoadString(IDS_MENUTIPCOPYMOVETOSPECIAL_STRING))); + std::shared_ptr menuItem(std::make_shared(rResManager.LoadString(IDS_MENUCOPYMOVETOSPECIAL_STRING), rResManager.LoadString(IDS_MENUTIPCOPYMOVETOSPECIAL_STRING))); BOOST_FOREACH(const CShortcut& tShortcut, vShortcuts) { - menuItem->AddChild(boost::make_shared((PCTSTR)tShortcut.m_strName, (PCTSTR)tShortcut.m_strPath, + menuItem->AddChild(std::make_shared((PCTSTR)tShortcut.m_strName, (PCTSTR)tShortcut.m_strPath, TOperationTypeInfo(TOperationTypeInfo::eOpType_Specified, chcore::eOperation_Copy), TSourcePathsInfo(TSourcePathsInfo::eSrcType_InitializeAuto), TDestinationPathInfo(TDestinationPathInfo::eDstType_Specified, chcore::PathFromString((PCTSTR)tShortcut.m_strPath)), true)); @@ -837,10 +867,10 @@ // optionally separator if(!vShortcuts.empty()) - menuItem->AddChild(boost::make_shared()); + menuItem->AddChild(std::make_shared()); // "Choose" menu option - menuItem->AddChild(boost::make_shared(rResManager.LoadString(IDS_SHELLEXT_CHOOSE_DIR_STRING), rResManager.LoadString(IDS_SHELLEXT_CHOOSE_DIR_TOOLTIP_STRING), + menuItem->AddChild(std::make_shared(rResManager.LoadString(IDS_SHELLEXT_CHOOSE_DIR_STRING), rResManager.LoadString(IDS_SHELLEXT_CHOOSE_DIR_TOOLTIP_STRING), TOperationTypeInfo(TOperationTypeInfo::eOpType_Specified, chcore::eOperation_Copy), TSourcePathsInfo(TSourcePathsInfo::eSrcType_InitializeAuto), TDestinationPathInfo(TDestinationPathInfo::eDstType_Choose, chcore::TSmartPath()), true)); @@ -903,7 +933,7 @@ } case WM_STATUSCLOSING: { - if (m_pdlgMiniView != NULL && m_pdlgMiniView->m_bLock && ::IsWindow(m_pdlgMiniView->m_hWnd)) + if (m_pdlgMiniView != nullptr && m_pdlgMiniView->m_bLock && ::IsWindow(m_pdlgMiniView->m_hWnd)) m_pdlgMiniView->RefreshStatus(); break; @@ -916,7 +946,6 @@ case WM_TRAYNOTIFY: { return OnTrayNotification(wParam, lParam); - break; } } @@ -979,7 +1008,7 @@ void CMainWnd::OnPopupHelp() { - GetApp().HtmlHelp(HH_DISPLAY_TOPIC, NULL); + GetApp().HtmlHelp(HH_DISPLAY_TOPIC, 0); } void CMainWnd::OnPopupCheckForUpdates() @@ -1027,7 +1056,7 @@ } // get last check time stored in configuration - unsigned long long ullCurrentStamp = _time64(NULL); + unsigned long long ullCurrentStamp = _time64(nullptr); unsigned long long ullTimestamp = GetPropValue(GetConfig()); // perform checking for updates only when the minimal interval has passed @@ -1042,7 +1071,7 @@ chcore::TConfig& rConfig = GetConfig(); try { - SetPropValue(rConfig, _time64(NULL)); + SetPropValue(rConfig, _time64(nullptr)); rConfig.Write(); } catch(const std::exception& /*e*/) @@ -1056,8 +1085,8 @@ void CMainWnd::SetupTimers() { // start saving timer - SetTimer(1023, GetPropValue(GetConfig()), NULL); + SetTimer(1023, GetPropValue(GetConfig()), nullptr); - SetTimer(3245, TM_AUTOREMOVE, NULL); - SetTimer(8743, TM_ACCEPTING, NULL); // ends wait state in tasks + SetTimer(3245, TM_AUTOREMOVE, nullptr); + SetTimer(8743, TM_ACCEPTING, nullptr); // ends wait state in tasks }