1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 *
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
9 *
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
14 *
15 * The Original Code is mozilla.org code.
16 *
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
21 *
22 * Contributor(s):
23 *
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
35 *
36 * ***** END LICENSE BLOCK ***** */
37
38#include "xp.h"
39#include "windowsx.h"
40
41#include "resource.h"
42#include "loggerw.h"
43#include "profilew.h"
44#include "actionnames.h"
45
46extern HINSTANCE hInst;
47static char szClassName[] = "NPSpyWindowClass";
48
49BOOL CALLBACK MainDlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
50BOOL CALLBACK PauseDlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
51
52LoggerWin::LoggerWin() : Logger(),
53  hWnd(NULL),
54  width(0),
55  height(0),
56  x(0),
57  y(0),
58  bSaveSettings(FALSE)
59{
60}
61
62LoggerWin::~LoggerWin()
63{
64}
65
66BOOL LoggerWin::platformInit()
67{
68  WNDCLASS wc;
69  wc.style         = 0;
70  wc.lpfnWndProc   = DefDlgProc;
71  wc.cbClsExtra    = 0;
72  wc.cbWndExtra    = DLGWINDOWEXTRA;
73  wc.hInstance     = hInst;
74  wc.hIcon         = LoadIcon(hInst, MAKEINTRESOURCE(IDI_ICON_APP));
75  wc.hCursor       = LoadCursor(0, IDC_ARROW);
76  wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
77  wc.lpszMenuName  = NULL;
78  wc.lpszClassName = szClassName;
79
80  if(!RegisterClass(&wc))
81    return FALSE;
82
83  // restore prefs
84  ProfileWin profile;
85
86  profile.getBool(NPSPY_REG_KEY_ONTOP, &bOnTop);
87  bOnTop = false;  // XXXMB
88  profile.getBool(NPSPY_REG_KEY_LOGTOWINDOW, &bToWindow);
89  profile.getBool(NPSPY_REG_KEY_LOGTOCONSOLE, &bToConsole);
90  profile.getBool(NPSPY_REG_KEY_LOGTOFILE, &bToFile);
91  profile.getBool(NPSPY_REG_KEY_SPALID, &bSPALID);
92  profile.getString(NPSPY_REG_KEY_LOGFILENAME, szFile, strlen(szFile));
93
94  for(int i = 1; i < TOTAL_NUMBER_OF_API_CALLS; i++)
95  {
96    BOOL selected = TRUE;
97    if(profile.getBool(ActionName[i], &selected))
98      bMutedCalls[i] = !selected;
99  }
100
101  if(!profile.getSizeAndPosition(&width, &height, &x, &y))
102  {
103    width = 0;
104    height = 0;
105    x = 0;
106    y = 0;
107  }
108
109  hWnd = CreateDialogParam(hInst, MAKEINTRESOURCE(IDD_DIALOG_MAIN), GetDesktopWindow(), (DLGPROC)MainDlgProc, (LPARAM)this);
110  if(hWnd == NULL)
111  {
112    UnregisterClass(szClassName, hInst);
113    return FALSE;
114  }
115
116  if(bOnTop)
117    SetWindowPos(hWnd, bOnTop ? HWND_TOPMOST : HWND_NOTOPMOST, 0,0,0,0, SWP_NOMOVE | SWP_NOSIZE);
118
119  return TRUE;
120}
121
122void LoggerWin::platformShut()
123{
124  if(hWnd != NULL)
125  {
126    char szLog[] = "--- GOING AWAY... PRESS SPACE BAR TO CONTINUE ---";
127    HWND hWndOutput = GetDlgItem(hWnd, IDC_MAIN_OUTPUT);
128    ListBox_AddString(hWndOutput, "");
129    ListBox_AddString(hWndOutput, szLog);
130    int count = ListBox_GetCount(hWndOutput);
131    ListBox_SetCaretIndex(hWndOutput, count - 1);
132    UpdateWindow(hWndOutput);
133
134    DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG_PAUSE), hWnd, (DLGPROC)PauseDlgProc);
135
136    ProfileWin profile;
137
138    RECT rc;
139    if(GetWindowRect(hWnd, &rc))
140      profile.setSizeAndPosition(rc.right - rc.left, rc.bottom - rc.top, rc.left, rc.top);
141
142    DestroyWindow(hWnd);
143    hWnd = NULL;
144  }
145
146  UnregisterClass(szClassName, hInst);
147}
148
149void LoggerWin::onDestroyWindow()
150{
151  hWnd = NULL;
152}
153
154void LoggerWin::dumpStringToMainWindow(const std::string& string)
155{
156  const char* output = string.c_str();
157  std::string temp_string;
158  // listboxes don't want <CR> and <LF> so cut them off if any. The order is important.
159  size_t newline = string.find('\n');
160  if(newline != std::string::npos) {
161    // make copy of string since it might be a constant
162    temp_string = string;
163
164    char* p = strrchr(const_cast<char*>(temp_string.c_str()), '\n');
165    *p = '\0';
166
167    p = strrchr(const_cast<char*>(temp_string.c_str()), '\r');
168    if(p)
169      *p = '\0';
170    output = temp_string.c_str();
171  }
172
173  HWND hWndOutput = GetDlgItem(hWnd, IDC_MAIN_OUTPUT);
174  ListBox_AddString(hWndOutput, output);
175  int count = ListBox_GetCount(hWndOutput);
176  if(count == 32767)
177    ListBox_ResetContent(hWndOutput);
178  ListBox_SetCaretIndex(hWndOutput, count - 1);
179  UpdateWindow(hWndOutput);
180}
181
182void LoggerWin::onClear()
183{
184  HWND hWndOutput = GetDlgItem(hWnd, IDC_MAIN_OUTPUT);
185  ListBox_ResetContent(hWndOutput);
186  UpdateWindow(hWndOutput);
187}
188
189Logger * NewLogger()
190{
191  LoggerWin * res = new LoggerWin();
192  return res;
193}
194
195void DeleteLogger(Logger * logger)
196{
197  if(logger)
198    delete logger;
199}