File: configSettings.cpp

package info (click to toggle)
massxpert 2.3.6-1squeeze1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 20,736 kB
  • ctags: 3,541
  • sloc: cpp: 44,108; xml: 7,381; sh: 604; makefile: 108; ansic: 7
file content (617 lines) | stat: -rw-r--r-- 17,336 bytes parent folder | download | duplicates (3)
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
/* massXpert - the true massist's program.
   --------------------------------------
   Copyright(C) 2006,2007 Filippo Rusconi

   http://www.massxpert.org/massXpert

   This file is part of the massXpert project.

   The massxpert project is the successor to the "GNU polyxmass"
   project that is an official GNU project package(see
   www.gnu.org). The massXpert project is not endorsed by the GNU
   project, although it is released ---in its entirety--- under the
   GNU General Public License. A huge part of the code in massXpert
   is actually a C++ rewrite of code in GNU polyxmass. As such
   massXpert was started at the Centre National de la Recherche
   Scientifique(FRANCE), that granted me the formal authorization to
   publish it under this Free Software License.

   This software is free software; you can redistribute it and/or
   modify it under the terms of the GNU  General Public
   License version 3, as published by the Free Software Foundation.
   

   This software is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this software; if not, write to the

   Free Software Foundation, Inc.,

   51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*/


/////////////////////// Qt includes
#include <QApplication>
#include <QDir>
#include <QDebug>

#ifdef Q_WS_MAC
#include <CoreFoundation/CoreFoundation.h>
#endif

/////////////////////// Local includes
#include "config.h"
#include "configSettings.hpp"



namespace massXpert
{

