File: ErrorDialog.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 (289 lines) | stat: -rw-r--r-- 8,320 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/* ====================================================================
 * Copyright (c) 2003-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 "ErrorDialog.h" 
#include "MessageBox.h"
#include "Utility.h"
#include "util/Exception.h"
#include "util/ExceptionStack.h"
#include "util/ErrorCodes.h"

// qt
#include <QtGui/QLayout>
#include <QtGui/QLabel>
#include <QtGui/QPushButton>
#include <QtGui/QTextEdit>
#include <QtGui/QGroupBox>
#include <QtGui/QLineEdit>

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


ErrorDialog::ErrorDialog( const sc::Exception& e, QWidget *parent )
: super( parent )
{
  const sc::ExceptionStack* est = dynamic_cast<const sc::ExceptionStack*>(&e);
  if( est )
  {
    setup(e.getError(),est->getStack());
  }
  else
  {
    Stackframes frames;
    setup(e.getError(),frames);
  }
}

ErrorDialog::ErrorDialog( const sc::Error* err, const Stackframes& stack, QWidget* parent )
: super( parent )
{
  setup(err,stack);
}

void ErrorDialog::setup( const sc::Error* err, const Stackframes& stack )
{
  setModal(true); // qt4
  setWindowTitle( _q("%1:exception").arg(getAppName()) );

  QString error = QString(err->getMessage());

  // extract dump file name
  const sc::Error* nested = err->getNested();
  if( nested && nested->getCode() == sc::ErrDump )
  {
    _dump = QString(nested->getMessage());
  }

  QVBoxLayout *vbl = new QVBoxLayout(this);
  vbl->setMargin(5);
  vbl->setSpacing(10);
  {
    QLabel *l = new QLabel(this);
    vbl->addWidget(l,0);
    l->setMargin(2);
    l->setText( 
      "<qt><font size=3><b>" +
      _q("sorry, %1 has a problem... :-(").arg(getLongAppName())+ 
      "</b></font></qt>" );
    l->setAlignment( Qt::AlignCenter );

    QGroupBox*   gb  = new QGroupBox(this);
    QVBoxLayout* gbl = new QVBoxLayout(gb);
    gbl->setContentsMargins(0,0,0,0);
    gb->setTitle( _q("crash details: ") );
    gb->setFlat(true);
    vbl->addWidget(gb);

    QLabel *l2 = new QLabel(this);
    vbl->addWidget(l2,0);
    l2->setMargin(2);
    l2->setText(error);
    l2->setAlignment( Qt::AlignCenter );
    //l2->setPaletteBackgroundColor( QColor(255,120,120) ); // qt4
    //l2->setBackgroundRole( QPalette::Window );

    QString textl3;
    textl3 +=
      _q("Please send this crash information and the crash dump\n"
      "(%1)\n"
      "to crash@subcommander.tigris.org.\n\n").arg(_dump);
    textl3 += 
      _q("Just press 'Send' to open your email client with a prepared email.");
    textl3 +=
      _q("\nIf this doesn't work please send the information manually.\nThank you.");

    QLabel *l3 = new QLabel(this);
    vbl->addWidget(l3,0);
    l3->setMargin(2);
    l3->setText(textl3);
    l3->setAlignment( Qt::AlignCenter );

    QGroupBox* gb2 = new QGroupBox(this); // qt4, layout needed?
    gb2->setTitle( _q("Send:") );
    gb2->setFlat(true);
    vbl->addWidget(gb2);

    _text = new QTextEdit(this);
    vbl->addWidget(_text,3);
    //te->setReadOnly(true);
    _text->setLineWidth(1);
    _text->setContentsMargins(1,1,1,1); // qt4
    _text->setWordWrapMode(QTextOption::NoWrap);
    _text->setFrameStyle( QFrame::Box | QFrame::Sunken );
    _text->setMinimumWidth(460);
    _text->setMinimumHeight(200);
    //te->setPaper( backgroundColor() );

    QString text;
    text += _q("email:\n");
    text += _q("crash@subcommander.tigris.org");
    text += _q("\n\nCrash Dump:\n");
    text += _dump;
    text += "\n\n";
    text += _q("Hi,\n\n%1 crashed when I ...\n\n").arg(getLongAppName());
    text += _q("<If you are able to reproduce the crash please describe how. Thank you!>" );
    text += "\n\n\n";
    text += "\n\n";
    text += _q("*** crash information: ***");
    text += "\n\n" + getAppName() + ":exception - " + error + "\n\n";

    for( Stackframes::const_iterator it = stack.begin(); it != stack.end(); it++ )
    {
      const Stackframe& f = *it;

      if(!f._error)
      {
        // address
        QString addr;
        text += "0x" +addr.setNum(f._addr,16).rightJustified(16,'0'); // qt4
        //+":0x" + addr.setNum(f._addrSeg).rightJustify(4,'0');

        if(f._symbol)
        {
          text += " : " +QString(f._symbolName);
        }
        else
        {
          text += " : " +QString("unknown");
        }

        if(f._module)
        {
          text += " " + QString(f._moduleName);
        }
        else
        {
          text += " " + QString("unknown");
        }

        text += "\n  ";

        if(f._line)
        {
          text += addr.setNum(f._lineNr);
          text += " + 0x" + addr.setNum(f._lineDisp,16) + " (";
          text += QString(f._fileName) + ")";
          text += "\n  ";
        }
        //else
        //{
        //text += QString("0")+ " (";
        //text += QString("unknown") + ")";
        //}

      }

      text += "\n";
    }

    _text->setText( text );

    QHBoxLayout* hu = new QHBoxLayout;
    vbl->addLayout(hu);
    {
      // eats extra space, so the buttons keep their size
      hu->addStretch(1); 

      QPushButton* send = new QPushButton(this);
      send->setText( _q("S&end") );
#ifdef _WIN32
      send->setDefault(true);
#else
      send->setDefault(false);
#endif // _WIN32
      hu->addWidget(send);

      connect( send, SIGNAL(clicked()), SLOT(send()) );

      QPushButton* cancel = new QPushButton(this);
      cancel->setText( _q("&Cancel") );
      hu->addWidget(cancel);

      connect( cancel, SIGNAL(clicked()), SLOT(reject()) );
    }
  }
}

ErrorDialog::~ErrorDialog()
{
}
#if _WIN32
static const char* copyString( const QString& s )
{
  QByteArray qs = s.toLocal8Bit();
  char* result = (char*)calloc( qs.length()+1, 1 );
  strncpy( result, (const char*)qs, qs.length() );
  return result;
}
#endif

void ErrorDialog::send()
{
#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(_dump),                      // full path
        NULL,                                          // display name
        NULL                                           // file details
      };


      QString target(_q("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
      };


      MapiMessage message =
      {
        0,                                             // reserved
        (LPSTR)copyString(_q("crash..!")),             // subject
        (LPSTR)copyString(_text->toPlainText()),       // message
        NULL,                                          // message type
        NULL,                                          // date
        NULL,                                          // conversation ID
        0L,                                            // flags
        NULL,                                          // originator
        1,                                             // recipient count
        &recipient,                                    // recipents
        1,                                             // attachment count
        &attachment                                    // attachments
      };

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

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