]> Creatis software - creaImageIO.git/blob - src2/creaImageIOGimmick.cpp
Modified due to problems in linux.
[creaImageIO.git] / src2 / creaImageIOGimmick.cpp
1 #include <creaImageIOGimmick.h>
2
3 #include <creaImageIOSystem.h>
4 #include <boost/filesystem.hpp>
5 #include <boost/algorithm/string.hpp>
6
7 #ifndef PATH_MAX // If not defined yet : do it 
8 #  define PATH_MAX 2048
9 #endif
10
11 namespace creaImageIO
12 {
13
14
15   //==============================================================
16   Gimmick::Gimmick()
17     : mImageAdder(0)
18   {    
19     RegisterGimmickMessageTypes();
20         mSettings=0;
21         mSynchronizer=0;
22   }
23   //==============================================================
24
25
26
27   //==============================================================
28   Gimmick::~Gimmick()
29   {
30          if(mSettings!=0)
31           {
32           mSettings->writeSettingsFile();
33       delete mSettings;
34           }
35           if(mSynchronizer!=0)
36           {
37           delete mSynchronizer;
38           }
39   }
40   //==============================================================
41   
42
43   //==============================================================
44   void Gimmick::Initialize()
45   {
46         std::string i_nameDB = "Local database";
47     // Create the UserSettings dir if does not exist
48     CreateUserSettingsDirectory();
49     // Sets the current directory to the home dir
50     mCurrentDirectory =  GetHomeDirectory();
51         mSynchronizer= new Synchronizer(GetUserSettingsDirectory());
52
53         mSettings  = new Settings(mCurrentDirectory);
54         
55
56         std::string dbpath = GetLocalDatabasePath();
57     // Create or open local database
58         std::string dpath= mCurrentDirectory + "/.gimmick/localdatabase_Descriptor.txt";
59         boost::algorithm::replace_all( dpath,
60                                        INVALID_FILE_SEPARATOR , 
61                                        VALID_FILE_SEPARATOR);
62         mLocalDatabase = createDB(i_nameDB, dpath, dbpath);
63     // Add it to the TreeHandlerMap
64     mTreeHandlerMap[i_nameDB] = mLocalDatabase;
65     
66         //Add additional DB from user Settings
67         addDBSettings();
68
69         // Creates files and directories database
70     mTimestampDatabase = new TimestampDatabaseHandler(GetTimestampDatabasePath());
71     // Create or open local database
72     if (! boost::filesystem::exists( GetTimestampDatabasePath() ) )
73       {
74         std::string mess = "Timestamp database '";
75         mess += GetTimestampDatabasePath();
76         mess += "' does not exist : creating it";
77         GimmickMessage(1,mess<<std::endl);
78         
79         if ( ! mTimestampDatabase->Create() )
80           {
81             GimmickError("ERROR CREATING '"<<GetTimestampDatabasePath()<<"'");
82           }
83         
84      }
85     else 
86       {
87         /// Open and test it
88         GimmickMessage(1,"Opening Timestamp database '"
89                        <<GetTimestampDatabasePath()<<"' "
90                        <<std::endl);
91         if ( ! mTimestampDatabase->Open() )
92           {
93             GimmickError("ERROR OPENING '"<<GetTimestampDatabasePath()<<"'");
94           }
95         
96       }
97
98   }
99
100    ///////////////////////////////////////////////////////////////////////
101    // add DB to TreeHandler Map                                                                                 //
102    // @param i_name : DB name                                                                               //
103    // @param i_location : DB location                                                               //
104    // return : -                                                                                                                //
105   ///////////////////////////////////////////////////////////////////////
106         void Gimmick::addDB(const std::string &i_name, 
107                             const std::string &i_location)
108         {
109                 if(mTreeHandlerMap.find(i_name) == mTreeHandlerMap.end())
110                 {
111                         mTreeHandlerMap[i_name] = new SQLiteTreeHandler(i_location);
112                         mTreeHandlerMap[i_name]->Open(true);
113                         mSettings->addDB(i_location);
114                 }
115         }
116
117  
118   ///////////////////////////////////////////////////////////////////////////
119   // create a DB from a attributes descriptor file for medical images      //
120   // @param i_name : DB name                                                                                       //
121   // @param i_locDesc : location of descriptor file                                                //
122   // @param i_locDB : location of DB                                                                       //
123   // return : the SQLiteTreeHandler object on DB                                                   //
124         /////////////////////////////////////////////////////////////////////////
125         SQLiteTreeHandler *Gimmick::createDB(const std::string &i_name,
126                                              const std::string &i_locDesc,
127                                              const std::string &i_locDB)
128   {
129       SQLiteTreeHandler *sqlTreeH = new SQLiteTreeHandler(i_locDB);
130     // Create or open local database
131     if (! boost::filesystem::exists(i_locDB) )
132      {
133          std::string mess = "Local database '";
134          mess += i_locDB;
135          mess += "' does not exist : creating it";
136          GimmickMessage(1,mess<<std::endl);
137          
138                  // CREATING DB STRUCTURE
139          sqlTreeH->GetTree().GetDescriptor().createDescriptorfromFile(i_locDesc);
140          if ( ! sqlTreeH->Create(true) )
141                  {
142                         GimmickError("ERROR CREATING '"<<i_locDB<<"'");
143          }
144          sqlTreeH->SetAttribute(0,"Name",i_name);
145          }
146          else 
147          {
148                 /// Open and test it
149                 GimmickMessage(1,"Opening local database '" <<i_locDB<< "' " << std::endl);
150         if ( !sqlTreeH->Open(true) )
151                 {
152                         GimmickError("ERROR OPENING '"<<i_locDB<<"'");
153                 }
154       }
155          return sqlTreeH;
156   }
157
158
159   //==============================================================
160   void Gimmick::Finalize()
161   {
162          
163           // delete SQLiteTreeHandler Object
164            for( TreeHandlerMapType::const_iterator it = mTreeHandlerMap.begin();
165            it!= mTreeHandlerMap.end(); ++it)
166            {
167                    delete it->second;
168            }
169         delete mTimestampDatabase;
170   }
171   //==============================================================
172
173   //================================================================
174   // file separator
175 #if defined(_WIN32)
176 #define VALID_FILE_SEPARATOR "\\"
177 #define INVALID_FILE_SEPARATOR "/"
178 #else
179 #define INVALID_FILE_SEPARATOR "\\"
180 #define VALID_FILE_SEPARATOR "/"
181 #endif
182   //================================================================
183
184   //================================================================
185   const std::string& Gimmick::GetHomeDirectory()
186   {
187     if (mHomeDirectory.size()==0) 
188       {
189 #if defined(__GNUC__)
190         mHomeDirectory = getenv("HOME");
191 #elif defined(_WIN32)
192         mHomeDirectory = getenv("USERPROFILE");
193 #endif
194       }
195     return mHomeDirectory;
196   }
197   //================================================================
198   const std::string& Gimmick::GetUserSettingsDirectory()
199   {
200     if (mUserSettingsDirectory.size()==0) 
201       {
202         mUserSettingsDirectory = GetHomeDirectory();
203         mUserSettingsDirectory += "/.gimmick/";
204         boost::algorithm::replace_all( mUserSettingsDirectory, 
205                                        INVALID_FILE_SEPARATOR , 
206                                        VALID_FILE_SEPARATOR);
207       }
208     return mUserSettingsDirectory;
209   }
210   //================================================================
211
212   //================================================================
213   int Gimmick::GetBinaryDirectory(char *pname, size_t pathsize)
214   {
215
216         #ifdef LINUX    
217                 /* Oddly, the readlink(2) man page says no NULL is appended. */
218                 /* So you have to do it yourself, based on the return value: */
219                 pathsize --; /* Preserve a space to add the trailing NULL */
220                 long result = readlink("/proc/self/exe", pname, pathsize);
221                 if (result > 0)
222                 {
223                         pname[result] = 0; /* add the #@!%ing NULL */
224                         
225                         if ((access(pname, 0) == 0))
226                                 return 0; /* file exists, return OK */
227                         /*else name doesn't seem to exist, return FAIL (falls
228                         through) */
229                 }
230         #endif /* LINUX */
231             
232         #ifdef WIN32
233                 long result = GetModuleFileName(NULL, pname, pathsize);
234                 if (result > 0)
235                 {
236                         /* fix up the dir slashes... */
237                         int len = strlen(pname);
238                         int idx;
239                         for (idx = 0; idx < len; idx++)
240                         {
241                                 if (pname[idx] == '\\') pname[idx] = '/';
242                         }
243                         
244                         for (idx = len-1; idx >=0 ; idx--)
245                         {
246                                 if (pname[idx] == '/')
247                                 { 
248                                         pname[idx+1] = '\0';
249                                         idx = -1;
250                                 }
251                         }
252                         
253                         if ((access(pname, 0) == 0))
254                                 return 0; /* file exists, return OK */
255                         /*else name doesn't seem to exist, return FAIL (falls
256                         through) */
257                 }
258         #endif /* WIN32 */
259             
260         #ifdef SOLARIS
261                 char *p = getexecname();
262                 if (p)
263                 {
264                         /* According to the Sun manpages, getexecname will
265                         "normally" return an */
266                         /* absolute path - BUT might not... AND that IF it is not,
267                         pre-pending */
268                         /* getcwd() will "usually" be the correct thing... Urgh!
269                         */
270                         
271                         /* check pathname is absolute (begins with a / ???) */
272                         if (p[0] == '/') /* assume this means we have an
273                         absolute path */
274                         {
275                                 strncpy(pname, p, pathsize);
276                                 if ((access(pname, 0) == 0))
277                                         return 0; /* file exists, return OK */
278                         }
279                         else /* if not, prepend getcwd() then check if file
280                         exists */
281                         {
282                                 getcwd(pname, pathsize);
283                                 long result = strlen(pname);
284                                 strncat(pname, "/", (pathsize - result));
285                                 result ++;
286                                 strncat(pname, p, (pathsize - result));
287                                 
288                                 if ((access(pname, 0) == 0))
289                                         return 0; /* file exists, return OK */
290                                 /*else name doesn't seem to exist, return FAIL
291                                 (falls through) */
292                         }
293                 }
294         #endif /* SOLARIS */
295             
296         #ifdef MACOSX /* assume this is OSX */
297                 /*
298                 from http://www.hmug.org/man/3/NSModule.html
299                  
300                 extern int _NSGetExecutablePath(char *buf, unsigned long
301                 *bufsize);
302                  
303                 _NSGetExecutablePath  copies  the  path  of the executable
304                 into the buffer and returns 0 if the path was successfully
305                 copied  in the provided buffer. If the buffer is not large
306                 enough, -1 is returned and the  expected  buffer  size  is
307                 copied  in  *bufsize.  Note that _NSGetExecutablePath will
308                 return "a path" to the executable not a "real path" to the
309                 executable.  That  is  the path may be a symbolic link and
310                 not the real file. And with  deep  directories  the  total
311                 bufsize needed could be more than MAXPATHLEN.
312                 */
313                 
314                 int status = -1;
315                 char *given_path = (char*)malloc(MAXPATHLEN * 2);
316                 if (!given_path) return status;
317             
318                 uint32_t npathsize = MAXPATHLEN * 2;
319                 long result = _NSGetExecutablePath(given_path, &npathsize);
320                 if (result == 0)
321                 { /* OK, we got something - now try and resolve the real path...
322                 */
323                         if (realpath(given_path, pname) != NULL)
324                         {
325                                 if ((access(pname, 0) == 0))
326                                         status = 0; /* file exists, return OK */
327                         }
328                 }
329                 free (given_path);
330                 return status;
331         #endif /* MACOSX */
332             
333                 return -1; /* Path Lookup Failed */
334   }
335
336   //================================================================
337   const std::string& Gimmick::GetLocalDatabasePath()
338   {
339     if (mLocalDatabasePath.size()==0) 
340       {
341         mLocalDatabasePath = GetUserSettingsDirectory();
342         mLocalDatabasePath += "local_database.sqlite3";
343         boost::algorithm::replace_all( mLocalDatabasePath,
344                                        INVALID_FILE_SEPARATOR , 
345                                        VALID_FILE_SEPARATOR);
346       }
347     return mLocalDatabasePath;    
348   }
349
350   //================================================================
351
352   //================================================================
353   const std::string& Gimmick::GetTimestampDatabasePath()
354   {
355     if (mTimestampDatabasePath.size()==0) 
356       {
357         mTimestampDatabasePath = GetUserSettingsDirectory();
358         mTimestampDatabasePath += "timestamp_database.sqlite3";
359         boost::algorithm::replace_all( mTimestampDatabasePath,
360                                        INVALID_FILE_SEPARATOR , 
361                                        VALID_FILE_SEPARATOR);
362       }
363     return mTimestampDatabasePath;    
364   }
365   //========================================================================
366
367   //========================================================================
368   void Gimmick::CreateUserSettingsDirectory()
369   {
370     if (! boost::filesystem::is_directory( GetUserSettingsDirectory() ) )
371       {
372         GimmickMessage(1,"Directory '"<<GetUserSettingsDirectory()<<"' "
373                        << "does not exist : creating it"<<std::endl);
374         
375         if ( ! boost::filesystem::create_directory( GetUserSettingsDirectory() ) )
376           {
377             GimmickError("ERROR CREATING '"<<GetUserSettingsDirectory()<<"'");
378           }
379       }
380
381         std::string setDir=GetUserSettingsDirectory();
382         boost::algorithm::replace_all( setDir,
383                                        INVALID_FILE_SEPARATOR , 
384                                        VALID_FILE_SEPARATOR);
385         setDir+="localdatabase_Descriptor.txt";
386         //if(!boost::filesystem::is_regular_file(setDir))  //JPRX
387         if(!boost::filesystem::is_regular(setDir))
388         {
389                 char name[PATH_MAX];
390                 int err = GetBinaryDirectory(name, PATH_MAX);
391                 std::string path=name;
392                 path=path.substr(0,path.size()-1);
393                 path=path.substr(0,path.find_last_of("/"));
394                 path+="/data/localdatabase_Descriptor.txt";
395                 boost::algorithm::replace_all( path,
396                                                 INVALID_FILE_SEPARATOR , 
397                                                 VALID_FILE_SEPARATOR);
398                 boost::filesystem::copy_file(path,setDir);
399         }
400   }
401   //========================================================================
402
403
404   //========================================================================
405   /// Sets message level
406   void Gimmick::SetMessageLevel(int l)
407   {
408     SetGimmickMessageLevel(l);
409   }
410   //========================================================================
411
412   //========================================================================
413   /// Sets message level
414   void Gimmick::SetDebugMessageLevel(int l)
415   {
416     SetGimmickDebugMessageLevel(l);
417   }
418   //========================================================================
419
420   //========================================================================
421   /// Returns the tree handler with the given name
422   TreeHandler* Gimmick::GetTreeHandler(const std::string& name) const 
423   {  
424     TreeHandlerMapType::const_iterator i;
425     i = GetTreeHandlerMap().find(name);
426     if ( i == GetTreeHandlerMap().end() )
427       {
428         GimmickError("TreeHandler '"<<name<<"' does not exist");
429       }
430     return i->second;
431   }
432
433   //========================================================================
434   ///Returns the timestamp database handler
435   TimestampDatabaseHandler* Gimmick::GetTimestampDatabase() const 
436   {  
437     return mTimestampDatabase;
438   }
439
440
441   //========================================================================
442   /// Add the files to the tree handler
443   void Gimmick::AddFiles(const std::string& d, 
444                         const std::vector<std::string>& filenames)
445   {
446     GimmickMessage(2,"Adding files to '"<<d<<"'"<<std::endl);
447  
448         mImageAdder.SetCurrentDatabase(d);
449     mImageAdder.SetTreeHandler(GetTreeHandler(d));
450         mImageAdder.SetTimestampHandler(mTimestampDatabase);
451         mImageAdder.SetSynchronizer(mSynchronizer);
452     mImageAdder.AddFiles(filenames);
453         
454   }
455   //========================================================================
456
457   //========================================================================
458   /// Add a dir to the local database
459   void Gimmick::AddDir(const std::string& d, const std::string& f, 
460                        bool recurse)
461   {
462     GimmickMessage(2,"Adding dir '"<<f<<"' to '"<<d<<"' recurse:"
463                    <<recurse<<std::endl);
464
465         TreeHandler * handler=GetTreeHandler(d);
466         mImageAdder.SetCurrentDatabase(d);
467     mImageAdder.SetTreeHandler(handler);
468         mImageAdder.SetTimestampHandler(mTimestampDatabase);
469         mImageAdder.SetSynchronizer(mSynchronizer);
470     mImageAdder.AddDirectory(f,recurse);  
471   }
472
473   //========================================================================
474
475   //========================================================================
476   void Gimmick::RemoveFile(const std::string& d, 
477                            tree::Node* node)
478   {
479           mImageAdder.SetCurrentDatabase(d);
480           mImageAdder.SetSynchronizer(mSynchronizer);
481           mTimestampDatabase->RemoveNode("PATH",node,d);
482           mImageAdder.RemoveFile(node);
483   }
484   //========================================================================
485
486   //========================================================================
487  
488   void Gimmick::CopyFiles(const std::vector<std::string>& filenames, const std::string& d )
489   {
490           TreeHandler * handler=GetTreeHandler(d);
491           mImageAdder.SetCurrentDatabase(d);
492           mImageAdder.SetTreeHandler(handler);
493           mImageAdder.SetTimestampHandler(mTimestampDatabase);
494           mImageAdder.SetSynchronizer(mSynchronizer);
495           mImageAdder.CopyFiles(filenames, mSettings->getValue(SETTINGS_COPY_PATH));
496   }
497
498   //========================================================================
499  
500   std::string Gimmick::Synchronize(const std::string& d, bool repair, bool checkAttributes)
501   {
502           TreeHandler * handler=GetTreeHandler(d);
503           mImageAdder.SetCurrentDatabase(d);
504           mImageAdder.SetTreeHandler(handler);
505           mImageAdder.SetTimestampHandler(mTimestampDatabase);
506           mImageAdder.SetSynchronizer(mSynchronizer);
507           return mImageAdder.Synchronize(repair, checkAttributes);
508   }
509
510   //========================================================================
511   /// 
512   void Gimmick::Print(const std::string& d)
513   {
514     GetTreeHandler(d)->GetTree().Print();
515   }
516   //========================================================================
517
518   void Gimmick::GetSetting(const std::string& name, std::string& value)
519   {
520           value = mSettings->getValue(name);
521   }
522   //========================================================================
523
524   //========================================================================
525
526   void Gimmick::GetAttributes(const std::string& d, 
527           const std::string& filename, 
528           const std::vector<std::string>& params, 
529           std::vector<std::string>& results)
530   {
531           TreeHandler * handler=GetTreeHandler(d);
532           mImageAdder.SetCurrentDatabase(d);
533           mImageAdder.SetTreeHandler(handler);
534           mImageAdder.SetTimestampHandler(mTimestampDatabase);
535           mImageAdder.SetSynchronizer(mSynchronizer);
536           mImageAdder.GetAttributes(params, filename, results);
537   }
538   //========================================================================
539
540   //========================================================================
541
542   void Gimmick::UpdateSetting(const std::string& name, const std::string& value)
543   {
544           mSettings->updateSetting(name,value);
545           mSettings->writeSettingsFile();
546   }
547   //========================================================================
548
549   void Gimmick::DeleteDrive(const std::string& drive)
550   {
551           for( TreeHandlerMapType::const_iterator it = mTreeHandlerMap.begin();
552            it!= mTreeHandlerMap.end(); ++it)
553            {
554                    mImageAdder.SetTreeHandler(it->second);
555                    mImageAdder.DeleteDriveFromMainDB(drive);
556            }
557           mImageAdder.SetTimestampHandler(mTimestampDatabase);
558           mImageAdder.SetSynchronizer(mSynchronizer);
559           mImageAdder.DeleteDriveFromOtherDB(drive);
560   }
561
562   //========================================================================
563   void Gimmick::EditField(tree::Node* node, const std::string& d, const std::string& name, const std::string& key, const std::string& val)
564   {
565           TreeHandler * handler=GetTreeHandler(d);
566           mImageAdder.SetCurrentDatabase(d);
567           mImageAdder.SetTreeHandler(handler);
568           mImageAdder.EditField(node,name,key,val);
569   }
570   //========================================================================
571
572
573   /////////////////////////////////////////////////////////////////////////
574   // add DB from Settings file                                                                               //
575   // @param : -                                                                                                                  //
576   // return : -                                                                                                                  //
577   /////////////////////////////////////////////////////////////////////////
578   void Gimmick::addDBSettings()
579   {
580
581          std::string pathSettings = mSettings->getValue(SETTINGS_DBPATH);
582          
583          // split to find all paths
584          std::vector<std::string> paths;
585          std::string separator = ";";
586          std::string::size_type last_pos = pathSettings.find_first_not_of(separator);
587          //find first separator
588          std::string::size_type pos = pathSettings.find_first_of(separator, last_pos);
589          while(std::string::npos != pos || std::string::npos != last_pos)
590          {
591                 paths.push_back(pathSettings.substr(last_pos, pos - last_pos));
592                 last_pos = pathSettings.find_first_not_of(separator, pos);
593                 pos = pathSettings.find_first_of(separator, last_pos);
594          }
595
596          std::vector<std::string>::iterator it_path = paths.begin();
597          for(; it_path != paths.end(); ++it_path)
598          {
599                  pos = it_path->find_last_of("\\");
600                  last_pos = it_path->find_last_of(".");
601                  std::string name = it_path->substr(pos +1, last_pos -pos-1 );
602                  addDB(name, it_path->c_str());
603          }
604
605   }
606         
607 }