Credit Andrew R for finding the direct copy mods for exr and ppm sequences
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / threadindexer.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
5  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #include "asset.h"
23 #include "assets.h"
24 #include "bcprogressbox.h"
25 #include "condition.h"
26 #include "bchash.h"
27 #include "filesystem.h"
28 #include "guicast.h"
29 #include "indexfile.h"
30 #include "language.h"
31 #include "loadfile.h"
32 #include "mwindowgui.h"
33 #include "mwindow.h"
34 #include "threadindexer.h"
35
36 #include <string.h>
37
38 // This doesn't appear to be used anymore.
39
40
41
42
43 ThreadIndexer::ThreadIndexer(MWindow *mwindow, Assets *assets)
44  : Thread()
45 {
46         this->mwindow = mwindow;
47         this->assets = assets;
48         set_synchronous(0);
49         indexfile = new IndexFile(mwindow);
50         interrupt_lock = new Condition(0, "ThreadIndexer::ThreadIndexer");
51 }
52
53 ThreadIndexer::~ThreadIndexer()
54 {
55         delete indexfile;
56         delete interrupt_lock;
57 }
58
59 void ThreadIndexer::start_build()
60 {
61         interrupt_flag = 0;
62         start();
63 }
64
65 // build all here to allow editing during build
66 void ThreadIndexer::run()
67 {
68 // check locations of each asset
69         FILE *test_file;
70         Asset *current_asset;
71         char new_filename[1024], old_filename[1024];
72         int result = 0;
73         BC_ProgressBox *progress = 0;
74
75         for(current_asset = assets->first; current_asset; current_asset = current_asset->next)
76         {
77                 if(!(test_file = fopen(current_asset->path, "rb")))
78                 {
79 // file doesn't exist
80                         strcpy(old_filename, current_asset->path);
81
82                         result = 1;
83 // get location from user
84                         char directory[1024];
85                         sprintf(directory, "~");
86                         mwindow->defaults->get("DIRECTORY", directory);
87
88                         char string[1024], name[1024];
89                         FileSystem dir;
90                         dir.extract_name(name, old_filename);
91                         sprintf(string, _("Where is %s?"), name);
92
93                         LocateFileWindow window(mwindow, directory, string);
94                         window.create_objects();
95                         int result2 = window.run_window();
96
97                         mwindow->defaults->update("DIRECTORY",
98                                 window.get_submitted_path());
99
100                         if(result2 == 1)
101                         {
102                                 new_filename[0] = 0;
103                         }
104                         else
105                         {
106                                 strcpy(new_filename,
107                                         window.get_submitted_path());
108                         }
109
110 // update assets containing old location
111                         assets->update_old_filename(old_filename, new_filename);
112                 }
113                 else
114                 {
115                         fclose(test_file);
116                 }
117         }
118
119
120 // test index of each asset
121         for(current_asset = assets->first;
122                 current_asset && !interrupt_flag;
123                 current_asset = current_asset->next)
124         {
125 // test for an index file already built before creating progress bar
126                 if(current_asset->index_status == INDEX_NOTTESTED &&
127                         current_asset->audio_data)
128                 {
129                         if(indexfile->open_index(mwindow, current_asset))
130                         {
131 // doesn't exist
132 // try to create now
133                                 if(!progress)
134                                 {
135                                         progress = new BC_ProgressBox(mwindow->gui->get_abs_cursor_x(1),
136                                                 mwindow->gui->get_abs_cursor_y(1),
137                                                 _("Building Indexes..."),
138                                                 1);
139                                         progress->start();
140                                 }
141
142 //                              indexfile->create_index(current_asset, 0, progress);
143                                 if(progress->is_cancelled()) interrupt_flag = 1;
144                         }
145                         else
146                         {
147                                 if(current_asset->index_status == INDEX_NOTTESTED)
148                                         current_asset->index_status = INDEX_READY;   // index has been tested
149                                 indexfile->close_index();
150                         }
151                 }
152         }
153
154 // progress box is only created when an index is built
155         if(progress)
156         {
157                 progress->stop_progress();
158                 delete progress;
159                 progress = 0;
160         }
161
162         interrupt_lock->unlock();
163 }
164
165 void ThreadIndexer::interrupt_build()
166 {
167         interrupt_flag = 1;
168         indexfile->interrupt_index();
169         interrupt_lock->lock(" ThreadIndexer::interrupt_build");
170         interrupt_lock->unlock();
171 }
172