1 // main.cpp - Main subroutine (application start).
3 // (c) 2012-2017 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 #if defined(__WIN32__)
43 #include "common/win32ssl.h"
46 class XestiaABApp: public wxApp
48 virtual bool OnInit();
51 IMPLEMENT_APP(XestiaABApp);
53 bool XestiaABApp::OnInit()
56 #if defined(__WIN32__)
58 PSecurityFunctionTableW SecurityFunctionTbl;
59 SecurityFunctionTbl = (*InitSecurityInterface)();
66 locale.Init(wxLANGUAGE_DEFAULT, wxLOCALE_LOAD_DEFAULT);
68 static const wxCmdLineEntryDesc g_cmdLineDesc [] =
70 { wxCMD_LINE_SWITCH, "h", "help", "Displays help on command line parameters",
71 wxCMD_LINE_VAL_NONE, wxCMD_LINE_OPTION_HELP },
72 { wxCMD_LINE_OPTION, "e", "edit", "Edit a vCard 4.0 formatted contact",
73 wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
74 { wxCMD_LINE_SWITCH, "c", "convert", "Convert a contact file into another format.",
75 wxCMD_LINE_VAL_NONE, wxCMD_LINE_PARAM_OPTIONAL },
76 { wxCMD_LINE_OPTION, "ifmt", NULL, "Input format to convert from. (used with -c)",
77 wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
78 { wxCMD_LINE_OPTION, "ofmt", NULL,"Output format to convert to. (used with -c)",
79 wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
80 { wxCMD_LINE_OPTION, "ifile", NULL, "Input filename to read from. (used with -c)",
81 wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
82 { wxCMD_LINE_OPTION, "ofile", NULL, "Output filename to write to (don't use to pipe to console). (used with -c)",
83 wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
84 { wxCMD_LINE_OPTION, "d", "display", "Display a contact in the contact information window.",
85 wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
86 { wxCMD_LINE_SWITCH, "s", "search", "Display the search window instead of starting normally",
87 wxCMD_LINE_VAL_NONE, wxCMD_LINE_PARAM_OPTIONAL },
88 { wxCMD_LINE_SWITCH, "v", "version", "Displays version number",
89 wxCMD_LINE_VAL_NONE, wxCMD_LINE_PARAM_OPTIONAL },
93 wxString wxSContactFilename;
94 wxString wxSContactOutputFilename;
95 wxString wxSContactFormat;
96 wxString wxSContactOutputFormat;
97 wxCmdLineParser XABArgs (g_cmdLineDesc, argc, argv);
100 if (XABArgs.Found(wxT("h"))){
102 // Print out the list of help commands.
108 if (XABArgs.Found(wxT("v"))){
110 // Print out the version number.
112 wxPuts(XSDAB_VERSION);
117 wxInitAllImageHandlers();
119 if (XABArgs.Found(wxT("c"))){
121 // Preform a conversion.
123 wxString InputFormat;
124 wxString ExportFormat;
125 wxString InputFilename;
126 wxString OutputFilename;
128 // Get the input format. Return error if blank
131 if (XABArgs.Found(wxT("ifmt"))){
132 XABArgs.Found(wxT("ifmt"), &InputFormat);
135 // Get the export format. Return error if blank
138 if (XABArgs.Found(wxT("ofmt"))){
139 XABArgs.Found(wxT("ofmt"), &ExportFormat);
142 // Get the input filename.
144 if (XABArgs.Found(wxT("ifile"))){
145 XABArgs.Found(wxT("ifile"), &InputFilename);
148 // Get the output filename (if any).
150 if (XABArgs.Found(wxT("ofile"))){
151 XABArgs.Found(wxT("ofile"), &OutputFilename);
155 wxPuts(_("Too many arguments given."));
159 // Run the conversion process.
161 ConvertResult ConvertRunStatus = ConvertContact(InputFormat, ExportFormat, InputFilename, OutputFilename);
163 switch (ConvertRunStatus){
164 case CONVERTRESULT_UNITTESTFAIL:
165 wxPuts(_("An internal unit testing failure has occured."));
168 case CONVERTRESULT_OK:
170 case CONVERTRESULT_FORMATSSAME:
171 wxPuts(_("Both input and output formats are the same."));
174 case CONVERTRESULT_INVALIDINPUTFORMAT:
175 wxPuts(_("Invalid input format given."));
178 case CONVERTRESULT_INVALIDOUTPUTFORMAT:
179 wxPuts(_("Invalid output format given."));
182 case CONVERTRESULT_INPUTFILEMISSING:
183 wxPuts(_("Input file with the filename given does not exist."));
186 case CONVERTRESULT_INPUTFILEEMPTY:
187 wxPuts(_("No input filename given."));
190 case CONVERTRESULT_INPUTFILEINVALIDFORMAT:
191 wxPuts(_("Input file is in an invalid format."));
194 case CONVERTRESULT_INPUTFILEERROR:
195 wxPuts(_("An error occured whilst trying to open the input file location."));
198 case CONVERTRESULT_OUTPUTFILEERROR:
199 wxPuts(_("An error occured whilst trying to open the output file location."));
208 if (XABArgs.Found(wxT("s"))){
210 // Open up the search window.
212 frmSearch *frmSearchPtr = new frmSearch( NULL );
213 frmSearchPtr->Show(true);
214 frmSearchPtr->SetSearchMode(true);
219 if (XABArgs.Found(wxT("e"), &wxSContactFilename)){
221 // Check if the filename exists.
223 wxFileName contactfile(wxSContactFilename);
225 if (!contactfile.FileExists()){
227 wxMessageBox(_("The file with the filename given does not exist."), _("Error loading contact"), wxICON_ERROR);
232 // Check if file is in the user's Xestia Address Book data storage
233 // path. If it is, refuse to open it.
235 wxString UserDir = GetUserDir();
236 long UserDirLength = UserDir.Len();
238 if (UserDir == contactfile.GetFullPath().Mid(0, UserDirLength)){
240 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);
245 wxMemoryInputStream istream(bigimgs_contactpersonicon48_png, sizeof(bigimgs_contactpersonicon48_png));
246 wxImage bigimgs_contactpersonicon48i(istream, wxBITMAP_TYPE_PNG);
247 wxBitmap contacticonbmp(bigimgs_contactpersonicon48i, -1);
249 contacticon.CopyFromBitmap(contacticonbmp);
251 // Setup the contact editor with the filename given.
253 frmContactEditor *ContactEditor = new frmContactEditor( NULL );
254 ContactEditor->SetupHeaders();
255 ContactEditor->SetMode(TRUE);
256 ContactEditor->LoadContact(wxSContactFilename);
257 ContactEditor->SetIcon(contacticon);
258 ContactEditor->Show(true);
260 SetTopWindow(ContactEditor);
266 if (XABArgs.Found(wxT("d"), &wxSContactFilename)){
268 // Check if the filename exists.
270 wxFileName contactfile(wxSContactFilename);
272 if (!contactfile.FileExists()){
274 wxMessageBox(_("The file with the filename given does not exist."), _("Error loading contact"), wxICON_ERROR);
279 // Check if file is in the user's Xestia Address Book data storage
280 // path. If it is, refuse to open it.
282 wxString UserDir = GetUserDir();
283 long UserDirLength = UserDir.Len();
285 if (UserDir == contactfile.GetFullPath().Mid(0, UserDirLength)){
287 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);
292 wxMemoryInputStream istream(bigimgs_contactpersonicon48_png, sizeof(bigimgs_contactpersonicon48_png));
293 wxImage bigimgs_contactpersonicon48i(istream, wxBITMAP_TYPE_PNG);
294 wxBitmap contacticonbmp(bigimgs_contactpersonicon48i, -1);
296 contacticon.CopyFromBitmap(contacticonbmp);
298 // Setup the contact information icons for later.
300 wxFileSystem::AddHandler(new wxMemoryFSHandler);
303 wxMemoryInputStream ciptostream(icons_cipto_png, sizeof(icons_cipto_png));
304 ciicon_png.LoadFile(ciptostream, wxBITMAP_TYPE_PNG);
305 wxMemoryFSHandler::AddFile(wxT("cipto.png"), ciicon_png, wxBITMAP_TYPE_PNG);
307 wxMemoryInputStream cilogstream(icons_cilog_png, sizeof(icons_cilog_png));
308 ciicon_png.LoadFile(cilogstream, wxBITMAP_TYPE_PNG);
309 wxMemoryFSHandler::AddFile(wxT("cilog.png"), ciicon_png, wxBITMAP_TYPE_PNG);
311 wxMemoryInputStream cisndstream(icons_cisnd_png, sizeof(icons_cisnd_png));
312 ciicon_png.LoadFile(cisndstream, wxBITMAP_TYPE_PNG);
313 wxMemoryFSHandler::AddFile(wxT("cisnd.png"), ciicon_png, wxBITMAP_TYPE_PNG);
315 wxMemoryInputStream cikeystream(icons_cikey_png, sizeof(icons_cikey_png));
316 ciicon_png.LoadFile(cikeystream, wxBITMAP_TYPE_PNG);
317 wxMemoryFSHandler::AddFile(wxT("cikey.png"), ciicon_png, wxBITMAP_TYPE_PNG);
319 wxMemoryInputStream civenstream(icons_civen_png, sizeof(icons_civen_png));
320 ciicon_png.LoadFile(civenstream, wxBITMAP_TYPE_PNG);
321 wxMemoryFSHandler::AddFile(wxT("civen.png"), ciicon_png, wxBITMAP_TYPE_PNG);
323 wxMemoryInputStream ciextstream(icons_ciext_png, sizeof(icons_ciext_png));
324 ciicon_png.LoadFile(ciextstream, wxBITMAP_TYPE_PNG);
325 wxMemoryFSHandler::AddFile(wxT("ciext.png"), ciicon_png, wxBITMAP_TYPE_PNG);
327 // Setup the contact information window.
329 frmContact *ContactWindow = new frmContact( NULL );
331 std::map<wxString, wxString> MemoryFileSystem;
334 FileLoadData.LoadFile(wxSContactFilename);
335 ContactWindow->SetMode(true);
336 ContactWindow->SetUID(0);
337 ContactWindow->SetupPointers(&MemoryFileSystem);
338 ContactWindow->SetupContactData(&FileLoadData);
339 ContactWindow->Show(true);
341 SetTopWindow(ContactWindow);
347 // Setup default settings and accounts if they don't exist.
350 SetupDefaultSettings();
351 SetupDefaultAddressBook();
353 wxMemoryInputStream istream(bigimgs_appicon48_png, sizeof(bigimgs_appicon_png));
354 wxImage bigimgs_appicon48i(istream, wxBITMAP_TYPE_PNG);
355 wxBitmap appiconbmp(bigimgs_appicon48i, -1);
358 appicon.CopyFromBitmap(appiconbmp);
359 frmMain *frame = new frmMain( NULL );
361 frmActivityMgr *frameActMgr = new frmActivityMgr ( frame );
362 frame->SetupPointers(frameActMgr);
364 frame->SetIcon(appicon);