  //! Initializes the members of the class.
  /*! The initialization of the members of the class involves
    determining where the system and user configuration directories are
    located.
  
    \li System configuration directory: it is set by the variable
    MASSXPERT_DATA_DIR in the config.h file. Thus, this directory is set
    at compile time.

    \li User configuration directory: it is determined at run time by
    inspecting the value returned by the call to QDir::homePath
   (). Under Windows this function will return the directory of the
    current user's profile. Typically, this is: "C:\\Documents and
    Settings\\Username". Under non-Windows operating systems the HOME
    environment variable is used if it exists, otherwise the path
    returned by the rootPath() function is used(this latter function
    returns '/' on non-Windows systems and "c:\\" on Windows).
  
    Inside the User/System configuration directories a "pol-chem-defs"
    directory will contain a number of files:

    \li \c massxpert-pol-chem-defs-cat is a catalogue that relates
    polymer chemistry definition names with the corresponding
    directories where the definition data are to be found. For example,
    one line from that file might be:

    protein=/home/rusconi/.massxpert/pol-chem-defs/protein/protein.xml

    This line tells that the "protein" polymer chemistry definition file
    is named protein.xml and that it is located, along with all the
    other data files pertaining to that same polymer chemistry
    definition, in the /home/rusconi/.massxpert/pol-chem-defs/protein
    directory.

    During initialization, if system configuration directories are not
    found, then these are not created. Which is not the case for the
    user configuration directories: if these are not found, they are
    created, thus yielding ---on a UNIX-like system for example--- the
    following directories:

    $HOME/.massxpert and $HOME/.massxpert/pol-chem-defs

    The user might then populate these directories with data suiting her
    needs.

    \return true if initialization was successful, false otherwise.
  */
  bool 
  ConfigSettings::initializeSystemConfig()
  {
    QDir dir;

    // First off, some general concepts.

    // When the software is built, a configuration process will
    // provide values for a number of #defined variables, depending on
    // the platform on which the software is built.

    // For example on MS-Windows, the following variables are defined:

    /*
      #define MASSXPERT_BIN_DIR C:/Program Files/massxpert
      #define MASSXPERT_DATA_DIR C:/Program Files/massxpert/data
      #define MASSXPERT_PLUGIN_DIR C:/Program Files/massxpert/plugins
      #define MASSXPERT_LOCALE_DIR C:/Program Files/massxpert/locales
      #define MASSXPERT_USERMAN_DIR C:/Program Files/massxpert/doc/usermanual
    
    */

    // On GNU/Linux, instead, the following variables are defined:

    /*
      #define MASSXPERT_BIN_DIR /usr/local/bin
      #define MASSXPERT_DATA_DIR /usr/local/share/massxpert
      #define MASSXPERT_PLUGIN_DIR /usr/local/lib/massxpert/plugins
      #define MASSXPERT_LOCALE_DIR /usr/local/share/massxpert/locales
      #define MASSXPERT_USERMAN_DIR: /usr/local/share/doc/massxpert/usermanual
    */

    /* On Mac OS X, instead, the software is built using not cmake,
       but qmake, and that makes it buildable using Xcode. The build
       generates in this case a Mac Bundle massXpert.app, which
       installs anywhere as long as its internal structure is
       conserved. There is an API to identify from which location the
       bundle is used(double-clicking onto the massXpert.app
       icon). We use that API to get the bundle directory, which might
       be something like /Applications/massXpert.app if the bundle was
       dropped into /Applications.
    */

#if defined(Q_WS_MAC)
    CFURLRef bundleRef = CFBundleCopyBundleURL(CFBundleGetMainBundle());
    
    CFStringRef macPath = 
      CFURLCopyFileSystemPath(bundleRef, kCFURLPOSIXPathStyle);
    
    QString bundleDir = 
      CFStringGetCStringPtr(macPath, CFStringGetSystemEncoding());
    
    CFRelease(bundleRef);
    CFRelease(macPath);
#endif


    // Now, it might be of interest for the user to install the
    // software in another location than the canonical one. In this
    // case, the program should still be able to access the data.

    // This is what this function is for. This function will try to
    // establish if the data that were built along the software
    // package are actually located in the cnaonical places listed
    // above. If not, this function returns false. The caller might
    // then take actions to let the user manually instruct the program
    // of where the data are located.

    // All this procedure is setup so as to let the user install the
    // software wherever she wants.


      /////////////////// MASSXPERT_DATA_DIR ///////////////////
      /////////////////// MASSXPERT_DATA_DIR ///////////////////
      /////////////////// MASSXPERT_DATA_DIR ///////////////////
    
#ifdef Q_WS_MAC
    dir.setPath(bundleDir + QDir::separator() + "Contents" +
		 QDir::separator() + "Resources" +
		 QDir::separator() + "data");
#else
    dir.setPath(QString(MASSXPERT_DATA_DIR));
#endif

    // The path must be absolute and must exist.

    if (!dir.isAbsolute())
      return false;
    
    if (!dir.exists())
      return false;

    m_systemDataDir = dir.absolutePath();
    
    // OK, the directory exists, we still have to make sure that we
    // can find the proper data in it. Namely the directory where the
    // data are acutally stored(pol-chem-defs subdirectory).

    dir.setPath(dir.absolutePath() + 
		 QDir::separator() + "pol-chem-defs");
    
    if (!dir.exists())
      return false;

    m_systemPolChemDefCatDir = dir.absolutePath();

    // Finally, make sure the system catalogue file of all the polymer
    // chemistry definitions shipped in the program package, that sits
    // in the "pol-chem-defs" directory.

    QString filePath = dir.absolutePath() +
      QDir::separator() + "massxpert-pol-chem-defs-cat";
    
    if (!QFile::exists(filePath))
      return false;
    

      /////////////////// MASSXPERT_PLUGIN_DIR ///////////////////
      /////////////////// MASSXPERT_PLUGIN_DIR ///////////////////
      /////////////////// MASSXPERT_PLUGIN_DIR ///////////////////

#ifdef Q_WS_MAC
    dir.setPath(bundleDir + QDir::separator() + "Contents" +
		 QDir::separator() + "Plugins");
#else
    dir.setPath(QString(MASSXPERT_PLUGIN_DIR));
#endif
    
    // The path must be absolute and must exist.
    
    if (!dir.isAbsolute())
      return false;
    
    if (!dir.exists())
      return false;
    
    // At the moment there are three plugins:

    // libnumeralsLocaleConverterPlugin.so 
    
    // libmassListSorterPlugin.so
    
    // libseqToolsPlugin.so
    
    QString libname;
    
#ifdef Q_OS_LINUX
    libname = "libnumeralsLocaleConverterPlugin.so";
#elif defined Q_OS_DARWIN
    libname = "libnumeralsLocaleConverterPlugin.dylib";
#elif defined Q_OS_WIN32
    libname ="libnumeralsLocaleConverterPlugin.dll";
#endif
    
    filePath = QString(dir.absolutePath() +
			QDir::separator() +
			libname);
    
    // But in fact, we cannot assume that there are plugins.
    //    if (!QFile::exists(filePath))
    //  return false;

    m_systemPluginDir = dir.absolutePath();
    

      /////////////////// MASSXPERT_LOCALE_DIR ///////////////////
      /////////////////// MASSXPERT_LOCALE_DIR ///////////////////
      /////////////////// MASSXPERT_LOCALE_DIR ///////////////////

#ifdef Q_WS_MAC
    dir.setPath(bundleDir + QDir::separator() + "Contents" +
		 QDir::separator() + "Locales");
#else
    dir.setPath(QString(MASSXPERT_LOCALE_DIR));
#endif
    
    // The path must be absolute and must exist.

    if (!dir.isAbsolute())
      return false;
    
    if (!dir.exists())
      return false;
    
    // At the moment there is the french translation: massxpert_fr.qm
    
    filePath = QString(dir.absolutePath() +
			QDir::separator() +
			"massxpert_fr.qm");
    
    if (!QFile::exists(filePath))
      return false;
    
    m_systemLocalizationDir = dir.absolutePath();
    return true;
  }


