r793: Small API addon, so plugins can 'see' camera and projector automation
[cinelerra_cv/mob.git] / cinelerra / mainindexes.C
blob8ddf366b56133612de98e6ce123e8ade179e07fc
1 #include "asset.h"
2 #include "defaults.h"
3 #include "edl.h"
4 #include "file.h"
5 #include "filesystem.h"
6 #include "indexfile.h"
7 #include "condition.h"
8 #include "language.h"
9 #include "loadfile.h"
10 #include "guicast.h"
11 #include "mainindexes.h"
12 #include "mainprogress.h"
13 #include "mwindow.h"
14 #include "mwindowgui.h"
15 #include "preferences.h"
17 #include <string.h>
20 MainIndexes::MainIndexes(MWindow *mwindow)
21  : Thread()
23         set_synchronous(1);
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");
28         interrupt_flag = 0;
29         done = 0;
30         indexfile = new IndexFile(mwindow);
33 MainIndexes::~MainIndexes()
35         mwindow->mainprogress->cancelled = 1;
36         stop_loop();
37         delete indexfile;
38         delete next_lock;
39         delete input_lock;
40         delete interrupt_lock;
43 void MainIndexes::add_next_asset(File *file, Asset *asset)
45         next_lock->lock("MainIndexes::add_next_asset");
47 // Test current asset
48         IndexFile indexfile(mwindow);
50         int got_it = 0;
52         if(!indexfile.open_index(asset))
53         {
54                 asset->index_status = INDEX_READY;
55                 indexfile.close_index();
56                 got_it = 1;
57         }
59         if(!got_it)
60         {
61                 File *this_file = file;
63                 if(!file)
64                 {
65                         this_file = new File;
66                         this_file->open_file(mwindow->preferences,
67                                 asset,
68                                 1,
69                                 0,
70                                 0,
71                                 0);
72                 }
74                 char index_filename[BCTEXTLEN];
75                 char source_filename[BCTEXTLEN];
76                 IndexFile::get_index_filename(source_filename, 
77                         mwindow->preferences->index_directory, 
78                         index_filename, 
79                         asset->path);
80                 if(!this_file->get_index(index_filename))
81                 {
82                         if(!indexfile.open_index(asset))
83                         {
84                                 indexfile.close_index();
85                                 asset->index_status = INDEX_READY;
86                                 got_it = 1;
87                         }
88                 }
89                 if(!file) delete this_file;
90         }
93 // Put copy of asset in stack, not the real thing.
94         if(!got_it)
95         {
96 //printf("MainIndexes::add_next_asset 3\n");
97                 Asset *new_asset = new Asset;
98                 *new_asset = *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);
102         }
104         next_lock->unlock();
107 void MainIndexes::delete_current_assets()
109         current_assets.remove_all_objects();
112 void MainIndexes::start_loop()
114         interrupt_flag = 0;
115         Thread::start();
118 void MainIndexes::stop_loop()
120         interrupt_flag = 1;
121         done = 1;
122         input_lock->unlock();
123         interrupt_lock->unlock();
124         Thread::join();
128 void MainIndexes::start_build()
130 //printf("MainIndexes::start_build 1\n");
131         interrupt_flag = 0;
132 // Locked up when indexes were already being built and an asset was 
133 // pasted.
134 //      interrupt_lock.lock();
135         input_lock->unlock();
138 void MainIndexes::interrupt_build()
140 //printf("MainIndexes::interrupt_build 1\n");
141         interrupt_flag = 1;
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();
161         next_lock->unlock();
165 void MainIndexes::run()
167         while(!done)
168         {
169 // Wait for new assets to be released
170                 input_lock->lock("MainIndexes::run 1");
171                 if(done) return;
172                 interrupt_lock->lock("MainIndexes::run 2");
173                 load_next_assets();
174                 interrupt_flag = 0;
181 // test index of each asset
182                 MainProgressBar *progress = 0;
183                 for(int i = 0; i < current_assets.total && !interrupt_flag; i++)
184                 {
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)
190                         {
196 // Doesn't exist if this returns 1.
197                                 if(indexfile->open_index(current_asset))
198                                 {
199 // Try to create index now.
200                                         if(!progress)
201                                         {
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();
205                                         }
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);
213                                 }
214                                 else
215 // Exists.  Update real thing.
216                                 {
217 //printf("MainIndexes::run 8\n");
218                                         if(current_asset->index_status == INDEX_NOTTESTED)
219                                         {
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();
224                                         }
225                                         indexfile->close_index();
226                                 }
229 //printf("MainIndexes::run 8\n");
230                         }
231 //printf("MainIndexes::run 9\n");
232                 }
234                 if(progress)     // progress box is only created when an index is built
235                 {
236                         if(mwindow->gui) mwindow->gui->lock_window("MainIndexes::run 3");
237                         progress->stop_progress();
238                         delete progress;
239                         if(mwindow->gui) mwindow->gui->unlock_window();
240                         progress = 0;
241                 }
248                 interrupt_lock->unlock();
249         }