summaryrefslogtreecommitdiffstats
path: root/examples/qtmail/browserwidget.cpp
blob: e9a7e2cf75838aba2970dfa67f72e224e06ee78e (plain)
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
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the Qt Messaging Framework.
**
** $QT_BEGIN_LICENSE:LGPL21$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** As a special exception, The Qt Company gives you certain additional
** rights. These rights are described in The Qt Company LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "browserwidget.h"

#include <qmailaddress.h>
#include <qmailmessage.h>
#include <qmailtimestamp.h>
#include "qmaillog.h"

#include <QApplication>
#include <QImageReader>
#include <QKeyEvent>
#include <QMenu>
#include <QStyle>
#include <QTextBrowser>
#include <QVBoxLayout>
#ifdef USE_WEBKIT
#include <QBuffer>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QTimer>
#include <QWebView>
#else
#include <QMap>
#include <QVariant>
#endif

#include <limits.h>

static QColor replyColor(Qt::darkGreen);

static QString dateString(const QDateTime& dt)
{
    QDateTime current = QDateTime::currentDateTime();
    if (dt.date() == current.date()) {
        //today
        return QString(qApp->translate("Browser", "Today %1")).arg(dt.toString("h:mm:ss ap"));
    } else if (dt.daysTo(current) == 1) {
        //yesterday
        return QString(qApp->translate("Browser", "Yesterday %1")).arg(dt.toString("h:mm:ss ap"));
    } else if (dt.daysTo(current) < 7) {
        //within 7 days
        return dt.toString("dddd h:mm:ss ap");
    } else {
        return dt.toString("dd/MM/yy h:mm:ss ap");
    }
}

#ifdef USE_WEBKIT
class ContentReply : public QNetworkReply
{
    Q_OBJECT

public:
    ContentReply(QObject *parent, QByteArray *data, const QString &contentType);
    ~ContentReply();

    qint64 bytesAvailable() const;
    qint64 readData(char *data, qint64 maxSize);

    void abort();

private:
    QBuffer m_buffer;
};

ContentReply::ContentReply(QObject *parent, QByteArray *data, const QString &contentType)
    : QNetworkReply(parent)
{
    setOpenMode(QIODevice::ReadOnly | QIODevice::Unbuffered);
    setHeader(QNetworkRequest::ContentTypeHeader, contentType);

    m_buffer.setBuffer(data);
    m_buffer.open(QIODevice::ReadOnly);

    QTimer::singleShot(0, this, SIGNAL(readyRead()));
    QTimer::singleShot(0, this, SIGNAL(finished()));
}

ContentReply::~ContentReply()
{
}

qint64 ContentReply::bytesAvailable() const
{
    return m_buffer.bytesAvailable() + QIODevice::bytesAvailable();
}

qint64 ContentReply::readData(char *data, qint64 maxSize)
{
    return m_buffer.read(data, maxSize);
}

void ContentReply::abort()
{
    m_buffer.close();
}

class ContentAccessManager : public QNetworkAccessManager
{
    Q_OBJECT

public:
    ContentAccessManager(QObject *parent);
    ~ContentAccessManager();

    void setResource(const QSet<QUrl> &identifiers, const QByteArray &data, const QString &contentType);

    void clear();

protected:
    QNetworkReply *createRequest(Operation op, const QNetworkRequest &req, QIODevice *outgoingData = Q_NULLPTR);

private:
    QMap<QUrl, QPair<QByteArray, QString> > m_data;
};

ContentAccessManager::ContentAccessManager(QObject *parent)
    : QNetworkAccessManager(parent)
{
}

ContentAccessManager::~ContentAccessManager()
{
}

void ContentAccessManager::setResource(const QSet<QUrl> &identifiers, const QByteArray &data, const QString &contentType)
{
    foreach (const QUrl url, identifiers) {
        m_data.insert(url, qMakePair(data, contentType));
    }
}

void ContentAccessManager::clear()
{
    m_data.clear();
}

QNetworkReply *ContentAccessManager::createRequest(Operation op, const QNetworkRequest &req, QIODevice *outgoingData)
{
    if (op == QNetworkAccessManager::GetOperation) {
        QUrl url(req.url());
        if ((url.scheme() == "cid") || (url.scheme() == "qmf-part")) {
            QString identifier(url.path().trimmed());
            // Remove any delimiters that are present
            if (identifier.startsWith('<') && identifier.endsWith('>')) {
                identifier = identifier.mid(1, identifier.length() - 2);
            }

            // See if we have any data for this content identifier
            QMap<QUrl, QPair<QByteArray, QString> >::iterator it = m_data.find(QUrl("cid:" + identifier));
            if (it == m_data.end()) {
                it = m_data.find(QUrl("qmf-part:" + identifier));
            }

            if (it != m_data.end()) {
                return new ContentReply(this, &it.value().first, it.value().second);
            }
        }
    }

    return QNetworkAccessManager::createRequest(op, req, outgoingData);
}
#else
class ContentRenderer : public QTextBrowser
{
    Q_OBJECT

public:
    ContentRenderer(QWidget *parent);
    ~ContentRenderer();

    void setResource(const QUrl &name, const QVariant &var);
    virtual QVariant loadResource(int type, const QUrl &name);

    void clear();

private:
    QMap<QUrl, QVariant> m_data;
};

ContentRenderer::ContentRenderer(QWidget *parent)
    : QTextBrowser(parent)
{
}

ContentRenderer::~ContentRenderer()
{
}

void ContentRenderer::setResource(const QUrl& name, const QVariant &var)
{
    if (!m_data.contains(name)) {
        m_data.insert(name, var);
    }
}

void ContentRenderer::clear()
{
    m_data.clear();
}

QVariant ContentRenderer::loadResource(int type, const QUrl& name)
{
    if (m_data.contains(name)) {
        return m_data[name];
    }

    return QTextBrowser::loadResource(type, name);
}
#endif

