kontact

todoplugin.cpp

00001 /*
00002     This file is part of Kontact.
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 <qwidget.h>
00026 #include <qdragobject.h>
00027 #include <qfile.h>
00028 
00029 #include <kapplication.h>
00030 #include <kabc/vcardconverter.h>
00031 #include <kaction.h>
00032 #include <kdebug.h>
00033 #include <kgenericfactory.h>
00034 #include <kiconloader.h>
00035 #include <kmessagebox.h>
00036 #include <dcopclient.h>
00037 #include <dcopref.h>
00038 #include <ktempfile.h>
00039 
00040 #include <libkcal/calendarlocal.h>
00041 #include <libkcal/icaldrag.h>
00042 
00043 #include <libkdepim/maillistdrag.h>
00044 #include <libkdepim/kvcarddrag.h>
00045 #include <libkdepim/kpimprefs.h>
00046 
00047 #include "core.h"
00048 
00049 #include "todoplugin.h"
00050 #include "todosummarywidget.h"
00051 #include "korg_uniqueapp.h"
00052 
00053 typedef KGenericFactory< TodoPlugin, Kontact::Core > TodoPluginFactory;
00054 K_EXPORT_COMPONENT_FACTORY( libkontact_todoplugin,
00055                             TodoPluginFactory( "kontact_todoplugin" ) )
00056 
00057 TodoPlugin::TodoPlugin( Kontact::Core *core, const char *, const QStringList& )
00058   : Kontact::Plugin( core, core, "korganizer" ),
00059     mIface( 0 )
00060 {
00061   setInstance( TodoPluginFactory::instance() );
00062   instance()->iconLoader()->addAppDir("kdepim");
00063 
00064   insertNewAction( new KAction( i18n( "New To-do..." ), "newtodo",
00065                    CTRL+SHIFT+Key_T, this, SLOT( slotNewTodo() ), actionCollection(),
00066                    "new_todo" ) );
00067 
00068   insertSyncAction( new KAction( i18n( "Synchronize To-do List" ), "reload",
00069                    0, this, SLOT( slotSyncTodos() ), actionCollection(),
00070                    "todo_sync" ) );
00071 
00072   mUniqueAppWatcher = new Kontact::UniqueAppWatcher(
00073       new Kontact::UniqueAppHandlerFactory<KOrganizerUniqueAppHandler>(), this );
00074 }
00075 
00076 TodoPlugin::~TodoPlugin()
00077 {
00078 }
00079 
00080 Kontact::Summary *TodoPlugin::createSummaryWidget( QWidget *parent )
00081 {
00082   return new TodoSummaryWidget( this, parent );
00083 }
00084 
00085 KParts::ReadOnlyPart *TodoPlugin::createPart()
00086 {
00087   KParts::ReadOnlyPart *part = loadPart();
00088 
00089   if ( !part )
00090     return 0;
00091 
00092   dcopClient(); // ensure that we register to DCOP as "korganizer"
00093   mIface = new KCalendarIface_stub( dcopClient(), "kontact", "CalendarIface" );
00094 
00095   return part;
00096 }
00097 
00098 void TodoPlugin::select()
00099 {
00100   interface()->showTodoView();
00101 }
00102 
00103 QStringList TodoPlugin::invisibleToolbarActions() const
00104 {
00105   QStringList invisible;
00106   invisible += "new_event";
00107   invisible += "new_todo";
00108   invisible += "new_journal";
00109 
00110   invisible += "view_day";
00111   invisible += "view_list";
00112   invisible += "view_workweek";
00113   invisible += "view_week";
00114   invisible += "view_nextx";
00115   invisible += "view_month";
00116   invisible += "view_journal";
00117   return invisible;
00118 }
00119 
00120 KCalendarIface_stub *TodoPlugin::interface()
00121 {
00122   if ( !mIface ) {
00123     part();
00124   }
00125   Q_ASSERT( mIface );
00126   return mIface;
00127 }
00128 
00129 void TodoPlugin::slotNewTodo()
00130 {
00131   interface()->openTodoEditor( "" );
00132 }
00133 
00134 void TodoPlugin::slotSyncTodos()
00135 {
00136   DCOPRef ref( "kmail", "KMailICalIface" );
00137   ref.send( "triggerSync", QString("Todo") );
00138 }
00139 
00140 bool TodoPlugin::createDCOPInterface( const QString& serviceType )
00141 {
00142   kdDebug(5602) << k_funcinfo << serviceType << endl;
00143   if ( serviceType == "DCOP/Organizer" || serviceType == "DCOP/Calendar" ) {
00144     if ( part() )
00145       return true;
00146   }
00147 
00148   return false;
00149 }
00150 
00151 bool TodoPlugin::canDecodeDrag( QMimeSource *mimeSource )
00152 {
00153   return QTextDrag::canDecode( mimeSource ) ||
00154          KPIM::MailListDrag::canDecode( mimeSource );
00155 }
00156 
00157 bool TodoPlugin::isRunningStandalone()
00158 {
00159   return mUniqueAppWatcher->isRunningStandalone();
00160 }
00161 
00162 void TodoPlugin::processDropEvent( QDropEvent *event )
00163 {
00164   QString text;
00165 
00166   KABC::VCardConverter converter;
00167   if ( KVCardDrag::canDecode( event ) && KVCardDrag::decode( event, text ) ) {
00168     KABC::Addressee::List contacts = converter.parseVCards( text );
00169     KABC::Addressee::List::Iterator it;
00170 
00171     QStringList attendees;
00172     for ( it = contacts.begin(); it != contacts.end(); ++it ) {
00173       QString email = (*it).fullEmail();
00174       if ( email.isEmpty() )
00175         attendees.append( (*it).realName() + "<>" );
00176       else
00177         attendees.append( email );
00178     }
00179 
00180     interface()->openTodoEditor( i18n( "Meeting" ), QString::null, QString::null,
00181                                  attendees );
00182     return;
00183   }
00184 
00185   if ( KCal::ICalDrag::canDecode( event) ) {
00186     KCal::CalendarLocal cal( KPimPrefs::timezone() );
00187     if ( KCal::ICalDrag::decode( event, &cal ) ) {
00188       KCal::Incidence::List incidences = cal.incidences();
00189       if ( !incidences.isEmpty() ) {
00190         event->accept();
00191         KCal::Incidence *i = incidences.first();
00192         QString summary;
00193         if ( dynamic_cast<KCal::Journal*>( i ) )
00194           summary = i18n( "Note: %1" ).arg( i->summary() );
00195         else
00196           summary = i->summary();
00197         interface()->openTodoEditor( summary, i->description(), QString() );
00198         return;
00199       }
00200       // else fall through to text decoding
00201     }
00202   }
00203 
00204   if ( QTextDrag::decode( event, text ) ) {
00205     interface()->openTodoEditor( text );
00206     return;
00207   }
00208 
00209   KPIM::MailList mails;
00210   if ( KPIM::MailListDrag::decode( event, mails ) ) {
00211     if ( mails.count() != 1 ) {
00212       KMessageBox::sorry( core(),
00213                           i18n("Drops of multiple mails are not supported." ) );
00214     } else {
00215       KPIM::MailSummary mail = mails.first();
00216       QString txt = i18n("From: %1\nTo: %2\nSubject: %3").arg( mail.from() )
00217                     .arg( mail.to() ).arg( mail.subject() );
00218 
00219       KTempFile tf;
00220       tf.setAutoDelete( true );
00221       QString uri = "kmail:" + QString::number( mail.serialNumber() ) + "/" +
00222                     mail.messageId();
00223       tf.file()->writeBlock( event->encodedData( "message/rfc822" ) );
00224       tf.close();
00225       interface()->openTodoEditor( 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 #include "todoplugin.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys