korganizer

mailscheduler.cpp
1 /*
2  This file is part of KOrganizer.
3 
4  Copyright (c) 2001 Cornelius Schumacher <schumacher@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 <tqdir.h>
26 #include <tqfile.h>
27 #include <tqregexp.h>
28 
29 #include <tdelocale.h>
30 #include <kstandarddirs.h>
31 #include <kdebug.h>
32 
33 #include <libkcal/calendar.h>
34 #include <libkcal/event.h>
35 #include <libkcal/icalformat.h>
36 
37 #include "komailclient.h"
38 #include "incidencechanger.h"
39 
40 #include "mailscheduler.h"
41 
42 
43 using namespace KCal;
44 
45 MailScheduler::MailScheduler( Calendar *calendar )
46  : IMIPScheduler( calendar )
47 {
48 }
49 
50 MailScheduler::~MailScheduler()
51 {
52 }
53 
54 bool MailScheduler::publish( IncidenceBase *incidence,
55  const TQString &recipients )
56 {
57  TQString messageText = mFormat->createScheduleMessage( incidence,
58  Scheduler::Publish );
59  KOMailClient mailer;
60  return mailer.mailTo( incidence, recipients, messageText );
61 }
62 
63 bool MailScheduler::performTransaction( IncidenceBase *incidence,
64  Method method,
65  const TQString &recipients )
66 {
67  TQString messageText = mFormat->createScheduleMessage( incidence, method );
68 
69  KOMailClient mailer;
70  return mailer.mailTo( incidence, recipients, messageText );
71 }
72 
73 bool MailScheduler::performTransaction( IncidenceBase *incidence,
74  Method method )
75 {
76  TQString messageText = mFormat->createScheduleMessage( incidence, method );
77 
78  KOMailClient mailer;
79  bool status;
80  if ( method == Request ||
81  method == Cancel ||
82  method == Add ||
83  method == Declinecounter ) {
84  status = mailer.mailAttendees( incidence, messageText );
85  } else {
86  TQString subject;
87  Incidence *inc = dynamic_cast<Incidence*>( incidence );
88  if ( inc && method == Counter )
89  subject = i18n( "Counter proposal: %1" ).arg( inc->summary() );
90  status = mailer.mailOrganizer( incidence, messageText, subject );
91  }
92  return status;
93 }
94 
95 TQPtrList<ScheduleMessage> MailScheduler::retrieveTransactions()
96 {
97  TQString incomingDirName = locateLocal( "data", "korganizer/income" );
98  kdDebug(5850) << "MailScheduler::retrieveTransactions: dir: "
99  << incomingDirName << endl;
100 
101  TQPtrList<ScheduleMessage> messageList;
102 
103  TQDir incomingDir( incomingDirName );
104  TQStringList incoming = incomingDir.entryList( TQDir::Files );
105  TQStringList::ConstIterator it;
106  for( it = incoming.begin(); it != incoming.end(); ++it ) {
107  kdDebug(5850) << "-- File: " << (*it) << endl;
108 
109  TQFile f( incomingDirName + "/" + (*it) );
110  bool inserted = false;
111  TQMap<IncidenceBase*, TQString>::Iterator iter;
112  for ( iter = mEventMap.begin(); iter != mEventMap.end(); ++iter ) {
113  if ( iter.data() == incomingDirName + "/" + (*it) )
114  inserted = true;
115  }
116  if ( !inserted ) {
117  if ( !f.open( IO_ReadOnly ) ) {
118  kdDebug(5850)
119  << "MailScheduler::retrieveTransactions(): Can't open file'"
120  << (*it) << "'" << endl;
121  } else {
122  TQTextStream t( &f );
123  t.setEncoding( TQTextStream::Latin1 );
124  TQString messageString = t.read();
125  messageString.replace( TQRegExp( "\n[ \t]"), "" );
126  messageString = TQString::fromUtf8( messageString.latin1() );
127  ScheduleMessage *mess = mFormat->parseScheduleMessage( mCalendar,
128  messageString );
129  if ( mess) {
130  kdDebug(5850)
131  << "MailScheduler::retrieveTransactions: got message '"
132  << (*it) << "'" << endl;
133  messageList.append( mess );
134  mEventMap[ mess->event() ] = incomingDirName + "/" + (*it);
135  } else {
136  TQString errorMessage;
137  if ( mFormat->exception() ) {
138  errorMessage = mFormat->exception()->message();
139  }
140  kdDebug(5850)
141  << "MailScheduler::retrieveTransactions() Error parsing message: "
142  << errorMessage << endl;
143  }
144  f.close();
145  }
146  }
147  }
148  return messageList;
149 }
150 
151 bool MailScheduler::deleteTransaction( IncidenceBase *incidence )
152 {
153  bool status;
154  TQFile f( mEventMap[incidence] );
155  mEventMap.remove( incidence );
156  if ( !f.exists() ) {
157  status = false;
158  } else {
159  status = f.remove();
160  }
161  return status;
162 }
163 
164 TQString MailScheduler::freeBusyDir()
165 {
166  return locateLocal( "data", "korganizer/freebusy" );
167 }
168 
169 bool MailScheduler::acceptCounterProposal( Incidence *incidence )
170 {
171  if ( !incidence )
172  return false;
173 
174  Incidence *exInc = mCalendar->incidence( incidence->uid() );
175  if ( !exInc )
176  exInc = mCalendar->incidenceFromSchedulingID( incidence->uid() );
177  incidence->setRevision( incidence->revision() + 1 );
178  if ( exInc ) {
179  incidence->setRevision( TQMAX( incidence->revision(), exInc->revision() + 1 ) );
180  // some stuff we don't want to change, just to be safe
181  incidence->setSchedulingID( exInc->schedulingID() );
182  incidence->setUid( exInc->uid() );
183 
184  mCalendar->beginChange( exInc );
185  IncidenceChanger::assignIncidence( exInc, incidence );
186  exInc->updated();
187  mCalendar->endChange( exInc );
188  } else {
189  mCalendar->addIncidence( incidence );
190  }
191  return true;
192 }
TQString uid() const
void setUid(const TQString &)
void setSchedulingID(const TQString &sid)
int revision() const
TQString schedulingID() const
TQString summary() const
void setRevision(int rev)
IncidenceBase * event()