r1009: Move the dependencies to newer package names
[cinelerra_cv/mob.git] / cinelerra / mainindexes.C
blob4b032ed6180e690d7420f86d8a73a9a5964086ca
1 #include "asset.h"
2 #include "bcsignals.h"
3 #include "bchash.h"
4 #include "edl.h"
5 #include "file.h"
6 #include "filesystem.h"
7 #include "indexfile.h"
8 #include "condition.h"
9 #include "language.h"
10 #include "loadfile.h"
11 #include "guicast.h"
12 #include "mainindexes.h"
13 #include "mainprogress.h"
14 #include "mutex.h"
15 #include "mwindow.h"
16 #include "mwindowgui.h"
17 #include "preferences.h"
19 #include <string.h>
22 MainIndexes::MainIndexes(MWindow *mwindow)
23  : Thread()
25         set_synchronous(1);
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");
30         interrupt_flag = 0;
31         done = 0;
32         indexfile = new IndexFile(mwindow);
35 MainIndexes::~MainIndexes()
37         mwindow->mainprogress->cancelled = 1;
38         stop_loop();
39         delete indexfile;
40         delete next_lock;
41         delete input_lock;
42         delete interrupt_lock;
45 void MainIndexes::add_next_asset(File *file, Asset *asset)
47         next_lock->lock("MainIndexes::add_next_asset");
49 // Test current asset
50         IndexFile indexfile(mwindow);
52         int got_it = 0;
54         if(!indexfile.open_index(asset))
55         {
56                 asset->index_status = INDEX_READY;
57                 indexfile.close_index();
58                 got_it = 1;
59         }
61         if(!got_it)
62         {
63                 File *this_file = file;
65                 if(!file)
66                 {
67                         this_file = new File;
68                         this_file->open_file(mwindow->preferences,
69                                 asset,
70                                 1,
71                                 0,
72                                 0,
73                                 0);
74                 }
76                 char index_filename[BCTEXTLEN];
77                 char source_filename[BCTEXTLEN];
78                 IndexFile::get_index_filename(source_filename, 
79                         mwindow->preferences->index_directory, 
80                         index_filename, 
81                         asset->path);
82                 if(!this_file->get_index(index_filename))
83                 {
84                         if(!indexfile.open_index(asset))
85                         {
86                                 indexfile.close_index();
87                                 asset->index_status = INDEX_READY;
88                                 got_it = 1;
89                         }
90                 }
91                 if(!file) delete this_file;
92         }
95 // Put copy of asset in stack, not the real thing.
96         if(!got_it)
97         {
98 //printf("MainIndexes::add_next_asset 3\n");
99                 Asset *new_asset = new Asset;
100                 *new_asset = *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);
104         }
106         next_lock->unlock();
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()
118         interrupt_flag = 0;
119         Thread::start();
122 void MainIndexes::stop_loop()
124         interrupt_flag = 1;
125         done = 1;
126         input_lock->unlock();
127         interrupt_lock->unlock();
128         Thread::join();
132 void MainIndexes::start_build()
134 //printf("MainIndexes::start_build 1\n");
135         interrupt_flag = 0;
136 // Locked up when indexes were already being built and an asset was 
137 // pasted.
138 //      interrupt_lock.lock();
139         input_lock->unlock();
142 void MainIndexes::interrupt_build()
144 //printf("MainIndexes::interrupt_build 1\n");
145         interrupt_flag = 1;
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();
165         next_lock->unlock();
169 void MainIndexes::run()
171         while(!done)
172         {
173 // Wait for new assets to be released
174                 input_lock->lock("MainIndexes::run 1");
175                 if(done) return;
176                 interrupt_lock->lock("MainIndexes::run 2");
177                 load_next_assets();
178                 interrupt_flag = 0;
185 // test index of each asset
186                 MainProgressBar *progress = 0;
187                 for(int i = 0; i < current_assets.total && !interrupt_flag; i++)
188                 {
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)
194                         {
200 // Doesn't exist if this returns 1.
201                                 if(indexfile->open_index(current_asset))
202                                 {
203 // Try to create index now.
204                                         if(!progress)
205                                         {
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();
209                                         }
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);
217                                 }
218                                 else
219 // Exists.  Update real thing.
220                                 {
221 //printf("MainIndexes::run 8\n");
222                                         if(current_asset->index_status == INDEX_NOTTESTED)
223                                         {
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();
228                                         }
229                                         indexfile->close_index();
230                                 }
233 //printf("MainIndexes::run 8\n");
234                         }
235 //printf("MainIndexes::run 9\n");
236                 }
238                 if(progress)     // progress box is only created when an index is built
239                 {
240                         if(mwindow->gui) mwindow->gui->lock_window("MainIndexes::run 3");
241                         progress->stop_progress();
242                         delete progress;
243                         if(mwindow->gui) mwindow->gui->unlock_window();
244                         progress = 0;
245                 }
252                 interrupt_lock->unlock();
253         }