Make a branch to make krunner Good Enough For Aaron™.
[kdebase/uwolfer.git] / runtime / kurifilter-plugins / localdomain / localdomainurifilter.cpp
blob09c83fb8ac511727fa6d6ac4c196e8e7f0fa9daf
1 /*
2 localdomainfilter.cpp
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"
23 #include <KProcess>
24 #include <kstandarddirs.h>
25 #include <kdebug.h>
27 #include <QtDBus/QtDBus>
29 #include <QRegExp>
30 #include <QFile>
32 #define HOSTPORT_PATTERN "[a-zA-Z0-9][a-zA-Z0-9+-]*(?:\\:[0-9]{1,5})?(?:/[\\w:@&=+$,-.!~*'()]*)*"
34 /**
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 ),
41 last_time( 0 ),
42 m_hostPortPattern( QLatin1String(HOSTPORT_PATTERN) )
44 QDBusConnection::sessionBus().connect(QString(), QString(), "org.kde.KUriFilterPlugin",
45 "configure", this, SLOT(configure()));
46 configure();
49 bool LocalDomainUriFilter::filterUri( KUriFilterData& data ) const
51 const KUrl url = data.uri();
52 QString cmd = url.url();
54 kDebug() << 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();
64 return true;
67 return false;
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 host.truncate( host.indexOf( ':' ) ); // Remove port number
77 if( !(host == last_host && last_time > time( NULL ) - 5 ) ) {
79 QString helper = KStandardDirs::findExe(QLatin1String( "klocaldomainurifilterhelper" ));
80 if( helper.isEmpty())
81 return last_result = false;
83 KProcess proc;
84 proc << helper << host;
85 proc.start();
86 if( !proc.waitForStarted( 1000 ) )
87 return last_result = false;
89 last_host = host;
90 last_time = time( (time_t *)0 );
92 last_result = proc.waitForFinished( 1000 ) && proc.exitCode() == QProcess::NormalExit;
94 const QString fullname = QFile::decodeName( proc.readAllStandardOutput() );
96 if( !fullname.isEmpty() )
97 cmd.replace( 0, host.length(), fullname );
100 return last_result;
103 void LocalDomainUriFilter::configure()
105 // nothing
108 K_PLUGIN_FACTORY(LocalDomainUriFilterFactory, registerPlugin<LocalDomainUriFilter>();)
109 K_EXPORT_PLUGIN(LocalDomainUriFilterFactory("kcmkurifilt"))
111 #include "localdomainurifilter.moc"