1313using System . Threading ;
1414using System . Windows . Forms ;
1515using System ;
16+ using System . Threading . Tasks ;
1617
1718
1819namespace 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 {
0 commit comments