Skip to content

Commit d63ae71

Browse files
Added a progress bar for time-consuming imports; Several minor fixes;
1 parent a241766 commit d63ae71

22 files changed

Lines changed: 1644 additions & 49 deletions

.github/workflows/release.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ jobs:
5757
git fetch origin master
5858
CHANGED_FILES=$(git diff --name-only HEAD^ HEAD)
5959
echo "Changed files: $CHANGED_FILES"
60-
if echo "$CHANGED_FILES" | grep -qE "${{ env.BUILD_PATH }}/CustomSettings.config|${{ env.BUILD_PATH }}/CalibreImport.dll|${{ env.BUILD_PATH }}/Setup.ps1"; then
60+
if echo "$CHANGED_FILES" | grep -qE "${{ env.BUILD_PATH }}/CalibreImport.config|${{ env.BUILD_PATH }}/CalibreImport.dll|${{ env.BUILD_PATH }}/Setup.ps1"; then
6161
echo "FILES_CHANGED=true" >> $GITHUB_ENV
6262
else
6363
echo "FILES_CHANGED=false" >> $GITHUB_ENV
@@ -77,7 +77,7 @@ jobs:
7777
if: env.FILES_CHANGED == 'true'
7878
run: |
7979
mkdir -p release
80-
cp $BUILD_PATH/CustomSettings.config $BUILD_PATH/CalibreImport.dll $BUILD_PATH/Setup.ps1 release/
80+
cp $BUILD_PATH/CalibreImport.config $BUILD_PATH/CalibreImport.dll $BUILD_PATH/Setup.ps1 release/
8181
zip -r ImportToCalibreExtension-v$NEW_VERSION.zip release/
8282
echo "ZIP_FILE=ImportToCalibreExtension-v$NEW_VERSION.zip" >> $GITHUB_ENV
8383

CalibreImport/CalibreContextMenuExtension.cs

Lines changed: 81 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using System.Threading;
1414
using System.Windows.Forms;
1515
using System;
16+
using System.Threading.Tasks;
1617

1718

1819
namespace CalibreImport
@@ -168,7 +169,9 @@ protected override ContextMenuStrip CreateMenu()
168169
foreach (var library in libraries)
169170
{
170171
var libraryItem = new ToolStripMenuItem(library.Name);
171-
libraryItem.Click += (sender, args) => ExecuteImport(library.Path);
172+
//
173+
//libraryItem.Click += (sender, args) => ExecuteImport(library.Path);
174+
libraryItem.Click += (sender, args) => ExecuteImportWithProgress(library.Path, submenu);
172175
libraryItem.Image = GetIconFromBase64(BooksIconBase64);
173176
submenu.DropDownItems.Add(libraryItem);
174177
}
@@ -202,8 +205,8 @@ protected override ContextMenuStrip CreateMenu()
202205
return menu;
203206
}
204207

