Files
MagicSetEditor2/src/data/settings.cpp
T
twanvl d2196eea09 Finally got precompiled headers to work.
Now all C++ files need to #include <util/prec.hpp>
 That is why all .cpp files are touched by this commit

Many changes to installers and update checking:
     - the window is now called PackagesWindow, in a new source file
     - update checking is now independent from the PackagesWindow. For update checking only a list of package versions are needed (vector<PackageDependency>). This is much less information to download at each startup.
     - the list of available packages is now a list of available Installers, since an installer can contain multiple packages.
     - moved the logic of dependency checking etc. to data/installer
     - moved the actual installation to util/io/package_manager
     - moved directory iteration/creation logic to util/file_utils
     - added PackageDirectory: the local and global package directory now have their own object (was part of PackageManager)
     - added PackageVersion: for detecting if a package has been modified after it was installed.
     - added PackageDescription: description/header of a package. Basicly the same as what Packaged provides.
     - added DownloadableInstaller: where to find an insaller, what does it contain?
     - added InstallablePackage: brining it all together: installer, package, status, action.

Current status: the insaller is currently broken in a few places, more on that soon.

git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@792 0fc631ac-6414-0410-93d0-97cfa31319b6
2007-12-29 18:30:41 +00:00

275 lines
8.7 KiB
C++

