2012-02-20 21:47:14 +00:00
|
|
|
/* This file is (c) 2008-2012 Konstantin Isakov <ikm@goldendict.org>
|
2009-10-21 19:37:07 +00:00
|
|
|
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
|
|
|
|
|
|
|
|
#ifndef __HISTORY_HH_INCLUDED__
|
|
|
|
#define __HISTORY_HH_INCLUDED__
|
|
|
|
|
|
|
|
#include <QObject>
|
|
|
|
#include <QList>
|
|
|
|
#include <QString>
|
|
|
|
|
2024-10-10 07:00:13 +00:00
|
|
|
constexpr unsigned DEFAULT_MAX_HISTORY_ITEM_LENGTH = 256;
|
|
|
|
|
2012-09-10 13:01:35 +00:00
|
|
|
|
2009-10-21 19:37:07 +00:00
|
|
|
/// Search history
|
2024-10-10 07:00:13 +00:00
|
|
|
///
|
|
|
|
/// To work with the original GD's history file,
|
|
|
|
/// every entry starts with a number like "123 word"
|
|
|
|
///
|
2009-10-21 19:37:07 +00:00
|
|
|
class History: public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
/// An item in history
|
|
|
|
struct Item
|
|
|
|
{
|
|
|
|
QString word;
|
|
|
|
|
2024-10-10 07:00:13 +00:00
|
|
|
// For assisting QList::contains & QList::removeOne
|
2009-10-21 19:37:07 +00:00
|
|
|
bool operator==( Item const & other ) const
|
2013-01-17 17:00:37 +00:00
|
|
|
{
|
2024-10-10 07:00:13 +00:00
|
|
|
return QString::compare( word, other.word, Qt::CaseInsensitive ) == 0;
|
2009-10-21 19:37:07 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-10-10 07:00:13 +00:00
|
|
|
/// Loads history from its file. If the loading fails, the result would be an empty history.
|
|
|
|
explicit History( unsigned size = 256, unsigned maxItemLength = DEFAULT_MAX_HISTORY_ITEM_LENGTH );
|
2009-10-21 19:37:07 +00:00
|
|
|
|
|
|
|
/// Adds new item. The item is always added at the beginning of the list.
|
|
|
|
/// If there was such an item already somewhere on the list, it gets removed
|
|
|
|
/// from there. If otherwise the resulting list gets too large, the oldest
|
|
|
|
/// item gets removed from the end of the list.
|
|
|
|
void addItem( Item const & );
|
|
|
|
|
2013-01-17 09:08:53 +00:00
|
|
|
Item getItem( int index );
|
|
|
|
|
2012-02-16 14:56:25 +00:00
|
|
|
/// Remove item with given index from list
|
|
|
|
void removeItem( int index )
|
2013-03-05 12:23:50 +00:00
|
|
|
{
|
|
|
|
items.removeAt( index );
|
|
|
|
dirty = true;
|
|
|
|
emit itemsChanged();
|
|
|
|
}
|
2012-02-16 14:56:25 +00:00
|
|
|
|
2009-10-21 19:37:07 +00:00
|
|
|
/// Attempts saving history. Returns true if succeeded - false otherwise.
|
|
|
|
/// Since history isn't really that valuable, failures can be ignored.
|
2013-02-05 12:51:23 +00:00
|
|
|
bool save();
|
2009-10-21 19:37:07 +00:00
|
|
|
|
|
|
|
/// Clears history.
|
|
|
|
void clear();
|
|
|
|
|
2013-01-17 09:08:53 +00:00
|
|
|
/// History size.
|
|
|
|
int size() const;
|
|
|
|
|
2009-10-21 19:37:07 +00:00
|
|
|
/// Gets the current items. The first one is the newest one on the list.
|
|
|
|
QList< Item > const & getItems() const
|
|
|
|
{
|
|
|
|
return items;
|
|
|
|
}
|
|
|
|
|
2012-02-16 14:56:25 +00:00
|
|
|
/// Enable/disable add words to hystory
|
|
|
|
void enableAdd( bool enable )
|
|
|
|
{
|
|
|
|
addingEnabled = enable;
|
|
|
|
}
|
|
|
|
bool enabled()
|
|
|
|
{
|
|
|
|
return addingEnabled;
|
|
|
|
}
|
|
|
|
|
2013-01-17 09:08:53 +00:00
|
|
|
void setMaxSize( unsigned maxSize_ );
|
|
|
|
|
2013-02-05 12:51:23 +00:00
|
|
|
void setSaveInterval( unsigned interval );
|
|
|
|
|
2012-09-10 13:01:35 +00:00
|
|
|
unsigned getMaxSize()
|
|
|
|
{
|
|
|
|
return maxSize;
|
|
|
|
}
|
|
|
|
|
2013-01-17 14:24:13 +00:00
|
|
|
unsigned getMaxItemLength() const
|
|
|
|
{
|
|
|
|
return maxItemLength;
|
|
|
|
}
|
|
|
|
|
2009-10-21 19:37:07 +00:00
|
|
|
signals:
|
|
|
|
|
|
|
|
/// Signals the changes in items in response to addItem() or clear().
|
|
|
|
void itemsChanged();
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
2013-01-17 09:08:53 +00:00
|
|
|
/// Returns true if the items list has been modified
|
|
|
|
/// in order to fit into the constraints.
|
|
|
|
bool ensureSizeConstraints();
|
|
|
|
|
2009-10-21 19:37:07 +00:00
|
|
|
QList< Item > items;
|
|
|
|
unsigned maxSize;
|
2013-01-17 14:24:13 +00:00
|
|
|
unsigned maxItemLength;
|
2012-02-16 14:56:25 +00:00
|
|
|
bool addingEnabled;
|
2013-02-05 12:51:23 +00:00
|
|
|
bool dirty;
|
|
|
|
int timerId;
|
2013-01-17 09:08:53 +00:00
|
|
|
|
2013-02-05 12:51:23 +00:00
|
|
|
protected:
|
|
|
|
virtual void timerEvent( QTimerEvent * );
|
2009-10-21 19:37:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|