initial commit, lordsawar source, slightly modified
[lordsawar] / src / recently-played-game-list.h
1 //  Copyright (C) 2008, Ben Asselstine
2 //
3 //  This program is free software; you can redistribute it and/or modify
4 //  it under the terms of the GNU General Public License as published by
5 //  the Free Software Foundation; either version 3 of the License, or
6 //  (at your option) any later version.
7 //
8 //  This program is distributed in the hope that it will be useful,
9 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 //  GNU Library General Public License for more details.
12 //
13 //  You should have received a copy of the GNU General Public License
14 //  along with this program; if not, write to the Free Software
15 //  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 
16 //  02110-1301, USA.
17
18 #ifndef RECENTLYPLAYEDGAMELIST_H
19 #define RECENTLYPLAYEDGAMELIST_H
20
21 #include <gtkmm.h>
22 #include <string>
23 #include <list>
24 #include <sigc++/trackable.h>
25
26 #include "xmlhelper.h"
27
28 class GameScenario;
29 class RecentlyPlayedGame;
30
31 //! A list of games that we've recently played.
32 /** 
33  * This is only used for network games at the moment.
34  * It is implemented as a singleton.
35  *
36  */
37 class RecentlyPlayedGameList: public std::list<RecentlyPlayedGame*>, public sigc::trackable
38 {
39     public:
40
41         //! The xml tag of this object in a recently played game file.
42         static std::string d_tag; 
43         
44         static const int TWO_WEEKS_OLD = 1209600; /* seconds */
45
46
47         // Methods that operate on the class data and do not modify the class.
48
49         //! Save the recently played game list to the given file.
50         bool saveToFile(std::string filename) const;
51
52         //! Save the recently played game list to an opened file.
53         bool save(XML_Helper* helper) const;
54
55
56         // Methods that operate on the class data and modify the class.
57
58         //! Load the recently game list from the given file.
59         bool loadFromFile(std::string filename);
60
61         //! Add a game entry to the list of recently played games.
62         void addEntry(GameScenario *game_scenario, std::string filename);
63
64         //! Add a networked game entry to the list of recently played games.
65         void addNetworkedEntry(GameScenario *game_scenario, std::string host, guint32 port);
66
67         //! Touch the game in the recently played list.
68         void updateEntry(GameScenario *game_scenario);
69
70         //! Remove a game entry from the list, by it's scenario id.
71         bool removeEntry(std::string id);
72
73         //! Removes all networked games from the list.
74         void removeAllNetworkedGames();
75
76         //! Removes games from the list that are too old, or just too numerous.
77         void pruneGames();
78         
79
80         // Static Methods
81
82         //! return the singleton instance of this class.
83         static RecentlyPlayedGameList * getInstance();
84
85         //! Loads the singleton instance from an opened file.
86         static RecentlyPlayedGameList * getInstance(XML_Helper *helper);
87
88         //! Explicitly delete the singleton instance of this class.
89         static void deleteInstance();
90
91     protected:
92         //! Default Constructor.
93         RecentlyPlayedGameList();
94
95         //! Loading constructor
96         RecentlyPlayedGameList(XML_Helper *helper);
97         
98         //! Destructor.
99         ~RecentlyPlayedGameList();
100
101     private:
102         //! Callback for loading recentlyplayedgames into this list.
103         bool load(std::string tag, XML_Helper *helper);
104
105         //! Helper method to sort the list by it's last-played time.
106         static bool orderByTime(RecentlyPlayedGame*rhs, RecentlyPlayedGame *lhs);
107
108         //! Remove the old games from the list.
109         void pruneOldGames(int stale = TWO_WEEKS_OLD);
110
111         //! Remove extraneous games form the list.
112         void pruneTooManyGames(int too_many = 10);
113
114         // DATA
115
116         //! A static pointer for the singleton instance.
117         static RecentlyPlayedGameList* s_instance;
118 };
119
120 #endif // RECENTLYPLAYEDGAMELIST_H
121