summaryrefslogtreecommitdiff
path: root/noncore/todayplugins/fortune/fortunepluginwidget.cpp
blob: e6a0d0966428edefc76e5d5334c67d2b7ae38642 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
 * fortunepluginwidget.cpp
 *
 * copyright   : (c) 2002 by Maximilian Reiß
 * email       : harlekin@handhelds.org
 *
 */
/***************************************************************************
 *   *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.     *
 *   *
 ***************************************************************************/

#include "fortunepluginwidget.h"

#include <qvaluelist.h>
#include <qtl.h>
#include <qstring.h>
#include <qscrollview.h>
#include <qobject.h>
#include <qlayout.h>

#include <qpe/config.h>
#include <qpe/qcopenvelope_qws.h>

#include <opie/oprocess.h>
#include <opie/oticker.h>

FortunePluginWidget::FortunePluginWidget( QWidget *parent,  const char* name )
	: QWidget( parent, name )
{

	fortune = NULL;
	getFortune();
}

FortunePluginWidget::~FortunePluginWidget() {
	if( fortuneProcess ){
		delete fortuneProcess;
	}
}

/**
 * Get the fortunes
 */
void FortunePluginWidget::getFortune() {

	QVBoxLayout* layoutFortune = new QVBoxLayout( this );

	if ( fortune ) {
		delete fortune;
	}

	fortune = new OTicker( this );
//	fortune->setReadOnly( TRUE );
//	fortune->setSizePolicy( QSizePolicy( QSizePolicy::Minimum, QSizePolicy::Minimum ) );

	fortune->setText( QString("Obtaining fortune...") );
	layoutFortune->addWidget( fortune );

	fortuneProcess = new OProcess();
	*fortuneProcess << "fortune";
	
	connect(fortuneProcess,  SIGNAL(receivedStdout(OProcess*,char*,int) ),
		this,  SLOT(slotStdOut(OProcess*,char*,int) ) );
	
	if(!fortuneProcess->start(OProcess::NotifyOnExit, OProcess::AllOutput) ) {
		qWarning("could not start :(");
		fortune->setText( QString("Failed to obtain fortune.") );
		delete fortuneProcess;
		fortuneProcess = 0;
	}
	
}

void FortunePluginWidget::slotStdOut( OProcess* proc, char* buf, int len )
{
	QCString s( buf, len );
	s.replace( QRegExp("\n"), "" );
	fortune->setText( s );
}