1 /***********************************************************************
2  *
3  * Copyright (C) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2016, 2017 Graeme Gott <graeme@gottcode.org>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  ***********************************************************************/
19 
20 #include "preferences.h"
21 
22 #include "dictionary_manager.h"
23 #include "format_manager.h"
24 #include "scene_model.h"
25 
26 #include <QApplication>
27 #include <QLocale>
28 #include <QSettings>
29 #include <QStyle>
30 
31 //-----------------------------------------------------------------------------
32 
Preferences()33 Preferences::Preferences() :
34 	m_goal_type(0, 2),
35 	m_goal_minutes(5, 1440),
36 	m_goal_words(100, 100000),
37 	m_goal_streak_minimum(1, 100),
38 	m_page_type(0, 2),
39 	m_page_characters(1, 10000),
40 	m_page_paragraphs(1, 100),
41 	m_page_words(1, 2000),
42 	m_wordcount_type(0, 2),
43 	m_save_format(FormatManager::types())
44 {
45 	forgetChanges();
46 }
47 
48 //-----------------------------------------------------------------------------
49 
~Preferences()50 Preferences::~Preferences()
51 {
52 	saveChanges();
53 }
54 
55 //-----------------------------------------------------------------------------
56 
goalType() const57 RangedInt Preferences::goalType() const
58 {
59 	return m_goal_type;
60 }
61 
62 //-----------------------------------------------------------------------------
63 
goalMinutes() const64 RangedInt Preferences::goalMinutes() const
65 {
66 	return m_goal_minutes;
67 }
68 
69 //-----------------------------------------------------------------------------
70 
goalWords() const71 RangedInt Preferences::goalWords() const
72 {
73 	return m_goal_words;
74 }
75 
76 //-----------------------------------------------------------------------------
77 
goalHistory() const78 bool Preferences::goalHistory() const
79 {
80 	return m_goal_history;
81 }
82 
83 //-----------------------------------------------------------------------------
84 
goalStreaks() const85 bool Preferences::goalStreaks() const
86 {
87 	return m_goal_streaks;
88 }
89 
90 //-----------------------------------------------------------------------------
91 
goalStreakMinimum() const92 RangedInt Preferences::goalStreakMinimum() const
93 {
94 	return m_goal_streak_minimum;
95 }
96 
97 //-----------------------------------------------------------------------------
98 
setGoalType(int goal)99 void Preferences::setGoalType(int goal)
100 {
101 	setValue(m_goal_type, goal);
102 }
103 
104 //-----------------------------------------------------------------------------
105 
setGoalMinutes(int goal)106 void Preferences::setGoalMinutes(int goal)
107 {
108 	setValue(m_goal_minutes, goal);
109 }
110 
111 //-----------------------------------------------------------------------------
112 
setGoalWords(int goal)113 void Preferences::setGoalWords(int goal)
114 {
115 	setValue(m_goal_words, goal);
116 }
117 
118 //-----------------------------------------------------------------------------
119 
setGoalHistory(bool enable)120 void Preferences::setGoalHistory(bool enable)
121 {
122 	setValue(m_goal_history, enable);
123 }
124 
125 //-----------------------------------------------------------------------------
126 
setGoalStreaks(bool enable)127 void Preferences::setGoalStreaks(bool enable)
128 {
129 	setValue(m_goal_streaks, enable);
130 }
131 
132 //-----------------------------------------------------------------------------
133 
setGoalStreakMinimum(int percent)134 void Preferences::setGoalStreakMinimum(int percent)
135 {
136 	setValue(m_goal_streak_minimum, percent);
137 }
138 
139 //-----------------------------------------------------------------------------
140 
showCharacters() const141 bool Preferences::showCharacters() const
142 {
143 	return m_show_characters;
144 }
145 
146 //-----------------------------------------------------------------------------
147 
showPages() const148 bool Preferences::showPages() const
149 {
150 	return m_show_pages;
151 }
152 
153 //-----------------------------------------------------------------------------
154 
showParagraphs() const155 bool Preferences::showParagraphs() const
156 {
157 	return m_show_paragraphs;
158 }
159 
160 //-----------------------------------------------------------------------------
161 
showWords() const162 bool Preferences::showWords() const
163 {
164 	return m_show_words;
165 }
166 
167 //-----------------------------------------------------------------------------
168 
setShowCharacters(bool show)169 void Preferences::setShowCharacters(bool show)
170 {
171 	setValue(m_show_characters, show);
172 }
173 
174 //-----------------------------------------------------------------------------
175 
setShowPages(bool show)176 void Preferences::setShowPages(bool show)
177 {
178 	setValue(m_show_pages, show);
179 }
180 
181 //-----------------------------------------------------------------------------
182 
setShowParagraphs(bool show)183 void Preferences::setShowParagraphs(bool show)
184 {
185 	setValue(m_show_paragraphs, show);
186 }
187 
188 //-----------------------------------------------------------------------------
189 
setShowWords(bool show)190 void Preferences::setShowWords(bool show)
191 {
192 	setValue(m_show_words, show);
193 }
194 
195 //-----------------------------------------------------------------------------
196 
pageType() const197 RangedInt Preferences::pageType() const
198 {
199 	return m_page_type;
200 }
201 
202 //-----------------------------------------------------------------------------
203 
pageCharacters() const204 RangedInt Preferences::pageCharacters() const
205 {
206 	return m_page_characters;
207 }
208 
209 //-----------------------------------------------------------------------------
210 
pageParagraphs() const211 RangedInt Preferences::pageParagraphs() const
212 {
213 	return m_page_paragraphs;
214 }
215 
216 //-----------------------------------------------------------------------------
217 
pageWords() const218 RangedInt Preferences::pageWords() const
219 {
220 	return m_page_words;
221 }
222 
223 //-----------------------------------------------------------------------------
224 
setPageType(int type)225 void Preferences::setPageType(int type)
226 {
227 	setValue(m_page_type, type);
228 }
229 
230 //-----------------------------------------------------------------------------
231 
setPageCharacters(int characters)232 void Preferences::setPageCharacters(int characters)
233 {
234 	setValue(m_page_characters, characters);
235 }
236 
237 //-----------------------------------------------------------------------------
238 
setPageParagraphs(int paragraphs)239 void Preferences::setPageParagraphs(int paragraphs)
240 {
241 	setValue(m_page_paragraphs, paragraphs);
242 }
243 
244 //-----------------------------------------------------------------------------
245 
setPageWords(int words)246 void Preferences::setPageWords(int words)
247 {
248 	setValue(m_page_words, words);
249 }
250 
251 //-----------------------------------------------------------------------------
252 
wordcountType() const253 RangedInt Preferences::wordcountType() const
254 {
255 	return m_wordcount_type;
256 }
257 
258 //-----------------------------------------------------------------------------
259 
setWordcountType(int type)260 void Preferences::setWordcountType(int type)
261 {
262 	setValue(m_wordcount_type, type);
263 }
264 
265 //-----------------------------------------------------------------------------
266 
alwaysCenter() const267 bool Preferences::alwaysCenter() const
268 {
269 	return m_always_center;
270 }
271 
272 //-----------------------------------------------------------------------------
273 
blockCursor() const274 bool Preferences::blockCursor() const
275 {
276 	return m_block_cursor;
277 }
278 
279 //-----------------------------------------------------------------------------
280 
smoothFonts() const281 bool Preferences::smoothFonts() const
282 {
283 	return m_smooth_fonts;
284 }
285 
286 //-----------------------------------------------------------------------------
287 
smartQuotes() const288 bool Preferences::smartQuotes() const
289 {
290 	return m_smart_quotes;
291 }
292 
293 //-----------------------------------------------------------------------------
294 
doubleQuotes() const295 int Preferences::doubleQuotes() const
296 {
297 	return m_double_quotes;
298 }
299 
300 //-----------------------------------------------------------------------------
301 
singleQuotes() const302 int Preferences::singleQuotes() const
303 {
304 	return m_single_quotes;
305 }
306 
307 //-----------------------------------------------------------------------------
308 
typewriterSounds() const309 bool Preferences::typewriterSounds() const
310 {
311 	return m_typewriter_sounds;
312 }
313 
314 //-----------------------------------------------------------------------------
315 
setAlwaysCenter(bool center)316 void Preferences::setAlwaysCenter(bool center)
317 {
318 	setValue(m_always_center, center);
319 }
320 
321 //-----------------------------------------------------------------------------
322 
setBlockCursor(bool block)323 void Preferences::setBlockCursor(bool block)
324 {
325 	setValue(m_block_cursor, block);
326 }
327 
328 //-----------------------------------------------------------------------------
329 
setSmoothFonts(bool smooth)330 void Preferences::setSmoothFonts(bool smooth)
331 {
332 	setValue(m_smooth_fonts, smooth);
333 }
334 
335 //-----------------------------------------------------------------------------
336 
setSmartQuotes(bool quotes)337 void Preferences::setSmartQuotes(bool quotes)
338 {
339 	setValue(m_smart_quotes, quotes);
340 }
341 
342 //-----------------------------------------------------------------------------
343 
setDoubleQuotes(int quotes)344 void Preferences::setDoubleQuotes(int quotes)
345 {
346 	setValue(m_double_quotes, quotes);
347 }
348 
349 //-----------------------------------------------------------------------------
350 
setSingleQuotes(int quotes)351 void Preferences::setSingleQuotes(int quotes)
352 {
353 	setValue(m_single_quotes, quotes);
354 }
355 
356 //-----------------------------------------------------------------------------
357 
setTypewriterSounds(bool sounds)358 void Preferences::setTypewriterSounds(bool sounds)
359 {
360 	setValue(m_typewriter_sounds, sounds);
361 }
362 
363 //-----------------------------------------------------------------------------
364 
sceneDivider() const365 QString Preferences::sceneDivider() const
366 {
367 	return m_scene_divider;
368 }
369 
370 //-----------------------------------------------------------------------------
371 
setSceneDivider(const QString & divider)372 void Preferences::setSceneDivider(const QString& divider)
373 {
374 	setValue(m_scene_divider, divider);
375 	SceneModel::setSceneDivider(m_scene_divider);
376 }
377 
378 //-----------------------------------------------------------------------------
379 
savePositions() const380 bool Preferences::savePositions() const
381 {
382 	return m_save_positions;
383 }
384 
385 //-----------------------------------------------------------------------------
386 
writeByteOrderMark() const387 bool Preferences::writeByteOrderMark() const
388 {
389 	return m_write_bom;
390 }
391 
392 //-----------------------------------------------------------------------------
393 
saveFormat() const394 RangedString Preferences::saveFormat() const
395 {
396 	return m_save_format;
397 }
398 
399 //-----------------------------------------------------------------------------
400 
setSavePositions(bool save)401 void Preferences::setSavePositions(bool save)
402 {
403 	setValue(m_save_positions, save);
404 }
405 
406 //-----------------------------------------------------------------------------
407 
setWriteByteOrderMark(bool write_bom)408 void Preferences::setWriteByteOrderMark(bool write_bom)
409 {
410 	setValue(m_write_bom, write_bom);
411 }
412 
413 //-----------------------------------------------------------------------------
414 
setSaveFormat(const QString & format)415 void Preferences::setSaveFormat(const QString& format)
416 {
417 	setValue(m_save_format, format);
418 }
419 
420 //-----------------------------------------------------------------------------
421 
alwaysShowScrollBar() const422 bool Preferences::alwaysShowScrollBar() const
423 {
424 	return m_always_show_scrollbar;
425 }
426 
427 //-----------------------------------------------------------------------------
428 
setAlwaysShowScrollbar(bool show_scrollbar)429 void Preferences::setAlwaysShowScrollbar(bool show_scrollbar)
430 {
431 	setValue(m_always_show_scrollbar, show_scrollbar);
432 }
433 
434 //-----------------------------------------------------------------------------
435 
alwaysShowHeader() const436 bool Preferences::alwaysShowHeader() const
437 {
438 	return m_always_show_header;
439 }
440 
441 //-----------------------------------------------------------------------------
442 
setAlwaysShowHeader(bool show_header)443 void Preferences::setAlwaysShowHeader(bool show_header)
444 {
445 	setValue(m_always_show_header, show_header);
446 }
447 
448 //-----------------------------------------------------------------------------
449 
alwaysShowFooter() const450 bool Preferences::alwaysShowFooter() const
451 {
452 	return m_always_show_footer;
453 }
454 
455 //-----------------------------------------------------------------------------
456 
setAlwaysShowFooter(bool show_footer)457 void Preferences::setAlwaysShowFooter(bool show_footer)
458 {
459 	setValue(m_always_show_footer, show_footer);
460 }
461 
462 //-----------------------------------------------------------------------------
463 
toolbarStyle() const464 int Preferences::toolbarStyle() const
465 {
466 	return m_toolbar_style;
467 }
468 
469 //-----------------------------------------------------------------------------
470 
toolbarActions() const471 QStringList Preferences::toolbarActions() const
472 {
473 	return m_toolbar_actions;
474 }
475 
476 //-----------------------------------------------------------------------------
477 
setToolbarStyle(int style)478 void Preferences::setToolbarStyle(int style)
479 {
480 	setValue(m_toolbar_style, style);
481 }
482 
483 //-----------------------------------------------------------------------------
484 
setToolbarActions(const QStringList & actions)485 void Preferences::setToolbarActions(const QStringList& actions)
486 {
487 	setValue(m_toolbar_actions, actions);
488 }
489 
490 //-----------------------------------------------------------------------------
491 
highlightMisspelled() const492 bool Preferences::highlightMisspelled() const
493 {
494 	return m_highlight_misspelled;
495 }
496 
497 //-----------------------------------------------------------------------------
498 
ignoredWordsWithNumbers() const499 bool Preferences::ignoredWordsWithNumbers() const
500 {
501 	return m_ignore_numbers;
502 }
503 
504 //-----------------------------------------------------------------------------
505 
ignoredUppercaseWords() const506 bool Preferences::ignoredUppercaseWords() const
507 {
508 	return m_ignore_uppercase;
509 }
510 
511 //-----------------------------------------------------------------------------
512 
language() const513 QString Preferences::language() const
514 {
515 	return m_language;
516 }
517 
518 //-----------------------------------------------------------------------------
519 
setHighlightMisspelled(bool highlight)520 void Preferences::setHighlightMisspelled(bool highlight)
521 {
522 	setValue(m_highlight_misspelled, highlight);
523 }
524 
525 //-----------------------------------------------------------------------------
526 
setIgnoreWordsWithNumbers(bool ignore)527 void Preferences::setIgnoreWordsWithNumbers(bool ignore)
528 {
529 	setValue(m_ignore_numbers, ignore);
530 }
531 
532 //-----------------------------------------------------------------------------
533 
setIgnoreUppercaseWords(bool ignore)534 void Preferences::setIgnoreUppercaseWords(bool ignore)
535 {
536 	setValue(m_ignore_uppercase, ignore);
537 }
538 
539 //-----------------------------------------------------------------------------
540 
setLanguage(const QString & language)541 void Preferences::setLanguage(const QString& language)
542 {
543 	setValue(m_language, DictionaryManager::instance().availableDictionary(language));
544 }
545 
546 //-----------------------------------------------------------------------------
547 
reload()548 void Preferences::reload()
549 {
550 	QSettings settings;
551 
552 	m_goal_type = settings.value("Goal/Type", 1).toInt();
553 	m_goal_minutes = settings.value("Goal/Minutes", 30).toInt();
554 	m_goal_words = settings.value("Goal/Words", 1000).toInt();
555 	m_goal_history = settings.value("Goal/History", true).toBool();
556 	m_goal_streaks = settings.value("Goal/Streaks", true).toBool();
557 	m_goal_streak_minimum = settings.value("Goal/StreakMinimum", 100).toInt();
558 
559 	m_show_characters = settings.value("Stats/ShowCharacters", false).toBool();
560 	m_show_pages = settings.value("Stats/ShowPages", false).toBool();
561 	m_show_paragraphs = settings.value("Stats/ShowParagraphs", false).toBool();
562 	m_show_words = settings.value("Stats/ShowWords", true).toBool();
563 
564 	m_page_type = settings.value("Stats/PageSizeType", 2).toInt();
565 	m_page_characters = settings.value("Stats/CharactersPerPage", 1500).toInt();
566 	m_page_paragraphs = settings.value("Stats/ParagraphsPerPage", 5).toInt();
567 	m_page_words = settings.value("Stats/WordsPerPage", 250).toInt();
568 
569 	int old_wordcount_type = !settings.value("Stats/AccurateWordcount", true).toBool();
570 	QLocale::Language language = QLocale().language();
571 	if (language == QLocale::Chinese ||
572 			language == QLocale::Japanese ||
573 			language == QLocale::Khmer ||
574 			language == QLocale::Lao ||
575 			language == QLocale::Thai) {
576 		old_wordcount_type = 2;
577 	}
578 	m_wordcount_type = settings.value("Stats/WordcountType", old_wordcount_type).toInt();
579 
580 	m_always_center = settings.value("Edit/AlwaysCenter", false).toBool();
581 	m_block_cursor = settings.value("Edit/BlockCursor", false).toBool();
582 	m_smooth_fonts = settings.value("Edit/SmoothFonts", true).toBool();
583 	m_smart_quotes = settings.value("Edit/SmartQuotes", true).toBool();
584 	m_double_quotes = settings.value("Edit/SmartDoubleQuotes", -1).toInt();
585 	m_single_quotes = settings.value("Edit/SmartSingleQuotes", -1).toInt();
586 	m_typewriter_sounds = settings.value("Edit/TypewriterSounds", false).toBool();
587 
588 	m_scene_divider = settings.value("SceneList/Divider", QLatin1String("##")).toString();
589 	SceneModel::setSceneDivider(m_scene_divider);
590 
591 	m_save_positions = settings.value("Save/RememberPositions", true).toBool();
592 	m_write_bom = settings.value("Save/WriteBOM", true).toBool();
593 	m_save_format = settings.value("Save/DefaultFormat", "odt").toString();
594 
595 	m_toolbar_style = settings.value("Toolbar/Style", QApplication::style()->styleHint(QStyle::SH_ToolButtonStyle)).toInt();
596 	m_toolbar_actions = QStringList() << "New" << "Open" << "Save" << "|" << "Undo" << "Redo" << "|" << "Cut" << "Copy" << "Paste" << "|" << "Find" << "Replace" << "|" << "Themes";
597 	m_toolbar_actions = settings.value("Toolbar/Actions", m_toolbar_actions).toStringList();
598 
599 	m_highlight_misspelled = settings.value("Spelling/HighlightMisspelled", true).toBool();
600 	m_ignore_numbers = settings.value("Spelling/IgnoreNumbers", true).toBool();
601 	m_ignore_uppercase = settings.value("Spelling/IgnoreUppercase", true).toBool();
602 	m_language = DictionaryManager::instance().availableDictionary(settings.value("Spelling/Language", QLocale().name()).toString());
603 
604 	m_always_show_scrollbar = settings.value("View/AlwaysShowScrollbar",false).toBool();
605 	m_always_show_header = settings.value("View/AlwaysShowHeader",false).toBool();
606 	m_always_show_footer = settings.value("View/AlwaysShowFooter",false).toBool();
607 
608 	DictionaryManager::instance().setDefaultLanguage(m_language);
609 	DictionaryManager::instance().setIgnoreNumbers(m_ignore_numbers);
610 	DictionaryManager::instance().setIgnoreUppercase(m_ignore_uppercase);
611 }
612 
613 //-----------------------------------------------------------------------------
614 
write()615 void Preferences::write()
616 {
617 	QSettings settings;
618 
619 	settings.setValue("Goal/Type", m_goal_type.value());
620 	settings.setValue("Goal/Minutes", m_goal_minutes.value());
621 	settings.setValue("Goal/Words", m_goal_words.value());
622 	settings.setValue("Goal/History", m_goal_history);
623 	settings.setValue("Goal/Streaks", m_goal_streaks);
624 	settings.setValue("Goal/StreakMinimum", m_goal_streak_minimum.value());
625 
626 	settings.setValue("Stats/ShowCharacters", m_show_characters);
627 	settings.setValue("Stats/ShowPages", m_show_pages);
628 	settings.setValue("Stats/ShowParagraphs", m_show_paragraphs);
629 	settings.setValue("Stats/ShowWords", m_show_words);
630 
631 	settings.setValue("Stats/PageSizeType", m_page_type.value());
632 	settings.setValue("Stats/CharactersPerPage", m_page_characters.value());
633 	settings.setValue("Stats/ParagraphsPerPage", m_page_paragraphs.value());
634 	settings.setValue("Stats/WordsPerPage", m_page_words.value());
635 
636 	settings.setValue("Stats/WordcountType", m_wordcount_type.value());
637 
638 	settings.setValue("Edit/AlwaysCenter", m_always_center);
639 	settings.setValue("Edit/BlockCursor", m_block_cursor);
640 	settings.setValue("Edit/SmoothFonts", m_smooth_fonts);
641 	settings.setValue("Edit/SmartQuotes", m_smart_quotes);
642 	settings.setValue("Edit/SmartDoubleQuotes", m_double_quotes);
643 	settings.setValue("Edit/SmartSingleQuotes", m_single_quotes);
644 	settings.setValue("Edit/TypewriterSounds", m_typewriter_sounds);
645 
646 	settings.setValue("SceneList/Divider", m_scene_divider);
647 
648 	settings.setValue("Save/RememberPositions", m_save_positions);
649 	settings.setValue("Save/WriteBOM", m_write_bom);
650 	settings.setValue("Save/DefaultFormat", m_save_format.value());
651 
652 	settings.setValue("Toolbar/Style", m_toolbar_style);
653 	settings.setValue("Toolbar/Actions", m_toolbar_actions);
654 
655 	settings.setValue("Spelling/HighlightMisspelled", m_highlight_misspelled);
656 	settings.setValue("Spelling/IgnoreNumbers", m_ignore_numbers);
657 	settings.setValue("Spelling/IgnoreUppercase", m_ignore_uppercase);
658 	settings.setValue("Spelling/Language", m_language);
659 
660 	settings.setValue("View/AlwaysShowScrollbar",m_always_show_scrollbar);
661 	settings.setValue("View/AlwaysShowHeader",m_always_show_header);
662 	settings.setValue("View/AlwaysShowFooter",m_always_show_footer);
663 
664 	DictionaryManager::instance().addProviders();
665 	DictionaryManager::instance().setDefaultLanguage(m_language);
666 	DictionaryManager::instance().setIgnoreNumbers(m_ignore_numbers);
667 	DictionaryManager::instance().setIgnoreUppercase(m_ignore_uppercase);
668 }
669 
670 //-----------------------------------------------------------------------------
671