1/* This file is part of the KDE project
2 Copyright 2008 Dominik Haumann <dhaumann kde org>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License version 2 as published by the Free Software Foundation.
7
8 This library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Library General Public License for more details.
12
13 You should have received a copy of the GNU Library General Public License
14 along with this library; see the file COPYING.LIB. If not, write to
15 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 Boston, MA 02110-1301, USA.
17*/
18
19#include "btdatabase.h"
20
21#include <QFile>
22#include <QDataStream>
23#include <QDir>
24
25#include <kdebug.h>
26
27void KateBtDatabase::loadFromFile(const QString& url)
28{
29 QFile file(url);
30 if (file.open(QIODevice::ReadOnly)) {
31 QMutexLocker locker(&mutex);
32 QDataStream ds(&file);
33 ds >> db;
34 }
35 kDebug() << "Number of entries in the backtrace database:" << db.size();
36}
37
38void KateBtDatabase::saveToFile(const QString& url) const
39{
40 QFile file(url);
41 if (file.open(QIODevice::WriteOnly)) {
42 QMutexLocker locker(&mutex);
43 QDataStream ds(&file);
44 ds << db;
45 }
46}
47
48QString KateBtDatabase::value(const QString& key)
49{
50 // key is either of the form "foo/bar.txt" or only "bar.txt"
51 QString file = key;
52 QStringList sl = key.split('/');
53 if (sl.size() > 1) {
54 file = sl[1];
55 }
56
57 QMutexLocker locker(&mutex);
58 if (db.contains(file)) {
59 const QStringList& sl = db.value(file);
60 for (int i = 0; i < sl.size(); ++i) {
61 if (sl[i].indexOf(key) != -1) {
62 return sl[i];
63 }
64 }
65 // try to use the first one
66 if (sl.size() > 0) {
67 return sl[0];
68 }
69 }
70
71 return QString();
72}
73
74void KateBtDatabase::add(const QString& folder, const QStringList& files)
75{
76 QMutexLocker locker(&mutex);
77 foreach (const QString& file, files) {
78 QStringList& sl = db[file];
79 QString entry = QDir::fromNativeSeparators(folder + '/' + file);
80 if (!sl.contains(entry)) {
81 sl << entry;
82 }
83 }
84}
85
86int KateBtDatabase::size() const
87{
88 QMutexLocker locker(&mutex);
89 return db.size();
90}
91
92// kate: space-indent on; indent-width 2; replace-tabs on;
93