BrowserWidget::BrowserWidget( QWidget *parent  )
    : QWidget( parent ),
      replySplitter( &BrowserWidget::handleReplies )
{
    QLayout* l = new QVBoxLayout(this);
    l->setSpacing(0);
    l->setContentsMargins(0,0,0,0);

#ifdef USE_WEBKIT
    m_accessManager = new ContentAccessManager(this);

    m_webView = new QWebView(this);
    m_webView->setObjectName("renderer");
    m_webView->page()->setNetworkAccessManager(m_accessManager);
    m_webView->setContextMenuPolicy(Qt::CustomContextMenu);
    connect(m_webView,SIGNAL(linkClicked(QUrl)),this,SIGNAL(anchorClicked(QUrl)));
    connect(m_webView,SIGNAL(customContextMenuRequested(QPoint)),this,SLOT(contextMenuRequested(QPoint)));

    l->addWidget(m_webView);
#else
    m_renderer = new ContentRenderer(this);
    m_renderer->setObjectName("renderer");
    m_renderer->setFrameStyle(QFrame::NoFrame);
    m_renderer->setContextMenuPolicy(Qt::CustomContextMenu);
    m_renderer->setOpenLinks(false);
    connect(m_renderer,SIGNAL(anchorClicked(QUrl)),this,SIGNAL(anchorClicked(QUrl)));
    connect(m_renderer,SIGNAL(customContextMenuRequested(QPoint)),this,SLOT(contextMenuRequested(QPoint)));

    l->addWidget(m_renderer);
#endif

    setFocusPolicy( Qt::StrongFocus );
}

#ifndef USE_WEBKIT
void BrowserWidget::setResource( const QUrl& name, QVariant var )
{
    m_renderer->setResource(name, var);
}
#endif

void BrowserWidget::clearResources()
{
#ifdef USE_WEBKIT
    m_accessManager->clear();
#else
    m_renderer->clear();
#endif
    numbers.clear();
}

QList<QString> BrowserWidget::embeddedNumbers() const
{
    QList<QString> result;
    return result;
}

void BrowserWidget::setTextResource(const QSet<QUrl>& names, const QString& textData, const QString &contentType)
{
#ifdef USE_WEBKIT
    m_accessManager->setResource(names, textData.toUtf8(), contentType);
#else
    QVariant data(textData);
    foreach (const QUrl &url, names) {
        setResource(url, data);
    }

    Q_UNUSED(contentType);
#endif
}

void BrowserWidget::setImageResource(const QSet<QUrl>& names, const QByteArray& imageData, const QString &contentType)
{
    // Create a image from the data
    QDataStream imageStream(&const_cast<QByteArray&>(imageData), QIODevice::ReadOnly);
    QImageReader imageReader(imageStream.device());

    // Max size should be bounded by our display window, which will possibly
    // have a vertical scrollbar (and a small fudge factor...)
    int maxWidth = (width() - style()->pixelMetric(QStyle::PM_ScrollBarExtent) - 4);

    QSize imageSize;
    if (imageReader.supportsOption(QImageIOHandler::Size)) {
        imageSize = imageReader.size();

        // See if the image needs to be down-scaled during load
        if (imageSize.width() > maxWidth) {
            // And the loaded size should maintain the image aspect ratio
            imageSize.scale(maxWidth, (INT_MAX >> 4), Qt::KeepAspectRatio);
            imageReader.setQuality( 49 ); // Otherwise Qt smooth scales
            imageReader.setScaledSize(imageSize);
#ifdef USE_WEBKIT
        } else {
            // We can use the image directly
            m_accessManager->setResource(names, imageData, contentType);
            return;
#endif
        }
    }

    QImage image = imageReader.read();

    if (!imageReader.supportsOption(QImageIOHandler::Size)) {
        // We need to scale it down now
        if (image.width() > maxWidth) {
            image = image.scaled(maxWidth, INT_MAX, Qt::KeepAspectRatio);
#ifdef USE_WEBKIT
        } else {
            // We can use the image directly
            m_accessManager->setResource(names, imageData, contentType);
            return;
#endif
        }
    }

#ifdef USE_WEBKIT
    QByteArray scaledData;
    {
        // Write the scaled image data to a buffer
        QBuffer buffer(&scaledData);
        buffer.open(QIODevice::WriteOnly);
        image.save(&buffer, "PNG"); // default format doesn't render in webkit
    }
    m_accessManager->setResource(names, scaledData, contentType);
#else
    QVariant data(image);
    foreach (const QUrl &url, names) {
        setResource(url, data);
    }

    Q_UNUSED(contentType);
#endif
}

void BrowserWidget::setPartResource(const QMailMessagePart& part)
{
    QSet<QUrl> names;

#ifdef USE_WEBKIT
    QString name(part.displayName());
    if (!name.isEmpty()) {
        // use 'qmf-part' url scheme to ensure inline images without a contentId are rendered
        names.insert(QUrl("qmf-part:" + name.toHtmlEscaped()));
    }

    name = part.contentID().toHtmlEscaped();
    if (!name.isEmpty()) {
        // We can only resolve URLs using the cid: scheme
        if (name.startsWith("cid:", Qt::CaseInsensitive)) {
            names.insert(QUrl(name));
        } else {
            names.insert(QUrl("cid:" + name));
        }
    }
#else
    QString name(part.displayName());
    if (!name.isEmpty()) {
        names.insert(QUrl(name.toHtmlEscaped()));
    }

    name = part.contentID().toHtmlEscaped();
    if (!name.isEmpty()) {
        // Add the content both with and without the cid: prefix
        names.insert(name);
        if (name.startsWith("cid:", Qt::CaseInsensitive)) {
            names.insert(QUrl(name.mid(4)));
        } else {
            names.insert(QUrl("cid:" + name));
        }
    }

    name = part.contentType().name();
    if (!name.isEmpty()) {
        names.insert(QUrl(name.toHtmlEscaped()));
    }
#endif

    const QMailMessageContentType &ct(part.contentType());

    // Ignore any charset info in the content type, since we extract string data as unicode
    QString contentType(ct.type() + '/' + ct.subType());

    if (ct.type().toLower() == "text") {
        setTextResource(names, part.body().data(), contentType);
    } else if (ct.type().toLower() == "image") {
        setImageResource(names, part.body().data(QMailMessageBody::Decoded), contentType);
    }
}

