1 /* This file is (c) 2008-2012 Konstantin Isakov <ikm@goldendict.org>
2  * Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
3 
4 #ifndef __HISTORY_HH_INCLUDED__
5 #define __HISTORY_HH_INCLUDED__
6 
7 #include <QObject>
8 #include <QList>
9 #include <QString>
10 
11 #define DEFAULT_MAX_HISTORY_ITEM_LENGTH 256
12 
13 /// Search history
14 class History: public QObject
15 {
16   Q_OBJECT
17 
18 public:
19 
20   /// An item in history
21   struct Item
22   {
23     /// Group the search was performed in
24     unsigned groupId;
25     /// The word that was searched
26     QString word;
27 
ItemHistory::Item28     Item(): groupId( 0 )
29     {}
30 
ItemHistory::Item31     Item( unsigned groupId_, QString const & word_ ):
32       groupId( groupId_ ), word( word_ )
33     {}
34 
operator ==History::Item35     bool operator == ( Item const & other ) const
36     { return QString::compare( word, other.word, Qt::CaseInsensitive) == 0 && groupId == other.groupId; }
37 
operator !=History::Item38     bool operator != ( Item const & other ) const
39     { return ! operator == ( other ); }
40   };
41 
42   /// Indicates an intention to load -- see the relevant History constructor.
43   struct Load {};
44 
45   /// Constructs an empty history which can hold at most "size" items.
46   History( unsigned size = 20 , unsigned maxItemLength = DEFAULT_MAX_HISTORY_ITEM_LENGTH );
47 
48   /// Loads history from its file. If load fails, the result would be an empty
49   /// history. The size parameter is same as in other constructor.
50   History( Load, unsigned size = 20, unsigned maxItemLength = DEFAULT_MAX_HISTORY_ITEM_LENGTH );
51 
52   /// Adds new item. The item is always added at the beginning of the list.
53   /// If there was such an item already somewhere on the list, it gets removed
54   /// from there. If otherwise the resulting list gets too large, the oldest
55   /// item gets removed from the end of the list.
56   void addItem( Item const & );
57 
58   Item getItem( int index );
59 
60   /// Remove item with given index from list
removeItem(int index)61   void removeItem( int index )
62   { items.removeAt( index ); dirty = true; emit itemsChanged(); }
63 
64   /// Attempts saving history. Returns true if succeeded - false otherwise.
65   /// Since history isn't really that valuable, failures can be ignored.
66   bool save();
67 
68   /// Clears history.
69   void clear();
70 
71   /// History size.
72   int size() const;
73 
74   /// Gets the current items. The first one is the newest one on the list.
getItems() const75   QList< Item > const & getItems() const
76   { return items; }
77 
78   /// Enable/disable add words to hystory
enableAdd(bool enable)79   void enableAdd( bool enable )
80   { addingEnabled = enable; }
enabled()81   bool enabled()
82   { return addingEnabled; }
83 
84   void setMaxSize( unsigned maxSize_ );
85 
86   void setSaveInterval( unsigned interval );
87 
getMaxSize()88   unsigned getMaxSize()
89   { return maxSize; }
90 
getMaxItemLength() const91   unsigned getMaxItemLength() const
92   { return maxItemLength; }
93 
94 signals:
95 
96   /// Signals the changes in items in response to addItem() or clear().
97   void itemsChanged();
98 
99 private:
100 
101   /// Returns true if the items list has been modified
102   /// in order to fit into the constraints.
103   bool ensureSizeConstraints();
104 
105   QList< Item > items;
106   unsigned maxSize;
107   unsigned maxItemLength;
108   bool addingEnabled;
109   bool dirty;
110   int timerId;
111 
112 protected:
113   virtual void timerEvent( QTimerEvent * );
114 };
115 
116 #endif
117