FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
ghostwriter/src/DocumentHistory.cpp at master · jackcoolpython/ghostwriter · GitHub
jackcoolpython
ghostwriter
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
ghostwriter
/
src
/
DocumentHistory.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
186 lines (151 loc) · 4.27 KB
Breadcrumbs
ghostwriter
/
src
/
DocumentHistory.cpp
Copy path
File metadata and controls
186 lines (151 loc) · 4.27 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
**********************************************************************
*
* Copyright (C) 2014, 2015 wereturtle
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
**********************************************************************
*/
#
include
<
QString
>
#
include
<
QStringList
>
#
include
<
QList
>
#
include
<
QFileInfo
>
#
include
<
QSettings
>
#
include
"
DocumentHistory.h
"
#
define
MAX_FILE_HISTORY_SIZE
20
#
define
FILE_HISTORY_KEY
"
FileHistory
"
#
define
FILE_PATH_KEY
"
filePath
"
#
define
CURSOR_POSITION_KEY
"
cursorPosition
"
DocumentHistory::DocumentHistory
()
{
}
DocumentHistory::~DocumentHistory
()
{
}
QStringList
DocumentHistory::getRecentFiles
(
int
max)
{
RecentFilesList recentFiles =
loadFromSettings
(max);
QStringList filePathList;
for
(
int
i =
0
; i < recentFiles.
size
(); i++)
{
filePathList.
append
(recentFiles.
at
(i).
filePath
);
}
return
filePathList;
}
void
DocumentHistory::add
(
const
QString& filePath,
int
cursorPosition
)
{
QFileInfo
fileInfo
(filePath);
if
(fileInfo.
exists
())
{
QString sanitizedPath = fileInfo.
canonicalFilePath
();
RecentFilesList recentFiles =
loadFromSettings
();
RecentFile lastFile;
lastFile.
filePath
= sanitizedPath;
lastFile.
position
= cursorPosition;
recentFiles.
removeAll
(lastFile);
recentFiles.
prepend
(lastFile);
cleanUpHistory
(recentFiles);
storeToSettings
(recentFiles);
}
}
int
DocumentHistory::getCursorPosition
(
const
QString& filePath)
{
QString sanitizedPath =
QFileInfo
(filePath).
canonicalFilePath
();
int
position =
0
;
RecentFilesList recentFiles =
loadFromSettings
();
cleanUpHistory
(recentFiles);
foreach
(RecentFile file, recentFiles)
{
if
(sanitizedPath == file.
filePath
)
{
position = file.
position
;
break
;
}
}
if
(position <
0
)
{
position =
0
;
}
return
position;
}
void
DocumentHistory::clear
()
{
QSettings settings;
settings.
remove
(
FILE_HISTORY_KEY
);
}
DocumentHistory::RecentFilesList
DocumentHistory::loadFromSettings
(
int
max)
{
QSettings settings;
int
size = settings.
beginReadArray
(
FILE_HISTORY_KEY
);
RecentFilesList recentFiles;
if
(max <
0
)
{
max = size;
}
int
filesAdded =
0
;
for
(
int
i =
0
; (i < size) && (filesAdded < max); i++)
{
settings.
setArrayIndex
(i);
QString filePath = settings.
value
(
FILE_PATH_KEY
).
toString
();
int
position = settings.
value
(
CURSOR_POSITION_KEY
,
0
).
toInt
();
if
(!filePath.
isNull
() && !filePath.
isEmpty
() &&
QFileInfo
(filePath).
exists
())
{
RecentFile recentFile;
recentFile.
filePath
= filePath;
recentFile.
position
= position;
recentFiles.
append
(recentFile);
filesAdded++;
}
}
settings.
endArray
();
return
recentFiles;
}
void
DocumentHistory::storeToSettings
(
const
DocumentHistory::RecentFilesList& recentFiles
)
{
QSettings settings;
if
(recentFiles.
isEmpty
())
{
settings.
remove
(
FILE_HISTORY_KEY
);
return
;
}
settings.
beginWriteArray
(
FILE_HISTORY_KEY
, recentFiles.
size
());
for
(
int
i =
0
; i < recentFiles.
size
(); i++)
{
RecentFile recentFile = recentFiles.
at
(i);
settings.
setArrayIndex
(i);
settings.
setValue
(
FILE_PATH_KEY
, recentFile.
filePath
);
settings.
setValue
(
CURSOR_POSITION_KEY
,
recentFile.
position
);
}
settings.
endArray
();
}
void
DocumentHistory::cleanUpHistory
(
DocumentHistory::RecentFilesList& recentFiles
)
{
while
(recentFiles.
size
() >
MAX_FILE_HISTORY_SIZE
)
{
recentFiles.
removeLast
();
}
}
Back
|
FazBrowse Home
|
New Git URL