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