[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-core-devel
Subject:    Re: Open Recent... class ?
From:       Michael Koch <m_koch () bigfoot ! de>
Date:       2000-03-02 16:02:31
[Download RAW message or body]

Hi,


I've written a little class for the "Open Recent ..." problem.
Its an action derived from KSelectAction. And can easily used
The source is in the attached files. Its also commited and works in KWrite.

Any commens or suggestions ?



Ciao,
Michael
--
koch@kde.org, m_koch@bigfoot.de
http://heaven.riednet.wh.tu-darmstadt.de/~mkoch

To me vi is Zen.  To use vi is to practice zen. Every command is
a koan. Profound to the user, unintelligible to the uninitiated.
You discover truth everytime you use it.
                                 -- reddy@lion.austin.ibm.com

["krfaction.h" (text/x-c++)]

#ifndef __krfaction_h__
#define __krfaction_h__

#include <kurl.h>
#include <kaction.h>

class KConfig;

class KRecentFilesAction : public KListAction
{
  Q_OBJECT

public:

  KRecentFilesAction( QObject* parent = 0, const char* name = 0, unsigned int maxItems = 10 );
  ~KRecentFilesAction();

  unsigned int maxItems();
  void setMaxItems( unsigned int );
  
  void addURL( const KURL& );
  void removeURL( const KURL& );
  void clearURLList();

  void loadEntries( KConfig* );
  void saveEntries( KConfig* );

signals:

  void urlSelected( const KURL& );

protected slots:

  void itemSelected( const QString& );

private:

  unsigned int m_maxItems;
};

#endif // __krfaction_h__

["krfaction.cpp" (text/x-c++)]

#include <qstringlist.h>

#include <kconfig.h>
#include <klocale.h>

#include "krfaction.h"

KRecentFilesAction::KRecentFilesAction( QObject* parent, const char* name, unsigned int maxItems )
  : KListAction( i18n( "Open recent..." ), 0, parent, name )
  , m_maxItems( maxItems )
{
    connect( this, SIGNAL( activated( const QString& ) ),
	     this, SLOT( itemSelected( const QString& ) ) );
}

KRecentFilesAction::~KRecentFilesAction()
{
}

unsigned int KRecentFilesAction::maxItems()
{
    return m_maxItems;
}

void KRecentFilesAction::setMaxItems( unsigned int maxItems )
{
    QStringList lst       = items();
    unsigned int oldCount = lst.count();
    
    // set new maxItems
    m_maxItems = maxItems;

    // remove all items that are too much
    while( lst.count() > maxItems )
    {
	// remove last item
	lst.remove( lst.last() );
    }
    
    // set new list if changed
    if( lst.count() != oldCount )
	setItems( lst );
}

void KRecentFilesAction::addURL( const KURL& url )
{
    QString     file = url.url();
    QStringList lst = items();

    // remove file if already in list
    lst.remove( file );
    
    // remove las item if already maxitems in list
    if( lst.count() == m_maxItems )
    {
	// remove last item
	lst.remove( lst.last() );
    }
    
    // add file to list
    lst.prepend( file );
    setItems( lst );
}

void KRecentFilesAction::removeURL( const KURL& url )
{
    QStringList lst = items();
    QString     file = url.url();

    // remove url
    if( lst.count() > 0 )
    {
	lst.remove( file );
	setItems( lst );
    }
}

void KRecentFilesAction::clearURLList()
{
    clear();
}

void KRecentFilesAction::loadEntries( KConfig* config )
{
    QString     key;
    QString     value;
    QStringList lst;
    
    config->setGroup( "RecentFiles" );

    // read file list
    for( unsigned int i = 1 ; i <= m_maxItems ; i++ )
    {
        key = QString( "File%1" ).arg( i );
	value = config->readEntry( key, QString::null );
	lst.append( value );
    }
    
    // set file
    setItems( lst );
}

void KRecentFilesAction::saveEntries( KConfig* config )
{
    QString     key;
    QString     value;
    QStringList lst = items();
    
    config->setGroup( "RecentFiles" );

    // write file list
    for( unsigned int i = 1 ; i <= lst.count() ; i++ )
    {
	key = QString( "File%1" ).arg( i );
	value = lst[ i - 1 ];
	config->writeEntry( key, value );
    }
}

void KRecentFilesAction::itemSelected( const QString& text )
{
    emit urlSelected( KURL( text ) );
}


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic