summaryrefslogtreecommitdiffstats
path: root/fbreader/src/encodingOption
diff options
context:
space:
mode:
Diffstat (limited to 'fbreader/src/encodingOption')
-rw-r--r--fbreader/src/encodingOption/EncodingOptionEntry.cpp122
-rw-r--r--fbreader/src/encodingOption/EncodingOptionEntry.h74
2 files changed, 196 insertions, 0 deletions
diff --git a/fbreader/src/encodingOption/EncodingOptionEntry.cpp b/fbreader/src/encodingOption/EncodingOptionEntry.cpp
new file mode 100644
index 0000000..04d25d0
--- /dev/null
+++ b/fbreader/src/encodingOption/EncodingOptionEntry.cpp
@@ -0,0 +1,122 @@
+/*
+ * Copyright (C) 2004-2012 Geometer Plus <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+#include <shared_ptr.h>
+#include <ZLEncodingConverter.h>
+#include <ZLUnicodeUtil.h>
+
+#include "EncodingOptionEntry.h"
+
+#include "../library/Book.h"
+
+AbstractEncodingEntry::AbstractEncodingEntry(const std::string &currentValue) {
+ if (currentValue == Book::AutoEncoding) {
+ myInitialSetName = currentValue;
+ myInitialValues[currentValue] = currentValue;
+ setActive(false);
+ return;
+ }
+
+ const std::string &value = ZLUnicodeUtil::toLower(currentValue);
+
+ const std::vector<shared_ptr<ZLEncodingSet> > &sets = ZLEncodingCollection::Instance().sets();
+ for (std::vector<shared_ptr<ZLEncodingSet> >::const_iterator it = sets.begin(); it != sets.end(); ++it) {
+ const std::vector<ZLEncodingConverterInfoPtr> &infos = (*it)->infos();
+ mySetNames.push_back((*it)->name());
+ std::vector<std::string> &names = myValues[(*it)->name()];
+ for (std::vector<ZLEncodingConverterInfoPtr>::const_iterator jt = infos.begin(); jt != infos.end(); ++jt) {
+ const std::vector<std::string> &aliases = (*jt)->aliases();
+ for (std::vector<std::string>::const_iterator kt = aliases.begin(); kt != aliases.end(); ++kt) {
+ if (value == ZLUnicodeUtil::toLower(*kt)) {
+ myInitialSetName = (*it)->name();
+ myInitialValues[myInitialSetName] = (*jt)->visibleName();
+ break;
+ }
+ }
+ names.push_back((*jt)->visibleName());
+ myValueByName[(*jt)->visibleName()] = (*jt)->name();
+ }
+ }
+
+ if (myInitialSetName.empty()) {
+ myInitialSetName = mySetNames[0];
+ }
+}
+
+const std::vector<std::string> &AbstractEncodingEntry::values() const {
+ if (initialValue() == Book::AutoEncoding) {
+ static std::vector<std::string> AUTO_ENCODING;
+ if (AUTO_ENCODING.empty()) {
+ AUTO_ENCODING.push_back(Book::AutoEncoding);
+ }
+ return AUTO_ENCODING;
+ }
+ std::map<std::string,std::vector<std::string> >::const_iterator it = myValues.find(myInitialSetName);
+ return it->second;
+}
+
+const std::string &AbstractEncodingEntry::initialValue() const {
+ if (myInitialValues[myInitialSetName].empty()) {
+ std::map<std::string,std::vector<std::string> >::const_iterator it = myValues.find(myInitialSetName);
+ myInitialValues[myInitialSetName] = it->second[0];
+ }
+ return myInitialValues[myInitialSetName];
+}
+
+void AbstractEncodingEntry::onAccept(const std::string &value) {
+ if (initialValue() != Book::AutoEncoding) {
+ onAcceptValue(myValueByName[value]);
+ }
+}
+
+void AbstractEncodingEntry::onValueSelected(int index) {
+ myInitialValues[myInitialSetName] = values()[index];
+}
+
+
+
+
+
+EncodingEntry::EncodingEntry(ZLStringOption &encodingOption) :
+ AbstractEncodingEntry(encodingOption.value()),
+ myEncodingOption(encodingOption) {
+}
+
+void EncodingEntry::onAcceptValue(const std::string &value) {
+ myEncodingOption.setValue(value);
+}
+
+
+
+
+EncodingSetEntry::EncodingSetEntry(AbstractEncodingEntry &encodingEntry) : myEncodingEntry(encodingEntry) {
+}
+
+const std::string &EncodingSetEntry::initialValue() const {
+ return myEncodingEntry.myInitialSetName;
+}
+
+const std::vector<std::string> &EncodingSetEntry::values() const {
+ return myEncodingEntry.mySetNames;
+}
+
+void EncodingSetEntry::onValueSelected(int index) {
+ myEncodingEntry.myInitialSetName = values()[index];
+ myEncodingEntry.resetView();
+}
diff --git a/fbreader/src/encodingOption/EncodingOptionEntry.h b/fbreader/src/encodingOption/EncodingOptionEntry.h
new file mode 100644
index 0000000..7b43c60
--- /dev/null
+++ b/fbreader/src/encodingOption/EncodingOptionEntry.h
@@ -0,0 +1,74 @@
+/*
+ * Copyright (C) 2004-2012 Geometer Plus <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+#ifndef __ENCODINGOPTIONENTRY_H__
+#define __ENCODINGOPTIONENTRY_H__
+
+#include <map>
+
+#include <ZLOptionEntry.h>
+
+class AbstractEncodingEntry : public ZLComboOptionEntry {
+
+public:
+ AbstractEncodingEntry(const std::string &currentValue);
+
+ const std::string &initialValue() const;
+ const std::vector<std::string> &values() const;
+ void onAccept(const std::string &value);
+ void onValueSelected(int index);
+
+ virtual void onAcceptValue(const std::string &value) = 0;
+
+private:
+ std::vector<std::string> mySetNames;
+ std::map<std::string,std::vector<std::string> > myValues;
+ mutable std::map<std::string,std::string> myInitialValues;
+ std::map<std::string,std::string> myValueByName;
+ std::string myInitialSetName;
+
+friend class EncodingSetEntry;
+};
+
+class EncodingEntry : public AbstractEncodingEntry {
+
+public:
+ EncodingEntry(ZLStringOption &encodingOption);
+
+ void onAcceptValue(const std::string &value);
+
+private:
+ ZLStringOption &myEncodingOption;
+};
+
+class EncodingSetEntry : public ZLComboOptionEntry {
+
+public:
+ EncodingSetEntry(AbstractEncodingEntry &encodingEntry);
+
+ const std::string &initialValue() const;
+ const std::vector<std::string> &values() const;
+ void onAccept(const std::string&) {}
+ void onValueSelected(int index);
+
+private:
+ AbstractEncodingEntry &myEncodingEntry;
+};
+
+#endif /* __ENCODINGOPTIONENTRY_H__ */