1 | |
---|
2 | // Copyright Eóin O'Callaghan 2006 - 2007. |
---|
3 | // Distributed under the Boost Software License, Version 1.0. |
---|
4 | // (See accompanying file LICENSE_1_0.txt or copy at |
---|
5 | // http://www.boost.org/LICENSE_1_0.txt) |
---|
6 | |
---|
7 | #define HALITE_GUID L"HALITE-{E3A8BF7D-962F-476E-886B-FECEDD2F0FC7}" |
---|
8 | #define WMU_ARE_YOU_ME_STRING L"WMU_ARE_YOU_ME_HALITE-{E3A8BF7D-962F-476E-886B-FECEDD2F0FC7}" |
---|
9 | #pragma comment(linker, "\"/manifestdependency:type='Win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"") |
---|
10 | |
---|
11 | #include "stdAfx.hpp" |
---|
12 | |
---|
13 | #include "WinAPIMutex.hpp" |
---|
14 | |
---|
15 | #include "global/ini.hpp" |
---|
16 | #include "global/logger.hpp" |
---|
17 | #include "halConfig.hpp" |
---|
18 | |
---|
19 | #include "HaliteWindowMini.hpp" |
---|
20 | #include "SplashDialog.hpp" |
---|
21 | |
---|
22 | #define HALITE_MINI |
---|
23 | #include "Halite.hpp" |
---|
24 | |
---|
25 | Halite& halite() |
---|
26 | { |
---|
27 | static Halite h; |
---|
28 | return h; |
---|
29 | } |
---|
30 | |
---|
31 | namespace fs = boost::filesystem; |
---|
32 | using fs::ifstream; |
---|
33 | using fs::ofstream; |
---|
34 | |
---|
35 | static class halite_log_file : public boost::signals::trackable |
---|
36 | { |
---|
37 | public: |
---|
38 | void operator()(shared_ptr<hal::EventDetail> event) |
---|
39 | { |
---|
40 | if (halite().logToFile()) |
---|
41 | { |
---|
42 | if (!wofs.is_open()) wofs.open(hal::app().working_directory()/L"HaliteLog.txt"); |
---|
43 | |
---|
44 | wofs << (wformat(L"%1% %2%, %3%\r\n") |
---|
45 | % event->timeStamp() % hal::Event::eventLevelToStr(event->level()) |
---|
46 | % event->msg()).str(); |
---|
47 | |
---|
48 | wofs.flush(); |
---|
49 | } |
---|
50 | } |
---|
51 | |
---|
52 | void connect() |
---|
53 | { |
---|
54 | conn_ = hal::event().attach(bind(&halite_log_file::operator(), this, _1)); |
---|
55 | assert(conn_.connected()); |
---|
56 | } |
---|
57 | |
---|
58 | void disconnect() { conn_.disconnect(); } |
---|
59 | |
---|
60 | private: |
---|
61 | fs::wofstream wofs; |
---|
62 | boost::signals::connection conn_; |
---|
63 | |
---|
64 | } halite_log_file_; |
---|
65 | |
---|
66 | static const unsigned WMU_ARE_YOU_ME = ::RegisterWindowMessage(WMU_ARE_YOU_ME_STRING); |
---|
67 | |
---|
68 | static BOOL CALLBACK hwndSearcher(HWND hWnd, LPARAM lParam) |
---|
69 | { |
---|
70 | ULONG_PTR result; |
---|
71 | LRESULT ok = ::SendMessageTimeout(hWnd, WMU_ARE_YOU_ME, |
---|
72 | 0, 0, SMTO_BLOCK | SMTO_ABORTIFHUNG, 200, &result); |
---|
73 | |
---|
74 | if (ok == 0) |
---|
75 | return false; |
---|
76 | |
---|
77 | if (result == WMU_ARE_YOU_ME) |
---|
78 | { |
---|
79 | HWND* target = (HWND*)lParam; |
---|
80 | *target = hWnd; |
---|
81 | return false; |
---|
82 | } |
---|
83 | |
---|
84 | return true; |
---|
85 | } |
---|
86 | |
---|
87 | int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) |
---|
88 | { |
---|
89 | |
---|
90 | // hal::event().post(shared_ptr<hal::EventDetail>(new hal::EventDebug(hal::Event::info, L"Hello"))); |
---|
91 | |
---|
92 | try |
---|
93 | { |
---|
94 | |
---|
95 | boost::filesystem::path::default_name_check(boost::filesystem::native); |
---|
96 | |
---|
97 | AtlInitCommonControls(ICC_COOL_CLASSES | ICC_BAR_CLASSES); |
---|
98 | HINSTANCE hInstRich = ::LoadLibrary(CRichEditCtrl::GetLibraryName()); |
---|
99 | ATLASSERT(hInstRich != NULL); |
---|
100 | |
---|
101 | int nRet; |
---|
102 | HRESULT hRes = _Module.Init(NULL, hInstance); |
---|
103 | assert (SUCCEEDED(hRes)); |
---|
104 | |
---|
105 | hal::ini().load_data(); |
---|
106 | halite_log_file_.connect(); |
---|
107 | |
---|
108 | { WinAPIMutex oneInstance(HALITE_GUID); |
---|
109 | |
---|
110 | if (!oneInstance.owner() && halite().oneInst) |
---|
111 | { |
---|
112 | WinAPIMutexLock lock(oneInstance, 5000L); |
---|
113 | |
---|
114 | HWND hOther = NULL; |
---|
115 | ::EnumWindows(static_cast<WNDENUMPROC>(hwndSearcher), (LPARAM)&hOther); |
---|
116 | |
---|
117 | if (hOther != NULL) |
---|
118 | { |
---|
119 | ::SetForegroundWindow(hOther); |
---|
120 | |
---|
121 | if (::IsIconic(hOther)) |
---|
122 | ::ShowWindow(hOther, SW_RESTORE); |
---|
123 | |
---|
124 | if (!hal::app().command_args().empty()) |
---|
125 | { |
---|
126 | COPYDATASTRUCT cmdLine; |
---|
127 | cmdLine.dwData = HALITE_SENDING_CMD; |
---|
128 | cmdLine.cbData = |
---|
129 | hal::app().command_args().front().length()*sizeof(wchar_t); |
---|
130 | cmdLine.lpData = const_cast<wchar_t*>( |
---|
131 | hal::app().command_args().front().c_str()); |
---|
132 | |
---|
133 | ::SendMessage(hOther, WM_COPYDATA, 0, (LPARAM)(LPVOID)&cmdLine); |
---|
134 | } |
---|
135 | } |
---|
136 | } |
---|
137 | else |
---|
138 | { |
---|
139 | // INI().LoadData(); |
---|
140 | hal::event().post(shared_ptr<hal::EventDetail>( |
---|
141 | new hal::EventMsg(wformat(L"Exe Path: %1%.") % hal::app().exe_path()))); |
---|
142 | |
---|
143 | hal::event().post(shared_ptr<hal::EventDetail>( |
---|
144 | new hal::EventMsg(wformat(L"Initial Path: %1%.") % hal::app().initial_path()))); |
---|
145 | |
---|
146 | hal::event().post(shared_ptr<hal::EventDetail>( |
---|
147 | new hal::EventMsg((wformat(L"Working Directory: %1%.") % hal::app().working_directory()), hal::Event::info))); |
---|
148 | |
---|
149 | CMessageLoop theLoop; |
---|
150 | _Module.AddMessageLoop(&theLoop); |
---|
151 | |
---|
152 | if (halite().dll() != L"") hal::app().res_set_dll(halite().dll()); |
---|
153 | |
---|
154 | HaliteWindow wndMain(WMU_ARE_YOU_ME); |
---|
155 | if (wndMain.CreateEx() == NULL) |
---|
156 | return 1; |
---|
157 | |
---|
158 | oneInstance.release(); |
---|
159 | |
---|
160 | if (!hal::app().command_args().empty()) |
---|
161 | wndMain.ProcessFile(hal::app().command_args().front().c_str()); |
---|
162 | |
---|
163 | wndMain.SetIcon(LoadIcon(hInstance, MAKEINTRESOURCE(IDR_APP_ICON)), false); |
---|
164 | wndMain.ShowWindow(nCmdShow); |
---|
165 | |
---|
166 | nRet = theLoop.Run(); |
---|
167 | |
---|
168 | _Module.RemoveMessageLoop(); |
---|
169 | |
---|
170 | hal::bittorrent().stopEventReceiver(); |
---|
171 | |
---|
172 | if (halite().showMessage) |
---|
173 | { |
---|
174 | SplashDialog splDlg; |
---|
175 | splDlg.DoModal(); |
---|
176 | } |
---|
177 | else |
---|
178 | { |
---|
179 | hal::bittorrent().closeAll(); |
---|
180 | hal::bittorrent().shutDownSession(); |
---|
181 | } |
---|
182 | |
---|
183 | // INI().SaveData(); |
---|
184 | |
---|
185 | halite().save(); |
---|
186 | hal::ini().save_data(); |
---|
187 | } |
---|
188 | } |
---|
189 | |
---|
190 | ::FreeLibrary(hInstRich); |
---|
191 | _Module.Term(); |
---|
192 | |
---|
193 | return nRet; |
---|
194 | |
---|
195 | } |
---|
196 | catch (const std::exception& e) |
---|
197 | { |
---|
198 | MessageBoxA(0, e.what(), "Exception Thrown!", 0); |
---|
199 | |
---|
200 | return -1; |
---|
201 | } |
---|
202 | |
---|
203 | } |
---|