| 1 | /************************************************************************** |
|---|
| 2 | * Copyright (C) 2004-2007 by Michael Medin <michael@medin.name> * |
|---|
| 3 | * * |
|---|
| 4 | * This code is part of NSClient++ - http://trac.nakednuns.org/nscp * |
|---|
| 5 | * * |
|---|
| 6 | * This program is free software; you can redistribute it and/or modify * |
|---|
| 7 | * it under the terms of the GNU General Public License as published by * |
|---|
| 8 | * the Free Software Foundation; either version 2 of the License, or * |
|---|
| 9 | * (at your option) any later version. * |
|---|
| 10 | * * |
|---|
| 11 | * This program is distributed in the hope that it will be useful, * |
|---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
|---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
|---|
| 14 | * GNU General Public License for more details. * |
|---|
| 15 | * * |
|---|
| 16 | * You should have received a copy of the GNU General Public License * |
|---|
| 17 | * along with this program; if not, write to the * |
|---|
| 18 | * Free Software Foundation, Inc., * |
|---|
| 19 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
|---|
| 20 | ***************************************************************************/ |
|---|
| 21 | #include "stdafx.h" |
|---|
| 22 | #include "SysTray.h" |
|---|
| 23 | #include "TrayIcon.h" |
|---|
| 24 | #include <ServiceCmd.h> |
|---|
| 25 | #include <config.h> |
|---|
| 26 | #include <Winwlx.h> |
|---|
| 27 | #include <sysinfo.h> |
|---|
| 28 | |
|---|
| 29 | SysTray gSysTray; |
|---|
| 30 | |
|---|
| 31 | BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) |
|---|
| 32 | { |
|---|
| 33 | NSCModuleWrapper::wrapDllMain(hModule, ul_reason_for_call); |
|---|
| 34 | return TRUE; |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | SysTray::SysTray() : icon(_T("SysTray")) {} |
|---|
| 38 | SysTray::~SysTray() {} |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | void SysTray::show() { |
|---|
| 42 | icon.createThread(); |
|---|
| 43 | } |
|---|
| 44 | bool SysTray::loadModule() { |
|---|
| 45 | if (systemInfo::isBelowXP(systemInfo::getOSVersion())) { |
|---|
| 46 | try { |
|---|
| 47 | if ((serviceControll::GetServiceType(SZSERVICENAME)&SERVICE_INTERACTIVE_PROCESS)!=SERVICE_INTERACTIVE_PROCESS) { |
|---|
| 48 | NSC_LOG_ERROR(_T("SysTray is not installed (or it cannot interact with the desktop) SysTray won't be loaded. Run ") SZAPPNAME _T(" SysTray install to change this.")); |
|---|
| 49 | return true; |
|---|
| 50 | } |
|---|
| 51 | } catch (serviceControll::SCException e) { |
|---|
| 52 | NSC_LOG_ERROR(_T("SysTray is not installed (or it cannot interact with the desktop) SysTray won't be loaded. Run ") SZAPPNAME _T(" SysTray install to change this.")); |
|---|
| 53 | return true; |
|---|
| 54 | } |
|---|
| 55 | show(); |
|---|
| 56 | } else { |
|---|
| 57 | NSC_LOG_ERROR(_T("SysTray module is not used on windows XP and above (so you can remove it).")); |
|---|
| 58 | } |
|---|
| 59 | return true; |
|---|
| 60 | } |
|---|
| 61 | void SysTray::hide() { |
|---|
| 62 | if (!icon.exitThread(20000)) { |
|---|
| 63 | std::wcout << _T("MAJOR ERROR: Could not unload thread...") << std::endl; |
|---|
| 64 | NSC_LOG_ERROR(_T("Could not exit the thread, memory leak and potential corruption may be the result...")); |
|---|
| 65 | } |
|---|
| 66 | } |
|---|
| 67 | bool SysTray::unloadModule() { |
|---|
| 68 | if (systemInfo::isBelowXP(systemInfo::getOSVersion())) { |
|---|
| 69 | hide(); |
|---|
| 70 | } |
|---|
| 71 | return true; |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | int SysTray::commandLineExec(const TCHAR* command,const unsigned int argLen,TCHAR** args) { |
|---|
| 75 | if (_wcsicmp(command, _T("install")) == 0) { |
|---|
| 76 | try { |
|---|
| 77 | serviceControll::ModifyServiceType(SZSERVICENAME, SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS); |
|---|
| 78 | std::wcout << _T(MODULE_NAME) << _T(" is now able to run as the SERVICE_INTERACTIVE_PROCESS flag has been set.") << std::endl; |
|---|
| 79 | } catch (const serviceControll::SCException& e) { |
|---|
| 80 | std::wcerr << _T("Could not modify service: ") << e.error_ << std::endl; |
|---|
| 81 | return -1; |
|---|
| 82 | } catch (...) { |
|---|
| 83 | std::wcerr << _T("Could not modify service: Unknown error!") << std::endl; |
|---|
| 84 | return -1; |
|---|
| 85 | } |
|---|
| 86 | } else if (_wcsicmp(command, _T("uninstall")) == 0) { |
|---|
| 87 | try { |
|---|
| 88 | serviceControll::ModifyServiceType(SZSERVICENAME, SERVICE_WIN32_OWN_PROCESS); |
|---|
| 89 | std::wcout << _T(" is now not able to run as the SERVICE_INTERACTIVE_PROCESS flag has been reset.") << std::endl; |
|---|
| 90 | } catch (const serviceControll::SCException& e) { |
|---|
| 91 | std::wcerr << _T("Could not modify service: ") << e.error_ << std::endl; |
|---|
| 92 | return -1; |
|---|
| 93 | } catch (...) { |
|---|
| 94 | std::wcerr << _T("Could not modify service: Unknown error!") << std::endl; |
|---|
| 95 | return -1; |
|---|
| 96 | } |
|---|
| 97 | } else { |
|---|
| 98 | std::wcerr << _T("Undefined command, usage: install or uninstall") << std::endl; |
|---|
| 99 | return -1; |
|---|
| 100 | } |
|---|
| 101 | return 0; |
|---|
| 102 | } |
|---|
| 103 | |
|---|
| 104 | bool SysTray::hasCommandHandler() { |
|---|
| 105 | return false; |
|---|
| 106 | } |
|---|
| 107 | bool SysTray::hasMessageHandler() { |
|---|
| 108 | return true; |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | void SysTray::setLogWindow(HWND hWnd) { |
|---|
| 112 | MutexLock lock(logLock); |
|---|
| 113 | if (lock.hasMutex()) { |
|---|
| 114 | hLogWnd = hWnd; |
|---|
| 115 | } |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | void SysTray::handleMessage(int msgType, TCHAR* file, int line, TCHAR* message) { |
|---|
| 119 | log_entry record(msgType, file, line, message); |
|---|
| 120 | HWND hWnd = NULL; |
|---|
| 121 | { |
|---|
| 122 | MutexLock lock(logLock); |
|---|
| 123 | if (lock.hasMutex()) { |
|---|
| 124 | log.push_back(record); |
|---|
| 125 | if (log.size() > 50) |
|---|
| 126 | log.pop_front(); |
|---|
| 127 | hWnd = hLogWnd; |
|---|
| 128 | } else { |
|---|
| 129 | NSC_LOG_ERROR_STD(_T("Failed to get mutex in logger, message discarded")); |
|---|
| 130 | } |
|---|
| 131 | } |
|---|
| 132 | if (hWnd) { |
|---|
| 133 | SendMessage(hWnd, WM_USER+1, reinterpret_cast<WPARAM>(&record), NULL); |
|---|
| 134 | } |
|---|
| 135 | } |
|---|
| 136 | SysTray::log_type SysTray::getLog() { |
|---|
| 137 | log_type ret; |
|---|
| 138 | for (log_type::const_iterator cit = log.begin(); cit != log.end(); ++cit) |
|---|
| 139 | ret.push_back(*cit); |
|---|
| 140 | return ret; |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | extern void ShowIcon() { |
|---|
| 144 | gSysTray.show(); |
|---|
| 145 | } |
|---|
| 146 | extern void HideIcon() { |
|---|
| 147 | gSysTray.hide(); |
|---|
| 148 | } |
|---|
| 149 | NSC_WRAPPERS_MAIN_DEF(gSysTray); |
|---|
| 150 | NSC_WRAPPERS_HANDLE_MSG_DEF(gSysTray); |
|---|
| 151 | NSC_WRAPPERS_IGNORE_CMD_DEF(); |
|---|
| 152 | NSC_WRAPPERS_CLI_DEF(gSysTray); |
|---|
| 153 | |
|---|