korganizerplugin.cpp
1 /*
2  This file is part of Kontact.
3 
4  Copyright (c) 2001 Matthias Hoelzer-Kluepfel <mhk@kde.org>
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 
20  As a special exception, permission is given to link this program
21  with any edition of TQt, and distribute the resulting executable,
22  without including the source code for TQt in the source distribution.
23 */
24 
25 #include <tqcursor.h>
26 #include <tqfile.h>
27 #include <tqwidget.h>
28 #include <tqdragobject.h>
29 
30 #include <tdeapplication.h>
31 #include <tdeabc/vcardconverter.h>
32 #include <tdeaction.h>
33 #include <dcopref.h>
34 #include <kdebug.h>
35 #include <kgenericfactory.h>
36 #include <kiconloader.h>
37 #include <tdemessagebox.h>
38 #include <kstandarddirs.h>
39 #include <tdetempfile.h>
40 
41 #include <dcopclient.h>
42 
43 #include <libtdepim/kvcarddrag.h>
44 #include <libtdepim/maillistdrag.h>
45 #include <libtdepim/kpimprefs.h>
46 
47 #include <libkcal/calendarlocal.h>
48 #include <libkcal/icaldrag.h>
49 
50 #include "core.h"
51 #include "summarywidget.h"
52 #include "korganizerplugin.h"
53 #include "korg_uniqueapp.h"
54 
55 typedef KGenericFactory< KOrganizerPlugin, Kontact::Core > KOrganizerPluginFactory;
56 K_EXPORT_COMPONENT_FACTORY( libkontact_korganizerplugin,
57  KOrganizerPluginFactory( "kontact_korganizerplugin" ) )
58 
59 KOrganizerPlugin::KOrganizerPlugin( Kontact::Core *core, const char *, const TQStringList& )
60  : Kontact::Plugin( core, core, "korganizer" ),
61  mIface( 0 )
62 {
63 
64  setInstance( KOrganizerPluginFactory::instance() );
65  instance()->iconLoader()->addAppDir("tdepim");
66 
67  insertNewAction( new TDEAction( i18n( "New Event..." ), "newappointment",
68  CTRL+SHIFT+Key_E, this, TQ_SLOT( slotNewEvent() ), actionCollection(),
69  "new_event" ) );
70 
71  insertSyncAction( new TDEAction( i18n( "Synchronize Calendar" ), "reload",
72  0, this, TQ_SLOT( slotSyncEvents() ), actionCollection(),
73  "korganizer_sync" ) );
74 
75  mUniqueAppWatcher = new Kontact::UniqueAppWatcher(
77 }
78 
79 KOrganizerPlugin::~KOrganizerPlugin()
80 {
81 }
82 
83 Kontact::Summary *KOrganizerPlugin::createSummaryWidget( TQWidget *parent )
84 {
85  // korg part must be loaded, otherwise when starting kontact on summary view
86  // it won't display our stuff.
87  // If the part is already loaded loadPart() is harmless and just returns
88  loadPart();
89 
90  return new SummaryWidget( this, parent );
91 }
92 
93 KParts::ReadOnlyPart *KOrganizerPlugin::createPart()
94 {
95  KParts::ReadOnlyPart *part = loadPart();
96 
97  if ( !part )
98  return 0;
99 
100  mIface = new KCalendarIface_stub( dcopClient(), "kontact", "CalendarIface" );
101 
102  return part;
103 }
104 
105 TQString KOrganizerPlugin::tipFile() const
106 {
107  TQString file = ::locate("data", "korganizer/tips");
108  return file;
109 }
110 
111 TQStringList KOrganizerPlugin::invisibleToolbarActions() const
112 {
113  TQStringList invisible;
114  invisible += "new_event";
115  invisible += "new_todo";
116  invisible += "new_journal";
117 
118  invisible += "view_todo";
119  invisible += "view_journal";
120  return invisible;
121 }
122 
123 void KOrganizerPlugin::select()
124 {
125  interface()->showEventView();
126 }
127 
128 KCalendarIface_stub *KOrganizerPlugin::interface()
129 {
130  if ( !mIface ) {
131  part();
132  }
133  Q_ASSERT( mIface );
134  return mIface;
135 }
136 
137 void KOrganizerPlugin::slotNewEvent()
138 {
139  interface()->openEventEditor( "" );
140 }
141 
142 void KOrganizerPlugin::slotSyncEvents()
143 {
144  DCOPRef ref( "korganizer", "KOrganizerIface" );
145  ref.send( "syncAllResources" );
146 }
147 
148 bool KOrganizerPlugin::createDCOPInterface( const TQString& serviceType )
149 {
150  kdDebug(5602) << k_funcinfo << serviceType << endl;
151  if ( serviceType == "DCOP/Organizer" || serviceType == "DCOP/Calendar" ) {
152  if ( part() )
153  return true;
154  }
155 
156  return false;
157 }
158 
159 bool KOrganizerPlugin::isRunningStandalone()
160 {
161  return mUniqueAppWatcher->isRunningStandalone();
162 }
163 
164 bool KOrganizerPlugin::canDecodeDrag( TQMimeSource *mimeSource )
165 {
166  return TQTextDrag::canDecode( mimeSource ) ||
167  KPIM::MailListDrag::canDecode( mimeSource );
168 }
169 
170 void KOrganizerPlugin::processDropEvent( TQDropEvent *event )
171 {
172  TDEABC::Addressee::List list;
173  if ( KVCardDrag::decode( event, list ) ) {
174  TQStringList attendees;
175  TDEABC::Addressee::List::Iterator it;
176  for ( it = list.begin(); it != list.end(); ++it ) {
177  TQString email = (*it).fullEmail();
178  if ( email.isEmpty() ) {
179  attendees.append( (*it).realName() + "<>" );
180  } else {
181  attendees.append( email );
182  }
183  }
184  interface()->openEventEditor( i18n( "Meeting" ), TQString(), TQString(),
185  attendees );
186  return;
187  }
188 
189  if ( KCal::ICalDrag::canDecode( event) ) {
190  KCal::CalendarLocal cal( KPimPrefs::timezone() );
191  if ( KCal::ICalDrag::decode( event, &cal ) ) {
192  KCal::Incidence::List incidences = cal.incidences();
193  if ( !incidences.isEmpty() ) {
194  event->accept();
195  KCal::Incidence *i = incidences.first();
196  TQString summary;
197  if ( dynamic_cast<KCal::Journal*>( i ) )
198  summary = i18n( "Note: %1" ).arg( i->summary() );
199  else
200  summary = i->summary();
201  interface()->openEventEditor( summary, i->description(), TQString() );
202  return;
203  }
204  // else fall through to text decoding
205  }
206  }
207 
208  TQString text;
209  if ( TQTextDrag::decode( event, text ) ) {
210  kdDebug(5602) << "DROP:" << text << endl;
211  interface()->openEventEditor( text );
212  return;
213  }
214 
215  KPIM::MailList mails;
216  if ( KPIM::MailListDrag::decode( event, mails ) ) {
217  if ( mails.count() != 1 ) {
218  KMessageBox::sorry( core(),
219  i18n("Drops of multiple mails are not supported." ) );
220  } else {
221  KPIM::MailSummary mail = mails.first();
222  TQString txt = i18n("From: %1\nTo: %2\nSubject: %3").arg( mail.from() )
223  .arg( mail.to() ).arg( mail.subject() );
224 
225  KTempFile tf;
226  tf.setAutoDelete( true );
227  TQString uri = TQString::fromLatin1("kmail:") + TQString::number( mail.serialNumber() );
228  tf.file()->writeBlock( event->encodedData( "message/rfc822" ) );
229  tf.close();
230  interface()->openEventEditor( i18n("Mail: %1").arg( mail.subject() ), txt,
231  uri, tf.name(), TQStringList(), "message/rfc822" );
232  }
233  return;
234  }
235 
236  KMessageBox::sorry( core(), i18n("Cannot handle drop events of type '%1'.")
237  .arg( event->format() ) );
238 }
239 
240 bool KOrganizerPlugin::queryClose() const {
241  KOrganizerIface_stub stub( kapp->dcopClient(), "korganizer", "KOrganizerIface" );
242  bool canClose=stub.canQueryClose();
243  return (!canClose);
244 }
245 
246 void KOrganizerPlugin::loadProfile( const TQString& directory )
247 {
248  DCOPRef ref( "korganizer", "KOrganizerIface" );
249  ref.send( "loadProfile", directory );
250 }
251 
252 void KOrganizerPlugin::saveToProfile( const TQString& directory ) const
253 {
254  DCOPRef ref( "korganizer", "KOrganizerIface" );
255  ref.send( "saveToProfile", directory );
256 }
257 
258 #include "korganizerplugin.moc"
static bool canDecode(TQMimeSource *)
static bool decode(TQMimeSource *e, Calendar *cal)
TQString description() const
TQString summary() const
Summary widget for display in the Summary View plugin.
Definition: summary.h:37
Used by UniqueAppWatcher below, to create the above UniqueAppHandler object when necessary.
If the standalone application is running by itself, we need to watch for when the user closes it,...