~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to ksysguard/gui/ksgrd/SensorShellAgent.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    KSysGuard, the KDE System Guard
 
3
   
 
4
    Copyright (c) 1999 - 2001 Chris Schlaeger <cs@kde.org>
 
5
    
 
6
    This program is free software; you can redistribute it and/or
 
7
    modify it under the terms of the GNU General Public
 
8
    License version 2 or at your option version 3 as published by
 
9
    the Free Software Foundation.
 
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
*/
 
21
 
 
22
#include <kdebug.h>
 
23
#include <kprocess.h>
 
24
#include <kshell.h>
 
25
#include <klocale.h>
 
26
 
 
27
//#include "SensorClient.h"
 
28
#include "SensorManager.h"
 
29
 
 
30
#include "SensorShellAgent.h"
 
31
 
 
32
using namespace KSGRD;
 
33
 
 
34
SensorShellAgent::SensorShellAgent( SensorManager *sm )
 
35
  : SensorAgent( sm ), mDaemon( 0 )
 
36
{
 
37
}
 
38
 
 
39
SensorShellAgent::~SensorShellAgent()
 
40
{
 
41
  if ( mDaemon ) {
 
42
    mDaemon->write( "quit\n", sizeof( "quit\n" )-1 );
 
43
    mDaemon->disconnect();
 
44
    mDaemon->waitForFinished();
 
45
    delete mDaemon;
 
46
    mDaemon = 0;
 
47
  }
 
48
}
 
49
        
 
50
bool SensorShellAgent::start( const QString &host, const QString &shell,
 
51
                              const QString &command, int )
 
52
{
 
53
  mDaemon = new KProcess();
 
54
  mDaemon->setOutputChannelMode( KProcess::SeparateChannels );
 
55
  mRetryCount=3;
 
56
  setHostName( host );
 
57
  mShell = shell;
 
58
  mCommand = command;
 
59
 
 
60
  connect( mDaemon, SIGNAL(  error ( QProcess::ProcessError  ) ),
 
61
           SLOT( daemonError( QProcess::ProcessError ) ) );
 
62
  connect( mDaemon, SIGNAL(  finished ( int, QProcess::ExitStatus ) ),
 
63
           SLOT( daemonExited( int, QProcess::ExitStatus ) ) );
 
64
  connect( mDaemon, SIGNAL( readyReadStandardOutput() ),
 
65
           SLOT( msgRcvd() ) );
 
66
  connect( mDaemon, SIGNAL( readyReadStandardError() ),
 
67
           SLOT( errMsgRcvd() ) );
 
68
 
 
69
  if ( !command.isEmpty() ) {
 
70
    *mDaemon << KShell::splitArgs(command);
 
71
  }
 
72
  else
 
73
    *mDaemon << mShell << hostName() << "ksysguardd";
 
74
  mDaemon->start();
 
75
 
 
76
  return true;
 
77
}
 
78
 
 
79
void SensorShellAgent::hostInfo( QString &shell, QString &command,
 
80
                                 int &port) const
 
81
{
 
82
  shell = mShell;
 
83
  command = mCommand;
 
84
  port = -1;
 
85
}
 
86
 
 
87
void SensorShellAgent::msgRcvd( )
 
88
{
 
89
  QByteArray buffer = mDaemon->readAllStandardOutput();
 
90
  mRetryCount = 3; //we received an answer, so reset our retry count back to 3
 
91
  processAnswer( buffer.constData(), buffer.size());
 
92
}
 
93
 
 
94
void SensorShellAgent::errMsgRcvd( )
 
95
{
 
96
  QByteArray buffer = mDaemon->readAllStandardOutput();
 
97
 
 
98
  // Because we read the error buffer in chunks, we may not have a proper utf8 string.
 
99
  // We should never get input over stderr anyway, so no need to worry too much about it.
 
100
  // But if this is extended, we will need to handle this better
 
101
  QString buf = QString::fromUtf8( buffer );
 
102
 
 
103
  kDebug(1215) << "SensorShellAgent: Warning, received text over stderr!"
 
104
                << endl << buf << endl;
 
105
}
 
106
 
 
107
void SensorShellAgent::daemonExited(  int exitCode, QProcess::ExitStatus exitStatus )
 
108
{
 
109
  Q_UNUSED(exitCode);
 
110
  kDebug(1215) << "daemon exited, exit status "  << exitStatus;
 
111
  if ( mRetryCount--  <= 0 || (mDaemon->start(), !mDaemon->waitForStarted()) )
 
112
  {
 
113
    setDaemonOnLine( false );
 
114
    if(sensorManager()) {
 
115
      sensorManager()->disengage( this ); //delete ourselves
 
116
    }
 
117
  }
 
118
}
 
119
 
 
120
void SensorShellAgent::daemonError( QProcess::ProcessError errorStatus )
 
121
{
 
122
  QString error;
 
123
  switch(errorStatus) {
 
124
    case QProcess::FailedToStart:
 
125
      kDebug(1215) << "failed to run" <<  mDaemon->program().join(" ");
 
126
      error = i18n("Could not run daemon program '%1'.", mDaemon->program().join(" "));
 
127
      break;
 
128
    case QProcess::Crashed:
 
129
    case QProcess::Timedout:
 
130
    case QProcess::WriteError:
 
131
    case QProcess::ReadError:
 
132
    default:
 
133
      error = i18n("The daemon program '%1' failed.", mDaemon->program().join(" "));
 
134
  }
 
135
  setReasonForOffline(error);
 
136
  kDebug(1215) << "Error received " << error << "(" << errorStatus << ")";
 
137
  setDaemonOnLine( false );
 
138
  if(sensorManager())
 
139
    sensorManager()->disengage( this ); //delete ourselves
 
140
}
 
141
bool SensorShellAgent::writeMsg( const char *msg, int len )
 
142
{
 
143
  //write returns -1 on error, in which case we should return false.  true otherwise.
 
144
  return mDaemon->write( msg, len ) != -1;
 
145
}
 
146
 
 
147
#include "SensorShellAgent.moc"