1 /*
2  * Copyright (C) 2008-2010 Geometer Plus <contact@geometerplus.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  */
19 
20 #include <cstdlib>
21 
22 #include <ZLStringUtil.h>
23 
24 #include "../options/FBCategoryKey.h"
25 
26 #include "Migration.h"
27 
moveOption(const ZLCategoryKey & oldCategory,const std::string & oldGroup,const std::string & oldName,const ZLCategoryKey & newCategory,const std::string & newGroup,const std::string & newName,const std::string & defaultValue)28 void Migration::moveOption(
29 	const ZLCategoryKey &oldCategory, const std::string &oldGroup, const std::string &oldName,
30 	const ZLCategoryKey &newCategory, const std::string &newGroup, const std::string &newName,
31 	const std::string &defaultValue
32 ) {
33 	ZLStringOption newOption(newCategory, newGroup, newName, defaultValue);
34 	const std::string newValue = newOption.value();
35 	ZLStringOption oldOption(oldCategory, oldGroup, oldName, newValue);
36 	const std::string oldValue = oldOption.value();
37 	if (newValue != oldValue) {
38 		newOption.setValue(oldValue);
39 		oldOption.setValue(newValue);
40 	}
41 }
42 
isLikeToFileName(const std::string & str)43 bool Migration::isLikeToFileName(const std::string &str) {
44 	return
45 		ZLStringUtil::stringStartsWith(str, "/") ||
46 		ZLStringUtil::stringStartsWith(str, "\\\\") ||
47 		((str.length() > 2) && (str.substr(1, 2) == ":\\"));
48 }
49 
Migration(const std::string & version)50 Migration::Migration(const std::string &version) : myVersion(version) {
51 }
52 
~Migration()53 Migration::~Migration() {
54 }
55 
extractVersionInformation(const std::string & name)56 int Migration::extractVersionInformation(const std::string &name) {
57 	int major = atoi(name.c_str());
58 	int minor = 0;
59 	int point = 0;
60 	int index = name.find('.');
61 	if (index > 0) {
62 		minor = atoi(name.c_str() + index + 1);
63 		index = name.find('.', index + 1);
64 		if (index > 0) {
65 			point = atoi(name.c_str() + index + 1);
66 		}
67 	}
68 	return 10000 * major + 100 * minor + point;
69 }
70 
doMigration()71 void Migration::doMigration() {
72 	ZLStringOption versionOption(FBCategoryKey::SYSTEM, "Version", "FBReaderVersion", "0");
73 	if (extractVersionInformation(versionOption.value()) <
74 			extractVersionInformation(myVersion)) {
75 		doMigrationInternal();
76 	}
77 }
78