plugin code
[lbook_fbreader.git] / fbreader / src / encodingOption / EncodingOptionEntry.cpp
blob7d807c14db25d63bc72160db6a302a8e7a49c217
1 /*
2 * Copyright (C) 2004-2008 Geometer Plus <contact@geometerplus.com>
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.
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.
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.
20 #include <shared_ptr.h>
21 #include <ZLEncodingConverter.h>
23 #include "EncodingOptionEntry.h"
25 static const std::string AUTO = "auto";
27 EncodingEntry::EncodingEntry(ZLStringOption &encodingOption) : myEncodingOption(encodingOption) {
28 const std::string &value = myEncodingOption.value();
29 if (value == AUTO) {
30 myInitialSetName = value;
31 myInitialValues[value] = value;
32 setActive(false);
33 return;
36 const std::vector<shared_ptr<ZLEncodingSet> > &sets = ZLEncodingCollection::instance().sets();
37 for (std::vector<shared_ptr<ZLEncodingSet> >::const_iterator it = sets.begin(); it != sets.end(); ++it) {
38 const std::vector<ZLEncodingConverterInfoPtr> &infos = (*it)->infos();
39 mySetNames.push_back((*it)->name());
40 std::vector<std::string> &names = myValues[(*it)->name()];
41 for (std::vector<ZLEncodingConverterInfoPtr>::const_iterator jt = infos.begin(); jt != infos.end(); ++jt) {
42 if ((*jt)->name() == value) {
43 myInitialSetName = (*it)->name();
44 myInitialValues[myInitialSetName] = (*jt)->visibleName();
46 names.push_back((*jt)->visibleName());
47 myValueByName[(*jt)->visibleName()] = (*jt)->name();
51 if (myInitialSetName.empty()) {
52 myInitialSetName = mySetNames[0];
56 const std::vector<std::string> &EncodingEntry::values() const {
57 if (initialValue() == AUTO) {
58 static std::vector<std::string> AUTO_ENCODING;
59 if (AUTO_ENCODING.empty()) {
60 AUTO_ENCODING.push_back(AUTO);
62 return AUTO_ENCODING;
64 std::map<std::string,std::vector<std::string> >::const_iterator it = myValues.find(myInitialSetName);
65 return it->second;
68 const std::string &EncodingEntry::initialValue() const {
69 if (myInitialValues[myInitialSetName].empty()) {
70 std::map<std::string,std::vector<std::string> >::const_iterator it = myValues.find(myInitialSetName);
71 myInitialValues[myInitialSetName] = it->second[0];
73 return myInitialValues[myInitialSetName];
76 void EncodingEntry::onAccept(const std::string &value) {
77 if (initialValue() != AUTO) {
78 myEncodingOption.setValue(myValueByName[value]);
82 void EncodingEntry::onValueSelected(int index) {
83 myInitialValues[myInitialSetName] = values()[index];
86 EncodingSetEntry::EncodingSetEntry(EncodingEntry &encodingEntry) : myEncodingEntry(encodingEntry) {
89 const std::string &EncodingSetEntry::initialValue() const {
90 return myEncodingEntry.myInitialSetName;
93 const std::vector<std::string> &EncodingSetEntry::values() const {
94 return myEncodingEntry.mySetNames;
97 void EncodingSetEntry::onValueSelected(int index) {
98 myEncodingEntry.myInitialSetName = values()[index];
99 myEncodingEntry.resetView();