The example program on this page may be used, distributed and modified
without limitation.
Biff (UNIX only)
Biff is a simple graphical program to indicate whether there is new
mail; it looks exactly like xbiff but is much shorter.
The example is divided into two .cpp files and one .h file.
main.cpp looks like most simple Qt-based main() implementations:
//
// Qt Example Application: A simple "biff" program (UNIX only)
//
#include <qapp.h>
#include "biff.h"
int main( int argc, char ** argv )
{
QApplication a( argc, argv );
Biff b;
a.setMainWidget( &b );
b.show();
return a.exec();
}
biff.h is the declaratin for the custom widget which does all the
work:
//
// Biff widget definition.
//
#ifndef BIFF_H
#define BIFF_H
#include <qwidget.h>
#include <qdatetm.h>
#include <qpixmap.h>
class Biff : public QWidget
{
Q_OBJECT
public:
Biff( QWidget *parent=0, const char *name=0 );
protected:
void timerEvent( QTimerEvent * );
void paintEvent( QPaintEvent * );
void mousePressEvent( QMouseEvent * );
private:
QDateTime lastModified;
QPixmap hasNewMail;
QPixmap noNewMail;
QString mailbox;
bool gotMail;
};
#endif // BIFF_H
Finally, biff.cpp implements this custom widget. Note in particular
how two images (hasmail_bmp_data and nomail_bmp_data, both from
bmp.cpp) are included into the executable.
//
// Biff widget implementation (UNIX only).
//
#include "biff.h"
#include <qstring.h>
#include <qfileinf.h>
#include <qpainter.h>
#include <unistd.h>
#include <stdlib.h>
#include "bmp.cpp"
Biff::Biff( QWidget *parent, const char *name )
: QWidget( parent, name, WType_Modal )
{
QFileInfo fi( getenv( "MAIL" ) );
if ( !fi.exists() ) {
QString s( "/var/spool/mail/" );
s += getlogin();
fi.setFile( s );
}
if ( fi.exists() ) {
mailbox = fi.absFilePath();
startTimer( 1000 );
}
setMinimumSize( 48, 48 );
setMaximumSize( 48, 48 );
resize( 48, 48 );
hasNewMail.loadFromData( hasmail_bmp_data, hasmail_bmp_len );
noNewMail.loadFromData( nomail_bmp_data, nomail_bmp_len );
gotMail = FALSE;
lastModified = fi.lastModified();
}
void Biff::timerEvent( QTimerEvent * )
{
QFileInfo fi( mailbox );
bool newState = ( fi.lastModified() != lastModified &&
fi.lastModified() > fi.lastRead() );
if ( newState != gotMail ) {
if ( gotMail )
lastModified = fi.lastModified();
gotMail = newState;
repaint( FALSE );
}
}
void Biff::paintEvent( QPaintEvent * )
{
if ( gotMail )
bitBlt( this, 0, 0, &hasNewMail );
else
bitBlt( this, 0, 0, &noNewMail );
}
void Biff::mousePressEvent( QMouseEvent * )
{
QFileInfo fi( mailbox );
lastModified = fi.lastModified();
}
Generated at 17:19, 1997/09/30 for Qt version 1.30 by the webmaster at Troll Tech