6 #include "filesystem.h"
12 #include "mainindexes.h"
13 #include "mainprogress.h"
16 #include "mwindowgui.h"
17 #include "preferences.h"
22 MainIndexes::MainIndexes(MWindow *mwindow)
26 this->mwindow = mwindow;
27 input_lock = new Condition(0, "MainIndexes::input_lock");
28 next_lock = new Mutex("MainIndexes::next_lock");
29 interrupt_lock = new Condition(1, "MainIndexes::interrupt_lock");
32 indexfile = new IndexFile(mwindow);
35 MainIndexes::~MainIndexes()
37 mwindow->mainprogress->cancelled = 1;
42 delete interrupt_lock;
45 void MainIndexes::add_next_asset(File *file, Asset *asset)
47 next_lock->lock("MainIndexes::add_next_asset");
50 IndexFile indexfile(mwindow);
54 if(!indexfile.open_index(asset))
56 asset->index_status = INDEX_READY;
57 indexfile.close_index();
63 File *this_file = file;
68 this_file->open_file(mwindow->preferences,
76 char index_filename[BCTEXTLEN];
77 char source_filename[BCTEXTLEN];
78 IndexFile::get_index_filename(source_filename,
79 mwindow->preferences->index_directory,
82 if(!this_file->get_index(index_filename))
84 if(!indexfile.open_index(asset))
86 indexfile.close_index();
87 asset->index_status = INDEX_READY;
91 if(!file) delete this_file;
95 // Put copy of asset in stack, not the real thing.
98 //printf("MainIndexes::add_next_asset 3\n");
99 Asset *new_asset = new Asset;
101 // If the asset existed and was overwritten, the status will be READY.
102 new_asset->index_status = INDEX_NOTTESTED;
103 next_assets.append(new_asset);
109 void MainIndexes::delete_current_assets()
111 for(int i = 0; i < current_assets.total; i++)
112 Garbage::delete_object(current_assets.values[i]);
113 current_assets.remove_all();
116 void MainIndexes::start_loop()
122 void MainIndexes::stop_loop()
126 input_lock->unlock();
127 interrupt_lock->unlock();
132 void MainIndexes::start_build()
134 //printf("MainIndexes::start_build 1\n");
136 // Locked up when indexes were already being built and an asset was
138 // interrupt_lock.lock();
139 input_lock->unlock();
142 void MainIndexes::interrupt_build()
144 //printf("MainIndexes::interrupt_build 1\n");
146 indexfile->interrupt_index();
147 //printf("MainIndexes::interrupt_build 2\n");
148 interrupt_lock->lock("MainIndexes::interrupt_build");
149 //printf("MainIndexes::interrupt_build 3\n");
150 interrupt_lock->unlock();
151 //printf("MainIndexes::interrupt_build 4\n");
154 void MainIndexes::load_next_assets()
156 delete_current_assets();
158 // Transfer from new list
159 next_lock->lock("MainIndexes::load_next_assets");
160 for(int i = 0; i < next_assets.total; i++)
161 current_assets.append(next_assets.values[i]);
163 // Clear pointers from new list only
164 next_assets.remove_all();
169 void MainIndexes::run()
173 // Wait for new assets to be released
174 input_lock->lock("MainIndexes::run 1");
176 interrupt_lock->lock("MainIndexes::run 2");
185 // test index of each asset
186 MainProgressBar *progress = 0;
187 for(int i = 0; i < current_assets.total && !interrupt_flag; i++)
189 Asset *current_asset = current_assets.values[i];
190 //printf("MainIndexes::run 3 %s %d %d\n", current_asset->path, current_asset->index_status, current_asset->audio_data);
192 if(current_asset->index_status == INDEX_NOTTESTED &&
193 current_asset->audio_data)
200 // Doesn't exist if this returns 1.
201 if(indexfile->open_index(current_asset))
203 // Try to create index now.
206 if(mwindow->gui) mwindow->gui->lock_window("MainIndexes::run 1");
207 progress = mwindow->mainprogress->start_progress(_("Building Indexes..."), 1);
208 if(mwindow->gui) mwindow->gui->unlock_window();
211 //printf("MainIndexes::run 5 %p %s\n", current_asset, current_asset->path);
213 indexfile->create_index(current_asset, progress);
214 //printf("MainIndexes::run 6 %p %s\n", current_asset, current_asset->path);
215 if(progress->is_cancelled()) interrupt_flag = 1;
216 //printf("MainIndexes::run 7 %p %s\n", current_asset, current_asset->path);
219 // Exists. Update real thing.
221 //printf("MainIndexes::run 8\n");
222 if(current_asset->index_status == INDEX_NOTTESTED)
224 current_asset->index_status = INDEX_READY;
225 if(mwindow->gui) mwindow->gui->lock_window("MainIndexes::run 2");
226 mwindow->edl->set_index_file(current_asset);
227 if(mwindow->gui) mwindow->gui->unlock_window();
229 indexfile->close_index();
233 //printf("MainIndexes::run 8\n");
235 //printf("MainIndexes::run 9\n");
238 if(progress) // progress box is only created when an index is built
240 if(mwindow->gui) mwindow->gui->lock_window("MainIndexes::run 3");
241 progress->stop_progress();
243 if(mwindow->gui) mwindow->gui->unlock_window();
252 interrupt_lock->unlock();