4 This file is part of the KDE project
5 Copyright (C) 2002 Lubos Lunak <llunak@suse.cz>
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "localdomainurifilter.h"
24 #include <kstandarddirs.h>
27 #include <QtDBus/QtDBus>
32 #define HOSTPORT_PATTERN "[a-zA-Z0-9][a-zA-Z0-9+-]*(?:\\:[0-9]{1,5})?(?:/[\\w:@&=+$,-.!~*'()]*)*"
35 * IMPORTANT: If you change anything here, please run the regression test
36 * ../tests/kurifiltertest
39 LocalDomainUriFilter::LocalDomainUriFilter( QObject
*parent
, const QVariantList
& /*args*/ )
40 : KUriFilterPlugin( "localdomainurifilter", parent
),
42 m_hostPortPattern( QLatin1String(HOSTPORT_PATTERN
) )
44 QDBusConnection::sessionBus().connect(QString(), QString(), "org.kde.KUriFilterPlugin",
45 "configure", this, SLOT(configure()));
49 bool LocalDomainUriFilter::filterUri( KUriFilterData
& data
) const
51 const KUrl url
= data
.uri();
52 QString cmd
= url
.url();
56 if( m_hostPortPattern
.exactMatch( cmd
) &&
57 isLocalDomainHost( cmd
) )
59 cmd
.prepend( QLatin1String("http://") );
60 setFilteredUri( data
, KUrl( cmd
) );
61 setUriType( data
, KUriFilterData::NetProtocol
);
63 kDebug() << "FilteredUri: " << data
.uri();
70 // if it's e.g. just 'www', try if it's a hostname in the local search domain
71 bool LocalDomainUriFilter::isLocalDomainHost( QString
& cmd
) const
73 // find() returns -1 when no match -> left()/truncate() are noops then
74 QString
host( cmd
.left( cmd
.indexOf( '/' ) ) );
75 const int pos
= host
.indexOf(':');
77 host
.truncate(pos
); // Remove port number
80 if( !(host
== last_host
&& last_time
> time( NULL
) - 5 ) ) {
82 QString helper
= KStandardDirs::findExe(QLatin1String( "klocaldomainurifilterhelper" ));
84 return last_result
= false;
87 proc
.setOutputChannelMode(KProcess::SeparateChannels
);
88 proc
<< helper
<< host
;
90 if( !proc
.waitForStarted( 1000 ) )
91 return last_result
= false;
94 last_time
= time( (time_t *)0 );
96 last_result
= proc
.waitForFinished( 1000 ) && proc
.exitCode() == QProcess::NormalExit
;
98 const QString fullname
= QFile::decodeName( proc
.readAllStandardOutput() );
100 if( !fullname
.isEmpty() ) {
101 //kDebug() << "success, replacing" << host << "with" << fullname;
102 cmd
.replace( 0, host
.length(), fullname
);
109 void LocalDomainUriFilter::configure()
114 K_PLUGIN_FACTORY(LocalDomainUriFilterFactory
, registerPlugin
<LocalDomainUriFilter
>();)
115 K_EXPORT_PLUGIN(LocalDomainUriFilterFactory("kcmkurifilt"))
117 #include "localdomainurifilter.moc"