00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <qfile.h>
00021
00022 #include <kapplication.h>
00023 #include <kconfig.h>
00024 #include <kstandarddirs.h>
00025
00026 #include <kurl.h>
00027 #include <kdebug.h>
00028
00029 #include <kmessagebox.h>
00030 #include <klocale.h>
00031 #include <kaction.h>
00032 #include <kglobal.h>
00033
00034 #include "korganizer/korganizer.h"
00035 #include "korganizer/calendarview.h"
00036
00037 #include <exchangeclient.h>
00038 #include <exchangeaccount.h>
00039
00040 #include "exchange.h"
00041 #include "exchangedialog.h"
00042 #include "exchangeconfig.h"
00043
00044
00045 using namespace KCal;
00046
00047 class ExchangeFactory : public KOrg::PartFactory {
00048 public:
00049 KOrg::Part *create(KOrg::MainWindow *parent, const char *name)
00050 {
00051 return new Exchange(parent,name);
00052 }
00053 };
00054
00055 extern "C" {
00056 void *init_libkorg_exchange()
00057 {
00058 kdDebug() << "Registering Exchange Plugin...\n";
00059 KGlobal::locale()->insertCatalogue("libkpimexchange");
00060 return (new ExchangeFactory);
00061 }
00062 }
00063
00064 Exchange::Exchange(KOrg::MainWindow *parent, const char *name) :
00065 KOrg::Part(parent,name)
00066 {
00067 kdDebug() << "Creating Exchange Plugin...\n";
00068
00069 mAccount = new KPIM::ExchangeAccount( "Calendar/Exchange Plugin" );
00070 mClient = new KPIM::ExchangeClient( mAccount );
00071 mClient->setWindow( parent );
00072
00073 setXMLFile("plugins/exchangeui.rc");
00074
00075 new KAction(i18n("Download..."), 0, this, SLOT(download()),
00076 actionCollection(), "exchange_download");
00077
00078
00079
00080
00081 KAction *action = new KAction(i18n("Upload Event..."), 0, this, SLOT(upload()),
00082 actionCollection(), "exchange_upload");
00083 QObject::connect(mainWindow()->view(),SIGNAL(incidenceSelected(Incidence *)),
00084 this, SLOT(slotIncidenceSelected(Incidence *)));
00085 action->setEnabled( false );
00086 QObject::connect(this,SIGNAL(enableIncidenceActions(bool)),
00087 action,SLOT(setEnabled(bool)));
00088
00089
00090 action = new KAction(i18n("Delete Event"), 0, this, SLOT(remove()),
00091 actionCollection(), "exchange_delete");
00092 QObject::connect(this,SIGNAL(enableIncidenceActions(bool)),
00093 action,SLOT(setEnabled(bool)));
00094 action->setEnabled( false );
00095
00096 new KAction(i18n("Configure..."), 0, this, SLOT(configure()),
00097 actionCollection(), "exchange_configure");
00098
00099 connect( this, SIGNAL( calendarChanged() ), mainWindow()->view(), SLOT( updateView() ) );
00100 connect( this, SIGNAL( calendarChanged(const QDate &, const QDate &)),
00101 mainWindow()->view(), SLOT(updateView(const QDate &, const QDate &)) );
00102 }
00103
00104 Exchange::~Exchange()
00105 {
00106 kdDebug() << "Exchange Plugin destructor" << endl;
00107 }
00108
00109 QString Exchange::info()
00110 {
00111 return i18n("This plugin imports and export calendar events from/to a Microsoft Exchange 2000 Server.");
00112 }
00113
00114 void Exchange::slotIncidenceSelected( Incidence *incidence )
00115 {
00116 emit enableIncidenceActions( incidence != 0 );
00117 }
00118
00119 void Exchange::download()
00120 {
00121 ExchangeDialog dialog( mainWindow()->view()->startDate(), mainWindow()->view()->endDate() );
00122
00123 if (dialog.exec() != QDialog::Accepted )
00124 return;
00125
00126 QDate start = dialog.m_start->date();
00127 QDate end = dialog.m_end->date();
00128
00129 KCal::Calendar* calendar = mainWindow()->view()->calendar();
00130
00131 int result = mClient->downloadSynchronous(calendar, start, end, true );
00132
00133 if ( result == KPIM::ExchangeClient::ResultOK )
00134 emit calendarChanged();
00135 else
00136 showError( result, mClient->detailedErrorString() );
00137
00138 }
00139
00140 void Exchange::upload()
00141 {
00142 kdDebug() << "Called Exchange::upload()" << endl;
00143
00144 Event* event = static_cast<Event *> ( mainWindow()->view()->currentSelection() );
00145 if ( ! event )
00146 {
00147 KMessageBox::information( 0L, i18n("Please select an appointment"), i18n("Exchange Plugin") );
00148 return;
00149 }
00150 if ( KMessageBox::warningContinueCancel( 0L, i18n("Exchange Upload is EXPERIMENTAL, you may lose data on this appointment!"), i18n("Exchange Plugin") )
00151 == KMessageBox::Continue ) {
00152 kdDebug() << "Trying to add appointment " << event->summary() << endl;
00153 int result = mClient->uploadSynchronous( event );
00154 if ( result != KPIM::ExchangeClient::ResultOK )
00155 showError( result, mClient->detailedErrorString() );
00156 }
00157 }
00158
00159 void Exchange::remove()
00160 {
00161 kdDebug() << "Called Exchange::remove()" << endl;
00162
00163 Event* event = static_cast<Event *> ( mainWindow()->view()->currentSelection() );
00164 if ( ! event )
00165 {
00166 KMessageBox::information( 0L, i18n("Please select an appointment"), i18n("Exchange Plugin") );
00167 return;
00168 }
00169
00170 if ( KMessageBox::warningContinueCancel( 0L, i18n("Exchange Delete is EXPERIMENTAL, if this is a recurring event it will delete all instances!"), i18n("Exchange Plugin") )
00171 == KMessageBox::Continue ) {
00172 kdDebug() << "Trying to delete appointment " << event->summary() << endl;
00173 int result = mClient->removeSynchronous( event );
00174
00175 if ( result == KPIM::ExchangeClient::ResultOK ) {
00176 mainWindow()->view()->calendar()->deleteEvent( event );
00177 emit calendarChanged();
00178 } else
00179 showError( result, mClient->detailedErrorString() );
00180 }
00181 }
00182
00183 void Exchange::configure()
00184 {
00185 kdDebug() << "Exchange::configure" << endl;
00186 ExchangeConfig dialog( mAccount );
00187
00188 if (dialog.exec() == QDialog::Accepted )
00189 mAccount->save( "Calendar/Exchange Plugin" );
00190 }
00191
00192 void Exchange::showError( int error, const QString& moreInfo )
00193 {
00194 QString errorText;
00195 switch( error ) {
00196 case KPIM::ExchangeClient::ResultOK:
00197 errorText = i18n( "No Error" );
00198 break;
00199 case KPIM::ExchangeClient::CommunicationError:
00200 errorText = i18n( "The Exchange server could not be reached or returned an error." );
00201 break;
00202 case KPIM::ExchangeClient::ServerResponseError:
00203 errorText = i18n( "Server response could not be interpreted." );
00204 break;
00205 case KPIM::ExchangeClient::IllegalAppointmentError:
00206 errorText = i18n( "Appointment data could not be interpreted." );
00207 break;
00208 case KPIM::ExchangeClient::NonEventError:
00209 errorText = i18n( "This should not happen: trying to upload wrong type of event." );
00210 break;
00211 case KPIM::ExchangeClient::EventWriteError:
00212 errorText = i18n( "An error occured trying to write an appointment to the server." );
00213 break;
00214 case KPIM::ExchangeClient::DeleteUnknownEventError:
00215 errorText = i18n( "Trying to delete an event that is not present on the server." );
00216 break;
00217 case KPIM::ExchangeClient::UnknownError:
00218 default:
00219 errorText = i18n( "Unknown Error" );
00220 }
00221
00222 if ( error != KPIM::ExchangeClient::ResultOK ) {
00223 if ( moreInfo.isNull() )
00224 KMessageBox::error( mainWindow(), errorText, i18n( "Exchange Plugin" ) );
00225 else
00226 KMessageBox::detailedError( mainWindow(), errorText, moreInfo, i18n( "Exchange Plugin" ) );
00227 }
00228 }
00229
00230 void Exchange::test()
00231 {
00232 kdDebug() << "Entering test()" << endl;
00233 mClient->test();
00234 }
00235
00236 void Exchange::test2()
00237 {
00238 kdDebug() << "Entering test2()" << endl;
00239 }
00240 #include "exchange.moc"