205-
// Execute the import process
206-
private void ExecuteImport(string selectedLibrary = null)
208+
// Handles ebook import
209+
private void ExecuteImport(string selectedLibrary = null, Action<int> reportProgress = null)
207210
{
208211
try
209212
{
@@ -235,6 +238,7 @@ private void ExecuteImport(string selectedLibrary = null)
235238
}
236239
else
237240
{
241+
// message prompt on whether to close calibre, otherwise break import
238242
if (MessageBox.Show(ResourceStrings.CalibreRunningRes, ResourceStrings.CalibreRunning2Res, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
239243
{
240244
KillCalibre();
@@ -252,7 +256,7 @@ private void ExecuteImport(string selectedLibrary = null)
252256
var libraries = GetCalibreLibraries();
253257
if (libraries == null || !libraries.Any())
254258
{
255-
Logger.LogThis("No Calibre libraries found. Ensure Calibre is configured properly.");
259+
// Logger.LogThis("No Calibre libraries found. Ensure Calibre is configured properly.");
256260
return;
257261
}
258262

@@ -270,19 +274,31 @@ private void ExecuteImport(string selectedLibrary = null)
270274
}
271275
}
272276

277+
int totalFiles = filePaths.Count;
278+
for (int i = 0; i < totalFiles; i++)
279+
{
280+
// Import each file
281+
ImportFileIntoCalibre(selectedLibrary, filePaths[i]);
282+
283+
// Report progress
284+
reportProgress?.Invoke((i + 1) * 100 / totalFiles);
285+
}
286+
273287
var importSuccess = ImportFilesIntoCalibre(selectedLibrary, filePaths);
274288

275289
if (importSuccess)
276290
{
277-
var result = MessageBox.Show(ResourceStrings.CalibreRunningRes, ResourceStrings.CalibreRunning2Res, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
291+
// MessageBox after successful import, prompt the user to start calibre
292+
var result = MessageBox.Show(ResourceStrings.ImportSuccessRes, ResourceStrings.NameAppRes, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
278293
if (result == DialogResult.Yes)
279294
{
280295
LaunchCalibre(selectedLibrary);
281296
}
282297
}
283298
else
284299
{
285-
MessageBox.Show(ResourceStrings.CalibreRunningRes, ResourceStrings.CalibreRunning2Res, MessageBoxButtons.OK, MessageBoxIcon.Error);
300+
// otherwise invite to check log
301+
MessageBox.Show(ResourceStrings.ImportFailureRes, ResourceStrings.NameAppRes, MessageBoxButtons.OK, MessageBoxIcon.Error);
286302
}
287303
}
288304
catch (Exception ex)
@@ -291,6 +307,26 @@ private void ExecuteImport(string selectedLibrary = null)
291307
}
292308
}
293309

310+
// handles ebook import with progress bar
311+
private void ExecuteImportWithProgress(string selectedLibrary, ToolStripMenuItem parentMenu)
312+
{
313+
var progressForm = new ProgressForm();
314+
progressForm.Show();
315+
316+
Task.Run(() =>
317+
{
318+
ExecuteImport(selectedLibrary, progress =>
319+
{
320+
progressForm.UpdateProgress(progress);
321+
});
322+
323+
progressForm.Invoke(new Action(() =>
324+
{
325+
progressForm.Close();
326+
}));
327+
});
328+
}
329+
294330
// Load ebook extensions supported by Calibre from Registry
295331
private void LoadSupportedExtensions()
296332
{
@@ -423,7 +459,7 @@ public void ShowSettingsForm()
423459
}
424460
}
425461

426-
// Import ebook files into Calibre library
462+
// Import multiple ebook files into Calibre library
427463
private bool ImportFilesIntoCalibre(string libraryPath, List<string> filePaths)
428464
{
429465
try
@@ -462,6 +498,44 @@ private bool ImportFilesIntoCalibre(string libraryPath, List<string> filePaths)
462498
}
463499
}
464500

