-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCApp.cpp
376 lines (306 loc) · 11.6 KB
/
CApp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
* This software is a part to the amonsoft solution
*
* Copyright Amon & Sesam Micro
* Developper contact [email protected]
*
* $Id: CApp.cpp,v 1.5 2007/04/06 15:34:43 pascal Exp $
*/
#include "CApp.h"
// QT
#include <qapplication.h>
#include <qsplashscreen.h>
#include <qimage.h>
#include <qtabwidget.h>
#include <qmessagebox.h>
#include <qpushbutton.h>
#include <qurloperator.h>
#include <qdir.h>
// Xerces
#include <xercesc/util/PlatformUtils.hpp>
// XML Parser
#include "XMLParser.h"
#include "updater.h"
// Main interface
#include "Forms/Dynamique.h"
// Mains Datas
#include "Datas/CDatas.h"
#include "Datas/CPersonnes.h"
#include "Datas/CEntreprise.h"
#include "Datas/CSearch.h"
//----------------------------------------------------------------------------
// Global var
//----------------------------------------------------------------------------
CParser *g_Parser;
CParser *g_ParserEntses;
CParser *g_ParserFamille;
Preferences::CPreferences *g_Preferences;
dlgAmonSoft *g_gui;
CApp *g_App;
static const char sPersonnesFile[] = "./xml/personnes.xml";
static const char sEntreprisesFile[] = "./xml/entreprises.xml";
static const char sFamilleFile[] = "./xml/familles.xml";
static const char sSearchFile[] = "./search.ini";
XERCES_CPP_NAMESPACE_USE
//----------------------------------------------------------------------------
// Constructor
//----------------------------------------------------------------------------
CApp::CApp( int argc, char **argv ) : argc_( argc ), argv_( argv )
{
g_App = this;
}
//----------------------------------------------------------------------------
// Run app
//----------------------------------------------------------------------------
void CApp::runApp()
{
// We must always have an application
QApplication application( argc_, argv_ );
AmonSoft *main = new AmonSoft; // We create our dialog
dlgAmonSoft *gui = const_cast< dlgAmonSoft* >( g_gui );
gui = main;
// Shareware version !
QDate currentDate = QDate::currentDate();
QDate expireDate;
expireDate.setYMD( 2007, 12, 30 );
if( currentDate > expireDate )
{
QMessageBox::critical( main,
"Votre version a expiré",
"Veuillez contacter votre revendeur pour continuer à utiliser ce programme",
QMessageBox::Ok,
QMessageBox::NoButton );
exit( 0 );
}
// Splash screen
QImage imgSplash( "Forms/Img/Splash.PNG" );
QSplashScreen *splash =
new QSplashScreen( imgSplash );
splash->show();
Datas::usLastId = 0;
Datas::usLastIdEntses = 0;
splash->message(QObject::tr("Initialisation de Xerces..."),
Qt::AlignLeft | Qt::AlignTop, Qt::blue);
XMLPlatformUtils::Initialize();
//<-- Insert here modules to load during splash screen
splash->message(QObject::tr("Chargement de l'interface..."),
Qt::AlignLeft | Qt::AlignTop, Qt::blue);
//-->
application.setMainWidget( main ); // It is our main widget
CUpdater up( *main );
up.updateAvailable();
// Interface init
splash->message(QObject::tr("Chargement des données..."),
Qt::AlignLeft | Qt::AlignTop, Qt::blue);
makeBackup();
// Load searchs
Datas::g_Search = new Datas::CSearch();
Datas::g_Search->loadFileSearch( sSearchFile );
Datas::g_Search->loadInComboBox( main->comboBoxSearchFunction, true );
// Add empty enterprise and person to reset form
Datas::CPersonnes *pEmptyPerson = new Datas::CPersonnes( true );
Datas::CEntreprise *pEmptyEnterprise = new Datas::CEntreprise( true );
Datas::g_listPersonnes.push_back( pEmptyPerson );
Datas::g_listEntreprises.push_back( pEmptyEnterprise );
// Initialize & load preference system
g_Preferences = new Preferences::CPreferences();
// Initialize parser
g_Parser = new CParser( sPersonnesFile );
g_ParserEntses = new CParser( sEntreprisesFile );
g_ParserFamille = new CParser( sFamilleFile );
// Be carrefull, order is really important !
loadXercesData( *main );
loadXercesDataEntses( *main );
loadXercesDataFamille( *main ); // Always after Personnes load !!
// TODO: Put progress bar
// Show version information
QString strVersion = "Amonsoft (c) Sesam micro 2005-2006 Version ";
qDebug( strVersion + STR_APP_VERSION );
main->lister();
main->show(); // Show it...
delete splash;
application.exec(); // And run!
saveXercesData();
delete g_Preferences;
XMLPlatformUtils::Terminate();
}
//----------------------------------------------------------------------------
// Be sure about files compability
//----------------------------------------------------------------------------
static void CheckDataFileVersion( const DOMElement *pElem, dlgAmonSoft &amon )
{
std::string sVersion = XMLString::transcode( pElem->getAttribute( XMLString::transcode( "version" ) ) );
if( sVersion != CONF_COMPTABILITY && sVersion != CONF_VERSION )
{
std::string sMsg = "Vous utilisez une version des données incompatibles avec la version du programme : ";
sMsg += CONF_COMPTABILITY;
QMessageBox::critical( &amon,
"Version de fichiers de données",
sMsg.c_str(),
QMessageBox::Ok,
QMessageBox::NoButton );
exit( -1 );
}
}
/*
* Load datas
*/
int CApp::loadXercesData( dlgAmonSoft &amon )
{
try {
XERCES_CPP_NAMESPACE::DOMDocument *pDoc = g_Parser->getDocument();
XERCES_CPP_NAMESPACE::DOMElement *pElem = pDoc->getDocumentElement();
CheckDataFileVersion( pElem, amon );
DOMNodeList *pNodeList = pElem->getChildNodes ();
uint size = pNodeList->getLength();
for( uint i = 0; i < size; i++ )
{
DOMNode *pNode = pNodeList->item( i );
if( XMLString::equals( XMLString::transcode( pNode->getNodeName() ), "Personne" ) )
{
QString string;
Datas::CPersonnes *pPersonne = new Datas::CPersonnes;
DOMElement *elmt = static_cast< DOMElement* >( pNode );
mAssert( elmt != 0, "elmt != 0" );
pPersonne->loadFromXML( elmt );
QListView *listView = amon.getListViewPersonnes();
QListViewItem *item = new QListViewItem( listView,
pPersonne->getNom(),
pPersonne->getPrenom(),
pPersonne->getTelephone(),
pPersonne->getEmail(),
string.setNum( pPersonne->getID() ) );
Datas::g_listPersonnes.push_back( pPersonne );
}
}
}
catch( DOMException &e )
{
qDebug( XMLString::transcode( e.getMessage() ) );
}
return 0;
}
int CApp::loadXercesDataFamille( dlgAmonSoft &amon )
{
try {
XERCES_CPP_NAMESPACE::DOMDocument *pDoc = g_ParserFamille->getDocument();
XERCES_CPP_NAMESPACE::DOMElement *pElem = pDoc->getDocumentElement();
CheckDataFileVersion( pElem, amon );
DOMNodeList *pNodeList = pElem->getChildNodes ();
uint size = pNodeList->getLength();
for( uint i = 0; i < size; i++ )
{
DOMNode *pNode = pNodeList->item( i );
if( XMLString::equals( XMLString::transcode( pNode->getNodeName() ), "Famille" ) )
{
Datas::CFamille *pFamille = new Datas::CFamille( 0 );
DOMElement *elmt = static_cast< DOMElement* >( pNode );
mAssert( elmt != 0, "elmt != 0" );
// loadFromXML has to link with correct CPersonne, this means
// that all CPersonne *must* be loaded before !
int iRet = pFamille->loadFromXML( elmt );
if( iRet == -1 )
delete pFamille;
}
}
}
catch( DOMException &e )
{
qDebug( XMLString::transcode( e.getMessage() ) );
}
return 0;
}
int CApp::loadXercesDataEntses( dlgAmonSoft &amon )
{
try {
XERCES_CPP_NAMESPACE::DOMDocument *pDoc = g_ParserEntses->getDocument();
XERCES_CPP_NAMESPACE::DOMElement *pElem = pDoc->getDocumentElement();
CheckDataFileVersion( pElem, amon );
DOMNodeList *pNodeList = pElem->getChildNodes ();
uint size = pNodeList->getLength();
for( uint i = 0; i < size; i++ )
{
DOMNode *pNode = pNodeList->item( i );
if( XMLString::equals( XMLString::transcode( pNode->getNodeName() ), "Entreprise" ) )
{
QString string;
Datas::CEntreprise *pEntreprise = new Datas::CEntreprise;
DOMElement *elmt = static_cast< DOMElement* >( pNode );
mAssert( elmt != 0, "elmt != 0" );
pEntreprise->loadFromXML( elmt );
QListView *listView = amon.getListViewEntses();
QListViewItem *item = new QListViewItem( listView,
pEntreprise->getRaisonSociale(),
pEntreprise->getNumeroSiret(),
string.setNum( pEntreprise->getID() ) );
Datas::g_listEntreprises.push_back( pEntreprise );
}
}
}
catch( DOMException &e )
{
qDebug( XMLString::transcode( e.getMessage() ) );
}
return 0;
}
/*
* Save datas to XML
*/
void CApp::saveXercesData()
{
using namespace Datas;
g_Parser->initWrite( "ListPersonnes", CONF_VERSION );
g_ParserFamille->initWrite( "ListFamilles", CONF_VERSION );
// Search item in the global list
// TODO: Try to find more efficient method
std::list< CPersonnes* >::const_iterator ite;
for( ite = g_listPersonnes.begin();
ite != g_listPersonnes.end(); ite++ )
{
// Write CPersonne and call function to save CFamille too
(*ite)->saveToXML( g_Parser->getDocument(), g_ParserFamille->getDocument() );
}
g_ParserFamille->writeFile();
g_Parser->writeFile();
// List of societies
g_ParserEntses->initWrite( "ListeEntreprises", CONF_VERSION );
// Search item in the global list
// TODO: Try to find more efficient method
std::list< CEntreprise* >::const_iterator ites;
for( ites = g_listEntreprises.begin();
ites != g_listEntreprises.end(); ites++ )
{
// Write
(*ites)->saveToXML( g_ParserEntses->getDocument() );
}
g_ParserEntses->writeFile( );
g_Preferences->saveToXerces();
}
//----------------------------------------------------------------------------
// Make copy of daya file
// TODO:Memory leak probably
//----------------------------------------------------------------------------
void CApp::makeBackup()
{
try
{
QStringList listStr;
listStr << sPersonnesFile
<< sEntreprisesFile
<< sFamilleFile
<< sSearchFile
<< "./xml/preferences.xml"; //TODO: Share prefernce filename
QStringList::Iterator ite = listStr.begin();
for( ; ite != listStr.end(); ite++ )
{
QUrlOperator *op = new QUrlOperator();
QString src = QDir::currentDirPath() + *ite;
QString target = QDir::currentDirPath() + *ite + ".bak";
op->copy( src, target, false, false );
}
}
catch(...)
{
qDebug( "Impossible de sauvegarder les fichiers" );
}
}