///////////////////////////////////////////////////////////////////////////////
// Name:        src/generic/dbgrptg.cpp
// Purpose:     implementation of wxDebugReportPreviewStd
// Author:      Vadim Zeitlin, Andrej Putrin
// Modified by:
// Created:     2005-01-21
// Copyright:   (c) 2005 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
// Licence:     wxWindows licence
///////////////////////////////////////////////////////////////////////////////

// ============================================================================
// declarations
// ============================================================================

// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------

#include "wx/wxprec.h"


#if wxUSE_DEBUGREPORT && wxUSE_XML

#include "wx/debugrpt.h"

#ifndef WX_PRECOMP
    #include "wx/sizer.h"
    #include "wx/checklst.h"
    #include "wx/textctrl.h"
    #include "wx/intl.h"
    #include "wx/stattext.h"
    #include "wx/filedlg.h"
    #include "wx/valtext.h"
    #include "wx/button.h"
#endif // WX_PRECOMP

#include "wx/filename.h"
#ifdef wxUSE_FFILE
    #include "wx/ffile.h"
#else
    #include "wx/file.h"
#endif
#include "wx/mimetype.h"

#include "wx/statline.h"

#ifdef __WXMSW__
    #include "wx/evtloop.h"     // for SetCriticalWindow()
    #include "wx/scopeguard.h"
#endif // __WXMSW__

// ----------------------------------------------------------------------------
// wxDumpPreviewDlg: simple class for showing ASCII preview of dump files
// ----------------------------------------------------------------------------

class wxDumpPreviewDlg : public wxDialog
{
public:
    wxDumpPreviewDlg(wxWindow *parent,
                     const wxString& title,
                     const wxString& text);

private:
    // the text we show
    wxTextCtrl *m_text;

    wxDECLARE_NO_COPY_CLASS(wxDumpPreviewDlg);
};

