kontact

kaddressbook_plugin.cpp

00001 /*
00002     This file is part of Kontact.
00003 
00004     Copyright (c) 2001 Matthias Hoelzer-Kluepfel <mhk@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 <qwidget.h>
00026 #include <qdragobject.h>
00027 
00028 #include <kaction.h>
00029 #include <kapplication.h>
00030 #include <kdebug.h>
00031 #include <kgenericfactory.h>
00032 #include <kiconloader.h>
00033 #include <kmessagebox.h>
00034 #include <kparts/componentfactory.h>
00035 
00036 #include <kaddrbook.h>
00037 #include <kabc/addressbook.h>
00038 #include <kabc/stdaddressbook.h>
00039 
00040 #include <dcopclient.h>
00041 #include "kmailIface_stub.h"
00042 
00043 #include <libkdepim/maillistdrag.h>
00044 
00045 #include "core.h"
00046 #include "plugin.h"
00047 
00048 #include "kaddressbook_plugin.h"
00049 
00050 typedef KGenericFactory< KAddressbookPlugin, Kontact::Core > KAddressbookPluginFactory;
00051 K_EXPORT_COMPONENT_FACTORY( libkontact_kaddressbookplugin,
00052                             KAddressbookPluginFactory( "kontact_kaddressbookplugin" ) )
00053 
00054 KAddressbookPlugin::KAddressbookPlugin( Kontact::Core *core, const char *, const QStringList& )
00055   : Kontact::Plugin( core, core, "kaddressbook" ),
00056     mStub( 0 )
00057 {
00058   setInstance( KAddressbookPluginFactory::instance() );
00059 
00060   insertNewAction( new KAction( i18n( "New Contact..." ), "identity",
00061                          CTRL+SHIFT+Key_C, this, SLOT( slotNewContact() ), actionCollection(),
00062                    "new_contact" ) );
00063 
00064   insertNewAction( new KAction( i18n( "&New Distribution List..." ), "kontact_contacts", 0, this,
00065                                 SLOT( slotNewDistributionList() ), actionCollection(), "new_distributionlist" ) );
00066 
00067   insertSyncAction( new KAction( i18n( "Synchronize Contacts" ), "reload",
00068                     0, this, SLOT( slotSyncContacts() ), actionCollection(),
00069                    "kaddressbook_sync" ) );
00070   mUniqueAppWatcher = new Kontact::UniqueAppWatcher(
00071       new Kontact::UniqueAppHandlerFactory<KABUniqueAppHandler>(), this );
00072 }
00073 
00074 KAddressbookPlugin::~KAddressbookPlugin()
00075 {
00076 }
00077 
00078 KParts::ReadOnlyPart* KAddressbookPlugin::createPart()
00079 {
00080   KParts::ReadOnlyPart * part = loadPart();
00081   if ( !part ) return 0;
00082 
00083   // Create the stub that allows us to talk to the part
00084   mStub = new KAddressBookIface_stub( dcopClient(), "kaddressbook",
00085                                       "KAddressBookIface" );
00086   return part;
00087 }
00088 
00089 QStringList KAddressbookPlugin::configModules() const
00090 {
00091   QStringList modules;
00092   modules << "PIM/kabconfig.desktop" << "PIM/kabldapconfig.desktop";
00093   return modules;
00094 }
00095 
00096 QStringList KAddressbookPlugin::invisibleToolbarActions() const
00097 {
00098   return QStringList( "file_new_contact" );
00099 }
00100 
00101 KAddressBookIface_stub *KAddressbookPlugin::interface()
00102 {
00103   if ( !mStub ) {
00104     part();
00105   }
00106   Q_ASSERT( mStub );
00107   return mStub;
00108 }
00109 
00110 void KAddressbookPlugin::slotNewContact()
00111 {
00112   interface()->newContact();
00113 }
00114 
00115 
00116 void KAddressbookPlugin::slotNewDistributionList()
00117 {
00118   interface()->newDistributionList();
00119 }
00120 
00121 void KAddressbookPlugin::slotSyncContacts()
00122 {
00123   DCOPRef ref( "kmail", "KMailICalIface" );
00124   ref.send( "triggerSync", QString("Contact") );
00125 }
00126 
00127 bool KAddressbookPlugin::createDCOPInterface( const QString& serviceType )
00128 {
00129   if ( serviceType == "DCOP/AddressBook" )  {
00130     Q_ASSERT( mStub );
00131     return true;
00132   }
00133 
00134   return false;
00135 }
00136 
00137 void KAddressbookPlugin::configUpdated()
00138 {
00139 }
00140 
00141 bool KAddressbookPlugin::isRunningStandalone()
00142 {
00143   return mUniqueAppWatcher->isRunningStandalone();
00144 }
00145 
00146 bool KAddressbookPlugin::canDecodeDrag( QMimeSource *mimeSource )
00147 {
00148   return QTextDrag::canDecode( mimeSource ) ||
00149     KPIM::MailListDrag::canDecode( mimeSource );
00150 }
00151 
00152 #include <dcopref.h>
00153 
00154 void KAddressbookPlugin::processDropEvent( QDropEvent *event )
00155 {
00156   KPIM::MailList mails;
00157   if ( KPIM::MailListDrag::decode( event, mails ) ) {
00158     if ( mails.count() != 1 ) {
00159       KMessageBox::sorry( core(),
00160                           i18n( "Drops of multiple mails are not supported." ) );
00161     } else {
00162       KPIM::MailSummary mail = mails.first();
00163 
00164       KMailIface_stub kmailIface( "kmail", "KMailIface" );
00165       QString sFrom = kmailIface.getFrom( mail.serialNumber() );
00166 
00167       if ( !sFrom.isEmpty() ) {
00168         KAddrBookExternal::addEmail( sFrom, core() );
00169       }
00170     }
00171     return;
00172   }
00173 
00174   KMessageBox::sorry( core(), i18n( "Cannot handle drop events of type '%1'." )
00175                       .arg( event->format() ) );
00176 }
00177 
00178 
00179 void KAddressbookPlugin::loadProfile( const QString& directory )
00180 {
00181   DCOPRef ref( "kaddressbook", "KAddressBookIface" );
00182   ref.send( "loadProfile", directory );
00183 }
00184 
00185 void KAddressbookPlugin::saveToProfile( const QString& directory ) const
00186 {
00187   DCOPRef ref( "kaddressbook", "KAddressBookIface" );
00188   ref.send( "saveToProfile", directory );
00189 }
00190 
00192 
00193 #include "../../../kaddressbook/kaddressbook_options.h"
00194 
00195 void KABUniqueAppHandler::loadCommandLineOptions()
00196 {
00197     KCmdLineArgs::addCmdLineOptions( kaddressbook_options );
00198 }
00199 
00200 int KABUniqueAppHandler::newInstance()
00201 {
00202     kdDebug(5602) << k_funcinfo << endl;
00203     // Ensure part is loaded
00204     (void)plugin()->part();
00205     DCOPRef kAB( "kaddressbook", "KAddressBookIface" );
00206     DCOPReply reply = kAB.call( "handleCommandLine" );
00207     if ( reply.isValid() ) {
00208         bool handled = reply;
00209         kdDebug(5602) << k_funcinfo << "handled=" << handled << endl;
00210         if ( !handled ) // no args -> simply bring kaddressbook plugin to front
00211             return Kontact::UniqueAppHandler::newInstance();
00212     }
00213     return 0;
00214 }
00215 
00216 #include "kaddressbook_plugin.moc"
00217 
00218 // vim: sw=2 sts=2 tw=80 et
KDE Home | KDE Accessibility Home | Description of Access Keys