void BrowserWidget::setSource(const QUrl &name)
{
    Q_UNUSED(name)
// We deal with this ourselves.
//    QTextBrowser::setSource( name );
}

void BrowserWidget::contextMenuRequested(const QPoint& pos)
{
#ifdef USE_WEBKIT
    QMenu menu(this);
    menu.addAction(m_webView->pageAction(QWebPage::Copy));
    menu.addAction(m_webView->pageAction(QWebPage::CopyLinkToClipboard));
    menu.addSeparator();
    menu.addActions(m_webView->actions());
    menu.exec(mapToGlobal(pos));
#else
    QMenu* menu = m_renderer->createStandardContextMenu();
    menu->addSeparator();
    menu->addActions(m_renderer->actions());
    menu->exec(mapToGlobal(pos));
    delete menu;
#endif
}

void BrowserWidget::setMessage(const QMailMessage& email, bool plainTextMode)
{
    if (plainTextMode) {
        // Complete MMS messages must be displayed in HTML
        if (email.messageType() == QMailMessage::Mms) {
            QString mmsType = email.headerFieldText("X-Mms-Message-Type");
            if (mmsType.contains("m-retrieve-conf") || mmsType.contains("m-send-req"))
                plainTextMode = false;
        }
    }

    // Maintain original linelengths if display width allows it
    if (email.messageType() == QMailMessage::Sms) {
        replySplitter = &BrowserWidget::smsBreakReplies;
    } else {
        uint lineCharLength;
        if ( fontInfo().pointSize() >= 10 ) {
            lineCharLength = width() / (fontInfo().pointSize() - 4 );
        } else {
            lineCharLength = width() / (fontInfo().pointSize() - 3 );
        }

        // Determine how to split lines in text
        if ( lineCharLength >= 78 )
            replySplitter = &BrowserWidget::noBreakReplies;
        else
            replySplitter = &BrowserWidget::handleReplies;
    }

    if (plainTextMode)
        displayPlainText(&email);
    else
        displayHtml(&email);
}

void BrowserWidget::displayPlainText(const QMailMessage* mail)
{
    QString bodyText;

    if ( (mail->status() & QMailMessage::Incoming) &&
        !(mail->status() & QMailMessage::PartialContentAvailable) ) {
        if ( !(mail->status() & QMailMessage::Removed) ) {
            bodyText += '\n' + tr("Awaiting download") + '\n';
            bodyText += tr("Size of message") + ": " + describeMailSize(mail->size());
        } else {
            // TODO: what?
        }
    } else {
        if (mail->hasBody()) {
            bodyText += mail->body().data();
        } else {
            if ( mail->multipartType() == QMailMessagePartContainer::MultipartAlternative ) {
                const QMailMessagePart* bestPart = 0;

                // Find the best alternative for text rendering
                for ( uint i = 0; i < mail->partCount(); i++ ) {
                    const QMailMessagePart &part = mail->partAt( i );

                    // TODO: A good implementation would be able to extract the plain text parts
                    // from text/html and text/enriched...

                    if (part.contentType().type().toLower() == "text") {
                        if (part.contentType().subType().toLower() == "plain") {
                            // This is the best part for us
                            bestPart = &part;
                            break;
                        }
                        else if (part.contentType().subType().toLower() == "html") {
                            // This is the worst, but still acceptable, text part for us
                            if (bestPart == 0)
                                bestPart = &part;
                        }
                        else  {
                            // Some other text - better than html, probably
                            if ((bestPart != 0) && (bestPart->contentType().subType().toLower() == "html"))
                                bestPart = &part;
                        }
                    }
                }

                if (bestPart != 0)
                    bodyText += bestPart->body().data() + '\n';
                else
                    bodyText += QLatin1String("\n<") + tr("Message part is not displayable") +
                                QLatin1String(">\n");
            }
            else if ( mail->multipartType() == QMailMessagePartContainer::MultipartRelated ) {
                const QMailMessagePart* startPart = &mail->partAt(0);

                // If not specified, the first part is the start
                QByteArray startCID = mail->contentType().parameter("start");
                if (!startCID.isEmpty()) {
                    for ( uint i = 1; i < mail->partCount(); i++ )
                        if (mail->partAt(i).contentID() == startCID) {
                            startPart = &mail->partAt(i);
                            break;
                        }
                }

                // Render the start part, if possible
                if (startPart->contentType().type().toLower() == "text")
                    bodyText += startPart->body().data() + '\n';
                else
                    bodyText +=  QLatin1String("\n<") + tr("Message part is not displayable") +
                                  QLatin1String(">\n");
            }
            else {
                // According to RFC 2046, any unrecognised type should be treated as 'mixed'
                if (mail->multipartType() != QMailMessagePartContainer::MultipartMixed)
                    qWarning() << "Generic viewer: Unimplemented multipart type:" << mail->contentType().toString();

                // Render each succesive part to text, where possible
                for ( uint i = 0; i < mail->partCount(); i++ ) {
                    const QMailMessagePart &part = mail->partAt( i );

                    if (part.hasBody() && (part.contentType().type().toLower() == "text")) {
                        bodyText += part.body().data() + '\n';
                    } else {
                        bodyText += QLatin1String("\n<") + tr("Part") + ": " + part.displayName() +
                                    QLatin1String(">\n");
                    }
                }
            }
        }
    }

    QString text;

    if ((mail->messageType() != QMailMessage::Sms) && (mail->messageType() != QMailMessage::Instant))
        text += tr("Subject") +  QLatin1String(": ") + mail->subject().simplified() + '\n';

    QMailAddress fromAddress(mail->from());
    if (!fromAddress.isNull())
        text += tr("From") + ": " + fromAddress.toString() + '\n';

    if (mail->to().count() > 0) {
        text += tr("To") + ": ";
        text += QMailAddress::toStringList(mail->to()).join(QLatin1String(", "));
    }
    if (mail->cc().count() > 0) {
        text += '\n' + tr("CC") +  QLatin1String(": ");
        text += QMailAddress::toStringList(mail->cc()).join(QLatin1String(", "));
    }
    if (mail->bcc().count() > 0) {
        text += '\n' + tr("BCC") +  QLatin1String(": ");
        text += QMailAddress::toStringList(mail->bcc()).join(QLatin1String(", "));
    }
    if ( !mail->replyTo().isNull() ) {
        text += '\n' + tr("Reply-To") + QLatin1String(": ");
        text += mail->replyTo().toString();
    }

    text += '\n' + tr("Date") + QLatin1String(": ");
    text += dateString(mail->date().toLocalTime()) + '\n';

    if (mail->status() & QMailMessage::Removed) {
        if (!bodyText.isEmpty()) {
            // Don't include the notice - the most likely reason to view plain text
            // is for printing, and we don't want to print the notice
        } else {
            text += '\n';
            text += tr("Message deleted from server");
        }
    }

    if (!bodyText.isEmpty()) {
        text += '\n';
        text += bodyText;
    }

    setPlainText(text);
}

