summaryrefslogtreecommitdiffabout
path: root/korganizer/koimportoldialog.cpp
Unidiff
Diffstat (limited to 'korganizer/koimportoldialog.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--korganizer/koimportoldialog.cpp466
1 files changed, 466 insertions, 0 deletions
diff --git a/korganizer/koimportoldialog.cpp b/korganizer/koimportoldialog.cpp
new file mode 100644
index 0000000..36363e5
--- a/dev/null
+++ b/korganizer/koimportoldialog.cpp
@@ -0,0 +1,466 @@
1/*
2 This file is part of KOrganizer.
3 Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
19 As a special exception, permission is given to link this program
20 with any edition of Qt, and distribute the resulting executable,
21 without including the source code for Qt in the source distribution.
22*/
23
24#include <qtooltip.h>
25#include <qframe.h>
26#include <qpixmap.h>
27#include <qlayout.h>
28#include <qprogressbar.h>
29#include <qwidgetstack.h>
30#include <qdatetime.h>
31#include <qdir.h>
32#include <qapplication.h>
33#include <qhbox.h>
34#include <qheader.h>
35#include <qdatetime.h>
36#include <qlistview.h>
37
38#include <kdebug.h>
39#include <klocale.h>
40#include <kstandarddirs.h>
41#include <kmessagebox.h>
42#include <kfiledialog.h>
43
44#include <libkdepim/categoryselectdialog.h>
45#include <libkdepim/kinputdialog.h>
46
47#include <libkcal/calendarlocal.h>
48#include <libkcal/icalformat.h>
49
50#include "koprefs.h"
51#include "koglobals.h"
52
53#include "koimportoldialog.h"
54
55#include "../outport/msoutl9.h"
56#include <ole2.h>
57#include <comutil.h>
58_Application gOlApp;
59
60QDateTime mDdate2Qdtr( DATE dt)
61{
62 COleDateTime odt;
63 SYSTEMTIME st;
64 odt = dt;
65 odt.GetAsSystemTime(st);
66 QDateTime qdt (QDate(st.wYear, st.wMonth,st.wDay ),QTime( st.wHour, st.wMinute,st.wSecond ) );
67 return qdt;
68}
69
70class OLEListViewItem : public QCheckListItem
71{
72 public:
73 OLEListViewItem( QListView *parent, QString text ) :
74 QCheckListItem( parent, text, QCheckListItem::CheckBox ) { ; };
75 OLEListViewItem( QListViewItem *after, QString text ) :
76 QCheckListItem( after, text, QCheckListItem::CheckBox ) { ; };
77 ~OLEListViewItem() {};
78 void setData( DWORD data ) {mData= data; };
79 DWORD data() { return mData ;};
80 private:
81 DWORD mData;
82};
83
84KOImportOLdialog::KOImportOLdialog( const QString &caption,
85 Calendar *calendar, QWidget *parent ) :
86 KDialogBase( Plain, caption, User1 | Close, Ok,
87 parent, caption, true, false, i18n("Import!") )
88{
89 QHBox * mw = new QHBox( this );
90 setMainWidget( mw );
91 mListView = new QListView( mw );
92 mListView->addColumn(i18n("Select Folder to import"));
93 mListView->addColumn(i18n("Content Type"));
94 mCalendar = calendar;
95 connect( this, SIGNAL( user1Clicked() ),SLOT ( slotApply()));
96 setupFolderView();
97 resize( sizeHint().width()+50, sizeHint().height()+50 );
98}
99
100KOImportOLdialog::~KOImportOLdialog()
101{
102
103}
104
105
106void KOImportOLdialog::setupFolderView()
107{
108 SCODE sc = ::OleInitialize(NULL);
109 if ( FAILED ( sc ) ) {
110 KMessageBox::information(this,"OLE initialisation failed");
111 return;
112 }
113
114 if(!gOlApp.CreateDispatch(_T("Outlook.Application"),NULL)){
115 KMessageBox::information(this,"Sorry, cannot access Outlook");
116 return ;
117 }
118 MAPIFolder mfInbox;
119 MAPIFolder mfRoot;
120 CString szName;
121 _NameSpace olNS;
122 olNS = gOlApp.GetNamespace(_T("MAPI"));
123 mfInbox = olNS.GetDefaultFolder(6);
124 mfRoot = mfInbox.GetParent();
125 szName = mfRoot.GetName();
126 long iType = mfRoot.GetDefaultItemType();
127 QString mes;
128 mes = QString::fromUcs2( szName.GetBuffer() );
129 OLEListViewItem * root = new OLEListViewItem( mListView, mes );
130 mfRoot.m_lpDispatch->AddRef();
131 addFolder( root, mfRoot.m_lpDispatch );
132 root->setOpen( true );
133 mListView->setSortColumn( 0 );
134 mListView->sort( );
135}
136
137
138void KOImportOLdialog::addFolder(OLEListViewItem* iParent, LPDISPATCH dispParent)
139{
140 MAPIFolder mfParent(dispParent), mfChild;
141 _Folders folders;
142 _variant_t fndx((long)0);
143 CString szName;
144 long iType;
145 OLEListViewItem* hChild;
146
147 folders = mfParent.GetFolders();
148 for(int i=1; i <= folders.GetCount(); ++i)
149 {
150 fndx = (long)i;
151 mfChild = folders.Item(fndx.Detach());
152 mfChild.m_lpDispatch->AddRef();
153 szName = mfChild.GetName();
154 iType = mfChild.GetDefaultItemType();
155 hChild = new OLEListViewItem( iParent , QString::fromUcs2( szName.GetBuffer() ) );
156 if ( iType != 1)
157 hChild->setEnabled( false );
158 QString ts;
159 switch( iType ) {
160 case 0:
161 ts = i18n("Mail");
162 break;
163 case 1:
164 ts = i18n("Calendar");
165 break;
166 case 2:
167 ts = i18n("Contacts");
168 break;
169 case 3:
170 ts = i18n("Todos");
171 break;
172 case 4:
173 ts = i18n("Journals");
174 break;
175 case 5:
176 ts = i18n("Notes");
177 break;
178 default:
179 ts = i18n("Unknown");
180 }
181 hChild->setText( 1,ts);
182 hChild->setData( (DWORD) mfChild.m_lpDispatch );
183 mfChild.m_lpDispatch->AddRef();
184 addFolder(hChild, mfChild.m_lpDispatch);
185 }
186}
187
188void KOImportOLdialog::slotApply()
189{
190 importedItems = 0;
191 OLEListViewItem* child = (OLEListViewItem*) mListView->firstChild();
192 while ( child ) {
193 if ( child->isOn() )
194 readCalendarData( child->data() );
195 child = (OLEListViewItem*) child->itemBelow();
196 }
197 QString mes = i18n("Importing complete.\n\n%1 items imported.").arg( importedItems);
198 KMessageBox::information(this,mes);
199}
200void KOImportOLdialog::readCalendarData( DWORD folder )
201{
202
203 LPDISPATCH dispItem = (LPDISPATCH)folder;
204 dispItem->AddRef();
205 MAPIFolder mf(dispItem);
206 mf.m_lpDispatch->AddRef();
207 _Items folderItems;
208 _variant_t indx((long)0);
209 LPDISPATCH itm;
210 int i;
211 folderItems = mf.GetItems();
212 QProgressBar bar( folderItems.GetCount(),0 );
213 bar.setCaption (i18n("Importing - close to abort!") );
214 int h = bar.sizeHint().height() ;
215 int w = 300;
216 int dw = QApplication::desktop()->width();
217 int dh = QApplication::desktop()->height();
218 bar.setGeometry( (dw-w)/2, (dh - h )/2 ,w,h );
219 bar.show();
220 for(i=1; i <= folderItems.GetCount(); ++i)
221 {
222 qApp->processEvents();
223 if ( ! bar.isVisible() )
224 return ;
225 bar.setProgress( i );
226 indx = (long)i;
227 itm = folderItems.Item(indx.Detach());
228 _AppointmentItem * pItem = (_AppointmentItem *)&itm;
229 ol2kopiCalendar( pItem );
230 itm->Release();
231 }
232}
233void KOImportOLdialog::slotOk()
234{
235 QDialog::accept();
236}
237
238void KOImportOLdialog::ol2kopiCalendar( _AppointmentItem * aItem, bool computeRecurrence )
239{
240 KCal::Event* event = new KCal::Event();
241 if ( aItem->GetAllDayEvent() ){
242 event->setDtStart( QDateTime( mDdate2Qdtr( aItem->GetStart()).date(),QTime(0,0,0 ) ));
243 event->setDtEnd( QDateTime( mDdate2Qdtr( aItem->GetEnd()) .date(),QTime(0,0,0 )).addDays(-1));
244 event->setFloats( true );
245 } else {
246 event->setDtStart( mDdate2Qdtr( aItem->GetStart()) );
247 event->setDtEnd( mDdate2Qdtr( aItem->GetEnd()) );
248 event->setFloats( false );
249 }
250 event->setSummary( QString::fromUcs2( aItem->GetSubject().GetBuffer()) );
251 event->setLocation( QString::fromUcs2( aItem->GetLocation().GetBuffer()) );
252 event->setDescription( QString::fromUcs2( aItem->GetBody().GetBuffer()) );
253 QString cat = QString::fromUcs2( aItem->GetCategories().GetBuffer());
254 event->setCategories( QStringList::split( ";", cat ));
255 if ( aItem->GetReminderSet() ) {
256 event->clearAlarms();
257 Alarm* alarm = event->newAlarm();
258 alarm->setStartOffset( -aItem->GetReminderMinutesBeforeStart()*60 );
259 alarm->setEnabled( true );
260 if ( aItem->GetReminderPlaySound() ) {
261 alarm->setType( Alarm::Audio );
262 alarm->setAudioFile( QString::fromUcs2( aItem->GetReminderSoundFile().GetBuffer()));
263 }
264 else
265 alarm->setType( Alarm::Display );
266 alarm->setRepeatCount( aItem->GetReplyTime() );
267 }
268 // OL :pub 0 - pers 1 - priv 2 - conf 3
269 // KO : pub 0 - priv 1 - conf 2
270 int sec = aItem->GetSensitivity() ;
271 if ( sec > 1 )// mapping pers -> private
272 --sec;
273 event->setSecrecy( sec );
274 if ( aItem->GetBusyStatus() == 0 )
275 event->setTransparency( Event::Transparent);// OL free
276 else
277 event->setTransparency( Event::Opaque);//OL all other
278
279 if ( aItem->GetIsRecurring() && computeRecurrence ) { //recur
280
281 RecurrencePattern recpat = aItem->GetRecurrencePattern();
282
283 QDate startDate = mDdate2Qdtr(recpat.GetPatternStartDate()).date();
284 int freq = recpat.GetInterval();
285
286 bool hasEndDate = !recpat.GetNoEndDate();
287 QDate endDate = mDdate2Qdtr(recpat.GetPatternEndDate()).date();
288 QBitArray weekDays( 7 );
289 weekDays.fill(false );
290 uint weekDaysNum = recpat.GetDayOfWeekMask();
291 int i;
292 int bb = 2;
293 for( i = 1; i <= 6; ++i ) {
294 weekDays.setBit( i - 1, ( bb & weekDaysNum ));
295 bb = 4 << (i-1);
296 //qDebug(" %d bit %d ",i-1,weekDays.at(i-1) );
297 }
298 if ( 1 & weekDaysNum)
299 weekDays.setBit( 6 );
300 // int pos = 1;// pending
301
302 Recurrence *r = event->recurrence();
303 int rtype = recpat.GetRecurrenceType();
304 //recurrence types are:
305 /*
306 olRecursDaily(0)
307 olRecursWeekly(1)
308 olRecursMonthly(2)
309 olRecursMonthNth(3)
310 olRecursYearly(5)
311 olRecursYearNth(6)
312 */
313
314 int duration = recpat.GetOccurrences();
315 if ( !hasEndDate )
316 duration = -1;
317
318 //LPDISPATCH RecurrencePattern::GetExceptions()
319 //long RecurrencePattern::GetMonthOfYear()
320 if ( rtype == 0 ) {
321 if ( hasEndDate ) r->setDaily( freq, endDate );
322 else r->setDaily( freq, duration );
323 } else if ( rtype == 1 ) {
324 if ( hasEndDate ) r->setWeekly( freq, weekDays, endDate );
325 else r->setWeekly( freq, weekDays, duration );
326 } else if ( rtype == 2 ) {
327 if ( hasEndDate )
328 r->setMonthly( Recurrence::rMonthlyDay, freq, endDate );
329 else
330 r->setMonthly( Recurrence::rMonthlyDay, freq, duration );
331 //r->addMonthlyDay( startDate.day() );
332 r->addMonthlyDay( recpat.GetDayOfMonth() );
333 } else if ( rtype == 3 ) {
334 if ( hasEndDate )
335 r->setMonthly( Recurrence::rMonthlyPos, freq, endDate );
336 else
337 r->setMonthly( Recurrence::rMonthlyPos, freq, duration );
338 QBitArray days( 7 );
339 days.fill( false );
340 days.setBit( startDate.dayOfWeek() - 1 );
341 int pos = (startDate.day()/7)+1;
342 r->addMonthlyPos( pos, days );
343 //QString mes = i18n("Importing monthlypos.\n\npos: %1 , day: %2").arg( pos).arg( startDate.dayOfWeek() - 1);
344 //KMessageBox::information(this,mes);
345 } else if ( rtype == 5 ) {
346 freq = 1;
347 if ( hasEndDate )
348 r->setYearly( Recurrence::rYearlyMonth, freq, endDate );
349 else
350 r->setYearly( Recurrence::rYearlyMonth, freq, duration );
351 r->addYearlyNum( startDate.month() );
352 } else if ( true /*rtype == 6*/ ) {
353 // KOganizer cannot handle this in the GUI
354 // we are mapping this to monthly - every 12. month
355 freq = 12;
356 if ( hasEndDate )
357 r->setMonthly( Recurrence::rMonthlyPos, freq, endDate );
358 else
359 r->setMonthly( Recurrence::rMonthlyPos, freq, duration );
360 QBitArray days( 7 );
361 days.fill( false );
362 days.setBit( startDate.dayOfWeek() - 1 );
363 int pos = (startDate.day()/7)+1;
364 r->addMonthlyPos( pos, days );
365 }
366 // recurrence exceptions
367 LPDISPATCH dispItem = recpat.GetExceptions();
368 dispItem->AddRef();
369 Exceptions ex(dispItem);
370 _variant_t indx((long)0);
371 LPDISPATCH itm;
372 for(i=1; i <= ex.GetCount(); ++i) {
373 indx = (long)i;
374 itm = ex.Item( indx.Detach() );
375 ::Exception * pItem = (::Exception *)&itm;
376 event->addExDate( QDateTime( mDdate2Qdtr( pItem->GetOriginalDate())).date() );
377 if ( !pItem->GetDeleted() ) {
378 LPDISPATCH appIt = pItem->GetAppointmentItem();
379 _AppointmentItem * paItem = (_AppointmentItem *)&appIt;
380 ol2kopiCalendar( paItem, false );
381 }
382 itm->Release();
383 }
384 }
385 // recurrence ENTE
386 event->setOrganizer( QString::fromUcs2( aItem->GetOrganizer().GetBuffer()));
387
388 //GetOptionalAttendees()
389 //GetRequiredAttendees()
390 LPDISPATCH dispItem = aItem->GetRecipients();
391 dispItem->AddRef();
392 _Folders mf(dispItem);
393 mf.m_lpDispatch->AddRef();
394 _variant_t indx((long)0);
395 LPDISPATCH itm;
396 int i;
397 QString optAtt = QString::fromUcs2( aItem->GetOptionalAttendees().GetBuffer());
398 QString reqAtt = QString::fromUcs2( aItem->GetRequiredAttendees().GetBuffer());
399 //GetRequiredAttendees()
400 for(i=1; i <= mf.GetCount(); ++i) {
401 indx = (long)i;
402 itm = mf.Item( indx.Detach() );
403 Recipient * pItem = (Recipient *)&itm;
404
405 //a = new KCal::Attendee( (*it).realName(), (*it).preferredEmail(),false,KCal::Attendee::NeedsAction,KCal::Attendee::ReqParticipant,(*it).uid()) ;
406 QString name = QString::fromUcs2( pItem->GetName().GetBuffer());
407 KCal::Attendee::PartStat stat;
408 bool rsvp = false;
409 switch ( pItem->GetMeetingResponseStatus() ) {
410 case 0: //not answered
411 rsvp = true;
412 case 5: //not answered
413 stat = Attendee::NeedsAction;
414 break;
415 case 1: //organizer
416 stat = Attendee::Delegated ;
417 break;
418 case 2: //tentative
419 stat = Attendee::Tentative ;
420 break;
421 case 3: //accepted
422 stat = Attendee::Accepted;
423 break;
424 case 4: //declined
425 stat =Attendee::Declined ;
426 break;
427 default:
428 stat = Attendee::NeedsAction ;
429
430 }
431 KCal::Attendee::Role role;
432 if ( event->organizer() == name )
433 role = KCal::Attendee::Chair;
434 else if ( reqAtt.find( name ) >= 0 )
435 role = KCal::Attendee::ReqParticipant;
436 else if ( optAtt.find( name ) >= 0 )
437 role = KCal::Attendee::OptParticipant;
438 else
439 role = KCal::Attendee::NonParticipant;
440 QString mail = QString::fromUcs2(pItem->GetAddress().GetBuffer());
441 if( mail.isEmpty() && name.find("@") > 0 )
442 mail = name;
443 QString uid;
444 if ( mail.isEmpty() )
445 uid = QString::fromUcs2( pItem->GetEntryID().GetBuffer());
446 else
447 uid = mail;
448 mail = mail.stripWhiteSpace();
449 KCal::Attendee * a = new KCal::Attendee( name, mail,rsvp,stat,role,uid) ;
450 event->addAttendee( a , false );
451 itm->Release();
452 }
453
454
455 if ( !mCalendar->addEventNoDup( event ))
456 delete event;
457 else {
458 // QString mes = i18n("Importing %1.\n date: %2 date: %3").arg( event->summary()).arg( event->dtStart().toString()).arg( event->dtEnd().toString());
459 //KMessageBox::information(this,mes);
460 ++importedItems;
461 }
462}
463void KOImportOLdialog::slotCancel()
464{
465 reject();
466}