Merge branch 'ct' of git.pipapo.org:cinelerra-ct into ct
[cinelerra_cv/ct.git] / cinelerra / mainindexes.C
blob2cec92670bd464e6f530917d00f8405b5907e343
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_GC 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_GC new_asset = Asset_GC(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.push_back(new_asset);
104         }
106         next_lock->unlock();
109 void MainIndexes::delete_current_assets()
111         current_assets.clear();
114 void MainIndexes::start_loop()
116         interrupt_flag = 0;
117         Thread::start();
120 void MainIndexes::stop_loop()
122         interrupt_flag = 1;
123         done = 1;
124         input_lock->unlock();
125         interrupt_lock->unlock();
126         Thread::join();
130 void MainIndexes::start_build()
132 //printf("MainIndexes::start_build 1\n");
133         interrupt_flag = 0;
134 // Locked up when indexes were already being built and an asset was 
135 // pasted.
136 //      interrupt_lock.lock();
137         input_lock->unlock();
140 void MainIndexes::interrupt_build()
142 //printf("MainIndexes::interrupt_build 1\n");
143         interrupt_flag = 1;
144         indexfile->interrupt_index();
145 //printf("MainIndexes::interrupt_build 2\n");
146         interrupt_lock->lock("MainIndexes::interrupt_build");
147 //printf("MainIndexes::interrupt_build 3\n");
148         interrupt_lock->unlock();
149 //printf("MainIndexes::interrupt_build 4\n");
152 void MainIndexes::load_next_assets()
154         delete_current_assets();
156 // Transfer from new list
157         next_lock->lock("MainIndexes::load_next_assets");
158         for(int i = 0; i < next_assets.size(); i++)
159                 current_assets.push_back(next_assets[i]);
161 // Clear pointers from new list only
162         next_assets.clear();
163         next_lock->unlock();
167 void MainIndexes::run()
169         while(!done)
170         {
171 // Wait for new assets to be released
172                 input_lock->lock("MainIndexes::run 1");
173                 if(done) return;
174                 interrupt_lock->lock("MainIndexes::run 2");
175                 load_next_assets();
176                 interrupt_flag = 0;
183 // test index of each asset
184                 MainProgressBar *progress = 0;
185                 for(int i = 0; i < current_assets.size() && !interrupt_flag; i++)
186                 {
187                         Asset_GC current_asset = current_assets[i];
188 //printf("MainIndexes::run 3 %s %d %d\n", current_asset->path, current_asset->index_status, current_asset->audio_data);
190                         if(current_asset->index_status == INDEX_NOTTESTED && 
191                                 current_asset->audio_data)
192                         {
198 // Doesn't exist if this returns 1.
199                                 if(indexfile->open_index(current_asset))
200                                 {
201 // Try to create index now.
202                                         if(!progress)
203                                         {
204                                                 if(mwindow->gui) mwindow->gui->lock_window("MainIndexes::run 1");
205                                                 progress = mwindow->mainprogress->start_progress(_("Building Indexes..."), 1);
206                                                 if(mwindow->gui) mwindow->gui->unlock_window();
207                                         }
209 //printf("MainIndexes::run 5 %p %s\n", current_asset, current_asset->path);
211                                         indexfile->create_index(current_asset, progress);
212 //printf("MainIndexes::run 6 %p %s\n", current_asset, current_asset->path);
213                                         if(progress->is_cancelled()) interrupt_flag = 1;
214 //printf("MainIndexes::run 7 %p %s\n", current_asset, current_asset->path);
215                                 }
216                                 else
217 // Exists.  Update real thing.
218                                 {
219 //printf("MainIndexes::run 8\n");
220                                         if(current_asset->index_status == INDEX_NOTTESTED)
221                                         {
222                                                 current_asset->index_status = INDEX_READY;
223                                                 if(mwindow->gui) mwindow->gui->lock_window("MainIndexes::run 2");
224                                                 mwindow->edl->set_index_file(current_asset);
225                                                 if(mwindow->gui) mwindow->gui->unlock_window();
226                                         }
227                                         indexfile->close_index();
228                                 }
231 //printf("MainIndexes::run 8\n");
232                         }
233 //printf("MainIndexes::run 9\n");
234                 }
236                 if(progress)     // progress box is only created when an index is built
237                 {
238                         if(mwindow->gui) mwindow->gui->lock_window("MainIndexes::run 3");
239                         progress->stop_progress();
240                         delete progress;
241                         if(mwindow->gui) mwindow->gui->unlock_window();
242                         progress = 0;
243                 }
250                 interrupt_lock->unlock();
251         }
255 //      Local Variables:
256 //      mode: C++
257 //      c-file-style: "linux"
258 //      End: