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