add more spacing
[personal-kdebase.git] / runtime / kurifilter-plugins / localdomain / localdomainurifilter.cpp
blob899bef257dafc2b170056ef8be250a6accfecd7c
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 const int pos = host.indexOf(':');
76 if (pos > -1) {
77 host.truncate(pos); // Remove port number
80 if( !(host == last_host && last_time > time( NULL ) - 5 ) ) {
82 QString helper = KStandardDirs::findExe(QLatin1String( "klocaldomainurifilterhelper" ));
83 if( helper.isEmpty())
84 return last_result = false;
86 KProcess proc;
87 proc.setOutputChannelMode(KProcess::SeparateChannels);
88 proc << helper << host;
89 proc.start();
90 if( !proc.waitForStarted( 1000 ) )
91 return last_result = false;
93 last_host = host;
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 );
106 return last_result;
109 void LocalDomainUriFilter::configure()
111 // nothing
114 K_PLUGIN_FACTORY(LocalDomainUriFilterFactory, registerPlugin<LocalDomainUriFilter>();)
115 K_EXPORT_PLUGIN(LocalDomainUriFilterFactory("kcmkurifilt"))
117 #include "localdomainurifilter.moc"