1 //  Copyright (C) 2011, 2014, 2015 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 #pragma once
19 #ifndef FILE_COMPAT_H
20 #define FILE_COMPAT_H
21 
22 #include <gtkmm.h>
23 #include <list>
24 #include <sigc++/trackable.h>
25 
26 class UpgradeDetails;
27 
28 //! A helper class for upgrading the schema of files.
29 class FileDetails
30 {
31 public:
FileDetails(guint32 k,Glib::ustring f,Glib::ustring t,bool ta)32   FileDetails(guint32 k, Glib::ustring f, Glib::ustring t, bool ta)
33     {type = k; file_extension = f; tag = t; tar = ta;};
34   guint32 type;
35   Glib::ustring file_extension;
36   Glib::ustring tag;
37   bool tar;
38 };
39 
40 //! An interface to provide backwards compatibility for files.
41 class FileCompat: public std::list<FileDetails>, public sigc::trackable
42 {
43     public:
44 
45         enum Type {
46           UNKNOWN = 0,
47           CONFIGURATION,
48           ITEMLIST,
49           PROFILELIST,
50           RECENTLYPLAYEDGAMELIST,
51           GAMELIST,
52           ARMYSET,
53           TILESET,
54           CITYSET,
55           SHIELDSET,
56           GAMESCENARIO
57         };
58 
59         //! upgrade common files.
60         void initialize();
61 
62         typedef sigc::slot<bool, Glib::ustring, Glib::ustring, Glib::ustring> Slot;
support_type(guint32 k,Glib::ustring f,Glib::ustring t,bool ta)63         void support_type (guint32 k, Glib::ustring f, Glib::ustring t, bool ta)
64           {push_back(FileDetails(k,f,t,ta));};
65         void support_version(guint32 k, Glib::ustring from, Glib::ustring to, FileCompat::Slot slot);
66 
67         bool contains(FileCompat::Type type) const;
68         FileCompat::Type getType(Glib::ustring filename) const;
69         FileCompat::Type getTypeByFileInspection(Glib::ustring filename, bool &tar) const;
70 
71         bool isTarFile(FileCompat::Type type) const;
72 
73         Glib::ustring getTag(FileCompat::Type type) const;
74         FileCompat::Type getTypeByFileExtension(Glib::ustring ext) const;
75         Glib::ustring getFileExtension(FileCompat::Type type) const;
76         std::list<Glib::ustring> getFileExtensions(FileCompat::Type type) const;
77         bool get_tag_and_version_from_file(Glib::ustring filename, FileCompat::Type type, Glib::ustring &tag, Glib::ustring &version) const;
78 
79         bool upgrade(Glib::ustring filename, bool &same) const;
80         bool upgrade(Glib::ustring filename, Glib::ustring old_version, Glib::ustring new_version, FileCompat::Type t, Glib::ustring tag) const;
81         bool rewrite_with_updated_version(Glib::ustring filename, FileCompat::Type type, Glib::ustring tag, Glib::ustring version) const;
82         bool upgradeGameScenario(Glib::ustring filename, Glib::ustring version, bool& upgraded_armyset, bool& upgraded_tileset, bool& upgraded_cityset, bool& upgraded_shieldset) const;
83 
84 	// Static Methods
85         static void support_backward_compatibility_for_common_files();
86         static Glib::ustring typeToString(const FileCompat::Type type);
87         static Glib::ustring typeToCode(const FileCompat::Type type);
88 
89         //! return the singleton instance of this class.
90         static FileCompat * getInstance();
91 
92         //! Explicitly delete the singleton instance of this class.
93         static void deleteInstance();
94 
95     protected:
96         //! Default Constructor.
97         FileCompat();
98 
99         //! Destructor.
~FileCompat()100         ~FileCompat() {};
101 
102         // helpers
103         FileCompat::Type getTypeByXmlFileInspection(Glib::ustring filename) const;
104         FileCompat::Type getTypeByTarFileInspection(Glib::ustring filename) const;
105 
106         bool upgradeGameScenarioWithXslt(Glib::ustring filename, Glib::ustring xsl_file, bool& armyset_upgraded, bool& tileset_upgraded, bool &cityset_upgraded, bool &shieldset_upgraded) const;
107 
108         bool can_upgrade_to(FileCompat::Type type, Glib::ustring version) const;
109         bool get_upgrade_method(FileCompat::Type type, Glib::ustring version, Glib::ustring &next_version, FileCompat::Slot &slot) const;
110         bool xsl_transform(Glib::ustring filename, Glib::ustring xsl_file) const;
111         bool rewrite_with_xslt(Glib::ustring filename, FileCompat::Type type, Glib::ustring xsl_file) const;
112 
113     private:
114 	// DATA
115         std::list<UpgradeDetails> versions[GAMESCENARIO + 1];
116 
117 
118         //! A static pointer for the singleton instance.
119         static FileCompat* s_instance;
120 };
121 
122 //! A helper class for providing backwards compatibility in game files.
123 class UpgradeDetails
124 {
125 public:
UpgradeDetails(Glib::ustring f,Glib::ustring t,FileCompat::Slot s)126   UpgradeDetails(Glib::ustring f, Glib::ustring t, FileCompat::Slot s)
127     {from_version = f; to_version = t; slot = s;};
128   Glib::ustring from_version;
129   Glib::ustring to_version;
130   FileCompat::Slot slot;
131 };
132 
133 
134 #endif // FILE_COMPAT_H
135 
136