@@ -78,159 +78,8 @@ bool AnkiConfig::load()
7878 return false ;
7979 }
8080
81- /* Error check the config */
81+ /* Get the config */
8282 QJsonObject jsonObj = jsonDoc.object ();
83- if (!jsonObj[Anki::Keys::ENABLED].isBool ())
84- {
85- setError (
86- tr (PREFIX_ERR_STR " \" %1\" is not a boolean" )
87- .arg (Anki::Keys::ENABLED)
88- );
89- return false ;
90- }
91- else if (!jsonObj[Anki::Keys::SET_PROFILE].isString ())
92- {
93- setError (
94- tr (PREFIX_ERR_STR " \" %1\" is not a string" )
95- .arg (Anki::Keys::SET_PROFILE)
96- );
97- return false ;
98- }
99- else if (!jsonObj[Anki::Keys::PROFILES].isArray ())
100- {
101- setError (
102- tr (PREFIX_ERR_STR " \" %1\" is not an array" )
103- .arg (Anki::Keys::PROFILES)
104- );
105- return false ;
106- }
107- QJsonArray profiles = jsonObj[Anki::Keys::PROFILES].toArray ();
108- for (qsizetype i = 0 ; i < profiles.size (); ++i)
109- {
110- if (!profiles[i].isObject ())
111- {
112- setError (
113- tr (PREFIX_ERR_STR " \" %1\" element is not a JSON object" )
114- .arg (Anki::Keys::PROFILES)
115- );
116- return false ;
117- }
118- QJsonObject profile = profiles[i].toObject ();
119-
120- /* Error check values */
121- if (!profile[Anki::Keys::NAME].isString ())
122- {
123- setError (
124- tr (PREFIX_ERR_STR " \" %1\" is not a string" )
125- .arg (Anki::Keys::NAME)
126- );
127- return false ;
128- }
129- else if (!profile[Anki::Keys::HOSTNAME].isString ())
130- {
131- setError (
132- tr (PREFIX_ERR_STR " \" %1\" is not a string" )
133- .arg (Anki::Keys::HOSTNAME)
134- );
135- return false ;
136- }
137- else if (!profile[Anki::Keys::PORT].isString ())
138- {
139- setError (
140- tr (PREFIX_ERR_STR " \" %1\" is not a string" )
141- .arg (Anki::Keys::PORT)
142- );
143- return false ;
144- }
145- else if (!profile[Anki::Keys::DUPLICATE_POLICY].isDouble ())
146- {
147- setError (
148- tr (PREFIX_ERR_STR " \" %1\" is not a double" )
149- .arg (Anki::Keys::DUPLICATE_POLICY)
150- );
151- return false ;
152- }
153- else if (!profile[Anki::Keys::NEWLINE_REPLACER].isString ())
154- {
155- setError (
156- tr (PREFIX_ERR_STR " \" %1\" is not a string" )
157- .arg (Anki::Keys::NEWLINE_REPLACER)
158- );
159- return false ;
160- }
161- else if (!profile[Anki::Keys::SCREENSHOT].isDouble ())
162- {
163- setError (
164- tr (PREFIX_ERR_STR " \" %1\" is not a double" )
165- .arg (Anki::Keys::SCREENSHOT)
166- );
167- return false ;
168- }
169- else if (!profile[Anki::Keys::AUDIO_PAD_START].isDouble ())
170- {
171- setError (
172- tr (PREFIX_ERR_STR " \" %1\" is not a double" )
173- .arg (Anki::Keys::AUDIO_PAD_START)
174- );
175- return false ;
176- }
177- else if (!profile[Anki::Keys::AUDIO_PAD_END].isDouble ())
178- {
179- setError (
180- tr (PREFIX_ERR_STR " \" %1\" is not a double" )
181- .arg (Anki::Keys::AUDIO_PAD_END)
182- );
183- return false ;
184- }
185- else if (!profile[Anki::Keys::AUDIO_NORMALIZE].isBool ())
186- {
187- setError (
188- tr (PREFIX_ERR_STR " \" %1\" is not a bool" )
189- .arg (Anki::Keys::AUDIO_NORMALIZE)
190- );
191- return false ;
192- }
193- else if (!profile[Anki::Keys::AUDIO_DB].isDouble ())
194- {
195- setError (
196- tr (PREFIX_ERR_STR " \" %1\" is not a double" )
197- .arg (Anki::Keys::AUDIO_DB)
198- );
199- return false ;
200- }
201- else if (!profile[Anki::Keys::TAGS].isArray ())
202- {
203- setError (
204- tr (PREFIX_ERR_STR " \" %1\" is not an array" )
205- .arg (Anki::Keys::TAGS)
206- );
207- return false ;
208- }
209- else if (!profile[Anki::Keys::TERM].isObject ())
210- {
211- setError (
212- tr (PREFIX_ERR_STR " \" %1\" is not an object" )
213- .arg (Anki::Keys::TERM)
214- );
215- return false ;
216- }
217- else if (!profile[Anki::Keys::KANJI].isObject ())
218- {
219- setError (
220- tr (PREFIX_ERR_STR " \" %1\" is not an object" )
221- .arg (Anki::Keys::KANJI)
222- );
223- return false ;
224- }
225- else if (!profile[Anki::Keys::EXCLUDE_GLOSSARIES].isArray ())
226- {
227- setError (
228- tr (PREFIX_ERR_STR " \" %1\" is not an array" )
229- .arg (Anki::Keys::EXCLUDE_GLOSSARIES)
230- );
231- return false ;
232- }
233- }
23483
23584 /* Clear out existing profiles */
23685 qDeleteAll (m_profiles);
@@ -241,20 +90,37 @@ bool AnkiConfig::load()
24190 m_enabled = jsonObj[Anki::Keys::ENABLED]
24291 .toBool (Anki::Keys::ENABLED_DEFAULT);
24392 QString currentProfile = jsonObj[Anki::Keys::SET_PROFILE].toString ();
93+ QJsonArray profiles = jsonObj[Anki::Keys::PROFILES].toArray ();
24494 for (const QJsonValue &val : profiles)
24595 {
24696 QJsonObject profile = val.toObject ();
24797
24898 AnkiProfile *ankiProfile = new AnkiProfile (this );
24999 ankiProfile->setName (
250- profile[Anki::Keys::NAME].toString (Anki::Keys::NAME_DEFAULT)
100+ profile[Anki::Keys::NAME]
101+ .toString (Anki::Keys::NAME_DEFAULT)
251102 );
252103 ankiProfile->setHostname (
253104 profile[Anki::Keys::HOSTNAME]
254105 .toString (Anki::Keys::HOSTNAME_DEFAULT)
255106 );
107+ /* Make sure that hostnames include http:// or https:// */
108+ if (!ankiProfile->hostname ().startsWith (" http://" ) &&
109+ !ankiProfile->hostname ().startsWith (" https://" ))
110+ {
111+ ankiProfile->setHostname (" http://" + ankiProfile->hostname ());
112+ }
256113 ankiProfile->setPort (
257- profile[Anki::Keys::PORT].toString (Anki::Keys::PORT)
114+ profile[Anki::Keys::PORT]
115+ .toString (Anki::Keys::PORT_DEFAULT)
116+ );
117+ ankiProfile->setUseApiKey (
118+ profile[Anki::Keys::USE_API_KEY]
119+ .toBool (Anki::Keys::USE_API_KEY_DEFAULT)
120+ );
121+ ankiProfile->setApiKey (
122+ profile[Anki::Keys::API_KEY]
123+ .toString (Anki::Keys::API_KEY_DEFAULT)
258124 );
259125 ankiProfile->setDuplicatePolicy (static_cast <Anki::DuplicatePolicy>(
260126 profile[Anki::Keys::DUPLICATE_POLICY]
@@ -332,6 +198,10 @@ bool AnkiConfig::load()
332198 return lhs->name () < rhs->name ();
333199 }
334200 );
201+ if (m_profiles.isEmpty ())
202+ {
203+ m_profiles.emplaceBack (new AnkiProfile{this });
204+ }
335205 if (m_profile == nullptr )
336206 {
337207 m_profile = m_profiles.first ();
@@ -377,6 +247,8 @@ bool AnkiConfig::write()
377247 configObj[Anki::Keys::NAME] = profile->name ();
378248 configObj[Anki::Keys::HOSTNAME] = profile->hostname ();
379249 configObj[Anki::Keys::PORT] = profile->port ();
250+ configObj[Anki::Keys::USE_API_KEY] = profile->useApiKey ();
251+ configObj[Anki::Keys::API_KEY] = profile->apiKey ();
380252 configObj[Anki::Keys::DUPLICATE_POLICY] = profile->duplicatePolicy ();
381253 configObj[Anki::Keys::NEWLINE_REPLACER] = profile->newlineReplacer ();
382254 configObj[Anki::Keys::SCREENSHOT] = profile->screenshotType ();
0 commit comments