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 TQt, and distribute the resulting executable, 00022 without including the source code for TQt in the source distribution. 00023 */ 00024 00025 #include <tqcursor.h> 00026 #include <tqfile.h> 00027 #include <tqwidget.h> 00028 #include <tqdragobject.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 TQStringList& ) 00060 : Kontact::Plugin( core, TQT_TQOBJECT(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, TQT_SLOT( slotNewEvent() ), actionCollection(), 00069 "new_event" ) ); 00070 00071 insertSyncAction( new KAction( i18n( "Synchronize Calendar" ), "reload", 00072 0, this, TQT_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( TQWidget *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 TQString KOrganizerPlugin::tipFile() const 00106 { 00107 TQString file = ::locate("data", "korganizer/tips"); 00108 return file; 00109 } 00110 00111 TQStringList KOrganizerPlugin::invisibleToolbarActions() const 00112 { 00113 TQStringList 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( "korganizer", "KOrganizerIface" ); 00145 ref.send( "syncAllResources" ); 00146 } 00147 00148 bool KOrganizerPlugin::createDCOPInterface( const TQString& 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( TQMimeSource *mimeSource ) 00165 { 00166 return TQTextDrag::canDecode( mimeSource ) || 00167 KPIM::MailListDrag::canDecode( mimeSource ); 00168 } 00169 00170 void KOrganizerPlugin::processDropEvent( TQDropEvent *event ) 00171 { 00172 KABC::Addressee::List list; 00173 if ( KVCardDrag::decode( event, list ) ) { 00174 TQStringList attendees; 00175 KABC::Addressee::List::Iterator it; 00176 for ( it = list.begin(); it != list.end(); ++it ) { 00177 TQString 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" ), TQString(), TQString(), 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 TQString 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(), TQString() ); 00202 return; 00203 } 00204 // else fall through to text decoding 00205 } 00206 } 00207 00208 TQString text; 00209 if ( TQTextDrag::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 TQString 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 TQString uri = TQString::fromLatin1("kmail:") + TQString::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(), TQStringList(), "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 bool KOrganizerPlugin::queryClose() const { 00241 KOrganizerIface_stub stub( kapp->dcopClient(), "korganizer", "KOrganizerIface" ); 00242 bool canClose=stub.canQueryClose(); 00243 return (!canClose); 00244 } 00245 00246 void KOrganizerPlugin::loadProfile( const TQString& directory ) 00247 { 00248 DCOPRef ref( "korganizer", "KOrganizerIface" ); 00249 ref.send( "loadProfile", directory ); 00250 } 00251 00252 void KOrganizerPlugin::saveToProfile( const TQString& directory ) const 00253 { 00254 DCOPRef ref( "korganizer", "KOrganizerIface" ); 00255 ref.send( "saveToProfile", directory ); 00256 } 00257 00258 #include "korganizerplugin.moc"