  bool 
  ConfigSettings::initializeUserConfig()
  {
    QDir dir;


    // First off, some general concepts.

    // massXpert should let the user define any polymer chemistry of
    // his requirement, without needing the root priviledges for
    // installation of these new data.

    // This is why massXpert allows for personal data directories
    // modelled on the same filesystem scheme as for the system data
    // directory. 

    // When the program is run, it check if the .massxpert directory
    // exists in the home directory of the user running the
    // program. If that directory does not exist, it is created, along
    // with its pol-chem-defs directory.

    // On MS-Windows sytems, the home directory is located at:

    // C:\Documents and Settings\username

    // On GNU/Linux and UNIX-based systems, the home directory is:

    // /home/username
        
    // Thus, respectively, the user has at its disposal the following
    // personal user data directories:

    // C:\Documents and Settings\username\.massxpert\pol-chem-defs

    // and

    // /home/username/.massxpert/pol-chem-defs

    
    // Get the absolute directory path to user's data directory.
    
    dir.setPath(QDir::homePath() + QDir::separator() + ".massxpert");
    
    // Now make some checks: if this directory does not exist, we have
    // to create it.
    if (!dir.exists())
      {
	if(!dir.mkpath(dir.absolutePath()))
	  {
	    qWarning() << __FILE__ << __LINE__ 
		       << "Directory" << dir.absolutePath()
		       << "does not exist, but its creation failed.";
	    
	    return false;
	  }
	else
	  {
// 	    qWarning() << __FILE__ << __LINE__ 
// 			<< "Creation of " 
// 			<< dir.absolutePath()
// 			<< "succeeded.";
	  }
      }
  
    m_userDataDir = dir.absolutePath();
  
    dir.setPath(m_userDataDir + QDir::separator() + "pol-chem-defs");
    
    if (!dir.exists())
      {
	if(!dir.mkpath(dir.absolutePath()))
	  {
	    qWarning() << __FILE__ << __LINE__ 
		       << "Directory" << dir.absolutePath()
		       << "does not exist, but its creation failed.";
	    
	    return false;
	  }
	else
	  {
// 	    qWarning() << __FILE__ << __LINE__ 
// 			<< "Creation of " 
// 			<< dir.absolutePath()
// 			<< "succeeded.";
	  }
      }
    
    m_userPolChemDefCatDir = dir.absolutePath();
    
    
    dir.setPath(m_userDataDir + QDir::separator() + "plugins");
  
    if (!dir.exists())
      {
	if(!dir.mkpath(dir.absolutePath()))
	  {
	    qWarning() << __FILE__ << __LINE__ 
		       << "Directory" << dir.absolutePath()
		       << "does not exist, but its creation failed.";
	    
	    return false;
	  }
	else
	  {
// 	    qWarning() << __FILE__ << __LINE__ 
// 			<< "Creation of " 
// 			<< dir.absolutePath()
// 			<< "succeeded.";
	  }
      }
     
    m_userPluginDir = dir.absolutePath();

    return true;
  }


