korganizer

urihandler.cpp

00001 /*
00002     This file is part of KOrganizer.
00003 
00004     Copyright (c) 2003 Cornelius Schumacher <schumacher@kde.org>
00005 
00006     This program is free software; you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or
00009     (at your option) any later version.
00010 
00011     This program is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00014     GNU General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this program; if not, write to the Free Software
00018     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019 
00020     As a special exception, permission is given to link this program
00021     with any edition of Qt, and distribute the resulting executable,
00022     without including the source code for Qt in the source distribution.
00023 */
00024 
00025 #include "urihandler.h"
00026 
00027 #include <libkcal/attachment.h>
00028 #include <libkcal/calendarresources.h>
00029 #include <libkcal/incidence.h>
00030 using namespace KCal;
00031 
00032 #ifndef KORG_NODCOP
00033 #include <dcopclient.h>
00034 #include "kmailIface_stub.h"
00035 #endif
00036 
00037 #include <kiconloader.h>
00038 #include <krun.h>
00039 #include <kapplication.h>
00040 #include <klocale.h>
00041 #include <kmessagebox.h>
00042 #include <kmimetype.h>
00043 #include <kprocess.h>
00044 #include <ktempfile.h>
00045 #include <kdebug.h>
00046 #include <kio/netaccess.h>
00047 
00048 #include <qfile.h>
00049 #include <qregexp.h>
00050 
00051 
00052 static Attachment *findAttachment( const QString &name, const QString &uid )
00053 {
00054   CalendarResources *cal = new CalendarResources( "UTC" );
00055   cal->readConfig();
00056   cal->load();
00057   Incidence *incidence = cal->incidence( uid );
00058   if ( !incidence ) {
00059     KMessageBox::sorry(
00060       0,
00061       i18n( "The incidence that owns the attachment named \"%1\" could not be found. "
00062             "Perhaps it was removed from your calendar?" ).arg( name ) );
00063     return 0;
00064   }
00065 
00066   // get the attachment by name from the incidence
00067   Attachment::List as = incidence->attachments();
00068   Attachment *a = 0;
00069   if ( as.count() > 0 ) {
00070     Attachment::List::ConstIterator it;
00071     for ( it = as.begin(); it != as.end(); ++it ) {
00072       if ( (*it)->label() == name ) {
00073         a = *it;
00074         break;
00075       }
00076     }
00077   }
00078 
00079   if ( !a ) {
00080     KMessageBox::error(
00081       0,
00082       i18n( "No attachment named \"%1\" found in the incidence." ).arg( name ) );
00083     return 0;
00084   }
00085 
00086   if ( a->isUri() ) {
00087     if ( !KIO::NetAccess::exists( a->uri(), true, 0 ) ) {
00088       KMessageBox::sorry(
00089         0,
00090         i18n( "The attachment \"%1\" is a web link that is inaccessible from this computer. " ).
00091         arg( KURL::decode_string( a->uri() ) ) );
00092       return 0;
00093     }
00094   }
00095   return a;
00096 }
00097 
00098 static bool openAttachment( const QString &name, const QString &uid )
00099 {
00100   Attachment *a = findAttachment( name, uid );
00101   if ( !a ) {
00102     return false;
00103   }
00104 
00105   if ( a->isUri() ) {
00106     kapp->invokeBrowser( a->uri() );
00107   } else {
00108     // put the attachment in a temporary file and launch it
00109     KTempFile *file;
00110     QStringList patterns = KMimeType::mimeType( a->mimeType() )->patterns();
00111     if ( !patterns.empty() ) {
00112       file = new KTempFile( QString::null,
00113                             QString( patterns.first() ).remove( '*' ),0600 );
00114     } else {
00115       file = new KTempFile( QString::null, QString::null, 0600 );
00116     }
00117     file->file()->open( IO_WriteOnly );
00118     QTextStream stream( file->file() );
00119     stream.writeRawBytes( a->decodedData().data(), a->size() );
00120     file->close();
00121 
00122     bool stat = KRun::runURL( KURL( file->name() ), a->mimeType(), 0, true );
00123     delete file;
00124     return stat;
00125   }
00126   return true;
00127 }
00128 
00129 bool UriHandler::process( const QString &uri )
00130 {
00131   kdDebug(5850) << "UriHandler::process(): " << uri << endl;
00132 
00133 #ifndef KORG_NODCOP
00134   if ( uri.startsWith( "kmail:" ) ) {
00135 
00136     // make sure kmail is running or the part is shown
00137     kapp->startServiceByDesktopPath("kmail");
00138 
00139     // parse string, show
00140     int colon = uri.find( ':' );
00141     // extract 'number' from 'kmail:<number>/<id>'
00142     QString serialNumberStr = uri.mid( colon + 1 );
00143     serialNumberStr = serialNumberStr.left( serialNumberStr.find( '/' ) );
00144 
00145     KMailIface_stub kmailIface( "kmail", "KMailIface" );
00146     kmailIface.showMail( serialNumberStr.toUInt(), QString() );
00147     return true;
00148 
00149   } else if ( uri.startsWith( "mailto:" ) ) {
00150 
00151     KApplication::kApplication()->invokeMailer( uri.mid(7), QString::null );
00152     return true;
00153 
00154   } else if ( uri.startsWith( "uid:" ) ) {
00155 
00156     DCOPClient *client = KApplication::kApplication()->dcopClient();
00157     const QByteArray noParamData;
00158     const QByteArray paramData;
00159     QByteArray replyData;
00160     QCString replyTypeStr;
00161     bool foundAbbrowser = client->call( "kaddressbook", "KAddressBookIface",
00162                                         "interfaces()",  noParamData,
00163                                         replyTypeStr, replyData );
00164     if ( foundAbbrowser ) {
00165       // KAddressbook is already running, so just DCOP to it to bring up the contact editor
00166 #if KDE_IS_VERSION( 3, 2, 90 )
00167       kapp->updateRemoteUserTimestamp("kaddressbook");
00168 #endif
00169       DCOPRef kaddressbook( "kaddressbook", "KAddressBookIface" );
00170       kaddressbook.send( "showContactEditor", uri.mid( 6 ) );
00171       return true;
00172     } else {
00173       // KaddressBook is not already running.
00174       // Pass it the UID of the contact via the command line while starting it - its neater.
00175       // We start it without its main interface
00176       QString iconPath = KGlobal::iconLoader()->iconPath( "go", KIcon::Small );
00177       QString tmpStr = "kaddressbook --editor-only --uid ";
00178       tmpStr += KProcess::quote( uri.mid( 6 ) );
00179       KRun::runCommand( tmpStr, "KAddressBook", iconPath );
00180       return true;
00181     }
00182 
00183   } else if ( uri.startsWith( "ATTACH:" ) ) {
00184 
00185     // a calendar incidence attachment
00186     QString tmp = uri;
00187     tmp.remove( QRegExp( "^ATTACH://" ) );
00188     QString uid = tmp.section( ':', 0, 0 );
00189     QString name = tmp.section( ':', -1, -1 );
00190     return openAttachment( name, uid );
00191 
00192   } else {  // no special URI, let KDE handle it
00193     new KRun( KURL( uri ) );
00194   }
00195 #endif
00196 
00197   return false;
00198 }
KDE Home | KDE Accessibility Home | Description of Access Keys