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 static const wxCmdLineEntryDesc g_cmdLineDesc [] =
59 { wxCMD_LINE_SWITCH, wxT_2("h"), wxT_2("help"), wxTRANSLATE(_("Displays help on command line parameters")),
60 wxCMD_LINE_VAL_NONE, wxCMD_LINE_OPTION_HELP },
61 { wxCMD_LINE_OPTION, wxT_2("e"), wxT_2("edit"), wxTRANSLATE(_("Edit a vCard 4.0 formatted contact")),
62 wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
63 { wxCMD_LINE_SWITCH, wxT_2("c"), wxT_2("convert"), wxTRANSLATE(_("Convert a contact file into another format.")),
64 wxCMD_LINE_VAL_NONE, wxCMD_LINE_PARAM_OPTIONAL },
65 { wxCMD_LINE_OPTION, wxT_2("ifmt"), NULL, wxTRANSLATE(_("Input format to convert from. (used with -c)")),
66 wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
67 { wxCMD_LINE_OPTION, wxT_2("ofmt"), NULL, wxTRANSLATE(_("Output format to convert to. (used with -c)")),
68 wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
69 { wxCMD_LINE_OPTION, wxT_2("ifile"), NULL, wxTRANSLATE(_("Input filename to read from. (used with -c)")),
70 wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
71 { wxCMD_LINE_OPTION, wxT_2("ofile"), NULL, wxTRANSLATE(_("Output filename to write to (don't use to pipe to console). (used with -c)")),
72 wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
73 { wxCMD_LINE_OPTION, wxT_2("d"), wxT_2("display"), wxTRANSLATE(_("Display a contact in the contact information window.")),
74 wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
75 { wxCMD_LINE_SWITCH, wxT_2("s"), wxT_2("search"), wxTRANSLATE(_("Display the search window instead of starting normally")),
76 wxCMD_LINE_VAL_NONE, wxCMD_LINE_PARAM_OPTIONAL },
77 { wxCMD_LINE_SWITCH, wxT_2("v"), wxT_2("version"), wxTRANSLATE(_("Displays version number")),
78 wxCMD_LINE_VAL_NONE, wxCMD_LINE_PARAM_OPTIONAL },
82 wxString wxSContactFilename;
83 wxString wxSContactOutputFilename;
84 wxString wxSContactFormat;
85 wxString wxSContactOutputFormat;
86 wxCmdLineParser XABArgs (g_cmdLineDesc, argc, argv);
89 if (XABArgs.Found(wxT("h"))){
91 // Print out the list of help commands.
97 if (XABArgs.Found(wxT("v"))){
99 // Print out the version number.
101 wxPuts(XSDAB_VERSION);
106 wxInitAllImageHandlers();
108 if (XABArgs.Found(wxT("c"))){
110 // Preform a conversion.
112 wxString InputFormat;
113 wxString ExportFormat;
114 wxString InputFilename;
115 wxString OutputFilename;
117 // Get the input format. Return error if blank
120 if (XABArgs.Found(wxT("ifmt"))){
121 XABArgs.Found(wxT("ifmt"), &InputFormat);
124 // Get the export format. Return error if blank
127 if (XABArgs.Found(wxT("ofmt"))){
128 XABArgs.Found(wxT("ofmt"), &ExportFormat);
131 // Get the input filename.
133 if (XABArgs.Found(wxT("ifile"))){
134 XABArgs.Found(wxT("ifile"), &InputFilename);
137 // Get the output filename (if any).
139 if (XABArgs.Found(wxT("ofile"))){
140 XABArgs.Found(wxT("ofile"), &OutputFilename);
144 wxPuts(_("Too many arguments given."));
148 // Run the conversion process.
150 ConvertResult ConvertRunStatus = ConvertContact(InputFormat, ExportFormat, InputFilename, OutputFilename);
152 switch (ConvertRunStatus){
153 case CONVERTRESULT_UNITTESTFAIL:
154 wxPuts(_("An internal unit testing failure has occured."));
157 case CONVERTRESULT_OK:
159 case CONVERTRESULT_FORMATSSAME:
160 wxPuts(_("Both input and output formats are the same."));
163 case CONVERTRESULT_INVALIDINPUTFORMAT:
164 wxPuts(_("Invalid input format given."));
167 case CONVERTRESULT_INVALIDOUTPUTFORMAT:
168 wxPuts(_("Invalid output format given."));
171 case CONVERTRESULT_INPUTFILEMISSING:
172 wxPuts(_("Input file with the filename given does not exist."));
175 case CONVERTRESULT_INPUTFILEEMPTY:
176 wxPuts(_("No input filename given."));
179 case CONVERTRESULT_INPUTFILEINVALIDFORMAT:
180 wxPuts(_("Input file is in an invalid format."));
183 case CONVERTRESULT_INPUTFILEERROR:
184 wxPuts(_("An error occured whilst trying to open the input file location."));
187 case CONVERTRESULT_OUTPUTFILEERROR:
188 wxPuts(_("An error occured whilst trying to open the output file location."));
197 if (XABArgs.Found(wxT("s"))){
199 // Open up the search window.
201 frmSearch *frmSearchPtr = new frmSearch( NULL );
202 frmSearchPtr->Show(true);
203 frmSearchPtr->SetSearchMode(true);
208 if (XABArgs.Found(wxT("e"), &wxSContactFilename)){
210 // Check if the filename exists.
212 wxFileName contactfile(wxSContactFilename);
214 if (!contactfile.FileExists()){
216 wxMessageBox(_("The file with the filename given does not exist."), _("Error loading contact"), wxICON_ERROR);
221 // Check if file is in the user's Xestia Address Book data storage
222 // path. If it is, refuse to open it.
224 wxString UserDir = GetUserDir();
225 long UserDirLength = UserDir.Len();
227 if (UserDir == contactfile.GetFullPath().Mid(0, UserDirLength)){
229 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);
234 wxMemoryInputStream istream(bigimgs_contactpersonicon48_png, sizeof(bigimgs_contactpersonicon48_png));
235 wxImage bigimgs_contactpersonicon48i(istream, wxBITMAP_TYPE_PNG);
236 wxBitmap contacticonbmp(bigimgs_contactpersonicon48i, -1);
238 contacticon.CopyFromBitmap(contacticonbmp);
240 // Setup the contact editor with the filename given.
242 frmContactEditor *ContactEditor = new frmContactEditor( NULL );
243 ContactEditor->SetupHeaders();
244 ContactEditor->SetMode(TRUE);
245 ContactEditor->LoadContact(wxSContactFilename);
246 ContactEditor->SetIcon(contacticon);
247 ContactEditor->Show(true);
249 SetTopWindow(ContactEditor);
255 if (XABArgs.Found(wxT("d"), &wxSContactFilename)){
257 // Check if the filename exists.
259 wxFileName contactfile(wxSContactFilename);
261 if (!contactfile.FileExists()){
263 wxMessageBox(_("The file with the filename given does not exist."), _("Error loading contact"), wxICON_ERROR);
268 // Check if file is in the user's Xestia Address Book data storage
269 // path. If it is, refuse to open it.
271 wxString UserDir = GetUserDir();
272 long UserDirLength = UserDir.Len();
274 if (UserDir == contactfile.GetFullPath().Mid(0, UserDirLength)){
276 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);
281 wxMemoryInputStream istream(bigimgs_contactpersonicon48_png, sizeof(bigimgs_contactpersonicon48_png));
282 wxImage bigimgs_contactpersonicon48i(istream, wxBITMAP_TYPE_PNG);
283 wxBitmap contacticonbmp(bigimgs_contactpersonicon48i, -1);
285 contacticon.CopyFromBitmap(contacticonbmp);
287 // Setup the contact information icons for later.
289 wxFileSystem::AddHandler(new wxMemoryFSHandler);
292 wxMemoryInputStream ciptostream(icons_cipto_png, sizeof(icons_cipto_png));
293 ciicon_png.LoadFile(ciptostream, wxBITMAP_TYPE_PNG);
294 wxMemoryFSHandler::AddFile(wxT("cipto.png"), ciicon_png, wxBITMAP_TYPE_PNG);
296 wxMemoryInputStream cilogstream(icons_cilog_png, sizeof(icons_cilog_png));
297 ciicon_png.LoadFile(cilogstream, wxBITMAP_TYPE_PNG);
298 wxMemoryFSHandler::AddFile(wxT("cilog.png"), ciicon_png, wxBITMAP_TYPE_PNG);
300 wxMemoryInputStream cisndstream(icons_cisnd_png, sizeof(icons_cisnd_png));
301 ciicon_png.LoadFile(cisndstream, wxBITMAP_TYPE_PNG);
302 wxMemoryFSHandler::AddFile(wxT("cisnd.png"), ciicon_png, wxBITMAP_TYPE_PNG);
304 wxMemoryInputStream cikeystream(icons_cikey_png, sizeof(icons_cikey_png));
305 ciicon_png.LoadFile(cikeystream, wxBITMAP_TYPE_PNG);
306 wxMemoryFSHandler::AddFile(wxT("cikey.png"), ciicon_png, wxBITMAP_TYPE_PNG);
308 wxMemoryInputStream civenstream(icons_civen_png, sizeof(icons_civen_png));
309 ciicon_png.LoadFile(civenstream, wxBITMAP_TYPE_PNG);
310 wxMemoryFSHandler::AddFile(wxT("civen.png"), ciicon_png, wxBITMAP_TYPE_PNG);
312 wxMemoryInputStream ciextstream(icons_ciext_png, sizeof(icons_ciext_png));
313 ciicon_png.LoadFile(ciextstream, wxBITMAP_TYPE_PNG);
314 wxMemoryFSHandler::AddFile(wxT("ciext.png"), ciicon_png, wxBITMAP_TYPE_PNG);
316 // Setup the contact information window.
318 frmContact *ContactWindow = new frmContact( NULL );
320 std::map<wxString, wxString> MemoryFileSystem;
323 FileLoadData.LoadFile(wxSContactFilename);
324 ContactWindow->SetMode(true);
325 ContactWindow->SetUID(0);
326 ContactWindow->SetupPointers(&MemoryFileSystem);
327 ContactWindow->SetupContactData(&FileLoadData);
328 ContactWindow->Show(true);
330 SetTopWindow(ContactWindow);
336 // Setup default settings and accounts if they don't exist.
339 SetupDefaultSettings();
340 SetupDefaultAddressBook();
342 wxMemoryInputStream istream(bigimgs_appicon48_png, sizeof(bigimgs_appicon_png));
343 wxImage bigimgs_appicon48i(istream, wxBITMAP_TYPE_PNG);
344 wxBitmap appiconbmp(bigimgs_appicon48i, -1);
347 appicon.CopyFromBitmap(appiconbmp);
348 frmMain *frame = new frmMain( NULL );
350 frmActivityMgr *frameActMgr = new frmActivityMgr ( frame );
351 frame->SetupPointers(frameActMgr);
353 frame->SetIcon(appicon);