File indexing completed on 2024-05-05 05:51:19
0001 /* This file is part of the KDE project 0002 SPDX-FileCopyrightText: 2008-2014 Dominik Haumann <dhaumann kde org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-only 0005 */ 0006 0007 #include "btdatabase.h" 0008 0009 #include <QDataStream> 0010 #include <QDebug> 0011 #include <QDir> 0012 #include <QFile> 0013 0014 void KateBtDatabase::loadFromFile(const QString &url) 0015 { 0016 QFile file(url); 0017 if (file.open(QIODevice::ReadOnly)) { 0018 QMutexLocker locker(&mutex); 0019 QDataStream ds(&file); 0020 ds >> db; 0021 } 0022 // qDebug() << "Number of entries in the backtrace database" << url << ":" << db.size(); 0023 } 0024 0025 void KateBtDatabase::saveToFile(const QString &url) const 0026 { 0027 QFile file(url); 0028 if (file.open(QIODevice::WriteOnly)) { 0029 QMutexLocker locker(&mutex); 0030 QDataStream ds(&file); 0031 ds << db; 0032 } 0033 // qDebug() << "Saved backtrace database to" << url; 0034 } 0035 0036 QString KateBtDatabase::value(const QString &key) 0037 { 0038 // key is either of the form "foo/bar.txt" or only "bar.txt" 0039 QString file = key; 0040 const QStringList split_key = key.split(QLatin1Char('/')); 0041 if (split_key.size() > 1) { 0042 file = split_key[1]; 0043 } 0044 0045 QMutexLocker locker(&mutex); 0046 if (db.contains(file)) { 0047 const QStringList &sl = db.value(file); 0048 for (int i = 0; i < sl.size(); ++i) { 0049 if (sl[i].indexOf(key) != -1) { 0050 return sl[i]; 0051 } 0052 } 0053 // try to use the first one 0054 if (!sl.empty()) { 0055 return sl[0]; 0056 } 0057 } 0058 0059 return QString(); 0060 } 0061 0062 void KateBtDatabase::add(const QString &folder, const QStringList &files) 0063 { 0064 QMutexLocker locker(&mutex); 0065 for (const QString &file : files) { 0066 QStringList &sl = db[file]; 0067 QString entry = QDir::fromNativeSeparators(folder + QLatin1Char('/') + file); 0068 if (!sl.contains(entry)) { 0069 sl << entry; 0070 } 0071 } 0072 } 0073 0074 int KateBtDatabase::size() const 0075 { 0076 QMutexLocker locker(&mutex); 0077 return db.size(); 0078 } 0079 0080 // kate: space-indent on; indent-width 4; replace-tabs on;