summaryrefslogtreecommitdiff
path: root/src/rekonqrun.cpp
blob: da1f4d934ea761c25761a23f90f74a80a9d222d5 (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
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
/* ============================================================
*
* 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::OpenType& type)
{
    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(type)
    {
    case Rekonq::Default:
        if (!ReKonfig::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::OpenType& type)
{    
    return loadUrl( guessUrlFromString(urlString), type );
}


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;
}