summaryrefslogtreecommitdiffstats
path: root/fbreader/src/optionsDialog/IntegrationTab.cpp
blob: ca3ebf0f4a11ea3559cfd66f1c7d274430da71bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
 * 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 <ZLOptionsDialog.h>
#include <ZLOptionEntry.h>
#include <ZLStringUtil.h>

#include <ZLToggleBooleanOptionEntry.h>
#include <ZLSimpleOptionEntry.h>

#include "AbstractOptionsDialog.h"

#include "../options/FBCategoryKey.h"
#include "../external/ProgramCollection.h"


class ProgramChoiceEntry : public ZLComboOptionEntry {

public:
	ProgramChoiceEntry(const ProgramCollection &collection);

private:
	const std::vector<std::string> &values() const;
	void onAccept(const std::string &value);
	void onValueSelected(int index);

public:
	const std::string &initialValue() const;
	void addDependentEntry(const std::string &name, ZLOptionEntry *dependentEntry);
	void updateDependentEntries(bool visible);

private:
	const ProgramCollection &myCollection;
	std::string myValue;
	std::map<ZLOptionEntry*,std::string> myDependentEntries;
};

class EnableIntegrationEntry : public ZLToggleBooleanOptionEntry {

public:
	EnableIntegrationEntry(ZLBooleanOption &option);
	void setProgramChoiceEntry(ProgramChoiceEntry *programChoiceEntry);
	void onStateChanged(bool state);

private:
	ProgramChoiceEntry *myProgramChoiceEntry;
};

ProgramChoiceEntry::ProgramChoiceEntry(const ProgramCollection &collection) : myCollection(collection) {
	myValue = initialValue();
}

const std::string &ProgramChoiceEntry::initialValue() const {
	return myCollection.CurrentNameOption.value();
}

const std::vector<std::string> &ProgramChoiceEntry::values() const {
	return myCollection.names();
}

void ProgramChoiceEntry::onAccept(const std::string &value) {
	myCollection.CurrentNameOption.setValue(value);
}

void ProgramChoiceEntry::addDependentEntry(const std::string &name, ZLOptionEntry *dependentEntry) {
	myDependentEntries[dependentEntry] = name;
}

void ProgramChoiceEntry::onValueSelected(int index) {
	myValue = values()[index];
	updateDependentEntries(true);
}

void ProgramChoiceEntry::updateDependentEntries(bool visible) {
	for (std::map<ZLOptionEntry*,std::string>::const_iterator it = myDependentEntries.begin(); it != myDependentEntries.end(); ++it) {
		it->first->setVisible(visible && (it->second == myValue));
	}
}

EnableIntegrationEntry::EnableIntegrationEntry(ZLBooleanOption &option) : ZLToggleBooleanOptionEntry(option), myProgramChoiceEntry(0) {
}

void EnableIntegrationEntry::setProgramChoiceEntry(ProgramChoiceEntry *programChoiceEntry) {
	addDependentEntry(programChoiceEntry);
	myProgramChoiceEntry = programChoiceEntry;
}

void EnableIntegrationEntry::onStateChanged(bool state) {
	ZLToggleBooleanOptionEntry::onStateChanged(state);
	if (myProgramChoiceEntry != 0) {
		myProgramChoiceEntry->updateDependentEntries(state);
	}
}

void AbstractOptionsDialog::createIntegrationTab(shared_ptr<ProgramCollection> collection, const ZLResourceKey &key, std::vector<std::pair<ZLResourceKey,ZLOptionEntry*> > &additionalOptions) {
	if (!collection.isNull()) {
		const std::vector<std::string> &programNames = collection->names();
		if (!programNames.empty()) {
			ZLDialogContent &tab = myDialog->createTab(key);
			std::string optionName;
			if (programNames.size() == 1) {
				optionName = ZLStringUtil::printf(tab.value(ZLResourceKey("enableIntegration")), programNames[0]);
			} else {
				optionName = tab.value(ZLResourceKey("defaultText"));
			}
			EnableIntegrationEntry *enableIntegrationEntry =
				new EnableIntegrationEntry(collection->EnableCollectionOption);
			tab.addOption(optionName, "", enableIntegrationEntry);

			ProgramChoiceEntry *programChoiceEntry = 0;
			if (programNames.size() > 1) {
				programChoiceEntry = new ProgramChoiceEntry(*collection);
				tab.addOption(ZLResourceKey("choice"), programChoiceEntry);
				enableIntegrationEntry->setProgramChoiceEntry(programChoiceEntry);
			}

			for (std::vector<std::string>::const_iterator it = programNames.begin(); it != programNames.end(); ++it) {
				const std::vector<Program::OptionDescription> &options = collection->program(*it)->options();
				for (std::vector<Program::OptionDescription>::const_iterator jt = options.begin(); jt != options.end(); ++jt) {
					ZLStringOption *parameterOption = new ZLStringOption(FBCategoryKey::EXTERNAL, *it, jt->OptionName, jt->DefaultValue);
					storeTemporaryOption(parameterOption);
					ZLOptionEntry *parameterEntry = new ZLSimpleStringOptionEntry(*parameterOption);
					if (programChoiceEntry != 0) {
						programChoiceEntry->addDependentEntry(*it, parameterEntry);
					} else {
						enableIntegrationEntry->addDependentEntry(parameterEntry);
					}
					tab.addOption(ZLResourceKey(jt->OptionName), parameterEntry);
				}
			}
			for (std::vector<std::pair<ZLResourceKey,ZLOptionEntry*> >::const_iterator it = additionalOptions.begin(); it != additionalOptions.end(); ++it) {
				enableIntegrationEntry->addDependentEntry(it->second);
				tab.addOption(it->first, it->second);
			}
			enableIntegrationEntry->onStateChanged(enableIntegrationEntry->initialState());
			return;
		}
	}

	for (std::vector<std::pair<ZLResourceKey,ZLOptionEntry*> >::const_iterator it = additionalOptions.begin(); it != additionalOptions.end(); ++it) {
		delete it->second;
	}
}