kmail Library API Documentation

urlhandlermanager.cpp

00001 /*  -*- c++ -*-
00002     urlhandlermanager.cpp
00003 
00004     This file is part of KMail, the KDE mail client.
00005     Copyright (c) 2002-2003 Klarälvdalens Datakonsult AB
00006     Copyright (c) 2003      Marc Mutz <mutz@kde.org>
00007 
00008     KMail is free software; you can redistribute it and/or modify it
00009     under the terms of the GNU General Public License, version 2, as
00010     published by the Free Software Foundation.
00011 
00012     KMail is distributed in the hope that it will be useful, but
00013     WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015     General Public License for more details.
00016 
00017     You should have received a copy of the GNU General Public License
00018     along with this program; if not, write to the Free Software
00019     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 
00021     In addition, as a special exception, the copyright holders give
00022     permission to link the code of this program with any edition of
00023     the Qt library by Trolltech AS, Norway (or with modified versions
00024     of Qt that use the same license as Qt), and distribute linked
00025     combinations including the two.  You must obey the GNU General
00026     Public License in all respects for all of the code used other than
00027     Qt.  If you modify this file, you may extend this exception to
00028     your version of the file, but you are not obligated to do so.  If
00029     you do not wish to do so, delete this exception statement from
00030     your version.
00031 */
00032 
00033 #ifdef HAVE_CONFIG_H
00034 #include <config.h>
00035 #endif
00036 
00037 #include "urlhandlermanager.h"
00038 
00039 #include "interfaces/urlhandler.h"
00040 
00041 #include <kurl.h>
00042 
00043 #include <algorithm>
00044 using std::for_each;
00045 using std::remove;
00046 using std::find;
00047 
00048 KMail::URLHandlerManager * KMail::URLHandlerManager::self = 0;
00049 
00050 namespace {
00051   class ShowHtmlSwitchURLHandler : public KMail::URLHandler {
00052   public:
00053     ShowHtmlSwitchURLHandler() : KMail::URLHandler() {}
00054     ~ShowHtmlSwitchURLHandler() {}
00055 
00056     bool handleClick( const KURL &, KMReaderWin * ) const;
00057     bool handleContextMenuRequest( const KURL &, const QPoint &, KMReaderWin * ) const {
00058       return false;
00059     }
00060     QString statusBarMessage( const KURL &, KMReaderWin * ) const;
00061   };
00062 
00063   class SMimeURLHandler : public KMail::URLHandler {
00064   public:
00065     SMimeURLHandler() : KMail::URLHandler() {}
00066     ~SMimeURLHandler() {}
00067 
00068     bool handleClick( const KURL &, KMReaderWin * ) const;
00069     bool handleContextMenuRequest( const KURL &, const QPoint &, KMReaderWin * ) const {
00070       return false;
00071     }
00072     QString statusBarMessage( const KURL &, KMReaderWin * ) const;
00073   };
00074 
00075   class GroupwareURLHandler : public KMail::URLHandler {
00076   public:
00077     GroupwareURLHandler() : KMail::URLHandler() {}
00078     ~GroupwareURLHandler() {}
00079 
00080     bool handleClick( const KURL &, KMReaderWin * ) const;
00081     bool handleContextMenuRequest( const KURL &, const QPoint &, KMReaderWin * ) const {
00082       return false;
00083     }
00084     QString statusBarMessage( const KURL &, KMReaderWin * ) const;
00085   };
00086 
00087   class MailToURLHandler : public KMail::URLHandler {
00088   public:
00089     MailToURLHandler() : KMail::URLHandler() {}
00090     ~MailToURLHandler() {}
00091 
00092     bool handleClick( const KURL &, KMReaderWin * ) const { return false; }
00093     bool handleContextMenuRequest( const KURL &, const QPoint &, KMReaderWin * ) const {
00094       return false;
00095     }
00096     QString statusBarMessage( const KURL &, KMReaderWin * ) const;
00097   };
00098 
00099   class HtmlAnchorHandler : public KMail::URLHandler {
00100   public:
00101     HtmlAnchorHandler() : KMail::URLHandler() {}
00102     ~HtmlAnchorHandler() {}
00103 
00104     bool handleClick( const KURL &, KMReaderWin * ) const;
00105     bool handleContextMenuRequest( const KURL &, const QPoint &, KMReaderWin * ) const {
00106       return false;
00107     }
00108     QString statusBarMessage( const KURL &, KMReaderWin * ) const { return QString::null; }
00109   };
00110 
00111   class AttachmentURLHandler : public KMail::URLHandler {
00112   public:
00113     AttachmentURLHandler() : KMail::URLHandler() {}
00114     ~AttachmentURLHandler() {}
00115 
00116     bool handleClick( const KURL &, KMReaderWin * ) const;
00117     bool handleContextMenuRequest( const KURL &, const QPoint &, KMReaderWin * ) const;
00118     QString statusBarMessage( const KURL &, KMReaderWin * ) const;
00119   };
00120 
00121   class FallBackURLHandler : public KMail::URLHandler {
00122   public:
00123     FallBackURLHandler() : KMail::URLHandler() {}
00124     ~FallBackURLHandler() {}
00125 
00126     bool handleClick( const KURL &, KMReaderWin * ) const;
00127     bool handleContextMenuRequest( const KURL &, const QPoint &, KMReaderWin * ) const;
00128     QString statusBarMessage( const KURL & url, KMReaderWin * ) const {
00129       return url.prettyURL();
00130     }
00131   };
00132 
00133 
00134 }
00135 
00136 KMail::URLHandlerManager::URLHandlerManager() {
00137   registerHandler( new ShowHtmlSwitchURLHandler() );
00138   registerHandler( new SMimeURLHandler() );
00139   registerHandler( new GroupwareURLHandler() );
00140   registerHandler( new MailToURLHandler() );
00141   registerHandler( new HtmlAnchorHandler() );
00142   registerHandler( new AttachmentURLHandler() );
00143   registerHandler( new FallBackURLHandler() );
00144 }
00145 
00146 namespace {
00147   template <typename T> struct Delete {
00148     void operator()( const T * x ) { delete x; x = 0; }
00149   };
00150 }
00151 
00152 KMail::URLHandlerManager::~URLHandlerManager() {
00153   for_each( mHandlers.begin(), mHandlers.end(), Delete<URLHandler>() );
00154 }
00155 
00156 void KMail::URLHandlerManager::registerHandler( const URLHandler * handler ) {
00157   if ( !handler )
00158     return;
00159   unregisterHandler( handler ); // don't produce duplicates
00160   mHandlers.push_back( handler );
00161 }
00162 
00163 void KMail::URLHandlerManager::unregisterHandler( const URLHandler * handler ) {
00164   // don't delete them, only remove them from the list!
00165   mHandlers.erase( remove( mHandlers.begin(), mHandlers.end(), handler ), mHandlers.end() );
00166 }
00167 
00168 bool KMail::URLHandlerManager::handleClick( const KURL & url, KMReaderWin * w ) const {
00169   for ( const_iterator it = mHandlers.begin() ; it != mHandlers.end() ; ++it )
00170     if ( (*it)->handleClick( url, w ) )
00171       return true;
00172   return false;
00173 }
00174 
00175 bool KMail::URLHandlerManager::handleContextMenuRequest( const KURL & url, const QPoint & p, KMReaderWin * w ) const {
00176   for ( const_iterator it = mHandlers.begin() ; it != mHandlers.end() ; ++it )
00177     if ( (*it)->handleContextMenuRequest( url, p, w ) )
00178       return true;
00179   return false;
00180 }
00181 
00182 QString KMail::URLHandlerManager::statusBarMessage( const KURL & url, KMReaderWin * w ) const {
00183   for ( const_iterator it = mHandlers.begin() ; it != mHandlers.end() ; ++it ) {
00184     const QString msg = (*it)->statusBarMessage( url, w );
00185     if ( !msg.isEmpty() )
00186       return msg;
00187   }
00188   return QString::null;
00189 }
00190 
00191 // these includes are temporary and should not be needed for the code
00192 // above this line, so they appear only here:
00193 #include "kmgroupware.h"
00194 #include "kmmessage.h"
00195 #include "kmkernel.h"
00196 #include "kmreaderwin.h"
00197 #include "partNode.h"
00198 #include "kmmsgpart.h"
00199 
00200 #include <klocale.h>
00201 #include <kprocess.h>
00202 #include <kmessagebox.h>
00203 #include <khtml_part.h>
00204 
00205 #include <qstring.h>
00206 
00207 namespace {
00208   bool ShowHtmlSwitchURLHandler::handleClick( const KURL & url, KMReaderWin * w ) const {
00209     if ( url.protocol() != "kmail" || url.path() != "showHTML" )
00210       return false;
00211     if ( w ) {
00212       w->setHtmlOverride( !w->htmlOverride() );
00213       w->update( true );
00214     }
00215     return true;
00216   }
00217 
00218   QString ShowHtmlSwitchURLHandler::statusBarMessage( const KURL & url, KMReaderWin * ) const {
00219     return url.url() == "kmail:showHTML"
00220       ? i18n("Turn on HTML rendering for this message.")
00221       : QString::null ;
00222   }
00223 }
00224 
00225 // defined in kmreaderwin.cpp...
00226 extern bool foundSMIMEData( const QString aUrl, QString & displayName,
00227                 QString & libName, QString & keyId );
00228 
00229 namespace {
00230   bool SMimeURLHandler::handleClick( const KURL & url, KMReaderWin * w ) const {
00231     if ( !url.hasRef() )
00232       return false;
00233     QString displayName, libName, keyId;
00234     if ( !foundSMIMEData( url.path() + '#' + url.ref(), displayName, libName, keyId ) )
00235       return false;
00236     KProcess cmp;
00237     cmp << "kgpgcertmanager" << displayName << libName << "-query" << keyId;
00238     if ( !cmp.start( KProcess::DontCare ) )
00239       KMessageBox::error( w, i18n("Could not start certificate manager. "
00240                   "Please check your installation."),
00241               i18n("KMail Error") );
00242     return true;
00243   }
00244 
00245   QString SMimeURLHandler::statusBarMessage( const KURL & url, KMReaderWin * ) const {
00246     QString displayName, libName, keyId;
00247     if ( !foundSMIMEData( url.path() + '#' + url.ref(), displayName, libName, keyId ) )
00248       return QString::null;
00249     return i18n("Show certificate 0x%1").arg( keyId );
00250   }
00251 }
00252 
00253 namespace {
00254   bool GroupwareURLHandler::handleClick( const KURL & url, KMReaderWin * w ) const {
00255     if ( !kmkernel->groupware().isEnabled() )
00256       return false;
00257     return !w || kmkernel->groupware().handleLink( url, w->message() );
00258   }
00259 
00260   QString GroupwareURLHandler::statusBarMessage( const KURL & url, KMReaderWin * ) const {
00261     QString type, action, action2, dummy;
00262     if ( !KMGroupware::foundGroupwareLink( url.url(), type, action, action2, dummy ) )
00263       return QString::null;
00264     QString result = type + ' ' + action;
00265     if ( !action2.isEmpty() )
00266       result += ' ' + action2;
00267     return i18n("Groupware: \"%1\"").arg( result );
00268   }
00269 }
00270 
00271 namespace {
00272   bool HtmlAnchorHandler::handleClick( const KURL & url, KMReaderWin * w ) const {
00273     if ( url.hasHost() || url.path() != "/" || !url.hasRef() )
00274       return false;
00275     if ( w && !w->htmlPart()->gotoAnchor( url.ref() ) )
00276       static_cast<QScrollView*>( w->htmlPart()->widget() )->ensureVisible( 0, 0 );
00277     return true;
00278   }
00279 }
00280 
00281 namespace {
00282   QString MailToURLHandler::statusBarMessage( const KURL & url, KMReaderWin * ) const {
00283     if ( url.protocol() != "mailto" )
00284       return QString::null;
00285     return KMMessage::decodeMailtoUrl( url.url() );
00286   }
00287 }
00288 
00289 namespace {
00290   bool AttachmentURLHandler::handleClick( const KURL & url, KMReaderWin * w ) const {
00291     if ( !w || !w->message() )
00292       return false;
00293     const int id = KMReaderWin::msgPartFromUrl( url );
00294     if ( id <= 0 )
00295       return false;
00296     w->openAttachment( id, url.path() );
00297     return true;
00298   }
00299 
00300   bool AttachmentURLHandler::handleContextMenuRequest( const KURL & url, const QPoint & p, KMReaderWin * w ) const {
00301     if ( !w || !w->message() )
00302       return false;
00303     const int id = KMReaderWin::msgPartFromUrl( url );
00304     if ( id <= 0 )
00305       return false;
00306     w->showAttachmentPopup( id, url.path(), p );
00307     return true;
00308   }
00309 
00310   QString AttachmentURLHandler::statusBarMessage( const KURL & url, KMReaderWin * w ) const {
00311     if ( !w || !w->message() )
00312       return QString::null;
00313     const partNode * node = w->partNodeFromUrl( url );
00314     if ( !node )
00315       return QString::null;
00316     const KMMessagePart & msgPart = node->msgPart();
00317     QString name = msgPart.fileName();
00318     if ( name.isEmpty() )
00319       name = msgPart.name();
00320     if ( !name.isEmpty() )
00321       return i18n( "Attachment: %1" ).arg( name );
00322     return i18n( "Attachment #%1 (unnamed)" ).arg( KMReaderWin::msgPartFromUrl( url ) );
00323   }
00324 }
00325 
00326 namespace {
00327   bool FallBackURLHandler::handleClick( const KURL & url, KMReaderWin * w ) const {
00328     if ( w )
00329       w->emitUrlClicked( url, Qt::LeftButton );
00330     return true;
00331   }
00332 
00333   bool FallBackURLHandler::handleContextMenuRequest( const KURL & url, const QPoint & p, KMReaderWin * w ) const {
00334     if ( w )
00335       w->emitPopupMenu( url, p );
00336     return true;
00337   }
00338 }
KDE Logo
This file is part of the documentation for kmail Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat May 1 11:37:35 2004 by doxygen 1.2.15 written by Dimitri van Heesch, © 1997-2003