static QString replaceLast(const QString container, const QString& before, const QString& after)
{
    QString result(container);

    int index;
    if ((index = container.lastIndexOf(before)) != -1)
        result.replace(index, before.length(), after);

    return result;
}

QString BrowserWidget::renderSimplePart(const QMailMessagePart& part)
{
    QString result;

    QString partId = part.displayName().toHtmlEscaped();

    QMailMessageContentType contentType = part.contentType();
    if ( contentType.type().toLower() == "text") { // No tr
        if (part.hasBody()) {
            QString partText = part.body().data();
            if ( !partText.isEmpty() ) {
                if ( contentType.subType().toLower() == "html" ) {
                    result = partText + "<br>";
                } else {
                    result = formatText( partText );
                }
            }
        } else {
            result = renderAttachment(part);
        }
    } else if ( contentType.type().toLower() == "image") { // No tr
        setPartResource(part);
#ifdef USE_WEBKIT
        result = "<img src=\"qmf-part:" + partId + "\"></img>";
#else
        result = "<img src=\"" + partId + "\"></img>";
#endif

    } else {
        result = renderAttachment(part);
    }

    return result;
}

QString BrowserWidget::renderAttachment(const QMailMessagePart& part)
{
    QString partId = part.displayName().toHtmlEscaped();

    QString attachmentTemplate =
"<hr><b>ATTACHMENT_TEXT</b>: <a href=\"attachment;ATTACHMENT_ACTION;ATTACHMENT_LOCATION\">NAME_TEXT</a>DISPOSITION<br>";

    attachmentTemplate = replaceLast(attachmentTemplate, "ATTACHMENT_TEXT", tr("Attachment"));
    attachmentTemplate = replaceLast(attachmentTemplate, "ATTACHMENT_ACTION", part.partialContentAvailable() ? "view" : "retrieve");
    attachmentTemplate = replaceLast(attachmentTemplate, "ATTACHMENT_LOCATION", part.location().toString(false));
    attachmentTemplate = replaceLast(attachmentTemplate, "NAME_TEXT", partId);
    return replaceLast(attachmentTemplate, "DISPOSITION", part.partialContentAvailable() ? "" : tr(" (on server)"));
}

QString BrowserWidget::renderPart(const QMailMessagePart& part)
{
    QString result;

    if (part.multipartType() != QMailMessage::MultipartNone) {
        result = renderMultipart(part);
    } else {
        bool displayAsAttachment(!part.contentAvailable());
        if (!displayAsAttachment) {
            QMailMessageContentDisposition disposition = part.contentDisposition();
            if (!disposition.isNull() && disposition.type() == QMailMessageContentDisposition::Attachment) {
                displayAsAttachment = true;
            }
        }

        result = (displayAsAttachment ? renderAttachment(part) : renderSimplePart(part));
    }

    return result;
}

QString BrowserWidget::renderMultipart(const QMailMessagePartContainer& partContainer)
{
    QString result;

    if (partContainer.multipartType() == QMailMessagePartContainer::MultipartAlternative) {
        int partIndex = -1;

        // Find the best alternative for rendering
        for (uint i = 0; i < partContainer.partCount(); ++i) {
            const QMailMessagePart &part = partContainer.partAt(i);

            // Parts are ordered simplest to most complex
            QString type(part.contentType().type().toLower());
            if ((type == "text") || (type == "image")) {
                // These parts are probably displayable
                partIndex = i;
            }
        }

        if (partIndex != -1) {
            result += renderPart(partContainer.partAt(partIndex));
        } else {
            result += "\n<" + tr("No displayable part") + ">\n";
        }
    } else if (partContainer.multipartType() == QMailMessagePartContainer::MultipartRelated) {
        uint startIndex = 0;

        // If not specified, the first part is the start
        QByteArray startCID = partContainer.contentType().parameter("start");
        if (!startCID.isEmpty()) {
            for (uint i = 1; i < partContainer.partCount(); ++i) {
                if (partContainer.partAt(i).contentID() == startCID) {
                    startIndex = i;
                    break;
                }
            }
        }

        // Add any other parts as resources
        QList<const QMailMessagePart*> absentParts;
        for (uint i = 0; i < partContainer.partCount(); ++i) {
            if (i != startIndex) {
                const QMailMessagePart &part = partContainer.partAt(i);
                if (part.partialContentAvailable()) {
                    setPartResource(partContainer.partAt(i));
                } else {
                    absentParts.append(&part);
                }
            }
        }

        // Render the start part
        result += renderPart(partContainer.partAt(startIndex));

        // Show any unavailable parts as attachments
        foreach (const QMailMessagePart *part, absentParts) {
            result += renderAttachment(*part);
        }
    } else {
        // According to RFC 2046, any unrecognised type should be treated as 'mixed'
        if (partContainer.multipartType() != QMailMessagePartContainer::MultipartMixed)
            qWarning() << "Unimplemented multipart type:" << partContainer.contentType().toString();

        // Render each part successively according to its disposition
        for (uint i = 0; i < partContainer.partCount(); ++i) {
            result += renderPart(partContainer.partAt(i));
        }
    }

    return result;
}

typedef QPair<QString, QString> TextPair;

void BrowserWidget::displayHtml(const QMailMessage* mail)
{
    QString subjectText, bodyText;
    QList<TextPair> metadata;

    // For SMS messages subject is the same as body, so for SMS don't
    // show the message text twice (same for IMs)
    if ((mail->messageType() != QMailMessage::Sms) && (mail->messageType() != QMailMessage::Instant))
        subjectText = mail->subject();

    QString from = mail->headerFieldText("From");
    if (!from.isEmpty() && from != "\"\" <>") // ugh
        metadata.append(qMakePair(tr("From"), refMailTo( mail->from() )));

    if (mail->to().count() > 0)
        metadata.append(qMakePair(tr("To"), listRefMailTo( mail->to() )));

    if (mail->cc().count() > 0)
        metadata.append(qMakePair(tr("CC"), listRefMailTo( mail->cc() )));

    if (mail->bcc().count() > 0)
        metadata.append(qMakePair(tr("BCC"), listRefMailTo( mail->bcc() )));

    if (!mail->replyTo().isNull())
        metadata.append(qMakePair(tr("Reply-To"), refMailTo( mail->replyTo() )));

    metadata.append(qMakePair(tr("Date"), dateString(mail->date().toLocalTime())));

    metadata.append(qMakePair(tr("Message Id"), QString::number(mail->id().toULongLong())));
    metadata.append(qMakePair(tr("CalendarInvitation"), (mail->status() & QMailMessage::CalendarInvitation) ? QString("true") : QString("false")));
    metadata.append(qMakePair(tr("CalendarCancellation"), (mail->status() & QMailMessage::CalendarCancellation) ? QString("true") : QString("false")));

    if ( (mail->status() & QMailMessage::Incoming) &&
        !(mail->status() & QMailMessage::PartialContentAvailable) ) {
        if ( !(mail->status() & QMailMessage::Removed) ) {
            bodyText =
"<b>WAITING_TEXT</b><br>"
"SIZE_TEXT<br>"
"<br>"
"<a href=\"download\">DOWNLOAD_TEXT</a>";

            bodyText = replaceLast(bodyText, "WAITING_TEXT", tr("Awaiting download"));
            bodyText = replaceLast(bodyText, "SIZE_TEXT", tr("Size of message content") + ": " + describeMailSize(mail->contentSize()));
            bodyText = replaceLast(bodyText, "DOWNLOAD_TEXT", tr("Download this message"));
        } else {
            // TODO: what?
        }
    } else {
        if (mail->partCount() > 0) {
            bodyText = renderMultipart(*mail);
        } else {
            bodyText = (mail->content() == QMailMessage::PlainTextContent) ? formatText(mail->body().data()) : mail->body().data();

            if (!mail->contentAvailable()) {
                QString trailer =
"<br>"
"WAITING_TEXT<br>"
"SIZE_TEXT<br>"
"<a href=\"DOWNLOAD_ACTION\">DOWNLOAD_TEXT</a>";

                trailer = replaceLast(trailer, "WAITING_TEXT", tr("More data available"));
                trailer = replaceLast(trailer, "SIZE_TEXT", tr("Size") + ": " + describeMailSize(mail->body().length()) + tr(" of ") + describeMailSize(mail->contentSize()));
                if ((mail->contentType().type().toLower() == "text") && (mail->contentType().subType().toLower() == "plain")) {
                    trailer = replaceLast(trailer, "DOWNLOAD_ACTION", "download;5120");
                } else {
                    trailer = replaceLast(trailer, "DOWNLOAD_ACTION", "download");
                }
                trailer = replaceLast(trailer, "DOWNLOAD_TEXT", tr("Retrieve more data"));

                bodyText += trailer;
            }
        }
    }

    // Form our parts into a displayable page
    QString pageData;

    if (mail->status() & QMailMessage::Removed) {
        QString noticeTemplate =
"<div align=center>"
    "NOTICE_TEXT<br>"
"</div>";

        QString notice = tr("Message deleted from server");
        if (!bodyText.isEmpty()) {
            notice.prepend("<font size=\"-5\">[");
            notice.append("]</font>");
        }

        pageData += replaceLast(noticeTemplate, "NOTICE_TEXT", notice);
    }

    QString headerTemplate = \
"<div align=left>"
    "<table border=0 cellspacing=0 cellpadding=0 width=100%>"
        "<tr>"
            "<td bgcolor=\"#000000\">"
                "<table border=0 width=100% cellspacing=1 cellpadding=4>"
                    "<tr>"
                        "<td align=left bgcolor=\"HIGHLIGHT_COLOR\">"
                            "<b><font color=\"LINK_COLOR\">SUBJECT_TEXT</font></b>"
                        "</td>"
                    "</tr>"
                    "<tr>"
                        "<td bgcolor=\"WINDOW_COLOR\">"
                            "<table border=0>"
                                "METADATA_TEXT"
                            "</table>"
                        "</td>"
                    "</tr>"
                "</table>"
            "</td>"
        "</tr>"
    "</table>"
"</div>"
"<br>";

    headerTemplate = replaceLast(headerTemplate, "HIGHLIGHT_COLOR", palette().color(QPalette::Highlight).name());
    headerTemplate = replaceLast(headerTemplate, "LINK_COLOR", palette().color(QPalette::HighlightedText).name());
    headerTemplate = replaceLast(headerTemplate, "SUBJECT_TEXT", subjectText.toHtmlEscaped());
    headerTemplate = replaceLast(headerTemplate, "WINDOW_COLOR", palette().color(QPalette::Window).name());

    QString itemTemplate =
"<tr>"
    "<td align='right' valign='top'>"
        "<b>ID_TEXT: </b>"
    "</td>"
    "<td align='left' valign='top'>"
        "CONTENT_TEXT"
    "</td>"
"</tr>";

    QString metadataText;
    foreach (const TextPair item, metadata) {
        QString element = replaceLast(itemTemplate, "ID_TEXT", item.first.toHtmlEscaped());
        element = replaceLast(element, "CONTENT_TEXT", item.second);
        metadataText.append(element);
    }

    pageData += replaceLast(headerTemplate, "METADATA_TEXT", metadataText);

    QString bodyTemplate =
 "<div align=left>BODY_TEXT</div>";

    pageData += replaceLast(bodyTemplate, "BODY_TEXT", bodyText);

    QString pageTemplate =
"<table width=100% height=100% border=0 cellspacing=8 cellpadding=0>"
    "<tr>"
        "<td valign=\"top\">"
            "PAGE_DATA"
        "</td>"
    "</tr>"
"</table>";

    QString html = replaceLast(pageTemplate,"PAGE_DATA",pageData);
#ifdef USE_WEBKIT
    m_webView->setHtml(html);
    m_webView->page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks);
