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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
|
/****************************************************************************
**
** Implementation of MIME support
**
** Copyright (C) 2010 Timothy Pearson and (C) 1992-2008 Trolltech ASA.
**
** This file is part of the kernel module of the TQt GUI Toolkit.
**
** This file may be used under the terms of the GNU General
** Public License versions 2.0 or 3.0 as published by the Free
** Software Foundation and appearing in the files LICENSE.GPL2
** and LICENSE.GPL3 included in the packaging of this file.
** Alternatively you may (at your option) use any later version
** of the GNU General Public License if such license has been
** publicly approved by Trolltech ASA (or its successors, if any)
** and the KDE Free TQt Foundation.
**
** Please review the following information to ensure GNU General
** Public Licensing requirements will be met:
** http://trolltech.com/products/qt/licenses/licensing/opensource/.
** If you are unsure which license is appropriate for your use, please
** review the following information:
** http://trolltech.com/products/qt/licenses/licensing/licensingoverview
** or contact the sales department at sales@trolltech.com.
**
** This file may be used under the terms of the Q Public License as
** defined by Trolltech ASA and appearing in the file LICENSE.TQPL
** included in the packaging of this file. Licensees holding valid TQt
** Commercial licenses may use this file in accordance with the TQt
** Commercial License Agreement provided with the Software.
**
** This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted
** herein.
**
**********************************************************************/
#include "tqmime.h"
#ifndef TQT_NO_MIME
#include "tqmap.h"
#include "tqstringlist.h"
#include "tqfileinfo.h"
#include "tqdir.h"
#include "tqdragobject.h"
#include "tqcleanuphandler.h"
#include "tqapplication.h" // ### for now
#include "tqclipboard.h" // ### for now
#ifdef USE_QT4
#include "Qt/qmap.h"
#include "Qt/qstringlist.h"
#include "Qt/qfileinfo.h"
#include "Qt/qdir.h"
#include "tqdragobject.h"
#include "Qt/qpixmap.h"
#include "Qt/qimagereader.h"
#include "tqcleanuphandler.h"
#include "private/qt4_qtextimagehandler_p.h"
#endif // USE_QT4
static int qt_mime_serial_number = 0;
static TQMimeSourceFactory* defaultfactory = 0;
static TQSingleCleanupHandler<TQMimeSourceFactory> qmime_cleanup_factory;
#ifdef USE_QT4
// #if 0
TQMimeSource::TQMimeSource() : QMimeSource() {
cacheType = NoCache;
ser_no = qt_mime_serial_number++;
}
/*!
\fn int TQMimeSource::serialNumber() const
Returns the mime source's globally unique serial number.
*/
void TQMimeSource::clearCache()
{
if ( cacheType == Text ) {
delete cache.txt.str;
delete cache.txt.subtype;
cache.txt.str = 0;
cache.txt.subtype = 0;
} else if ( cacheType == Graphics ) {
delete cache.gfx.img;
delete cache.gfx.pix;
cache.gfx.img = 0;
cache.gfx.pix = 0;
}
cacheType = NoCache;
}
#else // USE_QT4
/*!
\class TQMimeSource tqmime.h
\brief The TQMimeSource class is an abstraction of objects which provide formatted data of a certain MIME type.
\ingroup io
\ingroup draganddrop
\ingroup misc
\link dnd.html Drag-and-drop\endlink and
\link TQClipboard clipboard\endlink use this abstraction.
\sa \link http://www.isi.edu/in-notes/iana/assignments/media-types/
IANA list of MIME media types\endlink
*/
/*!
Constructs a mime source and assigns a globally unique serial
number to it.
\sa serialNumber()
*/
TQMimeSource::TQMimeSource()
{
ser_no = qt_mime_serial_number++;
cacheType = NoCache;
}
/*!
\fn int TQMimeSource::serialNumber() const
Returns the mime source's globally unique serial number.
*/
void TQMimeSource::clearCache()
{
if ( cacheType == Text ) {
delete cache.txt.str;
delete cache.txt.subtype;
cache.txt.str = 0;
cache.txt.subtype = 0;
} else if ( cacheType == Graphics ) {
delete cache.gfx.img;
delete cache.gfx.pix;
cache.gfx.img = 0;
cache.gfx.pix = 0;
}
cacheType = NoCache;
}
/*!
Provided to ensure that subclasses destroy themselves correctly.
*/
TQMimeSource::~TQMimeSource()
{
#ifndef TQT_NO_CLIPBOARD
extern void qt_clipboard_cleanup_mime_source(TQMimeSource *);
qt_clipboard_cleanup_mime_source(this);
#endif
clearCache();
}
/*!
\fn TQByteArray TQMimeSource::tqencodedData(const char*) const
Returns the encoded data of this object in the specified MIME
format.
Subclasses must reimplement this function.
*/
/*!
Returns TRUE if the object can provide the data in format \a
mimeType; otherwise returns FALSE.
If you inherit from TQMimeSource, for consistency reasons it is
better to implement the more abstract canDecode() functions such
as TQTextDrag::canDecode() and TQImageDrag::canDecode().
*/
bool TQMimeSource::provides(const char* mimeType) const
{
const char* fmt;
for (int i=0; (fmt = format(i)); i++) {
if ( !qstricmp(mimeType,fmt) )
return TRUE;
}
return FALSE;
}
#endif // USE_QT4
// #ifdef USE_QT4
#if 0
QT_BEGIN_NAMESPACE
class TQMimeSourceFactoryData {
public:
TQMimeSourceFactoryData() :
last(0)
{
}
~TQMimeSourceFactoryData()
{
QMap<QString, QMimeSource*>::Iterator it = stored.begin();
while (it != stored.end()) {
delete *it;
++it;
}
delete last;
}
QMap<QString, QMimeSource*> stored;
QMap<QString, QString> extensions;
QStringList path;
QMimeSource* last;
QList<TQMimeSourceFactory*> factories;
};
static QImage richTextImageLoader(const QString &name, const QString &context)
{
QImage img;
const TQMimeSource *src = static_cast<const TQMimeSource*>(TQMimeSourceFactory::defaultFactory()->data(name, context));
if (src && TQImageDrag::decode(src, img))
return img;
return QImage();
}
/*!
\class TQMimeSourceFactory
\brief The TQMimeSourceFactory class is an extensible provider of mime-typed data.
\compat
A TQMimeSourceFactory provides an abstract interface to a
collection of information. Each piece of information is
represented by a QMimeSource object which can be examined and
converted to concrete data types by functions such as
TQImageDrag::canDecode() and TQImageDrag::decode().
The base TQMimeSourceFactory can be used in two ways: as an
abstraction of a collection of files or as specifically stored
data. For it to access files, call setFilePath() before accessing
data. For stored data, call setData() for each item (there are
also convenience functions, e.g. setText(), setImage() and
setPixmap(), that simply call setData() with appropriate
parameters).
The rich text widgets, QTextEdit and QTextBrowser, use
TQMimeSourceFactory to resolve references such as images or links
within rich text documents. They either access the default factory
(see \l{defaultFactory()}) or their own. Other classes that are
capable of displaying rich text (such as QLabel, QWhatsThis or
QMessageBox) always use the default factory.
A factory can also be used as a container to store data associated
with a name. This technique is useful whenever rich text contains
images that are stored in the program itself, not loaded from the
hard disk. Your program may, for example, define some image data
as:
\snippet doc/src/snippets/code/src_qt3support_other_q3mimefactory.cpp 0
To be able to use this image within some rich text, for example
inside a QLabel, you must create a QImage from the raw data and
insert it into the factory with a unique name:
\snippet doc/src/snippets/code/src_qt3support_other_q3mimefactory.cpp 1
Now you can create a rich text QLabel with
\snippet doc/src/snippets/code/src_qt3support_other_q3mimefactory.cpp 2
When no longer needed, you can clear the data from the factory:
\snippet doc/src/snippets/code/src_qt3support_other_q3mimefactory.cpp 3
*/
/*!
Constructs a TQMimeSourceFactory that has no file path and no
stored content.
*/
TQMimeSourceFactory::TQMimeSourceFactory() :
d(new TQMimeSourceFactoryData)
{
// addFilePath(QLatin1String(":/qt/q3mimesourcefactory/")); //to get from the resources // Where did this come from??!?!?!?
// add some reasonable defaults
setExtensionType(QLatin1String("htm"), "text/html;charset=iso8859-1");
setExtensionType(QLatin1String("html"), "text/html;charset=iso8859-1");
setExtensionType(QLatin1String("txt"), "text/plain");
setExtensionType(QLatin1String("xml"), "text/xml;charset=UTF-8");
setExtensionType(QLatin1String("jpg"), "image/jpeg"); // support misspelled jpeg files
}
/*!
Destroys the TQMimeSourceFactory, deleting all stored content.
*/
TQMimeSourceFactory::~TQMimeSourceFactory()
{
if (defaultFactory() == this)
defaultfactory = 0;
delete d;
}
QMimeSource* TQMimeSourceFactory::dataInternal(const QString& abs_name, const QMap<QString, QString> &extensions) const
{
QMimeSource* r = 0;
QStringList attempted_names(abs_name);
TQFileInfo fi(abs_name);
if (fi.isReadable()) {
// get the right mimetype
QString e = fi.extension(false);
QByteArray mimetype("application/octet-stream");
if (extensions.contains(e))
mimetype = TQT_TQSTRING(extensions[e]).latin1();
if (!QImageReader::imageFormat(abs_name).isEmpty())
mimetype = "application/x-qt-image";
TQFile f(abs_name);
if (f.open(QIODevice::ReadOnly) && f.size()) {
QByteArray ba;
ba.resize(f.size());
f.readBlock(ba.data(), ba.size());
TQStoredDrag* sr = new TQStoredDrag(mimetype);
sr->setEncodedData(TQT_TQBYTEARRAY_OBJECT(ba));
delete d->last;
d->last = r = sr;
}
}
// we didn't find the mime-source, so ask the default factory for
// the mime-source (this one will iterate over all installed ones)
//
// this looks dangerous, as this dataInternal() function will be
// called again when the default factory loops over all installed
// factories (including this), but the static bool looping in
// data() avoids endless recursions
if (!r && this != defaultFactory())
r = (QMimeSource*)defaultFactory()->data(abs_name);
return r;
}
/*!
Returns a reference to the data associated with \a abs_name. The
return value remains valid only until the next data() or setData()
call, so you should immediately decode the result.
If there is no data associated with \a abs_name in the factory's
store, the factory tries to access the local filesystem. If \a
abs_name isn't an absolute file name, the factory will search for
it in all defined paths (see \l{setFilePath()}).
The factory understands all the image formats supported by
QImageReader. Any other mime types are determined by the file name
extension. The default settings are
\snippet doc/src/snippets/code/src_qt3support_other_q3mimefactory.cpp 4
The effect of these is that file names ending in "txt" will be
treated as text encoded in the local encoding; those ending in
"xml" will be treated as text encoded in Unicode UTF-8 encoding.
The text/html type is treated specially, since the encoding can be
specified in the html file itself. "html" or "htm" will be treated
as text encoded in the encoding specified by the html meta tag, if
none could be found, the charset of the mime type will be used.
The text subtype ("html", "plain", or "xml") does not affect the
factory, but users of the factory may behave differently. We
recommend creating "xml" files where practical. These files can be
viewed regardless of the runtime encoding and can encode any
Unicode characters without resorting to encoding definitions
inside the file.
Any file data that is not recognized will be retrieved as a
QMimeSource providing the "application/octet-stream" mime type,
meaning uninterpreted binary data.
You can add further extensions or change existing ones with
subsequent calls to setExtensionType(). If the extension mechanism
is not sufficient for your problem domain, you can inherit
TQMimeSourceFactory and reimplement this function to perform some
more specialized mime-type detection. The same applies if you want
to use the mime source factory to access URL referenced data over
a network.
*/
const TQMimeSource *TQMimeSourceFactory::data(const QString& abs_name) const
{
if (d->stored.contains(abs_name))
return static_cast<TQMimeSource*>(d->stored[abs_name]);
const QMimeSource *r = 0;
if (abs_name.isEmpty())
return static_cast<const TQMimeSource*>(r);
QStringList::Iterator it;
if (abs_name[0] == QLatin1Char('/')
#ifdef Q_WS_WIN
|| (abs_name[0].isLetter() && abs_name[1] == QLatin1Char(':')) || abs_name.startsWith(QLatin1String("\\\\"))
#endif
)
{
// handle absolute file names directly
r = dataInternal(abs_name, d->extensions);
}
else { // check list of paths
for (it = d->path.begin(); !r && it != d->path.end(); ++it) {
QString filename = *it;
if (filename[(int)filename.length()-1] != QLatin1Char('/'))
filename += QLatin1Char('/');
filename += abs_name;
r = dataInternal(filename, d->extensions);
}
}
static bool looping = false;
if (!r && this == defaultFactory()) {
// we found no mime-source and we are the default factory, so
// we know all the other installed mime-source factories, so
// ask them
if (!looping) {
// to avoid endless recustions, don't enter the loop below
// if data() got called from within the loop below
looping = true;
for (int i = 0; i < d->factories.size(); ++i) {
const TQMimeSourceFactory *f = d->factories.at(i);
if (f == this)
continue;
r = static_cast<const QMimeSource *>(f->data(abs_name));
if (r) {
looping = false;
return static_cast<const TQMimeSource*>(r);
}
}
looping = false;
}
} else if (!r) {
// we are not the default mime-source factory, so ask the
// default one for the mime-source, as this one will loop over
// all installed mime-source factories and ask these
r = static_cast<const QMimeSource *>(defaultFactory()->data(abs_name));
}
return static_cast<const TQMimeSource*>(r);
}
/*!
\fn void TQMimeSourceFactory::setFilePath(const QStringList &path)
\fn void TQMimeSourceFactory::setFilePath(const QString &path)
Sets the list of directories that will be searched when named data
is requested to those given in the string list \a path.
\sa filePath()
*/
void TQMimeSourceFactory::setFilePath(const QStringList& path)
{
d->path = path;
}
/*!
Returns the currently set search paths.
*/
TQStringList TQMimeSourceFactory::filePath() const
{
return TQT_TQSTRINGLIST_OBJECT(d->path);
}
/*!
Adds another search path, \a p to the existing search paths.
\sa setFilePath()
*/
void TQMimeSourceFactory::addFilePath(const QString& p)
{
d->path += p;
}
/*!
Sets the mime-type to be associated with the file name extension,
\a ext to \a mimetype. This determines the mime-type for files
found via the paths set by setFilePath().
*/
void TQMimeSourceFactory::setExtensionType(const QString& ext, const char* mimetype)
{
d->extensions.insert(ext, QLatin1String(mimetype));
}
/*!
Converts the absolute or relative data item name \a
abs_or_rel_name to an absolute name, interpreted within the
context (path) of the data item named \a context (this must be an
absolute name).
*/
TQString TQMimeSourceFactory::makeAbsolute(const QString& abs_or_rel_name, const QString& context) const
{
if (context.isNull() ||
!(context[0] == QLatin1Char('/')
#ifdef Q_WS_WIN
|| (context[0].isLetter() && context[1] == QLatin1Char(':'))
#endif
))
return abs_or_rel_name;
if (abs_or_rel_name.isEmpty())
return context;
TQFileInfo c(context);
if (!c.isDir()) {
TQFileInfo r(c.dir(true), abs_or_rel_name);
return r.absFilePath();
} else {
TQDir d(context);
TQFileInfo r(d, abs_or_rel_name);
return r.absFilePath();
}
}
/*!
\overload
A convenience function. See data(const QString& abs_name). The
file name is given in \a abs_or_rel_name and the path is in \a
context.
*/
const TQMimeSource* TQMimeSourceFactory::data(const QString& abs_or_rel_name, const QString& context) const
{
const QMimeSource* r = data(makeAbsolute(abs_or_rel_name,context));
if (!r && !d->path.isEmpty())
r = data(abs_or_rel_name);
return static_cast<const TQMimeSource*>(r);
}
/*!
Sets \a text to be the data item associated with the absolute name
\a abs_name.
Equivalent to setData(abs_name, new TQTextDrag(text)).
*/
void TQMimeSourceFactory::setText(const QString& abs_name, const QString& text)
{
setData(abs_name, new TQTextDrag(text));
}
/*!
Sets \a image to be the data item associated with the absolute
name \a abs_name.
Equivalent to setData(abs_name, new TQImageDrag(image)).
*/
void TQMimeSourceFactory::setImage(const QString& abs_name, const QImage& image)
{
setData(abs_name, new TQImageDrag(image));
}
/*!
Sets \a pixmap to be the data item associated with the absolute
name \a abs_name.
*/
void TQMimeSourceFactory::setPixmap(const QString& abs_name, const QPixmap& pixmap)
{
setData(abs_name, new TQImageDrag(TQT_TQPIXMAP_OBJECT(pixmap).convertToImage()));
}
/*!
Sets \a data to be the data item associated with
the absolute name \a abs_name. Note that the ownership of \a data is
transferred to the factory: do not delete or access the pointer after
passing it to this function.
Passing 0 for data removes previously stored data.
*/
void TQMimeSourceFactory::setData(const QString& abs_name, QMimeSource* data)
{
if (d->stored.contains(abs_name))
delete d->stored[abs_name];
d->stored.insert(abs_name,data);
}
/*!
Returns the application-wide default mime source factory. This
factory is used by rich text rendering classes such as
QSimpleRichText, QWhatsThis and QMessageBox to resolve named
references within rich text documents. It serves also as the
initial factory for the more complex render widgets, QTextEdit and
QTextBrowser.
\sa setDefaultFactory()
*/
TQMimeSourceFactory* TQMimeSourceFactory::defaultFactory()
{
if (!defaultfactory)
{
defaultfactory = new TQMimeSourceFactory();
qmime_cleanup_factory.set(&defaultfactory);
QTextImageHandler::externalLoader = richTextImageLoader;
}
return defaultfactory;
}
/*!
Sets the default \a factory, destroying any previously set mime
source provider. The ownership of the factory is transferred to
Qt.
\sa defaultFactory()
*/
void TQMimeSourceFactory::setDefaultFactory(TQMimeSourceFactory* factory)
{
if (!defaultfactory)
qmime_cleanup_factory.set(&defaultfactory);
else if (defaultfactory != factory)
delete defaultfactory;
defaultfactory = factory;
}
/*!
Sets the defaultFactory() to 0 and returns the previous one.
*/
TQMimeSourceFactory* TQMimeSourceFactory::takeDefaultFactory()
{
TQMimeSourceFactory *f = defaultfactory;
defaultfactory = 0;
return f;
}
/*!
Adds the TQMimeSourceFactory \a f to the list of available
mimesource factories. If the defaultFactory() can't resolve a
data() it iterates over the list of installed mimesource factories
until the data can be resolved.
\sa removeFactory()
*/
void TQMimeSourceFactory::addFactory(TQMimeSourceFactory *f)
{
TQMimeSourceFactory::defaultFactory()->d->factories.append(f);
}
/*!
Removes the mimesource factory \a f from the list of available
mimesource factories.
\sa addFactory()
*/
void TQMimeSourceFactory::removeFactory(TQMimeSourceFactory *f)
{
TQMimeSourceFactory::defaultFactory()->d->factories.removeAll(f);
}
QPixmap qPixmapFromMimeSource(const QString &abs_name)
{
const TQMimeSource *m = TQT_TQMIMESOURCE_CONST(TQMimeSourceFactory::defaultFactory()->data(abs_name));
if (!m) {
if (QFile::exists(abs_name))
return QPixmap(abs_name);
if (!abs_name.isEmpty())
qWarning("QPixmap::fromMimeSource: Cannot find pixmap \"%s\" in the mime source factory",
TQT_TQSTRING(abs_name).latin1());
return QPixmap();
}
QPixmap pix;
TQImageDrag::decode(m, pix);
return pix;
}
QImage qImageFromMimeSource(const QString &abs_name)
{
const TQMimeSource *m = TQT_TQMIMESOURCE_CONST(TQMimeSourceFactory::defaultFactory()->data(abs_name));
if (!m) {
qWarning("QImage::fromMimeSource: Cannot find image \"%s\" in the mime source factory", TQT_TQSTRING(abs_name).latin1());
return QImage();
}
QImage img;
TQImageDrag::decode(m, img);
return img;
}
QT_END_NAMESPACE
#else // USE_QT4
/*!
\fn const char * TQMimeSource::format(int i) const
Returns the \a{i}-th supported MIME format, or 0.
*/
class TQMimeSourceFactoryData {
public:
TQMimeSourceFactoryData() :
last(0)
{
}
~TQMimeSourceFactoryData()
{
TQMap<TQString, TQMimeSource*>::Iterator it = stored.begin();
while ( it != stored.end() ) {
delete *it;
++it;
}
delete last;
}
TQMap<TQString, TQMimeSource*> stored;
TQMap<TQString, TQString> extensions;
TQStringList path;
TQMimeSource* last;
TQPtrList<TQMimeSourceFactory> factories;
};
/*!
\class TQMimeSourceFactory tqmime.h
\brief The TQMimeSourceFactory class is an extensible provider of mime-typed data.
\ingroup io
\ingroup environment
A TQMimeSourceFactory provides an abstract interface to a
collection of information. Each piece of information is
represented by a TQMimeSource object which can be examined and
converted to concrete data types by functions such as
TQImageDrag::canDecode() and TQImageDrag::decode().
The base TQMimeSourceFactory can be used in two ways: as an
abstraction of a collection of files or as specifically stored
data. For it to access files, call setFilePath() before accessing
data. For stored data, call setData() for each item (there are
also convenience functions, e.g. setText(), setImage() and
setPixmap(), that simply call setData() with appropriate
parameters).
The rich text widgets, TQTextEdit and TQTextBrowser, use
TQMimeSourceFactory to resolve references such as images or links
within rich text documents. They either access the default factory
(see \l{defaultFactory()}) or their own (see
\l{TQTextEdit::setMimeSourceFactory()}). Other classes that are
capable of displaying rich text (such as TQLabel, TQWhatsThis or
TQMessageBox) always use the default factory.
A factory can also be used as a container to store data associated
with a name. This technique is useful whenever rich text contains
images that are stored in the program itself, not loaded from the
hard disk. Your program may, for example, define some image data
as:
\code
static const char* myimage_data[]={
"...",
...
"..."};
\endcode
To be able to use this image within some rich text, for example
inside a TQLabel, you must create a TQImage from the raw data and
insert it into the factory with a unique name:
\code
TQMimeSourceFactory::defaultFactory()->setImage( "myimage", TQImage(myimage_data) );
\endcode
Now you can create a rich text TQLabel with
\code
TQLabel* label = new TQLabel(
"Rich text with embedded image:<img source=\"myimage\">"
"Isn't that <em>cute</em>?" );
\endcode
When no longer needed, you can clear the data from the factory:
\code
delete label;
TQMimeSourceFactory::defaultFactory()->setData( "myimage", 0 );
\endcode
*/
/*!
Constructs a TQMimeSourceFactory that has no file path and no
stored content.
*/
TQMimeSourceFactory::TQMimeSourceFactory() :
d(new TQMimeSourceFactoryData)
{
// add some reasonable defaults
setExtensionType("htm", "text/html;charset=iso8859-1");
setExtensionType("html", "text/html;charset=iso8859-1");
setExtensionType("txt", "text/plain");
setExtensionType("xml", "text/xml;charset=UTF-8");
setExtensionType("jpg", "image/jpeg"); // support misspelled jpeg files
}
/*!
Destroys the TQMimeSourceFactory, deleting all stored content.
*/
TQMimeSourceFactory::~TQMimeSourceFactory()
{
if ( defaultFactory() == this )
defaultfactory = 0;
delete d;
}
TQMimeSource* TQMimeSourceFactory::dataInternal(const TQString& abs_name, const TQMap<TQString, TQString> &extensions ) const
{
TQMimeSource* r = 0;
TQFileInfo fi(abs_name);
if ( fi.isReadable() ) {
// get the right mimetype
TQString e = fi.extension(FALSE);
TQCString mimetype = "application/octet-stream";
const char* imgfmt;
if ( extensions.contains(e) )
mimetype = extensions[e].latin1();
else if ( ( imgfmt = TQImage::imageFormat( abs_name ) ) )
mimetype = TQCString("image/")+TQCString(imgfmt).lower();
TQFile f(abs_name);
if ( f.open(IO_ReadOnly) && f.size() ) {
TQByteArray ba(f.size());
f.readBlock(ba.data(), ba.size());
TQStoredDrag* sr = new TQStoredDrag( mimetype );
sr->setEncodedData( ba );
delete d->last;
d->last = r = sr;
}
}
// we didn't find the mime-source, so ask the default factory for
// the mime-source (this one will iterate over all installed ones)
//
// this looks dangerous, as this dataInternal() function will be
// called again when the default factory loops over all installed
// factories (including this), but the static bool looping in
// data() avoids endless recursions
if ( !r && this != defaultFactory() )
r = (TQMimeSource*)defaultFactory()->data( abs_name );
return r;
}
/*!
Returns a reference to the data associated with \a abs_name. The
return value remains valid only until the next data() or setData()
call, so you should immediately decode the result.
If there is no data associated with \a abs_name in the factory's
store, the factory tries to access the local filesystem. If \a
abs_name isn't an absolute file name, the factory will search for
it in all defined paths (see \l{setFilePath()}).
The factory understands all the image formats supported by
TQImageIO. Any other mime types are determined by the file name
extension. The default settings are
\code
setExtensionType("html", "text/html;charset=iso8859-1");
setExtensionType("htm", "text/html;charset=iso8859-1");
setExtensionType("txt", "text/plain");
setExtensionType("xml", "text/xml;charset=UTF-8");
\endcode
The effect of these is that file names ending in "txt" will be
treated as text encoded in the local encoding; those ending in
"xml" will be treated as text encoded in Unicode UTF-8 encoding.
The text/html type is treated specially, since the encoding can be
specified in the html file itself. "html" or "htm" will be treated
as text encoded in the encoding specified by the html meta tag, if
none could be found, the charset of the mime type will be used.
The text subtype ("html", "plain", or "xml") does not affect the
factory, but users of the factory may behave differently. We
recommend creating "xml" files where practical. These files can be
viewed regardless of the runtime encoding and can encode any
Unicode characters without resorting to encoding definitions
inside the file.
Any file data that is not recognized will be retrieved as a
TQMimeSource providing the "application/octet-stream" mime type,
meaning uninterpreted binary data.
You can add further extensions or change existing ones with
subsequent calls to setExtensionType(). If the extension mechanism
is not sufficient for your problem domain, you can inherit
TQMimeSourceFactory and reimplement this function to perform some
more specialized mime-type detection. The same applies if you want
to use the mime source factory to access URL referenced data over
a network.
*/
const TQMimeSource* TQMimeSourceFactory::data(const TQString& abs_name) const
{
if ( d->stored.contains(abs_name) )
return d->stored[abs_name];
TQMimeSource* r = 0;
TQStringList::Iterator it;
if ( abs_name[0] == '/'
#ifdef TQ_WS_WIN
|| ( abs_name[0] && abs_name[1] == ':' ) || abs_name.startsWith("\\\\")
#endif
)
{
// handle absolute file names directly
r = dataInternal( abs_name, d->extensions);
}
else { // check list of paths
for ( it = d->path.begin(); !r && it != d->path.end(); ++it ) {
TQString filename = *it;
if ( filename[(int)filename.length()-1] != '/' )
filename += '/';
filename += abs_name;
r = dataInternal( filename, d->extensions );
}
}
static bool looping = FALSE;
if ( !r && this == defaultFactory() ) {
// we found no mime-source and we are the default factory, so
// we know all the other installed mime-source factories, so
// ask them
if ( !looping ) {
// to avoid endless recursions, don't enter the loop below
// if data() got called from within the loop below
looping = TRUE;
TQPtrListIterator<TQMimeSourceFactory> it( d->factories );
TQMimeSourceFactory *f;
while ( ( f = it.current() ) ) {
++it;
if ( f == this )
continue;
r = (TQMimeSource*)f->data( abs_name );
if ( r ) {
looping = FALSE;
return r;
}
}
looping = FALSE;
}
} else if ( !r ) {
// we are not the default mime-source factory, so ask the
// default one for the mime-source, as this one will loop over
// all installed mime-source factories and ask these
r = (TQMimeSource*)defaultFactory()->data( abs_name );
}
return r;
}
/*!
Sets the list of directories that will be searched when named data
is requested to the those given in the string list \a path.
\sa filePath()
*/
void TQMimeSourceFactory::setFilePath( const TQStringList& path )
{
d->path = path;
}
/*!
Returns the currently set search paths.
*/
TQStringList TQMimeSourceFactory::filePath() const
{
return d->path;
}
/*!
Adds another search path, \a p to the existing search paths.
\sa setFilePath()
*/
void TQMimeSourceFactory::addFilePath( const TQString& p )
{
d->path += p;
}
/*!
Sets the mime-type to be associated with the file name extension,
\a ext to \a mimetype. This determines the mime-type for files
found via the paths set by setFilePath().
*/
void TQMimeSourceFactory::setExtensionType( const TQString& ext, const char* mimetype )
{
d->extensions.replace(ext, mimetype);
}
/*!
Converts the absolute or relative data item name \a
abs_or_rel_name to an absolute name, interpreted within the
context (path) of the data item named \a context (this must be an
absolute name).
*/
TQString TQMimeSourceFactory::makeAbsolute(const TQString& abs_or_rel_name, const TQString& context) const
{
if ( context.isNull() ||
!(context[0] == '/'
#ifdef TQ_WS_WIN
|| ( context[0] && context[1] == ':')
#endif
))
return abs_or_rel_name;
if ( abs_or_rel_name.isEmpty() )
return context;
TQFileInfo c( context );
if (!c.isDir()) {
TQFileInfo r( c.dir(TRUE), abs_or_rel_name );
return r.absFilePath();
} else {
TQDir d(context);
TQFileInfo r(d, abs_or_rel_name);
return r.absFilePath();
}
}
/*!
\overload
A convenience function. See data(const TQString& abs_name). The
file name is given in \a abs_or_rel_name and the path is in \a
context.
*/
const TQMimeSource* TQMimeSourceFactory::data(const TQString& abs_or_rel_name, const TQString& context) const
{
const TQMimeSource* r = data(makeAbsolute(abs_or_rel_name,context));
if ( !r && !d->path.isEmpty() )
r = data(abs_or_rel_name);
return r;
}
/*!
Sets \a text to be the data item associated with the absolute name
\a abs_name.
Equivalent to setData(abs_name, new TQTextDrag(text)).
*/
void TQMimeSourceFactory::setText( const TQString& abs_name, const TQString& text )
{
setData(abs_name, new TQTextDrag(text));
}
/*!
Sets \a image to be the data item associated with the absolute
name \a abs_name.
Equivalent to setData(abs_name, new TQImageDrag(image)).
*/
void TQMimeSourceFactory::setImage( const TQString& abs_name, const TQImage& image )
{
setData(abs_name, new TQImageDrag(image));
}
/*!
Sets \a pixmap to be the data item associated with the absolute
name \a abs_name.
*/
void TQMimeSourceFactory::setPixmap( const TQString& abs_name, const TQPixmap& pixmap )
{
setData(abs_name, new TQImageDrag(pixmap.convertToImage()));
}
/*!
Sets \a data to be the data item associated with
the absolute name \a abs_name. Note that the ownership of \a data is
transferred to the factory: do not delete or access the pointer after
passing it to this function.
Passing 0 for data removes previously stored data.
*/
void TQMimeSourceFactory::setData( const TQString& abs_name, TQMimeSource* data )
{
if ( d->stored.contains(abs_name) )
delete d->stored[abs_name];
d->stored.replace(abs_name,data);
}
/*!
Returns the application-wide default mime source factory. This
factory is used by rich text rendering classes such as
TQSimpleRichText, TQWhatsThis and TQMessageBox to resolve named
references within rich text documents. It serves also as the
initial factory for the more complex render widgets, TQTextEdit and
TQTextBrowser.
\sa setDefaultFactory()
*/
TQMimeSourceFactory* TQMimeSourceFactory::defaultFactory()
{
if (!defaultfactory)
{
defaultfactory = new TQMimeSourceFactory();
qmime_cleanup_factory.set( &defaultfactory );
}
return defaultfactory;
}
/*!
Sets the default \a factory, destroying any previously set mime
source provider. The ownership of the factory is transferred to
TQt.
\sa defaultFactory()
*/
void TQMimeSourceFactory::setDefaultFactory( TQMimeSourceFactory* factory)
{
if ( !defaultfactory )
qmime_cleanup_factory.set( &defaultfactory );
else if ( defaultfactory != factory )
delete defaultfactory;
defaultfactory = factory;
}
/*!
Sets the defaultFactory() to 0 and returns the previous one.
*/
TQMimeSourceFactory* TQMimeSourceFactory::takeDefaultFactory()
{
TQMimeSourceFactory *f = defaultfactory;
defaultfactory = 0;
return f;
}
/*!
Adds the TQMimeSourceFactory \a f to the list of available
mimesource factories. If the defaultFactory() can't resolve a
data() it iterates over the list of installed mimesource factories
until the data can be resolved.
\sa removeFactory();
*/
void TQMimeSourceFactory::addFactory( TQMimeSourceFactory *f )
{
TQMimeSourceFactory::defaultFactory()->d->factories.append( f );
}
/*!
Removes the mimesource factory \a f from the list of available
mimesource factories.
\sa addFactory();
*/
void TQMimeSourceFactory::removeFactory( TQMimeSourceFactory *f )
{
TQMimeSourceFactory::defaultFactory()->d->factories.removeRef( f );
}
#endif // USE_QT4
#endif // TQT_NO_MIME
|