1 // main.cpp - Main subroutine (application start).
3 // (c) 2012-2015 Xestia Software Development.
5 // This file is part of Xestia Address Book.
7 // Xestia Address Book is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by the
9 // Free Software Foundation, version 3 of the license.
11 // Xestia Address Book is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with Xestia Address Book. If not, see <http://www.gnu.org/licenses/>
23 #include <wx/mstream.h>
24 #include <wx/cmdline.h>
25 #include <wx/wxprec.h>
26 #include <wx/filename.h>
27 #include <curl/curl.h>
29 #include "contacteditor/frmContactEditor.h"
31 #include "frmContact.h"
32 #include "vcard/vcard.h"
36 #include "actmgr/frmActivityMgr.h"
37 #include "search/frmSearch.h"
38 #include "common/timers.h"
39 #include "common/defaults.h"
40 #include "common/dirs.h"
42 class XestiaABApp: public wxApp
44 virtual bool OnInit();
47 IMPLEMENT_APP(XestiaABApp);
49 bool XestiaABApp::OnInit()
55 locale.Init(wxLANGUAGE_DEFAULT, wxLOCALE_LOAD_DEFAULT);
57 /*#if defined(__WIN32__)
59 // Check that the minimum version of Xestia Common Components is installed on the system.
61 #include <xestiaccdll.h>
63 if (!CheckXCCVersion(1, 0, 0)){
64 MessageBox(0, L"The version of Xestia Common Components installed is an older version not supported by this version of Xestia Address Book.\n\nPlease visit http://xestia.co.uk/commoncomponents and follow the page instructions to download the version required.", L"Older version of Xestia Common Components installed", MB_OK|MB_ICONSTOP);
70 static const wxCmdLineEntryDesc g_cmdLineDesc [] =
72 { wxCMD_LINE_SWITCH, wxT_2("h"), wxT_2("help"), wxTRANSLATE(_("Displays help on command line parameters")),
73 wxCMD_LINE_VAL_NONE, wxCMD_LINE_OPTION_HELP },
74 { wxCMD_LINE_OPTION, wxT_2("e"), wxT_2("edit"), wxTRANSLATE(_("Edit a vCard 4.0 formatted contact")),
75 wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
76 { wxCMD_LINE_SWITCH, wxT_2("c"), wxT_2("convert"), wxTRANSLATE(_("Convert a contact file into another format.")),
77 wxCMD_LINE_VAL_NONE, wxCMD_LINE_PARAM_OPTIONAL },
78 { wxCMD_LINE_OPTION, wxT_2("ifmt"), NULL, wxTRANSLATE(_("Input format to convert from. (used with -c)")),
79 wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
80 { wxCMD_LINE_OPTION, wxT_2("ofmt"), NULL, wxTRANSLATE(_("Output format to convert to. (used with -c)")),
81 wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
82 { wxCMD_LINE_OPTION, wxT_2("ifile"), NULL, wxTRANSLATE(_("Input filename to read from. (used with -c)")),
83 wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
84 { wxCMD_LINE_OPTION, wxT_2("ofile"), NULL, wxTRANSLATE(_("Output filename to write to (don't use to pipe to console). (used with -c)")),
85 wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
86 { wxCMD_LINE_OPTION, wxT_2("d"), wxT_2("display"), wxTRANSLATE(_("Display a contact in the contact information window.")),
87 wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
88 { wxCMD_LINE_SWITCH, wxT_2("s"), wxT_2("search"), wxTRANSLATE(_("Display the search window instead of starting normally")),
89 wxCMD_LINE_VAL_NONE, wxCMD_LINE_PARAM_OPTIONAL },
90 { wxCMD_LINE_SWITCH, wxT_2("v"), wxT_2("version"), wxTRANSLATE(_("Displays version number")),
91 wxCMD_LINE_VAL_NONE, wxCMD_LINE_PARAM_OPTIONAL },
95 wxString wxSContactFilename;
96 wxString wxSContactOutputFilename;
97 wxString wxSContactFormat;
98 wxString wxSContactOutputFormat;
99 wxCmdLineParser XABArgs (g_cmdLineDesc, argc, argv);
102 if (XABArgs.Found(wxT("h"))){
104 // Print out the list of help commands.
110 if (XABArgs.Found(wxT("v"))){
112 // Print out the version number.
114 wxPuts(XSDAB_VERSION);
119 wxInitAllImageHandlers();
121 if (XABArgs.Found(wxT("c"))){
123 // Preform a conversion.
125 wxString InputFormat;
126 wxString ExportFormat;
127 wxString InputFilename;
128 wxString OutputFilename;
130 // Get the input format. Return error if blank
133 if (XABArgs.Found(wxT("ifmt"))){
134 XABArgs.Found(wxT("ifmt"), &InputFormat);
137 // Get the export format. Return error if blank
140 if (XABArgs.Found(wxT("ofmt"))){
141 XABArgs.Found(wxT("ofmt"), &ExportFormat);
144 // Get the input filename.
146 if (XABArgs.Found(wxT("ifile"))){
147 XABArgs.Found(wxT("ifile"), &InputFilename);
150 // Get the output filename (if any).
152 if (XABArgs.Found(wxT("ofile"))){
153 XABArgs.Found(wxT("ofile"), &OutputFilename);
157 wxPuts(_("Too many arguments given."));
161 // Run the conversion process.
163 ConvertResult ConvertRunStatus = ConvertContact(InputFormat, ExportFormat, InputFilename, OutputFilename);
165 switch (ConvertRunStatus){
166 case CONVERTRESULT_UNITTESTFAIL:
167 wxPuts(_("An internal unit testing failure has occured."));
170 case CONVERTRESULT_OK:
172 case CONVERTRESULT_FORMATSSAME:
173 wxPuts(_("Both input and output formats are the same."));
176 case CONVERTRESULT_INVALIDINPUTFORMAT:
177 wxPuts(_("Invalid input format given."));
180 case CONVERTRESULT_INVALIDOUTPUTFORMAT:
181 wxPuts(_("Invalid output format given."));
184 case CONVERTRESULT_INPUTFILEMISSING:
185 wxPuts(_("Input file with the filename given does not exist."));
188 case CONVERTRESULT_INPUTFILEEMPTY:
189 wxPuts(_("No input filename given."));
192 case CONVERTRESULT_INPUTFILEINVALIDFORMAT:
193 wxPuts(_("Input file is in an invalid format."));
196 case CONVERTRESULT_INPUTFILEERROR:
197 wxPuts(_("An error occured whilst trying to open the input file location."));
200 case CONVERTRESULT_OUTPUTFILEERROR:
201 wxPuts(_("An error occured whilst trying to open the output file location."));
210 if (XABArgs.Found(wxT("s"))){
212 // Open up the search window.
214 frmSearch *frmSearchPtr = new frmSearch( NULL );
215 frmSearchPtr->Show(true);
216 frmSearchPtr->SetSearchMode(true);
221 if (XABArgs.Found(wxT("e"), &wxSContactFilename)){
223 // Check if the filename exists.
225 wxFileName contactfile(wxSContactFilename);
227 if (!contactfile.FileExists()){
229 wxMessageBox(_("The file with the filename given does not exist."), _("Error loading contact"), wxICON_ERROR);
234 // Check if file is in the user's Xestia Address Book data storage
235 // path. If it is, refuse to open it.
237 wxString UserDir = GetUserDir();
238 long UserDirLength = UserDir.Len();
240 if (UserDir == contactfile.GetFullPath().Mid(0, UserDirLength)){
242 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);
247 wxMemoryInputStream istream(bigimgs_contactpersonicon48_png, sizeof(bigimgs_contactpersonicon48_png));
248 wxImage bigimgs_contactpersonicon48i(istream, wxBITMAP_TYPE_PNG);
249 wxBitmap contacticonbmp(bigimgs_contactpersonicon48i, -1);
251 contacticon.CopyFromBitmap(contacticonbmp);
253 // Setup the contact editor with the filename given.
255 frmContactEditor *ContactEditor = new frmContactEditor( NULL );
256 ContactEditor->SetupHeaders();
257 ContactEditor->SetMode(TRUE);
258 ContactEditor->LoadContact(wxSContactFilename);
259 ContactEditor->SetIcon(contacticon);
260 ContactEditor->Show(true);
262 SetTopWindow(ContactEditor);
268 if (XABArgs.Found(wxT("d"), &wxSContactFilename)){
270 // Check if the filename exists.
272 wxFileName contactfile(wxSContactFilename);
274 if (!contactfile.FileExists()){
276 wxMessageBox(_("The file with the filename given does not exist."), _("Error loading contact"), wxICON_ERROR);
281 // Check if file is in the user's Xestia Address Book data storage
282 // path. If it is, refuse to open it.
284 wxString UserDir = GetUserDir();
285 long UserDirLength = UserDir.Len();
287 if (UserDir == contactfile.GetFullPath().Mid(0, UserDirLength)){
289 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);
294 wxMemoryInputStream istream(bigimgs_contactpersonicon48_png, sizeof(bigimgs_contactpersonicon48_png));
295 wxImage bigimgs_contactpersonicon48i(istream, wxBITMAP_TYPE_PNG);
296 wxBitmap contacticonbmp(bigimgs_contactpersonicon48i, -1);
298 contacticon.CopyFromBitmap(contacticonbmp);
300 // Setup the contact information icons for later.
302 wxFileSystem::AddHandler(new wxMemoryFSHandler);
305 wxMemoryInputStream ciptostream(icons_cipto_png, sizeof(icons_cipto_png));
306 ciicon_png.LoadFile(ciptostream, wxBITMAP_TYPE_PNG);
307 wxMemoryFSHandler::AddFile(wxT("cipto.png"), ciicon_png, wxBITMAP_TYPE_PNG);
309 wxMemoryInputStream cilogstream(icons_cilog_png, sizeof(icons_cilog_png));
310 ciicon_png.LoadFile(cilogstream, wxBITMAP_TYPE_PNG);
311 wxMemoryFSHandler::AddFile(wxT("cilog.png"), ciicon_png, wxBITMAP_TYPE_PNG);
313 wxMemoryInputStream cisndstream(icons_cisnd_png, sizeof(icons_cisnd_png));
314 ciicon_png.LoadFile(cisndstream, wxBITMAP_TYPE_PNG);
315 wxMemoryFSHandler::AddFile(wxT("cisnd.png"), ciicon_png, wxBITMAP_TYPE_PNG);
317 wxMemoryInputStream cikeystream(icons_cikey_png, sizeof(icons_cikey_png));
318 ciicon_png.LoadFile(cikeystream, wxBITMAP_TYPE_PNG);
319 wxMemoryFSHandler::AddFile(wxT("cikey.png"), ciicon_png, wxBITMAP_TYPE_PNG);
321 wxMemoryInputStream civenstream(icons_civen_png, sizeof(icons_civen_png));
322 ciicon_png.LoadFile(civenstream, wxBITMAP_TYPE_PNG);
323 wxMemoryFSHandler::AddFile(wxT("civen.png"), ciicon_png, wxBITMAP_TYPE_PNG);
325 wxMemoryInputStream ciextstream(icons_ciext_png, sizeof(icons_ciext_png));
326 ciicon_png.LoadFile(ciextstream, wxBITMAP_TYPE_PNG);
327 wxMemoryFSHandler::AddFile(wxT("ciext.png"), ciicon_png, wxBITMAP_TYPE_PNG);
329 // Setup the contact information window.
331 frmContact *ContactWindow = new frmContact( NULL );
333 std::map<wxString, wxString> MemoryFileSystem;
336 FileLoadData.LoadFile(wxSContactFilename);
337 ContactWindow->SetMode(true);
338 ContactWindow->SetUID(0);
339 ContactWindow->SetupPointers(&MemoryFileSystem);
340 ContactWindow->SetupContactData(&FileLoadData);
341 ContactWindow->Show(true);
343 /*frmContactEditor *ContactEditor = new frmContactEditor( NULL );
344 ContactEditor->SetupHeaders();
345 ContactEditor->SetMode(TRUE);
346 ContactEditor->LoadContact(wxSContactFilename);
347 ContactEditor->SetIcon(contacticon);
348 ContactEditor->Show(true);*/
350 SetTopWindow(ContactWindow);
356 /*if (!XABArgs.Found(wxT(""))){
362 // Setup default settings and accounts if they don't exist.
365 SetupDefaultSettings();
366 SetupDefaultAddressBook();
368 wxMemoryInputStream istream(bigimgs_appicon48_png, sizeof(bigimgs_appicon_png));
369 wxImage bigimgs_appicon48i(istream, wxBITMAP_TYPE_PNG);
370 wxBitmap appiconbmp(bigimgs_appicon48i, -1);
373 appicon.CopyFromBitmap(appiconbmp);
374 frmMain *frame = new frmMain( NULL );
376 frmActivityMgr *frameActMgr = new frmActivityMgr ( frame );
377 frame->SetupPointers(frameActMgr);
379 frame->SetIcon(appicon);