kitchensync

debugger.cpp

00001 /*
00002     This file is part of KitchenSync.
00003 
00004     Copyright (c) 2003 Cornelius Schumacher <schumacher@kde.org>
00005 
00006     This library is free software; you can redistribute it and/or
00007     modify it under the terms of the GNU Library General Public
00008     License as published by the Free Software Foundation; either
00009     version 2 of the License, or (at your option) any later version.
00010 
00011     This library is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014     Library General Public License for more details.
00015 
00016     You should have received a copy of the GNU Library General Public License
00017     along with this library; see the file COPYING.LIB.  If not, write to
00018     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00019     Boston, MA 02110-1301, USA.
00020 */
00021 #include "debugger.h"
00022 
00023 #include <konnector.h>
00024 #include <konnectormanager.h>
00025 #include <konnectorinfo.h>
00026 #include <mainwindow.h>
00027 #include <calendarsyncee.h>
00028 
00029 #include <kaboutdata.h>
00030 #include <kiconloader.h>
00031 #include <kparts/genericfactory.h>
00032 #include <kmessagebox.h>
00033 #include <kdialog.h>
00034 #include <kdialogbase.h>
00035 #include <kresources/configdialog.h>
00036 
00037 #include <qlabel.h>
00038 #include <qlistview.h>
00039 #include <qcombobox.h>
00040 #include <qpushbutton.h>
00041 #include <qtextview.h>
00042 #include <qlayout.h>
00043 #include <qdatetime.h>
00044 #include <qcheckbox.h>
00045 #include <qvbox.h>
00046 
00047 using namespace KCal;
00048 using namespace KSync;
00049 
00050 typedef KParts::GenericFactory< Debugger> DebuggerFactory;
00051 K_EXPORT_COMPONENT_FACTORY( libksync_debugger, DebuggerFactory )
00052 
00053 
00054 class CustomComboBox : public QComboBox
00055 {
00056   public:
00057     CustomComboBox( QWidget *parent, const char *name = 0 )
00058       : QComboBox( parent, name ) {}
00059 
00060     void insertItem( Konnector *k, const QString &text )
00061     {
00062       QComboBox::insertItem( text );
00063       mKonnectors.append( k );
00064     }
00065 
00066     Konnector *currentKonnector()
00067     {
00068       return mKonnectors.at( currentItem() );
00069     }
00070 
00071   private:
00072     QPtrList<Konnector> mKonnectors;
00073 };
00074 
00075 
00076 Debugger::Debugger( QWidget *parent, const char *name,
00077                     QObject *, const char *,const QStringList & )
00078   : ActionPart( parent, name ), m_widget( 0 ),
00079     mCalendar( QString::fromLatin1( "UTC" ) )
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( KSync::Konnector * ) ),
00222              SLOT( slotReceiveData( KSync::Konnector * ) ) );
00223     mConnectedKonnectors.append( k );
00224   }
00225 
00226   return k;
00227 }
00228 
00229 void Debugger::readSyncees()
00230 {
00231   logMessage( i18n("Read Syncees") );
00232 
00233   Konnector *k = currentKonnector();
00234 
00235   if ( k ) k->readSyncees();
00236 }
00237 
00238 void Debugger::slotReceiveData( Konnector *k )
00239 {
00240   logMessage( i18n("Got Syncee list from Konnector at address %1").arg( (long)k ) );
00241   mSynceeList = k->syncees();
00242 
00243   SynceeList::ConstIterator it;
00244   for( it = mSynceeList.begin(); it != mSynceeList.end(); ++it ) {
00245     Syncee *syncee = *it;
00246     logMessage( i18n("Got Syncee of type %1").arg( syncee->type() ) );
00247     SyncEntry *syncEntry;
00248     int i = 0;
00249     for( syncEntry = syncee->firstEntry(); syncEntry;
00250          syncEntry = syncee->nextEntry() ) {
00251       logMessage( " " + syncEntry->id() + ": " + syncEntry->name() );
00252       ++i;
00253     }
00254     if ( i == 0 ) logMessage( i18n(" Empty") );
00255   }
00256 }
00257 
00258 void Debugger::writeSyncees()
00259 {
00260   KDialogBase dialog( m_widget, 0, true, i18n("Select Syncees"),
00261                       KDialogBase::Ok | KDialogBase::Cancel );
00262   QVBox *topBox = dialog.makeVBoxMainWidget();
00263   QCheckBox mEventCheck( i18n("Events"), topBox );
00264   mEventCheck.setChecked( true );
00265   QCheckBox mAddresseeCheck( i18n("Addressees"), topBox );
00266   mAddresseeCheck.setChecked( true );
00267   int result = dialog.exec();
00268   if ( result == QDialog::Accepted ) {
00269     logMessage( i18n("Write Syncees") );
00270     if ( mEventCheck.isChecked() ) {
00271       logMessage( i18n("Write events") );
00272       CalendarSyncee *calendarSyncee = mSynceeList.calendarSyncee();
00273       if ( !calendarSyncee ) {
00274         logMessage( i18n("No calendar syncee.") );
00275       } else {
00276         Calendar *cal = calendarSyncee->calendar();
00277         Event *e = new Event();
00278         e->setSummary( "Debugger was here (" + QTime::currentTime().toString()
00279                        + ")" );
00280         cal->addEvent( e );
00281       }
00282     }
00283     if ( mAddresseeCheck.isChecked() ) {
00284       logMessage( i18n("Write Addressees") );
00285       kdDebug() << "To be implemented: Create debugger addressee syncee."
00286                 << endl;
00287     }
00288     kdDebug() << "Send data" << endl;
00289     Konnector *k = currentKonnector();
00290     if ( k ) k->writeSyncees();
00291   }
00292 }
00293 
00294 void Debugger::connectDevice()
00295 {
00296   logMessage( i18n("Connecting to Device.") );
00297 
00298   Konnector *k = currentKonnector();
00299   if ( k ) k->connectDevice();
00300 }
00301 
00302 void Debugger::disconnectDevice()
00303 {
00304   logMessage( i18n("Disconnecting from Device.") );
00305 
00306   Konnector *k = currentKonnector();
00307   if ( k ) k->disconnectDevice();
00308 }
00309 
00310 void Debugger::logMessage( const QString &message )
00311 {
00312   QString text = "<b>" + QTime::currentTime().toString() + "</b>: ";
00313   text += message;
00314 
00315   kdDebug() << "LOG: " << text << endl;
00316 
00317   mLogView->append( text );
00318 }
00319 
00320 void Debugger::executeAction()
00321 {
00322   logMessage( i18n("actionSync()") );
00323 }
00324 
00325 #include "debugger.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys