exchange.cpp
00001 /* 00002 This file is part of KOrganizer. 00003 Copyright (c) 2002 Jan-Pascal van Best <janpascal@vanbest.org> 00004 00005 This program is free software; you can redistribute it and/or modify 00006 it under the terms of the GNU General Public License as published by 00007 the Free Software Foundation; either version 2 of the License, or 00008 (at your option) any later version. 00009 00010 This program is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 GNU General Public License for more details. 00014 00015 You should have received a copy of the GNU General Public License 00016 along with this program; if not, write to the Free Software 00017 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00018 */ 00019 00020 #include <tqfile.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; // Needed for connecting slots 00046 00047 class ExchangeFactory : public KOrg::PartFactory { 00048 public: 00049 KOrg::Part *create(KOrg::MainWindow *parent, const char *name) 00050 { 00051 kdDebug(5850) << "Registering Exchange Plugin...\n"; 00052 KGlobal::locale()->insertCatalogue("libkpimexchange"); 00053 return new Exchange(parent,name); 00054 } 00055 }; 00056 00057 K_EXPORT_COMPONENT_FACTORY( libkorg_exchange, ExchangeFactory ) 00058 00059 Exchange::Exchange(KOrg::MainWindow *parent, const char *name) : 00060 KOrg::Part(parent,name) 00061 { 00062 setInstance( new KInstance( "korganizer" ) ); 00063 00064 kdDebug(5850) << "Creating Exchange Plugin...\n"; 00065 00066 mAccount = new KPIM::ExchangeAccount( "Calendar/Exchange Plugin" ); 00067 mClient = new KPIM::ExchangeClient( mAccount ); 00068 mClient->setWindow( parent->topLevelWidget() ); 00069 00070 setXMLFile("plugins/exchangeui.rc"); 00071 00072 new KAction(i18n("&Download..."), 0, this, TQT_SLOT(download()), 00073 actionCollection(), "exchange_download"); 00074 00075 KAction *action = new KAction(i18n("&Upload Event..."), 0, this, TQT_SLOT(upload()), 00076 actionCollection(), "exchange_upload"); 00077 TQObject::connect(mainWindow()->view(),TQT_SIGNAL(incidenceSelected(Incidence *)), 00078 this, TQT_SLOT(slotIncidenceSelected(Incidence *))); 00079 action->setEnabled( false ); 00080 TQObject::connect(this,TQT_SIGNAL(enableIncidenceActions(bool)), 00081 action,TQT_SLOT(setEnabled(bool))); 00082 00083 action = new KAction(i18n("De&lete Event"), 0, this, TQT_SLOT(remove()), 00084 actionCollection(), "exchange_delete"); 00085 TQObject::connect(this,TQT_SIGNAL(enableIncidenceActions(bool)), 00086 action,TQT_SLOT(setEnabled(bool))); 00087 action->setEnabled( false ); 00088 00089 new KAction(i18n("&Configure..."), 0, this, TQT_SLOT(configure()), 00090 actionCollection(), "exchange_configure"); 00091 00092 connect( this, TQT_SIGNAL( calendarChanged() ), mainWindow()->view(), TQT_SLOT( updateView() ) ); 00093 connect( this, TQT_SIGNAL( calendarChanged(const TQDate &, const TQDate &)), 00094 mainWindow()->view(), TQT_SLOT(updateView(const TQDate &, const TQDate &)) ); 00095 } 00096 00097 Exchange::~Exchange() 00098 { 00099 kdDebug(5850) << "Exchange Plugin destructor" << endl; 00100 } 00101 00102 TQString Exchange::info() 00103 { 00104 return i18n("This plugin imports and export calendar events from/to a Microsoft Exchange 2000 Server."); 00105 } 00106 00107 TQString Exchange::shortInfo() 00108 { 00109 return i18n("Exchange Plugin"); 00110 } 00111 00112 void Exchange::slotIncidenceSelected( Incidence *incidence ) 00113 { 00114 emit enableIncidenceActions( incidence != 0 ); 00115 } 00116 00117 void Exchange::download() 00118 { 00119 ExchangeDialog dialog( mainWindow()->view()->startDate(), mainWindow()->view()->endDate() ); 00120 00121 if (dialog.exec() != TQDialog::Accepted ) 00122 return; 00123 00124 TQDate start = dialog.m_start->date(); 00125 TQDate end = dialog.m_end->date(); 00126 00127 KCal::Calendar* calendar = mainWindow()->view()->calendar(); 00128 00129 int result = mClient->downloadSynchronous(calendar, start, end, true ); 00130 00131 if ( result == KPIM::ExchangeClient::ResultOK ) 00132 emit calendarChanged(); 00133 else 00134 showError( result, mClient->detailedErrorString() ); 00135 00136 } 00137 00138 void Exchange::upload() 00139 { 00140 kdDebug(5850) << "Called Exchange::upload()" << endl; 00141 00142 Event* event = dynamic_cast<Event *> ( mainWindow()->view()->currentSelection() ); 00143 if ( ! event ) 00144 { 00145 KMessageBox::information( 0L, i18n("Please select an appointment."), i18n("Exchange Plugin") ); 00146 return; 00147 } 00148 if ( KMessageBox::warningContinueCancel( 0L, i18n("Exchange Upload is EXPERIMENTAL, you may lose data on this appointment!"), i18n("Exchange Plugin"), i18n("&Upload") ) 00149 == KMessageBox::Continue ) { 00150 kdDebug(5850) << "Trying to add appointment " << event->summary() << endl; 00151 int result = mClient->uploadSynchronous( event ); 00152 if ( result != KPIM::ExchangeClient::ResultOK ) 00153 showError( result, mClient->detailedErrorString() ); 00154 } 00155 } 00156 00157 void Exchange::remove() 00158 { 00159 kdDebug(5850) << "Called Exchange::remove()" << endl; 00160 00161 Event* event = dynamic_cast<Event *> ( mainWindow()->view()->currentSelection() ); 00162 if ( ! event ) 00163 { 00164 KMessageBox::information( 0L, i18n("Please select an appointment."), i18n("Exchange Plugin") ); 00165 return; 00166 } 00167 00168 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"),"editdelete") ) 00169 == KMessageBox::Continue ) { 00170 kdDebug(5850) << "Trying to delete appointment " << event->summary() << endl; 00171 int result = mClient->removeSynchronous( event ); 00172 00173 if ( result == KPIM::ExchangeClient::ResultOK ) { 00174 mainWindow()->view()->calendar()->deleteEvent( event ); 00175 emit calendarChanged(); 00176 } else 00177 showError( result, mClient->detailedErrorString() ); 00178 } 00179 } 00180 00181 void Exchange::configure() 00182 { 00183 kdDebug(5850) << "Exchange::configure" << endl; 00184 ExchangeConfig dialog( mAccount ); 00185 00186 if (dialog.exec() == TQDialog::Accepted ) 00187 mAccount->save( "Calendar/Exchange Plugin" ); 00188 } 00189 00190 void Exchange::showError( int error, const TQString& moreInfo /* = TQString() */ ) 00191 { 00192 TQString errorText; 00193 switch( error ) { 00194 case KPIM::ExchangeClient::ResultOK: 00195 errorText = i18n( "No Error" ); 00196 break; 00197 case KPIM::ExchangeClient::CommunicationError: 00198 errorText = i18n( "The Exchange server could not be reached or returned an error." ); 00199 break; 00200 case KPIM::ExchangeClient::ServerResponseError: 00201 errorText = i18n( "Server response could not be interpreted." ); 00202 break; 00203 case KPIM::ExchangeClient::IllegalAppointmentError: 00204 errorText = i18n( "Appointment data could not be interpreted." ); 00205 break; 00206 case KPIM::ExchangeClient::NonEventError: 00207 errorText = i18n( "This should not happen: trying to upload wrong type of event." ); 00208 break; 00209 case KPIM::ExchangeClient::EventWriteError: 00210 errorText = i18n( "An error occurred trying to write an appointment to the server." ); 00211 break; 00212 case KPIM::ExchangeClient::DeleteUnknownEventError: 00213 errorText = i18n( "Trying to delete an event that is not present on the server." ); 00214 break; 00215 case KPIM::ExchangeClient::UnknownError: 00216 default: 00217 errorText = i18n( "Unknown Error" ); 00218 } 00219 00220 if ( error != KPIM::ExchangeClient::ResultOK ) { 00221 if ( moreInfo.isNull() ) 00222 KMessageBox::error( mainWindow()->topLevelWidget(), errorText, i18n( "Exchange Plugin" ) ); 00223 else 00224 KMessageBox::detailedError( mainWindow()->topLevelWidget(), errorText, moreInfo, i18n( "Exchange Plugin" ) ); 00225 } 00226 } 00227 00228 void Exchange::test() 00229 { 00230 kdDebug(5850) << "Entering test()" << endl; 00231 mClient->test(); 00232 } 00233 00234 void Exchange::test2() 00235 { 00236 kdDebug(5850) << "Entering test2()" << endl; 00237 } 00238 #include "exchange.moc"