source: nscp/modules/SysTray/SysTray.cpp @ c0522cd

0.4.00.4.10.4.2stable
Last change on this file since c0522cd was c0522cd, checked in by Michael Medin <michael@…>, 5 years ago

2008-08-16 MickeM

  • *WARNING* THIS IS VERY VERY UNSTABEL (possibly)
  • *WARNING* A lot of new untested code here so dont run in production enviornments :) + Added shared session so system tray can communicate with master + Added new system tray handlig (via TS so FUS should work with it) + Added new option [System] / shared_session=0 (or 1) to enable / disable the new shared memory framework (it is for now disabled by default) If you want to try this remember to change that option but also beware! it is dagerous and not finnished and and also there is as of now no security at all.
  • Property mode set to 100644
File size: 5.4 KB
Line 
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
29SysTray gSysTray;
30
31BOOL 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
37SysTray::SysTray() : icon(_T("SysTray")) {}
38SysTray::~SysTray() {}
39
40
41void SysTray::show() {
42        icon.createThread();
43}
44bool 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}
61void 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}
67bool SysTray::unloadModule() {
68        if (systemInfo::isBelowXP(systemInfo::getOSVersion())) {
69                hide();
70        }
71        return true;
72}
73
74int 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
104bool SysTray::hasCommandHandler() {
105        return false;
106}
107bool SysTray::hasMessageHandler() {
108        return true;
109}
110
111void SysTray::setLogWindow(HWND hWnd) {
112        MutexLock lock(logLock);
113        if (lock.hasMutex()) {
114                hLogWnd = hWnd;
115        }
116}
117
118void 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}
136SysTray::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
143extern void ShowIcon() {
144        gSysTray.show();
145}
146extern void HideIcon() {
147        gSysTray.hide();
148}
149NSC_WRAPPERS_MAIN_DEF(gSysTray);
150NSC_WRAPPERS_HANDLE_MSG_DEF(gSysTray);
151NSC_WRAPPERS_IGNORE_CMD_DEF();
152NSC_WRAPPERS_CLI_DEF(gSysTray);
153
Note: See TracBrowser for help on using the repository browser.