501+
// Import one single ebook file into Calibre library
502+
private void ImportFileIntoCalibre(string libraryPath, string filePath)
503+
{
504+
try
505+
{
506+
Logger.LogThis($"Importing file: {filePath} into library: {libraryPath}", true);
507+
508+
var arguments = $"add --library-path \"{libraryPath}\" --automerge {automerge} \"{filePath}\"";
509+
var startInfo = new ProcessStartInfo
510+
{
511+
FileName = calibredbPath,
512+
Arguments = arguments,
513+
UseShellExecute = false,
514+
RedirectStandardOutput = true,
515+
RedirectStandardError = true,
516+
CreateNoWindow = true
517+
};
518+
519+
var process = Process.Start(startInfo);
520+
var output = process.StandardOutput.ReadToEnd();
521+
var error = process.StandardError.ReadToEnd();
522+
process.WaitForExit();
523+
524+
if (process.ExitCode == 0)
525+
{
526+
Logger.LogThis($"Successfully imported file: {filePath}");
527+
}
528+
else
529+
{
530+
Logger.LogThis($"Failed to import file: {filePath}. Error: {error}");
531+
}
532+
}
533+
catch (Exception ex)
534+
{
535+
Logger.LogThis($"Error importing file: {filePath}. Exception: {ex.Message}");
536+
}
537+
}
538+
465539
// Launch Calibre with specified library
466540
private void LaunchCalibre(string libraryPath)
467541
{

CalibreImport/CalibreImport.csproj

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
Release Configuration Property Group
3737
-->
3838
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
39-
<DebugType>pdbonly</DebugType>
39+
<DebugType>none</DebugType>
4040
<Optimize>true</Optimize>
4141
<OutputPath>bin\Release\</OutputPath>
4242
<DefineConstants>TRACE</DefineConstants>
@@ -63,7 +63,7 @@
6363
<OutputPath>bin\x64\Release\</OutputPath>
6464
<DefineConstants>TRACE</DefineConstants>
6565
<Optimize>true</Optimize>
66-
<DebugType>pdbonly</DebugType>
66+
<DebugType>none</DebugType>
6767
<PlatformTarget>x64</PlatformTarget>
6868
<ErrorReport>prompt</ErrorReport>
6969
</PropertyGroup>
@@ -121,12 +121,18 @@
121121
<Compile Include="Locales.cs" />
122122
<Compile Include="Logger.cs" />
123123
<Compile Include="Program.cs" />
124+
<Compile Include="ProgressForm.cs">
125+
<SubType>Form</SubType>
126+
</Compile>
127+
<Compile Include="ProgressForm.Designer.cs">
128+
<DependentUpon>ProgressForm.cs</DependentUpon>
129+
</Compile>
124130
<Compile Include="Properties\AssemblyInfo.cs" />
125131
<Compile Include="Properties\Settings.Designer.cs">
126132
<AutoGen>True</AutoGen>
127133
<DesignTimeSharedInput>True</DesignTimeSharedInput>
128134
<DependentUpon>Settings.settings</DependentUpon>
129-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
135+
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
130136
</Compile>
131137
<Compile Include="ResourceStrings.cs" />
132138
<Compile Include="SettingsForm.cs">
@@ -144,6 +150,9 @@
144150
<DependentUpon>ImportForm.cs</DependentUpon>
145151
<SubType>Designer</SubType>
146152
</EmbeddedResource>
153+
<EmbeddedResource Include="ProgressForm.resx">
154+
<DependentUpon>ProgressForm.cs</DependentUpon>
155+
</EmbeddedResource>
147156
<EmbeddedResource Include="SettingsForm.resx">
148157
<DependentUpon>SettingsForm.cs</DependentUpon>
149158
<SubType>Designer</SubType>
@@ -153,20 +162,20 @@
153162
Extra Files included in the Project (some in the Output too)
154163
-->
155164
<ItemGroup>
156-
<None Include="app.config" />
165+
<None Include="app.config" />
157166
<None Include="CalibreImportKey.snk" />
158167
<None Include="PostBuild.ps1" />
159168
<None Include="Properties\Settings.settings">
160169
<Generator>SettingsSingleFileGenerator</Generator>
161170
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
162171
</None>
163172
<None Include="ReleaseFiles\CalibreImport.dll" />
164-
<None Include="ReleaseFiles\CustomSettings.config" />
173+
<None Include="ReleaseFiles\CalibreImport.config" />
165174
<None Include="ReleaseFiles\Setup.ps1" />
166175
<None Include="Setup.ps1">
167176
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
168177
</None>
169-
<None Include="CustomSettings.config">
178+
<None Include="CalibreImport.config">
170179
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
171180
</None>
172181
</ItemGroup>

CalibreImport/CustomSettings.cs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
//using System;
1+
using System;
22
using System.Configuration;
33
using System.IO;
4+
using System.Reflection;
45

56
namespace CalibreImport
67
{
@@ -10,7 +11,24 @@ public static class CustomSettings
1011

1112
static CustomSettings()
1213
{
13-
string configFilePath = Path.Combine(Path.GetDirectoryName(typeof(CalibreContextMenuExtension).Assembly.Location), "CustomSettings.config");
14+
string configFilePath;
15+
string appDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
16+
string assemblyFolder = Path.GetDirectoryName(typeof(CalibreContextMenuExtension).Assembly.Location);
17+
string assemblyName = Assembly.GetExecutingAssembly().GetName().Name;
18+
string configFileName = $"{assemblyName}.config";
19+
string portableFilePath = Path.Combine(assemblyFolder, "portable");
20+
21+
if (File.Exists(portableFilePath))
22+
{
23+
configFilePath = Path.Combine(assemblyFolder, configFileName);
24+
//Logger.LogThis("Portable mode detected. Using configuration file in the same folder as the DLL.", true);
25+
}
26+
else
27+
{
28+
configFilePath = Path.Combine(appDataFolder, "YourAppName", configFileName);
29+
//Logger.LogThis("Portable mode not detected. Using configuration file in AppData.", true);
30+
}
31+
1432
ExeConfigurationFileMap configMap = new ExeConfigurationFileMap { ExeConfigFilename = configFilePath };
1533

1634
if (!File.Exists(configFilePath))
@@ -47,7 +65,5 @@ private static void CreateDefaultConfig(string configFilePath)
4765
config.Save(ConfigurationSaveMode.Modified);
4866
ConfigurationManager.RefreshSection(config.AppSettings.SectionInformation.Name);
4967
}
50-
5168
}
5269
}
53-

CalibreImport/ImportForm.Designer.cs

Lines changed: 18 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)