Source Code (Use browser search to find items of interest.)

Class Index

karm'TaskBroker (./kdeutils/karm/task.h:71)

class TaskBroker	:	public QObject
{
	Q_OBJECT
private:
	QList<Task> *_taskList;

	/** create a task from saved data. The buffer is parsed
	* and a new Task is created from it. Returns 0 if no
	* task was found.
	*/
	static Task *readTask(const char *buffer);

public:
	/** constructor
	*/
	TaskBroker();

	/** destructor
	*/
	~TaskBroker();

	/** reads in a saved task list.
	*/
	int readFromFile( const char *file );

	/** saves the current task list
	*/
	bool writeToFile( const char *file );

	/** moves the cursor. If the new position is out of bounds, the
	* cursor is not moved and <0 is returned.
	* @param d	move the cursor by d items. Negative d moves the
	*		cursor upward.
	* @return	the position of the new item, <0 if out of bounds.
	*/
	int moveCursor(int d);

	/** the current cursor position
	*/
	int current() const
		{ return _taskList->at(); };

	/** The task element at the current position.
	* @return the task at the current element, or NULL if there is
	* no current element (empty list).
	*/
	Task *currentTask() const
		{ return _taskList->current(); };

	/** Add a new task */
	void addTask( const QString& name, long time = 0 );


	/** delete the current Task */
	void deleteCurrentTask();


	/** returns TRUE if list is empty or cursor is at last element */
	bool atLast() const;

	/** returns the number of tasks in the list */
	int count() const
		{ return _taskList->count(); };

public slots:


	/**moves cursor to the next element*/
	void next()
		{ moveCursor(1); };	

	/**moves cursor to be previous element*/
	void previous()
		{ moveCursor(-1); };

	/**moves cursor to the top of the list*/
	void top();

	/**moves cursor to the end of the list*/
	void bottom();

	/** moves the cursor to the absolute index
	*/
	void moveTo( int );

signals:
	/** raised on cursor move */
	void cursorMoved( int );

	/** raised on changes to the list, rather than to a
	* particular item.
	*/
	void dataChanged();

	/** raised on change in a particular item.
	*/
	void currentChanged( int i );

	/** raised on file read or write error.
	*/
	void fileError( const char * );
};



karm'TaskBroker::moveCursor() (./kdeutils/karm/task.cpp:19)

int TaskBroker::moveCursor(int d)
{

        if( _taskList->count() == 0 
                || ((unsigned)(_taskList->at() + d) >=_taskList->count()))
                return 0;

	_taskList->at( _taskList->at() + d );

        emit cursorMoved( _taskList->at() );

        return _taskList->at();
}


karm'TaskBroker::readFromFile() (./kdeutils/karm/task.cpp:33)

int TaskBroker::readFromFile( const char *fname ) 
{
	if ( fname == 0 )
		return 0;

	QFile *file = new QFile( fname );

	if( file == 0 ) {
		warning( "out of memory creating file object");
		return 0;
	}

	if( !file->open( IO_ReadOnly ) ) {
		// file open error
		emit fileError( fname );
		delete file;
		return 0;
	}

	_taskList->setAutoDelete( TRUE );
	_taskList->clear();

	char *line = new char[T_LINESIZE+1];

	if( line == 0 ) {
		delete file;
		warning("out of memory creating string.");
		return 0;
	}

	while( !file->atEnd() ) {
		Task *newTask;

		if ( file->readLine( line, T_LINESIZE ) == 0 )
			break;

		newTask = readTask( line );

		if( newTask != 0 )
			_taskList->append( newTask );
	}

	file->close();
	_taskList->first();

	delete file;
	delete[] line;

	return _taskList->count();
}


karm'TaskBroker::readTask() (./kdeutils/karm/task.cpp:84)

Task *TaskBroker::readTask(const char *buffer)
{
	int totalTime;

	while( isspace( *buffer ) )
		buffer++;

	// return an error if we didn't find a number
	// possible empty line?
	if( !isdigit( *buffer) )
		return 0;

	// let atoi find our number
	totalTime = atol(buffer);

	// skip number and following white space
	while( isdigit(*buffer) )
		buffer++;
	
	while( isspace(*buffer) )
		buffer++;

	// assume that buffer now points to the name of the task

	return new Task( buffer, totalTime );
}


karm'TaskBroker::writeToFile() (./kdeutils/karm/task.cpp:111)

bool TaskBroker::writeToFile( const char *fname )
{
	if( fname == 0 ) return 0;

	FILE *file = fopen( fname, "w" );

	if( file == 0 ) {
		emit fileError( fname );
		return FALSE;
	}


	fputs( "# Karm save data\n", file );	// file comment

	_taskList->first();

	while( _taskList->current() != 0 )  {
		
		fprintf(file, "%ld\t%s\n", 
			_taskList->current()->time(),
			_taskList->current()->name() );

		_taskList->next();
	}

	_taskList->at();

	fclose( file );

	emit dataChanged();

	return TRUE;
}


karm'TaskBroker::addTask() (./kdeutils/karm/task.cpp:145)

void TaskBroker::addTask( const QString& task, long time )
{
	if( task == 0 )
		return;

	_taskList->append( new Task( task, time ) );

	emit cursorMoved( _taskList->at() );
}


karm'TaskBroker::deleteCurrentTask() (./kdeutils/karm/task.cpp:155)

void TaskBroker::deleteCurrentTask()
{
	if( _taskList->current() != 0 ) {
		_taskList->setAutoDelete( TRUE );
		_taskList->remove();
	}
}

karm'TaskBroker::TaskBroker() (./kdeutils/karm/task.h:182)

inline TaskBroker::TaskBroker() 
	: QObject()
{ 
	_taskList = new QList<Task>; 
}


karm'TaskBroker::~TaskBroker() (./kdeutils/karm/task.h:188)

inline TaskBroker::~TaskBroker()
{ 
	delete _taskList;
}


karm'TaskBroker::bottom() (./kdeutils/karm/task.h:193)

inline void TaskBroker::bottom()
{
	_taskList->last();  
	emit cursorMoved( _taskList->at() ) ; 
}


karm'TaskBroker::top() (./kdeutils/karm/task.h:199)

inline void TaskBroker::top()
{ 
	_taskList->first();
	emit cursorMoved( _taskList->at() );
}


karm'TaskBroker::moveTo() (./kdeutils/karm/task.h:205)

inline void TaskBroker::moveTo( int pos )
{ 
	_taskList->at( pos );
	emit cursorMoved( _taskList->at() );
}


karm'TaskBroker::atLast() (./kdeutils/karm/task.h:211)

inline bool TaskBroker::atLast() const
{
	if( _taskList->count() == 0L ||
		(unsigned)_taskList->at() == _taskList->count()-1 )
		return TRUE;

	return FALSE;
}