~adamreichold/qpdfview/trunk

« back to all changes in this revision

Viewing changes to sources/documentview.cpp

  • Committer: Adam Reichold
  • Date: 2013-01-17 14:49:07 UTC
  • mto: This revision was merged to the branch mainline in revision 924.
  • Revision ID: adamreichold@myopera.com-20130117144907-9hd3docidcx98kp3
add usage of libmagic to determine file type

Show diffs side-by-side

added added

removed removed

Lines of Context:
53
53
 
54
54
#endif // WITH_SYNCTEX
55
55
 
 
56
#ifdef WITH_MAGIC
 
57
 
 
58
#include <magic.h>
 
59
 
 
60
#endif // WITH_MAGIC
 
61
 
56
62
#include "model.h"
57
63
#include "pageitem.h"
58
64
#include "searchthread.h"
1429
1435
 
1430
1436
Model::Document* DocumentView::loadDocument(const QString& filePath)
1431
1437
{
 
1438
    enum { UnknownType = 0, PDF = 1, PS = 2, DjVu = 3 } fileType = UnknownType;
 
1439
 
 
1440
#ifdef WITH_MAGIC
 
1441
 
 
1442
    magic_t cookie = magic_open(MAGIC_MIME_TYPE);
 
1443
 
 
1444
    if(magic_load(cookie, 0) == 0)
 
1445
    {
 
1446
        const char* mime_type = magic_file(cookie, QFile::encodeName(filePath));
 
1447
 
 
1448
        if(qstrncmp(mime_type, "application/pdf", 15) == 0)
 
1449
        {
 
1450
            fileType = PDF;
 
1451
        }
 
1452
        else if(qstrncmp(mime_type, "application/postscript", 22) == 0)
 
1453
        {
 
1454
            fileType = PS;
 
1455
        }
 
1456
        else if(qstrncmp(mime_type, "image/vnd.djvu", 14) == 0)
 
1457
        {
 
1458
            fileType = DjVu;
 
1459
        }
 
1460
        else
 
1461
        {
 
1462
            qDebug() << "Unknown file type:" << mime_type;
 
1463
        }
 
1464
    }
 
1465
 
 
1466
    magic_close(cookie);
 
1467
 
 
1468
#else
 
1469
 
1432
1470
    QFileInfo fileInfo(filePath);
1433
1471
 
1434
 
#ifdef WITH_PDF
1435
 
 
1436
1472
    if(fileInfo.suffix().toLower() == "pdf")
1437
1473
    {
 
1474
        fileType = PDF;
 
1475
    }
 
1476
    else if(fileInfo.suffix().toLower() == "ps")
 
1477
    {
 
1478
        fileType = PS;
 
1479
    }
 
1480
    else if(fileInfo.suffix().toLower() == "djvu" || fileInfo.suffix().toLower() == "djv")
 
1481
    {
 
1482
        fileType = DjVu;
 
1483
    }
 
1484
    else
 
1485
    {
 
1486
        qDebug() << "Unkown file type:" << fileInfo.suffix().toLower();
 
1487
    }
 
1488
 
 
1489
#endif // WITH_MAGIC
 
1490
 
 
1491
#ifdef WITH_PDF
 
1492
 
 
1493
    if(fileType == PDF)
 
1494
    {
1438
1495
        if(s_pdfDocumentLoader == 0)
1439
1496
        {
1440
1497
#ifndef STATIC_PDF_PLUGIN
1462
1519
 
1463
1520
#ifdef WITH_PS
1464
1521
 
1465
 
    if(fileInfo.suffix().toLower() == "ps")
 
1522
    if(fileType == PS)
1466
1523
    {
1467
1524
        if(s_psDocumentLoader == 0)
1468
1525
        {
1492
1549
 
1493
1550
#ifdef WITH_DJVU
1494
1551
 
1495
 
    if(fileInfo.suffix().toLower() == "djvu" || fileInfo.suffix().toLower() == "djv")
 
1552
    if(fileType == DjVu)
1496
1553
    {
1497
1554
        if(s_djvuDocumentLoader == 0)
1498
1555
        {