  //! Sets the system data directory path.
  /*! Sets the system data directory path to the string passed
    as argument.
  
    \param config string holding the new system data directory
    path.
  */
  void 
  ConfigSettings::setSystemDataDir(const QString &config)
  {
    m_systemDataDir = config;
  }

  //! Returns the system data directory path.
  /*! 
  
    \return a constant reference to the system data directory
    member data.
  */
  const QString &
  ConfigSettings::systemDataDir() const
  {
    return m_systemDataDir;
  }


  //! Sets the system plugin directory path.
  /*! Sets the system plugin directory path to the string passed
    as argument.
  
    \param config string holding the new system plugin directory
    path.
  */
  void 
  ConfigSettings::setSystemPluginDir(const QString &config)
  {
    m_systemPluginDir = config;
  }

  //! Returns the system plugin directory path.
  /*! 
  
    \return a constant reference to the system plugin directory
    member data.
  */
  const QString &
  ConfigSettings::systemPluginDir() const
  {
    return m_systemPluginDir;
  }
				   
  
  void 
  ConfigSettings::setSystemLocalizationDir(const QString &config)
  {
    m_systemLocalizationDir = config;
  }
  
  
  const QString &
  ConfigSettings::systemLocalizationDir() const
  {
    return m_systemLocalizationDir;
  }
  

  //! Sets the user manual directory path.
  /*! Sets the user manual directory path to the string passed
    as argument.
  
    \param config string holding the new user manual directory
    path.
  */
  void 
  ConfigSettings::setUserManDir(const QString &config)
  {
    m_userManDir = config;
  }

  //! Returns the user manual directory path.
  /*! 
  
    \return a constant reference to the user manual directory
    member data.
  */
  const QString &
  ConfigSettings::userManDir() const
  {
    return m_userManDir;
  }

  
  //! Sets the user plugin directory path.
  /*! Sets the user plugin directory path to the string passed as
    argument.
  
    \param config string holding the new user plugin directory
    path.
  */
  void 
  ConfigSettings::setUserPluginDir(const QString &config)
  {
    m_userPluginDir = config;
  }


  //! Returns the user plugin directory path.
  /*! 
  
    \return a constant reference to the user plugin directory
    member data.
  */
  const QString &
  ConfigSettings::userPluginDir() const
  {
    return m_userPluginDir;
  }
  

  //! Sets the user data directory path.
  /*! Sets the user data directory path to the string passed as
    argument.
  
    \param config string holding the new user data directory
    path.
  */
  void 
  ConfigSettings::setUserDataDir(const QString &config)
  {
    m_userDataDir = config;
  }


  //! Returns the user configuration directory path.
  /*! 
  
    \return a constant reference to the user configuration directory
    member data.
  */
  const QString &
  ConfigSettings::userDataDir() const
  {
    return m_userDataDir;
  }
  

  //! Sets a system configuration directory path.
  /*! Sets the system configuration directory path where the polymer
    chemistry definition catalogues are stored.
  
    \param config string holding the configuration directory path.
  */
  void 
  ConfigSettings::setSystemPolChemDefCatDir(const QString &config)
  {
    m_systemPolChemDefCatDir = config;
  }


  //! Returns a configuration directory path.
  /*! Returns the system configuration directory path where the polymer
    chemistry definition catalogues are stored.

    \return a constant reference to the configuration directory member
    data.
  */
  const QString &
  ConfigSettings::systemPolChemDefCatDir() const
  {
    return m_systemPolChemDefCatDir;
  }

  
  //! Sets a user configuration directory path.
  /*! Sets the user configuration directory path where the polymer
    chemistry definition catalogues are stored.
  
    \param config string holding the configuration directory path.
  */
  void 
  ConfigSettings::setUserPolChemDefCatDir(const QString &config)
  {
    m_userPolChemDefCatDir = config;
  }


  //! Returns a configuration directory path.
  /*! Returns the user configuration directory path where the polymer
    chemistry definition catalogues are stored.

    \return a constant reference to the configuration directory member
    data.
  */
  const QString &
  ConfigSettings::userPolChemDefCatDir() const
  {
    return m_userPolChemDefCatDir;
  }
 
} // namespace massXpert