source: nscp/modules/SysTray/SysTray.cpp @ 5da0459

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

BROKEN!
First attempt at merging...

  • 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}
44
45bool SysTray::loadModule(NSCAPI::moduleLoadMode mode) {
46        if (mode == NSCAPI::normalStart) {
47                if (SETTINGS_GET_BOOL(settings_def::SHARED_SESSION)) {
48                        NSC_LOG_ERROR(_T("You have enabled shared session, systray module will not load..."));
49                        return true;
50                }
51                try {
52                        if ((serviceControll::GetServiceType(SZSERVICENAME)&SERVICE_INTERACTIVE_PROCESS)!=SERVICE_INTERACTIVE_PROCESS) {
53                                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."));
54                                return true;
55                        }
56                } catch (serviceControll::SCException e) {
57                        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."));
58                        return true;
59                }
60                show();
61        }
62        return true;
63}
64void SysTray::hide() {
65        if (!icon.exitThread(20000)) {
66                std::wcout << _T("MAJOR ERROR: Could not unload thread...") << std::endl;
67                NSC_LOG_ERROR(_T("Could not exit the thread, memory leak and potential corruption may be the result..."));
68        }
69}
70bool SysTray::unloadModule() {
71        if (systemInfo::isBelowXP(systemInfo::getOSVersion())) {
72                hide();
73        }
74        hide();
75        return true;
76}
77
78int SysTray::commandLineExec(const TCHAR* command,const unsigned int argLen,TCHAR** args) {
79        if (_wcsicmp(command, _T("install")) == 0) {
80                try {
81                        serviceControll::ModifyServiceType(SZSERVICENAME, SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS);
82                        std::wcout << _T(MODULE_NAME) << _T(" is now able to run as the SERVICE_INTERACTIVE_PROCESS flag has been set.") << std::endl;
83                } catch (const serviceControll::SCException& e) {
84                        std::wcerr << _T("Could not modify service: ") << e.error_ << std::endl;
85                        return -1;
86                } catch (...) {
87                        std::wcerr << _T("Could not modify service: Unknown error!") << std::endl;
88                        return -1;
89                }
90        } else if (_wcsicmp(command, _T("uninstall")) == 0) {
91                try {
92                        serviceControll::ModifyServiceType(SZSERVICENAME, SERVICE_WIN32_OWN_PROCESS);
93                        std::wcout << _T(" is now not able to run as the SERVICE_INTERACTIVE_PROCESS flag has been reset.") << std::endl;
94                } catch (const serviceControll::SCException& e) {
95                        std::wcerr << _T("Could not modify service: ") << e.error_ << std::endl;
96                        return -1;
97                } catch (...) {
98                        std::wcerr << _T("Could not modify service: Unknown error!") << std::endl;
99                        return -1;
100                }
101        } else {
102                std::wcerr << _T("Undefined command, usage: install or uninstall") << std::endl;
103                return -1;
104        }
105        return 0;
106}
107
108bool SysTray::hasCommandHandler() {
109        return false;
110}
111bool SysTray::hasMessageHandler() {
112        return true;
113}
114
115void SysTray::setLogWindow(HWND hWnd) {
116        MutexLock lock(logLock);
117        if (lock.hasMutex()) {
118                hLogWnd = hWnd;
119        }
120}
121
122void SysTray::handleMessage(int msgType, TCHAR* file, int line, TCHAR* message) {
123        log_entry record(msgType, file, line, message);
124        HWND hWnd = NULL;
125        {
126                MutexLock lock(logLock);
127                if (lock.hasMutex()) {
128                        log.push_back(record);
129                        if (log.size() > 50)
130                                log.pop_front();
131                        hWnd = hLogWnd;
132                } else {
133                        NSC_LOG_ERROR_STD(_T("Failed to get mutex in logger, message discarded"));
134                }
135        }
136        if (hWnd) {
137                SendMessage(hWnd, WM_USER+1, reinterpret_cast<WPARAM>(&record), NULL);
138        }
139}
140SysTray::log_type SysTray::getLog() {
141        log_type ret;
142        for (log_type::const_iterator cit = log.begin(); cit != log.end(); ++cit)
143                ret.push_back(*cit);
144        return ret;
145}
146
147extern void ShowIcon() {
148        gSysTray.show();
149}
150extern void HideIcon() {
151        gSysTray.hide();
152}
153NSC_WRAPPERS_MAIN_DEF(gSysTray);
154NSC_WRAPPERS_HANDLE_MSG_DEF(gSysTray);
155NSC_WRAPPERS_IGNORE_CMD_DEF();
156NSC_WRAPPERS_CLI_DEF(gSysTray);
157
Note: See TracBrowser for help on using the repository browser.