exchange.cpp
1 /*
2  This file is part of KOrganizer.
3  Copyright (c) 2002 Jan-Pascal van Best <janpascal@vanbest.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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19 
20 #include <tqfile.h>
21 
22 #include <tdeapplication.h>
23 #include <tdeconfig.h>
24 #include <kstandarddirs.h>
25 
26 #include <kurl.h>
27 #include <kdebug.h>
28 
29 #include <tdemessagebox.h>
30 #include <tdelocale.h>
31 #include <tdeaction.h>
32 #include <tdeglobal.h>
33 
34 #include "korganizer/korganizer.h"
35 #include "korganizer/calendarview.h"
36 
37 #include <exchangeclient.h>
38 #include <exchangeaccount.h>
39 
40 #include "exchange.h"
41 #include "exchangedialog.h"
42 #include "exchangeconfig.h"
43 
44 
45 using namespace KCal; // Needed for connecting slots
46 
47 class ExchangeFactory : public KOrg::PartFactory {
48  public:
49  KOrg::Part *create(KOrg::MainWindow *parent, const char *name)
50  {
51  kdDebug(5850) << "Registering Exchange Plugin...\n";
52  TDEGlobal::locale()->insertCatalogue("libkpimexchange");
53  return new Exchange(parent,name);
54  }
55 };
56 
57 K_EXPORT_COMPONENT_FACTORY( libkorg_exchange, ExchangeFactory )
58 
59 Exchange::Exchange(KOrg::MainWindow *parent, const char *name) :
60  KOrg::Part(parent,name)
61 {
62  setInstance( new TDEInstance( "korganizer" ) );
63 
64  kdDebug(5850) << "Creating Exchange Plugin...\n";
65 
66  mAccount = new KPIM::ExchangeAccount( "Calendar/Exchange Plugin" );
67  mClient = new KPIM::ExchangeClient( mAccount );
68  mClient->setWindow( parent->topLevelWidget() );
69 
70  setXMLFile("plugins/exchangeui.rc");
71 
72  new TDEAction(i18n("&Download..."), 0, this, TQ_SLOT(download()),
73  actionCollection(), "exchange_download");
74 
75  TDEAction *action = new TDEAction(i18n("&Upload Event..."), 0, this, TQ_SLOT(upload()),
76  actionCollection(), "exchange_upload");
77  TQObject::connect(mainWindow()->view(),TQ_SIGNAL(incidenceSelected(Incidence *)),
78  this, TQ_SLOT(slotIncidenceSelected(Incidence *)));
79  action->setEnabled( false );
80  TQObject::connect(this,TQ_SIGNAL(enableIncidenceActions(bool)),
81  action,TQ_SLOT(setEnabled(bool)));
82 
83  action = new TDEAction(i18n("De&lete Event"), 0, this, TQ_SLOT(remove()),
84  actionCollection(), "exchange_delete");
85  TQObject::connect(this,TQ_SIGNAL(enableIncidenceActions(bool)),
86  action,TQ_SLOT(setEnabled(bool)));
87  action->setEnabled( false );
88 
89  new TDEAction(i18n("&Configure..."), 0, this, TQ_SLOT(configure()),
90  actionCollection(), "exchange_configure");
91 
92  connect( this, TQ_SIGNAL( calendarChanged() ), mainWindow()->view(), TQ_SLOT( updateView() ) );
93  connect( this, TQ_SIGNAL( calendarChanged(const TQDate &, const TQDate &)),
94  mainWindow()->view(), TQ_SLOT(updateView(const TQDate &, const TQDate &)) );
95 }
96 
97 Exchange::~Exchange()
98 {
99  kdDebug(5850) << "Exchange Plugin destructor" << endl;
100 }
101 
102 TQString Exchange::info()
103 {
104  return i18n("This plugin imports and export calendar events from/to a Microsoft Exchange 2000 Server.");
105 }
106 
107 TQString Exchange::shortInfo()
108 {
109  return i18n("Exchange Plugin");
110 }
111 
112 void Exchange::slotIncidenceSelected( Incidence *incidence )
113 {
114  emit enableIncidenceActions( incidence != 0 );
115 }
116 
117 void Exchange::download()
118 {
119  ExchangeDialog dialog( mainWindow()->view()->startDate(), mainWindow()->view()->endDate() );
120 
121  if (dialog.exec() != TQDialog::Accepted )
122  return;
123 
124  TQDate start = dialog.m_start->date();
125  TQDate end = dialog.m_end->date();
126 
127  KCal::Calendar* calendar = mainWindow()->view()->calendar();
128 
129  int result = mClient->downloadSynchronous(calendar, start, end, true );
130 
131  if ( result == KPIM::ExchangeClient::ResultOK )
132  emit calendarChanged();
133  else
134  showError( result, mClient->detailedErrorString() );
135 
136 }
137 
138 void Exchange::upload()
139 {
140  kdDebug(5850) << "Called Exchange::upload()" << endl;
141 
142  Event* event = dynamic_cast<Event *> ( mainWindow()->view()->currentSelection() );
143  if ( ! event )
144  {
145  KMessageBox::information( 0L, i18n("Please select an appointment."), i18n("Exchange Plugin") );
146  return;
147  }
148  if ( KMessageBox::warningContinueCancel( 0L, i18n("Exchange Upload is EXPERIMENTAL, you may lose data on this appointment!"), i18n("Exchange Plugin"), i18n("&Upload") )
149  == KMessageBox::Continue ) {
150  kdDebug(5850) << "Trying to add appointment " << event->summary() << endl;
151  int result = mClient->uploadSynchronous( event );
152  if ( result != KPIM::ExchangeClient::ResultOK )
153  showError( result, mClient->detailedErrorString() );
154  }
155 }
156 
157 void Exchange::remove()
158 {
159  kdDebug(5850) << "Called Exchange::remove()" << endl;
160 
161  Event* event = dynamic_cast<Event *> ( mainWindow()->view()->currentSelection() );
162  if ( ! event )
163  {
164  KMessageBox::information( 0L, i18n("Please select an appointment."), i18n("Exchange Plugin") );
165  return;
166  }
167 
168  if ( KMessageBox::warningContinueCancel( 0L, i18n("Exchange Delete is EXPERIMENTAL, if this is a recurring event it will delete all instances!"), i18n("Exchange Plugin"), KGuiItem(i18n("&Delete"),"edit-delete") )
169  == KMessageBox::Continue ) {
170  kdDebug(5850) << "Trying to delete appointment " << event->summary() << endl;
171  int result = mClient->removeSynchronous( event );
172 
173  if ( result == KPIM::ExchangeClient::ResultOK ) {
174  mainWindow()->view()->calendar()->deleteEvent( event );
175  emit calendarChanged();
176  } else
177  showError( result, mClient->detailedErrorString() );
178  }
179 }
180 
181 void Exchange::configure()
182 {
183  kdDebug(5850) << "Exchange::configure" << endl;
184  ExchangeConfig dialog( mAccount );
185 
186  if (dialog.exec() == TQDialog::Accepted )
187  mAccount->save( "Calendar/Exchange Plugin" );
188 }
189 
190 void Exchange::showError( int error, const TQString& moreInfo /* = TQString() */ )
191 {
192  TQString errorText;
193  switch( error ) {
194  case KPIM::ExchangeClient::ResultOK:
195  errorText = i18n( "No Error" );
196  break;
197  case KPIM::ExchangeClient::CommunicationError:
198  errorText = i18n( "The Exchange server could not be reached or returned an error." );
199  break;
200  case KPIM::ExchangeClient::ServerResponseError:
201  errorText = i18n( "Server response could not be interpreted." );
202  break;
203  case KPIM::ExchangeClient::IllegalAppointmentError:
204  errorText = i18n( "Appointment data could not be interpreted." );
205  break;
206  case KPIM::ExchangeClient::NonEventError:
207  errorText = i18n( "This should not happen: trying to upload wrong type of event." );
208  break;
209  case KPIM::ExchangeClient::EventWriteError:
210  errorText = i18n( "An error occurred trying to write an appointment to the server." );
211  break;
212  case KPIM::ExchangeClient::DeleteUnknownEventError:
213  errorText = i18n( "Trying to delete an event that is not present on the server." );
214  break;
215  case KPIM::ExchangeClient::UnknownError:
216  default:
217  errorText = i18n( "Unknown Error" );
218  }
219 
220  if ( error != KPIM::ExchangeClient::ResultOK ) {
221  if ( moreInfo.isNull() )
222  KMessageBox::error( mainWindow()->topLevelWidget(), errorText, i18n( "Exchange Plugin" ) );
223  else
224  KMessageBox::detailedError( mainWindow()->topLevelWidget(), errorText, moreInfo, i18n( "Exchange Plugin" ) );
225  }
226 }
227 
228 void Exchange::test()
229 {
230  kdDebug(5850) << "Entering test()" << endl;
231  mClient->test();
232 }
233 
234 void Exchange::test2()
235 {
236  kdDebug(5850) << "Entering test2()" << endl;
237 }
238 #include "exchange.moc"
interface for korganizer main window
Definition: mainwindow.h:41
virtual TQWidget * topLevelWidget()=0
Return widget whcih represents this main window.
bool view(TQWidget *parent, Attachment *attachment)