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"
34 #include "actmgr/frmActivityMgr.h"
35 #include "search/frmSearch.h"
36 #include "common/timers.h"
37 #include "common/defaults.h"
38 #include "common/dirs.h"
40 class XestiaABApp: public wxApp
42 virtual bool OnInit();
45 IMPLEMENT_APP(XestiaABApp);
47 bool XestiaABApp::OnInit()
53 locale.Init(wxLANGUAGE_DEFAULT, wxLOCALE_LOAD_DEFAULT);
55 /*#if defined(__WIN32__)
57 // Check that the minimum version of Xestia Common Components is installed on the system.
59 #include <xestiaccdll.h>
61 if (!CheckXCCVersion(1, 0, 0)){
62 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);
68 static const wxCmdLineEntryDesc g_cmdLineDesc [] =
70 { wxCMD_LINE_SWITCH, wxT_2("h"), wxT_2("help"), wxTRANSLATE("Displays help on command line parameters"),
71 wxCMD_LINE_VAL_NONE, wxCMD_LINE_OPTION_HELP },
72 { wxCMD_LINE_OPTION, wxT_2("e"), wxT_2("edit"), wxTRANSLATE("Edit a vCard 4.0 formatted contact"),
73 wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
74 { wxCMD_LINE_SWITCH, wxT_2("c"), wxT_2("convert"), wxTRANSLATE("Convert a contact file into another format."),
75 wxCMD_LINE_VAL_NONE, wxCMD_LINE_PARAM_OPTIONAL },
76 { wxCMD_LINE_OPTION, wxT_2("ifmt"), NULL, wxTRANSLATE("Input format to convert from. (used with -c)"),
77 wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
78 { wxCMD_LINE_OPTION, wxT_2("ofmt"), NULL, wxTRANSLATE("Output format to convert to. (used with -c)"),
79 wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
80 { wxCMD_LINE_OPTION, wxT_2("ifile"), NULL, wxTRANSLATE("Input filename to read from. (used with -c)"),
81 wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
82 { wxCMD_LINE_OPTION, wxT_2("ofile"), NULL, wxTRANSLATE("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_SWITCH, wxT_2("s"), wxT_2("search"), wxTRANSLATE("Display the search window instead of starting normally"),
85 wxCMD_LINE_VAL_NONE, wxCMD_LINE_PARAM_OPTIONAL },
86 { wxCMD_LINE_SWITCH, wxT_2("v"), wxT_2("version"), wxTRANSLATE("Displays version number"),
87 wxCMD_LINE_VAL_NONE, wxCMD_LINE_PARAM_OPTIONAL },
91 wxString wxSContactFilename;
92 wxString wxSContactOutputFilename;
93 wxString wxSContactFormat;
94 wxString wxSContactOutputFormat;
95 wxCmdLineParser XABArgs (g_cmdLineDesc, argc, argv);
98 if (XABArgs.Found(wxT("h"))){
100 // Print out the list of help commands.
106 if (XABArgs.Found(wxT("v"))){
108 // Print out the version number.
110 wxPuts(XSDAB_VERSION);
115 wxInitAllImageHandlers();
117 if (XABArgs.Found(wxT("c"))){
119 // Preform a conversion.
121 wxString InputFormat;
122 wxString ExportFormat;
123 wxString InputFilename;
124 wxString OutputFilename;
126 // Get the input format. Return error if blank
129 if (XABArgs.Found(wxT("ifmt"))){
130 XABArgs.Found(wxT("ifmt"), &InputFormat);
133 // Get the export format. Return error if blank
136 if (XABArgs.Found(wxT("ofmt"))){
137 XABArgs.Found(wxT("ofmt"), &ExportFormat);
140 // Get the input filename.
142 if (XABArgs.Found(wxT("ifile"))){
143 XABArgs.Found(wxT("ifile"), &InputFilename);
146 // Get the output filename (if any).
148 if (XABArgs.Found(wxT("ofile"))){
149 XABArgs.Found(wxT("ofile"), &OutputFilename);
153 wxPuts("Too many arguments given.");
157 // Run the conversion process.
159 ConvertResult ConvertRunStatus = ConvertContact(InputFormat, ExportFormat, InputFilename, OutputFilename);
161 switch (ConvertRunStatus){
162 case CONVERTRESULT_UNITTESTFAIL:
163 wxPuts("An internal unit testing failure has occured.");
166 case CONVERTRESULT_OK:
168 case CONVERTRESULT_FORMATSSAME:
169 wxPuts("Both input and output formats are the same.");
172 case CONVERTRESULT_INVALIDINPUTFORMAT:
173 wxPuts("Invalid input format given.");
176 case CONVERTRESULT_INVALIDOUTPUTFORMAT:
177 wxPuts("Invalid output format given.");
180 case CONVERTRESULT_INPUTFILEMISSING:
181 wxPuts("Input file with the filename given does not exist.");
184 case CONVERTRESULT_INPUTFILEEMPTY:
185 wxPuts("No input filename given.");
188 case CONVERTRESULT_INPUTFILEINVALIDFORMAT:
189 wxPuts("Input file is in an invalid format.");
192 case CONVERTRESULT_INPUTFILEERROR:
193 wxPuts("An error occured whilst trying to open the input file location.");
196 case CONVERTRESULT_OUTPUTFILEERROR:
197 wxPuts("An error occured whilst trying to open the output file location.");
206 if (XABArgs.Found(wxT("s"))){
208 // Open up the search window.
210 frmSearch *frmSearchPtr = new frmSearch( NULL );
211 frmSearchPtr->Show(true);
212 frmSearchPtr->SetSearchMode(true);
217 if (XABArgs.Found(wxT("e"), &wxSContactFilename)){
219 // Check if the filename exists.
221 wxFileName contactfile(wxSContactFilename);
223 if (!contactfile.FileExists()){
225 wxMessageBox(_("The file with the filename given does not exist."), _("Error loading contact"), wxICON_ERROR);
230 // Check if file is in the user's Xestia Address Book data storage
231 // path. If it is, refuse to open it.
233 wxString UserDir = GetUserDir();
234 long UserDirLength = UserDir.Len();
236 if (UserDir == contactfile.GetFullPath().Mid(0, UserDirLength)){
238 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);
243 wxMemoryInputStream istream(bigimgs_contactpersonicon48_png, sizeof(bigimgs_contactpersonicon48_png));
244 wxImage bigimgs_contactpersonicon48i(istream, wxBITMAP_TYPE_PNG);
245 wxBitmap contacticonbmp(bigimgs_contactpersonicon48i, -1);
247 contacticon.CopyFromBitmap(contacticonbmp);
249 // Get the filename of the selected contact.
251 frmContactEditor *ContactEditor = new frmContactEditor( NULL );
252 ContactEditor->SetupHeaders();
253 ContactEditor->SetMode(TRUE);
254 ContactEditor->LoadContact(wxSContactFilename);
255 ContactEditor->SetIcon(contacticon);
256 ContactEditor->Show(true);
258 SetTopWindow(ContactEditor);
264 if (!XABArgs.Found(wxT(""))){
270 // Setup default settings and accounts if they don't exist.
273 SetupDefaultSettings();
274 SetupDefaultAddressBook();
276 wxMemoryInputStream istream(bigimgs_appicon48_png, sizeof(bigimgs_appicon_png));
277 wxImage bigimgs_appicon48i(istream, wxBITMAP_TYPE_PNG);
278 wxBitmap appiconbmp(bigimgs_appicon48i, -1);
281 appicon.CopyFromBitmap(appiconbmp);
282 frmMain *frame = new frmMain( NULL );
284 frmActivityMgr *frameActMgr = new frmActivityMgr ( frame );
285 frame->SetupPointers(frameActMgr);
287 frame->SetIcon(appicon);