2
This file is part of the Nepomuk KDE project.
3
Copyright (C) 2010 Vishesh Handa <handa.vish@gmail.com>
5
This library is free software; you can redistribute it and/or
6
modify it under the terms of the GNU Lesser General Public
7
License as published by the Free Software Foundation; either
8
version 2.1 of the License, or (at your option) version 3, or any
9
later version accepted by the membership of KDE e.V. (or its
10
successor approved by the membership of KDE e.V.), which shall
11
act as a proxy defined in Section 6 of version 3 of the license.
13
This library is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
Lesser General Public License for more details.
18
You should have received a copy of the GNU Lesser General Public
19
License along with this library. If not, see <http://www.gnu.org/licenses/>.
23
#include "logstorage.h"
25
#include <QtCore/QDir>
27
#include "changelog.h"
28
#include "changelogrecord.h"
30
#include <KStandardDirs>
33
Nepomuk::LogStorage::LogStorage()
35
m_dirUrl = KStandardDirs::locateLocal( "data", "nepomuk/backupsync/log/" );
40
Nepomuk::LogStorage::~LogStorage()
42
// Always save all the records on exit
47
class Nepomuk::LogStorageHelper
50
Nepomuk::LogStorage q;
52
K_GLOBAL_STATIC(Nepomuk::LogStorageHelper, instanceHelper)
55
Nepomuk::LogStorage* Nepomuk::LogStorage::instance()
57
return &(instanceHelper->q);
61
Nepomuk::ChangeLog Nepomuk::LogStorage::getChangeLog(const QString& minDate)
63
return getChangeLog( QDateTime::fromString( minDate, ChangeLog::dateTimeFormat() ) );
66
Nepomuk::ChangeLog Nepomuk::LogStorage::getChangeLog(const QDateTime& min)
68
// Always save all the existing records before accessing them. It's like flushing the cache
74
const QStringList& entries = dir.entryList( QDir::Files, QDir::Name );
75
if( entries.empty() ) {
76
kDebug() << "No enteries to generate a ChangeLog from";
80
foreach( const QString & fileName, entries ) {
81
QDateTime dt = QDateTime::fromString( fileName, ChangeLog::dateTimeFormat() );
85
//TODO: Optimize : Every record shouldn't be checked. Be smart!
86
log += ChangeLog::fromUrl( QString( m_dirUrl + fileName ), min );
89
//TODO: Optimize this!
91
log.removeRecordsAfter( m_lockTime );
97
void Nepomuk::LogStorage::addRecord(const Nepomuk::ChangeLogRecord& record)
99
//kDebug() << record.st();
100
m_records.append( record );
102
if( m_records.size() >= m_maxFileRecords ) {
103
kDebug() << "Saving Records .. " << m_records.size();
110
//IMPORTANT: This function doesn't actually remove ALL the records less than min
111
// This has been done purposely, as otherwise one would need to read the entire contents
112
// of at least one LogFile.
113
void Nepomuk::LogStorage::removeRecords(const QDateTime& min)
115
QDir dir( m_dirUrl );
116
const QStringList& entries = dir.entryList( QDir::Files, QDir::Name );
118
foreach( const QString fileName, entries ) {
119
QDateTime dt = QDateTime::fromString(fileName, ChangeLog::dateTimeFormat());
121
QFile file( m_dirUrl + fileName );
130
//TODO: Optimize : Currently O(n)
131
int countRecords( const QString & url ) {
135
file.open( QIODevice::ReadOnly | QIODevice::Text );
137
QTextStream in( &file );
141
QString line = in.readLine();
149
//FIXME: The code is too complicated and POSSIBLY buggy. It's buggy in the way that it makes
150
// the service usage skyrocket to 50%+ for large amounts of time.
151
bool Nepomuk::LogStorage::saveRecords()
153
if( m_records.empty() )
158
// Always sort them not mattter what. I don't know why I get weird results otherwise.
161
QDir dir( m_dirUrl );
162
const QStringList& entries = dir.entryList( QDir::Files, QDir::Reversed );
164
QDateTime max = m_records.last().dateTime();
167
if( entries.empty() ) {
168
QString path = m_dirUrl + max.toString( ChangeLog::dateTimeFormat() );
169
ChangeLogRecord::saveRecords( m_records, path );;
173
QString fileName = entries.first();
176
const int end = m_records.size() - 1;
179
QString filePath = m_dirUrl + fileName;
181
int alreadyPresent = countRecords( filePath );
182
int numRecords = end - begin + 1;
183
int availSize = m_maxFileRecords - alreadyPresent;
185
int numToSave = availSize > numRecords ? numRecords : availSize;
187
if( numToSave > 0 ) {
188
ChangeLogRecord::saveRecords( m_records.mid( begin, numToSave ), filePath );
189
QString newName = m_records[ begin + numToSave -1 ].dateTime().toString( ChangeLog::dateTimeFormat() );
190
QFile file( filePath );
191
file.rename( m_dirUrl + newName );
199
fileName = m_records.last().dateTime().toString( ChangeLog::dateTimeFormat() );
202
kDebug() << "Saved!";
207
void Nepomuk::LogStorage::lock()
210
m_lockTime = QDateTime::currentDateTime();
213
void Nepomuk::LogStorage::unlock()