#else
    m_renderer->setHtml(html);
#endif
}

QString BrowserWidget::describeMailSize(uint bytes) const
{
    QString size;

    // No translation?
    if (bytes < 1024) {
        size.setNum(bytes);
        size += " Bytes";
    } else if (bytes < 1024*1024) {
        size.setNum( (bytes / 1024) );
        size += " KB";
    } else {
        float f = static_cast<float>( bytes )/ (1024*1024);
        size.setNum(f, 'g', 3);
        size += " MB";
    }

    return size;
}

QString BrowserWidget::formatText(const QString& txt) const
{
    return (*this.*replySplitter)(txt);
}

QString BrowserWidget::smsBreakReplies(const QString& txt) const
{
    /*  Preserve white space, add linebreaks so text is wrapped to
        fit the display width */
    QString str = "";
    QStringList p = txt.split('\n');

    QStringList::Iterator it = p.begin();
    while ( it != p.end() ) {
        str += buildParagraph( *it, "", true ) + "<br>";
        it++;
    }

    return str;
}

QString BrowserWidget::noBreakReplies(const QString& txt) const
{
    /*  Maintains the original linebreaks, but colours the lines
        according to reply level    */
    QString str = "";
    QStringList p = txt.split('\n');

    int x, levelList;
    QStringList::Iterator it = p.begin();
    while ( it != p.end() ) {

        x = 0;
        levelList = 0;
        while (x < (*it).length() ) {
            if ( (*it)[x] == '>' ) {
                levelList++;
            } else if ( (*it)[x] == ' ' ) {
            } else break;

            x++;
        }

        if (levelList == 0 ) {
            str += encodeUrlAndMail(*it) + "<br>";
        } else {
            const QString para("<font color=\"%1\">%2</font><br>");
            str += para.arg(replyColor.name()).arg(encodeUrlAndMail(*it));
        }

        it++;
    }

    while ( str.endsWith("<br>") ) {
        str.chop(4);   //remove trailing br
    }
    return str;
}

QString appendLine(const QString& preceding, const QString& suffix)
{
    if (suffix.isEmpty())
        return preceding;

    QString result(preceding);

    int nwsIndex = suffix.indexOf(QRegularExpression("[^\\s]"));
    if (nwsIndex > 0) {
        // This line starts with whitespace, which we'll have to protect to keep

        // We can't afford to make huge tracts of whitespace; ASCII art will be broken!
        // Convert any run of up to 4 spaces to a tab; convert all tabs to two spaces each
        QString leader(suffix.left(nwsIndex));
        leader.replace(QRegularExpression(" {1,4}"), "\t");

        // Convert the spaces to non-breaking
        leader.replace("\t", "&nbsp;&nbsp;");
        result.append(leader).append(suffix.mid(nwsIndex));
    }
    else
        result.append(suffix);

    return result;
}

QString unwrap(const QString& txt, const QString& prepend)
{
    QStringList lines = txt.split('\n', Qt::KeepEmptyParts);

    QString result;
    result.reserve(txt.length());

    QStringList::iterator it = lines.begin(), prev = it, end = lines.end();
    if (it != end) {
        for (++it; it != end; ++prev, ++it) {
            QString terminator = "<br>";

            int prevLength = (*prev).length();
            if (prevLength == 0) {
                // If the very first line is empty, skip it; otherwise include
                if (prev == lines.begin())
                    continue;
            } else {
                int wsIndex = (*it).indexOf(QRegularExpression("\\s"));
                if (wsIndex == 0) {
                    // This was probably an intentional newline
                } else {
                    if (wsIndex == -1)
                        wsIndex = (*it).length();

                    bool logicalEnd = false;

                    const QChar last = (*prev)[prevLength - 1];
                    logicalEnd = ((last == '.') || (last == '!') || (last == '?'));

                    if ((*it)[0].isUpper() && logicalEnd) {
                        // This was probably an intentional newline
                    } else {
                        int totalLength = prevLength + prepend.length();
                        if ((wsIndex != -1) && ((totalLength + wsIndex) > 78)) {
                            // This was probably a forced newline - convert it to a space
                            terminator = ' ';
                        }
                    }
                }
            }

            result = appendLine(result, BrowserWidget::encodeUrlAndMail(*prev) + terminator);
        }
        if (!(*prev).isEmpty())
            result = appendLine(result, BrowserWidget::encodeUrlAndMail(*prev));
    }

    return result;
}

/*  This one is a bit complicated.  It divides up all lines according
    to their reply level, defined as count of ">" before normal text
    It then strips them from the text, builds the formatted paragraph
    and inserts them back into the beginning of each line.  Probably not
    too speed efficient on large texts, but this manipulation greatly increases
    the readability (trust me, I'm using this program for my daily email reading..)
*/
QString BrowserWidget::handleReplies(const QString& txt) const
{
    QStringList out;
    QStringList p = txt.split('\n');
    QList<uint> levelList;
    QStringList::Iterator it = p.begin();
    uint lastLevel = 0, level = 0;

    // Skip the last string, if it's non-existent
    int offset = (txt.endsWith('\n') ? 1 : 0);

    QString str, line;
    while ( (it + offset) != p.end() ) {
        line = (*it);
        level = 0;

        if ( line.startsWith(">") ) {
            for (int x = 0; x < line.length(); x++) {
                if ( line[x] == ' ') {
                    // do nothing
                } else if ( line[x] == '>' ) {
                    level++;
                    if ( (level > 1 ) && (line[x-1] != ' ') ) {
                        line.insert(x, ' ');    //we need it to be "> > " etc..
                        x++;
                    }
                } else {
                    // make sure it follows style "> > This is easier to format"
                    if ( line[x - 1] != ' ' )
                        line.insert(x, ' ');
                    break;
                }
            }
        }

        if ( level != lastLevel ) {
            if ( !str.isEmpty() ) {
                out.append( str );
                levelList.append( lastLevel );
            }

            str.clear();
            lastLevel = level;
            it--;
        } else {
            str += line.mid(level * 2) + '\n';
        }

        it++;
    }
    if ( !str.isEmpty() ) {
        out.append( str );
        levelList.append( level );
    }

    str = "";
    lastLevel = 0;
    int pos = 0;
    it = out.begin();
    while ( it != out.end() ) {
        if ( levelList[pos] == 0 ) {
            str += unwrap( *it, "" ) + "<br>";
        } else {
            QString pre = "";
            QString preString = "";
            for ( uint x = 0; x < levelList[pos]; x++) {
                pre += "&gt; ";
                preString += "> ";
            }

            QString segment = unwrap( *it, preString );

            const QString para("<font color=\"%1\">%2</font><br>");
            str += para.arg(replyColor.name()).arg(pre + segment);
        }

        lastLevel = levelList[pos];
        pos++;
        it++;
    }

    while ( str.endsWith("<br>") ) {
        str.chop(4);   //remove trailing br
    }

    return str;
}

QString BrowserWidget::buildParagraph(const QString& txt, const QString& prepend, bool preserveWs) const
{
    Q_UNUSED(prepend);
    QStringList out;

    QString input = encodeUrlAndMail( preserveWs ? txt : txt.simplified() );
    if (preserveWs)
        return input.replace('\n', "<br>");

    QStringList p = input.split( ' ', Qt::SkipEmptyParts );
    return p.join(QString(' '));
}

