1 /***************************************************************************
2  *   (C) 2005-2006 William Hoggarth <whoggarth@users.sourceforge.net       *
3  *   (C) 2007-2009 Michal Rudolf <mrudolf@kdewebdev.org>                   *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  ***************************************************************************/
10 
11 #ifndef DATABASE_H_INCLUDED
12 #define DATABASE_H_INCLUDED
13 
14 #include "filter.h"
15 #include "gamex.h"
16 #include "index.h"
17 #include "refcount.h"
18 #include "move.h"
19 #include "movedata.h"
20 
21 #include <QMutex>
22 #include <QString>
23 #include <QTextStream>
24 
25 
26 /** @defgroup Database Database - classes to manipulate chess game files*/
27 
28 /** @ingroup Database
29    The Database class is abstract, providing a common interface for all
30    database types. There are methods for the loading and saving of games,
31    and for performing searches and queries.
32 */
33 
34 class Database : public QObject
35 {
36     Q_OBJECT
37     SUPPORTS_REFCOUNTING
38 public:
39 
40     /** Flags to customize position search */
41     enum PositionSearchOptions: unsigned
42     {
43         /** Default search */
44         PositionSearch_Default = 0,
45 
46         /** Match only if position occurs at the game end */
47         PositionSearch_GameEnd = 1,
48     };
49 
50     /** Standard constructor. */
51     Database();
52     volatile bool m_break;
53     /** Virtual destructor */
54     virtual ~Database();
55 
56     /** Query file format */
57     bool isUtf8() const;
58     /** Set file format */
59     void setUtf8(bool utf8);
60 
mutex()61     QMutex* mutex() { return &m_mutex; }
62     //database operations
63 
64     /** Opens the given database */
65     virtual bool open(const QString& filename, bool utf8) = 0;
66     /** Parse the database */
67     virtual bool parseFile() = 0;
68     /** File-based database name */
69     virtual QString filename() const = 0;
70     /** Determine if index files are supported */
hasIndexFile()71     virtual bool hasIndexFile() const { return false; }
72 
73     /** Returns whether the database is read-only or not */
74     virtual bool isReadOnly() const;
75     /** File-based database name for display (no path and extension) */
76     virtual QString name() const;
77 
78     //game retrieval & storage
79     /** Loads a game at @p index, returns true if successful */
80     virtual bool loadGame(GameId gameId, GameX& game);
81     /** Load all tags for GameId from index into game object */
82     virtual void loadGameHeaders(GameId gameId, GameX& game) const;
83     /** Load a tag into a game */
84     virtual void loadGameHeader(GameId gameId, GameX& game, const QString& tag) const;
85     /** Get the tag for a game */
86     virtual QString tagValue(GameId gameId, const QString &tag) const;
87     virtual QString tagValue(GameId gameId, TagIndex tag) const;
88     /** Loads only moves into a game from the given position */
89     virtual void loadGameMoves(GameId index, GameX& game) = 0;
90     /** Loads game moves and try to find a position */
91     virtual int findPosition(GameId index, const BoardX& position) = 0;
92     /** Perform batched position search */
93     virtual void findPosition(const BoardX& position, PositionSearchOptions options, const QList<GameId>& games, QList<MoveId>& output, QMap<Move, MoveData>& stats);
94     /** Saves a game at the given position, returns true if successful */
95     virtual bool replace(GameId, GameX&);
96     /** Adds a game to the database */
97     virtual bool appendGame(const GameX&);
98     /** Removes a game from the database */
99     virtual bool remove(GameId);
100     /** Remove all games from a database */
101     virtual void clear();
102     /** Undelete a game from the database */
103     virtual bool undelete(GameId);
104     /** Removes multiple games from the database as specified by the filter */
105     virtual bool remove(const FilterX&);
106     /** @return pointer to the index of the database */
107     IndexX *index();
108     /** @return const pointer to the index of the database */
109     const IndexX *index() const;
110     /** Returns the number of games in the database */
111     virtual quint64 count() const;
112     /** @return true if the database has been modified. */
113     virtual bool isModified() const;
114     /** Set / Reset the modification flag. */
setModified(bool)115     virtual void setModified(bool) { }
116     /** Get the Valid Flag for a given game id from the index */
117     virtual bool getValidFlag(GameId gameId) const;
118     /** Get the Valid Flag for a given game id from the index */
119     virtual bool deleted(GameId gameId) const;
120     /** Returns true, if the database is a clipboard database, false otherwise */
IsClipboard()121     virtual bool IsClipboard() const { return false; }
122     /** Get a map of MoveData from a given board position */
getMoveMapForBoard(const BoardX &,QMap<Move,MoveData> &)123     virtual unsigned int getMoveMapForBoard(const BoardX& , QMap<Move, MoveData> &) { return 0; }
124 protected:
125     /** Copies all tags from @p game to the Index */
126     void setTagsToIndex(const GameX& game, GameId id);
127 
128 signals:
129     /** Signal emitted when some progress is done. */
130     void progress(int);
131     void dirtyChanged(bool);
132 
133 protected:
134     IndexX m_index;
135     bool m_utf8;
136     QMutex m_mutex;
137 };
138 
139 #endif
140 
141