Import of original sources
[qcomicbook-oblomov] / src / bookmarks.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 "bookmarks.h"
14 #include "cbsettings.h"
15 #include <qpopupmenu.h>
16 #include <qfile.h>
17 #include <qdir.h>
18 #include <qtextstream.h>
19
20 using namespace QComicBook;
21
22 Bookmarks::Bookmarks(QPopupMenu *menu): bmenu(menu), changed(false)
23 {
24         blist.setAutoDelete(true);
25         fname = ComicBookSettings::bookmarksDir() + "/bookmarks";
26 }
27
28 Bookmarks::~Bookmarks()
29 {
30 }
31
32 bool Bookmarks::load()
33 {
34         if (fname.isEmpty())
35                 return false;
36         QFile f(fname);
37         if (f.open(IO_ReadOnly))
38         {
39                 QTextStream str(&f);
40                 while (!str.atEnd())
41                 {
42                         QString name = str.readLine();
43                         if (str.atEnd())
44                                 break;
45                         int page = str.readLine().toInt();
46                         set(name, page);
47                 }
48                 f.close();
49         }
50         return false;
51 }
52
53 bool Bookmarks::save()
54 {
55         if (!changed)
56                 return true;
57         if (fname.isEmpty())
58                 return false;
59
60         QFile f(fname);
61         if (f.open(IO_WriteOnly))
62         {
63                 QTextStream str(&f);
64                 for (Bookmark *b = blist.first(); b; b = blist.next())
65                         str << b->getName() << endl << b->getPage() << endl;
66                 f.close();
67                 return true;
68         }
69         return false;
70 }
71
72 void Bookmarks::set(const QString &cbname, int page)
73 {
74         int id;
75         for (Bookmark *b = blist.first(); b; b = blist.next())
76                 if ((b->getName() == cbname))
77                 {
78                         if (b->getPage() == page) //same page, do nothing
79                                 return;
80                         id = b->getId();
81                         bmenu->removeItem(id);
82                         bmap.remove(id);
83                         b->setName(cbname);
84                         b->setPage(page);
85                         id = bmenu->insertItem(b->menuItemName());
86                         b->setId(id);
87                         bmap.insert(id, b);
88                         changed = true;
89                         return;
90                 }
91         Bookmark *b = new Bookmark(cbname, page);
92         blist.append(b);
93         id = bmenu->insertItem(b->menuItemName());
94         b->setId(id);
95         bmap.insert(id, b);
96         changed = true;
97         return;
98 }
99
100 bool Bookmarks::remove(const QString &cbname)
101 {
102         for (Bookmark *b = blist.first(); b; b = blist.next())
103                 if ((b->getName() == cbname))
104                 {
105                         bmenu->removeItem(b->getId());
106                         bmap.remove(b->getId());
107                         blist.remove();
108                         return true;
109                 }
110         return false;
111 }
112
113 bool Bookmarks::remove(int id)
114 {
115         if (bmap.contains(id))
116         {
117                 Bookmark *b = bmap[id];
118                 bmenu->removeItem(id);
119                 bmap.remove(id);
120                 blist.remove(b);
121                 changed = true;
122         }
123 }
124
125 bool Bookmarks::get(int id, Bookmark &b)
126 {
127         if (bmap.contains(id))
128         {
129                 b = *bmap[id];
130                 return true;
131         }
132         return false;
133 }
134
135 QValueList<Bookmark> Bookmarks::get()
136 {
137         QValueList<Bookmark> res;
138         for (Bookmark *b = blist.first(); b; b = blist.next())
139                 res.push_back(*b);
140         return res;
141 }
142
143 bool Bookmarks::exists(const QString &cbname)
144 {
145         for (Bookmark *b = blist.first(); b; b = blist.next())
146                 if (b->getName() == cbname)
147                         return true;
148         return false;
149 }
150