forked from chris2511/xca
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbhistory.cpp
94 lines (78 loc) · 1.57 KB
/
dbhistory.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/* vi: set sw=4 ts=4:
*
* Copyright (C) 2020 Christian Hohnstaedt.
*
* All rights reserved.
*/
#include <QString>
#include <QDir>
#include "xfile.h"
#include "func.h"
#include "dbhistory.h"
#include "database_model.h"
static QString dbhistory_file()
{
return getUserSettingsDir() + "/dbhistory";
}
QString dbhistory::lastRemote;
dbhistory::dbhistory()
{
QString name;
XFile file(dbhistory_file());
try {
file.open_read();
} catch (...) {
return;
}
history.clear();
while (!file.atEnd()) {
QByteArray ba;
ba = file.readLine(1024);
if (ba.size() == 0)
break;
name = QString::fromUtf8(ba).trimmed();
if (name.size() == 0)
continue;
if (history.indexOf(name) == -1)
history << name;
}
file.close();
foreach(name, history) {
if (database_model::isRemoteDB(name)) {
setLastRemote(name);
break;
}
}
}
void dbhistory::addEntry(const QString &name)
{
int pos;
QString fname = name;
if (!database_model::isRemoteDB(fname))
fname = relativePath(fname);
pos = history.indexOf(fname);
if (pos == 0)
return; /* no changes */
if (pos > 0)
history.removeAt(pos);
history.prepend(fname);
while (history.size() > 10)
history.removeLast();
XFile file(dbhistory_file());
if (!file.open_write())
return;
QString all = history.join("\n");
if (file.write(all.toUtf8()) <= 0)
qDebug() << "Error writing history" << file.fileName()
<< file.errorString();
file.close();
}
void dbhistory::setLastRemote(const QString &db)
{
if (database_model::isRemoteDB(db))
lastRemote = db;
}
QString dbhistory::getLastRemote()
{
return lastRemote;
}