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   return new SummaryWidget( this, parent );
00086 }
00087 
00088 KParts::ReadOnlyPart *KOrganizerPlugin::createPart()
00089 {
00090   KParts::ReadOnlyPart *part = loadPart();
00091 
00092   if ( !part )
00093     return 0;
00094 
00095   mIface = new KCalendarIface_stub( dcopClient(), "kontact", "CalendarIface" );
00096 
00097   return part;
00098 }
00099 
00100 QString KOrganizerPlugin::tipFile() const
00101 {
00102   QString file = ::locate("data", "korganizer/tips");
00103   return file;
00104 }
00105 
00106 QStringList KOrganizerPlugin::invisibleToolbarActions() const
00107 {
00108   QStringList invisible;
00109   invisible += "new_event";
00110   invisible += "new_todo";
00111   invisible += "new_journal";
00112 
00113   invisible += "view_todo";
00114   invisible += "view_journal";
00115   return invisible;
00116 }
00117 
00118 void KOrganizerPlugin::select()
00119 {
00120   interface()->showEventView();
00121 }
00122 
00123 KCalendarIface_stub *KOrganizerPlugin::interface()
00124 {
00125   if ( !mIface ) {
00126     part();
00127   }
00128   Q_ASSERT( mIface );
00129   return mIface;
00130 }
00131 
00132 void KOrganizerPlugin::slotNewEvent()
00133 {
00134   interface()->openEventEditor( "" );
00135 }
00136 
00137 void KOrganizerPlugin::slotSyncEvents()
00138 {
00139   DCOPRef ref( "kmail", "KMailICalIface" );
00140   ref.send( "triggerSync", QString("Calendar") );
00141 }
00142 
00143 bool KOrganizerPlugin::createDCOPInterface( const QString& serviceType )
00144 {
00145   kdDebug(5602) << k_funcinfo << serviceType << endl;
00146   if ( serviceType == "DCOP/Organizer" || serviceType == "DCOP/Calendar" ) {
00147     if ( part() )
00148       return true;
00149   }
00150 
00151   return false;
00152 }
00153 
00154 bool KOrganizerPlugin::isRunningStandalone()
00155 {
00156   return mUniqueAppWatcher->isRunningStandalone();
00157 }
00158 
00159 bool KOrganizerPlugin::canDecodeDrag( QMimeSource *mimeSource )
00160 {
00161   return QTextDrag::canDecode( mimeSource ) ||
00162          KPIM::MailListDrag::canDecode( mimeSource );
00163 }
00164 
00165 void KOrganizerPlugin::processDropEvent( QDropEvent *event )
00166 {
00167   KABC::Addressee::List list;
00168   if ( KVCardDrag::decode( event, list ) ) {
00169     QStringList attendees;
00170     KABC::Addressee::List::Iterator it;
00171     for ( it = list.begin(); it != list.end(); ++it ) {
00172       QString email = (*it).fullEmail();
00173       if ( email.isEmpty() ) {
00174         attendees.append( (*it).realName() + "<>" );
00175       } else {
00176         attendees.append( email );
00177       }
00178     }
00179     interface()->openEventEditor( i18n( "Meeting" ), QString::null, QString::null,
00180                                   attendees );
00181     return;
00182   }
00183 
00184   if ( KCal::ICalDrag::canDecode( event) ) {
00185       KCal::CalendarLocal cal( KPimPrefs::timezone() );
00186       if ( KCal::ICalDrag::decode( event, &cal ) ) {
00187           KCal::Incidence::List incidences = cal.incidences();
00188           if ( !incidences.isEmpty() ) {
00189               event->accept();
00190               KCal::Incidence *i = incidences.first();
00191               QString summary;
00192               if ( dynamic_cast<KCal::Journal*>( i ) )
00193                   summary = i18n( "Note: %1" ).arg( i->summary() );
00194               else
00195                   summary = i->summary();
00196               interface()->openEventEditor( summary, i->description(), QString() );
00197               return;
00198           }
00199       // else fall through to text decoding
00200       }
00201   }
00202 
00203   QString text;
00204   if ( QTextDrag::decode( event, text ) ) {
00205     kdDebug(5602) << "DROP:" << text << endl;
00206     interface()->openEventEditor( text );
00207     return;
00208   }
00209 
00210   KPIM::MailList mails;
00211   if ( KPIM::MailListDrag::decode( event, mails ) ) {
00212     if ( mails.count() != 1 ) {
00213       KMessageBox::sorry( core(),
00214                           i18n("Drops of multiple mails are not supported." ) );
00215     } else {
00216       KPIM::MailSummary mail = mails.first();
00217       QString txt = i18n("From: %1\nTo: %2\nSubject: %3").arg( mail.from() )
00218                     .arg( mail.to() ).arg( mail.subject() );
00219 
00220       KTempFile tf;
00221       tf.setAutoDelete( true );
00222       QString uri = QString::fromLatin1("kmail:") + QString::number( mail.serialNumber() );
00223       tf.file()->writeBlock( event->encodedData( "message/rfc822" ) );
00224       tf.close();
00225       interface()->openEventEditor( i18n("Mail: %1").arg( mail.subject() ), txt,
00226                                     uri, tf.name(), QStringList(), "message/rfc822" );
00227     }
00228     return;
00229   }
00230 
00231   KMessageBox::sorry( core(), i18n("Cannot handle drop events of type '%1'.")
00232                               .arg( event->format() ) );
00233 }
00234 
00235 void KOrganizerPlugin::loadProfile( const QString& directory )
00236 {
00237   DCOPRef ref( "korganizer", "KOrganizerIface" );
00238   ref.send( "loadProfile", directory );
00239 }
00240 
00241 void KOrganizerPlugin::saveToProfile( const QString& directory ) const
00242 {
00243   DCOPRef ref( "korganizer", "KOrganizerIface" );
00244   ref.send( "saveToProfile", directory );
00245 }
00246 
00247 #include "korganizerplugin.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys