kontact

kmail_plugin.cpp

00001 /*
00002     This file is part of Kontact.
00003     Copyright (c) 2003 Kontact Developer
00004 
00005     This program is free software; you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published by
00007     the Free Software Foundation; either version 2 of the License, or
00008     (at your option) any later version.
00009 
00010     This program is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with this program; if not, write to the Free Software
00017     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00018 
00019     As a special exception, permission is given to link this program
00020     with any edition of Qt, and distribute the resulting executable,
00021     without including the source code for Qt in the source distribution.
00022 */
00023 
00024 #include <qwidget.h>
00025 
00026 #include <kaction.h>
00027 #include <kapplication.h>
00028 #include <kdebug.h>
00029 #include <kgenericfactory.h>
00030 #include <kiconloader.h>
00031 #include <kparts/componentfactory.h>
00032 #include <kstandarddirs.h>
00033 #include <dcopclient.h>
00034 #include <ktempfile.h>
00035 
00036 #include <kabc/addressee.h>
00037 
00038 #include <libkcal/vcaldrag.h>
00039 #include <libkcal/icaldrag.h>
00040 #include <libkcal/calendarlocal.h>
00041 
00042 #include <libkdepim/kvcarddrag.h>
00043 
00044 #include <kmail/kmail_part.h>
00045 #include <kmail/kmkernel.h>
00046 
00047 #include "core.h"
00048 #include "summarywidget.h"
00049 
00050 #include "kmail_plugin.h"
00051 
00052 using namespace KCal;
00053 
00054 typedef KGenericFactory<KMailPlugin, Kontact::Core> KMailPluginFactory;
00055 K_EXPORT_COMPONENT_FACTORY( libkontact_kmailplugin,
00056                             KMailPluginFactory( "kontact_kmailplugin" ) )
00057 
00058 KMailPlugin::KMailPlugin(Kontact::Core *core, const char *, const QStringList& )
00059   : Kontact::Plugin( core, core, "kmail" ),
00060     mStub( 0 )
00061 {
00062   setInstance( KMailPluginFactory::instance() );
00063 
00064   insertNewAction( new KAction( i18n( "New Message..." ), "mail_new",
00065                    CTRL+SHIFT+Key_M, this, SLOT( slotNewMail() ), actionCollection(),
00066                    "new_mail" ) );
00067 
00068   mUniqueAppWatcher = new Kontact::UniqueAppWatcher(
00069       new Kontact::UniqueAppHandlerFactory<KMailUniqueAppHandler>(), this );
00070 }
00071 
00072 bool KMailPlugin::canDecodeDrag( QMimeSource *qms )
00073 {
00074   return ( ICalDrag::canDecode( qms ) ||
00075            VCalDrag::canDecode( qms ) ||
00076            KVCardDrag::canDecode( qms ) );
00077 }
00078 
00079 void KMailPlugin::processDropEvent( QDropEvent * de )
00080 {
00081   kdDebug() << k_funcinfo << endl;
00082   CalendarLocal cal( QString::fromLatin1("UTC") );
00083   KABC::Addressee::List list;
00084 
00085   if ( VCalDrag::decode( de, &cal ) || ICalDrag::decode( de, &cal ) ) {
00086     KTempFile tmp( locateLocal( "tmp", "incidences-" ), ".ics" );
00087     cal.save( tmp.name() );
00088     openComposer( KURL::fromPathOrURL( tmp.name() ) );
00089   }
00090   else if ( KVCardDrag::decode( de, list ) ) {
00091     KABC::Addressee::List::Iterator it;
00092     QStringList to;
00093     for ( it = list.begin(); it != list.end(); ++it ) {
00094       to.append( ( *it ).fullEmail() );
00095     }
00096     openComposer( to.join(", ") );
00097   }
00098 
00099 }
00100 
00101 void KMailPlugin::openComposer( const KURL& attach )
00102 {
00103   (void) part(); // ensure part is loaded
00104   Q_ASSERT( mStub );
00105   if ( mStub ) {
00106     if ( attach.isValid() )
00107       mStub->newMessage( "", "", "", false, true, KURL(), attach );
00108     else
00109       mStub->newMessage( "", "", "", false, true, KURL(), KURL() );
00110   }
00111 }
00112 
00113 void KMailPlugin::openComposer( const QString& to )
00114 {
00115   (void) part(); // ensure part is loaded
00116   Q_ASSERT( mStub );
00117   if ( mStub ) {
00118     mStub->newMessage( to, "", "", false, true, KURL(), KURL() );
00119   }
00120 }
00121 
00122 void KMailPlugin::slotNewMail()
00123 {
00124   openComposer( QString::null );
00125 }
00126 
00127 KMailPlugin::~KMailPlugin()
00128 {
00129 }
00130 
00131 bool KMailPlugin::createDCOPInterface( const QString& serviceType )
00132 {
00133   if ( serviceType == "DCOP/ResourceBackend/IMAP" ) {
00134     if ( part() )
00135       return true;
00136   }
00137 
00138   return false;
00139 }
00140 
00141 QString KMailPlugin::tipFile() const
00142 {
00143   QString file = ::locate("data", "kmail/tips");
00144   return file;
00145 }
00146 
00147 KParts::ReadOnlyPart* KMailPlugin::createPart()
00148 {
00149   KParts::ReadOnlyPart *part = loadPart();
00150   if ( !part ) return 0;
00151 
00152   mStub = new KMailIface_stub( dcopClient(), "kmail", "KMailIface" );
00153 
00154   return part;
00155 }
00156 
00157 QStringList KMailPlugin::invisibleToolbarActions() const
00158 {
00159   return QStringList( "new_message" );
00160 }
00161 
00162 bool KMailPlugin::isRunningStandalone()
00163 {
00164   return mUniqueAppWatcher->isRunningStandalone();
00165 }
00166 
00167 Kontact::Summary *KMailPlugin::createSummaryWidget( QWidget *parent )
00168 {
00169   return new SummaryWidget( this, parent );
00170 }
00171 
00173 
00174 #include "../../../kmail/kmail_options.h"
00175 void KMailUniqueAppHandler::loadCommandLineOptions()
00176 {
00177     KCmdLineArgs::addCmdLineOptions( kmail_options );
00178 }
00179 
00180 int KMailUniqueAppHandler::newInstance()
00181 {
00182     // Ensure part is loaded
00183     (void)plugin()->part();
00184     DCOPRef kmail( "kmail", "KMailIface" );
00185     DCOPReply reply = kmail.call( "handleCommandLine", false );
00186     if ( reply.isValid() ) {
00187         bool handled = reply;
00188         //kdDebug(5602) << k_funcinfo << "handled=" << handled << endl;
00189         if ( !handled ) // no args -> simply bring kmail plugin to front
00190             return Kontact::UniqueAppHandler::newInstance();
00191     }
00192     return 0;
00193 }
00194 
00195 bool KMailPlugin::queryClose() const {
00196   KMailIface_stub stub( kapp->dcopClient(), "kmail", "KMailIface" );
00197   bool canClose=stub.canQueryClose();
00198   return canClose;
00199 }
00200 
00201 #include "kmail_plugin.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys