Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members

mainindexes.C

Go to the documentation of this file.
00001 #include "asset.h"
00002 #include "defaults.h"
00003 #include "edl.h"
00004 #include "file.h"
00005 #include "filesystem.h"
00006 #include "indexfile.h"
00007 #include "condition.h"
00008 #include "language.h"
00009 #include "loadfile.h"
00010 #include "guicast.h"
00011 #include "mainindexes.h"
00012 #include "mainprogress.h"
00013 #include "mwindow.h"
00014 #include "mwindowgui.h"
00015 #include "preferences.h"
00016 
00017 #include <string.h>
00018 
00019 
00020 MainIndexes::MainIndexes(MWindow *mwindow)
00021  : Thread()
00022 {
00023         set_synchronous(1);
00024         this->mwindow = mwindow;
00025         input_lock = new Condition(0, "MainIndexes::input_lock");
00026         next_lock = new Mutex("MainIndexes::next_lock");
00027         interrupt_lock = new Condition(1, "MainIndexes::interrupt_lock");
00028         interrupt_flag = 0;
00029         done = 0;
00030         indexfile = new IndexFile(mwindow);
00031 }
00032 
00033 MainIndexes::~MainIndexes()
00034 {
00035         mwindow->mainprogress->cancelled = 1;
00036         stop_loop();
00037         delete indexfile;
00038         delete next_lock;
00039         delete input_lock;
00040         delete interrupt_lock;
00041 }
00042 
00043 void MainIndexes::add_next_asset(File *file, Asset *asset)
00044 {
00045         next_lock->lock("MainIndexes::add_next_asset");
00046 
00047 // Test current asset
00048         IndexFile indexfile(mwindow);
00049 
00050         int got_it = 0;
00051 
00052         if(!indexfile.open_index(asset))
00053         {
00054                 asset->index_status = INDEX_READY;
00055                 indexfile.close_index();
00056                 got_it = 1;
00057         }
00058 
00059         if(!got_it)
00060         {
00061                 File *this_file = file;
00062 
00063                 if(!file)
00064                 {
00065                         this_file = new File;
00066                         this_file->open_file(mwindow->preferences,
00067                                 asset,
00068                                 1,
00069                                 0,
00070                                 0,
00071                                 0);
00072                 }
00073 
00074                 char index_filename[BCTEXTLEN];
00075                 char source_filename[BCTEXTLEN];
00076                 IndexFile::get_index_filename(source_filename, 
00077                         mwindow->preferences->index_directory, 
00078                         index_filename, 
00079                         asset->path);
00080                 if(!this_file->get_index(index_filename))
00081                 {
00082                         if(!indexfile.open_index(asset))
00083                         {
00084                                 indexfile.close_index();
00085                                 asset->index_status = INDEX_READY;
00086                                 got_it = 1;
00087                         }
00088                 }
00089                 if(!file) delete this_file;
00090         }
00091 
00092 
00093 // Put copy of asset in stack, not the real thing.
00094         if(!got_it)
00095         {
00096 //printf("MainIndexes::add_next_asset 3\n");
00097                 Asset *new_asset = new Asset;
00098                 *new_asset = *asset;
00099 // If the asset existed and was overwritten, the status will be READY.
00100                 new_asset->index_status = INDEX_NOTTESTED;
00101                 next_assets.append(new_asset);
00102         }
00103 
00104         next_lock->unlock();
00105 }
00106 
00107 void MainIndexes::delete_current_assets()
00108 {
00109         current_assets.remove_all_objects();
00110 }
00111 
00112 void MainIndexes::start_loop()
00113 {
00114         interrupt_flag = 0;
00115         Thread::start();
00116 }
00117 
00118 void MainIndexes::stop_loop()
00119 {
00120         interrupt_flag = 1;
00121         done = 1;
00122         input_lock->unlock();
00123         interrupt_lock->unlock();
00124         Thread::join();
00125 }
00126 
00127 
00128 void MainIndexes::start_build()
00129 {
00130 //printf("MainIndexes::start_build 1\n");
00131         interrupt_flag = 0;
00132 // Locked up when indexes were already being built and an asset was 
00133 // pasted.
00134 //      interrupt_lock.lock();
00135         input_lock->unlock();
00136 }
00137 
00138 void MainIndexes::interrupt_build()
00139 {
00140 //printf("MainIndexes::interrupt_build 1\n");
00141         interrupt_flag = 1;
00142         indexfile->interrupt_index();
00143 //printf("MainIndexes::interrupt_build 2\n");
00144         interrupt_lock->lock("MainIndexes::interrupt_build");
00145 //printf("MainIndexes::interrupt_build 3\n");
00146         interrupt_lock->unlock();
00147 //printf("MainIndexes::interrupt_build 4\n");
00148 }
00149 
00150 void MainIndexes::load_next_assets()
00151 {
00152         delete_current_assets();
00153 
00154 // Transfer from new list
00155         next_lock->lock("MainIndexes::load_next_assets");
00156         for(int i = 0; i < next_assets.total; i++)
00157                 current_assets.append(next_assets.values[i]);
00158 
00159 // Clear pointers from new list only
00160         next_assets.remove_all();
00161         next_lock->unlock();
00162 }
00163 
00164 
00165 void MainIndexes::run()
00166 {
00167         while(!done)
00168         {
00169 // Wait for new assets to be released
00170                 input_lock->lock("MainIndexes::run 1");
00171                 if(done) return;
00172                 interrupt_lock->lock("MainIndexes::run 2");
00173                 load_next_assets();
00174                 interrupt_flag = 0;
00175 
00176 
00177 
00178 
00179 
00180 
00181 // test index of each asset
00182                 MainProgressBar *progress = 0;
00183                 for(int i = 0; i < current_assets.total && !interrupt_flag; i++)
00184                 {
00185                         Asset *current_asset = current_assets.values[i];
00186 //printf("MainIndexes::run 3 %s %d %d\n", current_asset->path, current_asset->index_status, current_asset->audio_data);
00187 
00188                         if(current_asset->index_status == INDEX_NOTTESTED && 
00189                                 current_asset->audio_data)
00190                         {
00191 
00192 
00193 
00194 
00195 
00196 // Doesn't exist if this returns 1.
00197                                 if(indexfile->open_index(current_asset))
00198                                 {
00199 // Try to create index now.
00200                                         if(!progress)
00201                                         {
00202                                                 if(mwindow->gui) mwindow->gui->lock_window("MainIndexes::run 1");
00203                                                 progress = mwindow->mainprogress->start_progress(_("Building Indexes..."), 1);
00204                                                 if(mwindow->gui) mwindow->gui->unlock_window();
00205                                         }
00206 
00207 //printf("MainIndexes::run 5 %p %s\n", current_asset, current_asset->path);
00208 
00209                                         indexfile->create_index(current_asset, progress);
00210 //printf("MainIndexes::run 6 %p %s\n", current_asset, current_asset->path);
00211                                         if(progress->is_cancelled()) interrupt_flag = 1;
00212 //printf("MainIndexes::run 7 %p %s\n", current_asset, current_asset->path);
00213                                 }
00214                                 else
00215 // Exists.  Update real thing.
00216                                 {
00217 //printf("MainIndexes::run 8\n");
00218                                         if(current_asset->index_status == INDEX_NOTTESTED)
00219                                         {
00220                                                 current_asset->index_status = INDEX_READY;
00221                                                 if(mwindow->gui) mwindow->gui->lock_window("MainIndexes::run 2");
00222                                                 mwindow->edl->set_index_file(current_asset);
00223                                                 if(mwindow->gui) mwindow->gui->unlock_window();
00224                                         }
00225                                         indexfile->close_index();
00226                                 }
00227 
00228 
00229 //printf("MainIndexes::run 8\n");
00230                         }
00231 //printf("MainIndexes::run 9\n");
00232                 }
00233 
00234                 if(progress)     // progress box is only created when an index is built
00235                 {
00236                         if(mwindow->gui) mwindow->gui->lock_window("MainIndexes::run 3");
00237                         progress->stop_progress();
00238                         delete progress;
00239                         if(mwindow->gui) mwindow->gui->unlock_window();
00240                         progress = 0;
00241                 }
00242 
00243 
00244 
00245 
00246 
00247 
00248                 interrupt_lock->unlock();
00249         }
00250 }
00251 

Generated on Sun Jan 8 13:38:56 2006 for Cinelerra-svn by  doxygen 1.4.4