19 #include "browserrun.h"
20 #include <tdemessagebox.h>
21 #include <tdefiledialog.h>
22 #include <tdeio/job.h>
23 #include <tdeio/scheduler.h>
25 #include <tdeprocess.h>
26 #include <kstringhandler.h>
27 #include <kuserprofile.h>
28 #include <tdetempfile.h>
30 #include <kstandarddirs.h>
33 using namespace KParts;
35 class BrowserRun::BrowserRunPrivate
38 bool m_bHideErrorDialog;
44 bool removeReferrer,
bool trustedSource )
45 : KRun( url, window, 0 , false , false ),
46 m_args( args ), m_part( part ), m_window( window ),
47 m_bRemoveReferrer( removeReferrer ), m_bTrustedSource( trustedSource )
49 d =
new BrowserRunPrivate;
50 d->m_bHideErrorDialog =
false;
56 bool removeReferrer,
bool trustedSource,
bool hideErrorDialog )
57 : KRun( url, window, 0 , false , false ),
58 m_args( args ), m_part( part ), m_window( window ),
59 m_bRemoveReferrer( removeReferrer ), m_bTrustedSource( trustedSource )
61 d =
new BrowserRunPrivate;
65 BrowserRun::~BrowserRun()
72 if ( d->m_bHideErrorDialog )
77 if ( !m_strURL.isValid() ) {
78 redirectToError( TDEIO::ERR_MALFORMED_URL, m_strURL.url() );
81 if ( !m_bIsLocalFile && !m_bFault && m_strURL.isLocalFile() )
82 m_bIsLocalFile =
true;
84 if ( m_bIsLocalFile ) {
86 if ( stat( TQFile::encodeName(m_strURL.path()), &buff ) == -1 )
88 kdDebug(1000) <<
"BrowserRun::init : " << m_strURL.prettyURL() <<
" doesn't exist." <<
endl;
89 redirectToError( TDEIO::ERR_DOES_NOT_EXIST, m_strURL.path() );
92 m_mode = buff.st_mode;
100 kdDebug(1000) <<
"BrowserRun::scanfile " << m_strURL.prettyURL() <<
endl;
105 if ( m_strURL.query().isEmpty() && !m_strURL.protocol().startsWith(
"http") )
107 KMimeType::Ptr mime = KMimeType::findByURL( m_strURL );
108 assert( mime != 0L );
109 if ( mime->name() !=
"application/octet-stream" || m_bIsLocalFile )
111 kdDebug(1000) <<
"Scanfile: MIME TYPE is " << mime->name() <<
endl;
112 foundMimeType( mime->name() );
121 if (proto ==
"https" || proto ==
"webdavs") {
122 m_args.
metaData().insert(
"main_frame_request",
"TRUE" );
123 m_args.
metaData().insert(
"ssl_was_in_use",
"TRUE" );
124 m_args.
metaData().insert(
"ssl_activate_warnings",
"TRUE" );
125 }
else if (proto ==
"http" || proto ==
"webdav") {
126 m_args.
metaData().insert(
"ssl_activate_warnings",
"TRUE" );
127 m_args.
metaData().insert(
"ssl_was_in_use",
"FALSE" );
131 if (!m_args.
metaData().contains(
"PropagateHttpHeader"))
132 m_args.
metaData().insert(
"PropagateHttpHeader",
"TRUE");
135 TDEIO::TransferJob *job;
136 if ( m_args.
doPost() && m_strURL.protocol().startsWith(
"http"))
138 job = TDEIO::http_post( m_strURL, m_args.
postData,
false );
139 job->addMetaData(
"content-type", m_args.
contentType() );
142 job = TDEIO::get(m_strURL, m_args.
reload,
false);
144 if ( m_bRemoveReferrer )
145 m_args.
metaData().remove(
"referrer");
147 job->addMetaData( m_args.
metaData() );
148 job->setWindow( m_window );
149 connect( job, TQ_SIGNAL( result( TDEIO::Job *)),
150 this, TQ_SLOT( slotBrowserScanFinished(TDEIO::Job *)));
151 connect( job, TQ_SIGNAL( mimetype( TDEIO::Job *,
const TQString &)),
152 this, TQ_SLOT( slotBrowserMimetype(TDEIO::Job *,
const TQString &)));
156 void BrowserRun::slotBrowserScanFinished(TDEIO::Job *job)
158 kdDebug(1000) <<
"BrowserRun::slotBrowserScanFinished" <<
endl;
159 if ( job->error() == TDEIO::ERR_IS_DIRECTORY )
164 kdDebug(1000) <<
"It is in fact a directory!" <<
endl;
166 m_strURL =
static_cast<TDEIO::TransferJob *
>(job)->
url();
168 foundMimeType(
"inode/directory" );
175 KRun::slotScanFinished(job);
179 void BrowserRun::slotBrowserMimetype( TDEIO::Job *_job,
const TQString &type )
181 Q_ASSERT( _job == m_job );
182 TDEIO::TransferJob *job =
static_cast<TDEIO::TransferJob *
>(m_job);
186 m_strURL = job->url();
187 kdDebug(1000) <<
"slotBrowserMimetype: found " << type <<
" for " << m_strURL.prettyURL() <<
endl;
189 m_suggestedFilename = job->queryMetaData(
"content-disposition-filename");
190 d->contentDisposition = job->queryMetaData(
"content-disposition-type");
194 TQString _type = type;
198 KRun::setSuggestedFileName(m_suggestedFilename);
200 foundMimeType( _type );
205 TQString mimeType( _mimeType );
206 Q_ASSERT( !m_bFinished );
208 if ( mimeType !=
"inode/directory" &&
209 !m_strURL.isLocalFile() )
211 if ( isTextExecutable(mimeType) )
212 mimeType = TQString::fromLatin1(
"text/plain");
213 kdDebug(1000) <<
"BrowserRun: ask for saving" <<
endl;
214 KService::Ptr offer = KServiceTypeProfile::preferredService(mimeType,
"Application");
216 KParts::BrowserRun::AskSaveResult res =
askSave( m_strURL, offer, mimeType, m_suggestedFilename );
217 if ( res == KParts::BrowserRun::Save ) {
218 save( m_strURL, m_suggestedFilename );
219 kdDebug(1000) <<
"BrowserRun::handleNonEmbeddable: Save: returning Handled" <<
endl;
223 else if ( res == KParts::BrowserRun::Cancel ) {
225 kdDebug(1000) <<
"BrowserRun::handleNonEmbeddable: Cancel: returning Handled" <<
endl;
235 kdDebug(1000) <<
"BrowserRun: request comes from a POST, can't pass a URL to another app, need to save" <<
endl;
236 m_sMimeType = mimeType;
238 TQString fileName = m_suggestedFilename.isEmpty() ? m_strURL.fileName() : m_suggestedFilename;
239 int extensionPos = fileName.findRev(
'.' );
240 if ( extensionPos != -1 )
241 extension = fileName.mid( extensionPos );
242 KTempFile tempFile( TQString::null, extension );
245 TDEIO::Job *job = TDEIO::file_copy( m_strURL, destURL, 0600,
true ,
false ,
true );
246 job->setWindow (m_window);
247 connect( job, TQ_SIGNAL( result( TDEIO::Job *)),
248 this, TQ_SLOT( slotCopyToTempFileResult(TDEIO::Job *)) );
255 if ( !m_bTrustedSource &&
256 !allowExecution( mimeType, m_strURL ) )
262 TDEIO::SimpleJob::removeOnHold();
267 bool BrowserRun::allowExecution(
const TQString &serviceType,
const KURL &url )
276 i18n(
"Execute File?"), i18n(
"Execute") ) == KMessageBox::Continue );
279 static TQString makeQuestion(
const KURL& url,
const TQString& mimeType,
const TQString& suggestedFilename )
282 KMimeType::Ptr mime = KMimeType::mimeType( mimeType );
283 TQString comment = mimeType;
287 if (mime->name() != KMimeType::defaultMimeType()) {
289 comment = mime->comment();
293 if ( suggestedFilename.isEmpty() )
294 return i18n(
"Open '%2'?\nType: %1").arg(comment, surl);
296 return i18n(
"Open '%3'?\nName: %2\nType: %1").arg(comment, suggestedFilename, surl);
300 BrowserRun::AskSaveResult
BrowserRun::askSave(
const KURL & url, KService::Ptr offer,
const TQString& mimeType,
const TQString & suggestedFilename )
309 TQString openText = (offer && !offer->name().isEmpty())
310 ? i18n(
"&Open with '%1'").arg(offer->name())
311 : i18n(
"&Open With...");
315 KStdGuiItem::saveAs(), openText,
316 TQString::fromLatin1(
"askSave")+ mimeType );
318 return choice == KMessageBox::Yes ? Save : ( choice == KMessageBox::No ? Open : Cancel );
329 KMimeType::Ptr mime = KMimeType::mimeType( mimeType );
339 if (flags != (
int)AttachmentDisposition && (
340 mime->is(
"text/html" ) ||
341 mime->is(
"text/xml" ) ||
342 mime->is(
"inode/directory" ) ||
343 mimeType.startsWith(
"image" ) ||
344 mime->is(
"multipart/x-mixed-replace" ) ||
345 mime->is(
"multipart/replace" ) ||
346 mimeType.startsWith(
"print" ) ) )
353 KStdGuiItem::saveAs(),
KGuiItem( i18n(
"&Open" ),
"document-open"),
354 TQString::fromLatin1(
"askEmbedOrSave")+ mimeType );
355 return choice == KMessageBox::Yes ? Save : ( choice == KMessageBox::No ? Open : Cancel );
360 void BrowserRun::save(
const KURL & url,
const TQString & suggestedFilename )
366 void BrowserRun::simpleSave(
const KURL & url,
const TQString & suggestedFilename )
371 void BrowserRun::simpleSave(
const KURL & url,
const TQString & suggestedFilename,
380 TDEConfig cfg(
"konquerorrc",
false,
false);
381 cfg.setGroup(
"HTML Settings");
382 TQString downloadManger = cfg.readPathEntry(
"DownloadManager");
383 if (!downloadManger.isEmpty())
386 kdDebug(1000) <<
"Using: "<<downloadManger <<
" as Download Manager" <<
endl;
390 TQString errMsg=i18n(
"The Download Manager (%1) could not be found in your $PATH ").arg(downloadManger);
391 TQString errMsgEx= i18n(
"Try to reinstall it \n\nThe integration with Konqueror will be disabled!");
393 cfg.writePathEntry(
"DownloadManager",TQString::null);
406 kdDebug(1000) <<
"Calling command " << cmd <<
endl;
408 TDEIO::Scheduler::publishSlaveOnHold();
409 KRun::runCommand(cmd);
416 KFileDialog *dlg =
new KFileDialog( TQString::null, TQString::null ,
417 window ,
"filedialog",
true );
418 dlg->setOperationMode( KFileDialog::Saving );
419 dlg->setCaption(i18n(
"Save As"));
424 KURL destURL( dlg->selectedURL() );
425 if ( destURL.isValid() )
427 TDEIO::Job *job = TDEIO::copy(
url, destURL );
428 job->setWindow (window);
429 job->setAutoErrorHandlingEnabled(
true );
435 void BrowserRun::slotStatResult( TDEIO::Job *job )
437 if ( job->error() ) {
438 kdDebug(1000) <<
"BrowserRun::slotStatResult : " << job->errorString() <<
endl;
441 KRun::slotStatResult( job );
447 kdWarning(1000) <<
"BrowserRun::handleError called with job=0! hideErrorDialog=" << d->m_bHideErrorDialog <<
endl;
451 if (d->m_bHideErrorDialog && job->error() != TDEIO::ERR_NO_CONTENT)
453 redirectToError( job->error(), job->errorText() );
458 KRun::slotStatResult( job );
461 void BrowserRun::redirectToError(
int error,
const TQString& errorText )
473 KURL newURL(TQString(
"error:/?error=%1&errText=%2")
475 m_strURL.setPass( TQString::null );
478 lst << newURL << m_strURL;
483 foundMimeType(
"text/html" );
486 void BrowserRun::slotCopyToTempFileResult(TDEIO::Job *job)
488 if ( job->error() ) {
489 job->showErrorDialog( m_window );
492 (void) (KRun::runURL(
static_cast<TDEIO::FileCopyJob *
>(job)->destURL(), m_sMimeType ));
496 m_timer.start( 0,
true );
499 bool BrowserRun::isTextExecutable(
const TQString &serviceType )
501 return ( serviceType ==
"application/x-desktop" ||
502 serviceType ==
"media/builtin-mydocuments" ||
503 serviceType ==
"media/builtin-mycomputer" ||
504 serviceType ==
"media/builtin-mynetworkplaces" ||
505 serviceType ==
"media/builtin-printers" ||
506 serviceType ==
"media/builtin-trash" ||
507 serviceType ==
"media/builtin-webbrowser" ||
508 serviceType ==
"application/x-shellscript" );
513 return KRun::isExecutable( serviceType );
518 return d->m_bHideErrorDialog;
522 return d->contentDisposition;
525 #include "browserrun.moc"
static void detailedSorry(TQWidget *parent, const TQString &text, const TQString &details, const TQString &caption=TQString::null, int options=Notify)
static int warningContinueCancel(TQWidget *parent, const TQString &text, const TQString &caption=TQString::null, const KGuiItem &buttonContinue=KStdGuiItem::cont(), const TQString &dontAskAgainName=TQString::null, int options=Notify)
static int questionYesNoCancel(TQWidget *parent, const TQString &text, const TQString &caption=TQString::null, const KGuiItem &buttonYes=KStdGuiItem::yes(), const KGuiItem &buttonNo=KStdGuiItem::no(), const TQString &dontAskAgainName=TQString::null, int options=Notify)
virtual void handleError(TDEIO::Job *job)
Called when an error happens.
bool hideErrorDialog() const
virtual void scanFile()
Reimplemented from KRun.
static AskSaveResult askEmbedOrSave(const KURL &url, const TQString &mimeType, const TQString &suggestedFilename=TQString::null, int flags=0)
Similar to askSave() but for the case where the current application is able to embed the url itself (...
virtual void init()
Reimplemented from KRun.
BrowserRun(const KURL &url, const KParts::URLArgs &args, KParts::ReadOnlyPart *part, TQWidget *window, bool removeReferrer, bool trustedSource)
static AskSaveResult askSave(const KURL &url, KService::Ptr offer, const TQString &mimeType, const TQString &suggestedFilename=TQString::null)
Ask the user whether to save or open a url in another application.
TQString suggestedFilename() const
NonEmbeddableResult handleNonEmbeddable(const TQString &mimeType)
Helper for foundMimeType: call this if the mimetype couldn't be embedded.
static bool isExecutable(const TQString &serviceType)
BIC: Obsoleted by KRun::isExecutable( const TQString &serviceType );.
NonEmbeddableResult
NotHandled means that foundMimeType should call KRun::foundMimeType, i.e.
TQString contentDisposition() const
Base class for any "viewer" part.
KURL url() const
Returns the currently in part used URL.
static TQString csqueeze(const TQString &str, uint maxlen=40)
static TQString encode_string(const TQString &str, int encoding_hint=0)
void setPath(const TQString &path)
TQString protocol() const
static KURL join(const List &_list)
TQString fileName(bool _ignore_trailing_slash_in_path=true) const
TQString url(int _trailing=0, int encoding_hint=0) const
TQString prettyURL(int _trailing=0) const
static TQString quote(const TQString &arg)
static TQString findExe(const TQString &appname, const TQString &pathstr=TQString::null, bool ignoreExecBit=false)
kndbgstream & endl(kndbgstream &s)
kdbgstream kdWarning(int area=0)
kdbgstream kdDebug(int area=0)
URLArgs is a set of arguments bundled into a structure, to allow specifying how a URL should be opene...
TQString contentType() const
TDEHTML-specific field, header defining the type of the POST data.
TQMap< TQString, TQString > & metaData()
Meta-data to associate with the next TDEIO operation.
bool reload
reload is set when the cache shouldn't be used (forced reload).
bool doPost() const
TDEHTML-specific field, whether to do a POST instead of a GET, for the next openURL.
TQByteArray postData
TDEHTML-specific field, contents of the HTTP POST data.