File: CrashHandler.cpp

package info (click to toggle)
subcommander 2.0.0~b5p2-5
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 15,344 kB
  • sloc: cpp: 63,594; sh: 4,050; xml: 1,992; makefile: 1,134; ansic: 786; ruby: 251; lisp: 24
file content (132 lines) | stat: -rw-r--r-- 4,356 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/* ====================================================================
 * Copyright (c) 2006-2007, Martin Hauner
 *                          http://subcommander.tigris.org
 *
 * Subcommander is licensed as described in the file doc/COPYING, which
 * you should have received as part of this distribution.
 * ====================================================================
 */

// sc
#include "config.h"
#include "CrashHandler.h"
#include "version.out.h"
#include "Utility.h"

// qt
#include <QtCore/QString>

// sys
#ifdef _WIN32
#include <mapi.h>
#endif // _WIN32

#if _WIN32
// helper
static const char* copyString( const sc::String& src )
{
  QString s = QString::fromUtf8(src); 
  QByteArray qs = s.toLocal8Bit();
  char* result = (char*)calloc( qs.length()+1, 1 );
  strncpy( result, (const char*)qs, qs.length() );
  return result;
}

static sc::String getSubject()
{
  QString sbj = _q("%1 [%2] crashed...").arg(getLongAppName()).arg(SUBCOMMANDER_VERSION);
  return sc::String(sbj.toUtf8());
}

static sc::String getMessage()
{
  QString msg;
  msg +=
    _q("Please send the attached stack trace and crash dump to crash@subcommander.tigris.org.");
  msg += "\n\n";
  msg +=
    _q("<If you are able to reproduce the crash please describe how.>" );
  msg += "\n\n";
  msg +=
    _q("Thank you.");

  return sc::String(msg.toUtf8());
}
#endif

CrashHandler::CrashHandler()
{
}

void CrashHandler::handleException( const sc::String& stack, const sc::String& dump )
{
#if _WIN32
  typedef ULONG (FAR PASCAL *MAPISendMail)(
    LHANDLE lhSession,ULONG ulUIParam,lpMapiMessage lpMessage,FLAGS flFlags,ULONG ulReserved);

  HMODULE hMapi = LoadLibrary( "MAPI32.dll" );
  if( hMapi )
  {
    MAPISendMail MapiSendMail = (MAPISendMail)GetProcAddress( hMapi, "MAPISendMail" );
    if( MapiSendMail )
    {
      MapiFileDesc attachment[] =
      {
        { 
          0,                                             // reserved
          0,                                             // data file
          (ULONG)-1,                                     // position
          (LPSTR)copyString(stack),                      // full path
          NULL,                                          // display name
          NULL                                           // file details
        },
        { 
          0,                                             // reserved
          0,                                             // data file
          (ULONG)-1,                                     // position
          (LPSTR)copyString(dump),                       // full path
          NULL,                                          // display name
          NULL                                           // file details
        }
      };


      sc::String target(_s("crash@subcommander.tigris.org"));

      MapiRecipDesc recipient =
      {
        0,                                             // reserved
        MAPI_TO,                                       // recipient class
        NULL,                                          // name
        (LPSTR)copyString(target),                     // address
        0,                                             // entry id size
        NULL                                           // entry id
      };


      sc::String subject = getSubject();
      sc::String text    = getMessage();

      MapiMessage message =
      {
        0,                                             // reserved
        (LPSTR)copyString(subject),                    // subject
        (LPSTR)copyString(text),                       // message
        NULL,                                          // message type
        NULL,                                          // date
        NULL,                                          // conversation ID
        0L,                                            // flags
        NULL,                                          // originator
        1,                                             // recipient count
        &recipient,                                    // recipents
        2,                                             // attachment count
        &attachment[0]                                 // attachments
      };

      ULONG res = MapiSendMail( 0L, 0L, &message, MAPI_DIALOG, 0L );
    }

    FreeLibrary(hMapi);
  }
#endif // _WIN32
}