r136: This commit was manufactured by cvs2svn to create tag 'hv_1_1_8'.
[cinelerra_cv/mob.git] / hvirtual / cinelerra / mainindexes.C
blob32cd235e34caa7552f5243f7ced7ae42a72d79cb
1 #include "asset.h"
2 #include "defaults.h"
3 #include "edl.h"
4 #include "filesystem.h"
5 #include "indexfile.h"
6 #include "condition.h"
7 #include "loadfile.h"
8 #include "guicast.h"
9 #include "mainindexes.h"
10 #include "mainprogress.h"
11 #include "mwindow.h"
12 #include "mwindowgui.h"
14 #include <string.h>
16 #include <libintl.h>
17 #define _(String) gettext(String)
18 #define gettext_noop(String) String
19 #define N_(String) gettext_noop (String)
21 MainIndexes::MainIndexes(MWindow *mwindow)
22  : Thread()
24         set_synchronous(1);
25         this->mwindow = mwindow;
26         input_lock = new Condition(0, "MainIndexes::input_lock");
27         next_lock = new Mutex("MainIndexes::next_lock");
28         interrupt_lock = new Condition(1, "MainIndexes::interrupt_lock");
29         interrupt_flag = 0;
30         done = 0;
31         indexfile = new IndexFile(mwindow);
34 MainIndexes::~MainIndexes()
36         mwindow->mainprogress->cancelled = 1;
37         stop_loop();
38         delete indexfile;
39         delete next_lock;
40         delete input_lock;
41         delete interrupt_lock;
44 void MainIndexes::add_next_asset(Asset *asset)
46         next_lock->lock("MainIndexes::add_next_asset");
48 // Test current asset
49         IndexFile indexfile(mwindow);
51 //printf("MainIndexes::add_next_asset 1 %s\n", asset->path);
52         if(!indexfile.open_index(asset))
53         {
54 //printf("MainIndexes::add_next_asset 2\n");
55                 asset->index_status = INDEX_READY;
56                 indexfile.close_index();
57         }
58         else
59 // Put copy of asset in stack, not the real thing.
60         {
61 //printf("MainIndexes::add_next_asset 3\n");
62                 Asset *new_asset = new Asset;
63                 *new_asset = *asset;
64 // If the asset existed and was overwritten, the status will be READY.
65                 new_asset->index_status = INDEX_NOTTESTED;
66                 next_assets.append(new_asset);
67         }
69         next_lock->unlock();
72 void MainIndexes::delete_current_assets()
74         current_assets.remove_all_objects();
77 void MainIndexes::start_loop()
79         interrupt_flag = 0;
80         start();
83 void MainIndexes::stop_loop()
85         interrupt_flag = 1;
86         done = 1;
87         input_lock->unlock();
88         interrupt_lock->unlock();
89         Thread::join();
93 void MainIndexes::start_build()
95 //printf("MainIndexes::start_build 1\n");
96         interrupt_flag = 0;
97 // Locked up when indexes were already being built and an asset was 
98 // pasted.
99 //      interrupt_lock.lock();
100         input_lock->unlock();
103 void MainIndexes::interrupt_build()
105 //printf("MainIndexes::interrupt_build 1\n");
106         interrupt_flag = 1;
107         indexfile->interrupt_index();
108 //printf("MainIndexes::interrupt_build 2\n");
109         interrupt_lock->lock("MainIndexes::interrupt_build");
110 //printf("MainIndexes::interrupt_build 3\n");
111         interrupt_lock->unlock();
112 //printf("MainIndexes::interrupt_build 4\n");
115 void MainIndexes::load_next_assets()
117         delete_current_assets();
119 // Transfer from new list
120         next_lock->lock("MainIndexes::load_next_assets");
121         for(int i = 0; i < next_assets.total; i++)
122                 current_assets.append(next_assets.values[i]);
124 // Clear pointers from new list only
125         next_assets.remove_all();
126         next_lock->unlock();
130 void MainIndexes::run()
132         while(!done)
133         {
134 // Wait for new assets to be released
135                 input_lock->lock("MainIndexes::run 1");
136                 if(done) return;
138                 interrupt_lock->lock("MainIndexes::run 2");
139 //printf("MainIndexes::run 1 %d\n", next_assets.total);
140                 load_next_assets();
141                 interrupt_flag = 0;
142 //printf("MainIndexes::run 2 %d\n", current_assets.total);
149 // test index of each asset
150                 MainProgressBar *progress = 0;
151                 for(int i = 0; i < current_assets.total && !interrupt_flag; i++)
152                 {
153                         Asset *current_asset = current_assets.values[i];
154 //printf("MainIndexes::run 3 %s %d %d\n", current_asset->path, current_asset->index_status, current_asset->audio_data);
156                         if(current_asset->index_status == INDEX_NOTTESTED && 
157                                 current_asset->audio_data)
158                         {
159 //printf("MainIndexes::run 4\n");
162 // Doesn't exist.
163 // Try to create index now.
164                                 if(indexfile->open_index(current_asset))
165                                 {
166 //printf("MainIndexes::run 5 %p %s %p %p\n", current_asset, current_asset->path, mwindow, mwindow->mainprogress);
167                                         if(!progress)
168                                         {
169                                                 if(mwindow->gui) mwindow->gui->lock_window("MainIndexes::run 1");
170                                                 progress = mwindow->mainprogress->start_progress("Building Indexes...", 1);
171                                                 if(mwindow->gui) mwindow->gui->unlock_window();
172                                         }
174 //printf("MainIndexes::run 5 %p %s\n", current_asset, current_asset->path);
176                                         indexfile->create_index(current_asset, progress);
177 //printf("MainIndexes::run 6 %p %s\n", current_asset, current_asset->path);
178                                         if(progress->is_cancelled()) interrupt_flag = 1;
179 //printf("MainIndexes::run 7 %p %s\n", current_asset, current_asset->path);
180                                 }
181                                 else
182 // Exists.  Update real thing.
183                                 {
184 //printf("MainIndexes::run 8\n");
185                                         if(current_asset->index_status == INDEX_NOTTESTED)
186                                         {
187                                                 current_asset->index_status = INDEX_READY;
188                                                 if(mwindow->gui) mwindow->gui->lock_window("MainIndexes::run 2");
189                                                 mwindow->edl->set_index_file(current_asset);
190                                                 if(mwindow->gui) mwindow->gui->unlock_window();
191                                         }
192                                         indexfile->close_index();
193                                 }
196 //printf("MainIndexes::run 8\n");
197                         }
198 //printf("MainIndexes::run 9\n");
199                 }
200 //printf("MainIndexes::run 10\n");
202                 if(progress)     // progress box is only created when an index is built
203                 {
204                         if(mwindow->gui) mwindow->gui->lock_window("MainIndexes::run 3");
205                         progress->stop_progress();
206                         delete progress;
207                         if(mwindow->gui) mwindow->gui->unlock_window();
208                         progress = 0;
209                 }
215 //printf("MainIndexes::run 11\n");
217                 interrupt_lock->unlock();
218         }