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
|
/* This file is part of the KDE project
*
* Copyright (C) 2002 David Faure <faure@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 version 2, as published by the Free Software Foundation.
*
* 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 "browserrun.h"
#include <tdemessagebox.h>
#include <tdefiledialog.h>
#include <tdeio/job.h>
#include <tdeio/scheduler.h>
#include <tdelocale.h>
#include <kprocess.h>
#include <kstringhandler.h>
#include <kuserprofile.h>
#include <tdetempfile.h>
#include <kdebug.h>
#include <kstandarddirs.h>
#include <assert.h>
using namespace KParts;
class BrowserRun::BrowserRunPrivate
{
public:
bool m_bHideErrorDialog;
TQString contentDisposition;
};
BrowserRun::BrowserRun( const KURL& url, const KParts::URLArgs& args,
KParts::ReadOnlyPart *part, TQWidget* window,
bool removeReferrer, bool trustedSource )
: KRun( url, window, 0 /*mode*/, false /*is_local_file known*/, false /* no GUI */ ),
m_args( args ), m_part( part ), m_window( window ),
m_bRemoveReferrer( removeReferrer ), m_bTrustedSource( trustedSource )
{
d = new BrowserRunPrivate;
d->m_bHideErrorDialog = false;
}
// BIC: merge with above ctor
BrowserRun::BrowserRun( const KURL& url, const KParts::URLArgs& args,
KParts::ReadOnlyPart *part, TQWidget* window,
bool removeReferrer, bool trustedSource, bool hideErrorDialog )
: KRun( url, window, 0 /*mode*/, false /*is_local_file known*/, false /* no GUI */ ),
m_args( args ), m_part( part ), m_window( window ),
m_bRemoveReferrer( removeReferrer ), m_bTrustedSource( trustedSource )
{
d = new BrowserRunPrivate;
d->m_bHideErrorDialog = hideErrorDialog;
}
BrowserRun::~BrowserRun()
{
delete d;
}
void BrowserRun::init()
{
if ( d->m_bHideErrorDialog )
{
// ### KRun doesn't call a virtual method when it finds out that the URL
// is either malformed, or points to a non-existing local file...
// So we need to reimplement some of the checks, to handle m_bHideErrorDialog
if ( !m_strURL.isValid() ) {
redirectToError( TDEIO::ERR_MALFORMED_URL, m_strURL.url() );
return;
}
if ( !m_bIsLocalFile && !m_bFault && m_strURL.isLocalFile() )
m_bIsLocalFile = true;
if ( m_bIsLocalFile ) {
struct stat buff;
if ( stat( TQFile::encodeName(m_strURL.path()), &buff ) == -1 )
{
kdDebug(1000) << "BrowserRun::init : " << m_strURL.prettyURL() << " doesn't exist." << endl;
redirectToError( TDEIO::ERR_DOES_NOT_EXIST, m_strURL.path() );
return;
}
m_mode = buff.st_mode; // while we're at it, save it for KRun::init() to use it
}
}
KRun::init();
}
void BrowserRun::scanFile()
{
kdDebug(1000) << "BrowserRun::scanfile " << m_strURL.prettyURL() << endl;
// Let's check for well-known extensions
// Not when there is a query in the URL, in any case.
// Optimization for http/https, findByURL doesn't trust extensions over http.
if ( m_strURL.query().isEmpty() && !m_strURL.protocol().startsWith("http") )
{
KMimeType::Ptr mime = KMimeType::findByURL( m_strURL );
assert( mime != 0L );
if ( mime->name() != "application/octet-stream" || m_bIsLocalFile )
{
kdDebug(1000) << "Scanfile: MIME TYPE is " << mime->name() << endl;
foundMimeType( mime->name() );
return;
}
}
if ( m_part )
{
TQString proto = m_part->url().protocol().lower();
if (proto == "https" || proto == "webdavs") {
m_args.metaData().insert("main_frame_request", "TRUE" );
m_args.metaData().insert("ssl_was_in_use", "TRUE" );
m_args.metaData().insert("ssl_activate_warnings", "TRUE" );
} else if (proto == "http" || proto == "webdav") {
m_args.metaData().insert("ssl_activate_warnings", "TRUE" );
m_args.metaData().insert("ssl_was_in_use", "FALSE" );
}
// Set the PropagateHttpHeader meta-data if it has not already been set...
if (!m_args.metaData().contains("PropagateHttpHeader"))
m_args.metaData().insert("PropagateHttpHeader", "TRUE");
}
TDEIO::TransferJob *job;
if ( m_args.doPost() && m_strURL.protocol().startsWith("http"))
{
job = TDEIO::http_post( m_strURL, m_args.postData, false );
job->addMetaData( "content-type", m_args.contentType() );
}
else
job = TDEIO::get(m_strURL, m_args.reload, false);
if ( m_bRemoveReferrer )
m_args.metaData().remove("referrer");
job->addMetaData( m_args.metaData() );
job->setWindow( m_window );
connect( job, TQT_SIGNAL( result( TDEIO::Job *)),
this, TQT_SLOT( slotBrowserScanFinished(TDEIO::Job *)));
connect( job, TQT_SIGNAL( mimetype( TDEIO::Job *, const TQString &)),
this, TQT_SLOT( slotBrowserMimetype(TDEIO::Job *, const TQString &)));
m_job = job;
}
void BrowserRun::slotBrowserScanFinished(TDEIO::Job *job)
{
kdDebug(1000) << "BrowserRun::slotBrowserScanFinished" << endl;
if ( job->error() == TDEIO::ERR_IS_DIRECTORY )
{
// It is in fact a directory. This happens when HTTP redirects to FTP.
// Due to the "protocol doesn't support listing" code in BrowserRun, we
// assumed it was a file.
kdDebug(1000) << "It is in fact a directory!" << endl;
// Update our URL in case of a redirection
m_strURL = static_cast<TDEIO::TransferJob *>(job)->url();
m_job = 0;
foundMimeType( "inode/directory" );
}
else
{
if ( job->error() )
handleError( job );
else
KRun::slotScanFinished(job);
}
}
void BrowserRun::slotBrowserMimetype( TDEIO::Job *_job, const TQString &type )
{
Q_ASSERT( _job == m_job );
TDEIO::TransferJob *job = static_cast<TDEIO::TransferJob *>(m_job);
// Update our URL in case of a redirection
//kdDebug(1000) << "old URL=" << m_strURL.url() << endl;
//kdDebug(1000) << "new URL=" << job->url().url() << endl;
m_strURL = job->url();
kdDebug(1000) << "slotBrowserMimetype: found " << type << " for " << m_strURL.prettyURL() << endl;
m_suggestedFilename = job->queryMetaData("content-disposition-filename");
d->contentDisposition = job->queryMetaData("content-disposition-type");
//kdDebug(1000) << "m_suggestedFilename=" << m_suggestedFilename << endl;
// Make a copy to avoid a dead reference
TQString _type = type;
job->putOnHold();
m_job = 0;
KRun::setSuggestedFileName(m_suggestedFilename);
foundMimeType( _type );
}
BrowserRun::NonEmbeddableResult BrowserRun::handleNonEmbeddable( const TQString& _mimeType )
{
TQString mimeType( _mimeType );
Q_ASSERT( !m_bFinished ); // only come here if the mimetype couldn't be embedded
// Support for saving remote files.
if ( mimeType != "inode/directory" && // dirs can't be saved
!m_strURL.isLocalFile() )
{
if ( isTextExecutable(mimeType) )
mimeType = TQString::fromLatin1("text/plain"); // view, don't execute
kdDebug(1000) << "BrowserRun: ask for saving" << endl;
KService::Ptr offer = KServiceTypeProfile::preferredService(mimeType, "Application");
// ... -> ask whether to save
KParts::BrowserRun::AskSaveResult res = askSave( m_strURL, offer, mimeType, m_suggestedFilename );
if ( res == KParts::BrowserRun::Save ) {
save( m_strURL, m_suggestedFilename );
kdDebug(1000) << "BrowserRun::handleNonEmbeddable: Save: returning Handled" << endl;
m_bFinished = true;
return Handled;
}
else if ( res == KParts::BrowserRun::Cancel ) {
// saving done or canceled
kdDebug(1000) << "BrowserRun::handleNonEmbeddable: Cancel: returning Handled" << endl;
m_bFinished = true;
return Handled;
}
else // "Open" chosen (done by KRun::foundMimeType, called when returning NotHandled)
{
// If we were in a POST, we can't just pass a URL to an external application.
// We must save the data to a tempfile first.
if ( m_args.doPost() )
{
kdDebug(1000) << "BrowserRun: request comes from a POST, can't pass a URL to another app, need to save" << endl;
m_sMimeType = mimeType;
TQString extension;
TQString fileName = m_suggestedFilename.isEmpty() ? m_strURL.fileName() : m_suggestedFilename;
int extensionPos = fileName.findRev( '.' );
if ( extensionPos != -1 )
extension = fileName.mid( extensionPos ); // keep the '.'
KTempFile tempFile( TQString::null, extension );
KURL destURL;
destURL.setPath( tempFile.name() );
TDEIO::Job *job = TDEIO::file_copy( m_strURL, destURL, 0600, true /*overwrite*/, false /*no resume*/, true /*progress info*/ );
job->setWindow (m_window);
connect( job, TQT_SIGNAL( result( TDEIO::Job *)),
this, TQT_SLOT( slotCopyToTempFileResult(TDEIO::Job *)) );
return Delayed; // We'll continue after the job has finished
}
}
}
// Check if running is allowed
if ( !m_bTrustedSource && // ... and untrusted source...
!allowExecution( mimeType, m_strURL ) ) // ...and the user said no (for executables etc.)
{
m_bFinished = true;
return Handled;
}
TDEIO::SimpleJob::removeOnHold(); // Kill any slave that was put on hold.
return NotHandled;
}
//static
bool BrowserRun::allowExecution( const TQString &serviceType, const KURL &url )
{
if ( !isExecutable( serviceType ) )
return true;
if ( !url.isLocalFile() ) // Don't permit to execute remote files
return false;
return ( KMessageBox::warningContinueCancel( 0, i18n( "Do you really want to execute '%1'? " ).arg( url.prettyURL() ),
i18n("Execute File?"), i18n("Execute") ) == KMessageBox::Continue );
}
static TQString makeQuestion( const KURL& url, const TQString& mimeType, const TQString& suggestedFilename )
{
TQString surl = KStringHandler::csqueeze( url.prettyURL() );
KMimeType::Ptr mime = KMimeType::mimeType( mimeType );
TQString comment = mimeType;
// Test if the mimeType is not recognize as octet-stream.
// If so then keep mime-type as comment
if (mime->name() != KMimeType::defaultMimeType()) {
// The mime-type is known so display the comment instead of mime-type
comment = mime->comment();
}
// The strange order in the i18n() calls below is due to the possibility
// of surl containing a '%'
if ( suggestedFilename.isEmpty() )
return i18n("Open '%2'?\nType: %1").arg(comment, surl);
else
return i18n("Open '%3'?\nName: %2\nType: %1").arg(comment, suggestedFilename, surl);
}
//static
BrowserRun::AskSaveResult BrowserRun::askSave( const KURL & url, KService::Ptr offer, const TQString& mimeType, const TQString & suggestedFilename )
{
// SYNC SYNC SYNC SYNC SYNC SYNC SYNC SYNC SYNC SYNC SYNC SYNC SYNC SYNC
// NOTE: Keep this function in sync with tdebase/kcontrol/filetypes/filetypedetails.cpp
// FileTypeDetails::updateAskSave()
TQString question = makeQuestion( url, mimeType, suggestedFilename );
// Text used for the open button
TQString openText = (offer && !offer->name().isEmpty())
? i18n("&Open with '%1'").arg(offer->name())
: i18n("&Open With...");
int choice = KMessageBox::questionYesNoCancel(
0L, question, url.host(),
KStdGuiItem::saveAs(), openText,
TQString::fromLatin1("askSave")+ mimeType ); // dontAskAgainName, KEEP IN SYNC!!!
return choice == KMessageBox::Yes ? Save : ( choice == KMessageBox::No ? Open : Cancel );
// SYNC SYNC SYNC SYNC SYNC SYNC SYNC SYNC SYNC SYNC SYNC SYNC SYNC SYNC
}
//static
BrowserRun::AskSaveResult BrowserRun::askEmbedOrSave( const KURL & url, const TQString& mimeType, const TQString & suggestedFilename, int flags )
{
// SYNC SYNC SYNC SYNC SYNC SYNC SYNC SYNC SYNC SYNC SYNC SYNC SYNC SYNC
// NOTE: Keep this funcion in sync with tdebase/kcontrol/filetypes/filetypedetails.cpp
// FileTypeDetails::updateAskSave()
KMimeType::Ptr mime = KMimeType::mimeType( mimeType );
// Don't ask for:
// - html (even new tabs would ask, due to about:blank!)
// - dirs obviously (though not common over HTTP :),
// - images (reasoning: no need to save, most of the time, because fast to see)
// e.g. postscript is different, because takes longer to read, so
// it's more likely that the user might want to save it.
// - multipart/* ("server push", see tdemultipart)
// - other strange 'internal' mimetypes like print/manager...
// KEEP IN SYNC!!!
if (flags != (int)AttachmentDisposition && (
mime->is( "text/html" ) ||
mime->is( "text/xml" ) ||
mime->is( "inode/directory" ) ||
mimeType.startsWith( "image" ) ||
mime->is( "multipart/x-mixed-replace" ) ||
mime->is( "multipart/replace" ) ||
mimeType.startsWith( "print" ) ) )
return Open;
TQString question = makeQuestion( url, mimeType, suggestedFilename );
int choice = KMessageBox::questionYesNoCancel(
0L, question, url.host(),
KStdGuiItem::saveAs(), KGuiItem( i18n( "&Open" ), "document-open"),
TQString::fromLatin1("askEmbedOrSave")+ mimeType ); // dontAskAgainName, KEEP IN SYNC!!!
return choice == KMessageBox::Yes ? Save : ( choice == KMessageBox::No ? Open : Cancel );
// SYNC SYNC SYNC SYNC SYNC SYNC SYNC SYNC SYNC SYNC SYNC SYNC SYNC SYNC
}
// Default implementation, overridden in TDEHTMLRun
void BrowserRun::save( const KURL & url, const TQString & suggestedFilename )
{
simpleSave( url, suggestedFilename, m_window );
}
// static
void BrowserRun::simpleSave( const KURL & url, const TQString & suggestedFilename )
{
simpleSave (url, suggestedFilename, 0);
}
void BrowserRun::simpleSave( const KURL & url, const TQString & suggestedFilename,
TQWidget* window )
{
// DownloadManager <-> konqueror integration
// find if the integration is enabled
// the empty key means no integration
// only use the downloadmanager for non-local urls
if ( !url.isLocalFile() )
{
TDEConfig cfg("konquerorrc", false, false);
cfg.setGroup("HTML Settings");
TQString downloadManger = cfg.readPathEntry("DownloadManager");
if (!downloadManger.isEmpty())
{
// then find the download manager location
kdDebug(1000) << "Using: "<<downloadManger <<" as Download Manager" <<endl;
TQString cmd=TDEStandardDirs::findExe(downloadManger);
if (cmd.isEmpty())
{
TQString errMsg=i18n("The Download Manager (%1) could not be found in your $PATH ").arg(downloadManger);
TQString errMsgEx= i18n("Try to reinstall it \n\nThe integration with Konqueror will be disabled!");
KMessageBox::detailedSorry(0,errMsg,errMsgEx);
cfg.writePathEntry("DownloadManager",TQString::null);
cfg.sync ();
}
else
{
// ### suggestedFilename not taken into account. Fix this (and
// the duplicated code) with shiny new KDownload class for 3.2 (pfeiffer)
// Until the shiny new class comes about, send the suggestedFilename
// along with the actual URL to download. (DA)
cmd += " " + TDEProcess::quote(url.url());
if ( !suggestedFilename.isEmpty() )
cmd +=" " + TDEProcess::quote(suggestedFilename);
kdDebug(1000) << "Calling command " << cmd << endl;
// slave is already on hold (slotBrowserMimetype())
TDEIO::Scheduler::publishSlaveOnHold();
KRun::runCommand(cmd);
return;
}
}
}
// no download manager available, let's do it ourself
KFileDialog *dlg = new KFileDialog( TQString::null, TQString::null /*all files*/,
window , "filedialog", true );
dlg->setOperationMode( KFileDialog::Saving );
dlg->setCaption(i18n("Save As"));
dlg->setSelection( suggestedFilename.isEmpty() ? url.fileName() : suggestedFilename );
if ( dlg->exec() )
{
KURL destURL( dlg->selectedURL() );
if ( destURL.isValid() )
{
TDEIO::Job *job = TDEIO::copy( url, destURL );
job->setWindow (window);
job->setAutoErrorHandlingEnabled( true );
}
}
delete dlg;
}
void BrowserRun::slotStatResult( TDEIO::Job *job )
{
if ( job->error() ) {
kdDebug(1000) << "BrowserRun::slotStatResult : " << job->errorString() << endl;
handleError( job );
} else
KRun::slotStatResult( job );
}
void BrowserRun::handleError( TDEIO::Job * job )
{
if ( !job ) { // Shouldn't happen, see docu.
kdWarning(1000) << "BrowserRun::handleError called with job=0! hideErrorDialog=" << d->m_bHideErrorDialog << endl;
return;
}
if (d->m_bHideErrorDialog && job->error() != TDEIO::ERR_NO_CONTENT)
{
redirectToError( job->error(), job->errorText() );
return;
}
// Reuse code in KRun, to benefit from d->m_showingError etc.
KRun::slotStatResult( job );
}
void BrowserRun::redirectToError( int error, const TQString& errorText )
{
/**
* To display this error in TDEHTMLPart instead of inside a dialog box,
* we tell konq that the mimetype is text/html, and we redirect to
* an error:/ URL that sends the info to tdehtml.
*
* The format of the error:/ URL is error:/?query#url,
* where two variables are passed in the query:
* error = int tdeio error code, errText = TQString error text from tdeio
* The sub-url is the URL that we were trying to open.
*/
KURL newURL(TQString("error:/?error=%1&errText=%2")
.arg( error ).arg( KURL::encode_string(errorText) ), 106 );
m_strURL.setPass( TQString::null ); // don't put the password in the error URL
KURL::List lst;
lst << newURL << m_strURL;
m_strURL = KURL::join( lst );
//kdDebug(1202) << "BrowserRun::handleError m_strURL=" << m_strURL.prettyURL() << endl;
m_job = 0;
foundMimeType( "text/html" );
}
void BrowserRun::slotCopyToTempFileResult(TDEIO::Job *job)
{
if ( job->error() ) {
job->showErrorDialog( m_window );
} else {
// Same as KRun::foundMimeType but with a different URL
(void) (KRun::runURL( static_cast<TDEIO::FileCopyJob *>(job)->destURL(), m_sMimeType ));
}
m_bFault = true; // see above
m_bFinished = true;
m_timer.start( 0, true );
}
bool BrowserRun::isTextExecutable( const TQString &serviceType )
{
return ( serviceType == "application/x-desktop" ||
serviceType == "media/builtin-mydocuments" ||
serviceType == "media/builtin-mycomputer" ||
serviceType == "media/builtin-mynetworkplaces" ||
serviceType == "media/builtin-printers" ||
serviceType == "media/builtin-trash" ||
serviceType == "media/builtin-webbrowser" ||
serviceType == "application/x-shellscript" );
}
bool BrowserRun::isExecutable( const TQString &serviceType )
{
return KRun::isExecutable( serviceType );
}
bool BrowserRun::hideErrorDialog() const
{
return d->m_bHideErrorDialog;
}
TQString BrowserRun::contentDisposition() const {
return d->contentDisposition;
}
#include "browserrun.moc"
|