Initial commit
[keepassx] / src / lib / bookmarks.cpp
1 /***************************************************************************
2  *   Copyright (C) 2007 by Tarek Saidi                                     *
3  *   tarek.saidi@arcor.de                                                  *
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; version 2 of the License.               *
8  *                                                                         *
9  *   This program is distributed in the hope that it will be useful,       *
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12  *   GNU General Public License for more details.                          *
13  *                                                                         *
14  *   You should have received a copy of the GNU General Public License     *
15  *   along with this program; if not, write to the                         *
16  *   Free Software Foundation, Inc.,                                       *
17  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
18  ***************************************************************************/
19
20
21
22 QList<KpxBookmarks::BookmarkEntry> KpxBookmarks::Bookmarks;
23
24 void KpxBookmarks::load(){
25         if (!config->settings.contains("Bookmarks/size") || config->settings.value("Bookmarks/size").toInt()==0)
26                 return;
27         
28         int size = config->settings.value("Bookmarks/size").toInt();
29         for(int i=1;i<=size;i++){
30                 BookmarkEntry entry;
31                 entry.Title = config->settings.value( QString("Bookmarks/%1/Title").arg(i) ).toString();
32                 entry.Path = config->settings.value( QString("Bookmarks/%1/Path").arg(i) ).toString();
33                 if (!entry.Title.isNull() && !entry.Path.isNull())
34                         Bookmarks << entry;
35         }
36 }
37
38 int KpxBookmarks::count(){
39         return Bookmarks.size();
40 }
41
42 QString KpxBookmarks::title(int i){
43         return Bookmarks[i].Title;
44 }
45
46 QString KpxBookmarks::path(int i){
47         return Bookmarks[i].Path;
48 }
49
50
51 int KpxBookmarks::add(const QString& Title,const QString& Path){
52         BookmarkEntry entry;
53         entry.Title=Title;
54         entry.Path=Path;
55         entry.Index=Bookmarks.size();
56         Bookmarks<<entry;
57         save();
58         return Bookmarks.size()-1;
59 }
60
61 void KpxBookmarks::save(){
62         for (int i=0;i<count();i++){
63                 config->settings.setValue( QString("Bookmarks/%1/Title").arg(i+1), Bookmarks[i].Title );
64                 config->settings.setValue( QString("Bookmarks/%1/Path").arg(i+1), Bookmarks[i].Path );
65         }
66         config->settings.setValue("Bookmarks/size", count());
67         
68         // remove orphaned entries
69         int i = count()+1;
70         while ( config->settings.contains( QString("Bookmarks/%1/Title").arg(i) ) ){
71                 config->settings.remove( QString("Bookmarks/%1/Title").arg(i) );
72                 config->settings.remove( QString("Bookmarks/%1/Path").arg(i) );
73                 i++;
74         }
75 }
76
77 void KpxBookmarks::remove(int index){
78         Bookmarks.removeAt(index);
79         save();
80 }
81
82 void KpxBookmarks::edit(const QString& Title,const QString& Path,int i){
83         Bookmarks[i].Title=Title;
84         Bookmarks[i].Path=Path;
85         save();
86 }
87
88
89 void KpxBookmarks::resort(QList<int> order){
90         QList<BookmarkEntry> NewList;
91         for(int i=0;i<order.size();i++){
92                 NewList << Bookmarks[order[i]];
93         }
94         Bookmarks=NewList;
95         save();
96 }