Skip to content
This repository was archived by the owner on Aug 25, 2024. It is now read-only.

Commit 485143c

Browse files
committed
add clear secondary entry
1 parent bff664f commit 485143c

File tree

6 files changed

+95
-24
lines changed

6 files changed

+95
-24
lines changed

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44
*.svg
55
*.xcf
66
*.bak
7+
ConsoleApp*/
8+
Ignore*
9+
ClipboardR.db
10+
*.sqlite
11+
copy*.ps1
12+
QueryHandler.cs
13+
*PluginApi.cs
714

815
## Ignore Visual Studio temporary files, build results, and
916
## files generated by popular Visual Studio add-ons.

src/ClipboardR.Core/CbMonitor.cs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -172,13 +172,22 @@ internal void Invoke(object? content, CbContentType type, SourceApplication sour
172172
/// </summary>
173173
public class ClipboardChangedEventArgs : EventArgs
174174
{
175-
public ClipboardChangedEventArgs(object? content, CbContentType contentType, SourceApplication source)
175+
public ClipboardChangedEventArgs(
176+
object? content,
177+
CbContentType contentType,
178+
SourceApplication source
179+
)
176180
{
177181
Content = content;
178182
ContentType = contentType;
179183

180-
SourceApplication = new SourceApplication(source.Id, source.Handle, source.Name,
181-
source.Title, source.Path);
184+
SourceApplication = new SourceApplication(
185+
source.Id,
186+
source.Handle,
187+
source.Name,
188+
source.Title,
189+
source.Path
190+
);
182191
}
183192

184193
#region Properties
@@ -209,8 +218,8 @@ public ClipboardChangedEventArgs(object? content, CbContentType contentType, Sou
209218
#region Private
210219

211220
/// <summary>
212-
/// This initiates a Timer that then begins the
213-
/// clipboard-monitoring service. The Timer will
221+
/// This initiates a Timer that then begins the
222+
/// clipboard-monitoring service. The Timer will
214223
/// auto-shutdown once the service has started.
215224
/// </summary>
216225
private void OnLoad(object? sender, EventArgs e)
@@ -287,8 +296,7 @@ public class SourceApplication
287296
/// <param name="name">The application's name.</param>
288297
/// <param name="title">The application's title.</param>
289298
/// <param name="path">The application's path.</param>
290-
internal SourceApplication(int id, IntPtr handle, string name,
291-
string title, string path)
299+
internal SourceApplication(int id, IntPtr handle, string name, string title, string path)
292300
{
293301
Id = id;
294302
Name = name;
@@ -329,16 +337,15 @@ internal SourceApplication(int id, IntPtr handle, string name,
329337
#region Overrides
330338

331339
/// <summary>
332-
/// Returns a <see cref="string"/> containing the list
340+
/// Returns a <see cref="string"/> containing the list
333341
/// of application details provided.
334342
/// </summary>
335343
public override string ToString()
336344
{
337-
return $"ID: {Id}; Handle: {Handle}, Name: {Name}; " +
338-
$"Title: {Title}; Path: {Path}";
345+
return $"ID: {Id}; Handle: {Handle}, Name: {Name}; " + $"Title: {Title}; Path: {Path}";
339346
}
340347

341348
#endregion
342349
}
343350

344-
#endregion
351+
#endregion

src/ClipboardR.Core/DbHelper.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,13 @@ public async void DeleteOneRecord(ClipboardData clipboardData)
131131
CloseIfNotKeep();
132132
}
133133

134+
public async void DeleteAllRecords()
135+
{
136+
var sql = "DROP TABLE IF EXISTS record; DROP TABLE IF EXISTS assets; VACUUM;";
137+
await Connection.ExecuteAsync(sql);
138+
CreateDb();
139+
}
140+
134141
public async void PinOneRecord(ClipboardData clipboardData)
135142
{
136143
var sql = "UPDATE record SET pined=@Pin WHERE hash_id=@HashId";

src/ClipboardR.Core/Enums.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,10 @@ public enum CbOrders
1414
CreateTime = 1,
1515
SourceApplication = 2,
1616
Type = 3,
17+
}
18+
19+
public enum CbResultType
20+
{
21+
Record,
22+
Clear,
1723
}

src/ClipboardR.Core/Settings.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ public class Settings
2222

2323
public int OrderBy { get; set; } = 0;
2424

25+
// TODO: add this in settings panel
26+
public string ClearKeyword { get; set; } = "clear";
27+
2528
public void Save()
2629
{
2730
var options = new JsonSerializerOptions { WriteIndented = true };
@@ -30,10 +33,11 @@ public void Save()
3033

3134
public static Settings Load(string filePath)
3235
{
33-
var options = new JsonSerializerOptions() { WriteIndented = true};
36+
var options = new JsonSerializerOptions() { WriteIndented = true };
3437
using var fs = File.OpenRead(filePath);
3538
Console.WriteLine();
36-
return JsonSerializer.Deserialize<Settings>(fs, options) ?? new Settings(){ConfigFile = filePath};
39+
return JsonSerializer.Deserialize<Settings>(fs, options)
40+
?? new Settings() { ConfigFile = filePath };
3741
}
3842

3943
public override string ToString()

src/ClipboardR/Main.cs

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,43 @@ private async void RestoreRecordsFromDb()
100100

101101
public List<Result> Query(Query query)
102102
{
103+
var results = new List<Result>();
104+
if (query.FirstSearch == _settings.ClearKeyword)
105+
{
106+
results.AddRange(
107+
new[]
108+
{
109+
new Result
110+
{
111+
Title = "records",
112+
SubTitle = "clear records in memory only",
113+
IcoPath = _defaultIconPath,
114+
Score = 21,
115+
Action = _ =>
116+
{
117+
_dataList.Clear();
118+
return true;
119+
},
120+
},
121+
new Result
122+
{
123+
Title = "database",
124+
SubTitle = "clear records in database too",
125+
IcoPath = _defaultIconPath,
126+
Score = 1,
127+
Action = _ =>
128+
{
129+
_dataList.Clear();
130+
_dbHelper.DeleteAllRecords();
131+
CurrentScore = 1;
132+
return true;
133+
},
134+
}
135+
}
136+
);
137+
138+
return results;
139+
}
103140
var displayData =
104141
query.Search.Trim().Length == 0
105142
? _dataList.ToArray()
@@ -111,22 +148,22 @@ public List<Result> Query(Query query)
111148
)
112149
.ToArray();
113150

114-
var results = new List<Result>();
115151
results.AddRange(displayData.Select(ClipDataToResult));
116152
_context.API.LogDebug(ClassName, "Added to result");
117153
results.Add(
118-
new Result()
154+
new Result
119155
{
120156
Title = "Clear All Records",
121157
SubTitle = "Click to clear all records",
122158
IcoPath = _defaultIconPath,
123159
Score = 1,
124160
Action = _ =>
125161
{
126-
_dataList.Clear();
127-
// TODO: Add option in settings to select clear records from database
128-
CurrentScore = 1;
129-
return true;
162+
_context.API.ChangeQuery(
163+
_context.CurrentPluginMetadata.ActionKeyword + " clear ",
164+
true
165+
);
166+
return false;
130167
},
131168
}
132169
);
@@ -229,7 +266,7 @@ private void _OnClipboardChange(object? sender, CbMonitor.ClipboardChangedEventA
229266
clipboardData.DisplayTitle = Regex.Replace(clipboardData.Text.Trim(), @"(\r|\n|\t|\v)", "");
230267

231268
// make sure no repeat
232-
if (_dataList.Any(node => node.GetMd5()==clipboardData.GetMd5()))
269+
if (_dataList.Any(node => node.GetMd5() == clipboardData.GetMd5()))
233270
return;
234271
_context.API.LogDebug(ClassName, "Adding to dataList");
235272
_dataList.AddFirst(clipboardData);
@@ -265,8 +302,10 @@ public void Dispose()
265302
};
266303
foreach (var pair in kv)
267304
{
268-
_dbHelper?.DeleteRecordByKeepTime((int)pair.Item1,
269-
CmBoxIndexMapper.ToKeepTime(pair.Item2));
305+
_dbHelper?.DeleteRecordByKeepTime(
306+
(int)pair.Item1,
307+
CmBoxIndexMapper.ToKeepTime(pair.Item2)
308+
);
270309
}
271310

272311
_dbHelper?.Close();
@@ -306,7 +345,8 @@ public void PinOneRecord(ClipboardData c)
306345

307346
public int GetNewScoreByOrderBy(ClipboardData clipboardData)
308347
{
309-
if (clipboardData.Pined) return int.MaxValue;
348+
if (clipboardData.Pined)
349+
return int.MaxValue;
310350
var orderBy = (CbOrders)_settings.OrderBy;
311351
int score = 0;
312352
switch (orderBy)
@@ -336,4 +376,4 @@ public void Save()
336376
{
337377
_settings.Save();
338378
}
339-
}
379+
}

0 commit comments

Comments
 (0)