00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "debugger.h"
00022
00023 #include <konnector.h>
00024 #include <configwidget.h>
00025 #include <konnectormanager.h>
00026 #include <konnectorinfo.h>
00027 #include <mainwindow.h>
00028 #include <calendarsyncee.h>
00029
00030 #include <kaboutdata.h>
00031 #include <kiconloader.h>
00032 #include <kparts/genericfactory.h>
00033 #include <kmessagebox.h>
00034 #include <kdialog.h>
00035 #include <kdialogbase.h>
00036 #include <kresources/configdialog.h>
00037
00038 #include <qlabel.h>
00039 #include <qlistview.h>
00040 #include <qcombobox.h>
00041 #include <qpushbutton.h>
00042 #include <qtextview.h>
00043 #include <qlayout.h>
00044 #include <qdatetime.h>
00045 #include <qcheckbox.h>
00046 #include <qvbox.h>
00047
00048 using namespace KCal;
00049 using namespace KSync;
00050
00051 typedef KParts::GenericFactory< Debugger> DebuggerFactory;
00052 K_EXPORT_COMPONENT_FACTORY( libksync_debugger, DebuggerFactory )
00053
00054
00055 class CustomComboBox : public QComboBox
00056 {
00057 public:
00058 CustomComboBox( QWidget *parent, const char *name = 0 )
00059 : QComboBox( parent, name ) {}
00060
00061 void insertItem( Konnector *k, const QString &text )
00062 {
00063 QComboBox::insertItem( text );
00064 mKonnectors.append( k );
00065 }
00066
00067 Konnector *currentKonnector()
00068 {
00069 return mKonnectors.at( currentItem() );
00070 }
00071
00072 private:
00073 QPtrList<Konnector> mKonnectors;
00074 };
00075
00076
00077 Debugger::Debugger( QWidget *parent, const char *name,
00078 QObject *, const char *,const QStringList & )
00079 : ActionPart( parent, name ), m_widget( 0 )
00080 {
00081 m_pixmap = KGlobal::iconLoader()->loadIcon("package_settings", KIcon::Desktop, 48 );
00082
00083 Event *event = new Event;
00084 event->setSummary( "Debugger Event" );
00085 mCalendar.addEvent( event );
00086 }
00087
00088 KAboutData *Debugger::createAboutData()
00089 {
00090 return new KAboutData("KSyncDebugger", I18N_NOOP("Sync Debugger Part"), "0.0" );
00091 }
00092
00093 Debugger::~Debugger()
00094 {
00095 delete m_widget;
00096 }
00097
00098 QString Debugger::type() const
00099 {
00100 return QString::fromLatin1("debugger");
00101 }
00102
00103 QString Debugger::title() const
00104 {
00105 return i18n("Konnector Debugger");
00106 }
00107
00108 QString Debugger::description() const
00109 {
00110 return i18n("Debugger for Konnectors");
00111 }
00112
00113 QPixmap *Debugger::pixmap()
00114 {
00115 return &m_pixmap;
00116 }
00117
00118 QString Debugger::iconName() const
00119 {
00120 return QString::fromLatin1("kcmsystem");
00121 }
00122
00123 bool Debugger::hasGui() const
00124 {
00125 return true;
00126 }
00127
00128 QWidget *Debugger::widget()
00129 {
00130 if( !m_widget ) {
00131 m_widget = new QWidget;
00132 QBoxLayout *topLayout = new QVBoxLayout( m_widget );
00133 topLayout->setSpacing( KDialog::spacingHint() );
00134 topLayout->setMargin( KDialog::spacingHint() );
00135
00136
00137 QBoxLayout *konnectorLayout = new QHBoxLayout( topLayout );
00138
00139 konnectorLayout->addWidget( new QLabel( i18n("Current Konnector:" ),
00140 m_widget ) );
00141
00142 mKonnectorCombo = new CustomComboBox( m_widget );
00143 konnectorLayout->addWidget( mKonnectorCombo );
00144
00145 updateKonnectors();
00146
00147 konnectorLayout->addStretch();
00148
00149
00150 QBoxLayout *commandLayout = new QHBoxLayout( topLayout );
00151
00152 QPushButton *button = new QPushButton( "Configure...", m_widget );
00153 connect( button, SIGNAL( clicked() ), SLOT( configureKonnector() ) );
00154 commandLayout->addWidget( button );
00155
00156 button = new QPushButton( "Connect Device", m_widget );
00157 connect( button, SIGNAL( clicked() ), SLOT( connectDevice() ) );
00158 commandLayout->addWidget( button );
00159
00160 button = new QPushButton( "Read Syncees", m_widget );
00161 connect( button, SIGNAL( clicked() ), SLOT( readSyncees() ) );
00162 commandLayout->addWidget( button );
00163
00164 button = new QPushButton( "Write Syncees", m_widget );
00165 connect( button, SIGNAL( clicked() ), SLOT( writeSyncees() ) );
00166 commandLayout->addWidget( button );
00167
00168 button = new QPushButton( "Disconnect Device", m_widget );
00169 connect( button, SIGNAL( clicked() ), SLOT( disconnectDevice() ) );
00170 commandLayout->addWidget( button );
00171
00172
00173 commandLayout->addStretch();
00174
00175
00176 mLogView = new QTextView( m_widget );
00177 mLogView->setTextFormat( LogText );
00178 topLayout->addWidget( mLogView );
00179
00180 logMessage( i18n("Ready.") );
00181 }
00182 return m_widget;
00183 }
00184
00185 void Debugger::updateKonnectors()
00186 {
00187 kdDebug() << "Debugger::updateKonnectors()" << endl;
00188
00189 KRES::Manager<Konnector> *manager = KonnectorManager::self();
00190
00191 KRES::Manager<Konnector>::ActiveIterator it;
00192 for( it = manager->activeBegin(); it != manager->activeEnd(); ++it ) {
00193 kdDebug() << "Konnector: id: " << (*it)->identifier() << endl;
00194 mKonnectorCombo->insertItem( *it, (*it)->resourceName() );
00195 }
00196 }
00197
00198 void Debugger::configureKonnector()
00199 {
00200 Konnector *k = currentKonnector();
00201 if ( !k ) {
00202 KMessageBox::sorry( m_widget, i18n("Konnector isn't loaded" ) );
00203 } else {
00204 KRES::ConfigDialog *dialog = new KRES::ConfigDialog( m_widget, "konnector",
00205 k );
00206 if ( !dialog ) {
00207 KMessageBox::sorry( m_widget,
00208 i18n("No configuration widget available.") );
00209 } else {
00210 dialog->show();
00211 }
00212 }
00213 }
00214
00215 Konnector *Debugger::currentKonnector()
00216 {
00217 Konnector *k = mKonnectorCombo->currentKonnector();
00218
00219 if ( mConnectedKonnectors.find( k ) < 0 ) {
00220 kdDebug() << "Connect Konnector" << endl;
00221 connect( k, SIGNAL( synceesRead( Konnector * ) ),
00222 SLOT( slotReceiveData( Konnector * ) ) );
00223 connect( k, SIGNAL( sig_progress( Konnector *, const Progress & ) ),
00224 SLOT( slotProgress( Konnector *, const Progress & ) ) );
00225 connect( k, SIGNAL( sig_error( Konnector *, const Error & ) ),
00226 SLOT( slotError( Konnector *, const Error & ) ) );
00227 mConnectedKonnectors.append( k );
00228 }
00229
00230 return k;
00231 }
00232
00233 void Debugger::readSyncees()
00234 {
00235 logMessage( i18n("Read Syncees") );
00236
00237 Konnector *k = currentKonnector();
00238
00239 if ( k ) k->readSyncees();
00240 }
00241
00242 void Debugger::slotReceiveData( Konnector *k )
00243 {
00244 logMessage( i18n("Got Syncee list from Konnector at address %1").arg( (long)k ) );
00245 mSynceeList = k->syncees();
00246
00247 SynceeList::ConstIterator it;
00248 for( it = mSynceeList.begin(); it != mSynceeList.end(); ++it ) {
00249 Syncee *syncee = *it;
00250 logMessage( i18n("Got Syncee of type %1").arg( syncee->type() ) );
00251 SyncEntry *syncEntry;
00252 int i = 0;
00253 for( syncEntry = syncee->firstEntry(); syncEntry;
00254 syncEntry = syncee->nextEntry() ) {
00255 logMessage( " " + syncEntry->id() + ": " + syncEntry->name() );
00256 ++i;
00257 }
00258 if ( i == 0 ) logMessage( i18n(" Empty") );
00259 }
00260 }
00261
00262 void Debugger::slotProgress( Konnector *k, const Progress &p )
00263 {
00264 logMessage( i18n("Got Progress from Konnector at address %1: %2").arg( (long)k ).arg( p.text() ) );
00265 }
00266
00267 void Debugger::slotError( Konnector *k, const Error &e )
00268 {
00269 logMessage( i18n("Got Progress from Konnector at address %1: %2").arg( (long)k ).arg( e.text() ) );
00270 }
00271
00272 void Debugger::writeSyncees()
00273 {
00274 KDialogBase dialog( m_widget, 0, true, i18n("Select Syncees"),
00275 KDialogBase::Ok | KDialogBase::Cancel );
00276 QVBox *topBox = dialog.makeVBoxMainWidget();
00277 QCheckBox mEventCheck( i18n("Events"), topBox );
00278 mEventCheck.setChecked( true );
00279 QCheckBox mAddresseeCheck( i18n("Addressees"), topBox );
00280 mAddresseeCheck.setChecked( true );
00281 int result = dialog.exec();
00282 if ( result == QDialog::Accepted ) {
00283 logMessage( i18n("Write Syncees") );
00284 if ( mEventCheck.isChecked() ) {
00285 logMessage( i18n("Write events") );
00286 CalendarSyncee *calendarSyncee = mSynceeList.calendarSyncee();
00287 if ( !calendarSyncee ) {
00288 logMessage( i18n("No calendar syncee.") );
00289 } else {
00290 Calendar *cal = calendarSyncee->calendar();
00291 Event *e = new Event();
00292 e->setSummary( "Debugger was here (" + QTime::currentTime().toString()
00293 + ")" );
00294 cal->addEvent( e );
00295 }
00296 }
00297 if ( mAddresseeCheck.isChecked() ) {
00298 logMessage( i18n("Write Addressees") );
00299 kdDebug() << "To be implemented: Create debugger addressee syncee."
00300 << endl;
00301 }
00302 kdDebug() << "Send data" << endl;
00303 Konnector *k = currentKonnector();
00304 if ( k ) k->writeSyncees();
00305 }
00306 }
00307
00308 void Debugger::connectDevice()
00309 {
00310 logMessage( i18n("Connecting to Device.") );
00311
00312 Konnector *k = currentKonnector();
00313 if ( k ) k->connectDevice();
00314 }
00315
00316 void Debugger::disconnectDevice()
00317 {
00318 logMessage( i18n("Disconnecting from Device.") );
00319
00320 Konnector *k = currentKonnector();
00321 if ( k ) k->disconnectDevice();
00322 }
00323
00324 void Debugger::logMessage( const QString &message )
00325 {
00326 QString text = "<b>" + QTime::currentTime().toString() + "</b>: ";
00327 text += message;
00328
00329 kdDebug() << "LOG: " << text << endl;
00330
00331 mLogView->append( text );
00332 }
00333
00334 void Debugger::executeAction()
00335 {
00336 logMessage( i18n("actionSync()") );
00337 }
00338
00339 #include "debugger.moc"