wxDumpPreviewDlg::wxDumpPreviewDlg(wxWindow *parent,
                                   const wxString& title,
                                   const wxString& text)
                : wxDialog(parent, wxID_ANY, title,
                           wxDefaultPosition, wxDefaultSize,
                           wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
{
    // create controls
    // ---------------

    // use wxTE_RICH2 style to avoid 64kB limit under MSW and display big files
    // faster than with wxTE_RICH
    m_text = new wxTextCtrl(this, wxID_ANY, wxEmptyString,
                            wxPoint(0, 0), wxDefaultSize,
                            wxTE_MULTILINE |
                            wxTE_READONLY |
                            wxTE_NOHIDESEL |
                            wxTE_RICH2);
    m_text->SetValue(text);

    // use fixed-width font
    m_text->SetFont(wxFont(12, wxFONTFAMILY_TELETYPE,
                           wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL));

    wxButton *btnClose = new wxButton(this, wxID_CANCEL, _("Close"));


    // layout them
    // -----------

    wxSizer *sizerTop = new wxBoxSizer(wxVERTICAL),
            *sizerBtns = new wxBoxSizer(wxHORIZONTAL);

    sizerBtns->Add(btnClose, 0, 0, 1);

    sizerTop->Add(m_text, 1, wxEXPAND);
    sizerTop->Add(sizerBtns, 0, wxALIGN_RIGHT | wxTOP | wxBOTTOM | wxRIGHT, 1);

    // set the sizer &c
    // ----------------

    // make the text window bigger to show more contents of the file
    sizerTop->SetItemMinSize(m_text, 600, 300);
    SetSizer(sizerTop);

    Layout();
    Fit();

    m_text->SetFocus();
}

// ----------------------------------------------------------------------------
// wxDumpOpenExternalDlg: choose a command for opening the given file
// ----------------------------------------------------------------------------

class wxDumpOpenExternalDlg : public wxDialog
{
public:
    wxDumpOpenExternalDlg(wxWindow *parent, const wxFileName& filename);

    // return the command chosed by user to open this file
    const wxString& GetCommand() const { return m_command; }

    wxString m_command;

private:

#if wxUSE_FILEDLG
    void OnBrowse(wxCommandEvent& event);
#endif // wxUSE_FILEDLG

    wxDECLARE_EVENT_TABLE();
    wxDECLARE_NO_COPY_CLASS(wxDumpOpenExternalDlg);
};

wxBEGIN_EVENT_TABLE(wxDumpOpenExternalDlg, wxDialog)

#if wxUSE_FILEDLG
    EVT_BUTTON(wxID_MORE, wxDumpOpenExternalDlg::OnBrowse)
#endif

wxEND_EVENT_TABLE()


wxDumpOpenExternalDlg::wxDumpOpenExternalDlg(wxWindow *parent,
                                             const wxFileName& filename)
                     : wxDialog(parent,
                                wxID_ANY,
                                wxString::Format
                                (
                                    _("Open file \"%s\""),
                                    filename.GetFullPath().c_str()
                                ))
{
    // create controls
    // ---------------

    wxSizer *sizerTop = new wxBoxSizer(wxVERTICAL);
    sizerTop->Add(new wxStaticText(this, wxID_ANY,
                                   wxString::Format
                                   (
                                    _("Enter command to open file \"%s\":"),
                                    filename.GetFullName().c_str()
                                   )),
                  wxSizerFlags().Border());

    wxSizer *sizerH = new wxBoxSizer(wxHORIZONTAL);

    wxTextCtrl *command = new wxTextCtrl
                              (
                                this,
                                wxID_ANY,
                                wxEmptyString,
                                wxDefaultPosition,
                                wxSize(250, wxDefaultCoord),
                                0
#if wxUSE_VALIDATORS
                                ,wxTextValidator(wxFILTER_NONE, &m_command)
#endif
                              );
    sizerH->Add(command,
                    wxSizerFlags(1).Align(wxALIGN_CENTER_VERTICAL));

#if wxUSE_FILEDLG

    wxButton *browse = new wxButton(this, wxID_MORE, wxT(">>"),
                                    wxDefaultPosition, wxDefaultSize,
                                    wxBU_EXACTFIT);
    sizerH->Add(browse,
                wxSizerFlags(0).Align(wxALIGN_CENTER_VERTICAL). Border(wxLEFT));

#endif // wxUSE_FILEDLG

    sizerTop->Add(sizerH, wxSizerFlags(0).Expand().Border());

    sizerTop->Add(new wxStaticLine(this), wxSizerFlags().Expand().Border());

    sizerTop->Add(CreateStdDialogButtonSizer(wxOK | wxCANCEL),
                      wxSizerFlags().Align(wxALIGN_RIGHT).Border());

    // set the sizer &c
    // ----------------

    SetSizer(sizerTop);

    Layout();
    Fit();

    command->SetFocus();
}

#if wxUSE_FILEDLG

void wxDumpOpenExternalDlg::OnBrowse(wxCommandEvent& )
{
    wxFileName fname(m_command);
    wxFileDialog dlg(this,
                     wxFileSelectorPromptStr,
                     fname.GetPathWithSep(),
                     fname.GetFullName()
#ifdef __WXMSW__
                     , wxString(_("Executable files (*.exe)|*.exe|")) + wxALL_FILES
#endif // __WXMSW__
                     );
    if ( dlg.ShowModal() == wxID_OK )
    {
        m_command = dlg.GetPath();
        TransferDataToWindow();
    }
}

#endif // wxUSE_FILEDLG

// ----------------------------------------------------------------------------
// wxDebugReportDialog: class showing debug report to the user
// ----------------------------------------------------------------------------

class wxDebugReportDialog : public wxDialog
{
public:
    wxDebugReportDialog(wxDebugReport& dbgrpt);

    virtual bool TransferDataToWindow() wxOVERRIDE;
    virtual bool TransferDataFromWindow() wxOVERRIDE;

private:
    void OnView(wxCommandEvent& );
    void OnViewUpdate(wxUpdateUIEvent& );
    void OnOpen(wxCommandEvent& );


    // small helper: add wxEXPAND and wxALL flags
    static wxSizerFlags SizerFlags(int proportion)
    {
        return wxSizerFlags(proportion).Expand().Border();
    }


    wxDebugReport& m_dbgrpt;

#if wxUSE_CHECKLISTBOX
    wxCheckListBox *m_checklst;
#else
    wxListBox *m_checklst;
#endif
    wxTextCtrl *m_notes;

    wxArrayString m_files;

    wxDECLARE_EVENT_TABLE();
    wxDECLARE_NO_COPY_CLASS(wxDebugReportDialog);
};

// ============================================================================
// wxDebugReportDialog implementation
// ============================================================================

wxBEGIN_EVENT_TABLE(wxDebugReportDialog, wxDialog)
    EVT_BUTTON(wxID_VIEW_DETAILS, wxDebugReportDialog::OnView)
    EVT_UPDATE_UI(wxID_VIEW_DETAILS, wxDebugReportDialog::OnViewUpdate)
    EVT_BUTTON(wxID_OPEN, wxDebugReportDialog::OnOpen)
    EVT_UPDATE_UI(wxID_OPEN, wxDebugReportDialog::OnViewUpdate)
wxEND_EVENT_TABLE()


// ----------------------------------------------------------------------------
// construction
// ----------------------------------------------------------------------------

wxDebugReportDialog::wxDebugReportDialog(wxDebugReport& dbgrpt)
                   : wxDialog(NULL, wxID_ANY,
                              wxString::Format(_("Debug report \"%s\""),
                              dbgrpt.GetReportName().c_str()),
                              wxDefaultPosition,
                              wxDefaultSize,
                              wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER),
                     m_dbgrpt(dbgrpt)
{
    // upper part of the dialog: explanatory message
    wxString msg;

    // The temporary directory can be the short form on Windows;
    // normalize it for the benefit of users.
    wxFileName debugDirFilename(dbgrpt.GetSaveLocation());
    debugDirFilename.Normalize(wxPATH_NORM_LONG);
    wxString debugDir = debugDirFilename.GetFullPath();
    msg << (debugDirFilename.IsDir()
            ? _("A debug report has been generated in the directory\n")
            : _("The following debug report will be generated\n"))
        << wxT('\n')
        << wxT("             \"") << debugDir << wxT("\"\n")
        << wxT('\n')
        << _("The report contains the files listed below. If any of these files contain private information,\nplease uncheck them and they will be removed from the report.\n")
        << wxT('\n')
        << _("If you wish to suppress this debug report completely, please choose the \"Cancel\" button,\nbut be warned that it may hinder improving the program, so if\nat all possible please do continue with the report generation.\n")
        << wxT('\n')
        << _("              Thank you and we're sorry for the inconvenience!\n")
        << wxT("\n\n"); // just some white space to separate from other stuff

    const wxSizerFlags flagsFixed(SizerFlags(0));
    const wxSizerFlags flagsExpand(SizerFlags(1));
    const wxSizerFlags flagsExpand2(SizerFlags(2));

    wxSizer *sizerPreview =
        new wxStaticBoxSizer(wxVERTICAL, this, _("&Debug report preview:"));
    sizerPreview->Add(CreateTextSizer(msg), wxSizerFlags().Centre().Border());

    // ... and the list of files in this debug report with buttons to view them
    wxSizer *sizerFileBtns = new wxBoxSizer(wxVERTICAL);
    sizerFileBtns->AddStretchSpacer(1);
    sizerFileBtns->Add(new wxButton(this, wxID_VIEW_DETAILS, _("&View...")),
                        wxSizerFlags().Border(wxBOTTOM));
    sizerFileBtns->Add(new wxButton(this, wxID_OPEN, _("&Open...")),
                        wxSizerFlags().Border(wxTOP));
    sizerFileBtns->AddStretchSpacer(1);

#if wxUSE_CHECKLISTBOX
    m_checklst = new wxCheckListBox(this, wxID_ANY);
#else
    m_checklst = new wxListBox(this, wxID_ANY);
#endif

    wxSizer *sizerFiles = new wxBoxSizer(wxHORIZONTAL);
    sizerFiles->Add(m_checklst, flagsExpand);
    sizerFiles->Add(sizerFileBtns, flagsFixed);

    sizerPreview->Add(sizerFiles, flagsExpand2);


    // lower part of the dialog: notes field
    wxSizer *sizerNotes = new wxStaticBoxSizer(wxVERTICAL, this, _("&Notes:"));

    msg = _("If you have any additional information pertaining to this bug\nreport, please enter it here and it will be joined to it:");

    m_notes = new wxTextCtrl(this, wxID_ANY, wxEmptyString,
                             wxDefaultPosition, wxDefaultSize,
                             wxTE_MULTILINE);

    sizerNotes->Add(CreateTextSizer(msg), flagsFixed);
    sizerNotes->Add(m_notes, flagsExpand);


    wxSizer *sizerTop = new wxBoxSizer(wxVERTICAL);
    sizerTop->Add(sizerPreview, flagsExpand2);
    sizerTop->AddSpacer(5);
    sizerTop->Add(sizerNotes, flagsExpand);
    sizerTop->Add(CreateStdDialogButtonSizer(wxOK | wxCANCEL), flagsFixed);

    SetSizerAndFit(sizerTop);
    Layout();
    CentreOnScreen();
}

// ----------------------------------------------------------------------------
// data exchange
// ----------------------------------------------------------------------------

bool wxDebugReportDialog::TransferDataToWindow()
{
    // all files are included in the report by default
    const size_t count = m_dbgrpt.GetFilesCount();
    for ( size_t n = 0; n < count; n++ )
    {
        wxString name,
            desc;
        if ( m_dbgrpt.GetFile(n, &name, &desc) )
        {
            m_checklst->Append(name + wxT(" (") + desc + wxT(')'));
#if wxUSE_CHECKLISTBOX
            m_checklst->Check(n);
#endif

            m_files.Add(name);
        }
    }

    return true;
}

bool wxDebugReportDialog::TransferDataFromWindow()
{
#if wxUSE_CHECKLISTBOX
    // any unchecked files should be removed from the report
    const size_t count = m_checklst->GetCount();
    for ( size_t n = 0; n < count; n++ )
    {
        if ( !m_checklst->IsChecked(n) )
        {
            m_dbgrpt.RemoveFile(m_files[n]);
        }
    }
#endif

    // if the user entered any notes, add them to the report
    const wxString notes = m_notes->GetValue();
    if ( !notes.empty() )
    {
        // for now filename fixed, could make it configurable in the future...
        m_dbgrpt.AddText(wxT("notes.txt"), notes, wxT("user notes"));
    }

    return true;
}

// ----------------------------------------------------------------------------
// event handlers
// ----------------------------------------------------------------------------

void wxDebugReportDialog::OnView(wxCommandEvent& )
{
    const int sel = m_checklst->GetSelection();
    wxCHECK_RET( sel != wxNOT_FOUND, wxT("invalid selection in OnView()") );

    wxFileName fn(m_dbgrpt.GetDirectory(), m_files[sel]);
    wxString str;

    const wxString& fullPath = fn.GetFullPath();
#if wxUSE_FFILE
    wxFFile file(fullPath);
#elif wxUSE_FILE
    wxFile file(fullPath);
#endif
    if ( file.IsOpened() && file.ReadAll(&str) )
    {
        wxDumpPreviewDlg dlg(this, m_files[sel], str);
        dlg.ShowModal();
    }
}

void wxDebugReportDialog::OnOpen(wxCommandEvent& )
{
    const int sel = m_checklst->GetSelection();
    wxCHECK_RET( sel != wxNOT_FOUND, wxT("invalid selection in OnOpen()") );

    wxFileName fn(m_dbgrpt.GetDirectory(), m_files[sel]);

    // try to get the command to open this kind of files ourselves
    wxString command;
#if wxUSE_MIMETYPE
    wxFileType *
        ft = wxTheMimeTypesManager->GetFileTypeFromExtension(fn.GetExt());
    if ( ft )
    {
        command = ft->GetOpenCommand(fn.GetFullPath());
        delete ft;
    }
#endif // wxUSE_MIMETYPE

    // if we couldn't, ask the user
    if ( command.empty() )
    {
        wxDumpOpenExternalDlg dlg(this, fn);
        if ( dlg.ShowModal() == wxID_OK )
        {
            // get the command chosen by the user and append file name to it

            // if we don't have place marker for file name in the command...
            wxString cmd = dlg.GetCommand();
            if ( !cmd.empty() )
            {
#if wxUSE_MIMETYPE
                if ( cmd.find(wxT('%')) != wxString::npos )
                {
                    command = wxFileType::ExpandCommand(cmd, fn.GetFullPath());
                }
                else // no %s nor %1
#endif // wxUSE_MIMETYPE
                {
                    // append the file name to the end
                    command << cmd << wxT(" \"") << fn.GetFullPath() << wxT('"');
                }
            }
        }
    }

    if ( !command.empty() )
        ::wxExecute(command);
}

void wxDebugReportDialog::OnViewUpdate(wxUpdateUIEvent& event)
{
    int sel = m_checklst->GetSelection();
    if (sel >= 0)
    {
        wxFileName fn(m_dbgrpt.GetDirectory(), m_files[sel]);
        event.Enable(fn.FileExists());
    }
    else
        event.Enable(false);
}


// ============================================================================
// wxDebugReportPreviewStd implementation
// ============================================================================

bool wxDebugReportPreviewStd::Show(wxDebugReport& dbgrpt) const
{
    if ( !dbgrpt.GetFilesCount() )
        return false;

    wxDebugReportDialog dlg(dbgrpt);

#ifdef __WXMSW__
    // before entering the event loop (from ShowModal()), block the event
    // handling for all other windows as this could result in more crashes
    wxEventLoop::SetCriticalWindow(&dlg);

    wxON_BLOCK_EXIT1( wxEventLoop::SetCriticalWindow,
                        static_cast<wxWindow *>(NULL) );
#endif // __WXMSW__

    return dlg.ShowModal() == wxID_OK && dbgrpt.GetFilesCount() != 0;
}

#endif // wxUSE_DEBUGREPORT && wxUSE_XML
