File: khtmlimage.cpp

package info (click to toggle)
khtml 5.54.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 18,940 kB
  • sloc: cpp: 206,281; java: 4,060; ansic: 2,829; perl: 2,313; yacc: 1,497; python: 339; sh: 141; xml: 37; makefile: 7
file content (339 lines) | stat: -rw-r--r-- 10,826 bytes parent folder | download | duplicates (2)
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
/* This file is part of the KDE project
   Copyright (C) 2000 Simon Hausmann <hausmann@kde.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include "khtmlimage.h"
#include "khtmlview.h"
#include "khtml_ext.h"
#include "xml/dom_docimpl.h"
#include "html/html_documentimpl.h"
#include "html/html_elementimpl.h"
#include "rendering/render_image.h"
#include "misc/loader.h"

#include <QTimer>
#include <QVBoxLayout>

#include <kparts/guiactivateevent.h>
#include <kjobuidelegate.h>
#include <kio/job.h>
#include <qmimedatabase.h>
#include <klocalizedstring.h>
#include <kactioncollection.h>
#include <kaboutdata.h>

#include "../khtml_version.h"


KHTMLImage::KHTMLImage(QWidget *parentWidget,
                       QObject *parent, const QVariantList &args)
    : KParts::ReadOnlyPart(parent), m_image(nullptr)
{
    KHTMLPart *parentPart = qobject_cast<KHTMLPart *>(parent);

    KAboutData about("khtmlimage", i18n("KHTML Image"), QStringLiteral(KHTML_VERSION_STRING));
    KHTMLPart::GUIProfile prof = KHTMLPart::DefaultGUI;
    if (args.contains("Browser/View")) {
        prof = KHTMLPart::BrowserViewGUI;
    }
    setComponentData(about, prof == KHTMLPart::BrowserViewGUI && !parentPart);

    QWidget *box = new QWidget(parentWidget);
    box->setLayout(new QVBoxLayout(box));
    box->setAcceptDrops(true);

    m_khtml = new KHTMLPart(box, this, prof);
    box->layout()->addWidget(m_khtml->widget());
    m_khtml->setAutoloadImages(true);

    // We do not want our subpart to be destroyed when its widget is,
    // since that may cause all KHTMLParts to die when we're dealing
    // with
    m_khtml->setAutoDeletePart(false);

    connect(m_khtml->view(), SIGNAL(finishedLayout()), this, SLOT(restoreScrollPosition()));

    setWidget(box);

    // VBox can't take focus, so pass it on to sub-widget
    box->setFocusProxy(m_khtml->widget());

    m_ext = new KHTMLImageBrowserExtension(this);
    m_ext->setObjectName("be");

    m_sbExt = new KParts::StatusBarExtension(this);
    m_sbExt->setObjectName("sbe");

    // Remove unnecessary actions.
    delete actionCollection()->action("setEncoding");
    delete actionCollection()->action("viewDocumentSource");
    delete actionCollection()->action("selectAll");

    // forward important signals from the khtml part

    // forward opening requests to parent frame (if existing)
    KHTMLPart *p = qobject_cast<KHTMLPart *>(parent);
    KParts::BrowserExtension *be = p ? p->browserExtension() : m_ext;
    connect(m_khtml->browserExtension(), SIGNAL(openUrlRequestDelayed(QUrl,KParts::OpenUrlArguments,KParts::BrowserArguments)),
            be, SIGNAL(openUrlRequestDelayed(QUrl,KParts::OpenUrlArguments,KParts::BrowserArguments)));

    connect(m_khtml->browserExtension(), SIGNAL(popupMenu(QPoint,QUrl,mode_t,KParts::OpenUrlArguments,KParts::BrowserArguments,KParts::BrowserExtension::PopupFlags,KParts::BrowserExtension::ActionGroupMap)),
            this, SLOT(slotPopupMenu(QPoint,QUrl,mode_t,KParts::OpenUrlArguments,KParts::BrowserArguments,KParts::BrowserExtension::PopupFlags,KParts::BrowserExtension::ActionGroupMap)));

    connect(m_khtml->browserExtension(), SIGNAL(enableAction(const char*,bool)),
            m_ext, SIGNAL(enableAction(const char*,bool)));

    m_ext->setURLDropHandlingEnabled(true);
}

KHTMLImage::~KHTMLImage()
{
    disposeImage();

    // important: delete the html part before the part or qobject destructor runs.
    // we now delete the htmlpart which deletes the part's widget which makes
    // _OUR_ m_widget 0 which in turn avoids our part destructor to delete the
    // widget ;-)
    // ### additional note: it _can_ be that the part has been deleted before:
    // when we're in a html frameset and the view dies first, then it will also
    // kill the htmlpart
    if (m_khtml) {
        delete static_cast<KHTMLPart *>(m_khtml);
    }
}

bool KHTMLImage::openUrl(const QUrl &url)
{
    static const QString &html = QString::fromLatin1("<html><body><img src=\"%1\"></body></html>");

    // Propagate statusbar to our kid part.
    KParts::StatusBarExtension::childObject(m_khtml)->setStatusBar(m_sbExt->statusBar());

    disposeImage();

    setUrl(url);

    emit started(nullptr);

    KParts::OpenUrlArguments args = arguments();
    m_mimeType = args.mimeType();

    QUrl kurl(url);
    emit setWindowCaption(kurl.toDisplayString());

    // Need to keep a copy of the offsets since they are cleared when emitting completed
    m_xOffset = args.xOffset();
    m_yOffset = args.yOffset();

    m_khtml->begin(this->url());
    m_khtml->setAutoloadImages(true);

    DOM::DocumentImpl *impl = dynamic_cast<DOM::DocumentImpl *>(m_khtml->document().handle());   // ### hack ;-)
    if (!impl) {
        return false;
    }

    if (arguments().reload()) {
        impl->docLoader()->setCachePolicy(KIO::CC_Reload);
    }

    khtml::DocLoader *dl = impl->docLoader();
    m_image = dl->requestImage(this->url().toString());
    if (m_image) {
        m_image->ref(this);
    }

    m_khtml->write(html.arg(this->url().toString()));
    m_khtml->end();

    /*
    connect( khtml::Cache::loader(), SIGNAL(requestDone(khtml::DocLoader*,khtml::CachedObject*)),
            this, SLOT(updateWindowCaption()) );
            */
    return true;
}

