diff options
author | Andrea Diamantini <adjam7@gmail.com> | 2009-07-23 11:48:58 +0200 |
---|---|---|
committer | Andrea Diamantini <adjam7@gmail.com> | 2009-07-24 01:33:59 +0200 |
commit | 59652d084d3a2ad69693440ebc6b691a74e68bf1 (patch) | |
tree | c2cbf81bea487c07ae211609e20431c2a65a30d1 | |
parent | Definitely fixing rekonq API. First step. (diff) | |
download | rekonq-59652d084d3a2ad69693440ebc6b691a74e68bf1.tar.xz |
Added NON fixed rekonqrun class.
Sorry about this, but I need to start from some place :)
-rw-r--r-- | src/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/rekonqrun.cpp | 201 | ||||
-rw-r--r-- | src/rekonqrun.h | 72 |
3 files changed, 274 insertions, 0 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 73ef1dc0..726ac82e 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -32,6 +32,7 @@ SET( rekonq_SRCS webpage.cpp cookiedialog.cpp cookieexceptiondialog.cpp + rekonqrun.cpp ) diff --git a/src/rekonqrun.cpp b/src/rekonqrun.cpp new file mode 100644 index 00000000..ecabe9bc --- /dev/null +++ b/src/rekonqrun.cpp @@ -0,0 +1,201 @@ +/* ============================================================ +* +* This file is a part of the rekonq project +* +* Copyright (C) 2009 by Andrea Diamantini <adjam7 at gmail dot com> +* +* +* 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 3, 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. +* +* ============================================================ */ + + +// Self Includes +#include "rekonqrun.h" +#include "rekonqrun.moc" + +// Auto Includes +#include "rekonq.h" + +// Local Includes +#include "mainview.h" +#include "urlbar.h" + +// KDE Includes +#include <KToolInvocation> + +// Qt Includes +#include <QRegExp> +#include <QFile> +#include <QFileInfo> + + +RekonqRun::RekonqRun(QWidget *parent = 0) + : QObject(parent) + , m_window(parent) +{ +} + + +RekonqRun::~RekonqRun() +{ +} + + +void RekonqRun::loadUrl(const KUrl& url, const Rekonq::BrowserArguments& browserArgs) +{ + if (url.isEmpty()) + return; + + QString scheme = url.scheme(); + + if (scheme == QLatin1String("mailto")) + { + KToolInvocation::invokeMailer(url); + return; + } + + KUrl loadingUrl(url); + + // create convenience fake api:// protocol for KDE apidox search and Qt docs + if (scheme == QLatin1String("api")) + { + QString path; + QString className = url.host().toLower(); + if (className[0] == 'k') + { + path = QString("http://api.kde.org/new.classmapper.php?class=%1").arg(className); + } + else if (className[0] == 'q') + { + path = QString("http://doc.trolltech.com/4.5/%1.html").arg(className); + } + loadingUrl.setUrl(path); + } + + if (loadingUrl.isRelative()) + { + if(loadingUrl.path().contains('.')) + { + QString fn = loadingUrl.url(KUrl::RemoveTrailingSlash); + loadingUrl.setUrl("//" + fn); + loadingUrl.setScheme("http"); + } + else + { + scheme = QLatin1String("gg"); + } + } + + // create convenience fake gg:// protocol, waiting for KServices learning + if(scheme == QLatin1String("gg")) + { + QString str = loadingUrl.path(); + loadingUrl.setUrl( QString("http://google.com/search?&q=%1").arg(str) ); + } + + // create convenience fake wk:// protocol, waiting for KServices learning + if(scheme == QLatin1String("wk")) + { + QString str = loadingUrl.path(); + loadingUrl.setUrl( QString("http://en.wikipedia.org/wiki/%1").arg(str) ); + } + + + WebView *webView = m_window->newTab(); + m_window->currentUrlBar()->setUrl(loadingUrl.prettyUrl()); + + switch(browserArgs) + { + case Rekonq::Default: + if (!Rekonq::openTabsBack()) + { + setCurrentWidget(webView); // this method does NOT take ownership of webView + } + break; + case Rekonq::New: + m_window->setCurrentWidget(webView); // this method does NOT take ownership of webView + break; + case Rekonq::Background: + break; + }; + + if (webView) + { + webView->setFocus(); + webView->load(loadingUrl); + } +} + + +void RekonqRun::loadUrl(const QString& urlString, const Rekonq::BrowserArguments& browserArgs) +{ + return loadUrl( guessUrlFromString(urlString), browserArgs ); +} + + +KUrl RekonqRun::guessUrlFromString(const QString &string) +{ + QString urlStr = string.trimmed(); + QRegExp test(QLatin1String("^[a-zA-Z]+\\:.*")); + + // Check if it looks like a qualified URL. Try parsing it and see. + bool hasSchema = test.exactMatch(urlStr); + + if (hasSchema) + { + QUrl qurl(urlStr, QUrl::TolerantMode); + KUrl url(qurl); + + if (url.isValid()) + { + return url; + } + } + + // Might be a file. + if (QFile::exists(urlStr)) + { + QFileInfo info(urlStr); + return KUrl::fromPath(info.absoluteFilePath()); + } + + // Might be a shorturl - try to detect the schema. + if (!hasSchema) + { + int dotIndex = urlStr.indexOf(QLatin1Char('.')); + + if (dotIndex != -1) + { + QString prefix = urlStr.left(dotIndex).toLower(); + QString schema = (prefix == QLatin1String("ftp")) ? prefix : QLatin1String("http"); + QUrl qurl(schema + QLatin1String("://") + urlStr, QUrl::TolerantMode); + KUrl url(qurl); + + if (url.isValid()) + { + return url; + } + } + } + + // Fall back to QUrl's own tolerant parser. + QUrl qurl = QUrl(string, QUrl::TolerantMode); + KUrl url(qurl); + + // finally for cases where the user just types in a hostname add http + if (qurl.scheme().isEmpty()) + { + qurl = QUrl(QLatin1String("http://") + string, QUrl::TolerantMode); + url = KUrl(qurl); + } + return url; +} diff --git a/src/rekonqrun.h b/src/rekonqrun.h new file mode 100644 index 00000000..ee96024b --- /dev/null +++ b/src/rekonqrun.h @@ -0,0 +1,72 @@ +/* ============================================================ +* +* This file is a part of the rekonq project +* +* Copyright (C) 2009 by Andrea Diamantini <adjam7 at gmail dot com> +* +* +* 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 3, 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. +* +* ============================================================ */ + +#ifndef REKONQRUN_H +#define REKONQRUN_H + +// KDE Includes +#include <KUrl> + +// Qt Includes +#include <QtCore/QString> +#include <QtGui/QWidget> + +namespace Rekonq +{ + /** + * @short opening tab behaviour + * Different open tab behaviour + */ + + enum BrowserArguments + { + Default, ///< follow rekonq settings + New, ///< open tab with focus + Background ///< open tab in the background + }; +} + + +class RekonqRun : public QObject +{ + Q_OBJECT + +public: + RekonqRun(QWidget *parent = 0); + ~RekonqRun(); + + +public slots: + + void loadUrl( const KUrl& url, + const Rekonq::BrowserArguments& browserArgs = Rekonq::Default + ); + + void loadUrl( const QString& urlString, + const Rekonq::BrowserArguments& browserArgs = Rekonq::Default + ); + +private: + + KUrl guessUrlFromString(const QString &url); + + QWidget *m_window; +}; + +#endif |