5 #include "filesystem.h"
11 #include "mainindexes.h"
12 #include "mainprogress.h"
14 #include "mwindowgui.h"
15 #include "preferences.h"
20 MainIndexes::MainIndexes(MWindow *mwindow)
24 this->mwindow = mwindow;
25 input_lock = new Condition(0, "MainIndexes::input_lock");
26 next_lock = new Mutex("MainIndexes::next_lock");
27 interrupt_lock = new Condition(1, "MainIndexes::interrupt_lock");
30 indexfile = new IndexFile(mwindow);
33 MainIndexes::~MainIndexes()
35 mwindow->mainprogress->cancelled = 1;
40 delete interrupt_lock;
43 void MainIndexes::add_next_asset(File *file, Asset *asset)
45 next_lock->lock("MainIndexes::add_next_asset");
48 IndexFile indexfile(mwindow);
52 if(!indexfile.open_index(asset))
54 asset->index_status = INDEX_READY;
55 indexfile.close_index();
61 File *this_file = file;
66 this_file->open_file(mwindow->preferences,
74 char index_filename[BCTEXTLEN];
75 char source_filename[BCTEXTLEN];
76 IndexFile::get_index_filename(source_filename,
77 mwindow->preferences->index_directory,
80 if(!this_file->get_index(index_filename))
82 if(!indexfile.open_index(asset))
84 indexfile.close_index();
85 asset->index_status = INDEX_READY;
89 if(!file) delete this_file;
93 // Put copy of asset in stack, not the real thing.
96 //printf("MainIndexes::add_next_asset 3\n");
97 Asset *new_asset = new Asset;
99 // If the asset existed and was overwritten, the status will be READY.
100 new_asset->index_status = INDEX_NOTTESTED;
101 next_assets.append(new_asset);
107 void MainIndexes::delete_current_assets()
109 current_assets.remove_all_objects();
112 void MainIndexes::start_loop()
118 void MainIndexes::stop_loop()
122 input_lock->unlock();
123 interrupt_lock->unlock();
128 void MainIndexes::start_build()
130 //printf("MainIndexes::start_build 1\n");
132 // Locked up when indexes were already being built and an asset was
134 // interrupt_lock.lock();
135 input_lock->unlock();
138 void MainIndexes::interrupt_build()
140 //printf("MainIndexes::interrupt_build 1\n");
142 indexfile->interrupt_index();
143 //printf("MainIndexes::interrupt_build 2\n");
144 interrupt_lock->lock("MainIndexes::interrupt_build");
145 //printf("MainIndexes::interrupt_build 3\n");
146 interrupt_lock->unlock();
147 //printf("MainIndexes::interrupt_build 4\n");
150 void MainIndexes::load_next_assets()
152 delete_current_assets();
154 // Transfer from new list
155 next_lock->lock("MainIndexes::load_next_assets");
156 for(int i = 0; i < next_assets.total; i++)
157 current_assets.append(next_assets.values[i]);
159 // Clear pointers from new list only
160 next_assets.remove_all();
165 void MainIndexes::run()
169 // Wait for new assets to be released
170 input_lock->lock("MainIndexes::run 1");
172 interrupt_lock->lock("MainIndexes::run 2");
181 // test index of each asset
182 MainProgressBar *progress = 0;
183 for(int i = 0; i < current_assets.total && !interrupt_flag; i++)
185 Asset *current_asset = current_assets.values[i];
186 //printf("MainIndexes::run 3 %s %d %d\n", current_asset->path, current_asset->index_status, current_asset->audio_data);
188 if(current_asset->index_status == INDEX_NOTTESTED &&
189 current_asset->audio_data)
196 // Doesn't exist if this returns 1.
197 if(indexfile->open_index(current_asset))
199 // Try to create index now.
202 if(mwindow->gui) mwindow->gui->lock_window("MainIndexes::run 1");
203 progress = mwindow->mainprogress->start_progress(_("Building Indexes..."), 1);
204 if(mwindow->gui) mwindow->gui->unlock_window();
207 //printf("MainIndexes::run 5 %p %s\n", current_asset, current_asset->path);
209 indexfile->create_index(current_asset, progress);
210 //printf("MainIndexes::run 6 %p %s\n", current_asset, current_asset->path);
211 if(progress->is_cancelled()) interrupt_flag = 1;
212 //printf("MainIndexes::run 7 %p %s\n", current_asset, current_asset->path);
215 // Exists. Update real thing.
217 //printf("MainIndexes::run 8\n");
218 if(current_asset->index_status == INDEX_NOTTESTED)
220 current_asset->index_status = INDEX_READY;
221 if(mwindow->gui) mwindow->gui->lock_window("MainIndexes::run 2");
222 mwindow->edl->set_index_file(current_asset);
223 if(mwindow->gui) mwindow->gui->unlock_window();
225 indexfile->close_index();
229 //printf("MainIndexes::run 8\n");
231 //printf("MainIndexes::run 9\n");
234 if(progress) // progress box is only created when an index is built
236 if(mwindow->gui) mwindow->gui->lock_window("MainIndexes::run 3");
237 progress->stop_progress();
239 if(mwindow->gui) mwindow->gui->unlock_window();
248 interrupt_lock->unlock();