kontact

summarywidget.cpp

00001 /*
00002     This file is part of Kontact.
00003     Copyright (c) 2003 Tobias Koenig <tokoe@kde.org>
00004     Copyright (C) 2004 Reinhold Kainhofer <reinhold@kainhofer.com>
00005 
00006     This program is free software; you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or
00009     (at your option) any later version.
00010 
00011     This program 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
00014     GNU General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this program; if not, write to the Free Software
00018     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019 
00020     As a special exception, permission is given to link this program
00021     with any edition of Qt, and distribute the resulting executable,
00022     without including the source code for Qt in the source distribution.
00023 */
00024 
00025 #include <qimage.h>
00026 #include <qlabel.h>
00027 #include <qlayout.h>
00028 #include <qtooltip.h>
00029 #include <qfile.h>
00030 #include <qlabel.h>
00031 #include <qtextedit.h>
00032 #include <qvbox.h>
00033 
00034 #include <dcopclient.h>
00035 #include <dcopref.h>
00036 #include <kapplication.h>
00037 #include <kdebug.h>
00038 #include <kglobal.h>
00039 #include <kglobalsettings.h>
00040 #include <kiconloader.h>
00041 #include <klocale.h>
00042 #include <kurllabel.h>
00043 #include <kdialogbase.h>
00044 #include <kmessagebox.h>
00045 
00046 #include "pilotDaemonDCOP_stub.h"
00047 
00048 #include <ktextedit.h>
00049 
00050 #include "summarywidget.h"
00051 
00052 SummaryWidget::SummaryWidget( QWidget *parent, const char *name )
00053   : Kontact::Summary( parent, name ),
00054     DCOPObject( "KPilotSummaryWidget" ),
00055     mDCOPSuccess( false ),
00056     mStartedDaemon( false ),
00057     mShouldStopDaemon( true )
00058 {
00059   mLayout = new QGridLayout( this, 2, 3, 3, 3 );
00060 
00061   int row=0;
00062   QPixmap icon = KGlobal::iconLoader()->loadIcon( "kpilot", KIcon::Desktop, KIcon::SizeMedium );
00063   QWidget *header = createHeader( this, icon, i18n( "KPilot Information" ) );
00064   mLayout->addMultiCellWidget( header, row,row, 0,3 );
00065 
00066   // Last sync information
00067   row++;
00068   mSyncTimeTextLabel = new QLabel( i18n( "<i>Last sync:</i>" ), this);
00069   mLayout->addWidget( mSyncTimeTextLabel, row, 0 );
00070   mSyncTimeLabel = new QLabel( i18n( "No information available" ), this );
00071   mLayout->addWidget( mSyncTimeLabel, row, 1 );
00072   mShowSyncLogLabel = new KURLLabel( "", i18n( "[View Sync Log]" ), this );
00073   mLayout->addWidget( mShowSyncLogLabel, row, 3 );
00074   connect( mShowSyncLogLabel, SIGNAL( leftClickedURL( const QString& ) ),
00075     this, SLOT( showSyncLog( const QString& ) ) );
00076 
00077   // User
00078   row++;
00079   mPilotUserTextLabel = new QLabel( i18n( "<i>User:</i>" ), this);
00080   mLayout->addWidget( mPilotUserTextLabel, row, 0);
00081   mPilotUserLabel = new QLabel( i18n( "Unknown" ), this );
00082   mLayout->addMultiCellWidget( mPilotUserLabel, row, row, 1, 3 );
00083 
00084   // Device information
00085   row++;
00086   mPilotDeviceTextLabel = new QLabel( i18n( "<i>Device:</i>" ), this);
00087   mLayout->addWidget( mPilotDeviceTextLabel, row, 0 );
00088   mPilotDeviceLabel = new QLabel( i18n( "Unknown" ), this );
00089   mLayout->addMultiCellWidget( mPilotDeviceLabel, row, row, 1, 3 );
00090 
00091   // Status
00092   row++;
00093   mDaemonStatusTextLabel = new QLabel( i18n( "<i>Status:</i>" ), this);
00094   mLayout->addWidget( mDaemonStatusTextLabel, row, 0 );
00095   mDaemonStatusLabel = new QLabel( i18n( "No communication with the daemon possible" ), this );
00096   mLayout->addMultiCellWidget( mDaemonStatusLabel, row, row, 1, 3 );
00097 
00098   // Conduits:
00099   row++;
00100   mConduitsTextLabel = new QLabel( i18n( "<i>Conduits:</i>" ), this );
00101   mConduitsTextLabel->setAlignment( AlignAuto | AlignTop | ExpandTabs );
00102   mLayout->addWidget( mConduitsTextLabel, row, 0 );
00103   mConduitsLabel = new QLabel( i18n( "No information available" ), this );
00104   mConduitsLabel->setAlignment( mConduitsLabel->alignment() | Qt::WordBreak );
00105   mLayout->addMultiCellWidget( mConduitsLabel, row, row, 1, 3 );
00106 
00107   // widgets shown if kpilotDaemon is not running
00108   row++;
00109   mNoConnectionLabel = new QLabel( i18n( "KPilot is currently not running." ), this );
00110   mLayout->addMultiCellWidget( mNoConnectionLabel, row, row, 1, 2 );
00111   mNoConnectionStartLabel = new KURLLabel( "", i18n( "[Start KPilot]" ), this );
00112   mLayout->addWidget( mNoConnectionStartLabel, row, 3 );
00113   connect( mNoConnectionStartLabel, SIGNAL( leftClickedURL( const QString& ) ),
00114            this, SLOT( startKPilot() ) );
00115 
00116   if ( !kapp->dcopClient()->isApplicationRegistered( "kpilotDaemon" ) ) {
00117     startKPilot();
00118   }
00119 
00120   connectDCOPSignal( 0, 0, "kpilotDaemonStatusDetails(QDateTime,QString,QStringList,QString,QString,QString,bool)",
00121                      "receiveDaemonStatusDetails(QDateTime,QString,QStringList,QString,QString,QString,bool)", false );
00122     connect( kapp->dcopClient(), SIGNAL( applicationRemoved( const QCString & ) ), SLOT( slotAppRemoved( const QCString& ) ) );
00123 }
00124 
00125 SummaryWidget::~SummaryWidget()
00126 {
00127   if ( mStartedDaemon && mShouldStopDaemon ) {
00128     PilotDaemonDCOP_stub dcopToDaemon( "kpilotDaemon", "KPilotDaemonIface" );
00129     dcopToDaemon.quitNow(); // ASYNC, always succeeds.
00130   }
00131 }
00132 
00133 QStringList SummaryWidget::configModules() const
00134 {
00135   QStringList modules;
00136   modules << "kpilot_config.desktop";
00137   return modules;
00138 }
00139 
00140 void SummaryWidget::receiveDaemonStatusDetails(QDateTime lastSyncTime, QString status, QStringList conduits, QString logFileName, QString userName, QString pilotDevice, bool killOnExit )
00141 {
00142   mDCOPSuccess = true;
00143   mLastSyncTime = lastSyncTime;
00144   mDaemonStatus = status;
00145   mConduits = conduits;
00146   mSyncLog = logFileName;
00147   mUserName = userName;
00148   mPilotDevice = pilotDevice;
00149   mShouldStopDaemon = killOnExit;
00150   updateView();
00151 }
00152 
00153 void SummaryWidget::updateView()
00154 {
00155   if ( mDCOPSuccess ) {
00156     if ( mLastSyncTime.isValid() ) {
00157       mSyncTimeLabel->setText( mLastSyncTime.toString(Qt::LocalDate) );
00158     } else {
00159       mSyncTimeLabel->setText( i18n( "No information available" ) );
00160     }
00161     if ( !mSyncLog.isEmpty() ) {
00162       mShowSyncLogLabel->setEnabled( true );
00163       mShowSyncLogLabel->setURL( mSyncLog );
00164     } else {
00165       mShowSyncLogLabel->setEnabled( false );
00166     }
00167     mPilotUserLabel->setText( mUserName.isEmpty() ? i18n( "unknown" ) : mUserName );
00168     mPilotDeviceLabel->setText( mPilotDevice.isEmpty() ? i18n( "unknown" ) : mPilotDevice );
00169     mDaemonStatusLabel->setText( mDaemonStatus );
00170     mConduitsLabel->setText( mConduits.join( ", " ) );
00171   } else {
00172     mSyncTimeLabel->setText( i18n( "No information available (Daemon not running?)" ) );
00173     mShowSyncLogLabel->setEnabled( false );
00174     mPilotUserLabel->setText( i18n( "unknown" ) );
00175     mPilotDeviceLabel->setText( i18n( "unknown" ) );
00176     mDaemonStatusLabel->setText( i18n( "No communication with the daemon possible" ) );
00177     mConduitsLabel->setText( i18n( "No information available" ) );
00178   }
00179 
00180   mSyncTimeTextLabel->setShown( mDCOPSuccess );
00181   mSyncTimeLabel->setShown( mDCOPSuccess );
00182   mShowSyncLogLabel->setShown( mDCOPSuccess );
00183   mPilotUserTextLabel->setShown( mDCOPSuccess );
00184   mPilotUserLabel->setShown( mDCOPSuccess );
00185   mPilotDeviceTextLabel->setShown( mDCOPSuccess );
00186   mPilotDeviceLabel->setShown( mDCOPSuccess );
00187   mDaemonStatusTextLabel->setShown( mDCOPSuccess );
00188   mDaemonStatusLabel->setShown( mDCOPSuccess );
00189   mConduitsTextLabel->setShown( mDCOPSuccess );
00190   mConduitsLabel->setShown( mDCOPSuccess );
00191   mNoConnectionLabel->setShown( !mDCOPSuccess );
00192   mNoConnectionStartLabel->setShown( !mDCOPSuccess );
00193 }
00194 
00195 void SummaryWidget::showSyncLog( const QString &filename )
00196 {
00197   KDialogBase dlg( this, 0, true, QString::null, KDialogBase::Ok, KDialogBase::Ok );
00198   dlg.setCaption( i18n( "KPilot HotSync Log" ) );
00199 
00200   QTextEdit *edit = new QTextEdit( dlg.makeVBoxMainWidget() );
00201   edit->setReadOnly( true );
00202 
00203   QFile f(filename);
00204   if ( !f.open( IO_ReadOnly ) ) {
00205     KMessageBox::error( this, i18n( "Unable to open Hotsync log %1." ).arg( filename ) );
00206     return;
00207   }
00208 
00209   QTextStream s( &f );
00210   while ( !s.eof() )
00211     edit->append( s.readLine() );
00212 
00213   edit->moveCursor( QTextEdit::MoveHome, false );
00214 
00215   f.close();
00216 
00217   dlg.setInitialSize( QSize( 400, 350 ) );
00218   dlg.exec();
00219 }
00220 
00221 void SummaryWidget::startKPilot()
00222 {
00223   QString error;
00224   QCString appID;
00225   if ( !KApplication::kdeinitExec( "kpilotDaemon", QString( "--fail-silently" ) ) ) {
00226     kdDebug(5602) << "No service available..." << endl;
00227     mStartedDaemon = true;
00228   }
00229 }
00230 
00231 void SummaryWidget::slotAppRemoved( const QCString & appId )
00232 {
00233   if ( appId == "kpilotDaemon" )
00234   {
00235     mDCOPSuccess = false;
00236     updateView();
00237   }
00238 }
00239 
00240 
00241 #include "summarywidget.moc"
00242 
KDE Home | KDE Accessibility Home | Description of Access Keys