/////////////////////////////////////////////////////////////////////////////
// Name:        app.cpp
// Purpose:     wxWidgets Packager
// Author:      Julian Smart, Francesco Montorsi
// Modified by:
// Created:     2004-11-08
// RCS-ID:      $Id: app.cpp,v 1.4 2007/01/23 08:45:57 frm Exp $
// Copyright:   (c) Julian Smart
// Licence:     wxWidgets license
/////////////////////////////////////////////////////////////////////////////


// For compilers that support precompilation, includes "wx.h".
#include "wx/wxprec.h"

#ifdef __BORLANDC__
#pragma hdrstop
#endif

#include <wx/artprov.h>
#include <wx/cmdline.h>
#include <wx/fs_mem.h>
#include <wx/fs_zip.h>
#include <wx/tokenzr.h>
#include <wx/sysopt.h>
#include <wx/help.h>
#include <wx/cshelp.h>
#include <wx/image.h>
#include <wx/config.h>
#include <wx/stdpaths.h>

#include "wxp/wxp.h"
#include "wx/md5.h"

// wxP own headers
#include "symbols.h"
#include "mainfrm.h"
#include "app.h"



// ----------------------------------------------------------------------------
// wxPackager constants
// ----------------------------------------------------------------------------

#define wxPAPP_HELP_SWITCH              wxT("h")
#define wxPAPP_VERSION_SWITCH           wxT("v")

static const wxCmdLineEntryDesc g_cmdLineDesc[] =
{
    { wxCMD_LINE_SWITCH, wxPAPP_HELP_SWITCH, wxT("help"),
      wxT("displays help on the command line parameters") },

    { wxCMD_LINE_SWITCH, wxPAPP_VERSION_SWITCH, wxT("version"), wxT("prints version") },

    { wxCMD_LINE_PARAM, wxT(""), wxT(""), wxT("WXP file to load"), wxCMD_LINE_VAL_STRING,
      wxCMD_LINE_PARAM_OPTIONAL },

    { wxCMD_LINE_NONE }
};



// ----------------------------------------------------------------------------
// wxPackagerApp
// ----------------------------------------------------------------------------

IMPLEMENT_APP(wxPackagerApp)

wxPackagerApp::wxPackagerApp()
{
    // this is not our real name; see cmnsymbols.h for more info
    SetAppName(APPNAME);
}

wxPackagerApp::~wxPackagerApp()
{
}

bool wxPackagerApp::OnInit()
{
    wxLog::SetTimestamp(NULL);

    wxPlatformInfo::Get();

    // VERY IMPORTANT: do not change our working directory otherwise
    //                 we won't be able to load packages with relative paths
    //                 given to arguments of this program
    // wxSetWorkingDirectory(wxStandardPaths::Get().GetDataDir());

    // Parse command line
    wxString cmdFilename;
    wxCmdLineParser cmdParser(g_cmdLineDesc, argc, argv);
    cmdParser.SetLogo(wxT("wxWidgets Packager, ") COPYRIGHT);
    if (cmdParser.Parse() != 0)
        return false;

    if (cmdParser.Found(wxPAPP_HELP_SWITCH))
    {
        cmdParser.Usage();
        return false;
    }

    if (cmdParser.Found(wxPAPP_VERSION_SWITCH))
    {
        wxString msg;
        wxString date(wxString::FromAscii(__DATE__));
        msg.Printf(wxT("Version %.1f, %s"),
                   VERSION_NUMBER, date.c_str());
        wxLogMessage(msg);
        return false;
    }

    // init miscellaneous stuff
    wxHelpProvider::Set(new wxSimpleHelpProvider);
    wxInitAllImageHandlers();

    // load presets
    wxString path = wxStandardPaths::Get().GetDataDir() +
                    wxFileName::GetPathSeparator() + 
                    wxT("xml") + 
                    wxFileName::GetPathSeparator() + 
                    DEFAULT_PRESETS;
    if (!wxPresetXMLDescriptor::Load(path))
    {
        wxLogError(_("Could not load the presets file '%s'; it's not a valid preset file."),
                   path.c_str());

        // do not proceed as without presets the wxPM is quite useless
        wxLogError(_("Cannot proceed without presets. Aborting."));
        return false;
    }

    // init config object
    SetGlobalConfig();

    // create the frame
    wxPackagerFrame *fr = new wxPackagerFrame();
    if (!fr->Create())
    {
        wxLogError(_("Cannot initialize the packager frame. Aborting."));
        return false;
    }

    SetTopWindow(fr);

    // load the WXP given at command-line, if any
    if (cmdParser.GetParamCount() > 0)
        GetMainFrame()->Open(cmdParser.GetParam(0));

    GetTopWindow()->Show(true);
    return true;
}

int wxPackagerApp::OnExit(void)
{
    delete wxConfigBase::Set(NULL);
    return 0;
}

wxPackagerFrame* wxPackagerApp::GetMainFrame()
{
    return wxDynamicCast(GetTopWindow(), wxPackagerFrame);
}


