forked from thjaeger/easystroke
-
Notifications
You must be signed in to change notification settings - Fork 1
/
prefdb.cc
212 lines (202 loc) · 7.67 KB
/
prefdb.cc
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
* Copyright (c) 2008-2009, Thomas Jaeger <[email protected]>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "prefdb.h"
#include "main.h"
#include "win.h"
#include <glibmm/i18n.h>
#include <fstream>
#include <iostream>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/set.hpp>
#include <boost/serialization/vector.hpp>
#include <boost/serialization/map.hpp>
#include <boost/serialization/shared_ptr.hpp>
#include <boost/serialization/export.hpp>
const ButtonInfo default_button(Button2);
PrefDB::PrefDB() :
TimeoutWatcher(5000),
good_state(true),
button(default_button),
trace(TraceDefault),
advanced_ignore(false),
proximity(false),
feedback(true),
left_handed(false),
init_timeout(250),
final_timeout(250),
timeout_profile(TimeoutDefault),
timeout_gestures(false),
tray_icon(false),
color(Gdk::Color("#980101")),
trace_width(3),
advanced_popups(true),
scroll_invert(true),
scroll_speed(2.0),
tray_feedback(false),
show_osd(true),
move_back(false),
whitelist(false)
{}
template<class Archive> void PrefDB::serialize(Archive & ar, const unsigned int version) {
if (version < 11) {
std::set<std::string> old;
ar & boost::serialization::make_nvp("exceptions", old);
for (std::set<std::string>::iterator i = old.begin(); i != old.end(); i++)
exceptions.unsafe_ref()[*i] = RButtonInfo();
} else ar & boost::serialization::make_nvp("exceptions", exceptions.unsafe_ref());
if (version < 14) {
double p = 0.5;
ar & boost::serialization::make_nvp("p", p);
}
ar & boost::serialization::make_nvp("button", button.unsafe_ref());
if (version < 2) {
bool help;
ar & boost::serialization::make_nvp("help", help);
}
ar & boost::serialization::make_nvp("trace", trace.unsafe_ref());
if (trace.get() == TraceShape)
trace.unsafe_ref() = TraceDefault;
if (version < 3) {
int delay;
ar & boost::serialization::make_nvp("delay", delay);
}
if (version == 1) {
ButtonInfo foo;
ar & boost::serialization::make_nvp("foo", foo);
ar & boost::serialization::make_nvp("foo", foo);
return;
}
if (version < 2) return;
if (version != 6)
ar & boost::serialization::make_nvp("advanced_ignore", advanced_ignore.unsafe_ref());
int radius = 16;
ar & boost::serialization::make_nvp("radius", radius);
if (version < 4) return;
bool ignore_grab = false;
ar & boost::serialization::make_nvp("ignore_grab", ignore_grab);
bool timing_workaround = false;
ar & boost::serialization::make_nvp("timing_workaround", timing_workaround);
bool show_clicks = false;
ar & boost::serialization::make_nvp("show_clicks", show_clicks);
bool pressure_abort = false;
ar & boost::serialization::make_nvp("pressure_abort", pressure_abort);
int pressure_threshold = 192;
ar & boost::serialization::make_nvp("pressure_threshold", pressure_threshold);
ar & boost::serialization::make_nvp("proximity", proximity.unsafe_ref());
if (version < 5) return;
ar & boost::serialization::make_nvp("feedback", feedback.unsafe_ref());
ar & boost::serialization::make_nvp("left_handed", left_handed.unsafe_ref());
ar & boost::serialization::make_nvp("init_timeout", init_timeout.unsafe_ref());
ar & boost::serialization::make_nvp("final_timeout", final_timeout.unsafe_ref());
if (version < 8) return;
ar & boost::serialization::make_nvp("timeout_profile", timeout_profile.unsafe_ref());
if (version < 9) return;
ar & boost::serialization::make_nvp("timeout_gestures", timeout_gestures.unsafe_ref());
ar & boost::serialization::make_nvp("tray_icon", tray_icon.unsafe_ref());
if (version < 10) return;
ar & boost::serialization::make_nvp("excluded_devices", excluded_devices.unsafe_ref());
if (version < 12) {
unsigned long c = 0;
ar & boost::serialization::make_nvp("color", c);
color.unsafe_ref().color.set_rgb(257*(c >> 16), 257*((c >> 8) % 256), 257*(c % 256));
return;
} else {
ar & boost::serialization::make_nvp("color", color.unsafe_ref());
}
ar & boost::serialization::make_nvp("trace_width", trace_width.unsafe_ref());
if (version < 13) return;
ar & boost::serialization::make_nvp("extra_buttons", extra_buttons.unsafe_ref());
ar & boost::serialization::make_nvp("advanced_popups", advanced_popups.unsafe_ref());
ar & boost::serialization::make_nvp("scroll_invert", scroll_invert.unsafe_ref());
ar & boost::serialization::make_nvp("scroll_speed", scroll_speed.unsafe_ref());
ar & boost::serialization::make_nvp("tray_feedback", tray_feedback.unsafe_ref());
ar & boost::serialization::make_nvp("show_osd", show_osd.unsafe_ref());
if (version < 16) return;
ar & boost::serialization::make_nvp("move_back", move_back.unsafe_ref());
if (version < 17) return;
ar & boost::serialization::make_nvp("device_timeout", device_timeout.unsafe_ref());
if (version < 18) return;
ar & boost::serialization::make_nvp("whitelist", whitelist.unsafe_ref());
}
void PrefDB::timeout() {
std::string filename = config_dir+"preferences"+prefs_versions[0];
std::string tmp = filename + ".tmp";
try {
std::ofstream ofs(tmp.c_str());
{
boost::archive::xml_oarchive oa(ofs);
oa << boost::serialization::make_nvp("PrefDB", *this);
}
ofs.close();
if (rename(tmp.c_str(), filename.c_str()))
throw std::runtime_error("rename() failed");
if (verbosity >= 2)
printf("Saved preferences.\n");
} catch (std::exception &e) {
printf(_("Error: Couldn't save preferences: %s.\n"), e.what());
if (!good_state)
return;
good_state = false;
error_dialog(Glib::ustring::compose(_( "Couldn't save %1. Your changes will be lost. "
"Make sure that \"%2\" is a directory and that you have write access to it. "
"You can change the configuration directory "
"using the -c or --config-dir command line options."), _("preferences"), config_dir));
}
}
bool ButtonInfo::overlap(const ButtonInfo &bi) const {
if (button != bi.button)
return false;
if (state == AnyModifier || bi.state == AnyModifier)
return true;
return !((state ^ bi.state) & ~LockMask & ~Mod2Mask);
}
#include <boost/algorithm/string/predicate.hpp>
void PrefDB::init() {
std::string filename = config_dir+"preferences";
for (const char **v = prefs_versions; *v; v++) {
if (is_file(filename + *v)) {
filename += *v;
try {
std::ifstream ifs(filename.c_str(), std::ios::binary);
if (!ifs.fail()) {
if(boost::algorithm::ends_with(filename, ".xml")) {
boost::archive::xml_iarchive ia(ifs);
ia >> boost::serialization::make_nvp("PrefDB", *this);
}
else {
boost::archive::text_iarchive ia(ifs);
ia >> boost::serialization::make_nvp("PrefDB", *this);
}
if (verbosity >= 2)
std::cout << "Loaded preferences." << std::endl;
}
} catch (...) {
printf(_("Error: Couldn't read preferences.\n"));
}
break;
}
}
std::map<std::string, RButtonInfo>::iterator i = exceptions.unsafe_ref().find("(window manager frame)");
if (i != exceptions.unsafe_ref().end()) {
RButtonInfo bi = i->second;
exceptions.unsafe_ref().erase(i);
exceptions.unsafe_ref()[""] = bi;
}
}
PrefDB prefs;