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 "mainerror.h"
35 #include "mainindexes.h"
36 #include "mainprogress.h"
39 #include "mwindowgui.h"
40 #include "preferences.h"
45 MainIndexes::MainIndexes(MWindow *mwindow)
49 this->mwindow = mwindow;
50 input_lock = new Condition(0, "MainIndexes::input_lock");
51 next_lock = new Mutex("MainIndexes::next_lock");
52 index_lock = new Mutex("MainIndexes::index_lock");
53 interrupt_lock = new Condition(1, "MainIndexes::interrupt_lock");
59 MainIndexes::~MainIndexes()
61 mwindow->mainprogress->cancelled = 1;
65 delete interrupt_lock;
70 void MainIndexes::add_next_asset(File *file, Indexable *indexable)
72 next_lock->lock("MainIndexes::add_next_asset");
76 IndexFile indexfile(mwindow, indexable);
77 IndexState *index_state = 0;
78 index_state = indexable->index_state;
81 int ret = indexfile.open_index();
83 index_state->index_status = INDEX_READY;
84 indexfile.close_index();
87 // Put source in stack
88 index_state->index_status = INDEX_NOTTESTED;
89 next_indexables.append(indexable);
90 indexable->add_user();
95 void MainIndexes::delete_current_sources()
97 for(int i = 0; i < current_indexables.size(); i++)
98 current_indexables.get(i)->Garbage::remove_user();
99 current_indexables.remove_all();
102 void MainIndexes::start_loop()
108 void MainIndexes::stop_loop()
112 input_lock->unlock();
113 interrupt_lock->unlock();
118 void MainIndexes::start_build()
120 //printf("MainIndexes::start_build 1\n");
122 // Locked up when indexes were already being built and an indexable was
124 // interrupt_lock.lock();
125 input_lock->unlock();
128 void MainIndexes::interrupt_build()
130 //printf("MainIndexes::interrupt_build 1\n");
132 index_lock->lock("MainIndexes::interrupt_build");
133 if(indexfile) indexfile->interrupt_index();
134 index_lock->unlock();
135 //printf("MainIndexes::interrupt_build 2\n");
136 interrupt_lock->lock("MainIndexes::interrupt_build");
137 //printf("MainIndexes::interrupt_build 3\n");
138 interrupt_lock->unlock();
139 //printf("MainIndexes::interrupt_build 4\n");
142 void MainIndexes::load_next_sources()
144 delete_current_sources();
146 // Transfer from new list
147 next_lock->lock("MainIndexes::load_next_sources");
148 for(int i = 0; i < next_indexables.size(); i++)
149 current_indexables.append(next_indexables.get(i));
151 // Clear pointers from new list only
152 next_indexables.remove_all();
157 void MainIndexes::run()
160 // Wait for new indexables to be released
161 input_lock->lock("MainIndexes::run 1");
164 interrupt_lock->lock("MainIndexes::run 2");
168 // test index of each indexable
169 MainProgressBar *progress = 0;
170 int total_sources = current_indexables.size();
172 for( int i = 0; i < total_sources && !interrupt_flag; ++i ) {
173 Indexable *indexable = current_indexables[i];
174 IndexState *index_state = indexable->index_state;
175 // if status is known, no probe
176 if( index_state->index_status != INDEX_NOTTESTED ) continue;
178 IndexFile indexfile(mwindow, indexable);
179 int ret = indexfile.open_index();
181 indexfile.close_index();
182 // use existing index
183 index_state->index_status = INDEX_READY;
184 if(mwindow->gui) mwindow->gui->lock_window("MainIndexes::run 2");
185 mwindow->edl->set_index_file(indexable);
186 if(mwindow->gui) mwindow->gui->unlock_window();
191 if(mwindow->gui) mwindow->gui->lock_window("MainIndexes::run 1");
192 progress = mwindow->mainprogress->start_progress(_("Building Indexes..."), 1);
193 if(mwindow->gui) mwindow->gui->unlock_window();
195 // only if audio tracks
196 indexfile.create_index(progress);
197 if( progress->is_cancelled() )
201 if(progress) { // progress box is only created when an index is built
202 if(mwindow->gui) mwindow->gui->lock_window("MainIndexes::run 3");
203 progress->stop_progress();
205 if(mwindow->gui) mwindow->gui->unlock_window();
209 interrupt_lock->unlock();