Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Capitalise Bitmaps and Tools directories
[xestiaab/.git] / source / main.cpp
1 // main.cpp - Main subroutine (application start).
2 //
3 // (c) 2012-2017 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 #if defined(__WIN32__)
43 #include "common/win32ssl.h"
44 #endif
46 class XestiaABApp: public wxApp
47 {
48     virtual bool OnInit();
49 };
51 IMPLEMENT_APP(XestiaABApp);
53 bool XestiaABApp::OnInit()
54 {
56 #if defined(__WIN32__)
58         PSecurityFunctionTableW SecurityFunctionTbl;
59         SecurityFunctionTbl = (*InitSecurityInterface)();
61 #endif
63         // Setup the locale.
65         wxLocale locale;
66     locale.Init(wxLANGUAGE_DEFAULT, wxLOCALE_LOAD_DEFAULT);
68         static const wxCmdLineEntryDesc g_cmdLineDesc [] =
69         {
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 },
90                 { wxCMD_LINE_NONE }
91         };
93         wxString wxSContactFilename;
94         wxString wxSContactOutputFilename;
95         wxString wxSContactFormat;
96         wxString wxSContactOutputFormat;
97         wxCmdLineParser XABArgs (g_cmdLineDesc, argc, argv);
98         XABArgs.Parse();
99     
100         if (XABArgs.Found(wxT("h"))){
101     
102                 // Print out the list of help commands.
103     
104                 return false;
105     
106         }
107     
108         if (XABArgs.Found(wxT("v"))){
109     
110                 // Print out the version number.    
112                 wxPuts(XSDAB_VERSION);
113                 return false;
114     
115         }
116     
117         wxInitAllImageHandlers();
119         if (XABArgs.Found(wxT("c"))){
120         
121                 // Preform a conversion.
123                 wxString InputFormat;
124                 wxString ExportFormat;
125                 wxString InputFilename;
126                 wxString OutputFilename;
127                 
128                 // Get the input format. Return error if blank
129                 // or invalid.
131                 if (XABArgs.Found(wxT("ifmt"))){                
132                         XABArgs.Found(wxT("ifmt"), &InputFormat);
133                 }       
134                         
135                 // Get the export format. Return error if blank
136                 // or invalid.
138                 if (XABArgs.Found(wxT("ofmt"))){
139                         XABArgs.Found(wxT("ofmt"), &ExportFormat);
140                 }
141                 
142                 // Get the input filename.
144                 if (XABArgs.Found(wxT("ifile"))){
145                         XABArgs.Found(wxT("ifile"), &InputFilename);
146                 }
147                 
148                 // Get the output filename (if any).
149                 
150                 if (XABArgs.Found(wxT("ofile"))){
151                         XABArgs.Found(wxT("ofile"), &OutputFilename);
152                 }
153                 
154                 if (argc >= 7){
155                         wxPuts(_("Too many arguments given."));
156                         exit(EXIT_FAILURE);
157                 }
158                 
159                 // Run the conversion process.
160                 
161                 ConvertResult ConvertRunStatus = ConvertContact(InputFormat, ExportFormat, InputFilename, OutputFilename);
162         
163                 switch (ConvertRunStatus){
164                         case CONVERTRESULT_UNITTESTFAIL:
165                                 wxPuts(_("An internal unit testing failure has occured."));
166                                 exit(EXIT_SUCCESS);
167                                 break;
168                         case CONVERTRESULT_OK:
169                                 break;
170                         case CONVERTRESULT_FORMATSSAME:
171                                 wxPuts(_("Both input and output formats are the same."));
172                                 exit(EXIT_FAILURE);
173                                 break;
174                         case CONVERTRESULT_INVALIDINPUTFORMAT:
175                                 wxPuts(_("Invalid input format given."));
176                                 exit(EXIT_FAILURE);
177                                 break;
178                         case CONVERTRESULT_INVALIDOUTPUTFORMAT:
179                                 wxPuts(_("Invalid output format given."));
180                                 exit(EXIT_FAILURE);
181                                 break;
182                         case CONVERTRESULT_INPUTFILEMISSING:
183                                 wxPuts(_("Input file with the filename given does not exist."));
184                                 exit(EXIT_FAILURE);
185                                 break;
186                         case CONVERTRESULT_INPUTFILEEMPTY:
187                                 wxPuts(_("No input filename given."));
188                                 exit(EXIT_FAILURE);
189                                 break;
190                         case CONVERTRESULT_INPUTFILEINVALIDFORMAT:
191                                 wxPuts(_("Input file is in an invalid format."));
192                                 exit(EXIT_FAILURE);
193                                 break; 
194                         case CONVERTRESULT_INPUTFILEERROR:
195                                 wxPuts(_("An error occured whilst trying to open the input file location."));
196                                 exit(EXIT_FAILURE);
197                                 break;
198                         case CONVERTRESULT_OUTPUTFILEERROR:
199                                 wxPuts(_("An error occured whilst trying to open the output file location."));
200                                 exit(EXIT_FAILURE);
201                                 break;
202                 }
203         
204                 return true;
205         
206         }
208         if (XABArgs.Found(wxT("s"))){
209     
210                 // Open up the search window.
211         
212                 frmSearch *frmSearchPtr = new frmSearch( NULL );
213                 frmSearchPtr->Show(true);
214                 frmSearchPtr->SetSearchMode(true);
215                 return true;
216     
217         }
218     
219         if (XABArgs.Found(wxT("e"), &wxSContactFilename)){
221                 // Check if the filename exists.
223                 wxFileName contactfile(wxSContactFilename);
224         
225                 if (!contactfile.FileExists()){
226         
227                         wxMessageBox(_("The file with the filename given does not exist."), _("Error loading contact"), wxICON_ERROR);
228                         return false;
229         
230                 }
231         
232                 // Check if file is in the user's Xestia Address Book data storage
233                 // path. If it is, refuse to open it.
234         
235                 wxString UserDir = GetUserDir();
236                 long UserDirLength = UserDir.Len();
237         
238                 if (UserDir == contactfile.GetFullPath().Mid(0, UserDirLength)){
239         
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);
241                         return false;
242         
243                 }
244         
245                 wxMemoryInputStream istream(bigimgs_contactpersonicon48_png, sizeof(bigimgs_contactpersonicon48_png));
246                 wxImage bigimgs_contactpersonicon48i(istream, wxBITMAP_TYPE_PNG);
247                 wxBitmap contacticonbmp(bigimgs_contactpersonicon48i, -1);
248                 wxIcon contacticon;
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);
259         
260                 SetTopWindow(ContactEditor);
261         
262                 return true;
263     
264         }
265         
266         if (XABArgs.Found(wxT("d"), &wxSContactFilename)){
268                 // Check if the filename exists.
270                 wxFileName contactfile(wxSContactFilename);
271         
272                 if (!contactfile.FileExists()){
273         
274                         wxMessageBox(_("The file with the filename given does not exist."), _("Error loading contact"), wxICON_ERROR);
275                         return false;
276         
277                 }
278         
279                 // Check if file is in the user's Xestia Address Book data storage
280                 // path. If it is, refuse to open it.
281         
282                 wxString UserDir = GetUserDir();
283                 long UserDirLength = UserDir.Len();
284         
285                 if (UserDir == contactfile.GetFullPath().Mid(0, UserDirLength)){
286         
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);
288                         return false;
289         
290                 }
291         
292                 wxMemoryInputStream istream(bigimgs_contactpersonicon48_png, sizeof(bigimgs_contactpersonicon48_png));
293                 wxImage bigimgs_contactpersonicon48i(istream, wxBITMAP_TYPE_PNG);
294                 wxBitmap contacticonbmp(bigimgs_contactpersonicon48i, -1);
295                 wxIcon contacticon;
296                 contacticon.CopyFromBitmap(contacticonbmp); 
297                 
298                 // Setup the contact information icons for later.
299     
300                 wxFileSystem::AddHandler(new wxMemoryFSHandler);
301                 wxImage ciicon_png;
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);
306     
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);
310     
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);
314     
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);
318     
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);
322     
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);
326                 
327                 // Setup the contact information window.
329                 frmContact *ContactWindow = new frmContact( NULL );
330                 
331                 std::map<wxString, wxString> MemoryFileSystem;
332                 
333                 vCard FileLoadData;
334                 FileLoadData.LoadFile(wxSContactFilename);
335                 ContactWindow->SetMode(true);
336                 ContactWindow->SetUID(0);
337                 ContactWindow->SetupPointers(&MemoryFileSystem);
338                 ContactWindow->SetupContactData(&FileLoadData);
339                 ContactWindow->Show(true);
340                 
341                 SetTopWindow(ContactWindow);
342         
343                 return true;
344     
345         }
346     
347         // Setup default settings and accounts if they don't exist.
348     
349         SetupDirectories();
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);    
356         wxIcon appicon;
357     
358         appicon.CopyFromBitmap(appiconbmp);
359         frmMain *frame = new frmMain( NULL );
360         frame->Show(true);
361         frmActivityMgr *frameActMgr = new frmActivityMgr ( frame );
362         frame->SetupPointers(frameActMgr);
363         frame->SetupForm();
364         frame->SetIcon(appicon);
365    
366         SetTopWindow(frame);
367    
368         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