-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathsession.cpp
More file actions
82 lines (74 loc) · 1.98 KB
/
Copy pathsession.cpp
File metadata and controls
82 lines (74 loc) · 1.98 KB
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
#include "session.h"
#include <QStandardPaths>
#include <QFile>
#include <QDir>
#include <QTextStream>
#include <QDebug>
SessionList::SessionList(const QString& fnarg)
{
needSave_ = false;
QDir dir;
if (fnarg == "") {
dir = QStandardPaths::writableLocation(QStandardPaths::CacheLocation);
fn_ = dir.filePath("sessions");
} else {
fn_ = fnarg;
dir = QDir(fnarg).dirName();
}
QFile f(fn_);
if (!dir.mkpath(dir.absolutePath())) {
qDebug() << "cannot create dir" << dir.absolutePath();
return;
}
if (!f.exists()) {
qDebug() << "no session file" << fn_;
return;
}
if (!f.open(QIODevice::ReadOnly)) {
qDebug() << "open session failed";
return;
}
QTextStream in(&f);
while (!in.atEnd()) {
QString line = in.readLine();
auto cols = line.split("\t");
if (cols.length() < 5) {
qDebug() << "bad session line <"<<line<<">";
continue;
}
ses_.append(Session({cols[0], cols[1], cols[2], cols[3], cols[4]}));
}
f.close();
}
bool SessionList::save()
{
QFile f(fn_);
if (!f.open(QIODevice::WriteOnly|QIODevice::Truncate)) {
qDebug() << "failed to open w+" << fn_;
return false;
}
QTextStream out(&f);
for (int i = 0; i < qMin(ses_.size(), 5); i++) {
auto &s = ses_[i];
out << s.fileA << "\t"
<< s.filterA << "\t"
<< s.fileB << "\t"
<< s.filterB << "\t"
<< s.diffFilter << "\n";
}
f.close();
return true;
}
void SessionList::add(const QString &fna, const QString &fa, const QString &fnb, const QString &fb, const QString &df)
{
needSave_ = true;
for (auto &s : ses_) {
if (s.fileA == fna && s.fileB == fnb) {
s.filterA = fa;
s.filterB = fb;
s.diffFilter = df;
return;
}
}
ses_.append(Session({fna, fa, fnb, fb, df}));
}