<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/> <title>Vidalia: main.cpp Source File</title> <link href="tabs.css" rel="stylesheet" type="text/css"/> <link href="doxygen.css" rel="stylesheet" type="text/css"/> </head> <body> <!-- Generated by Doxygen 1.6.3 --> <div class="navigation" id="top"> <div class="tabs"> <ul> <li><a href="index.html"><span>Main Page</span></a></li> <li><a href="namespaces.html"><span>Namespaces</span></a></li> <li><a href="annotated.html"><span>Classes</span></a></li> <li class="current"><a href="files.html"><span>Files</span></a></li> <li><a href="dirs.html"><span>Directories</span></a></li> </ul> </div> <div class="tabs"> <ul> <li><a href="files.html"><span>File List</span></a></li> <li><a href="globals.html"><span>File Members</span></a></li> </ul> </div> <div class="navpath"><a class="el" href="dir_b47abd1aad201a152869566145babb61.html">src</a> » <a class="el" href="dir_038e8b3624109884d464700e800a8093.html">crashreporter</a> </div> </div> <div class="contents"> <h1>crashreporter/main.cpp</h1><a href="crashreporter_2main_8cpp.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/*</span> <a name="l00002"></a>00002 <span class="comment">** This file is part of Vidalia, and is subject to the license terms in the</span> <a name="l00003"></a>00003 <span class="comment">** LICENSE file, found in the top level directory of this distribution. If you</span> <a name="l00004"></a>00004 <span class="comment">** did not receive the LICENSE file with this file, you may obtain it from the</span> <a name="l00005"></a>00005 <span class="comment">** Vidalia source package distributed by the Vidalia Project at</span> <a name="l00006"></a>00006 <span class="comment">** http://www.vidalia-project.net/. No part of Vidalia, including this file,</span> <a name="l00007"></a>00007 <span class="comment">** may be copied, modified, propagated, or distributed except according to the</span> <a name="l00008"></a>00008 <span class="comment">** terms described in the LICENSE file.</span> <a name="l00009"></a>00009 <span class="comment">*/</span> <a name="l00010"></a>00010 <a name="l00011"></a>00011 <span class="comment">/*</span> <a name="l00012"></a>00012 <span class="comment">** \file main.cpp</span> <a name="l00013"></a>00013 <span class="comment">** \version $Id$</span> <a name="l00014"></a>00014 <span class="comment">** \brief Application that is run after Vidalia crashes and asks the</span> <a name="l00015"></a>00015 <span class="comment">** user if they would like to submit the crash report.</span> <a name="l00016"></a>00016 <span class="comment">*/</span> <a name="l00017"></a>00017 <a name="l00018"></a>00018 <span class="preprocessor">#include "<a class="code" href="_crash_report_dialog_8h.html">CrashReportDialog.h</a>"</span> <a name="l00019"></a>00019 <a name="l00020"></a>00020 <span class="preprocessor">#include <QApplication></span> <a name="l00021"></a>00021 <span class="preprocessor">#include <QFileInfo></span> <a name="l00022"></a>00022 <span class="preprocessor">#include <QMessageBox></span> <a name="l00023"></a>00023 <span class="preprocessor">#include <QTextStream></span> <a name="l00024"></a>00024 <span class="preprocessor">#include <QTextCodec></span> <a name="l00025"></a>00025 <a name="l00026"></a>00026 <span class="comment"></span> <a name="l00027"></a>00027 <span class="comment">/** Open the minidump file given by <b>fileName</b> and read its entire</span> <a name="l00028"></a>00028 <span class="comment"> * contents into a QByteArray. Returns the contents of the minidump file</span> <a name="l00029"></a>00029 <span class="comment"> * on success. If an error occurs, this returns a default-constructed</span> <a name="l00030"></a>00030 <span class="comment"> * QByteArray and sets <b>errorMessage</b> to a string describing the error</span> <a name="l00031"></a>00031 <span class="comment"> * ecountered.</span> <a name="l00032"></a>00032 <span class="comment"> */</span> <a name="l00033"></a>00033 QByteArray <a name="l00034"></a><a class="code" href="crashreporter_2main_8cpp.html#ae449ceb371bcc6a5349a09c14da744fc">00034</a> <a class="code" href="crashreporter_2main_8cpp.html#ae449ceb371bcc6a5349a09c14da744fc">read_minidump_file</a>(<span class="keyword">const</span> QString &fileName, QString &errorMessage) <a name="l00035"></a>00035 { <a name="l00036"></a>00036 QByteArray md; <a name="l00037"></a>00037 QFile mdFile(fileName); <a name="l00038"></a>00038 <a name="l00039"></a>00039 <span class="keywordflow">if</span> (! mdFile.open(QIODevice::ReadOnly)) { <a name="l00040"></a>00040 errorMessage = mdFile.errorString(); <a name="l00041"></a>00041 <span class="keywordflow">return</span> QByteArray(); <a name="l00042"></a>00042 } <a name="l00043"></a>00043 <span class="keywordflow">while</span> (! mdFile.atEnd()) { <a name="l00044"></a>00044 md.append(mdFile.readAll()); <a name="l00045"></a>00045 <span class="keywordflow">if</span> (mdFile.error() != QFile::NoError) { <a name="l00046"></a>00046 errorMessage = mdFile.errorString(); <a name="l00047"></a>00047 <span class="keywordflow">return</span> QByteArray(); <a name="l00048"></a>00048 } <a name="l00049"></a>00049 } <a name="l00050"></a>00050 mdFile.close(); <a name="l00051"></a>00051 <span class="keywordflow">return</span> md; <a name="l00052"></a>00052 } <a name="l00053"></a>00053 <span class="comment"></span> <a name="l00054"></a>00054 <span class="comment">/** Read the crash dump annotations file given by <b>fileName</b> and parse</span> <a name="l00055"></a>00055 <span class="comment"> * each line into a Key=Value pair. Returns a QHash containing the keys</span> <a name="l00056"></a>00056 <span class="comment"> * mapped to their values on success. If a file or parse error occurs, this</span> <a name="l00057"></a>00057 <span class="comment"> * returns a default-constructed QHash and sets <b>errorMessage</b> to a</span> <a name="l00058"></a>00058 <span class="comment"> * string describing the error encountered.</span> <a name="l00059"></a>00059 <span class="comment"> */</span> <a name="l00060"></a>00060 QHash<QString,QString> <a name="l00061"></a><a class="code" href="crashreporter_2main_8cpp.html#aa44efe08a13deffbec4467faa932b5c9">00061</a> <a class="code" href="crashreporter_2main_8cpp.html#aa44efe08a13deffbec4467faa932b5c9">read_annotations_file</a>(<span class="keyword">const</span> QString &fileName, QString &errorMessage) <a name="l00062"></a>00062 { <a name="l00063"></a>00063 QHash<QString, QString> annotations; <a name="l00064"></a>00064 <a name="l00065"></a>00065 <span class="comment">/* Open the annotations file for reading text */</span> <a name="l00066"></a>00066 QFile infile(fileName); <a name="l00067"></a>00067 <span class="keywordflow">if</span> (! infile.open(QIODevice::ReadOnly | QIODevice::Text)) { <a name="l00068"></a>00068 errorMessage = infile.errorString(); <a name="l00069"></a>00069 <span class="keywordflow">return</span> QHash<QString,QString>(); <a name="l00070"></a>00070 } <a name="l00071"></a>00071 <a name="l00072"></a>00072 <span class="comment">/* The annotations file should be UTF-8 encoded */</span> <a name="l00073"></a>00073 QTextStream reader(&infile); <a name="l00074"></a>00074 reader.setCodec(QTextCodec::codecForName(<span class="stringliteral">"utf-8"</span>)); <a name="l00075"></a>00075 <span class="keywordflow">while</span> (! reader.atEnd()) { <a name="l00076"></a>00076 QString line = reader.readLine().trimmed(); <a name="l00077"></a>00077 <span class="keywordflow">if</span> (line.isEmpty()) <a name="l00078"></a>00078 <span class="keywordflow">continue</span>; <a name="l00079"></a>00079 <a name="l00080"></a>00080 <span class="keywordtype">int</span> idx = line.indexOf(<span class="stringliteral">"="</span>); <a name="l00081"></a>00081 <span class="keywordflow">if</span> (idx > 0 && idx < line.length()-1) { <a name="l00082"></a>00082 QString key = line.mid(0, idx).trimmed(); <a name="l00083"></a>00083 QString val = line.mid(idx + 1).trimmed(); <a name="l00084"></a>00084 annotations.insert(key, val); <a name="l00085"></a>00085 } <a name="l00086"></a>00086 } <a name="l00087"></a>00087 <span class="keywordflow">return</span> annotations; <a name="l00088"></a>00088 } <a name="l00089"></a>00089 <a name="l00090"></a>00090 <span class="keywordtype">int</span> <a name="l00091"></a><a class="code" href="crashreporter_2main_8cpp.html#a0ddf1224851353fc92bfbff6f499fa97">00091</a> <a class="code" href="_c_make_c_x_x_compiler_id_8cpp.html#a0ddf1224851353fc92bfbff6f499fa97">main</a>(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> *argv[]) <a name="l00092"></a>00092 { <a name="l00093"></a>00093 QApplication app(argc, argv); <a name="l00094"></a>00094 <a class="code" href="class_crash_report_dialog.html">CrashReportDialog</a> crashDialog; <a name="l00095"></a>00095 QFileInfo minidumpFile, extraInfoFile; <a name="l00096"></a>00096 QString minidumpFilePath, extraInfoFilePath, errorMessage; <a name="l00097"></a>00097 QHash<QString,QString> annotations; <a name="l00098"></a>00098 QByteArray minidump; <a name="l00099"></a>00099 <a name="l00100"></a>00100 <span class="keywordflow">if</span> (argc < 2) { <a name="l00101"></a>00101 errorMessage = <span class="stringliteral">"No minidump file specified."</span>; <a name="l00102"></a>00102 <span class="keywordflow">goto</span> <a class="code" href="stringutil_8cpp.html#a6e3f242b28c9165146182aec25e351a3">err</a>; <a name="l00103"></a>00103 } <a name="l00104"></a>00104 <a name="l00105"></a>00105 <span class="comment">/* Ensure that the specified minidump file exists and is readable */</span> <a name="l00106"></a>00106 minidumpFile = QFileInfo(argv[1]); <a name="l00107"></a>00107 minidumpFilePath = minidumpFile.absoluteFilePath(); <a name="l00108"></a>00108 <span class="keywordflow">if</span> (! minidumpFile.exists() || ! minidumpFile.size()) { <a name="l00109"></a>00109 errorMessage = QString(<span class="stringliteral">"The specified minidump file does not exist: %1"</span>) <a name="l00110"></a>00110 .arg(minidumpFilePath); <a name="l00111"></a>00111 <span class="keywordflow">goto</span> <a class="code" href="stringutil_8cpp.html#a6e3f242b28c9165146182aec25e351a3">err</a>; <a name="l00112"></a>00112 } <a name="l00113"></a>00113 <span class="keywordflow">if</span> (! minidumpFile.isReadable()) { <a name="l00114"></a>00114 errorMessage = QString(<span class="stringliteral">"The specified minidump file is not readable: %1"</span>) <a name="l00115"></a>00115 .arg(minidumpFilePath); <a name="l00116"></a>00116 <span class="keywordflow">goto</span> <a class="code" href="stringutil_8cpp.html#a6e3f242b28c9165146182aec25e351a3">err</a>; <a name="l00117"></a>00117 } <a name="l00118"></a>00118 <a name="l00119"></a>00119 <span class="comment">/* Ensure that the specified minidump has an associated extra crash</span> <a name="l00120"></a>00120 <span class="comment"> * information file that exists and is readable. */</span> <a name="l00121"></a>00121 extraInfoFile = QFileInfo(minidumpFilePath + <span class="stringliteral">".info"</span>); <a name="l00122"></a>00122 extraInfoFilePath = extraInfoFile.absoluteFilePath(); <a name="l00123"></a>00123 <span class="keywordflow">if</span> (! extraInfoFile.exists() || ! extraInfoFile.size()) { <a name="l00124"></a>00124 errorMessage = QString(<span class="stringliteral">"The specified minidump does not have a "</span> <a name="l00125"></a>00125 <span class="stringliteral">"corresponding crash annotations file: %1"</span>) <a name="l00126"></a>00126 .arg(extraInfoFilePath); <a name="l00127"></a>00127 <span class="keywordflow">goto</span> <a class="code" href="stringutil_8cpp.html#a6e3f242b28c9165146182aec25e351a3">err</a>; <a name="l00128"></a>00128 } <a name="l00129"></a>00129 <span class="keywordflow">if</span> (! extraInfoFile.isReadable()) { <a name="l00130"></a>00130 errorMessage = QString(<span class="stringliteral">"The specified crash information file is not "</span> <a name="l00131"></a>00131 <span class="stringliteral">"readable: %1"</span>).arg(extraInfoFilePath); <a name="l00132"></a>00132 <span class="keywordflow">goto</span> <a class="code" href="stringutil_8cpp.html#a6e3f242b28c9165146182aec25e351a3">err</a>; <a name="l00133"></a>00133 } <a name="l00134"></a>00134 <a name="l00135"></a>00135 <span class="comment">/* Read the minidump file's contents */</span> <a name="l00136"></a>00136 minidump = <a class="code" href="crashreporter_2main_8cpp.html#ae449ceb371bcc6a5349a09c14da744fc">read_minidump_file</a>(minidumpFilePath, errorMessage); <a name="l00137"></a>00137 <span class="keywordflow">if</span> (minidump.isNull()) { <a name="l00138"></a>00138 errorMessage = QString(<span class="stringliteral">"Unable to read minidump file '%1': %2"</span>) <a name="l00139"></a>00139 .arg(minidumpFilePath) <a name="l00140"></a>00140 .arg(errorMessage); <a name="l00141"></a>00141 <span class="keywordflow">goto</span> <a class="code" href="stringutil_8cpp.html#a6e3f242b28c9165146182aec25e351a3">err</a>; <a name="l00142"></a>00142 } <a name="l00143"></a>00143 <span class="comment">/* Read and parse the crash annotations file */</span> <a name="l00144"></a>00144 annotations = <a class="code" href="crashreporter_2main_8cpp.html#aa44efe08a13deffbec4467faa932b5c9">read_annotations_file</a>(extraInfoFilePath, errorMessage); <a name="l00145"></a>00145 <span class="keywordflow">if</span> (annotations.isEmpty()) { <a name="l00146"></a>00146 errorMessage = QString(<span class="stringliteral">"Unable to read crash annotations file '%1': %2"</span>) <a name="l00147"></a>00147 .arg(extraInfoFilePath) <a name="l00148"></a>00148 .arg(errorMessage); <a name="l00149"></a>00149 <span class="keywordflow">goto</span> <a class="code" href="stringutil_8cpp.html#a6e3f242b28c9165146182aec25e351a3">err</a>; <a name="l00150"></a>00150 } <a name="l00151"></a>00151 <a name="l00152"></a>00152 <span class="comment">/* Display the crash reporting dialog */</span> <a name="l00153"></a>00153 crashDialog.<a class="code" href="class_crash_report_dialog.html#a8ea6147e516b2f7b8581aacb1f0031ed">setMinidump</a>(minidumpFile.baseName(), minidump); <a name="l00154"></a>00154 crashDialog.<a class="code" href="class_crash_report_dialog.html#a06d533cf39f3b2665e4784aa3bca3a17">setCrashAnnotations</a>(annotations); <a name="l00155"></a>00155 crashDialog.show(); <a name="l00156"></a>00156 <span class="keywordflow">return</span> app.exec(); <a name="l00157"></a>00157 <a name="l00158"></a>00158 <a class="code" href="stringutil_8cpp.html#a6e3f242b28c9165146182aec25e351a3">err</a>: <a name="l00159"></a>00159 <span class="comment">/* We encountered an error trying to load the minidump or extra crash</span> <a name="l00160"></a>00160 <span class="comment"> * information file. So, display an error and then bail, since now there's</span> <a name="l00161"></a>00161 <span class="comment"> * nothing for us to send. */</span> <a name="l00162"></a>00162 QMessageBox dlg; <a name="l00163"></a>00163 dlg.setWindowIcon(QIcon(<span class="stringliteral">":/images/32x32/tools-report-bug.png"</span>)); <a name="l00164"></a>00164 dlg.setWindowTitle(<span class="stringliteral">"Crash Reporter Error"</span>); <a name="l00165"></a>00165 <a name="l00166"></a>00166 dlg.setIconPixmap(QPixmap(<span class="stringliteral">":/images/64x64/tools-report-bug.png"</span>)); <a name="l00167"></a>00167 dlg.setStandardButtons(QMessageBox::Ok); <a name="l00168"></a>00168 <a name="l00169"></a>00169 dlg.setText(<span class="stringliteral">"<b>Vidalia encountered an error and needed to close</b>"</span>); <a name="l00170"></a>00170 dlg.setInformativeText( <a name="l00171"></a>00171 <span class="stringliteral">"<p>Vidalia attempted to automatically create an error report to "</span> <a name="l00172"></a>00172 <span class="stringliteral">"help diagnose the problem, but was unable to do so. Please report "</span> <a name="l00173"></a>00173 <span class="stringliteral">"this problem, along with what you were doing before Vidalia crashed, "</span> <a name="l00174"></a>00174 <span class="stringliteral">"to the developers at:</p><p>"</span> <a name="l00175"></a>00175 <span class="stringliteral">"<a href=\"https://trac.vidalia-project.net/wiki/ReportingBugs\">"</span> <a name="l00176"></a>00176 <span class="stringliteral">"https://trac.vidalia-project.net/wiki/ReportingBugs</a></p> "</span> <a name="l00177"></a>00177 <span class="stringliteral">"<p>Click \"Show Details\" below for information about the problem "</span> <a name="l00178"></a>00178 <span class="stringliteral">"encountered."</span>); <a name="l00179"></a>00179 <a name="l00180"></a>00180 dlg.setDetailedText(errorMessage); <a name="l00181"></a>00181 <span class="keywordflow">return</span> dlg.exec(); <a name="l00182"></a>00182 } </pre></div></div> <hr class="footer"/><address style="text-align: right;"><small>Generated on Mon Aug 30 22:58:54 2010 for Vidalia by <a href="http://www.doxygen.org/index.html"> <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.6.3 </small></address> </body> </html>