Skip to content

Commit 25feac5

Browse files
committed
add import database function
1 parent 3c39f77 commit 25feac5

File tree

3 files changed

+98
-33
lines changed

3 files changed

+98
-33
lines changed

src/Flow.Launcher.Plugin.ClipboardPlus.Core/Data/Models/Clipboard/ClipboardData.cs

+69-32
Original file line numberDiff line numberDiff line change
@@ -321,35 +321,6 @@ public ClipboardData()
321321
/// </returns>
322322
public static ClipboardData FromRecord(Record record)
323323
{
324-
static object? StringToData(string str, DataType type, bool encryptData)
325-
{
326-
if (!string.IsNullOrEmpty(str) && encryptData)
327-
{
328-
str = StringUtils.Decrypt(str, StringUtils.EncryptKey);
329-
}
330-
return type switch
331-
{
332-
DataType.UnicodeText => str,
333-
DataType.RichText => str,
334-
DataType.Image => str.ToBitmapImage(),
335-
DataType.Files => str.Split('\n'),
336-
_ => null
337-
};
338-
}
339-
340-
static string UnicodeTextToString(string str, bool encryptData)
341-
{
342-
if (string.IsNullOrEmpty(str))
343-
{
344-
return string.Empty;
345-
}
346-
if (encryptData)
347-
{
348-
str = StringUtils.Decrypt(str, StringUtils.EncryptKey);
349-
}
350-
return str;
351-
}
352-
353324
var data = record.DataMd5B64;
354325
var type = (DataType)record.DataType;
355326
var encrypt = record.EncryptData;
@@ -362,10 +333,74 @@ static string UnicodeTextToString(string str, bool encryptData)
362333
CachedImagePath = record.CachedImagePath,
363334
Pinned = record.Pinned,
364335
Saved = true,
365-
UnicodeText = UnicodeTextToString(record.UnicodeText, encrypt)
336+
UnicodeText = StringToUnicodeText(record.UnicodeText, encrypt)
337+
};
338+
}
339+
340+
/// <summary>
341+
/// Convert the json clipboard data to a clipboard data for selecting.
342+
/// DataB64 is the base64 encoded data.
343+
/// Json data isn't encrypted for importing.
344+
/// </summary>
345+
/// <param name="data">
346+
/// The json clipboard data to convert.
347+
/// </param>
348+
/// <param name="saved">
349+
/// Whether the data is saved to database.
350+
/// </param>
351+
/// <returns>
352+
/// The clipboard data converted from the json clipboard data.
353+
/// </returns>
354+
public static ClipboardData FromJsonClipboardData(JsonClipboardData data, bool saved)
355+
{
356+
var type = data.DataType;
357+
var encrypt = data.EncryptData;
358+
return new ClipboardData(StringToData(data.DataB64, type, false), type, encrypt)
359+
{
360+
HashId = data.HashId,
361+
SenderApp = data.SenderApp,
362+
InitScore = data.InitScore,
363+
CreateTime = data.CreateTime,
364+
CachedImagePath = data.CachedImagePath,
365+
Pinned = data.Pinned,
366+
Saved = saved,
367+
UnicodeText = StringToUnicodeText(data.UnicodeText, false)
368+
};
369+
}
370+
371+
#region Private
372+
373+
private static object? StringToData(string str, DataType type, bool encryptData)
374+
{
375+
if (!string.IsNullOrEmpty(str) && encryptData)
376+
{
377+
str = StringUtils.Decrypt(str, StringUtils.EncryptKey);
378+
}
379+
return type switch
380+
{
381+
DataType.UnicodeText => str,
382+
DataType.RichText => str,
383+
DataType.Image => str.ToBitmapImage(),
384+
DataType.Files => str.Split('\n'),
385+
_ => null
366386
};
367387
}
368388

389+
private static string StringToUnicodeText(string str, bool encryptData)
390+
{
391+
if (string.IsNullOrEmpty(str))
392+
{
393+
return string.Empty;
394+
}
395+
if (encryptData)
396+
{
397+
str = StringUtils.Decrypt(str, StringUtils.EncryptKey);
398+
}
399+
return str;
400+
}
401+
402+
#endregion
403+
369404
#endregion
370405

371406
#region Display Information
@@ -604,9 +639,10 @@ public override string ToString()
604639
public class JsonClipboardData
605640
{
606641
public string HashId { get; set; } = string.Empty;
607-
public string DataString { get; set; } = string.Empty;
642+
public string DataB64 { get; set; } = string.Empty;
608643
public string DataMd5 { get; set; } = string.Empty;
609644
public DataType DataType { get; set; }
645+
public bool EncryptData { get; set; }
610646
public string SenderApp { get; set; } = string.Empty;
611647
public int InitScore { get; set; }
612648
public DateTime CreateTime { get; set; }
@@ -620,9 +656,10 @@ public static JsonClipboardData FromClipboardData(ClipboardData data)
620656
return new JsonClipboardData()
621657
{
622658
HashId = data.HashId,
623-
DataString = data.DataToString(false)!,
659+
DataB64 = data.DataToString(false)!,
624660
DataMd5 = data.DataMd5,
625661
DataType = data.DataType,
662+
EncryptData = data.EncryptData,
626663
SenderApp = data.SenderApp,
627664
InitScore = data.InitScore,
628665
CreateTime = data.CreateTime,

src/Flow.Launcher.Plugin.ClipboardPlus.Core/Helpers/DatabaseHelper.cs

+27
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,31 @@ public static async Task ExportDatabase(SqliteDatabase database, string jsonPath
1212
await using FileStream createStream = File.Create(jsonPath);
1313
await JsonSerializer.SerializeAsync(createStream, jsonRecords, options);
1414
}
15+
16+
public static async Task ImportDatabase(SqliteDatabase database, string jsonPath)
17+
{
18+
await using FileStream openStream = File.OpenRead(jsonPath);
19+
List<JsonClipboardData>? jsonRecords = null;
20+
try
21+
{
22+
jsonRecords = await JsonSerializer.DeserializeAsync<List<JsonClipboardData>>(openStream);
23+
}
24+
catch (Exception)
25+
{
26+
// ignored
27+
}
28+
if (jsonRecords != null)
29+
{
30+
var records = jsonRecords.Select(r => ClipboardData.FromJsonClipboardData(r, true));
31+
var databaseRecords = await database.GetAllRecordsAsync();
32+
if (!databaseRecords.Any())
33+
{
34+
// TODO: Add records
35+
}
36+
else
37+
{
38+
// TODO: Merge records
39+
}
40+
}
41+
}
1542
}

src/Flow.Launcher.Plugin.ClipboardPlus.Panels/Views/SettingsPanel.xaml

+2-1
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,8 @@
367367
HorizontalAlignment="Left"
368368
VerticalAlignment="Center"
369369
Command="{Binding ImportJsonRecordsCommand}"
370-
Content="{DynamicResource flowlauncher_plugin_clipboardplus_import_records}" />
370+
Content="{DynamicResource flowlauncher_plugin_clipboardplus_import_records}"
371+
IsEnabled="False" />
371372
<Button
372373
Margin="5,0,0,0"
373374
HorizontalAlignment="Left"

0 commit comments

Comments
 (0)