kontact

korganizerplugin.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 <qcursor.h>
00026 #include <qfile.h>
00027 #include <qwidget.h>
00028 #include <qdragobject.h>
00029 
00030 #include <kapplication.h>
00031 #include <kabc/vcardconverter.h>
00032 #include <kaction.h>
00033 #include <dcopref.h>
00034 #include <kdebug.h>
00035 #include <kgenericfactory.h>
00036 #include <kiconloader.h>
00037 #include <kmessagebox.h>
00038 #include <kstandarddirs.h>
00039 #include <ktempfile.h>
00040 
00041 #include <dcopclient.h>
00042 
00043 #include <libkdepim/kvcarddrag.h>
00044 #include <libkdepim/maillistdrag.h>
00045 #include <libkdepim/kpimprefs.h>
00046 
00047 #include <libkcal/calendarlocal.h>
00048 #include <libkcal/icaldrag.h>
00049 
00050 #include "core.h"
00051 #include "summarywidget.h"
00052 #include "korganizerplugin.h"
00053 #include "korg_uniqueapp.h"
00054 
00055 typedef KGenericFactory< KOrganizerPlugin, Kontact::Core > KOrganizerPluginFactory;
00056 K_EXPORT_COMPONENT_FACTORY( libkontact_korganizerplugin,
00057                             KOrganizerPluginFactory( "kontact_korganizerplugin" ) )
00058 
00059 KOrganizerPlugin::KOrganizerPlugin( Kontact::Core *core, const char *, const QStringList& )
00060   : Kontact::Plugin( core, core, "korganizer" ),
00061     mIface( 0 )
00062 {
00063 
00064   setInstance( KOrganizerPluginFactory::instance() );
00065   instance()->iconLoader()->addAppDir("kdepim");
00066 
00067   insertNewAction( new KAction( i18n( "New Event..." ), "newappointment",
00068                    CTRL+SHIFT+Key_E, this, SLOT( slotNewEvent() ), actionCollection(),
00069                    "new_event" ) );
00070 
00071   insertSyncAction( new KAction( i18n( "Synchronize Calendar" ), "reload",
00072                    0, this, SLOT( slotSyncEvents() ), actionCollection(),
00073                    "korganizer_sync" ) );
00074 
00075   mUniqueAppWatcher = new Kontact::UniqueAppWatcher(
00076       new Kontact::UniqueAppHandlerFactory<KOrganizerUniqueAppHandler>(), this );
00077 }
00078 
00079 KOrganizerPlugin::~KOrganizerPlugin()
00080 {
00081 }
00082 
00083 Kontact::Summary *KOrganizerPlugin::createSummaryWidget( QWidget *parent )
00084 {
00085   // korg part must be loaded, otherwise when starting kontact on summary view
00086   // it won't display our stuff.
00087   // If the part is already loaded loadPart() is harmless and just returns
00088   loadPart();
00089 
00090   return new SummaryWidget( this, parent );
00091 }
00092 
00093 KParts::ReadOnlyPart *KOrganizerPlugin::createPart()
00094 {
00095   KParts::ReadOnlyPart *part = loadPart();
00096 
00097   if ( !part )
00098     return 0;
00099 
00100   mIface = new KCalendarIface_stub( dcopClient(), "kontact", "CalendarIface" );
00101 
00102   return part;
00103 }
00104 
00105 QString KOrganizerPlugin::tipFile() const
00106 {
00107   QString file = ::locate("data", "korganizer/tips");
00108   return file;
00109 }
00110 
00111 QStringList KOrganizerPlugin::invisibleToolbarActions() const
00112 {
00113   QStringList invisible;
00114   invisible += "new_event";
00115   invisible += "new_todo";
00116   invisible += "new_journal";
00117 
00118   invisible += "view_todo";
00119   invisible += "view_journal";
00120   return invisible;
00121 }
00122 
00123 void KOrganizerPlugin::select()
00124 {
00125   interface()->showEventView();
00126 }
00127 
00128 KCalendarIface_stub *KOrganizerPlugin::interface()
00129 {
00130   if ( !mIface ) {
00131     part();
00132   }
00133   Q_ASSERT( mIface );
00134   return mIface;
00135 }
00136 
00137 void KOrganizerPlugin::slotNewEvent()
00138 {
00139   interface()->openEventEditor( "" );
00140 }
00141 
00142 void KOrganizerPlugin::slotSyncEvents()
00143 {
00144   DCOPRef ref( "kmail", "KMailICalIface" );
00145   ref.send( "triggerSync", QString("Calendar") );
00146 }
00147 
00148 bool KOrganizerPlugin::createDCOPInterface( const QString& serviceType )
00149 {
00150   kdDebug(5602) << k_funcinfo << serviceType << endl;
00151   if ( serviceType == "DCOP/Organizer" || serviceType == "DCOP/Calendar" ) {
00152     if ( part() )
00153       return true;
00154   }
00155 
00156   return false;
00157 }
00158 
00159 bool KOrganizerPlugin::isRunningStandalone()
00160 {
00161   return mUniqueAppWatcher->isRunningStandalone();
00162 }
00163 
00164 bool KOrganizerPlugin::canDecodeDrag( QMimeSource *mimeSource )
00165 {
00166   return QTextDrag::canDecode( mimeSource ) ||
00167          KPIM::MailListDrag::canDecode( mimeSource );
00168 }
00169 
00170 void KOrganizerPlugin::processDropEvent( QDropEvent *event )
00171 {
00172   KABC::Addressee::List list;
00173   if ( KVCardDrag::decode( event, list ) ) {
00174     QStringList attendees;
00175     KABC::Addressee::List::Iterator it;
00176     for ( it = list.begin(); it != list.end(); ++it ) {
00177       QString email = (*it).fullEmail();
00178       if ( email.isEmpty() ) {
00179         attendees.append( (*it).realName() + "<>" );
00180       } else {
00181         attendees.append( email );
00182       }
00183     }
00184     interface()->openEventEditor( i18n( "Meeting" ), QString::null, QString::null,
00185                                   attendees );
00186     return;
00187   }
00188 
00189   if ( KCal::ICalDrag::canDecode( event) ) {
00190       KCal::CalendarLocal cal( KPimPrefs::timezone() );
00191       if ( KCal::ICalDrag::decode( event, &cal ) ) {
00192           KCal::Incidence::List incidences = cal.incidences();
00193           if ( !incidences.isEmpty() ) {
00194               event->accept();
00195               KCal::Incidence *i = incidences.first();
00196               QString summary;
00197               if ( dynamic_cast<KCal::Journal*>( i ) )
00198                   summary = i18n( "Note: %1" ).arg( i->summary() );
00199               else
00200                   summary = i->summary();
00201               interface()->openEventEditor( summary, i->description(), QString() );
00202               return;
00203           }
00204       // else fall through to text decoding
00205       }
00206   }
00207 
00208   QString text;
00209   if ( QTextDrag::decode( event, text ) ) {
00210     kdDebug(5602) << "DROP:" << text << endl;
00211     interface()->openEventEditor( text );
00212     return;
00213   }
00214 
00215   KPIM::MailList mails;
00216   if ( KPIM::MailListDrag::decode( event, mails ) ) {
00217     if ( mails.count() != 1 ) {
00218       KMessageBox::sorry( core(),
00219                           i18n("Drops of multiple mails are not supported." ) );
00220     } else {
00221       KPIM::MailSummary mail = mails.first();
00222       QString txt = i18n("From: %1\nTo: %2\nSubject: %3").arg( mail.from() )
00223                     .arg( mail.to() ).arg( mail.subject() );
00224 
00225       KTempFile tf;
00226       tf.setAutoDelete( true );
00227       QString uri = QString::fromLatin1("kmail:") + QString::number( mail.serialNumber() );
00228       tf.file()->writeBlock( event->encodedData( "message/rfc822" ) );
00229       tf.close();
00230       interface()->openEventEditor( i18n("Mail: %1").arg( mail.subject() ), txt,
00231                                     uri, tf.name(), QStringList(), "message/rfc822" );
00232     }
00233     return;
00234   }
00235 
00236   KMessageBox::sorry( core(), i18n("Cannot handle drop events of type '%1'.")
00237                               .arg( event->format() ) );
00238 }
00239 
00240 void KOrganizerPlugin::loadProfile( const QString& directory )
00241 {
00242   DCOPRef ref( "korganizer", "KOrganizerIface" );
00243   ref.send( "loadProfile", directory );
00244 }
00245 
00246 void KOrganizerPlugin::saveToProfile( const QString& directory ) const
00247 {
00248   DCOPRef ref( "korganizer", "KOrganizerIface" );
00249   ref.send( "saveToProfile", directory );
00250 }
00251 
00252 #include "korganizerplugin.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys