Skip to content

Commit

Permalink
使用IniParser库而不是使用kernel32提供的方法
Browse files Browse the repository at this point in the history
  • Loading branch information
ErrOwk committed Sep 17, 2024
1 parent 3f09854 commit 7edbd9f
Show file tree
Hide file tree
Showing 6 changed files with 228 additions and 196 deletions.
3 changes: 2 additions & 1 deletion LogBook-WPF/LogBook-WPF.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="ErrOwk.IniParser" Version="1.1.1" />
<PackageReference Include="iNKORE.UI.WPF" Version="1.2.7.1" />
<PackageReference Include="iNKORE.UI.WPF.Modern" Version="0.9.30" />
<PackageReference Include="sqlite-net-pcl" Version="1.9.172" />
<PackageReference Include="SQLitePCLRaw.bundle_green" Version="2.1.9" />
<PackageReference Include="SQLitePCLRaw.bundle_green" Version="2.1.10" />
</ItemGroup>

</Project>
93 changes: 58 additions & 35 deletions LogBook-WPF/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
using iNKORE.UI.WPF.Helpers;
using LogBook_WPF.Models;
using Microsoft.Win32;
using SQLite;
using System;
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
Expand All @@ -19,25 +15,29 @@
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml.Linq;
using ErrOwk.IniParser;
using iNKORE.UI.WPF.Helpers;
using LogBook_WPF.Models;
using Microsoft.Win32;
using SQLite;
using Windows.System;

namespace LogBook_WPF
{

/// <summary>
/// 主页面
/// </summary>
public partial class MainWindow : Window
{
internal SQLiteConnection conn;

public IniFile configFile;
public IniParser iniFile;

public MainWindow()
{
Directory.CreateDirectory("Data");
InitializeComponent();
configFile = new IniFile("Data\\config.ini");
iniFile = new IniParser("Data\\config.ini");
conn = new SQLiteConnection("Data\\QSO.db3");
conn.CreateTable<QSO>();
UpdateDgv();
Expand Down Expand Up @@ -66,11 +66,8 @@ private void UpdateDgv()
qsoList.Reverse();
dgvQSOs.ItemsSource = qsoList;
//更新列表

}



private void dgvQSOs_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (dgvQSOs.SelectedIndex != -1)
Expand All @@ -79,7 +76,9 @@ private void dgvQSOs_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
qsoList = conn.Table<QSO>().ToList<QSO>();
//获取QSO列表

Window updateQSO = new UpdateQSO(qsoList[qsoList.Count - dgvQSOs.SelectedIndex - 1]);
Window updateQSO = new UpdateQSO(
qsoList[qsoList.Count - dgvQSOs.SelectedIndex - 1]
);
updateQSO.ShowDialog();
//修改选中项

Expand All @@ -89,7 +88,6 @@ private void dgvQSOs_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)

private void exportQSO_Click(object sender, RoutedEventArgs e)
{

SaveFileDialog dlg = new SaveFileDialog();
dlg.Title = "保存.ADI文件";
dlg.FileName = "exportQSO.adi";
Expand All @@ -110,13 +108,15 @@ private void exportQSO_Click(object sender, RoutedEventArgs e)
private void exportToQRZ_Click(object sender, RoutedEventArgs e)
{
string originPwd = "";

try
{
//解密密码
StreamReader sr = new StreamReader("Data\\qrzPwd");
Byte[] protectedPwd = Convert.FromBase64String(sr.ReadToEnd());
originPwd = Encoding.UTF8.GetString(ProtectedData.Unprotect(protectedPwd, null, DataProtectionScope.CurrentUser));
originPwd = Encoding.UTF8.GetString(
ProtectedData.Unprotect(protectedPwd, null, DataProtectionScope.CurrentUser)
);
}
catch (Exception ex)
{
Expand All @@ -130,19 +130,22 @@ private void exportToQRZ_Click(object sender, RoutedEventArgs e)
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "qrz_auto_import_from_lotw.exe";

if (string.IsNullOrEmpty(configFile.Read("qrzCallsign", "Upload")))
if (string.IsNullOrEmpty(iniFile.Get("Upload", "qrzCallsign")))
{
MessageBox.Show("无法获取到呼号,是否在设置中配置了呼号?");
return;
};
}
;

if (string.IsNullOrEmpty(originPwd))
{
MessageBox.Show("无法获取到密码,是否保存了密码?");
return;
};
}
;

startInfo.Arguments = $"{configFile.Read("qrzCallsign", "QRZ")} {originPwd} --adif exportqrz.adi";
startInfo.Arguments =
$"{iniFile.Get("Upload", "qrzCallsign")} {originPwd} --adif exportqrz.adi";

Process.Start(startInfo);

Expand Down Expand Up @@ -175,19 +178,23 @@ private void exportADIF(string path)

foreach (QSO qso in qsoList)
{

//// 添加QSO记录
sw.WriteLine("<CALL:" + qso.toCallsign.Length + ">" + qso.toCallsign);
sw.WriteLine(" <MODE:" + qso.mode.Length + ">" + qso.mode);
sw.WriteLine(" <QSO_DATE:8>" + qso.UTCTime.ToString("yyyyMMdd"));
sw.WriteLine(" <TIME_ON:6>" + qso.UTCTime.ToString("HHmmss"));
sw.WriteLine(" <FREQ:" + qso.freq.ToString().Length + ">" + qso.freq.ToString());
sw.WriteLine(" <BAND:" + qso.band.Length + ">" + qso.band);
sw.WriteLine(" <BAND:" + qso.band!.Length + ">" + qso.band);

if (!string.IsNullOrEmpty(qso.freq_rx.ToString()))
{
sw.WriteLine(" <BAND_RX:" + qso.band_rx.Length + ">" + qso.band_rx);
sw.WriteLine(" <FREQ_RX:" + qso.freq_rx.ToString().Length + ">" + qso.freq_rx.ToString());
sw.WriteLine(" <BAND_RX:" + qso.band_rx!.Length + ">" + qso.band_rx);
sw.WriteLine(
" <FREQ_RX:"
+ qso.freq_rx.ToString()!.Length
+ ">"
+ qso.freq_rx.ToString()
);
}

if (!string.IsNullOrEmpty(qso.selfGrid))
Expand All @@ -202,12 +209,19 @@ private void exportADIF(string path)

if (!string.IsNullOrEmpty(qso.selfWatt.ToString()))
{
sw.WriteLine(" <TX_PWR:" + qso.selfWatt.ToString().Length + ">" + qso.selfWatt.ToString());
sw.WriteLine(
" <TX_PWR:"
+ qso.selfWatt.ToString()!.Length
+ ">"
+ qso.selfWatt.ToString()
);
}

if (!string.IsNullOrEmpty(qso.toWatt.ToString()))
{
sw.WriteLine(" <RX_PWR:" + qso.toWatt.ToString().Length + ">" + qso.toWatt.ToString());
sw.WriteLine(
" <RX_PWR:" + qso.toWatt.ToString()!.Length + ">" + qso.toWatt.ToString()
);
}

if (!string.IsNullOrEmpty(qso.prop_mode))
Expand Down Expand Up @@ -238,23 +252,34 @@ private void settings_Click(object sender, RoutedEventArgs e)
private void exportToLoTW_Click(object sender, RoutedEventArgs e)
{
exportADIF("exportlotw.adi");
if (string.IsNullOrEmpty(configFile.Read("tqslAddr", "Upload")))
if (string.IsNullOrEmpty(iniFile.Get("Upload", "tqslAddr")))
{
MessageBox.Show("无法获取TQSL文件地址,是否在设置中配置了TQSL可执行文件地址?");
return;
};
}
;

if (string.IsNullOrEmpty(configFile.Read("tqslStationName", "Upload")))
if (string.IsNullOrEmpty(iniFile.Get("Upload", "tqslStationName")))
{
MessageBox.Show("无法获取到台站名称,是否在设置中配置了台站名称?(台站名称应当填入TQSL应用中的台站地址)");
MessageBox.Show(
"无法获取到台站名称,是否在设置中配置了台站名称?(台站名称应当填入TQSL应用中的台站地址)"
);
return;
};
}
;

//工作方式参考https://gitee.com/yuzhenwu/x-qsl-amateur-radio-adif-tool,感谢并致以诚挚的73!
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "cmd.exe";
startInfo.WorkingDirectory = System.IO.Path.GetDirectoryName(configFile.Read("tqslAddr", "Upload"));
startInfo.Arguments = "/C \"tqsl -q -l \"" + configFile.Read("tqslStationName", "Upload") + "\" -p \"Insecure\" -a all -u -d \""+ System.Environment.CurrentDirectory +"\\exportlotw.adi\" 2>temp.txt ";
startInfo.WorkingDirectory = System.IO.Path.GetDirectoryName(
iniFile.Get("Upload", "tqslAddr")
);
startInfo.Arguments =
"/C \"tqsl -q -l \""
+ iniFile.Get("Upload", "tqslStationName")
+ "\" -p \"Insecure\" -a all -u -d \""
+ System.Environment.CurrentDirectory
+ "\\exportlotw.adi\" 2>temp.txt ";
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;
startInfo.StandardOutputEncoding = Encoding.Default;
Expand All @@ -266,8 +291,6 @@ private void exportToLoTW_Click(object sender, RoutedEventArgs e)
process.WaitForExit();

MessageBox.Show("导出任务执行完成,请打开LoTW网页核对是否正确导出");


}
}
}
}
55 changes: 0 additions & 55 deletions LogBook-WPF/Models/IniFile.cs

