1 /*
2  * This file is a part of QComicBook.
3  *
4  * Copyright (C) 2005-2010 Pawel Stolowski <stolowski@gmail.com>
5  *
6  * QComicBook is free software; you can redestribute it and/or modify it
7  * under terms of GNU General Public License by Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY. See GPL for more details.
11  */
12 
13 #include "ComicBookSettings.h"
14 #include "History.h"
15 #include "Utility.h"
16 #include "EnumMap.h"
17 #include "ComicMainWindow.h"
18 #include <QSettings>
19 #include <QColor>
20 #include <QDir>
21 #include <QTextStream>
22 #include <QStandardPaths>
23 #include <iostream>
24 
25 #define GRP_VIEW                     "/View"
26 #define OPT_TWOPAGES                 "/TwoPages"
27 #define OPT_JAPANESEMODE             "/JapaneseMode"
28 #define OPT_SCROLLBARS               "/Scrollbars"
29 #define OPT_SMOOTHSCALING            "/SmoothScaling"
30 #define OPT_PAGESIZE                 "/PageSize"
31 #define OPT_BACKGROUND               "/Background"
32 #define OPT_FULLSCREENHIDEMENU       "/FullScreenHideMenu"
33 #define OPT_FULLSCREENHIDESTATUS     "/FullScreenHideStatusbar"
34 #define OPT_FULLSCREENHIDETOOLBAR    "/FullScreenHideToolbar"
35 #define OPT_STATUSBAR                "/Statusbar"
36 #define OPT_FONT                     "/InfoFont"
37 #define OPT_SMALLCURSOR              "/SmallCursor"
38 #define OPT_EMBEDPAGENUMBERS         "/EmbedPageNumbers"
39 #define OPT_CONTSCROLL               "/ContinuousScroll"
40 #define OPT_VIEWTYPE                 "/ViewType"
41 
42 #define GRP_NAVI                     "/Navigation"
43 
44 #define GRP_WINDOW      "/Window"
45 #define OPT_GEOMETRY    "/Geometry"
46 #define OPT_DOCKLAYOUT  "/DockLayout"
47 
48 #define GRP_RUNTIME     "/Runtime"
49 #define OPT_LASTDIR     "/LastDir"
50 #define OPT_RECENT      "/RecentlyOpened"
51 
52 #define GRP_MISC        "/Misc"
53 #define OPT_AUTOINFO    "/InfoDialog"
54 #define OPT_CACHESIZE   "/CacheSize"
55 #define OPT_CACHEADJUST "/CacheAutoAdjust"
56 #define OPT_THUMBSAGE   "/ThumbnailsAge"
57 #define OPT_CACHETHUMBS "/CacheThumbnails"
58 #define OPT_PRELOAD     "/Preload"
59 #define OPT_CONFIRMEXIT "/ConfirmExit"
60 #define OPT_SHOWSPLASH  "/ShowSplashscreen"
61 #define OPT_TMPDIR      "/TmpDir"
62 #define OPT_DONATION    "/DonationDialog"
63 
64 using namespace QComicBook;
65 
66 const EnumMap<Size> ComicBookSettings::size2string[] = {
67 	{"original",  Original},
68 	{"fitwidth",  FitWidth},
69 	{"fitheight", FitHeight},
70 	{"wholepage", WholePage},
71 	{"bestfit",   BestFit},
72 	{QString::null}
73 };
74 
75 const EnumMap<ViewType> ComicBookSettings::viewtype2string[] = {
76 	{"simple",  Simple},
77 	{"continuous", Continuous},
78 	{"frame", Frame},
79 	{QString::null}
80 };
81 
instance()82 ComicBookSettings& ComicBookSettings::instance()
83 {
84     static ComicBookSettings cfg;
85     return cfg;
86 }
87 
ComicBookSettings()88 ComicBookSettings::ComicBookSettings(): QObject()
89                                       , m_bkpath(QString::null)
90                                       , m_thpath(QString::null)
91                                       , m_dirsok(false)
92 {
93 	m_cfg = new QSettings();
94 //	cfg->insertSearchPath(QSettings::Unix, QDir::homeDirPath() + "/.qcomicbook");
95 	m_cfg->beginGroup("/QComicBook");
96 }
97 
~ComicBookSettings()98 ComicBookSettings::~ComicBookSettings()
99 {
100 	m_cfg->endGroup();
101 	delete m_cfg;
102 }
103 
checkDirs()104 bool ComicBookSettings::checkDirs()
105 {
106 	m_dirsok = false;
107 	m_bkpath = QStandardPaths::writableLocation(QStandardPaths::DataLocation);
108 
109 	QDir dir(m_bkpath);
110 	if (!dir.exists())
111         {
112             if (!dir.mkpath(m_bkpath))
113             {
114                 return false;
115             }
116         }
117 
118         m_thpath = QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + QDir::separator() + "thumbs";
119 
120         dir.setPath(m_thpath);
121 	if (!dir.exists())
122         {
123             if (!dir.mkpath(m_thpath))
124             {
125                 return false;
126             }
127         }
128 	return m_dirsok = true;
129 }
130 
bookmarksDir()131 const QString& ComicBookSettings::bookmarksDir()
132 {
133 	return m_bkpath;
134 }
135 
thumbnailsDir()136 const QString& ComicBookSettings::thumbnailsDir()
137 {
138 	return m_thpath;
139 }
140 
load()141 void ComicBookSettings::load()
142 {
143 	QString fontdesc;
144 	m_cfg->beginGroup(GRP_VIEW);
145                 m_embedpagenumbers = m_cfg->value(OPT_EMBEDPAGENUMBERS, false).toBool();
146 		m_smallcursor = m_cfg->value(OPT_SMALLCURSOR, false).toBool();
147 		m_twopages = m_cfg->value(OPT_TWOPAGES, false).toBool();
148 		m_japanese = m_cfg->value(OPT_JAPANESEMODE, false).toBool();
149 		m_scrollbars = m_cfg->value(OPT_SCROLLBARS, false).toBool();
150 		m_smoothscaling = m_cfg->value(OPT_SMOOTHSCALING, true).toBool();
151 		m_pagesize = convert(size2string, m_cfg->value(OPT_PAGESIZE, size2string[0].str).toString());
152 		m_bgcolor = m_cfg->value(OPT_BACKGROUND).value<QColor>();
153 		m_fscrhidemenu = m_cfg->value(OPT_FULLSCREENHIDEMENU, true).toBool();
154 		m_fscrhidestatus = m_cfg->value(OPT_FULLSCREENHIDESTATUS, true).toBool();
155 		m_fscrhidetoolbar = m_cfg->value(OPT_FULLSCREENHIDETOOLBAR, false).toBool();
156 		m_statusbar = m_cfg->value(OPT_STATUSBAR, true).toBool();
157 		fontdesc = m_cfg->value(OPT_FONT, QString()).toString();
158 		if (fontdesc.isNull() || !m_font.fromString(fontdesc))
159 		{
160 			m_font.setFamily("Courier");
161 			m_font.setPointSize(10);
162 		}
163 		m_contscroll = m_cfg->value(OPT_CONTSCROLL, true).toBool();
164 		m_viewtype = convert(viewtype2string, m_cfg->value(OPT_VIEWTYPE, viewtype2string[0].str).toString());
165 	m_cfg->endGroup();
166 	m_cfg->beginGroup(GRP_RUNTIME);
167 		m_lastdir = m_cfg->value(OPT_LASTDIR, QString()).toString();
168 		m_recent = m_cfg->value(OPT_RECENT).toStringList();
169         m_cfg->endGroup();
170 	m_cfg->beginGroup(GRP_MISC);
171 		m_donationdlg = m_cfg->value(OPT_DONATION, true).toBool();
172 		m_cachesize = m_cfg->value(OPT_CACHESIZE, 3).toInt();
173 		if (m_cachesize < 1)
174                 {
175                     m_cachesize = 1;
176                 }
177 		m_cacheadjust = m_cfg->value(OPT_CACHEADJUST, true).toBool();
178 		m_preload = m_cfg->value(OPT_PRELOAD, true).toBool();
179 		m_confirmexit = m_cfg->value(OPT_CONFIRMEXIT, true).toBool();
180 		m_autoinfo = m_cfg->value(OPT_AUTOINFO, false).toBool();
181 		m_showsplash = m_cfg->value(OPT_SHOWSPLASH, true).toBool();
182 		m_thumbsage = m_cfg->value(OPT_THUMBSAGE, 7).toInt();
183 		m_cachethumbs = m_cfg->value(OPT_CACHETHUMBS, true).toBool();
184 		m_tmpdir = m_cfg->value(OPT_TMPDIR, QString()).toString();
185 		QDir dir(m_tmpdir);
186 		if (m_tmpdir.isNull() || !dir.exists())
187                 {
188                     m_tmpdir = QDir::tempPath();
189                 }
190 	m_cfg->endGroup();
191 }
192 
embedPageNumbers() const193 bool ComicBookSettings::embedPageNumbers() const
194 {
195     return m_embedpagenumbers;
196 }
197 
smallCursor() const198 bool ComicBookSettings::smallCursor() const
199 {
200     return m_smallcursor;
201 }
202 
twoPagesMode() const203 bool ComicBookSettings::twoPagesMode() const
204 {
205     return m_twopages;
206 }
207 
japaneseMode() const208 bool ComicBookSettings::japaneseMode() const
209 {
210     return m_japanese;
211 }
212 
continuousScrolling() const213 bool ComicBookSettings::continuousScrolling() const
214 {
215     return m_contscroll;
216 }
217 
viewType() const218 ViewType ComicBookSettings::viewType() const
219 {
220 	return m_viewtype;
221 }
222 
scrollbarsVisible() const223 bool ComicBookSettings::scrollbarsVisible() const
224 {
225     return m_scrollbars;
226 }
227 
restoreGeometry(ComicMainWindow * w) const228 void ComicBookSettings::restoreGeometry(ComicMainWindow *w) const
229 {
230     w->restoreGeometry(m_cfg->value(GRP_WINDOW OPT_GEOMETRY).toByteArray());
231 }
232 
pageSize() const233 Size ComicBookSettings::pageSize() const
234 {
235     return m_pagesize;
236 }
237 
smoothScaling() const238 bool ComicBookSettings::smoothScaling() const
239 {
240     return m_smoothscaling;
241 }
242 
lastDir() const243 QString ComicBookSettings::lastDir() const
244 {
245     return m_lastdir;
246 }
247 
recentlyOpened() const248 const QStringList& ComicBookSettings::recentlyOpened() const
249 {
250     return m_recent;
251 }
252 
background() const253 QColor ComicBookSettings::background() const
254 {
255 	return m_bgcolor;
256 }
257 
cacheSize() const258 int ComicBookSettings::cacheSize() const
259 {
260     return m_cachesize;
261 }
262 
cacheAutoAdjust() const263 bool ComicBookSettings::cacheAutoAdjust() const
264 {
265     return m_cacheadjust;
266 }
267 
cacheThumbnails() const268 bool ComicBookSettings::cacheThumbnails() const
269 {
270     return m_cachethumbs;
271 }
272 
thumbnailsAge() const273 int ComicBookSettings::thumbnailsAge() const
274 {
275     return m_thumbsage;
276 }
277 
preloadPages() const278 bool ComicBookSettings::preloadPages() const
279 {
280     return m_preload;
281 }
282 
confirmExit() const283 bool ComicBookSettings::confirmExit() const
284 {
285 	return m_confirmexit;
286 }
287 
autoInfo() const288 bool ComicBookSettings::autoInfo() const
289 {
290     return m_autoinfo;
291 }
292 
fullScreenHideMenu() const293 bool ComicBookSettings::fullScreenHideMenu() const
294 {
295     return m_fscrhidemenu;
296 }
297 
fullScreenHideStatusbar() const298 bool ComicBookSettings::fullScreenHideStatusbar() const
299 {
300     return m_fscrhidestatus;
301 }
302 
fullScreenHideToolbar() const303 bool ComicBookSettings::fullScreenHideToolbar() const
304 {
305     return m_fscrhidetoolbar;
306 }
307 
showStatusbar() const308 bool ComicBookSettings::showStatusbar() const
309 {
310     return m_statusbar;
311 }
312 
infoFont() const313 const QFont& ComicBookSettings::infoFont() const
314 {
315     return m_font;
316 }
317 
showSplash() const318 bool ComicBookSettings::showSplash() const
319 {
320     return m_showsplash;
321 }
322 
restoreDockLayout(ComicMainWindow * w) const323 void ComicBookSettings::restoreDockLayout(ComicMainWindow *w) const
324 {
325     w->restoreState(m_cfg->value(GRP_WINDOW OPT_DOCKLAYOUT).toByteArray());
326 }
327 
tmpDir() const328 QString ComicBookSettings::tmpDir() const
329 {
330     return m_tmpdir;
331 }
332 
showDonationDialog() const333 bool ComicBookSettings::showDonationDialog() const
334 {
335 	return m_donationdlg;
336 }
337 
embedPageNumbers(bool f)338 void ComicBookSettings::embedPageNumbers(bool f)
339 {
340     if (f != m_embedpagenumbers)
341     {
342         m_cfg->setValue(GRP_VIEW OPT_EMBEDPAGENUMBERS, m_embedpagenumbers = f);
343         emit displaySettingsChanged(OPT_EMBEDPAGENUMBERS);
344     }
345 }
346 
smallCursor(bool f)347 void ComicBookSettings::smallCursor(bool f)
348 {
349     if (f != m_smallcursor)
350     {
351         m_cfg->setValue(GRP_VIEW OPT_SMALLCURSOR, m_smallcursor = f);
352         emit displaySettingsChanged(OPT_SMALLCURSOR);
353     }
354 }
355 
twoPagesMode(bool f)356 void ComicBookSettings::twoPagesMode(bool f)
357 {
358     if (f != m_twopages)
359     {
360         m_cfg->setValue(GRP_VIEW OPT_TWOPAGES, m_twopages = f);
361         emit displaySettingsChanged(OPT_TWOPAGES);
362     }
363 }
364 
japaneseMode(bool f)365 void ComicBookSettings::japaneseMode(bool f)
366 {
367     if (f != m_japanese)
368     {
369         m_cfg->setValue(GRP_VIEW OPT_JAPANESEMODE, m_japanese = f);
370         emit displaySettingsChanged(OPT_JAPANESEMODE);
371     }
372 }
373 
continuousScrolling(bool f)374 void ComicBookSettings::continuousScrolling(bool f)
375 {
376     if (f != m_contscroll)
377     {
378         m_cfg->setValue(GRP_VIEW OPT_CONTSCROLL, m_contscroll = f);
379     }
380 }
381 
viewType(ViewType t)382 void ComicBookSettings::viewType(ViewType t)
383 {
384 	if (t != m_viewtype)
385 	{
386 		m_cfg->setValue(GRP_VIEW OPT_VIEWTYPE, convert(viewtype2string, m_viewtype = t));
387 	}
388 }
389 
scrollbarsVisible(bool f)390 void ComicBookSettings::scrollbarsVisible(bool f)
391 {
392     if (f != m_scrollbars)
393     {
394         m_cfg->setValue(GRP_VIEW OPT_SCROLLBARS, m_scrollbars = f);
395     }
396 }
397 
saveGeometry(ComicMainWindow * w)398 void ComicBookSettings::saveGeometry(ComicMainWindow *w)
399 {
400     m_cfg->setValue(GRP_WINDOW OPT_GEOMETRY, w->saveGeometry());
401 }
402 
pageSize(Size s)403 void ComicBookSettings::pageSize(Size s)
404 {
405     if (s != m_pagesize)
406     {
407         m_cfg->setValue(GRP_VIEW OPT_PAGESIZE, convert(size2string, m_pagesize = s));
408     }
409 }
410 
smoothScaling(bool s)411 void ComicBookSettings::smoothScaling(bool s)
412 {
413     if (s != m_smoothscaling)
414     {
415         m_cfg->setValue(GRP_VIEW OPT_SMOOTHSCALING, m_smoothscaling = s);
416         emit displaySettingsChanged(OPT_SMOOTHSCALING);
417     }
418 }
419 
lastDir(const QString & d)420 void ComicBookSettings::lastDir(const QString &d)
421 {
422     if (m_lastdir != d)
423     {
424         m_cfg->setValue(GRP_RUNTIME OPT_LASTDIR, m_lastdir = d);
425     }
426 }
427 
recentlyOpened(const QStringList & hist)428 void ComicBookSettings::recentlyOpened(const QStringList &hist)
429 {
430     m_recent = hist;
431     m_cfg->setValue(GRP_RUNTIME OPT_RECENT, m_recent);
432 }
433 
background(const QColor & color)434 void ComicBookSettings::background(const QColor &color)
435 {
436     if (color != m_bgcolor)
437     {
438         m_bgcolor = color;
439         m_cfg->setValue(GRP_VIEW OPT_BACKGROUND, m_bgcolor.name());
440         emit displaySettingsChanged(OPT_BACKGROUND);
441     }
442 }
443 
cacheSize(int s)444 void ComicBookSettings::cacheSize(int s)
445 {
446     if (s != m_cachesize)
447     {
448         if (s < 1)
449         {
450             s = 1;
451         }
452         m_cfg->setValue(GRP_MISC OPT_CACHESIZE, m_cachesize = s);
453     }
454 }
455 
cacheAutoAdjust(bool f)456 void ComicBookSettings::cacheAutoAdjust(bool f)
457 {
458     if (f != m_cacheadjust)
459     {
460         m_cacheadjust = f;
461         m_cfg->setValue(GRP_MISC OPT_CACHEADJUST, m_cacheadjust = f);
462     }
463 }
464 
cacheThumbnails(bool f)465 void ComicBookSettings::cacheThumbnails(bool f)
466 {
467     if (f != m_cachethumbs)
468     {
469         m_cfg->setValue(GRP_MISC OPT_CACHETHUMBS, m_cachethumbs = f);
470     }
471 }
472 
thumbnailsAge(int n)473 void ComicBookSettings::thumbnailsAge(int n)
474 {
475     if (n != m_thumbsage)
476     {
477         m_cfg->setValue(GRP_MISC OPT_THUMBSAGE, m_thumbsage = n);
478     }
479 }
480 
preloadPages(bool f)481 void ComicBookSettings::preloadPages(bool f)
482 {
483     if (f != m_preload)
484     {
485         m_cfg->setValue(GRP_MISC OPT_PRELOAD, m_preload = f);
486     }
487 }
488 
confirmExit(bool f)489 void ComicBookSettings::confirmExit(bool f)
490 {
491     if (f != m_confirmexit)
492     {
493         m_cfg->setValue(GRP_MISC OPT_CONFIRMEXIT, m_confirmexit = f);
494     }
495 }
496 
autoInfo(bool f)497 void ComicBookSettings::autoInfo(bool f)
498 {
499     if (f != m_autoinfo)
500     {
501         m_cfg->setValue(GRP_MISC OPT_AUTOINFO, m_autoinfo = f);
502     }
503 }
504 
fullScreenHideMenu(bool f)505 void ComicBookSettings::fullScreenHideMenu(bool f)
506 {
507     if (f != m_fscrhidemenu)
508     {
509         m_cfg->setValue(GRP_VIEW OPT_FULLSCREENHIDEMENU, m_fscrhidemenu = f);
510     }
511 }
512 
fullScreenHideStatusbar(bool f)513 void ComicBookSettings::fullScreenHideStatusbar(bool f)
514 {
515     if (f != m_fscrhidestatus)
516     {
517         m_cfg->setValue(GRP_VIEW OPT_FULLSCREENHIDESTATUS, m_fscrhidestatus = f);
518     }
519 }
520 
fullScreenHideToolbar(bool f)521 void ComicBookSettings::fullScreenHideToolbar(bool f)
522 {
523     if (f != m_fscrhidetoolbar)
524     {
525         m_cfg->setValue(GRP_VIEW OPT_FULLSCREENHIDETOOLBAR, m_fscrhidetoolbar = f);
526     }
527 }
528 
showStatusbar(bool f)529 void ComicBookSettings::showStatusbar(bool f)
530 {
531     if (f != m_statusbar)
532     {
533         m_cfg->setValue(GRP_VIEW OPT_STATUSBAR, m_statusbar = f);
534     }
535 }
536 
infoFont(const QFont & s)537 void ComicBookSettings::infoFont(const QFont &s)
538 {
539     if (s != m_font)
540     {
541         m_font = s;
542         m_cfg->setValue(GRP_VIEW OPT_FONT, m_font.toString());
543     }
544 }
545 
saveDockLayout(ComicMainWindow * w)546 void ComicBookSettings::saveDockLayout(ComicMainWindow *w)
547 {
548     m_cfg->setValue(GRP_WINDOW OPT_DOCKLAYOUT, w->saveState());
549 }
550 
showSplash(bool f)551 void ComicBookSettings::showSplash(bool f)
552 {
553     if (f != m_showsplash)
554     {
555         m_cfg->setValue(GRP_MISC OPT_SHOWSPLASH, m_showsplash = f);
556     }
557 }
558 
tmpDir(const QString & dir)559 void ComicBookSettings::tmpDir(const QString &dir)
560 {
561     if (dir != m_tmpdir)
562     {
563         m_cfg->setValue(GRP_MISC OPT_TMPDIR, m_tmpdir = dir);
564     }
565 }
566 
showDonationDialog(bool f)567 void ComicBookSettings::showDonationDialog(bool f)
568 {
569 	if (f != m_donationdlg)
570 	{
571 		m_cfg->setValue(GRP_MISC OPT_DONATION, m_donationdlg = f);
572 	}
573 }
574 
575