bool KHTMLImage::closeUrl()
{
    disposeImage();
    return m_khtml->closeUrl();
}

// This can happen after openUrl returns, or directly from m_image->ref()
void KHTMLImage::notifyFinished(khtml::CachedObject *o)
{
    if (!m_image || o != m_image) {
        return;
    }

    //const QPixmap &pix = m_image->pixmap();
    QString caption;

    QMimeDatabase db;
    QMimeType mimeType;
    if (!m_mimeType.isEmpty()) {
        mimeType = db.mimeTypeForName(m_mimeType);
    }

    if (mimeType.isValid()) {
        if (!m_image->suggestedTitle().isEmpty()) {
            caption = i18n("%1 (%2 - %3x%4 Pixels)", m_image->suggestedTitle(), mimeType.comment(), m_image->pixmap_size().width(), m_image->pixmap_size().height());
        } else {
            caption = i18n("%1 - %2x%3 Pixels",  mimeType.comment(),
                           m_image->pixmap_size().width(),  m_image->pixmap_size().height());
        }
    } else {
        if (!m_image->suggestedTitle().isEmpty()) {
            caption = i18n("%1 (%2x%3 Pixels)", m_image->suggestedTitle(),  m_image->pixmap_size().width(),  m_image->pixmap_size().height());
        } else {
            caption = i18n("Image - %1x%2 Pixels",  m_image->pixmap_size().width(),  m_image->pixmap_size().height());
        }
    }

    emit setWindowCaption(caption);
    emit completed();
    emit setStatusBarText(i18n("Done."));
}

