-
-
Notifications
You must be signed in to change notification settings - Fork 335
/
TranslatorRoster.cpp
1828 lines (1431 loc) · 43.2 KB
/
TranslatorRoster.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2002-2015, Haiku, Inc. All Rights Reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* Axel Dörfler, axeld@pinc-software.de
* Markus Himmel, markus@himmel-villmar.de
* Michael Wilber
*/
/*!
This class is the guts of the translation kit, it makes the
whole thing happen. It bridges the applications using this
object with the translators that the apps need to access.
*/
#include <TranslatorRoster.h>
#include <new>
#include <strings.h>
#include <stdio.h>
#include <stdlib.h>
#include <Application.h>
#include <Autolock.h>
#include <Directory.h>
#include <FindDirectory.h>
#include <NodeMonitor.h>
#include <Path.h>
#include <String.h>
#include <driver_settings.h>
#include <image.h>
#include <safemode_defs.h>
#include <syscalls.h>
#include "FuncTranslator.h"
#include "TranslatorRosterPrivate.h"
namespace BPrivate {
class QuarantineTranslatorImage {
public:
QuarantineTranslatorImage(
BTranslatorRoster::Private& privateRoster);
~QuarantineTranslatorImage();
void Put(const entry_ref& ref);
void Remove();
private:
BTranslatorRoster::Private& fRoster;
entry_ref fRef;
bool fRemove;
};
} // namespace BPrivate
// Extensions used in the extension BMessage, defined in TranslatorFormats.h
char B_TRANSLATOR_EXT_HEADER_ONLY[] = "/headerOnly";
char B_TRANSLATOR_EXT_DATA_ONLY[] = "/dataOnly";
char B_TRANSLATOR_EXT_COMMENT[] = "/comment";
char B_TRANSLATOR_EXT_TIME[] = "/time";
char B_TRANSLATOR_EXT_FRAME[] = "/frame";
char B_TRANSLATOR_EXT_BITMAP_RECT[] = "bits/Rect";
char B_TRANSLATOR_EXT_BITMAP_COLOR_SPACE[] = "bits/space";
char B_TRANSLATOR_EXT_BITMAP_PALETTE[] = "bits/palette";
char B_TRANSLATOR_EXT_SOUND_CHANNEL[] = "nois/channel";
char B_TRANSLATOR_EXT_SOUND_MONO[] = "nois/mono";
char B_TRANSLATOR_EXT_SOUND_MARKER[] = "nois/marker";
char B_TRANSLATOR_EXT_SOUND_LOOP[] = "nois/loop";
BTranslatorRoster* BTranslatorRoster::sDefaultRoster = NULL;
namespace BPrivate {
/*!
The purpose of this class is to put a translator entry_ref into - and remove
it from the list of translators on destruction (if Remove() was called
before).
This is used in Private::CreateTranslators() in case a translator hides a
previous one (ie. if you install a translator in the user's translators
directory that has the same name as one in the system's directory, it will
hide this entry).
*/
QuarantineTranslatorImage::QuarantineTranslatorImage(
BTranslatorRoster::Private& privateRoster)
:
fRoster(privateRoster),
fRemove(false)
{
}
QuarantineTranslatorImage::~QuarantineTranslatorImage()
{
if (fRef.device == -1 || !fRemove)
return;
fRoster.RemoveTranslators(fRef);
}
void
QuarantineTranslatorImage::Put(const entry_ref& ref)
{
fRef = ref;
}
void
QuarantineTranslatorImage::Remove()
{
fRemove = true;
}
} // namespace BPrivate
// #pragma mark -
BTranslatorRoster::Private::Private()
:
BHandler("translator roster"),
BLocker("translator list"),
fABISubDirectory(NULL),
fNextID(1),
fLazyScanning(true),
fSafeMode(false)
{
char parameter[32];
size_t parameterLength = sizeof(parameter);
if (_kern_get_safemode_option(B_SAFEMODE_SAFE_MODE, parameter,
¶meterLength) == B_OK) {
if (!strcasecmp(parameter, "enabled") || !strcasecmp(parameter, "on")
|| !strcasecmp(parameter, "true") || !strcasecmp(parameter, "yes")
|| !strcasecmp(parameter, "enable") || !strcmp(parameter, "1"))
fSafeMode = true;
}
if (_kern_get_safemode_option(B_SAFEMODE_DISABLE_USER_ADD_ONS, parameter,
¶meterLength) == B_OK) {
if (!strcasecmp(parameter, "enabled") || !strcasecmp(parameter, "on")
|| !strcasecmp(parameter, "true") || !strcasecmp(parameter, "yes")
|| !strcasecmp(parameter, "enable") || !strcmp(parameter, "1"))
fSafeMode = true;
}
// We might run in compatibility mode on a system with a different ABI. The
// translators matching our ABI can usually be found in respective
// subdirectories of the translator directories.
system_info info;
if (get_system_info(&info) == B_OK
&& (info.abi & B_HAIKU_ABI_MAJOR)
!= (B_HAIKU_ABI & B_HAIKU_ABI_MAJOR)) {
switch (B_HAIKU_ABI & B_HAIKU_ABI_MAJOR) {
case B_HAIKU_ABI_GCC_2:
fABISubDirectory = "gcc2";
break;
case B_HAIKU_ABI_GCC_4:
fABISubDirectory = "gcc4";
break;
}
}
// we're sneaking us into the BApplication
if (be_app != NULL && be_app->Lock()) {
be_app->AddHandler(this);
be_app->Unlock();
}
}
BTranslatorRoster::Private::~Private()
{
stop_watching(this);
if (Looper() && LockLooper()) {
BLooper* looper = Looper();
Looper()->RemoveHandler(this);
looper->Unlock();
}
// Release all translators, so that they can delete themselves
TranslatorMap::iterator iterator = fTranslators.begin();
std::set<image_id> images;
while (iterator != fTranslators.end()) {
BTranslator* translator = iterator->second.translator;
images.insert(iterator->second.image);
translator->Release();
iterator++;
}
// Unload all images
std::set<image_id>::const_iterator imageIterator = images.begin();
while (imageIterator != images.end()) {
unload_add_on(*imageIterator);
imageIterator++;
}
}
void
BTranslatorRoster::Private::MessageReceived(BMessage* message)
{
switch (message->what) {
case B_NODE_MONITOR:
{
BAutolock locker(this);
printf("translator roster node monitor: ");
message->PrintToStream();
int32 opcode;
if (message->FindInt32("opcode", &opcode) != B_OK)
return;
switch (opcode) {
case B_ENTRY_CREATED:
{
const char* name;
node_ref nodeRef;
if (message->FindInt32("device", &nodeRef.device) != B_OK
|| message->FindInt64("directory", &nodeRef.node)
!= B_OK
|| message->FindString("name", &name) != B_OK)
break;
// TODO: make this better (possible under Haiku)
snooze(100000);
// let the font be written completely before trying to
// open it
_EntryAdded(nodeRef, name);
break;
}
case B_ENTRY_MOVED:
{
// has the entry been moved into a monitored directory or
// has it been removed from one?
const char* name;
node_ref toNodeRef;
node_ref fromNodeRef;
node_ref nodeRef;
if (message->FindInt32("device", &nodeRef.device) != B_OK
|| message->FindInt64("to directory", &toNodeRef.node)
!= B_OK
|| message->FindInt64("from directory",
(int64*)&fromNodeRef.node) != B_OK
|| message->FindInt64("node", (int64*)&nodeRef.node)
!= B_OK
|| message->FindString("name", &name) != B_OK)
break;
fromNodeRef.device = nodeRef.device;
toNodeRef.device = nodeRef.device;
// Do we know this one yet?
translator_item* item = _FindTranslator(nodeRef);
if (item == NULL) {
// it's a new one!
if (_IsKnownDirectory(toNodeRef))
_EntryAdded(toNodeRef, name);
break;
}
if (!_IsKnownDirectory(toNodeRef)) {
// translator got removed
_RemoveTranslators(&nodeRef);
break;
}
// the name may have changed
item->ref.set_name(name);
item->ref.directory = toNodeRef.node;
if (_IsKnownDirectory(fromNodeRef)
&& _IsKnownDirectory(toNodeRef)) {
// TODO: we should rescan for the name, there might be
// name clashes with translators in other directories
// (as well as old ones revealed)
break;
}
break;
}
case B_ENTRY_REMOVED:
{
node_ref nodeRef;
uint64 directoryNode;
if (message->FindInt32("device", &nodeRef.device) != B_OK
|| message->FindInt64("directory",
(int64*)&directoryNode) != B_OK
|| message->FindInt64("node", &nodeRef.node) != B_OK)
break;
translator_item* item = _FindTranslator(nodeRef);
if (item != NULL)
_RemoveTranslators(&nodeRef);
break;
}
}
break;
}
case B_DELETE_TRANSLATOR:
{
// A translator's refcount has been reduced to zero and it wants
// us to delete it.
int32 id;
void* self;
if (message->FindInt32("id", &id) == B_OK
&& message->FindPointer("ptr", &self) == B_OK) {
_TranslatorDeleted(id, (BTranslator*)self);
}
break;
}
default:
BHandler::MessageReceived(message);
break;
}
}
void
BTranslatorRoster::Private::AddDefaultPaths()
{
// add user directories first, so that they can override system translators
const directory_which paths[] = {
B_USER_NONPACKAGED_ADDONS_DIRECTORY,
B_USER_ADDONS_DIRECTORY,
B_SYSTEM_NONPACKAGED_ADDONS_DIRECTORY,
B_SYSTEM_ADDONS_DIRECTORY,
};
for (uint32 i = fSafeMode ? 4 : 0; i < sizeof(paths) / sizeof(paths[0]);
i++) {
BPath path;
status_t status = find_directory(paths[i], &path, true);
if (status == B_OK && path.Append("Translators") == B_OK) {
mkdir(path.Path(), 0755);
// make sure the directory exists before we add it
AddPath(path.Path());
}
}
}
/*!
Adds the colon separated list of directories to the roster.
Note, the order in which these directories are added to actually matters,
translators with the same name will be taken from the earlier directory
first. See _CompareTranslatorDirectoryPriority().
*/
status_t
BTranslatorRoster::Private::AddPaths(const char* paths)
{
if (paths == NULL)
return B_BAD_VALUE;
status_t status = B_OK;
int32 added = 0;
while (paths != NULL) {
const char* end = strchr(paths, ':');
BString path;
if (end != NULL) {
path.SetTo(paths, end - 1 - paths);
paths = end + 1;
} else {
path.SetTo(paths);
paths = NULL;
}
// Keep the last error that occured, and return it
// but don't overwrite it, if the last path was
// added successfully.
int32 count;
status_t error = AddPath(path.String(), &count);
if (error != B_NO_ERROR)
status = error;
added += count;
}
if (added == 0)
return status;
return B_OK;
}
/*!
Adds a new directory to the roster.
Note, the order in which these directories are added to actually matters,
see AddPaths().
*/
status_t
BTranslatorRoster::Private::AddPath(const char* path, int32* _added)
{
BDirectory directory(path);
status_t status = directory.InitCheck();
if (status != B_OK)
return status;
// if a subdirectory for our ABI exists, use that instead
if (fABISubDirectory != NULL) {
BEntry entry(&directory, fABISubDirectory);
if (entry.IsDirectory()) {
status = directory.SetTo(&entry);
if (status != B_OK)
return status;
}
}
node_ref nodeRef;
status = directory.GetNodeRef(&nodeRef);
if (status < B_OK)
return status;
// do we know this directory already?
if (_IsKnownDirectory(nodeRef))
return B_OK;
if (Looper() != NULL) {
// watch that directory
watch_node(&nodeRef, B_WATCH_DIRECTORY, this);
fDirectories.push_back(nodeRef);
}
int32 count = 0;
int32 files = 0;
entry_ref ref;
while (directory.GetNextRef(&ref) == B_OK) {
BEntry entry(&ref);
if (entry.IsDirectory())
continue;
if (CreateTranslators(ref, count) == B_OK)
count++;
files++;
}
if (_added)
*_added = count;
if (files != 0 && count == 0)
return B_BAD_VALUE;
return B_OK;
}
status_t
BTranslatorRoster::Private::AddTranslator(BTranslator* translator,
image_id image, const entry_ref* ref, ino_t node)
{
BAutolock locker(this);
translator_item item;
item.translator = translator;
item.image = image;
item.node = node;
if (ref != NULL)
item.ref = *ref;
try {
fTranslators[fNextID] = item;
} catch (...) {
return B_NO_MEMORY;
}
translator->fOwningRoster = this;
translator->fID = fNextID++;
return B_OK;
}
void
BTranslatorRoster::Private::RemoveTranslators(entry_ref& ref)
{
_RemoveTranslators(NULL, &ref);
}
BTranslator*
BTranslatorRoster::Private::FindTranslator(translator_id id)
{
if (!IsLocked()) {
debugger("translator must be locked!");
return NULL;
}
const translator_item* item = _FindTranslator(id);
if (item != NULL)
return item->translator;
return NULL;
}
status_t
BTranslatorRoster::Private::GetTranslatorData(image_id image,
translator_data& data)
{
// If this is a translator add-on, it is in the C format
memset(&data, 0, sizeof(translator_data));
// find all the symbols
int32* version;
if (get_image_symbol(image, "translatorName", B_SYMBOL_TYPE_DATA,
(void**)&data.name) < B_OK
|| get_image_symbol(image, "translatorInfo", B_SYMBOL_TYPE_DATA,
(void**)&data.info) < B_OK
|| get_image_symbol(image, "translatorVersion", B_SYMBOL_TYPE_DATA,
(void**)&version) < B_OK || version == NULL
|| get_image_symbol(image, "inputFormats", B_SYMBOL_TYPE_DATA,
(void**)&data.input_formats) < B_OK
|| get_image_symbol(image, "outputFormats", B_SYMBOL_TYPE_DATA,
(void**)&data.output_formats) < B_OK
|| get_image_symbol(image, "Identify", B_SYMBOL_TYPE_TEXT,
(void**)&data.identify_hook) < B_OK
|| get_image_symbol(image, "Translate", B_SYMBOL_TYPE_TEXT,
(void**)&data.translate_hook) < B_OK) {
return B_BAD_TYPE;
}
data.version = *version;
// those calls are optional
get_image_symbol(image, "MakeConfig", B_SYMBOL_TYPE_TEXT,
(void**)&data.make_config_hook);
get_image_symbol(image, "GetConfigMessage", B_SYMBOL_TYPE_TEXT,
(void**)&data.get_config_message_hook);
return B_OK;
}
status_t
BTranslatorRoster::Private::CreateTranslators(const entry_ref& ref,
int32& count, BMessage* update)
{
BAutolock locker(this);
BPrivate::QuarantineTranslatorImage quarantine(*this);
const translator_item* item = _FindTranslator(ref.name);
if (item != NULL) {
// check if the known translator has a higher priority
if (_CompareTranslatorDirectoryPriority(item->ref, ref) <= 0) {
// keep the existing add-on
return B_OK;
}
// replace existing translator(s) if the new translator succeeds
quarantine.Put(item->ref);
}
BEntry entry(&ref);
node_ref nodeRef;
status_t status = entry.GetNodeRef(&nodeRef);
if (status < B_OK)
return status;
BPath path(&ref);
image_id image = load_add_on(path.Path());
if (image < B_OK)
return image;
// Function pointer used to create post R4.5 style translators
BTranslator *(*makeNthTranslator)(int32 n, image_id you, uint32 flags, ...);
status = get_image_symbol(image, "make_nth_translator",
B_SYMBOL_TYPE_TEXT, (void**)&makeNthTranslator);
if (status == B_OK) {
// If the translator add-on supports the post R4.5
// translator creation mechanism, keep loading translators
// until MakeNthTranslator stops returning them.
BTranslator* translator = NULL;
int32 created = 0;
for (int32 n = 0; (translator = makeNthTranslator(n, image, 0)) != NULL;
n++) {
if (AddTranslator(translator, image, &ref, nodeRef.node) == B_OK) {
if (update)
update->AddInt32("translator_id", translator->fID);
fImageOrigins.insert(std::make_pair(translator, image));
count++;
created++;
} else {
translator->Release();
// this will delete the translator
}
}
if (created == 0) {
unload_add_on(image);
} else {
// Initial refcount for the image that was just loaded
fKnownImages.insert(std::make_pair(image, created));
}
quarantine.Remove();
return B_OK;
}
// If this is a translator add-on, it is in the C format
translator_data translatorData;
status = GetTranslatorData(image, translatorData);
// add this translator to the list
BPrivate::BFuncTranslator* translator = NULL;
if (status == B_OK) {
translator = new (std::nothrow) BPrivate::BFuncTranslator(
translatorData);
if (translator == NULL)
status = B_NO_MEMORY;
}
if (status == B_OK)
status = AddTranslator(translator, image, &ref, nodeRef.node);
if (status == B_OK) {
if (update)
update->AddInt32("translator_id", translator->fID);
quarantine.Remove();
count++;
} else
unload_add_on(image);
return status;
}
status_t
BTranslatorRoster::Private::StartWatching(BMessenger target)
{
try {
fMessengers.push_back(target);
} catch (...) {
return B_NO_MEMORY;
}
if (fLazyScanning) {
fLazyScanning = false;
// Since we now have someone to report to, we cannot lazily
// adopt changes to the translator any longer
_RescanChanged();
}
return B_OK;
}
status_t
BTranslatorRoster::Private::StopWatching(BMessenger target)
{
MessengerList::iterator iterator = fMessengers.begin();
while (iterator != fMessengers.end()) {
if (*iterator == target) {
fMessengers.erase(iterator);
if (fMessengers.empty())
fLazyScanning = true;
return B_OK;
}
iterator++;
}
return B_BAD_VALUE;
}
status_t
BTranslatorRoster::Private::StoreTranslators(BMessage& archive)
{
BAutolock locker(this);
TranslatorMap::const_iterator iterator = fTranslators.begin();
while (iterator != fTranslators.end()) {
const translator_item& item = iterator->second;
BPath path(&item.ref);
if (path.InitCheck() == B_OK)
archive.AddString("be:translator_path", path.Path());
iterator++;
}
return B_OK;
}
status_t
BTranslatorRoster::Private::Identify(BPositionIO* source,
BMessage* ioExtension, uint32 hintType, const char* hintMIME,
uint32 wantType, translator_info* _info)
{
BAutolock locker(this);
_RescanChanged();
TranslatorMap::const_iterator iterator = fTranslators.begin();
BMessage baseExtension;
if (ioExtension != NULL)
baseExtension = *ioExtension;
float bestWeight = 0.0f;
while (iterator != fTranslators.end()) {
BTranslator& translator = *iterator->second.translator;
off_t pos = source->Seek(0, SEEK_SET);
if (pos != 0)
return pos < 0 ? (status_t)pos : B_IO_ERROR;
int32 formatsCount = 0;
const translation_format* formats = translator.InputFormats(
&formatsCount);
const translation_format* format = _CheckHints(formats, formatsCount,
hintType, hintMIME);
BMessage extension(baseExtension);
translator_info info;
if (translator.Identify(source, format, &extension, &info, wantType)
== B_OK) {
float weight = info.quality * info.capability;
if (weight > bestWeight) {
if (ioExtension != NULL)
*ioExtension = extension;
bestWeight = weight;
info.translator = iterator->first;
memcpy(_info, &info, sizeof(translator_info));
}
}
iterator++;
}
if (bestWeight > 0.0f)
return B_OK;
return B_NO_TRANSLATOR;
}
status_t
BTranslatorRoster::Private::GetTranslators(BPositionIO* source,
BMessage* ioExtension, uint32 hintType, const char* hintMIME,
uint32 wantType, translator_info** _info, int32* _numInfo)
{
BAutolock locker(this);
_RescanChanged();
int32 arraySize = fTranslators.size();
translator_info* array = new (std::nothrow) translator_info[arraySize];
if (array == NULL)
return B_NO_MEMORY;
TranslatorMap::const_iterator iterator = fTranslators.begin();
int32 count = 0;
while (iterator != fTranslators.end()) {
BTranslator& translator = *iterator->second.translator;
off_t pos = source->Seek(0, SEEK_SET);
if (pos != 0) {
delete[] array;
return pos < 0 ? status_t(pos) : B_IO_ERROR;
}
int32 formatsCount = 0;
const translation_format* formats = translator.InputFormats(
&formatsCount);
const translation_format* format = _CheckHints(formats, formatsCount,
hintType, hintMIME);
translator_info info;
if (translator.Identify(source, format, ioExtension, &info, wantType)
== B_OK) {
info.translator = iterator->first;
array[count++] = info;
}
iterator++;
}
*_info = array;
*_numInfo = count;
qsort(array, count, sizeof(translator_info),
BTranslatorRoster::Private::_CompareSupport);
// translators are sorted by best support
return B_OK;
}
status_t
BTranslatorRoster::Private::GetAllTranslators(translator_id** _ids,
int32* _count)
{
BAutolock locker(this);
_RescanChanged();
int32 arraySize = fTranslators.size();
translator_id* array = new (std::nothrow) translator_id[arraySize];
if (array == NULL)
return B_NO_MEMORY;
TranslatorMap::const_iterator iterator = fTranslators.begin();
int32 count = 0;
while (iterator != fTranslators.end()) {
array[count++] = iterator->first;
iterator++;
}
*_ids = array;
*_count = count;
return B_OK;
}
status_t
BTranslatorRoster::Private::GetRefFor(translator_id id, entry_ref& ref)
{
BAutolock locker(this);
const translator_item* item = _FindTranslator(id);
if (item == NULL)
return B_NO_TRANSLATOR;
BEntry entry(&item->ref);
if (entry.InitCheck() == B_OK && entry.Exists() && entry.IsFile()) {
ref = item->ref;
return B_OK;
}
return B_ERROR;
}
void
BTranslatorRoster::Private::_TranslatorDeleted(translator_id id, BTranslator* self)
{
BAutolock locker(this);
TranslatorMap::iterator iterator = fTranslators.find(id);
if (iterator != fTranslators.end())
fTranslators.erase(iterator);
image_id image = fImageOrigins[self];
delete self;
int32 former = atomic_add(&fKnownImages[image], -1);
if (former == 1) {
unload_add_on(image);
fImageOrigins.erase(self);
fKnownImages.erase(image);
}
}
/*static*/ int
BTranslatorRoster::Private::_CompareSupport(const void* _a, const void* _b)
{
const translator_info* infoA = (const translator_info*)_a;
const translator_info* infoB = (const translator_info*)_b;
float weightA = infoA->quality * infoA->capability;
float weightB = infoB->quality * infoB->capability;
if (weightA == weightB)
return 0;
if (weightA > weightB)
return -1;
return 1;
}
/*!
In lazy mode, freshly installed translator are not scanned immediately
when they become available. Instead, they are put into a set.
When a method is called that may be interested in these new translators,
they are scanned on the fly. Since lazy mode also means that this roster
does not have any listeners, we don't need to notify anyone about those
changes.
*/
void
BTranslatorRoster::Private::_RescanChanged()
{
while (!fRescanEntries.empty()) {
EntryRefSet::iterator iterator = fRescanEntries.begin();
int32 count;
CreateTranslators(*iterator, count);
fRescanEntries.erase(iterator);
}
}
/*!
Tests if the hints provided for a source stream are compatible to
the formats the translator exports.
*/
const translation_format*
BTranslatorRoster::Private::_CheckHints(const translation_format* formats,
int32 formatsCount, uint32 hintType, const char* hintMIME)
{
if (formats == NULL || formatsCount <= 0 || (!hintType && hintMIME == NULL))
return NULL;
// The provided MIME type hint may be a super type
int32 super = 0;
if (hintMIME && !strchr(hintMIME, '/'))
super = strlen(hintMIME);
// scan for suitable format
for (int32 i = 0; i < formatsCount && formats[i].type; i++) {
if (formats[i].type == hintType
|| (hintMIME
&& ((super && !strncmp(formats[i].MIME, hintMIME, super))
|| !strcmp(formats[i].MIME, hintMIME))))
return &formats[i];
}
return NULL;
}
const translator_item*
BTranslatorRoster::Private::_FindTranslator(translator_id id) const
{
TranslatorMap::const_iterator iterator = fTranslators.find(id);
if (iterator == fTranslators.end())
return NULL;
return &iterator->second;
}
const translator_item*
BTranslatorRoster::Private::_FindTranslator(const char* name) const
{
if (name == NULL)
return NULL;
TranslatorMap::const_iterator iterator = fTranslators.begin();
while (iterator != fTranslators.end()) {
const translator_item& item = iterator->second;
if (item.ref.name != NULL && !strcmp(item.ref.name, name))
return &item;
iterator++;
}
return NULL;
}
const translator_item*
BTranslatorRoster::Private::_FindTranslator(entry_ref& ref) const
{
if (ref.name == NULL)
return NULL;
TranslatorMap::const_iterator iterator = fTranslators.begin();
while (iterator != fTranslators.end()) {
const translator_item& item = iterator->second;
if (item.ref == ref)