kpilot/summarywidget.cpp
1 /*
2  This file is part of Kontact.
3  Copyright (c) 2003 Tobias Koenig <tokoe@kde.org>
4  Copyright (C) 2004 Reinhold Kainhofer <reinhold@kainhofer.com>
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 
20  As a special exception, permission is given to link this program
21  with any edition of TQt, and distribute the resulting executable,
22  without including the source code for TQt in the source distribution.
23 */
24 
25 #include <tqimage.h>
26 #include <tqlabel.h>
27 #include <tqlayout.h>
28 #include <tqtooltip.h>
29 #include <tqfile.h>
30 #include <tqlabel.h>
31 #include <tqtextedit.h>
32 #include <tqvbox.h>
33 
34 #include <dcopclient.h>
35 #include <dcopref.h>
36 #include <tdeapplication.h>
37 #include <kdebug.h>
38 #include <tdeglobal.h>
39 #include <tdeglobalsettings.h>
40 #include <kiconloader.h>
41 #include <tdelocale.h>
42 #include <kurllabel.h>
43 #include <kdialogbase.h>
44 #include <tdemessagebox.h>
45 
46 #include "pilotDaemonDCOP_stub.h"
47 
48 #include <ktextedit.h>
49 
50 #include "summarywidget.h"
51 
52 SummaryWidget::SummaryWidget( TQWidget *parent, const char *name )
53  : Kontact::Summary( parent, name ),
54  DCOPObject( "KPilotSummaryWidget" ),
55  mDCOPSuccess( false ),
56  mStartedDaemon( false ),
57  mShouldStopDaemon( true )
58 {
59  mLayout = new TQGridLayout( this, 2, 3, 3, 3 );
60 
61  int row=0;
62  TQPixmap icon = TDEGlobal::iconLoader()->loadIcon( "kpilot", TDEIcon::Desktop, TDEIcon::SizeMedium );
63  TQWidget *header = createHeader( this, icon, i18n( "KPilot Configuration" ) );
64  mLayout->addMultiCellWidget( header, row,row, 0,3 );
65 
66  // Last sync information
67  row++;
68  mSyncTimeTextLabel = new TQLabel( i18n( "<i>Last sync:</i>" ), this);
69  mLayout->addWidget( mSyncTimeTextLabel, row, 0 );
70  mSyncTimeLabel = new TQLabel( i18n( "No information available" ), this );
71  mLayout->addWidget( mSyncTimeLabel, row, 1 );
72  mShowSyncLogLabel = new KURLLabel( "", i18n( "[View Sync Log]" ), this );
73  mLayout->addWidget( mShowSyncLogLabel, row, 3 );
74  connect( mShowSyncLogLabel, TQ_SIGNAL( leftClickedURL( const TQString& ) ),
75  this, TQ_SLOT( showSyncLog( const TQString& ) ) );
76 
77  // User
78  row++;
79  mPilotUserTextLabel = new TQLabel( i18n( "<i>User:</i>" ), this);
80  mLayout->addWidget( mPilotUserTextLabel, row, 0);
81  mPilotUserLabel = new TQLabel( i18n( "Unknown" ), this );
82  mLayout->addMultiCellWidget( mPilotUserLabel, row, row, 1, 3 );
83 
84  // Device information
85  row++;
86  mPilotDeviceTextLabel = new TQLabel( i18n( "<i>Device:</i>" ), this);
87  mLayout->addWidget( mPilotDeviceTextLabel, row, 0 );
88  mPilotDeviceLabel = new TQLabel( i18n( "Unknown" ), this );
89  mLayout->addMultiCellWidget( mPilotDeviceLabel, row, row, 1, 3 );
90 
91  // Status
92  row++;
93  mDaemonStatusTextLabel = new TQLabel( i18n( "<i>Status:</i>" ), this);
94  mLayout->addWidget( mDaemonStatusTextLabel, row, 0 );
95  mDaemonStatusLabel = new TQLabel( i18n( "No communication with the daemon possible" ), this );
96  mLayout->addMultiCellWidget( mDaemonStatusLabel, row, row, 1, 3 );
97 
98  // Conduits:
99  row++;
100  mConduitsTextLabel = new TQLabel( i18n( "<i>Conduits:</i>" ), this );
101  mConduitsTextLabel->setAlignment( AlignAuto | AlignTop | ExpandTabs );
102  mLayout->addWidget( mConduitsTextLabel, row, 0 );
103  mConduitsLabel = new TQLabel( i18n( "No information available" ), this );
104  mConduitsLabel->setAlignment( mConduitsLabel->alignment() | TQt::WordBreak );
105  mLayout->addMultiCellWidget( mConduitsLabel, row, row, 1, 3 );
106 
107  // widgets shown if kpilotDaemon is not running
108  row++;
109  mNoConnectionLabel = new TQLabel( i18n( "KPilot is currently not running." ), this );
110  mLayout->addMultiCellWidget( mNoConnectionLabel, row, row, 1, 2 );
111  mNoConnectionStartLabel = new KURLLabel( "", i18n( "[Start KPilot]" ), this );
112  mLayout->addWidget( mNoConnectionStartLabel, row, 3 );
113  connect( mNoConnectionStartLabel, TQ_SIGNAL( leftClickedURL( const TQString& ) ),
114  this, TQ_SLOT( startKPilot() ) );
115 
116  if ( !kapp->dcopClient()->isApplicationRegistered( "kpilotDaemon" ) ) {
117  startKPilot();
118  }
119 
120  connectDCOPSignal( 0, 0, "kpilotDaemonStatusDetails(TQDateTime,TQString,TQStringList,TQString,TQString,TQString,bool)",
121  "receiveDaemonStatusDetails(TQDateTime,TQString,TQStringList,TQString,TQString,TQString,bool)", false );
122  connect( kapp->dcopClient(), TQ_SIGNAL( applicationRemoved( const TQCString & ) ), TQ_SLOT( slotAppRemoved( const TQCString& ) ) );
123 }
124 
125 SummaryWidget::~SummaryWidget()
126 {
127  if ( mStartedDaemon && mShouldStopDaemon ) {
128  PilotDaemonDCOP_stub dcopToDaemon( "kpilotDaemon", "KPilotDaemonIface" );
129  dcopToDaemon.quitNow(); // ASYNC, always succeeds.
130  }
131 }
132 
133 TQStringList SummaryWidget::configModules() const
134 {
135  TQStringList modules;
136  modules << "kpilot_config.desktop";
137  return modules;
138 }
139 
140 void SummaryWidget::receiveDaemonStatusDetails(TQDateTime lastSyncTime, TQString status, TQStringList conduits, TQString logFileName, TQString userName, TQString pilotDevice, bool killOnExit )
141 {
142  mDCOPSuccess = true;
143  mLastSyncTime = lastSyncTime;
144  mDaemonStatus = status;
145  mConduits = conduits;
146  mSyncLog = logFileName;
147  mUserName = userName;
148  mPilotDevice = pilotDevice;
149  mShouldStopDaemon = killOnExit;
150  updateView();
151 }
152 
153 void SummaryWidget::updateView()
154 {
155  if ( mDCOPSuccess ) {
156  if ( mLastSyncTime.isValid() ) {
157  mSyncTimeLabel->setText( mLastSyncTime.toString(TQt::LocalDate) );
158  } else {
159  mSyncTimeLabel->setText( i18n( "No information available" ) );
160  }
161  if ( !mSyncLog.isEmpty() ) {
162  mShowSyncLogLabel->setEnabled( true );
163  mShowSyncLogLabel->setURL( mSyncLog );
164  } else {
165  mShowSyncLogLabel->setEnabled( false );
166  }
167  mPilotUserLabel->setText( mUserName.isEmpty() ? i18n( "unknown" ) : mUserName );
168  mPilotDeviceLabel->setText( mPilotDevice.isEmpty() ? i18n( "unknown" ) : mPilotDevice );
169  mDaemonStatusLabel->setText( mDaemonStatus );
170  mConduitsLabel->setText( mConduits.join( ", " ) );
171  } else {
172  mSyncTimeLabel->setText( i18n( "No information available (Daemon not running?)" ) );
173  mShowSyncLogLabel->setEnabled( false );
174  mPilotUserLabel->setText( i18n( "unknown" ) );
175  mPilotDeviceLabel->setText( i18n( "unknown" ) );
176  mDaemonStatusLabel->setText( i18n( "No communication with the daemon possible" ) );
177  mConduitsLabel->setText( i18n( "No information available" ) );
178  }
179 
180  mSyncTimeTextLabel->setShown( mDCOPSuccess );
181  mSyncTimeLabel->setShown( mDCOPSuccess );
182  mShowSyncLogLabel->setShown( mDCOPSuccess );
183  mPilotUserTextLabel->setShown( mDCOPSuccess );
184  mPilotUserLabel->setShown( mDCOPSuccess );
185  mPilotDeviceTextLabel->setShown( mDCOPSuccess );
186  mPilotDeviceLabel->setShown( mDCOPSuccess );
187  mDaemonStatusTextLabel->setShown( mDCOPSuccess );
188  mDaemonStatusLabel->setShown( mDCOPSuccess );
189  mConduitsTextLabel->setShown( mDCOPSuccess );
190  mConduitsLabel->setShown( mDCOPSuccess );
191  mNoConnectionLabel->setShown( !mDCOPSuccess );
192  mNoConnectionStartLabel->setShown( !mDCOPSuccess );
193 }
194 
195 void SummaryWidget::showSyncLog( const TQString &filename )
196 {
197  KDialogBase dlg( this, 0, true, TQString(), KDialogBase::Ok, KDialogBase::Ok );
198  dlg.setCaption( i18n( "KPilot HotSync Log" ) );
199 
200  TQTextEdit *edit = new TQTextEdit( dlg.makeVBoxMainWidget() );
201  edit->setReadOnly( true );
202 
203  TQFile f(filename);
204  if ( !f.open( IO_ReadOnly ) ) {
205  KMessageBox::error( this, i18n( "Unable to open Hotsync log %1." ).arg( filename ) );
206  return;
207  }
208 
209  TQTextStream s( &f );
210  while ( !s.eof() )
211  edit->append( s.readLine() );
212 
213  edit->moveCursor( TQTextEdit::MoveHome, false );
214 
215  f.close();
216 
217  dlg.setInitialSize( TQSize( 400, 350 ) );
218  dlg.exec();
219 }
220 
221 void SummaryWidget::startKPilot()
222 {
223  TQString error;
224  TQCString appID;
225  if ( !TDEApplication::tdeinitExec( "kpilotDaemon", TQString( "--fail-silently" ) ) ) {
226  kdDebug(5602) << "No service available..." << endl;
227  mStartedDaemon = true;
228  }
229 }
230 
231 void SummaryWidget::slotAppRemoved( const TQCString & appId )
232 {
233  if ( appId == "kpilotDaemon" )
234  {
235  mDCOPSuccess = false;
236  updateView();
237  }
238 }
239 
240 
241 #include "summarywidget.moc"
242