# Home    # nevrax.com   
Nevrax
Nevrax.org
#News
#Mailing-list
#Documentation
#CVS
#Bugs
#License
Docs
 
Documentation  
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages   Search  

report.cpp

Go to the documentation of this file.
00001 
00008 /* Copyright, 2002 Nevrax Ltd.
00009  *
00010  * This file is part of NEVRAX NEL.
00011  * NEVRAX NEL is free software; you can redistribute it and/or modify
00012  * it under the terms of the GNU General Public License as published by
00013  * the Free Software Foundation; either version 2, or (at your option)
00014  * any later version.
00015 
00016  * NEVRAX NEL is distributed in the hope that it will be useful, but
00017  * WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00019  * General Public License for more details.
00020 
00021  * You should have received a copy of the GNU General Public License
00022  * along with NEVRAX NEL; see the file COPYING. If not, write to the
00023  * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00024  * MA 02111-1307, USA.
00025  */
00026 
00027 #include "stdmisc.h"
00028 
00029 #include <stdio.h>
00030 #include <stdlib.h>
00031 #include <malloc.h>
00032 #include <nel/misc/common.h>
00033 
00034 #include "nel/misc/report.h"
00035 
00036 #ifdef NL_OS_WINDOWS
00037 #include <windows.h>
00038 #include <windowsx.h>
00039 #include <winuser.h>
00040 
00041 #ifdef min
00042 #undef min
00043 #endif
00044 
00045 #ifdef max
00046 #undef max
00047 #endif
00048 #endif
00049 
00050 using namespace std;
00051 
00052 namespace NLMISC 
00053 {
00054 
00055 #ifdef NL_OS_WINDOWS
00056 static HWND sendReport;
00057 #endif
00058 
00059 typedef bool (*TEmailFunction) (const std::string &smtpServer, const std::string &from, const std::string &to, const std::string &subject, const std::string &body, const std::string &attachedFile = "", bool onlyCheck = false);
00060 
00061 #define DELETE_OBJECT(a) if((a)!=NULL) { DeleteObject (a); a = NULL; }
00062 
00063 static TEmailFunction EmailFunction = NULL;
00064 
00065 void setReportEmailFunction (void *emailFunction)
00066 {
00067         EmailFunction = (TEmailFunction)emailFunction;
00068 
00069 #ifdef NL_OS_WINDOWS
00070         EnableWindow(sendReport, FALSE);
00071 #endif
00072 }
00073 
00074 #ifndef NL_OS_WINDOWS
00075 
00076 // Linux, do nothing
00077 
00078 void report ()
00079 {
00080 }
00081 
00082 #else
00083 
00084 // Windows specific version
00085 
00086 static string Body;
00087 static string Subject;
00088 
00089 static HWND checkIgnore;
00090 static HWND debug;
00091 static HWND ignore;
00092 static HWND quit;
00093 static HWND dialog;
00094 
00095 static bool NeedExit;
00096 static TReportResult Result;
00097 static bool IgnoreNextTime;
00098 
00099 static bool DebugDefaultBehavior, QuitDefaultBehavior;
00100 
00101 static LRESULT CALLBACK WndProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
00102 {
00103         //MSGFILTER *pmf;
00104 
00105         if (message == WM_COMMAND && HIWORD(wParam) == BN_CLICKED)
00106         {
00107                 if ((HWND) lParam == checkIgnore)
00108                 {
00109                         IgnoreNextTime = !IgnoreNextTime;
00110                 }
00111                 else if ((HWND) lParam == debug)
00112                 {
00113                         NeedExit = true;
00114                         Result = ReportDebug;
00115                         if (DebugDefaultBehavior)
00116                         {
00117                                 NLMISC_BREAKPOINT;
00118                         }
00119                 }
00120                 else if ((HWND) lParam == ignore)
00121                 {
00122                         NeedExit = true;
00123                         Result = ReportIgnore;
00124                 }
00125                 else if ((HWND) lParam == quit)
00126                 {
00127                         NeedExit = true;
00128                         Result = ReportQuit;
00129 
00130                         if (QuitDefaultBehavior)
00131                         {
00132                                 exit(EXIT_SUCCESS);
00133                         }
00134                 }
00135                 else if ((HWND) lParam == sendReport)
00136                 {
00137                         if (EmailFunction != NULL)
00138                         {
00139                                 bool res = EmailFunction ("", "", "", Subject, Body);
00140                                 if (res)
00141                                 {
00142                                         EnableWindow(sendReport, FALSE);
00143                                         MessageBox (dialog, "The email was successfully sent", "email", MB_OK);
00144                                 }
00145                                 else
00146                                 {
00147                                         MessageBox (dialog, "Failed to send the email", "email", MB_OK | MB_ICONERROR);
00148                                 }
00149                         }
00150                 }
00151         }
00152         else if (message == WM_CHAR)
00153         {
00154                 if (wParam == 27)
00155                 {
00156                         // ESC -> ignore
00157                         NeedExit = true;
00158                         Result = ReportIgnore;
00159                 }
00160         }
00161 
00162         return DefWindowProc (hWnd, message, wParam, lParam);
00163 }
00164 
00165 TReportResult report (const std::string &title, const std::string &header, const std::string &subject, const std::string &body, bool enableCheckIgnore, uint debugButton, bool ignoreButton, sint quitButton, bool sendReportButton, bool &ignoreNextTime)
00166 {
00167         // register the window
00168         static bool AlreadyRegister = false;
00169         if(!AlreadyRegister)
00170         {
00171                 WNDCLASS wc;
00172                 memset (&wc,0,sizeof(wc));
00173                 wc.style                        = CS_HREDRAW | CS_VREDRAW;
00174                 wc.lpfnWndProc          = (WNDPROC)WndProc;
00175                 wc.cbClsExtra           = 0;
00176                 wc.cbWndExtra           = 0;
00177                 wc.hInstance            = GetModuleHandle(NULL);
00178                 wc.hIcon                        = NULL;
00179                 wc.hCursor                      = LoadCursor(NULL,IDC_ARROW);
00180                 wc.hbrBackground        = (HBRUSH)COLOR_WINDOW;
00181                 wc.lpszClassName        = "NLReportWindow";
00182                 wc.lpszMenuName         = NULL;
00183                 if (!RegisterClass(&wc)) return ReportError;
00184                 AlreadyRegister = true;
00185         }
00186 
00187         string formatedTitle = title.empty() ? "NeL report" : title;
00188 
00189 
00190         // create the window
00191         dialog = CreateWindow ("NLReportWindow", formatedTitle.c_str(), WS_DLGFRAME | WS_CAPTION /*| WS_THICKFRAME*/, CW_USEDEFAULT, CW_USEDEFAULT, 456, 375,  NULL, NULL, GetModuleHandle(NULL), NULL);
00192 
00193         // create the font
00194         HFONT font = CreateFont (-12, 0, 0, 0, FW_DONTCARE, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, "Arial");
00195 
00196         Subject = subject;
00197 
00198         // create the edit control
00199         HWND edit = CreateWindow ("EDIT", NULL, WS_BORDER | WS_CHILD | WS_VISIBLE | WS_HSCROLL | WS_VSCROLL | ES_READONLY | ES_LEFT | ES_MULTILINE, 7, 70, 429, 212, dialog, (HMENU) NULL, (HINSTANCE) GetWindowLong(dialog, GWL_HINSTANCE), NULL);
00200         SendMessage (edit, WM_SETFONT, (LONG) font, TRUE);
00201 
00202         // set the edit text limit to lot of :)
00203         SendMessage (edit, EM_LIMITTEXT, -1, 0);
00204 
00205         string formatedBody;
00206         // replace \n with \r\n
00207         for (uint i = 0; i < body.size(); i++)
00208         {
00209                 if (body[i] == '\n' && i > 0 && body[i-1] != '\r')
00210                 {
00211                         formatedBody += '\r';
00212                 }
00213                 formatedBody += body[i];
00214         }
00215 
00216         Body = formatedBody;
00217 
00218         // set the message in the edit text
00219         SendMessage (edit, WM_SETTEXT, (WPARAM)0, (LPARAM)formatedBody.c_str());
00220 
00221         if (enableCheckIgnore)
00222         {
00223                 // create the combo box control
00224                 checkIgnore = CreateWindow ("BUTTON", "Don't display this report again", WS_CHILD | WS_VISIBLE | BS_AUTOCHECKBOX | BS_CHECKBOX, 7, 290, 429, 18, dialog, (HMENU) NULL, (HINSTANCE) GetWindowLong(dialog, GWL_HINSTANCE), NULL);
00225                 SendMessage (checkIgnore, WM_SETFONT, (LONG) font, TRUE);
00226 
00227                 if(ignoreNextTime)
00228                 {
00229                         SendMessage (checkIgnore, BM_SETCHECK, BST_CHECKED, 0);
00230                 }
00231         }
00232 
00233         // create the debug button control
00234         debug = CreateWindow ("BUTTON", "Debug", WS_CHILD | WS_VISIBLE, 7, 315, 75, 25, dialog, (HMENU) NULL, (HINSTANCE) GetWindowLong(dialog, GWL_HINSTANCE), NULL);
00235         SendMessage (debug, WM_SETFONT, (LONG) font, TRUE);
00236 
00237         if (debugButton == 0)
00238                 EnableWindow(debug, FALSE);
00239 
00240         // create the ignore button control
00241         ignore = CreateWindow ("BUTTON", "Ignore", WS_CHILD | WS_VISIBLE, 75+7+7, 315, 75, 25, dialog, (HMENU) NULL, (HINSTANCE) GetWindowLong(dialog, GWL_HINSTANCE), NULL);
00242         SendMessage (ignore, WM_SETFONT, (LONG) font, TRUE);
00243 
00244         if (ignoreButton == 0)
00245                 EnableWindow(ignore, FALSE);
00246 
00247         // create the quit button control
00248         quit = CreateWindow ("BUTTON", "Quit", WS_CHILD | WS_VISIBLE, 75+75+7+7+7, 315, 75, 25, dialog, (HMENU) NULL, (HINSTANCE) GetWindowLong(dialog, GWL_HINSTANCE), NULL);
00249         SendMessage (quit, WM_SETFONT, (LONG) font, TRUE);
00250 
00251         if (quitButton == 0)
00252                 EnableWindow(quit, FALSE);
00253 
00254         // create the debug button control
00255         sendReport = CreateWindow ("BUTTON", "Send report", WS_CHILD | WS_VISIBLE, 429-80, 315, 90, 25, dialog, (HMENU) NULL, (HINSTANCE) GetWindowLong(dialog, GWL_HINSTANCE), NULL);
00256         SendMessage (sendReport, WM_SETFONT, (LONG) font, TRUE);
00257 
00258         string formatedHeader;
00259         if (header.empty())
00260         {
00261                 formatedHeader = "This application stopped to display this report.";
00262         }
00263         else
00264         {
00265                 formatedHeader = header;
00266         }
00267 
00268         // ace don't do that because it s slow to try to send a mail
00269         //bool canSendReport  = sendReportButton && EmailFunction != NULL && EmailFunction("", "", "", "", "", true);
00270         bool canSendReport  = sendReportButton && EmailFunction != NULL;
00271 
00272         if (canSendReport)
00273                 formatedHeader += " Send report will only email the contents of the box below. Please, send it to help us.";
00274         else
00275                 EnableWindow(sendReport, FALSE);
00276 
00277 
00278         // create the label control
00279         HWND label = CreateWindow ("STATIC", formatedHeader.c_str(), WS_CHILD | WS_VISIBLE /*| SS_WHITERECT*/, 7, 7, 429, 51, dialog, (HMENU) NULL, (HINSTANCE) GetWindowLong(dialog, GWL_HINSTANCE), NULL);
00280         SendMessage (label, WM_SETFONT, (LONG) font, TRUE);
00281 
00282         
00283         
00284         DebugDefaultBehavior = debugButton==1;
00285         QuitDefaultBehavior = quitButton==1;
00286 
00287         IgnoreNextTime = ignoreNextTime;
00288 
00289         ShowCursor (TRUE);
00290 
00291         SetWindowPos (dialog, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
00292 
00293         NeedExit = false;
00294 
00295         while(!NeedExit)
00296         {
00297                 MSG     msg;
00298                 while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
00299                 {
00300                         TranslateMessage(&msg);
00301                         DispatchMessage(&msg);
00302                 }
00303                 nlSleep (1);
00304         }
00305 
00306         // set the user result
00307         ignoreNextTime = IgnoreNextTime;
00308 
00309         ShowWindow(dialog, SW_HIDE);
00310 
00311         DELETE_OBJECT(sendReport)
00312         DELETE_OBJECT(quit)
00313         DELETE_OBJECT(ignore)
00314         DELETE_OBJECT(debug)
00315         DELETE_OBJECT(checkIgnore)
00316         DELETE_OBJECT(edit)
00317         DELETE_OBJECT(label)
00318         DELETE_OBJECT(dialog)
00319 
00320         return Result;
00321 }
00322 
00323 #endif
00324 
00325 
00326 } // NLMISC