Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Another attempt at fixing the command line text in main.cpp
[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         static const wxCmdLineEntryDesc g_cmdLineDesc [] =
58         {
59                 { wxCMD_LINE_SWITCH, "h", "help", _("Displays help on command line parameters"),
60                         wxCMD_LINE_VAL_NONE, wxCMD_LINE_OPTION_HELP },
61                 { wxCMD_LINE_OPTION, "e", "edit", _("Edit a vCard 4.0 formatted contact"),
62                         wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
63                 { wxCMD_LINE_SWITCH, "c", "convert", _("Convert a contact file into another format."),
64                         wxCMD_LINE_VAL_NONE, wxCMD_LINE_PARAM_OPTIONAL },
65                 { wxCMD_LINE_OPTION, "ifmt", NULL, _("Input format to convert from. (used with -c)"),
66                         wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
67                 { wxCMD_LINE_OPTION, "ofmt", NULL, _("Output format to convert to. (used with -c)"),
68                         wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
69                 { wxCMD_LINE_OPTION, "ifile", NULL, _("Input filename to read from. (used with -c)"),
70                         wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
71                 { wxCMD_LINE_OPTION, "ofile", NULL, _("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, "d", "display", _("Display a contact in the contact information window."),
74                         wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
75                 { wxCMD_LINE_SWITCH, "s", "search", _("Display the search window instead of starting normally"),
76                         wxCMD_LINE_VAL_NONE, wxCMD_LINE_PARAM_OPTIONAL },
77                 { wxCMD_LINE_SWITCH, "v", "version", _("Displays version number"),
78                         wxCMD_LINE_VAL_NONE, wxCMD_LINE_PARAM_OPTIONAL },
79                 { wxCMD_LINE_NONE }
80         };
82         wxString wxSContactFilename;
83         wxString wxSContactOutputFilename;
84         wxString wxSContactFormat;
85         wxString wxSContactOutputFormat;
86         wxCmdLineParser XABArgs (g_cmdLineDesc, argc, argv);
87         XABArgs.Parse();
88     
89         if (XABArgs.Found(wxT("h"))){
90     
91                 // Print out the list of help commands.
92     
93                 return false;
94     
95         }
96     
97         if (XABArgs.Found(wxT("v"))){
98     
99                 // Print out the version number.    
101                 wxPuts(XSDAB_VERSION);
102                 return false;
103     
104         }
105     
106         wxInitAllImageHandlers();
108         if (XABArgs.Found(wxT("c"))){
109         
110                 // Preform a conversion.
112                 wxString InputFormat;
113                 wxString ExportFormat;
114                 wxString InputFilename;
115                 wxString OutputFilename;
116                 
117                 // Get the input format. Return error if blank
118                 // or invalid.
120                 if (XABArgs.Found(wxT("ifmt"))){                
121                         XABArgs.Found(wxT("ifmt"), &InputFormat);
122                 }       
123                         
124                 // Get the export format. Return error if blank
125                 // or invalid.
127                 if (XABArgs.Found(wxT("ofmt"))){
128                         XABArgs.Found(wxT("ofmt"), &ExportFormat);
129                 }
130                 
131                 // Get the input filename.
133                 if (XABArgs.Found(wxT("ifile"))){
134                         XABArgs.Found(wxT("ifile"), &InputFilename);
135                 }
136                 
137                 // Get the output filename (if any).
138                 
139                 if (XABArgs.Found(wxT("ofile"))){
140                         XABArgs.Found(wxT("ofile"), &OutputFilename);
141                 }
142                 
143                 if (argc >= 7){
144                         wxPuts(_("Too many arguments given."));
145                         exit(EXIT_FAILURE);
146                 }
147                 
148                 // Run the conversion process.
149                 
150                 ConvertResult ConvertRunStatus = ConvertContact(InputFormat, ExportFormat, InputFilename, OutputFilename);
151         
152                 switch (ConvertRunStatus){
153                         case CONVERTRESULT_UNITTESTFAIL:
154                                 wxPuts(_("An internal unit testing failure has occured."));
155                                 exit(EXIT_SUCCESS);
156                                 break;
157                         case CONVERTRESULT_OK:
158                                 break;
159                         case CONVERTRESULT_FORMATSSAME:
160                                 wxPuts(_("Both input and output formats are the same."));
161                                 exit(EXIT_FAILURE);
162                                 break;
163                         case CONVERTRESULT_INVALIDINPUTFORMAT:
164                                 wxPuts(_("Invalid input format given."));
165                                 exit(EXIT_FAILURE);
166                                 break;
167                         case CONVERTRESULT_INVALIDOUTPUTFORMAT:
168                                 wxPuts(_("Invalid output format given."));
169                                 exit(EXIT_FAILURE);
170                                 break;
171                         case CONVERTRESULT_INPUTFILEMISSING:
172                                 wxPuts(_("Input file with the filename given does not exist."));
173                                 exit(EXIT_FAILURE);
174                                 break;
175                         case CONVERTRESULT_INPUTFILEEMPTY:
176                                 wxPuts(_("No input filename given."));
177                                 exit(EXIT_FAILURE);
178                                 break;
179                         case CONVERTRESULT_INPUTFILEINVALIDFORMAT:
180                                 wxPuts(_("Input file is in an invalid format."));
181                                 exit(EXIT_FAILURE);
182                                 break; 
183                         case CONVERTRESULT_INPUTFILEERROR:
184                                 wxPuts(_("An error occured whilst trying to open the input file location."));
185                                 exit(EXIT_FAILURE);
186                                 break;
187                         case CONVERTRESULT_OUTPUTFILEERROR:
188                                 wxPuts(_("An error occured whilst trying to open the output file location."));
189                                 exit(EXIT_FAILURE);
190                                 break;
191                 }
192         
193                 return true;
194         
195         }
197         if (XABArgs.Found(wxT("s"))){
198     
199                 // Open up the search window.
200         
201                 frmSearch *frmSearchPtr = new frmSearch( NULL );
202                 frmSearchPtr->Show(true);
203                 frmSearchPtr->SetSearchMode(true);
204                 return true;
205     
206         }
207     
208         if (XABArgs.Found(wxT("e"), &wxSContactFilename)){
210                 // Check if the filename exists.
212                 wxFileName contactfile(wxSContactFilename);
213         
214                 if (!contactfile.FileExists()){
215         
216                         wxMessageBox(_("The file with the filename given does not exist."), _("Error loading contact"), wxICON_ERROR);
217                         return false;
218         
219                 }
220         
221                 // Check if file is in the user's Xestia Address Book data storage
222                 // path. If it is, refuse to open it.
223         
224                 wxString UserDir = GetUserDir();
225                 long UserDirLength = UserDir.Len();
226         
227                 if (UserDir == contactfile.GetFullPath().Mid(0, UserDirLength)){
228         
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);
230                         return false;
231         
232                 }
233         
234                 wxMemoryInputStream istream(bigimgs_contactpersonicon48_png, sizeof(bigimgs_contactpersonicon48_png));
235                 wxImage bigimgs_contactpersonicon48i(istream, wxBITMAP_TYPE_PNG);
236                 wxBitmap contacticonbmp(bigimgs_contactpersonicon48i, -1);
237                 wxIcon contacticon;
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);
248         
249                 SetTopWindow(ContactEditor);
250         
251                 return true;
252     
253         }
254         
255         if (XABArgs.Found(wxT("d"), &wxSContactFilename)){
257                 // Check if the filename exists.
259                 wxFileName contactfile(wxSContactFilename);
260         
261                 if (!contactfile.FileExists()){
262         
263                         wxMessageBox(_("The file with the filename given does not exist."), _("Error loading contact"), wxICON_ERROR);
264                         return false;
265         
266                 }
267         
268                 // Check if file is in the user's Xestia Address Book data storage
269                 // path. If it is, refuse to open it.
270         
271                 wxString UserDir = GetUserDir();
272                 long UserDirLength = UserDir.Len();
273         
274                 if (UserDir == contactfile.GetFullPath().Mid(0, UserDirLength)){
275         
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);
277                         return false;
278         
279                 }
280         
281                 wxMemoryInputStream istream(bigimgs_contactpersonicon48_png, sizeof(bigimgs_contactpersonicon48_png));
282                 wxImage bigimgs_contactpersonicon48i(istream, wxBITMAP_TYPE_PNG);
283                 wxBitmap contacticonbmp(bigimgs_contactpersonicon48i, -1);
284                 wxIcon contacticon;
285                 contacticon.CopyFromBitmap(contacticonbmp); 
286                 
287                 // Setup the contact information icons for later.
288     
289                 wxFileSystem::AddHandler(new wxMemoryFSHandler);
290                 wxImage ciicon_png;
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);
295     
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);
299     
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);
303     
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);
307     
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);
311     
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);
315                 
316                 // Setup the contact information window.
318                 frmContact *ContactWindow = new frmContact( NULL );
319                 
320                 std::map<wxString, wxString> MemoryFileSystem;
321                 
322                 vCard FileLoadData;
323                 FileLoadData.LoadFile(wxSContactFilename);
324                 ContactWindow->SetMode(true);
325                 ContactWindow->SetUID(0);
326                 ContactWindow->SetupPointers(&MemoryFileSystem);
327                 ContactWindow->SetupContactData(&FileLoadData);
328                 ContactWindow->Show(true);
329                 
330                 SetTopWindow(ContactWindow);
331         
332                 return true;
333     
334         }
335     
336         // Setup default settings and accounts if they don't exist.
337     
338         SetupDirectories();
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);    
345         wxIcon appicon;
346     
347         appicon.CopyFromBitmap(appiconbmp);
348         frmMain *frame = new frmMain( NULL );
349         frame->Show(true);
350         frmActivityMgr *frameActMgr = new frmActivityMgr ( frame );
351         frame->SetupPointers(frameActMgr);
352         frame->SetupForm();
353         frame->SetIcon(appicon);
354    
355         SetTopWindow(frame);
356    
357         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