// main.cpp - Main subroutine (application start). // // (c) 2012-2017 Xestia Software Development. // // This file is part of Xestia Address Book. // // Xestia Address Book is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by the // Free Software Foundation, version 3 of the license. // // Xestia Address Book is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License along // with Xestia Address Book. If not, see #include #include #include #include #include #include #include #include #include "contacteditor/frmContactEditor.h" #include "frmMain.h" #include "frmContact.h" #include "vcard/vcard.h" #include "bitmaps.h" #include "version.h" #include "convert.h" #include "actmgr/frmActivityMgr.h" #include "search/frmSearch.h" #include "common/timers.h" #include "common/defaults.h" #include "common/dirs.h" #if defined(__WIN32__) #include "common/win32ssl.h" #endif class XestiaABApp: public wxApp { virtual bool OnInit(); }; IMPLEMENT_APP(XestiaABApp); bool XestiaABApp::OnInit() { #if defined(__WIN32__) PSecurityFunctionTableW SecurityFunctionTbl; SecurityFunctionTbl = (*InitSecurityInterface)(); #endif // Setup the locale. wxLocale locale; locale.Init(wxLANGUAGE_DEFAULT, wxLOCALE_LOAD_DEFAULT); static const wxCmdLineEntryDesc g_cmdLineDesc [] = { { wxCMD_LINE_SWITCH, "h", "help", "Displays help on command line parameters", wxCMD_LINE_VAL_NONE, wxCMD_LINE_OPTION_HELP }, { wxCMD_LINE_OPTION, "e", "edit", "Edit a vCard 4.0 formatted contact", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL }, { wxCMD_LINE_SWITCH, "c", "convert", "Convert a contact file into another format.", wxCMD_LINE_VAL_NONE, wxCMD_LINE_PARAM_OPTIONAL }, { wxCMD_LINE_OPTION, "ifmt", NULL, "Input format to convert from. (used with -c)", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL }, { wxCMD_LINE_OPTION, "ofmt", NULL,"Output format to convert to. (used with -c)", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL }, { wxCMD_LINE_OPTION, "ifile", NULL, "Input filename to read from. (used with -c)", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL }, { wxCMD_LINE_OPTION, "ofile", NULL, "Output filename to write to (don't use to pipe to console). (used with -c)", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL }, { wxCMD_LINE_OPTION, "d", "display", "Display a contact in the contact information window.", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL }, { wxCMD_LINE_SWITCH, "s", "search", "Display the search window instead of starting normally", wxCMD_LINE_VAL_NONE, wxCMD_LINE_PARAM_OPTIONAL }, { wxCMD_LINE_SWITCH, "v", "version", "Displays version number", wxCMD_LINE_VAL_NONE, wxCMD_LINE_PARAM_OPTIONAL }, { wxCMD_LINE_NONE } }; wxString wxSContactFilename; wxString wxSContactOutputFilename; wxString wxSContactFormat; wxString wxSContactOutputFormat; wxCmdLineParser XABArgs (g_cmdLineDesc, argc, argv); XABArgs.Parse(); if (XABArgs.Found(wxT("h"))){ // Print out the list of help commands. return false; } if (XABArgs.Found(wxT("v"))){ // Print out the version number. wxPuts(XSDAB_VERSION); return false; } wxInitAllImageHandlers(); if (XABArgs.Found(wxT("c"))){ // Preform a conversion. wxString InputFormat; wxString ExportFormat; wxString InputFilename; wxString OutputFilename; // Get the input format. Return error if blank // or invalid. if (XABArgs.Found(wxT("ifmt"))){ XABArgs.Found(wxT("ifmt"), &InputFormat); } // Get the export format. Return error if blank // or invalid. if (XABArgs.Found(wxT("ofmt"))){ XABArgs.Found(wxT("ofmt"), &ExportFormat); } // Get the input filename. if (XABArgs.Found(wxT("ifile"))){ XABArgs.Found(wxT("ifile"), &InputFilename); } // Get the output filename (if any). if (XABArgs.Found(wxT("ofile"))){ XABArgs.Found(wxT("ofile"), &OutputFilename); } if (argc >= 7){ wxPuts(_("Too many arguments given.")); exit(EXIT_FAILURE); } // Run the conversion process. ConvertResult ConvertRunStatus = ConvertContact(InputFormat, ExportFormat, InputFilename, OutputFilename); switch (ConvertRunStatus){ case CONVERTRESULT_UNITTESTFAIL: wxPuts(_("An internal unit testing failure has occured.")); exit(EXIT_SUCCESS); break; case CONVERTRESULT_OK: break; case CONVERTRESULT_FORMATSSAME: wxPuts(_("Both input and output formats are the same.")); exit(EXIT_FAILURE); break; case CONVERTRESULT_INVALIDINPUTFORMAT: wxPuts(_("Invalid input format given.")); exit(EXIT_FAILURE); break; case CONVERTRESULT_INVALIDOUTPUTFORMAT: wxPuts(_("Invalid output format given.")); exit(EXIT_FAILURE); break; case CONVERTRESULT_INPUTFILEMISSING: wxPuts(_("Input file with the filename given does not exist.")); exit(EXIT_FAILURE); break; case CONVERTRESULT_INPUTFILEEMPTY: wxPuts(_("No input filename given.")); exit(EXIT_FAILURE); break; case CONVERTRESULT_INPUTFILEINVALIDFORMAT: wxPuts(_("Input file is in an invalid format.")); exit(EXIT_FAILURE); break; case CONVERTRESULT_INPUTFILEERROR: wxPuts(_("An error occured whilst trying to open the input file location.")); exit(EXIT_FAILURE); break; case CONVERTRESULT_OUTPUTFILEERROR: wxPuts(_("An error occured whilst trying to open the output file location.")); exit(EXIT_FAILURE); break; } return true; } if (XABArgs.Found(wxT("s"))){ // Open up the search window. frmSearch *frmSearchPtr = new frmSearch( NULL ); frmSearchPtr->Show(true); frmSearchPtr->SetSearchMode(true); return true; } if (XABArgs.Found(wxT("e"), &wxSContactFilename)){ // Check if the filename exists. wxFileName contactfile(wxSContactFilename); if (!contactfile.FileExists()){ wxMessageBox(_("The file with the filename given does not exist."), _("Error loading contact"), wxICON_ERROR); return false; } // Check if file is in the user's Xestia Address Book data storage // path. If it is, refuse to open it. wxString UserDir = GetUserDir(); long UserDirLength = UserDir.Len(); if (UserDir == contactfile.GetFullPath().Mid(0, UserDirLength)){ wxMessageBox(_("The file with the filename given is in the directories that Xestia Address Book stores it's data and cannot be opened directly."), _("Error loading contact"), wxICON_ERROR); return false; } wxMemoryInputStream istream(bigimgs_contactpersonicon48_png, sizeof(bigimgs_contactpersonicon48_png)); wxImage bigimgs_contactpersonicon48i(istream, wxBITMAP_TYPE_PNG); wxBitmap contacticonbmp(bigimgs_contactpersonicon48i, -1); wxIcon contacticon; contacticon.CopyFromBitmap(contacticonbmp); // Setup the contact editor with the filename given. frmContactEditor *ContactEditor = new frmContactEditor( NULL ); ContactEditor->SetupHeaders(); ContactEditor->SetMode(TRUE); ContactEditor->LoadContact(wxSContactFilename); ContactEditor->SetIcon(contacticon); ContactEditor->Show(true); SetTopWindow(ContactEditor); return true; } if (XABArgs.Found(wxT("d"), &wxSContactFilename)){ // Check if the filename exists. wxFileName contactfile(wxSContactFilename); if (!contactfile.FileExists()){ wxMessageBox(_("The file with the filename given does not exist."), _("Error loading contact"), wxICON_ERROR); return false; } // Check if file is in the user's Xestia Address Book data storage // path. If it is, refuse to open it. wxString UserDir = GetUserDir(); long UserDirLength = UserDir.Len(); if (UserDir == contactfile.GetFullPath().Mid(0, UserDirLength)){ wxMessageBox(_("The file with the filename given is in the directories that Xestia Address Book stores it's data and cannot be opened directly."), _("Error loading contact"), wxICON_ERROR); return false; } wxMemoryInputStream istream(bigimgs_contactpersonicon48_png, sizeof(bigimgs_contactpersonicon48_png)); wxImage bigimgs_contactpersonicon48i(istream, wxBITMAP_TYPE_PNG); wxBitmap contacticonbmp(bigimgs_contactpersonicon48i, -1); wxIcon contacticon; contacticon.CopyFromBitmap(contacticonbmp); // Setup the contact information icons for later. wxFileSystem::AddHandler(new wxMemoryFSHandler); wxImage ciicon_png; wxMemoryInputStream ciptostream(icons_cipto_png, sizeof(icons_cipto_png)); ciicon_png.LoadFile(ciptostream, wxBITMAP_TYPE_PNG); wxMemoryFSHandler::AddFile(wxT("cipto.png"), ciicon_png, wxBITMAP_TYPE_PNG); wxMemoryInputStream cilogstream(icons_cilog_png, sizeof(icons_cilog_png)); ciicon_png.LoadFile(cilogstream, wxBITMAP_TYPE_PNG); wxMemoryFSHandler::AddFile(wxT("cilog.png"), ciicon_png, wxBITMAP_TYPE_PNG); wxMemoryInputStream cisndstream(icons_cisnd_png, sizeof(icons_cisnd_png)); ciicon_png.LoadFile(cisndstream, wxBITMAP_TYPE_PNG); wxMemoryFSHandler::AddFile(wxT("cisnd.png"), ciicon_png, wxBITMAP_TYPE_PNG); wxMemoryInputStream cikeystream(icons_cikey_png, sizeof(icons_cikey_png)); ciicon_png.LoadFile(cikeystream, wxBITMAP_TYPE_PNG); wxMemoryFSHandler::AddFile(wxT("cikey.png"), ciicon_png, wxBITMAP_TYPE_PNG); wxMemoryInputStream civenstream(icons_civen_png, sizeof(icons_civen_png)); ciicon_png.LoadFile(civenstream, wxBITMAP_TYPE_PNG); wxMemoryFSHandler::AddFile(wxT("civen.png"), ciicon_png, wxBITMAP_TYPE_PNG); wxMemoryInputStream ciextstream(icons_ciext_png, sizeof(icons_ciext_png)); ciicon_png.LoadFile(ciextstream, wxBITMAP_TYPE_PNG); wxMemoryFSHandler::AddFile(wxT("ciext.png"), ciicon_png, wxBITMAP_TYPE_PNG); // Setup the contact information window. frmContact *ContactWindow = new frmContact( NULL ); std::map MemoryFileSystem; vCard FileLoadData; FileLoadData.LoadFile(wxSContactFilename); ContactWindow->SetMode(true); ContactWindow->SetUID(0); ContactWindow->SetupPointers(&MemoryFileSystem); ContactWindow->SetupContactData(&FileLoadData); ContactWindow->Show(true); SetTopWindow(ContactWindow); return true; } // Setup default settings and accounts if they don't exist. SetupDirectories(); SetupDefaultSettings(); SetupDefaultAddressBook(); wxMemoryInputStream istream(bigimgs_appicon48_png, sizeof(bigimgs_appicon_png)); wxImage bigimgs_appicon48i(istream, wxBITMAP_TYPE_PNG); wxBitmap appiconbmp(bigimgs_appicon48i, -1); wxIcon appicon; appicon.CopyFromBitmap(appiconbmp); frmMain *frame = new frmMain( NULL ); frame->Show(true); frmActivityMgr *frameActMgr = new frmActivityMgr ( frame ); frame->SetupPointers(frameActMgr); frame->SetupForm(); frame->SetIcon(appicon); SetTopWindow(frame); return true; }