void KHTMLImage::restoreScrollPosition()
{
    if (m_khtml->view()->contentsY() == 0) {
        m_khtml->view()->setContentsPos(m_xOffset, m_yOffset);
    }
}

void KHTMLImage::guiActivateEvent(KParts::GUIActivateEvent *e)
{
    // prevent the base implementation from emitting setWindowCaption with
    // our url. It destroys our pretty, previously caption. Konq saves/restores
    // the caption for us anyway.
    if (e->activated()) {
        return;
    }
    KParts::ReadOnlyPart::guiActivateEvent(e);
}

/*
void KHTMLImage::slotImageJobFinished( KIO::Job *job )
{
    if ( job->error() )
    {
        job->uiDelegate()->showErrorMessage();
        emit canceled( job->errorString() );
    }
    else
    {
        emit completed();
        QTimer::singleShot( 0, this, SLOT(updateWindowCaption()) );
    }
}

void KHTMLImage::updateWindowCaption()
{
    if ( !m_khtml )
        return;

    DOM::HTMLDocumentImpl *impl = dynamic_cast<DOM::HTMLDocumentImpl *>( m_khtml->document().handle() );
    if ( !impl )
        return;

    DOM::HTMLElementImpl *body = impl->body();
    if ( !body )
        return;

    DOM::NodeImpl *image = body->firstChild();
    if ( !image )
        return;

    khtml::RenderImage *renderImage = dynamic_cast<khtml::RenderImage *>( image->renderer() );
    if ( !renderImage )
        return;

    QPixmap pix = renderImage->pixmap();

    QString caption;

    KMimeType::Ptr mimeType;
    if ( !m_mimeType.isEmpty() )
        mimeType = KMimeType::mimeType( m_mimeType, KMimeType::ResolveAliases );

    if ( mimeType )
        caption = i18n( "%1 - %2x%3 Pixels" ).arg( mimeType.comment() )
                  .arg( pix.width() ).arg( pix.height() );
    else
        caption = i18n( "Image - %1x%2 Pixels" ).arg( pix.width() ).arg( pix.height() );

    emit setWindowCaption( caption );
    emit completed();
    emit setStatusBarText(i18n("Done."));
}
*/

void KHTMLImage::disposeImage()
{
    if (!m_image) {
        return;
    }

    m_image->deref(this);
    m_image = nullptr;
}

KHTMLImageBrowserExtension::KHTMLImageBrowserExtension(KHTMLImage *parent)
    : KParts::BrowserExtension(parent)
{
    m_imgPart = parent;
}

int KHTMLImageBrowserExtension::xOffset()
{
    return m_imgPart->doc()->view()->contentsX();
}

int KHTMLImageBrowserExtension::yOffset()
{
    return m_imgPart->doc()->view()->contentsY();
}

void KHTMLImageBrowserExtension::print()
{
    static_cast<KHTMLPartBrowserExtension *>(m_imgPart->doc()->browserExtension())->print();
}

void KHTMLImageBrowserExtension::reparseConfiguration()
{
    static_cast<KHTMLPartBrowserExtension *>(m_imgPart->doc()->browserExtension())->reparseConfiguration();
    m_imgPart->doc()->setAutoloadImages(true);
}

void KHTMLImageBrowserExtension::disableScrolling()
{
    static_cast<KHTMLPartBrowserExtension *>(m_imgPart->doc()->browserExtension())->disableScrolling();
}

void KHTMLImage::slotPopupMenu(const QPoint &global, const QUrl &url, mode_t mode,
                               const KParts::OpenUrlArguments &origArgs,
                               const KParts::BrowserArguments &browserArgs,
                               KParts::BrowserExtension::PopupFlags flags,
                               const KParts::BrowserExtension::ActionGroupMap &actionGroups)
{
    KParts::OpenUrlArguments args = origArgs;
    args.setMimeType(m_mimeType);
    m_ext->popupMenu(global, url, mode, args, browserArgs, flags, actionGroups);
}