--- kuriikwsfiltereng.cpp.orig Thu Jun 7 21:44:13 2001 +++ kuriikwsfiltereng.cpp Sun Jun 10 16:37:27 2001 @@ -101,12 +101,12 @@ _url = _url.replace(question, ""); return formatResult(search, fallback->charset(), QString::null, _url, true); } else { - int pct = m_currInternetKeywordsEngine.m_strQueryWithSearch.find("\\|"); + int pct = m_currInternetKeywordsEngine.m_strQueryWithSearch.find("\\ikw_fallbackuri"); if (pct >= 0) { search = KURL::encode_string( search ); QString res = m_currInternetKeywordsEngine.m_strQueryWithSearch; - return formatResult( res.replace(pct, 2, search), fallback->charset(), QString::null, _url, url.isMalformed() ); + return formatResult( res.replace(pct, 16, search), fallback->charset(), QString::null, _url, url.isMalformed() ); } } } @@ -159,64 +159,79 @@ return s_pSelf; } -QString KURISearchFilterEngine::formatResult( const QString& query, +QString KURISearchFilterEngine::formatResult( const QString& url, const QString& cset1, const QString& cset2, - const QString& url, + const QString& query, bool isMalformed ) const { - // Substitute the variable part we find in the query. - if (!query.isEmpty()) - { - QString newurl = query; - int pct; + kdDebug(7023) << "(" << getpid() << ") query='" << query << "', url='" << url << "'\n"; + + // Return nothing if userquery is empty: + if (query.isEmpty()) return QString::null; + + typedef QMap IKWMap; + IKWMap map; + QString newurl = url; + int pct; + + // Decode strings for better handling (for ' ' and '='): + QString userquery = KURL::decode_string(query); + + kdDebug(7023) << "(" << getpid() << ") userquery='" << userquery << "', newurl='" << newurl << "'\n"; - // Create a codec for the desired encoding so that we can - // transcode the user's "url". - QString cseta = cset1; - if (cseta.isEmpty()) { + // Create a codec for the desired encoding so that we can transcode the user's "url". + QString cseta = cset1; + if (cseta.isEmpty()) { cseta = "iso-8859-1"; - } - QTextCodec *csetacodec = QTextCodec::codecForName(cseta.latin1()); - if (!csetacodec) { + } + QTextCodec *csetacodec = QTextCodec::codecForName(cseta.latin1()); + if (!csetacodec) { cseta = "iso-8859-1"; csetacodec = QTextCodec::codecForName(cseta.latin1()); - } - - // Substitute the charset indicator for the query. - if ((pct = newurl.find("\\2")) >= 0) { - newurl = newurl.replace(pct, 2, cseta); - } - - // Substitute the charset indicator for the fallback query. - if ((pct = newurl.find("\\3")) >= 0) { - QString csetb = cset2; - if (csetb.isEmpty()) { - csetb = "iso-8859-1"; - } - newurl = newurl.replace(pct, 2, csetb); - } + } - QString userquery = csetacodec->fromUnicode(url); - int space_pos; - while( (space_pos=userquery.find('+')) != -1 ) - userquery=userquery.replace( space_pos, 1, "%2B" ); - - while( (space_pos=userquery.find(' ')) != -1 ) - userquery=userquery.replace( space_pos, 1, "+" ); + // Add charset indicator for the query to substitution map: + map.replace("ikw_charset", cseta); - if ( isMalformed ) - userquery = KURL::encode_string(userquery); + // Add charset indicator for the fallback query to substitution map: + QString csetb = cset2; + if (csetb.isEmpty()) + csetb = "iso-8859-1"; + map.replace("ikw_responsecharset", csetb); + + userquery = csetacodec->fromUnicode(userquery); + int space_pos; + while ((space_pos=userquery.find('+')) != -1 ) + userquery=userquery.replace(space_pos, 1, "%2B"); + + // Add whole query with reference '\0' to substitution map: + map.replace("0", KURL::encode_string(userquery)); + + // Generate IKW substitution map from user query: + QStringList l = QStringList::split(" ", userquery.simplifyWhiteSpace()); + for (int i=0; i 0) + map.replace(v.left(pct), KURL::encode_string(v.right(pct + 2))); + } - if ((pct = newurl.find("\\1")) >= 0) - newurl = newurl.replace(pct, 2, userquery); + // Substitute references (\refname) from map with the values in the map: + for (IKWMap::Iterator it = map.begin(); it != map.end(); ++it) { + QString ref = "\\" + it.key(); + kdDebug (7023) << "(" << getpid() << ") key='" << it.key() << "' val='" << it.data() << "'\n"; + if ((pct = newurl.find(ref)) >= 0) + newurl = newurl.replace(pct, ref.length(), it.data()); + } - if ( m_bVerbose ) - kdDebug(7023) << "(" << getpid() << ") filtered " << url << " to " << newurl << "\n"; + kdDebug(7023) << "(" << getpid() << ") userquery='" << userquery << "', url='" << url << "', newurl='" << newurl << "'\n"; return newurl; - } - return QString::null; } void KURISearchFilterEngine::loadConfig() @@ -329,10 +344,10 @@ IKWSEntry rn = ikwsEntryByName(IKW_REALNAMES); if (rn.m_strName.isEmpty()) { rn.m_strName = IKW_REALNAMES; - rn.m_strQuery = QString::fromLatin1("http://navigation.realnames.com/resolver.dll?realname=\\1&charset=\\2&responsecharset=\\3&providerid=180"); + rn.m_strQuery = QString::fromLatin1("http://navigation.realnames.com/resolver.dll?realname=\\0&charset=\\ikw_charset&responsecharset=\\ikw_responsecharset&providerid=180"); rn.m_strCharset = "utf-8"; rn.m_strQueryWithSearch = QString::fromLatin1("http://navigation.realnames.com/resolver.dll?" - "action=navigation&realname=\\1&charset=\\2&providerid=180&fallbackuri=\\|"); + "action=navigation&realname=\\0&charset=\\ikw_charset&providerid=180&fallbackuri=\\ikw_fallbackuri"); if (rn.m_strName == selIKWSEngine) m_currInternetKeywordsEngine = rn;