Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Implemented support for the -d switch to display contact information.
[xestiaab/.git] / source / main.cpp
1 // main.cpp - Main subroutine (application start).
2 //
3 // (c) 2012-2015 Xestia Software Development.
4 //
5 // This file is part of Xestia Address Book.
6 //
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.
10 //
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.
15 //
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/>
19 #include <stdlib.h>
21 #include <wx/wx.h>
22 #include <wx/icon.h>
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"
30 #include "frmMain.h"
31 #include "frmContact.h"
32 #include "vcard/vcard.h"
33 #include "bitmaps.h"
34 #include "version.h"
35 #include "convert.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
43 {
44     virtual bool OnInit();
45 };
47 IMPLEMENT_APP(XestiaABApp);
49 bool XestiaABApp::OnInit()
50 {
52         // Setup the locale.
54         wxLocale locale;
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);
65                 this->Exit();
66         }
68 #endif*/
70         static const wxCmdLineEntryDesc g_cmdLineDesc [] =
71         {
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 },
92                 { wxCMD_LINE_NONE }
93         };
95         wxString wxSContactFilename;
96         wxString wxSContactOutputFilename;
97         wxString wxSContactFormat;
98         wxString wxSContactOutputFormat;
99         wxCmdLineParser XABArgs (g_cmdLineDesc, argc, argv);
100         XABArgs.Parse();
101     
102         if (XABArgs.Found(wxT("h"))){
103     
104                 // Print out the list of help commands.
105     
106                 return false;
107     
108         }
109     
110         if (XABArgs.Found(wxT("v"))){
111     
112                 // Print out the version number.    
114                 wxPuts(XSDAB_VERSION);
115                 return false;
116     
117         }
118     
119         wxInitAllImageHandlers();
121         if (XABArgs.Found(wxT("c"))){
122         
123                 // Preform a conversion.
125                 wxString InputFormat;
126                 wxString ExportFormat;
127                 wxString InputFilename;
128                 wxString OutputFilename;
129                 
130                 // Get the input format. Return error if blank
131                 // or invalid.
133                 if (XABArgs.Found(wxT("ifmt"))){                
134                         XABArgs.Found(wxT("ifmt"), &InputFormat);
135                 }       
136                         
137                 // Get the export format. Return error if blank
138                 // or invalid.
140                 if (XABArgs.Found(wxT("ofmt"))){
141                         XABArgs.Found(wxT("ofmt"), &ExportFormat);
142                 }
143                 
144                 // Get the input filename.
146                 if (XABArgs.Found(wxT("ifile"))){
147                         XABArgs.Found(wxT("ifile"), &InputFilename);
148                 }
149                 
150                 // Get the output filename (if any).
151                 
152                 if (XABArgs.Found(wxT("ofile"))){
153                         XABArgs.Found(wxT("ofile"), &OutputFilename);
154                 }
155                 
156                 if (argc >= 7){
157                         wxPuts(_("Too many arguments given."));
158                         exit(EXIT_FAILURE);
159                 }
160                 
161                 // Run the conversion process.
162                 
163                 ConvertResult ConvertRunStatus = ConvertContact(InputFormat, ExportFormat, InputFilename, OutputFilename);
164         
165                 switch (ConvertRunStatus){
166                         case CONVERTRESULT_UNITTESTFAIL:
167                                 wxPuts(_("An internal unit testing failure has occured."));
168                                 exit(EXIT_SUCCESS);
169                                 break;
170                         case CONVERTRESULT_OK:
171                                 break;
172                         case CONVERTRESULT_FORMATSSAME:
173                                 wxPuts(_("Both input and output formats are the same."));
174                                 exit(EXIT_FAILURE);
175                                 break;
176                         case CONVERTRESULT_INVALIDINPUTFORMAT:
177                                 wxPuts(_("Invalid input format given."));
178                                 exit(EXIT_FAILURE);
179                                 break;
180                         case CONVERTRESULT_INVALIDOUTPUTFORMAT:
181                                 wxPuts(_("Invalid output format given."));
182                                 exit(EXIT_FAILURE);
183                                 break;
184                         case CONVERTRESULT_INPUTFILEMISSING:
185                                 wxPuts(_("Input file with the filename given does not exist."));
186                                 exit(EXIT_FAILURE);
187                                 break;
188                         case CONVERTRESULT_INPUTFILEEMPTY:
189                                 wxPuts(_("No input filename given."));
190                                 exit(EXIT_FAILURE);
191                                 break;
192                         case CONVERTRESULT_INPUTFILEINVALIDFORMAT:
193                                 wxPuts(_("Input file is in an invalid format."));
194                                 exit(EXIT_FAILURE);
195                                 break; 
196                         case CONVERTRESULT_INPUTFILEERROR:
197                                 wxPuts(_("An error occured whilst trying to open the input file location."));
198                                 exit(EXIT_FAILURE);
199                                 break;
200                         case CONVERTRESULT_OUTPUTFILEERROR:
201                                 wxPuts(_("An error occured whilst trying to open the output file location."));
202                                 exit(EXIT_FAILURE);
203                                 break;
204                 }
205         
206                 return true;
207         
208         }
210         if (XABArgs.Found(wxT("s"))){
211     
212                 // Open up the search window.
213         
214                 frmSearch *frmSearchPtr = new frmSearch( NULL );
215                 frmSearchPtr->Show(true);
216                 frmSearchPtr->SetSearchMode(true);
217                 return true;
218     
219         }
220     
221         if (XABArgs.Found(wxT("e"), &wxSContactFilename)){
223                 // Check if the filename exists.
225                 wxFileName contactfile(wxSContactFilename);
226         
227                 if (!contactfile.FileExists()){
228         
229                         wxMessageBox(_("The file with the filename given does not exist."), _("Error loading contact"), wxICON_ERROR);
230                         return false;
231         
232                 }
233         
234                 // Check if file is in the user's Xestia Address Book data storage
235                 // path. If it is, refuse to open it.
236         
237                 wxString UserDir = GetUserDir();
238                 long UserDirLength = UserDir.Len();
239         
240                 if (UserDir == contactfile.GetFullPath().Mid(0, UserDirLength)){
241         
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);
243                         return false;
244         
245                 }
246         
247                 wxMemoryInputStream istream(bigimgs_contactpersonicon48_png, sizeof(bigimgs_contactpersonicon48_png));
248                 wxImage bigimgs_contactpersonicon48i(istream, wxBITMAP_TYPE_PNG);
249                 wxBitmap contacticonbmp(bigimgs_contactpersonicon48i, -1);
250                 wxIcon contacticon;
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);
261         
262                 SetTopWindow(ContactEditor);
263         
264                 return true;
265     
266         }
267         
268         if (XABArgs.Found(wxT("d"), &wxSContactFilename)){
270                 // Check if the filename exists.
272                 wxFileName contactfile(wxSContactFilename);
273         
274                 if (!contactfile.FileExists()){
275         
276                         wxMessageBox(_("The file with the filename given does not exist."), _("Error loading contact"), wxICON_ERROR);
277                         return false;
278         
279                 }
280         
281                 // Check if file is in the user's Xestia Address Book data storage
282                 // path. If it is, refuse to open it.
283         
284                 wxString UserDir = GetUserDir();
285                 long UserDirLength = UserDir.Len();
286         
287                 if (UserDir == contactfile.GetFullPath().Mid(0, UserDirLength)){
288         
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);
290                         return false;
291         
292                 }
293         
294                 wxMemoryInputStream istream(bigimgs_contactpersonicon48_png, sizeof(bigimgs_contactpersonicon48_png));
295                 wxImage bigimgs_contactpersonicon48i(istream, wxBITMAP_TYPE_PNG);
296                 wxBitmap contacticonbmp(bigimgs_contactpersonicon48i, -1);
297                 wxIcon contacticon;
298                 contacticon.CopyFromBitmap(contacticonbmp); 
299                 
300                 // Setup the contact information icons for later.
301     
302                 wxFileSystem::AddHandler(new wxMemoryFSHandler);
303                 wxImage ciicon_png;
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);
308     
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);
312     
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);
316     
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);
320     
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);
324     
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);
328                 
329                 // Setup the contact information window.
331                 frmContact *ContactWindow = new frmContact( NULL );
332                 
333                 std::map<wxString, wxString> MemoryFileSystem;
334                 
335                 vCard FileLoadData;
336                 FileLoadData.LoadFile(wxSContactFilename);
337                 ContactWindow->SetMode(true);
338                 ContactWindow->SetUID(0);
339                 ContactWindow->SetupPointers(&MemoryFileSystem);
340                 ContactWindow->SetupContactData(&FileLoadData);
341                 ContactWindow->Show(true);
342                 
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);*/
349                 
350                 SetTopWindow(ContactWindow);
351         
352                 return true;
353     
354         }
355         
356         /*if (!XABArgs.Found(wxT(""))){
357         
358                 return false;
359         
360         }*/
361     
362         // Setup default settings and accounts if they don't exist.
363     
364         SetupDirectories();
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);    
371         wxIcon appicon;
372     
373         appicon.CopyFromBitmap(appiconbmp);
374         frmMain *frame = new frmMain( NULL );
375         frame->Show(true);
376         frmActivityMgr *frameActMgr = new frmActivityMgr ( frame );
377         frame->SetupPointers(frameActMgr);
378         frame->SetupForm();
379         frame->SetIcon(appicon);
380    
381         SetTopWindow(frame);
382    
383         return true;
Xestia Software Development
Yn Maystri
© 2006 - 2019 Xestia Software Development
Software

Xestia Address Book
Xestia Calendar
Development

Xestia Gelforn
Everything else

About
News
Privacy Policy