Index: src/ch/ClipboardMonitor.cpp =================================================================== diff -u -r87efd9079c796add662b1189e84ca62472d2b757 -r25129f39720523eca2cb702242890a7862f70ec6 --- src/ch/ClipboardMonitor.cpp (.../ClipboardMonitor.cpp) (revision 87efd9079c796add662b1189e84ca62472d2b757) +++ src/ch/ClipboardMonitor.cpp (.../ClipboardMonitor.cpp) (revision 25129f39720523eca2cb702242890a7862f70ec6) @@ -29,6 +29,7 @@ #include "FolderDialog.h" #include "ShutdownDlg.h" #include "DirectoryChooser.h" +#include "TTaskManagerWrapper.h" using namespace chcore; @@ -43,19 +44,19 @@ Stop(); } -void CClipboardMonitor::StartMonitor(chcore::TTaskManager* pTasks) +void CClipboardMonitor::StartMonitor(chcore::TTaskManagerPtr spTasks) { - CClipboardMonitor::S_ClipboardMonitor.Start(pTasks); + CClipboardMonitor::S_ClipboardMonitor.Start(spTasks); } void CClipboardMonitor::StopMonitor() { return CClipboardMonitor::S_ClipboardMonitor.Stop(); } -void CClipboardMonitor::Start(chcore::TTaskManager* pTasks) +void CClipboardMonitor::Start(chcore::TTaskManagerPtr spTasks) { - m_pTasks = pTasks; + m_spTasks = spTasks; m_threadWorker.StartThread(&CClipboardMonitor::ClipboardMonitorProc, this); } @@ -131,41 +132,16 @@ // get dest path tTaskDefinition.SetDestinationPath(pathSelected); - // load resource strings - chcore::SetTaskPropValue(tTaskDefinition.GetConfiguration(), GetResManager().LoadString(IDS_FIRSTCOPY_STRING)); - chcore::SetTaskPropValue(tTaskDefinition.GetConfiguration(), GetResManager().LoadString(IDS_NEXTCOPY_STRING)); + TTaskManagerWrapper tTaskManager(pData->m_spTasks); - CString strMessage; - try - { - chcore::TTaskPtr spTask = pData->m_pTasks->CreateTask(tTaskDefinition); - - // write spTask to a file - spTask->Store(true); - - // start processing - spTask->BeginProcessing(); - } - catch(const std::exception& e) - { - strMessage = e.what(); - } - - if(!strMessage.IsEmpty()) - { - ictranslate::CFormat fmt; - - fmt.SetFormat(GetResManager().LoadString(IDS_TASK_CREATE_FAILED)); - fmt.SetParam(_T("%reason"), strMessage); - AfxMessageBox(fmt, MB_OK | MB_ICONERROR); - } + tTaskManager.CreateTask(tTaskDefinition); } } // do we need to check for turning computer off if(uiShutCounter == 0 && GetPropValue(GetConfig())) { - if(pData->m_pTasks->AreAllFinished()) + if(pData->m_spTasks->AreAllFinished()) { TRACE("Shut down windows\n"); bool bShutdown=true; Index: src/ch/ClipboardMonitor.h =================================================================== diff -u -rbeaf6c76a528f2bcf42efa705770079fd84c0156 -r25129f39720523eca2cb702242890a7862f70ec6 --- src/ch/ClipboardMonitor.h (.../ClipboardMonitor.h) (revision beaf6c76a528f2bcf42efa705770079fd84c0156) +++ src/ch/ClipboardMonitor.h (.../ClipboardMonitor.h) (revision 25129f39720523eca2cb702242890a7862f70ec6) @@ -23,6 +23,7 @@ #define __CLIPBOARDMONITOR_H__ #include "..\libchcore\TWorkerThreadController.h" +#include "..\libchcore\TTaskManager.h" namespace chcore { @@ -32,10 +33,10 @@ class CClipboardMonitor { public: - static void StartMonitor(chcore::TTaskManager* pTasks); + static void StartMonitor(chcore::TTaskManagerPtr spTasks); static void StopMonitor(); - void Start(chcore::TTaskManager* pTasks); + void Start(chcore::TTaskManagerPtr spTasks); void Stop(); protected: @@ -47,7 +48,7 @@ protected: static CClipboardMonitor S_ClipboardMonitor; - chcore::TTaskManager* m_pTasks = nullptr; + chcore::TTaskManagerPtr m_spTasks; // thread control chcore::TWorkerThreadController m_threadWorker; Index: src/ch/MainWnd.cpp =================================================================== diff -u -rbeaf6c76a528f2bcf42efa705770079fd84c0156 -r25129f39720523eca2cb702242890a7862f70ec6 --- src/ch/MainWnd.cpp (.../MainWnd.cpp) (revision beaf6c76a528f2bcf42efa705770079fd84c0156) +++ src/ch/MainWnd.cpp (.../MainWnd.cpp) (revision 25129f39720523eca2cb702242890a7862f70ec6) @@ -45,6 +45,7 @@ #include "DirectoryChooser.h" #include "FeedbackHandlerFactory.h" #include "../libchcore/TTask.h" +#include "TTaskManagerWrapper.h" #ifdef _DEBUG #define new DEBUG_NEW @@ -197,7 +198,7 @@ // start clipboard monitoring LOG_INFO(_T("Starting clipboard monitor...")); - CClipboardMonitor::StartMonitor(m_spTasks.get()); + CClipboardMonitor::StartMonitor(m_spTasks); CheckForUpdates(); @@ -381,7 +382,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(); } @@ -390,12 +419,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()), NULL); + break; + } case 3245: // auto-delete finished tasks timer KillTimer(3245); @@ -512,16 +556,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: @@ -622,15 +659,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); } } Index: src/ch/TTaskManagerWrapper.cpp =================================================================== diff -u --- src/ch/TTaskManagerWrapper.cpp (revision 0) +++ src/ch/TTaskManagerWrapper.cpp (revision 25129f39720523eca2cb702242890a7862f70ec6) @@ -0,0 +1,73 @@ +// ============================================================================ +// Copyright (C) 2001-2016 by Jozef Starosczyk +// ixen@copyhandler.com +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU Library General Public License +// (version 2) as published by the Free Software Foundation; +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU Library General Public +// License along with this program; if not, write to the +// Free Software Foundation, Inc., +// 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// ============================================================================ +#include "stdafx.h" +#include "TTaskManagerWrapper.h" +#include "resource.h" +#include "..\libictranslate\ResourceManager.h" +#include "..\libchcore\TTaskConfiguration.h" +#include "..\libchcore\TTaskDefinition.h" +#include "..\libchcore\TTask.h" +#include "..\libchcore\TBaseException.h" + +TTaskManagerWrapper::TTaskManagerWrapper(const chcore::TTaskManagerPtr& spTaskManager) : + m_spTaskManager(spTaskManager) +{ +} + +chcore::TTaskPtr TTaskManagerWrapper::CreateTask(chcore::TTaskDefinition& rTaskDefinition) +{ + ictranslate::CResourceManager& rResourceManager = ictranslate::CResourceManager::Acquire(); + + // load resource strings + chcore::SetTaskPropValue(rTaskDefinition.GetConfiguration(), rResourceManager.LoadString(IDS_FIRSTCOPY_STRING)); + chcore::SetTaskPropValue(rTaskDefinition.GetConfiguration(), rResourceManager.LoadString(IDS_NEXTCOPY_STRING)); + + CString strMessage; + try + { + // create task with the above definition + chcore::TTaskPtr spTask = m_spTaskManager->CreateTask(rTaskDefinition); + + // add to task list and start processing + spTask->BeginProcessing(); + + return spTask; + } + 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(); + } + + + ictranslate::CFormat fmt; + + fmt.SetFormat(rResourceManager.LoadString(IDS_TASK_CREATE_FAILED)); + fmt.SetParam(_T("%reason"), strMessage); + AfxMessageBox(fmt, MB_OK | MB_ICONERROR); + + return nullptr; +} Index: src/ch/TTaskManagerWrapper.h =================================================================== diff -u --- src/ch/TTaskManagerWrapper.h (revision 0) +++ src/ch/TTaskManagerWrapper.h (revision 25129f39720523eca2cb702242890a7862f70ec6) @@ -0,0 +1,35 @@ +// ============================================================================ +// Copyright (C) 2001-2016 by Jozef Starosczyk +// ixen@copyhandler.com +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU Library General Public License +// (version 2) as published by the Free Software Foundation; +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU Library General Public +// License along with this program; if not, write to the +// Free Software Foundation, Inc., +// 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +// ============================================================================ +#ifndef __TTASKMANAGERWRAPPER_H__ +#define __TTASKMANAGERWRAPPER_H__ + +#include "..\libchcore\TTaskManager.h" + +class TTaskManagerWrapper +{ +public: + TTaskManagerWrapper(const chcore::TTaskManagerPtr& spTaskManager); + + chcore::TTaskPtr CreateTask(chcore::TTaskDefinition& rTaskDefinition); + +private: + chcore::TTaskManagerPtr m_spTaskManager; +}; + +#endif Index: src/ch/ch.rc =================================================================== diff -u -rd037fe7234987387e26b0be460be37a956fcb27d -r25129f39720523eca2cb702242890a7862f70ec6 --- src/ch/ch.rc (.../ch.rc) (revision d037fe7234987387e26b0be460be37a956fcb27d) +++ src/ch/ch.rc (.../ch.rc) (revision 25129f39720523eca2cb702242890a7862f70ec6) @@ -1227,6 +1227,7 @@ IDS_ALWAYS_SHOW_STRING "Always show" IDS_HIDE_AND_REGISTER "Hide and register extension" IDS_HIDE_AND_DONT_REGISTER "Hide and do not register extension" + IDS_FINALIZE_CH_ERROR "Failed to finalize Copy Handler. Some information about tasks being processed might be lost. Error: %reason." END #endif // English (United States) resources Index: src/ch/ch.vc140.vcxproj =================================================================== diff -u -rffb46a396ce20cda2e04020cf01c118ae81642b5 -r25129f39720523eca2cb702242890a7862f70ec6 --- src/ch/ch.vc140.vcxproj (.../ch.vc140.vcxproj) (revision ffb46a396ce20cda2e04020cf01c118ae81642b5) +++ src/ch/ch.vc140.vcxproj (.../ch.vc140.vcxproj) (revision 25129f39720523eca2cb702242890a7862f70ec6) @@ -526,6 +526,7 @@ + @@ -775,6 +776,7 @@ + Index: src/ch/ch.vc140.vcxproj.filters =================================================================== diff -u -rffb46a396ce20cda2e04020cf01c118ae81642b5 -r25129f39720523eca2cb702242890a7862f70ec6 --- src/ch/ch.vc140.vcxproj.filters (.../ch.vc140.vcxproj.filters) (revision ffb46a396ce20cda2e04020cf01c118ae81642b5) +++ src/ch/ch.vc140.vcxproj.filters (.../ch.vc140.vcxproj.filters) (revision 25129f39720523eca2cb702242890a7862f70ec6) @@ -242,6 +242,9 @@ Source Files\Tools + + Source Files\Core + @@ -412,6 +415,9 @@ Source Files\Tools + + Source Files\Core + Index: src/ch/resource.h =================================================================== diff -u -r045540c818c374806d09742ef3d7a984d8d757d3 -r25129f39720523eca2cb702242890a7862f70ec6 --- src/ch/resource.h (.../resource.h) (revision 045540c818c374806d09742ef3d7a984d8d757d3) +++ src/ch/resource.h (.../resource.h) (revision 25129f39720523eca2cb702242890a7862f70ec6) @@ -422,6 +422,7 @@ #define IDS_UPDATER_ALREADYNEWESTVERSION 5047 #define IDS_UPDATER_NEWVERSIONEXISTS 5048 #define IDS_UPDATER_RELEASENOTES 5049 +#define IDS_FINALIZE_CH_ERROR 5050 #define IDS_ONECOPY_STRING 6000 #define IDS_REGISTEROK_STRING 6001 #define IDS_REGISTERERR_STRING 6002 Index: src/libchcore/TSQLiteStatement.cpp =================================================================== diff -u -r8d3154738b885f2e01997071e6180da0c4b8cc7c -r25129f39720523eca2cb702242890a7862f70ec6 --- src/libchcore/TSQLiteStatement.cpp (.../TSQLiteStatement.cpp) (revision 8d3154738b885f2e01997071e6180da0c4b8cc7c) +++ src/libchcore/TSQLiteStatement.cpp (.../TSQLiteStatement.cpp) (revision 25129f39720523eca2cb702242890a7862f70ec6) @@ -22,6 +22,7 @@ #include "ErrorCodes.h" #include "TSQLiteException.h" #include +#include "SerializerTrace.h" namespace chcore { @@ -39,8 +40,10 @@ TSQLiteStatement::~TSQLiteStatement() { int iResult = sqlite3_finalize(m_pStatement); - iResult; - _ASSERTE(iResult == SQLITE_OK); + if(iResult != SQLITE_OK) + { + DBTRACE1(L"sqlite3_finalize failed with error code: %d", iResult); + } } void TSQLiteStatement::Close() Index: src/libchcore/TTaskManager.cpp =================================================================== diff -u -re8f31b0f922b402878356e130c866c4f3682a7f5 -r25129f39720523eca2cb702242890a7862f70ec6 --- src/libchcore/TTaskManager.cpp (.../TTaskManager.cpp) (revision e8f31b0f922b402878356e130c866c4f3682a7f5) +++ src/libchcore/TTaskManager.cpp (.../TTaskManager.cpp) (revision 25129f39720523eca2cb702242890a7862f70ec6) @@ -62,10 +62,10 @@ spTask->SetLogPath(CreateTaskLogPath(tTaskDefinition.GetTaskName())); spTask->SetTaskDefinition(tTaskDefinition); - Add(spTask); - spTask->Store(true); + Add(spTask); + return spTask; }