Back to index

lightning-sunbird  0.9+nobinonly
loggerw.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "xp.h"
00039 #include "windowsx.h"
00040 
00041 #include "resource.h"
00042 #include "loggerw.h"
00043 #include "profilew.h"
00044 #include "actionnames.h"
00045 
00046 extern HINSTANCE hInst;
00047 static char szClassName[] = "NPSpyWindowClass";
00048 
00049 BOOL CALLBACK MainDlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
00050 BOOL CALLBACK PauseDlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
00051 
00052 LoggerWin::LoggerWin() : Logger(),
00053   hWnd(NULL),
00054   width(0),
00055   height(0),
00056   x(0),
00057   y(0),
00058   bSaveSettings(FALSE)
00059 {
00060 }
00061 
00062 LoggerWin::~LoggerWin()
00063 {
00064 }
00065 
00066 BOOL LoggerWin::platformInit()
00067 {
00068   WNDCLASS wc;
00069   wc.style         = 0; 
00070   wc.lpfnWndProc   = DefDlgProc; 
00071   wc.cbClsExtra    = 0; 
00072   wc.cbWndExtra    = DLGWINDOWEXTRA; 
00073   wc.hInstance     = hInst; 
00074   wc.hIcon         = LoadIcon(hInst, MAKEINTRESOURCE(IDI_ICON_APP)); 
00075   wc.hCursor       = LoadCursor(0, IDC_ARROW); 
00076   wc.hbrBackground = (HBRUSH)(COLOR_BTNFACE + 1);
00077   wc.lpszMenuName  = NULL; 
00078   wc.lpszClassName = szClassName;
00079 
00080   if(!RegisterClass(&wc))
00081     return FALSE;
00082 
00083   // restore prefs
00084   ProfileWin profile;
00085 
00086   profile.getBool(NPSPY_REG_KEY_ONTOP, &bOnTop);
00087   profile.getBool(NPSPY_REG_KEY_LOGTOWINDOW, &bToWindow);
00088   profile.getBool(NPSPY_REG_KEY_LOGTOCONSOLE, &bToConsole);
00089   profile.getBool(NPSPY_REG_KEY_LOGTOFILE, &bToFile);
00090   profile.getBool(NPSPY_REG_KEY_SPALID, &bSPALID);
00091   profile.getString(NPSPY_REG_KEY_LOGFILENAME, szFile, strlen(szFile));
00092 
00093   for(int i = 1; i < TOTAL_NUMBER_OF_API_CALLS; i++)
00094   {
00095     BOOL selected = TRUE;
00096     if(profile.getBool(ActionName[i], &selected))
00097       bMutedCalls[i] = !selected;
00098   }
00099   
00100   if(!profile.getSizeAndPosition(&width, &height, &x, &y))
00101   {
00102     width = 0;
00103     height = 0;
00104     x = 0;
00105     y = 0;
00106   }
00107 
00108   hWnd = CreateDialogParam(hInst, MAKEINTRESOURCE(IDD_DIALOG_MAIN), GetDesktopWindow(), (DLGPROC)MainDlgProc, (LPARAM)this);
00109   if(hWnd == NULL)
00110   {
00111     UnregisterClass(szClassName, hInst);
00112     return FALSE;
00113   }
00114 
00115   if(bOnTop)
00116     SetWindowPos(hWnd, bOnTop ? HWND_TOPMOST : HWND_NOTOPMOST, 0,0,0,0, SWP_NOMOVE | SWP_NOSIZE);
00117 
00118   return TRUE;
00119 }
00120 
00121 void LoggerWin::platformShut()
00122 {
00123   if(hWnd != NULL)
00124   {
00125     char szLog[] = "--- GOING AWAY... PRESS SPACE BAR TO CONTINUE ---";
00126     HWND hWndOutput = GetDlgItem(hWnd, IDC_MAIN_OUTPUT);
00127     ListBox_AddString(hWndOutput, "");
00128     ListBox_AddString(hWndOutput, szLog);
00129     int count = ListBox_GetCount(hWndOutput);
00130     ListBox_SetCaretIndex(hWndOutput, count - 1);
00131     UpdateWindow(hWndOutput);
00132 
00133     DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG_PAUSE), hWnd, (DLGPROC)PauseDlgProc);
00134 
00135     ProfileWin profile;
00136 
00137     RECT rc;
00138     if(GetWindowRect(hWnd, &rc))
00139       profile.setSizeAndPosition(rc.right - rc.left, rc.bottom - rc.top, rc.left, rc.top);
00140 
00141     DestroyWindow(hWnd);
00142     hWnd = NULL;
00143   }
00144 
00145   UnregisterClass(szClassName, hInst);
00146 }
00147 
00148 void LoggerWin::onDestroyWindow()
00149 {
00150   hWnd = NULL;
00151 }
00152 
00153 void LoggerWin::dumpStringToMainWindow(char * string)
00154 {
00155   // listboxes don't want <CR> and <LF> so cut them off if any. The order is important.
00156   char * p = strrchr(string, '\n');
00157   if(p)
00158     *p = '\0';
00159 
00160   p = strrchr(string, '\r');
00161   if(p)
00162     *p = '\0';
00163 
00164   HWND hWndOutput = GetDlgItem(hWnd, IDC_MAIN_OUTPUT);
00165   ListBox_AddString(hWndOutput, string);
00166   int count = ListBox_GetCount(hWndOutput);
00167   if(count == 32767)
00168     ListBox_ResetContent(hWndOutput);
00169   ListBox_SetCaretIndex(hWndOutput, count - 1);
00170   UpdateWindow(hWndOutput);
00171 }
00172 
00173 void LoggerWin::onClear()
00174 {
00175   HWND hWndOutput = GetDlgItem(hWnd, IDC_MAIN_OUTPUT);
00176   ListBox_ResetContent(hWndOutput);
00177   UpdateWindow(hWndOutput);
00178 }
00179 
00180 Logger * NewLogger()
00181 {
00182   LoggerWin * res = new LoggerWin();
00183   return res;
00184 }
00185 
00186 void DeleteLogger(Logger * logger)
00187 {
00188   if(logger)
00189     delete logger;
00190 }