]>
Commit | Line | Data |
---|---|---|
21ad818f JH |
1 | /* |
2 | * This file is part of the PulseView project. | |
3 | * | |
4 | * Copyright (C) 2013 Joel Holdsworth <joel@airwebreathe.org.uk> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
19 | */ | |
20 | ||
21 | #include "decoderoptions.h" | |
22 | ||
67fe5e9c JH |
23 | #include <boost/foreach.hpp> |
24 | #include <boost/none_t.hpp> | |
25 | ||
26 | #include <pv/prop/int.h> | |
27 | #include <pv/prop/string.h> | |
28 | ||
29 | using namespace boost; | |
30 | using namespace std; | |
31 | ||
21ad818f JH |
32 | namespace pv { |
33 | namespace prop { | |
34 | namespace binding { | |
35 | ||
67fe5e9c JH |
36 | DecoderOptions::DecoderOptions(const srd_decoder *decoder, |
37 | GHashTable *options) : | |
38 | _decoder(decoder), | |
39 | _options(options) | |
40 | { | |
41 | assert(decoder); | |
42 | ||
43 | ||
44 | for (GSList *l = decoder->options; l; l = l->next) | |
45 | { | |
46 | const srd_decoder_option *const opt = | |
47 | (srd_decoder_option*)l->data; | |
48 | ||
49 | const QString name(opt->desc); | |
50 | ||
51 | const Property::Getter getter = bind( | |
52 | &DecoderOptions::getter, this, opt->id); | |
53 | const Property::Setter setter = bind( | |
54 | &DecoderOptions::setter, this, opt->id, _1); | |
55 | ||
56 | shared_ptr<Property> prop; | |
57 | ||
58 | if (g_variant_is_of_type(opt->def, G_VARIANT_TYPE("x"))) | |
59 | prop = shared_ptr<Property>( | |
60 | new Int(name, "", none, getter, setter)); | |
61 | else if (g_variant_is_of_type(opt->def, G_VARIANT_TYPE("s"))) | |
62 | prop = shared_ptr<Property>( | |
63 | new String(name, getter, setter)); | |
64 | else | |
65 | continue; | |
66 | ||
67 | _properties.push_back(prop); | |
68 | } | |
69 | } | |
70 | ||
71 | GVariant* DecoderOptions::getter(const char *id) | |
72 | { | |
73 | // Get the value from the hash table if it is already present | |
74 | GVariant *val = (GVariant*)g_hash_table_lookup(_options, id); | |
75 | ||
76 | if (!val) | |
77 | { | |
78 | // Get the default value if not | |
79 | for (GSList *l = _decoder->options; l; l = l->next) | |
80 | { | |
81 | const srd_decoder_option *const opt = | |
82 | (srd_decoder_option*)l->data; | |
1429b21d | 83 | if (strcmp(opt->id, id) == 0) { |
67fe5e9c | 84 | val = opt->def; |
1429b21d BV |
85 | break; |
86 | } | |
67fe5e9c JH |
87 | } |
88 | } | |
89 | ||
90 | if (val) | |
91 | g_variant_ref(val); | |
92 | ||
93 | return val; | |
94 | } | |
95 | ||
96 | void DecoderOptions::setter(const char *id, GVariant *value) | |
21ad818f | 97 | { |
67fe5e9c | 98 | g_variant_ref(value); |
1429b21d | 99 | g_hash_table_insert(_options, (void*)g_strdup(id), value); |
21ad818f JH |
100 | } |
101 | ||
102 | } // binding | |
103 | } // prop | |
104 | } // pv |