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 TQt, and distribute the resulting executable,
00022     without including the source code for TQt in the source distribution.
00023 */
00024 
00025 #include <tqimage.h>
00026 #include <tqlabel.h>
00027 #include <tqlayout.h>
00028 #include <tqtooltip.h>
00029 #include <tqfile.h>
00030 #include <tqlabel.h>
00031 #include <tqtextedit.h>
00032 #include <tqvbox.h>
00033 
00034 #include <dcopclient.h>
00035 #include <dcopref.h>
00036 #include <tdeapplication.h>
00037 #include <kdebug.h>
00038 #include <tdeglobal.h>
00039 #include <tdeglobalsettings.h>
00040 #include <kiconloader.h>
00041 #include <tdelocale.h>
00042 #include <kurllabel.h>
00043 #include <kdialogbase.h>
00044 #include <tdemessagebox.h>
00045 
00046 #include "pilotDaemonDCOP_stub.h"
00047 
00048 #include <ktextedit.h>
00049 
00050 #include "summarywidget.h"
00051 
00052 SummaryWidget::SummaryWidget( TQWidget *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 TQGridLayout( this, 2, 3, 3, 3 );
00060 
00061   int row=0;
00062   TQPixmap icon = TDEGlobal::iconLoader()->loadIcon( "kpilot", TDEIcon::Desktop, TDEIcon::SizeMedium );
00063   TQWidget *header = createHeader( this, icon, i18n( "KPilot Configuration" ) );
00064   mLayout->addMultiCellWidget( header, row,row, 0,3 );
00065 
00066   // Last sync information
00067   row++;
00068   mSyncTimeTextLabel = new TQLabel( i18n( "<i>Last sync:</i>" ), this);
00069   mLayout->addWidget( mSyncTimeTextLabel, row, 0 );
00070   mSyncTimeLabel = new TQLabel( 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, TQT_SIGNAL( leftClickedURL( const TQString& ) ),
00075     this, TQT_SLOT( showSyncLog( const TQString& ) ) );
00076 
00077   // User
00078   row++;
00079   mPilotUserTextLabel = new TQLabel( i18n( "<i>User:</i>" ), this);
00080   mLayout->addWidget( mPilotUserTextLabel, row, 0);
00081   mPilotUserLabel = new TQLabel( i18n( "Unknown" ), this );
00082   mLayout->addMultiCellWidget( mPilotUserLabel, row, row, 1, 3 );
00083 
00084   // Device information
00085   row++;
00086   mPilotDeviceTextLabel = new TQLabel( i18n( "<i>Device:</i>" ), this);
00087   mLayout->addWidget( mPilotDeviceTextLabel, row, 0 );
00088   mPilotDeviceLabel = new TQLabel( i18n( "Unknown" ), this );
00089   mLayout->addMultiCellWidget( mPilotDeviceLabel, row, row, 1, 3 );
00090 
00091   // Status
00092   row++;
00093   mDaemonStatusTextLabel = new TQLabel( i18n( "<i>Status:</i>" ), this);
00094   mLayout->addWidget( mDaemonStatusTextLabel, row, 0 );
00095   mDaemonStatusLabel = new TQLabel( i18n( "No communication with the daemon possible" ), this );
00096   mLayout->addMultiCellWidget( mDaemonStatusLabel, row, row, 1, 3 );
00097 
00098   // Conduits:
00099   row++;
00100   mConduitsTextLabel = new TQLabel( i18n( "<i>Conduits:</i>" ), this );
00101   mConduitsTextLabel->setAlignment( AlignAuto | AlignTop | ExpandTabs );
00102   mLayout->addWidget( mConduitsTextLabel, row, 0 );
00103   mConduitsLabel = new TQLabel( i18n( "No information available" ), this );
00104   mConduitsLabel->setAlignment( mConduitsLabel->alignment() | TQt::WordBreak );
00105   mLayout->addMultiCellWidget( mConduitsLabel, row, row, 1, 3 );
00106 
00107   // widgets shown if kpilotDaemon is not running
00108   row++;
00109   mNoConnectionLabel = new TQLabel( 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, TQT_SIGNAL( leftClickedURL( const TQString& ) ),
00114            this, TQT_SLOT( startKPilot() ) );
00115 
00116   if ( !kapp->dcopClient()->isApplicationRegistered( "kpilotDaemon" ) ) {
00117     startKPilot();
00118   }
00119 
00120   connectDCOPSignal( 0, 0, "kpilotDaemonStatusDetails(TQDateTime,TQString,TQStringList,TQString,TQString,TQString,bool)",
00121                      "receiveDaemonStatusDetails(TQDateTime,TQString,TQStringList,TQString,TQString,TQString,bool)", false );
00122     connect( kapp->dcopClient(), TQT_SIGNAL( applicationRemoved( const TQCString & ) ), TQT_SLOT( slotAppRemoved( const TQCString& ) ) );
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 TQStringList SummaryWidget::configModules() const
00134 {
00135   TQStringList modules;
00136   modules << "kpilot_config.desktop";
00137   return modules;
00138 }
00139 
00140 void SummaryWidget::receiveDaemonStatusDetails(TQDateTime lastSyncTime, TQString status, TQStringList conduits, TQString logFileName, TQString userName, TQString 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(TQt::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 TQString &filename )
00196 {
00197   KDialogBase dlg( this, 0, true, TQString(), KDialogBase::Ok, KDialogBase::Ok );
00198   dlg.setCaption( i18n( "KPilot HotSync Log" ) );
00199 
00200   TQTextEdit *edit = new TQTextEdit( dlg.makeVBoxMainWidget() );
00201   edit->setReadOnly( true );
00202 
00203   TQFile 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   TQTextStream s( &f );
00210   while ( !s.eof() )
00211     edit->append( s.readLine() );
00212 
00213   edit->moveCursor( TQTextEdit::MoveHome, false );
00214 
00215   f.close();
00216 
00217   dlg.setInitialSize( TQSize( 400, 350 ) );
00218   dlg.exec();
00219 }
00220 
00221 void SummaryWidget::startKPilot()
00222 {
00223   TQString error;
00224   TQCString appID;
00225   if ( !TDEApplication::tdeinitExec( "kpilotDaemon", TQString( "--fail-silently" ) ) ) {
00226     kdDebug(5602) << "No service available..." << endl;
00227     mStartedDaemon = true;
00228   }
00229 }
00230 
00231 void SummaryWidget::slotAppRemoved( const TQCString & appId )
00232 {
00233   if ( appId == "kpilotDaemon" )
00234   {
00235     mDCOPSuccess = false;
00236     updateView();
00237   }
00238 }
00239 
00240 
00241 #include "summarywidget.moc"
00242