QString BrowserWidget::encodeUrlAndMail(const QString& txt)
{
    QStringList result;

    // TODO: is this necessary?
    // Find and encode URLs that aren't already inside anchors
    //QRegExp anchorPattern("<\\s*a\\s*href.*/\\s*a\\s*>");
    //anchorPattern.setMinimal(true);

    // We should be optimistic in our URL matching - the link resolution can
    // always fail, but if we don't match it, then we can't make it into a link
    QRegularExpression urlPattern("("
                            "(?:http|https|ftp)://"
                       "|"
                            "mailto:"
                       ")?"                                     // optional scheme
                       "((?:[^: \\t]+:)?[^ \\t@]+@)?"           // optional credentials
                       "("                                      // either:
                            "localhost"                             // 'localhost'
                       "|"                                      // or:
                            "(?:"                                   // one-or-more:
                            "[A-Za-z\\d]"                           // one: legal char,
                            "(?:"                                   // zero-or-one:
                                "[A-Za-z\\d-]*[A-Za-z\\d]"              // (zero-or-more: (legal char or '-'), one: legal char)
                            ")?"                                    // end of optional group
                            "\\."                                   // '.'
                            ")+"                                    // end of mandatory group
                            "[A-Za-z\\d]"                           // one: legal char
                            "(?:"                                   // zero-or-one:
                                "[A-Za-z\\d-]*[A-Za-z\\d]"              // (zero-or-more: (legal char or '-'), one: legal char)
                            ")?"                                    // end of optional group
                       ")"                                      // end of alternation
                       "(:\\d+)?"                               // optional port number
                       "("                                      // optional location
                            "/"                                 // beginning with a slash
                            "[A-Za-z\\d\\.\\!\\#\\$\\%\\'"      // containing any sequence of legal chars
                             "\\*\\+\\-\\/\\=\\?\\^\\_\\`"
                             "\\{\\|\\}\\~\\&\\(\\)]*"
                       ")?");
    QRegularExpressionMatch urlMatch;

    // Find and encode file:// links
    QRegularExpression filePattern("(file://\\S+)");
    QRegularExpressionMatch fileMatch;

    // Find and encode email addresses
    QRegularExpression addressPattern(QMailAddress::emailAddressPattern());
    QRegularExpressionMatch addressMatch;

    int urlPos = txt.indexOf(urlPattern, 0, &urlMatch);
    int addressPos = txt.indexOf(addressPattern, 0, &fileMatch);
    int filePos = txt.indexOf(filePattern, 0, &addressMatch);

    int lastPos = 0, matchLength = 0;
    while ((urlPos != -1) || (addressPos != -1) || (filePos != -1)) {
        int *matchPos = 0;
        QRegularExpression *matchPattern = 0;

        // Which pattern has the first match?
        if ((urlPos != -1) &&
            ((addressPos == -1) || (addressPos >= urlPos)) &&
            ((filePos == -1) || (filePos >= urlPos))) {
            matchPos = &urlPos;
            matchPattern = &urlPattern;
            matchLength = urlMatch.capturedLength(0);
        } else if ((addressPos != -1) &&
                   ((urlPos == -1) || (urlPos >= addressPos)) &&
                   ((filePos == -1) || (filePos >= addressPos))) {
            matchPos = &addressPos;
            matchPattern = &addressPattern;
            matchLength = addressMatch.capturedLength(0);
        } else if ((filePos != -1) &&
                   ((urlPos == -1) || (urlPos >= filePos)) &&
                   ((addressPos == -1) || (addressPos >= filePos))) {
            matchPos = &filePos;
            matchPattern = &filePattern;
            matchLength = fileMatch.capturedLength(0);
        } else {
            Q_ASSERT(false);
            return QString();
        }

        QString replacement;

        if (matchPattern == &urlPattern) {
            // Is this a valid URL?
            QString scheme = urlMatch.captured(1);
            QString credentials = urlMatch.captured(2);
            QString host(urlMatch.captured(3));

            // Ensure that the host is not purely a number
            // Also ignore credentials with no scheme
            if (scheme.isEmpty() &&
                ((host.indexOf(QRegularExpression("[^\\d\\.]")) == -1) || (!credentials.isEmpty()))) {
                // Ignore this match
                urlPos = txt.indexOf(urlPattern, urlPos + 1);
                continue;
            } else {
                char parenTypes[] = { '(', ')', '[', ']', '{', '}', '<', '>', '\0' };

                QString leading;
                QString trailing;
                QString url = urlMatch.captured(0);

                QChar firstChar(url.at(0));
                QChar lastChar(url.at(url.length() - 1));

                for (int i = 0; parenTypes[i] != '\0'; i += 2) {
                    if ((firstChar == parenTypes[i]) || (lastChar == parenTypes[i+1])) {
                        // Check for unbalanced parentheses
                        int left = url.count(parenTypes[i]);
                        int right = url.count(parenTypes[i+1]);

                        if ((right > left) && (lastChar == parenTypes[i+1])) {
                            // The last parenthesis is probably not part of the URL
                            url = url.left(url.length() - 1);
                            trailing.append(lastChar);
                            lastChar = url.at(url.length() - 1);
                        } else if ((right < left) && (firstChar == parenTypes[i])) {
                            // The first parenthesis is probably not part of the URL
                            url = url.mid(1);
                            leading.append(firstChar);
                            firstChar = url.at(0);
                        }
                    }
                }

                if ((lastChar == '.') || (lastChar == ',') || (lastChar == ';')) {
                    // The last character is probably part of the surrounding text
                    url = url.left(url.length() - 1);
                    trailing = lastChar;
                }

                replacement = refUrl(url, scheme, leading, trailing);
            }
        } else if (matchPattern == &addressPattern) {
            QString address = addressMatch.captured(0);

            replacement = refMailTo(QMailAddress(address));
        } else if (matchPattern == &filePattern) {
            QString file = fileMatch.captured(0);

            replacement = refUrl(file, "file://", QString(), QString());
        }

        // Write the unmatched text out in escaped form
        result.append(txt.mid(lastPos, (*matchPos - lastPos)).toHtmlEscaped());

        result.append(replacement);

        // Find the following pattern match for this pattern
        lastPos = *matchPos + matchLength;
        *matchPos = txt.indexOf(*matchPattern, lastPos);

        // Bypass any other matches contained within the matched text
        if ((urlPos != -1) && (urlPos < lastPos)) {
            urlPos = txt.indexOf(urlPattern, lastPos);
        }
        if ((addressPos != -1) && (addressPos < lastPos)) {
            addressPos = txt.indexOf(addressPattern, lastPos);
        }
        if ((filePos != -1) && (filePos < lastPos)) {
            filePos = txt.indexOf(filePattern, lastPos);
        }
    }

    if (lastPos < txt.length()) {
        result.append(txt.mid(lastPos).toHtmlEscaped());
    }

    return result.join("");
}

void BrowserWidget::scrollToAnchor(const QString& anchor)
{
#ifdef USE_WEBKIT
    // TODO
    Q_UNUSED(anchor);
#else
    m_renderer->scrollToAnchor(anchor);
#endif
}

void BrowserWidget::setPlainText(const QString& text)
{
#ifdef USE_WEBKIT
    QString html(text.toHtmlEscaped());
    html.replace("\n", "<br>");
    m_webView->setHtml("<html><body>" + html + "</body></html>");
    m_webView->page()->setLinkDelegationPolicy(QWebPage::DelegateAllLinks);
#else
    m_renderer->setPlainText(text);
#endif
}

void BrowserWidget::addAction(QAction* action)
{
#ifdef USE_WEBKIT
    m_webView->addAction(action);
#else
    m_renderer->addAction(action);
#endif
}

void BrowserWidget::addActions(const QList<QAction*>& actions)
{
#ifdef USE_WEBKIT
    m_webView->addActions(actions);
#else
    m_renderer->addActions(actions);
#endif
}

void BrowserWidget::removeAction(QAction* action)
{
#ifdef USE_WEBKIT
    m_webView->removeAction(action);
#else
    m_renderer->removeAction(action);
#endif
}

QString BrowserWidget::listRefMailTo(const QList<QMailAddress>& list)
{
    QStringList result;
    foreach ( const QMailAddress& address, list )
        result.append( refMailTo( address ) );

    return result.join( ", " );
}

QString BrowserWidget::refMailTo(const QMailAddress& address)
{
    QString name = address.toString().toHtmlEscaped();
    if (name == "System")
        return name;

    if (address.isPhoneNumber() || address.isEmailAddress())
        return "<a href=\"mailto:" + address.address().toHtmlEscaped() + "\">" + name + "</a>";

    return name;
}

QString BrowserWidget::refNumber(const QString& number)
{
    return "<a href=\"dial;" + number.toHtmlEscaped() + "\">" + number + "</a>";
}

QString BrowserWidget::refUrl(const QString& url, const QString& scheme, const QString& leading, const QString& trailing)
{
    // Assume HTTP if there is no scheme
    QString escaped(url.toHtmlEscaped());
    QString target(scheme.isEmpty() ? "http://" + escaped : escaped);

    return leading.toHtmlEscaped() + "<a href=\"" + target + "\">" + escaped + "</a>" + trailing.toHtmlEscaped();
}

#include "browserwidget.moc"