summaryrefslogtreecommitdiffstats
path: root/filters/chalk/pdf/kis_pdf_import.cpp
blob: 6d668f4a8b6b14b15443c7ae21da210ccd22a5f4 (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
/*
 *  Copyright (c) 2006 Cyrille Berger <cberger@cberger.net>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "kis_pdf_import.h"

// poppler's headers
#include <poppler-qt.h>

// TQt's headers
#include <tqfile.h>
#include <tqimage.h> // TODO that too
#include <tqradiobutton.h>

// KDE's headers
#include <tdeapplication.h>
#include <kdebug.h>
#include <kdialogbase.h>
#include <kgenericfactory.h>
#include <knuminput.h>
#include <kpassdlg.h>

#include <tdeio/netaccess.h>

// koffice's headers
#include <KoFilterChain.h>

// chalk's headers
#include <kis_doc.h>
#include <kis_colorspace.h>
#include <kis_colorspace_factory_registry.h>
#include <kis_group_layer.h>
#include <kis_image.h>
#include <kis_meta_registry.h>
#include <kis_paint_layer.h>

// plugins's headers
#include "kis_pdf_import_widget.h"

typedef KGenericFactory<KisPDFImport, KoFilter> PDFImportFactory;
K_EXPORT_COMPONENT_FACTORY(libchalkpdfimport, PDFImportFactory("kofficefilters"))

KisPDFImport::KisPDFImport(KoFilter *, const char *, const TQStringList&) : KoFilter()
{
}

KisPDFImport::~KisPDFImport()
{
}

KisPDFImport::ConversionStatus KisPDFImport::convert(const TQCString& , const TQCString& )
{
    TQString filename = m_chain -> inputFile();
    kdDebug(41008) << "Importing using PDFImport!" << filename << endl;
    
    if (filename.isEmpty())
        return KoFilter::FileNotFound;
    
    
    KURL url;
    url.setPath(filename);

    if (!TDEIO::NetAccess::exists(url, false, tqApp -> mainWidget())) {
        return KoFilter::FileNotFound;
    }

    // We're not set up to handle asynchronous loading at the moment.
    TQString tmpFile;
    if (TDEIO::NetAccess::download(url, tmpFile, tqApp -> mainWidget())) {
        url.setPath( tmpFile );
    }
    
    Poppler::Document* pdoc = Poppler::Document::load( TQFile::encodeName(url.path() ) );
    

    if ( !pdoc)
    {
        kdDebug(41008) << "Error when reading the PDF" << endl;
        return KoFilter::StorageCreationError;
    }


    while( pdoc->isLocked() )
    {
        TQCString password;
        int result = KPasswordDialog::getPassword(password, i18n("A password is required to read that pdf"));
        if (result == KPasswordDialog::Accepted)
        {
            pdoc->unlock(password);
        } else {
            kdDebug(41008) << "Password canceled" << endl;
            return KoFilter::StorageCreationError;
        }
    }

    KDialogBase* kdb = new KDialogBase(0, "", false, i18n("PDF Import Options"), KDialogBase::Ok | KDialogBase::Cancel);
    
    KisPDFImportWidget* wdg = new KisPDFImportWidget(pdoc, kdb);
    kdb->setMainWidget(wdg);
    kapp->restoreOverrideCursor();
    if(kdb->exec() == TQDialog::Rejected)
    {
        delete pdoc;
        delete kdb;
        return KoFilter::StorageCreationError; // FIXME Cancel doesn't exist :(
    }
    
    // Init kis's doc
    KisDoc * doc = dynamic_cast<KisDoc*>(m_chain -> outputDocument());
    if (!doc)
    {
        delete pdoc;
        delete kdb;
        return KoFilter::CreationError;
    }

    doc -> prepareForImport();
    // Create the chalk image
    KisColorSpace* cs = KisMetaRegistry::instance()->csRegistry()->getColorSpace(KisID("RGBA"), "");
    int width = wdg->intWidth->value();
    int height = wdg->intHeight->value();
    KisImageSP img = new KisImage(doc->undoAdapter(), width, height, cs, "built image");
    img->blockSignals(true); // Don't send out signals while we're building the image
    
    // create a layer
    TQValueList<int> pages = wdg->pages();
    for(TQValueList<int>::const_iterator it = pages.begin(); it != pages.end(); ++it)
    {
        KisPaintLayer* layer = new KisPaintLayer(img, TQString(i18n("Page %1")).arg( TQString::number(*it) + 1), TQ_UINT8_MAX);
        layer->paintDevice()->convertFromTQImage( pdoc->getPage( *it )->renderToImage(wdg->intHorizontal->value(), wdg->intVertical->value() ), "");
        img->addLayer(layer, img->rootLayer(), 0);
    }
    
    img->blockSignals(false);
    doc -> setCurrentImage( img);
    
    TDEIO::NetAccess::removeTempFile(tmpFile);
    
    delete pdoc;
    delete kdb;
    return KoFilter::OK;
}