4 * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "bcsignals.h"
27 #include "filesystem.h"
28 #include "indexfile.h"
29 #include "indexstate.h"
30 #include "condition.h"
34 #include "mainindexes.h"
35 #include "mainprogress.h"
38 #include "mwindowgui.h"
39 #include "preferences.h"
44 MainIndexes::MainIndexes(MWindow *mwindow)
48 this->mwindow = mwindow;
49 input_lock = new Condition(0, "MainIndexes::input_lock");
50 next_lock = new Mutex("MainIndexes::next_lock");
51 index_lock = new Mutex("MainIndexes::index_lock");
52 interrupt_lock = new Condition(1, "MainIndexes::interrupt_lock");
58 MainIndexes::~MainIndexes()
60 mwindow->mainprogress->cancelled = 1;
64 delete interrupt_lock;
69 void MainIndexes::add_next_asset(File *file, Indexable *indexable)
71 next_lock->lock("MainIndexes::add_next_asset");
75 IndexFile indexfile(mwindow, indexable);
76 IndexState *index_state = 0;
77 index_state = indexable->index_state;
83 if(!indexfile.open_index())
85 index_state->index_status = INDEX_READY;
86 indexfile.close_index();
90 //printf("MainIndexes::add_next_asset %d %f\n", __LINE__, indexable->get_frame_rate());
96 File *this_file = file;
99 if(!file && indexable->is_asset)
101 Asset *asset = (Asset *)indexable;
102 this_file = new File;
103 this_file->open_file(mwindow->preferences, asset, 1, 0);
108 char index_filename[BCTEXTLEN];
109 char source_filename[BCTEXTLEN];
111 IndexFile::get_index_filename(source_filename,
112 mwindow->preferences->index_directory,
117 if(this_file && !this_file->get_index(index_filename))
120 if(!indexfile.open_index())
123 indexfile.close_index();
125 index_state->index_status = INDEX_READY;
132 if(this_file && !file) delete this_file;
137 //printf("MainIndexes::add_next_asset %d %f\n", __LINE__, indexable->get_frame_rate());
139 // Put source in stack
142 index_state->index_status = INDEX_NOTTESTED;
143 next_indexables.append(indexable);
144 indexable->add_user();
149 void MainIndexes::delete_current_sources()
151 for(int i = 0; i < current_indexables.size(); i++)
152 current_indexables.get(i)->Garbage::remove_user();
153 current_indexables.remove_all();
156 void MainIndexes::start_loop()
162 void MainIndexes::stop_loop()
166 input_lock->unlock();
167 interrupt_lock->unlock();
172 void MainIndexes::start_build()
174 //printf("MainIndexes::start_build 1\n");
176 // Locked up when indexes were already being built and an indexable was
178 // interrupt_lock.lock();
179 input_lock->unlock();
182 void MainIndexes::interrupt_build()
184 //printf("MainIndexes::interrupt_build 1\n");
186 index_lock->lock("MainIndexes::interrupt_build");
187 if(indexfile) indexfile->interrupt_index();
188 index_lock->unlock();
189 //printf("MainIndexes::interrupt_build 2\n");
190 interrupt_lock->lock("MainIndexes::interrupt_build");
191 //printf("MainIndexes::interrupt_build 3\n");
192 interrupt_lock->unlock();
193 //printf("MainIndexes::interrupt_build 4\n");
196 void MainIndexes::load_next_sources()
198 delete_current_sources();
200 // Transfer from new list
201 next_lock->lock("MainIndexes::load_next_sources");
202 for(int i = 0; i < next_indexables.size(); i++)
203 current_indexables.append(next_indexables.get(i));
205 // Clear pointers from new list only
206 next_indexables.remove_all();
211 void MainIndexes::run()
215 // Wait for new indexables to be released
216 input_lock->lock("MainIndexes::run 1");
220 interrupt_lock->lock("MainIndexes::run 2");
229 // test index of each indexable
230 MainProgressBar *progress = 0;
231 int total_sources = current_indexables.size();
233 i < total_sources && !interrupt_flag;
236 Indexable *indexable = 0;
238 indexable = current_indexables.get(i);
240 IndexState *index_state = 0;
241 index_state = indexable->index_state;
243 //printf("MainIndexes::run 3 %s %d %d\n", indexable->path, indexable->index_status, indexable->audio_data);
245 if(index_state->index_status == INDEX_NOTTESTED &&
246 indexable->have_audio())
250 index_lock->lock("MainIndexes::run 1");
251 indexfile = new IndexFile(mwindow, indexable);
252 index_lock->unlock();
255 // Doesn't exist if this returns 1.
256 if(indexfile->open_index())
258 // Try to create index now.
261 if(mwindow->gui) mwindow->gui->lock_window("MainIndexes::run 1");
262 progress = mwindow->mainprogress->start_progress(_("Building Indexes..."), 1);
263 if(mwindow->gui) mwindow->gui->unlock_window();
267 indexfile->create_index(progress);
268 if(progress->is_cancelled()) interrupt_flag = 1;
271 // Exists. Update real thing.
273 //printf("MainIndexes::run 8\n");
274 if(index_state->index_status == INDEX_NOTTESTED)
276 index_state->index_status = INDEX_READY;
277 if(mwindow->gui) mwindow->gui->lock_window("MainIndexes::run 2");
278 mwindow->edl->set_index_file(indexable);
279 if(mwindow->gui) mwindow->gui->unlock_window();
281 indexfile->close_index();
284 index_lock->lock("MainIndexes::run 2");
287 index_lock->unlock();
288 //printf("MainIndexes::run 8\n");
290 //printf("MainIndexes::run 9\n");
293 if(progress) // progress box is only created when an index is built
295 if(mwindow->gui) mwindow->gui->lock_window("MainIndexes::run 3");
296 progress->stop_progress();
298 if(mwindow->gui) mwindow->gui->unlock_window();
307 interrupt_lock->unlock();