//+----------------------------------------------------------------------------+
//| Description: Magic Set Editor - Program to make Magic (tm) cards |
//| Copyright: (C) 2001 - 2007 Twan van Laarhoven |
//| License: GNU General Public License 2 or later (see file COPYING) |
//+----------------------------------------------------------------------------+
// ----------------------------------------------------------------------------- : Includes
#include <util/prec.hpp>
#include <data/settings.hpp>
#include <data/installer.hpp>
#include <data/game.hpp>
#include <data/stylesheet.hpp>
#include <data/field.hpp>
#include <data/export_template.hpp>
#include <data/word_list.hpp>
#include <util/reflect.hpp>
#include <util/platform.hpp>
#include <util/io/reader.hpp>
#include <util/io/writer.hpp>
#include <util/delayed_index_maps.hpp>
#include <wx/filename.h>
#include <wx/wfstream.h>
#include <wx/stdpaths.h>
DECLARE_TYPEOF_COLLECTION(AutoReplaceP);
// ----------------------------------------------------------------------------- : Extra types
IMPLEMENT_REFLECTION_ENUM(CheckUpdates) {
VALUE_N("if connected", CHECK_IF_CONNECTED); //default
VALUE_N("always", CHECK_ALWAYS);
VALUE_N("never", CHECK_NEVER);
}
IMPLEMENT_REFLECTION_ENUM(InstallType) {
VALUE_N("default", INSTALL_DEFAULT); //default
VALUE_N("local", INSTALL_LOCAL);
VALUE_N("global", INSTALL_GLOBAL);
}
bool is_install_local(InstallType type) {
#ifdef __WXMSW__
#define DEFAULT_INSTALL_LOCAL false
#else
#define DEFAULT_INSTALL_LOCAL true
#endif
return type == INSTALL_DEFAULT ? DEFAULT_INSTALL_LOCAL : type == INSTALL_LOCAL;
}
IMPLEMENT_REFLECTION_ENUM(FilenameConflicts) {
VALUE_N("keep old", CONFLICT_KEEP_OLD);
VALUE_N("overwrite", CONFLICT_OVERWRITE);
VALUE_N("number", CONFLICT_NUMBER);
VALUE_N("number overwrite", CONFLICT_NUMBER_OVERWRITE);
}
const int COLUMN_NOT_INITIALIZED = -100000;
ColumnSettings::ColumnSettings()
: width(100), position(COLUMN_NOT_INITIALIZED), visible(false)
{}
// dummy for ColumnSettings reflection
ScriptValueP to_script(const ColumnSettings&) { return script_nil; }
IMPLEMENT_REFLECTION_NO_SCRIPT(ColumnSettings) {
REFLECT(width);
REFLECT(position);
REFLECT(visible);
}
GameSettings::GameSettings()
: sort_cards_ascending(true)
, images_export_filename(_("{card.name}.jpg"))
, images_export_conflicts(CONFLICT_NUMBER_OVERWRITE)
, use_auto_replace(true)
, initialized(false)
{}
void GameSettings::initDefaults(const Game& game) {
if (initialized) return;
initialized = true;
// init auto_replaces, copy from game file
FOR_EACH_CONST(ar, game.auto_replaces) {
// do we have this one?
bool already_have = false;
FOR_EACH(ar2, auto_replaces) {
if (ar->match == ar2->match) {
ar2->custom = false;
already_have = true;
break;
}
}
if (!already_have) {
// TODO: when we start saving games, clone here
ar->custom = false;
auto_replaces.push_back(ar);
}
}
}
IMPLEMENT_REFLECTION_NO_SCRIPT(GameSettings) {
REFLECT(default_stylesheet);
REFLECT(default_export);
REFLECT_N("cardlist columns", columns);
REFLECT(sort_cards_by);
REFLECT(sort_cards_ascending);
REFLECT(images_export_filename);
REFLECT(images_export_conflicts);
REFLECT(use_auto_replace);
REFLECT(auto_replaces);
}
StyleSheetSettings::StyleSheetSettings()
: card_zoom (1.0, true)
, card_angle (0, true)
, card_anti_alias (true, true)
, card_borders (true, true)
, card_draw_editing (true, true)
, card_normal_export(true, true)
{}
void StyleSheetSettings::useDefault(const StyleSheetSettings& ss) {
if (card_zoom .isDefault()) card_zoom .assignDefault(ss.card_zoom);
if (card_angle .isDefault()) card_angle .assignDefault(ss.card_angle);
if (card_anti_alias .isDefault()) card_anti_alias .assignDefault(ss.card_anti_alias);
if (card_borders .isDefault()) card_borders .assignDefault(ss.card_borders);
if (card_draw_editing .isDefault()) card_draw_editing .assignDefault(ss.card_draw_editing);
if (card_normal_export.isDefault()) card_normal_export.assignDefault(ss.card_normal_export);
}
IMPLEMENT_REFLECTION_NO_SCRIPT(StyleSheetSettings) {
REFLECT(card_zoom);
REFLECT(card_angle);
REFLECT(card_anti_alias);
REFLECT(card_borders);
REFLECT(card_draw_editing);
REFLECT(card_normal_export);
}
// ----------------------------------------------------------------------------- : Settings
Settings settings;
Settings::Settings()
: locale (_("en"))
, set_window_maximized (false)
, set_window_width (790)
, set_window_height (300)
, card_notes_height (40)
, symbol_grid_size (30)
, symbol_grid (true)
, symbol_grid_snap (false)
, package_versions_url (_("http://magicseteditor.sourceforge.net/packages"))
, installer_list_url (_("http://magicseteditor.sourceforge.net/installers"))
, check_updates (CHECK_IF_CONNECTED)
, check_updates_all (true)
, install_type (INSTALL_DEFAULT)
, website_url (_("http://magicseteditor.sourceforge.net/"))
{}
void Settings::addRecentFile(const String& filename) {
// get absolute path
wxFileName fn(filename);
fn.Normalize();
String filenameAbs = fn.GetFullPath();
// remove duplicates
recent_sets.erase(
remove(recent_sets.begin(), recent_sets.end(), filenameAbs),
recent_sets.end()
);
// add to front of list
recent_sets.insert(recent_sets.begin(), filenameAbs);
// enforce size limit
if (recent_sets.size() > max_recent_sets) recent_sets.resize(max_recent_sets);
}
GameSettings& Settings::gameSettingsFor(const Game& game) {
GameSettingsP& gs = game_settings[game.name()];
if (!gs) gs = new_intrusive<GameSettings>();
gs->initDefaults(game);
return *gs;
}
ColumnSettings& Settings::columnSettingsFor(const Game& game, const Field& field) {
// Get game info
GameSettings& gs = gameSettingsFor(game);
// Get column info
ColumnSettings& cs = gs.columns[field.name];
if (cs.position == COLUMN_NOT_INITIALIZED) {
// column info not set, initialize based on the game
cs.visible = field.card_list_visible;
cs.position = field.card_list_column;
cs.width = field.card_list_width;
}
return cs;
}
StyleSheetSettings& Settings::stylesheetSettingsFor(const StyleSheet& stylesheet) {
StyleSheetSettingsP& ss = stylesheet_settings[stylesheet.name()];
if (!ss) ss = new_intrusive<StyleSheetSettings>();
ss->useDefault(default_stylesheet_settings); // update default settings
return *ss;
}
IndexMap<FieldP,ValueP>& Settings::exportOptionsFor(const ExportTemplate& export_template) {
return export_options.get(export_template.name(), export_template.option_fields);
}
/// Retrieve the directory to use for settings and other data files
String user_settings_dir() {
String dir = wxStandardPaths::Get().GetUserDataDir();
if (!wxDirExists(dir)) wxMkdir(dir);
return dir + _("/");
}
String Settings::settingsFile() {
// return user_settings_dir() + _("mse.config");
return user_settings_dir() + _("mse8.config"); // use different file during development of C++ port
}
IMPLEMENT_REFLECTION_NO_SCRIPT(Settings) {
REFLECT_ALIAS(300, "style settings", "stylesheet settings");
REFLECT_ALIAS(300, "default style settings", "default stylesheet settings");
REFLECT(locale);
REFLECT(recent_sets);
REFLECT(set_window_maximized);
REFLECT(set_window_width);
REFLECT(set_window_height);
REFLECT(card_notes_height);
REFLECT(symbol_grid_size);
REFLECT(symbol_grid);
REFLECT(symbol_grid_snap);
REFLECT(default_game);
REFLECT(apprentice_location);
REFLECT_IGNORE(306,"updates url");
REFLECT(package_versions_url);
REFLECT(installer_list_url);
REFLECT(check_updates);
REFLECT(check_updates_all);
REFLECT(install_type);
REFLECT(website_url);
REFLECT(game_settings);
REFLECT(stylesheet_settings);
REFLECT(default_stylesheet_settings);
REFLECT(export_options);
}
void Settings::clear() {
recent_sets.clear();
game_settings.clear();
stylesheet_settings.clear();
default_stylesheet_settings = StyleSheetSettings();
export_options.clear();
}
void Settings::read() {
// clear current settings, otherwise we duplicate vector elements
clear();
// (re)load settings
String filename = settingsFile();
if (wxFileExists(filename)) {
// settings file not existing is not an error
shared_ptr<wxFileInputStream> file = new_shared1<wxFileInputStream>(filename);
if (!file->Ok()) return; // failure is not an error
Reader reader(file, nullptr, filename);
reader.handle_greedy(*this);
}
}
void Settings::write() {
Writer writer(new_shared1<wxFileOutputStream>(settingsFile()));
writer.handle(*this);
}