This file was deleted.

35 changes: 18 additions & 17 deletions LogBook-WPF/NewQSO.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ErrOwk.IniParser;
using LogBook_WPF.Models;
using Microsoft.Win32.SafeHandles;
using SQLite;
Expand All @@ -31,7 +32,7 @@ public partial class NewQSO : Window

public SQLiteConnection conn;

public IniFile configFile = new IniFile("Data\\config.ini");
public IniParser iniFile = new IniParser("Data\\config.ini");

public NewQSO()
{
Expand Down Expand Up @@ -60,13 +61,13 @@ public NewQSO()
conn.CreateTable<QSO>();
//配置SQLite

selfCallsign.Text = configFile.Read("selfCallsign");
selfGrid.Text = configFile.Read("selfGrid");
selfWX.Text = configFile.Read("selfWX");
selfRIG.Text = configFile.Read("selfRIG");
selfANT.Text = configFile.Read("selfANT");
selfWatt.Text = configFile.Read("selfWatt");
selfQTH.Text = configFile.Read("selfQTH");
selfCallsign.Text = iniFile.Get("LogBook - WPF","selfCallsign");
selfGrid.Text = iniFile.Get("LogBook - WPF", "selfGrid");
selfWX.Text = iniFile.Get("LogBook - WPF", "selfWX");
selfRIG.Text = iniFile.Get("LogBook - WPF", "selfRIG");
selfANT.Text = iniFile.Get("LogBook - WPF", "selfANT");
selfWatt.Text = iniFile.Get("LogBook - WPF", "selfWatt");
selfQTH.Text = iniFile.Get("LogBook - WPF", "selfQTH");
//从ini文件中读取保存的信息
}

Expand Down Expand Up @@ -165,13 +166,13 @@ private void qsoSave_Click(object sender, RoutedEventArgs e)
conn.Insert(newQSO);
//将数据转换并检验后存入数据库中

configFile.Write("selfCallsign", selfCallsign.Text);
configFile.Write("selfGrid", selfGrid.Text);
configFile.Write("selfWX", selfWX.Text);
configFile.Write("selfRIG", selfRIG.Text);
configFile.Write("selfANT", selfANT.Text);
configFile.Write("selfWatt", selfWatt.Text);
configFile.Write("selfQTH", selfQTH.Text);
iniFile.Update("LogBook - WPF", "selfCallsign", selfCallsign.Text);
iniFile.Update("LogBook - WPF", "selfGrid", selfGrid.Text);
iniFile.Update("LogBook - WPF", "selfWX", selfWX.Text);
iniFile.Update("LogBook - WPF", "selfRIG", selfRIG.Text);
iniFile.Update("LogBook - WPF", "selfANT", selfANT.Text);
iniFile.Update("LogBook - WPF", "selfWatt", selfWatt.Text);
iniFile.Update("LogBook - WPF", "selfQTH", selfQTH.Text);
//向ini文件保存信息

this.Close();
Expand Down Expand Up @@ -283,7 +284,7 @@ private string getTxFreq(string sat)
};

string txFreq;
if (SatelliteFrequencies.TryGetValue(sat, out txFreq))
if (SatelliteFrequencies.TryGetValue(sat, out txFreq!))
{
return txFreq;
}
Expand Down Expand Up @@ -322,7 +323,7 @@ private string getRxFreq(string sat)
};

string rxFreq;
if (SatelliteFrequencies.TryGetValue(sat, out rxFreq))
if (SatelliteFrequencies.TryGetValue(sat, out rxFreq!))
{
return rxFreq;
}
Expand Down
Loading

0 comments on commit 7edbd9f

Please sign in to comment.