Add options to control the behaviour of spreads in two-pages mode
[qcomicbook-oblomov] / src / cbsettings.cpp
1 /*
2  * This file is a part of QComicBook.
3  *
4  * Copyright (C) 2005-2006 Pawel Stolowski <yogin@linux.bydg.org>
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 "cbsettings.h"
14 #include "history.h"
15 #include "miscutil.h"
16 #include "enummap.h"
17 #include <qsettings.h>
18 #include <qrect.h>
19 #include <qcolor.h>
20 #include <qdir.h>
21 #include <qtextstream.h>
22 #include <qmainwindow.h>
23
24 #define GRP_VIEW                     "/View"
25 #define OPT_TWOPAGES                 "/TwoPages"
26 #define OPT_JAPANESEMODE             "/JapaneseMode"
27 #define OPT_SCROLLBARS               "/Scrollbars"
28 //#define OPT_SCALING                  "/Scaling"
29 #define OPT_PAGESIZE                 "/PageSize"
30 #define OPT_BACKGROUND               "/Background"
31 #define OPT_FULLSCREENHIDEMENU       "/FullScreenHideMenu"
32 #define OPT_FULLSCREENHIDESTATUS     "/FullScreenHideStatusbar"
33 #define OPT_FULLSCREENHIDETOOLBAR    "/FullScreenHideToolbar"
34 #define OPT_STATUSBAR                "/Statusbar"
35 #define OPT_FONT                     "/InfoFont"
36 #define OPT_SMALLCURSOR              "/SmallCursor"
37
38 #define GRP_NAVI                     "/Navigation"
39 #define OPT_CONTSCROLL               "/ContinuousScroll"
40 #define OPT_TWOPAGESSTEP             "/TwoPagesStep"
41 #define OPT_TWOPAGESSPREAD           "/TwoPagesSpread"
42 #define OPT_TWOPAGESSKIP             "/TwoPagesSkip"
43
44 #define OPT_STRIPMODE                "/StripMode"
45 #define OPT_STRIPMAX                 "/StripMax"
46
47 #define GRP_WINDOW      "/Window"
48 #define OPT_X           "/X"
49 #define OPT_Y           "/Y"
50 #define OPT_WIDTH       "/Width"
51 #define OPT_HEIGHT      "/Height"
52 #define OPT_DOCKLAYOUT  "/DockLayout"
53
54 #define GRP_MISC        "/Misc"
55 #define OPT_AUTOINFO    "/InfoDialog"
56 #define OPT_LASTDIR     "/LastDir"
57 #define OPT_RECENT      "/RecentlyOpened"
58 #define OPT_CACHESIZE   "/CacheSize"
59 #define OPT_THUMBSAGE   "/ThumbnailsAge"
60 #define OPT_CACHETHUMBS "/CacheThumbnails"
61 #define OPT_PRELOAD     "/Preload"
62 #define OPT_CONFIRMEXIT "/ConfirmExit"
63 #define OPT_SHOWSPLASH  "/ShowSplashscreen"
64 #define OPT_INTBROWSER  "/UseIntBrowser"
65 #define OPT_EXTBROWSER  "/ExtBrowserCmd"
66 #define OPT_EDITING     "/Editing"
67 #define OPT_TMPDIR      "/TmpDir"
68
69
70 using namespace QComicBook;
71
72 QString ComicBookSettings::bkpath = QString::null;
73 QString ComicBookSettings::thpath = QString::null;
74 bool ComicBookSettings::dirsok = false;
75
76 const EnumMap<Size> ComicBookSettings::size2string[] = {
77         {"original",  Original},
78         {"fitwidth",  FitWidth},
79         {"fitheight", FitHeight},
80         {"wholepage", WholePage},
81         {"bestfit",   BestFit},
82         {QString::null}
83 };
84
85 /*const EnumMap<Scaling> ComicBookSettings::scaling2string[] = {
86         {"smooth", Smooth},
87         {"fast",   Fast},
88         {QString::null}
89 };*/
90
91 ComicBookSettings& ComicBookSettings::instance()
92 {
93         static ComicBookSettings cfg;
94         return cfg;
95 }
96
97 ComicBookSettings::ComicBookSettings(): QObject()
98 {
99         cfg = new QSettings();
100         cfg->insertSearchPath(QSettings::Unix, QDir::homeDirPath() + "/.qcomicbook");
101         cfg->beginGroup("/QComicBook");
102 }
103
104 ComicBookSettings::~ComicBookSettings()
105 {
106         cfg->endGroup();
107         delete cfg;
108 }
109
110 bool ComicBookSettings::checkDirs()
111 {
112         dirsok = false;
113         bkpath = QDir::homeDirPath() + "/.qcomicbook";
114         QDir dir(bkpath);
115         if (!dir.exists())
116                 if (!dir.mkdir(bkpath))
117                         return false;
118         
119         dir.setPath(thpath = bkpath + "/cache");
120         if (!dir.exists())
121                 if (!dir.mkdir(thpath))
122                         return false;
123         return dirsok = true;
124 }
125
126 const QString& ComicBookSettings::bookmarksDir()
127 {
128         return bkpath;
129 }
130
131 const QString& ComicBookSettings::thumbnailsDir()
132 {
133         return thpath;
134 }
135
136 void ComicBookSettings::load()
137 {
138         QString defbrowser(QString::null);
139         QString fontdesc;
140
141         const char *browsers[] = {"firefox", "mozilla", "konqueror", "opera", NULL};
142         for (int i=0; browsers[i]; i++)
143                 if (QString path = QComicBook::which(browsers[i]))
144                 {
145                         defbrowser = path;
146                         break;
147                 }
148         
149         cfg->beginGroup(GRP_WINDOW);
150                 x = cfg->readNumEntry(OPT_X, 0);
151                 y = cfg->readNumEntry(OPT_Y, 0);
152                 w = cfg->readNumEntry(OPT_WIDTH, 640);
153                 h = cfg->readNumEntry(OPT_HEIGHT, 400);
154                 docklayout = cfg->readEntry(OPT_DOCKLAYOUT, QString::null);
155         cfg->endGroup();
156         cfg->beginGroup(GRP_VIEW);
157                 smallcursor = cfg->readBoolEntry(OPT_SMALLCURSOR, false);
158                 twopages = cfg->readBoolEntry(OPT_TWOPAGES, false);
159                 stripmode = cfg->readBoolEntry(OPT_STRIPMODE, true);
160                 stripmax = cfg->readNumEntry(OPT_STRIPMAX, 7);
161                 japanese = cfg->readBoolEntry(OPT_JAPANESEMODE, false);
162                 scrollbars = cfg->readBoolEntry(OPT_SCROLLBARS, false);
163                 //scaling = convert(scaling2string, cfg->readEntry(OPT_SCALING, size2string[0].str));
164                 pagesize = convert(size2string, cfg->readEntry(OPT_PAGESIZE, size2string[0].str));
165                 bgcolor.setNamedColor(cfg->readEntry(OPT_BACKGROUND, "#000000"));
166                 fscrhidemenu = cfg->readBoolEntry(OPT_FULLSCREENHIDEMENU, true);
167                 fscrhidestatus = cfg->readBoolEntry(OPT_FULLSCREENHIDESTATUS, true);
168                 fscrhidetoolbar = cfg->readBoolEntry(OPT_FULLSCREENHIDETOOLBAR, false);
169                 statusbar = cfg->readBoolEntry(OPT_STATUSBAR, true);
170                 fontdesc = cfg->readEntry(OPT_FONT, QString::null);
171                 if (fontdesc.isNull() || !font.fromString(fontdesc))
172                 {
173                         font.setFamily("Courier");
174                         font.setPointSize(10);
175                 }
176         cfg->endGroup();
177         cfg->beginGroup(GRP_NAVI);
178                 contscroll = cfg->readBoolEntry(OPT_CONTSCROLL, true);
179                 twopagesstep = cfg->readBoolEntry(OPT_TWOPAGESSTEP, true);
180                 twopagesspread = cfg->readBoolEntry(OPT_TWOPAGESSPREAD, true);
181                 twopagesskip = cfg->readBoolEntry(OPT_TWOPAGESSKIP, false);
182         cfg->endGroup();
183         cfg->beginGroup(GRP_MISC);
184                 lastdir = cfg->readEntry(OPT_LASTDIR, QString::null);
185                 recent = cfg->readListEntry(OPT_RECENT);
186                 cachesize = cfg->readNumEntry(OPT_CACHESIZE, 3);
187                 if (cachesize < 1)
188                         cachesize = 1;
189                 preload = cfg->readBoolEntry(OPT_PRELOAD, true);
190                 confirmexit = cfg->readBoolEntry(OPT_CONFIRMEXIT, true);
191                 intbrowser = cfg->readBoolEntry(OPT_INTBROWSER, true);
192                 extbrowser = cfg->readEntry(OPT_EXTBROWSER, defbrowser);
193                 autoinfo = cfg->readBoolEntry(OPT_AUTOINFO, false);
194                 showsplash = cfg->readBoolEntry(OPT_SHOWSPLASH, true);
195                 thumbsage = cfg->readNumEntry(OPT_THUMBSAGE, 7);
196                 cachethumbs = cfg->readBoolEntry(OPT_CACHETHUMBS, true);
197                 editsupport = cfg->readBoolEntry(OPT_EDITING, false);
198                 tmpdir = cfg->readEntry(OPT_TMPDIR, QString::null);
199                 QDir dir(tmpdir);
200                 if (tmpdir.isNull() || !dir.exists())
201                         tmpdir = "/tmp";
202         cfg->endGroup();
203 }
204
205 bool ComicBookSettings::smallCursor() const
206 {
207         return smallcursor;
208 }
209
210 bool ComicBookSettings::twoPagesMode() const
211 {
212         return twopages;
213 }
214
215 bool ComicBookSettings::twoPagesStep() const
216 {
217         return twopagesstep;
218 }
219
220 bool ComicBookSettings::twoPagesSpread() const
221 {
222         return twopagesspread;
223 }
224
225 bool ComicBookSettings::twoPagesSkip() const
226 {
227         return twopagesskip;
228 }
229
230 bool ComicBookSettings::japaneseMode() const
231 {
232         return japanese;
233 }
234
235 bool ComicBookSettings::continuousScrolling() const
236 {
237         return contscroll;
238 }
239
240 bool ComicBookSettings::scrollbarsVisible() const
241 {
242         return scrollbars;
243 }
244
245 QRect ComicBookSettings::geometry() const
246 {
247         return QRect(x, y, w, h);
248 }
249
250 Size ComicBookSettings::pageSize() const
251 {
252         return pagesize;
253 }
254
255 /*Scaling ComicBookSettings::pageScaling() const
256 {
257         return scaling;
258 }*/
259
260 QString ComicBookSettings::lastDir() const
261 {
262         return lastdir;
263 }
264
265 const History& ComicBookSettings::recentlyOpened() const
266 {
267         return recent;
268 }
269
270 QColor ComicBookSettings::background() const
271 {
272         return bgcolor;
273 }
274
275 int ComicBookSettings::cacheSize() const
276 {
277         return cachesize;
278 }
279
280 bool ComicBookSettings::cacheThumbnails() const
281 {
282         return cachethumbs;
283 }
284
285 int ComicBookSettings::thumbnailsAge() const
286 {
287         return thumbsage;
288 }
289
290 bool ComicBookSettings::preloadPages() const
291 {
292         return preload;
293 }
294
295 bool ComicBookSettings::confirmExit() const
296 {
297         return confirmexit;
298 }
299
300 bool ComicBookSettings::autoInfo() const
301 {
302         return autoinfo;
303 }
304
305 bool ComicBookSettings::fullScreenHideMenu() const
306 {
307         return fscrhidemenu;
308 }
309
310 bool ComicBookSettings::fullScreenHideStatusbar() const
311 {
312         return fscrhidestatus;
313 }
314
315 bool ComicBookSettings::fullScreenHideToolbar() const
316 {
317         return fscrhidetoolbar;
318 }
319
320 bool ComicBookSettings::showStatusbar() const
321 {
322         return statusbar;
323 }
324
325 const QFont& ComicBookSettings::infoFont() const
326 {
327         return font;
328 }
329
330 bool ComicBookSettings::useInternalBrowser() const
331 {
332         return intbrowser;
333 }
334
335 QString ComicBookSettings::externalBrowser() const
336 {
337         return extbrowser;
338 }
339
340 bool ComicBookSettings::showSplash() const
341 {
342         return showsplash;
343 }
344
345 void ComicBookSettings::restoreDockLayout(QMainWindow *w)
346 {
347         QTextStream str(&docklayout, IO_ReadOnly);
348         str >> *w;
349 }
350
351 bool ComicBookSettings::editSupport() const
352 {
353         return editsupport;
354 }
355
356 QString ComicBookSettings::tmpDir() const
357 {
358         return tmpdir;
359 }
360
361 void ComicBookSettings::smallCursor(bool f)
362 {
363         if (f != smallcursor)
364         {
365                 cfg->writeEntry(GRP_VIEW OPT_SMALLCURSOR, smallcursor = f);
366                 emit cursorChanged(f);
367         }
368 }
369
370 void ComicBookSettings::twoPagesMode(bool f)
371 {
372         if (f != twopages)
373                 cfg->writeEntry(GRP_VIEW OPT_TWOPAGES, twopages = f);
374 }
375
376 void ComicBookSettings::twoPagesStep(bool f)
377 {
378         if (f != twopagesstep)
379                 cfg->writeEntry(GRP_NAVI OPT_TWOPAGESSTEP, twopagesstep = f);
380 }
381
382 void ComicBookSettings::twoPagesSpread(bool f)
383 {
384         if (f != twopagesspread)
385                 cfg->writeEntry(GRP_NAVI OPT_TWOPAGESSPREAD, twopagesspread = f);
386 }
387
388 void ComicBookSettings::twoPagesSkip(bool f)
389 {
390         if (f != twopagesskip)
391                 cfg->writeEntry(GRP_NAVI OPT_TWOPAGESSKIP, twopagesskip = f);
392 }
393
394 void ComicBookSettings::japaneseMode(bool f)
395 {
396         if (f != japanese)
397                 cfg->writeEntry(GRP_VIEW OPT_JAPANESEMODE, japanese = f);
398 }
399
400 void ComicBookSettings::continuousScrolling(bool f)
401 {
402         if (f != contscroll)
403                 cfg->writeEntry(GRP_NAVI OPT_CONTSCROLL, contscroll = f);
404 }
405
406 void ComicBookSettings::scrollbarsVisible(bool f)
407 {
408         if (f != scrollbars)
409                 cfg->writeEntry(GRP_VIEW OPT_SCROLLBARS, scrollbars = f);
410 }
411
412 void ComicBookSettings::geometry(const QRect g)
413 {
414         if (x != g.x() || y != g.y() || w != g.width() || h != g.height())
415         {
416                 cfg->beginGroup(GRP_WINDOW);
417                 cfg->writeEntry(OPT_X, x = g.x());
418                 cfg->writeEntry(OPT_Y, y = g.y());
419                 cfg->writeEntry(OPT_WIDTH, w = g.width());
420                 cfg->writeEntry(OPT_HEIGHT, h = g.height());
421                 cfg->endGroup();
422         }
423 }
424
425 void ComicBookSettings::pageSize(Size s)
426 {
427         if (s != pagesize)
428                 cfg->writeEntry(GRP_VIEW OPT_PAGESIZE, convert(size2string, pagesize = s));
429 }
430
431 /*void ComicBookSettings::pageScaling(Scaling s)
432 {
433         if (s != scaling)
434         {
435                 cfg->writeEntry(GRP_VIEW OPT_SCALING, convert(scaling2string, scaling = s));
436                 emit scalingMethodChanged(scaling);
437         }
438 }*/
439
440 void ComicBookSettings::lastDir(const QString &d)
441 {
442         if (lastdir != d)
443                 cfg->writeEntry(GRP_MISC OPT_LASTDIR, lastdir = d);
444 }
445
446 void ComicBookSettings::recentlyOpened(const History &hist)
447 {
448         recent = hist;
449         cfg->writeEntry(GRP_MISC OPT_RECENT, recent.getAll());
450 }
451
452 void ComicBookSettings::background(const QColor &color)
453 {
454         if (color != bgcolor)
455         {
456                 bgcolor = color;
457                 cfg->writeEntry(GRP_VIEW OPT_BACKGROUND, bgcolor.name());
458                 emit backgroundChanged(bgcolor);
459         }
460 }
461
462 void ComicBookSettings::cacheSize(int s)
463 {
464         if (s != cachesize)
465         {
466                 if (s < 1)
467                         s = 1;
468                 cfg->writeEntry(GRP_MISC OPT_CACHESIZE, cachesize = s);
469         }
470 }
471
472 void ComicBookSettings::cacheThumbnails(bool f)
473 {
474         if (f != cachethumbs)
475                 cfg->writeEntry(GRP_MISC OPT_CACHETHUMBS, cachethumbs = f);
476 }
477
478 void ComicBookSettings::thumbnailsAge(int n)
479 {
480         if (n != thumbsage)
481                 cfg->writeEntry(GRP_MISC OPT_THUMBSAGE, thumbsage = n);
482 }
483
484 void ComicBookSettings::preloadPages(bool f)
485 {
486         if (f != preload)
487                 cfg->writeEntry(GRP_MISC OPT_PRELOAD, preload = f);
488 }
489
490 void ComicBookSettings::confirmExit(bool f)
491 {
492         if (f != confirmexit)
493                 cfg->writeEntry(GRP_MISC OPT_CONFIRMEXIT, confirmexit = f);
494 }
495
496 void ComicBookSettings::autoInfo(bool f)
497 {
498         if (f != autoinfo)
499                 cfg->writeEntry(GRP_MISC OPT_AUTOINFO, autoinfo = f);
500 }
501
502 void ComicBookSettings::fullScreenHideMenu(bool f)
503 {
504         if (f != fscrhidemenu)
505                 cfg->writeEntry(GRP_VIEW OPT_FULLSCREENHIDEMENU, fscrhidemenu = f);
506 }
507
508 void ComicBookSettings::fullScreenHideStatusbar(bool f)
509 {
510         if (f != fscrhidestatus)
511                 cfg->writeEntry(GRP_VIEW OPT_FULLSCREENHIDESTATUS, fscrhidestatus = f);
512 }
513
514 void ComicBookSettings::fullScreenHideToolbar(bool f)
515 {
516         if (f != fscrhidetoolbar)
517                 cfg->writeEntry(GRP_VIEW OPT_FULLSCREENHIDETOOLBAR, fscrhidetoolbar = f);
518 }
519
520 void ComicBookSettings::showStatusbar(bool f)
521 {
522         if (f != statusbar)
523                 cfg->writeEntry(GRP_VIEW OPT_STATUSBAR, statusbar = f);
524 }
525
526 void ComicBookSettings::infoFont(const QFont &s)
527 {
528         if (s != font)
529         {
530                 font = s;
531                 cfg->writeEntry(GRP_VIEW OPT_FONT, font.toString());
532         }
533 }
534
535 void ComicBookSettings::useInternalBrowser(bool f)
536 {
537         if (f != intbrowser)
538                 cfg->writeEntry(GRP_MISC OPT_INTBROWSER, intbrowser = f);
539 }
540
541 void ComicBookSettings::externalBrowser(const QString& cmd)
542 {
543         if (cmd != extbrowser)
544                 cfg->writeEntry(GRP_MISC OPT_EXTBROWSER, extbrowser = cmd);
545 }
546
547 void ComicBookSettings::saveDockLayout(QMainWindow *w)
548 {
549         QString tmp;
550         QTextStream str(&tmp, IO_WriteOnly);
551         str << *w;
552         cfg->writeEntry(GRP_WINDOW OPT_DOCKLAYOUT, tmp);
553 }
554
555 void ComicBookSettings::editSupport(bool f)
556 {
557         if (f != editsupport)
558                 cfg->writeEntry(GRP_MISC OPT_EDITING, editsupport = f);
559 }
560
561 void ComicBookSettings::showSplash(bool f)
562 {
563         if (f != showsplash)
564                 cfg->writeEntry(GRP_MISC OPT_SHOWSPLASH, showsplash = f);
565 }
566
567 void ComicBookSettings::tmpDir(const QString &dir)
568 {
569         if (dir != tmpdir)
570                 cfg->writeEntry(GRP_MISC OPT_TMPDIR, tmpdir = dir);
571 }
572
573
574 bool ComicBookSettings::stripMode() const
575 {
576         return stripmode;
577 }
578
579 int ComicBookSettings::stripMax() const
580 {
581         return stripmax;
582 }
583
584 void ComicBookSettings::stripMode(bool f)
585 {
586         if (f != stripmode)
587                 cfg->writeEntry(GRP_VIEW OPT_STRIPMODE, stripmode = f);
588 }
589
590 void ComicBookSettings::stripMax(int sm)
591 {
592         if (sm != stripmax)
593                 cfg->writeEntry(GRP_VIEW OPT_STRIPMAX, stripmax = sm);
594         emit stripMaxChanged(sm);
595 }
596