@@ -100,16 +100,17 @@ def _show_kobotouch_message_if_enabled(self):
100
100
)
101
101
config .prefs ["show_kobotouch_message" ] = show_message
102
102
103
- def metaguide_selection_format (self , format_to_find : str , * , remove_metaguiding : bool = False ):
104
- from calibre .gui2 import error_dialog
105
-
106
- # Show KoboTouch message if enabled
107
- self ._show_kobotouch_message_if_enabled ()
108
-
109
- # warn the user that remove_metaguiding is an EXPERIMENTAL feature and
110
- # that it may not work as expected.
103
+ def _show_warning_dialog (self , remove_metaguiding : bool ) -> bool :
104
+ """Show a warning dialog to the user before proceeding with metaguiding operations.
105
+
106
+ Args:
107
+ remove_metaguiding: Whether we're removing metaguiding (True) or adding it (False)
108
+
109
+ Returns:
110
+ bool: True if the user wants to proceed, False if they want to cancel
111
+ """
111
112
if remove_metaguiding :
112
- if not question_dialog (
113
+ return question_dialog (
113
114
self .gui ,
114
115
"Remove metaguiding is an EXPERIMENTAL feature" ,
115
116
"This feature is still in development and may not work as expected. "
@@ -119,10 +120,9 @@ def metaguide_selection_format(self, format_to_find: str, *, remove_metaguiding:
119
120
"Do you want to continue?" ,
120
121
show_copy_button = True ,
121
122
default_yes = False ,
122
- ):
123
- return
123
+ )
124
124
else :
125
- if not question_dialog (
125
+ return question_dialog (
126
126
self .gui ,
127
127
"Metaguiding will modify your files" ,
128
128
"This feature will modify your files by adding metaguiding. "
@@ -132,52 +132,92 @@ def metaguide_selection_format(self, format_to_find: str, *, remove_metaguiding:
132
132
"Do you want to continue?" ,
133
133
show_copy_button = True ,
134
134
default_yes = False ,
135
- ):
136
- return
135
+ )
136
+
137
+ def _process_single_book (self , current_database , book_id , format_to_find , action_text , remove_metaguiding ):
138
+ from calibre .gui2 import error_dialog
139
+ """Process a single book for metaguiding operations.
140
+
141
+ Args:
142
+ current_database: The current calibre database
143
+ book_id: The ID of the book to process
144
+ format_to_find: The format to look for (epub/kepub)
145
+ action_text: Text describing the action being performed
146
+ remove_metaguiding: Whether to remove metaguiding
147
+
148
+ Returns:
149
+ bool: True if successful, False if an error occurred
150
+ """
151
+ temp_file = current_database .format (book_id , format_to_find , as_path = True )
152
+ book_title = current_database .field_for ("title" , book_id )
153
+ common .log .debug ("Converting book id: %d, format: %s" % (book_id , format_to_find ))
154
+
155
+ self .gui .status_bar .show_message (f'{ action_text .title ()} "{ book_title } "...' , 500 )
156
+ try :
157
+
158
+ if metaguiding .is_file_metaguided (temp_file ):
159
+ common .log .debug (f"File { temp_file } is already metaguided, skipping." )
160
+ # Show warning dialog about metaguided epub performance on Kobo
161
+ self .gui .status_bar .show_message (
162
+ f'"{ book_title } " is already metaguided. Skipping...' , 1000
163
+ )
164
+ return True
165
+
166
+ metaguiding .metaguide_epub_file (temp_file , temp_file , remove_metaguiding = remove_metaguiding )
167
+ except Exception as e : # pylint: disable=broad-except
168
+ common .log .error ("Error processing book id: %d, format: %s" % (book_id , format_to_find ))
169
+ common .log .error (e )
170
+ self .gui .status_bar .show_message (f'{ action_text } "{ book_title } failed!": { str (e )} ' , 5000 )
171
+ error_dialog (
172
+ self .gui ,
173
+ f"Cannot { action_text } . Please verify that the epub is valid." ,
174
+ "Error processing book id: %d, format: %s, error details: %s" % (book_id , format_to_find , e ),
175
+ show = True ,
176
+ )
177
+ return False
178
+
179
+ self .gui .status_bar .show_message (f'{ action_text } "{ book_title } success."' , 3000 )
180
+ current_database .save_original_format (book_id , format_to_find )
181
+ result = current_database .add_format (book_id , format_to_find , temp_file , replace = True , run_hooks = False )
182
+
183
+ if not result :
184
+ error_dialog (
185
+ self .gui ,
186
+ f"Failed to { action_text } " ,
187
+ f"Failed to { action_text } format %s to book id %d" % (format_to_find , book_id ),
188
+ show = True ,
189
+ )
190
+ return False
191
+
192
+ return True
193
+
194
+ def metaguide_selection_format (self , format_to_find : str , * , remove_metaguiding : bool = False ):
195
+ from calibre .gui2 import error_dialog
196
+
197
+ # Show KoboTouch message if enabled
198
+ self ._show_kobotouch_message_if_enabled ()
199
+
200
+ # Show warning dialog
201
+ if not self ._show_warning_dialog (remove_metaguiding ):
202
+ return
137
203
138
204
action_text = "remove metaguiding" if remove_metaguiding else "add metaguiding"
139
205
140
206
# Get currently selected books
141
207
selected_rows = self .gui .library_view .selectionModel ().selectedRows ()
142
208
if not selected_rows or len (selected_rows ) == 0 :
143
209
return error_dialog (self .gui , f"Cannot { action_text } " , "No books selected" , show = True )
210
+
144
211
# Map the rows to book ids
145
212
selected_ids = list (map (self .gui .library_view .model ().id , selected_rows ))
146
213
current_database = self .gui .current_db .new_api
147
214
epubs_found_count = 0
215
+
148
216
for book_id in selected_ids :
149
217
if current_database .has_format (book_id , format_to_find ):
150
218
epubs_found_count += 1
151
- temp_file = current_database .format (book_id , format_to_find , as_path = True )
152
- # Get the book title for status messages
153
- book_title = current_database .field_for ("title" , book_id )
154
- common .log .debug ("Converting book id: %d, format: %s" % (book_id , format_to_find ))
155
-
156
- self .gui .status_bar .show_message (f'{ action_text .title ()} "{ book_title } "...' , 1000 )
157
- try :
158
- metaguiding .metaguide_epub_file (temp_file , temp_file , remove_metaguiding = remove_metaguiding )
159
- except Exception as e : # pylint: disable=broad-except
160
- common .log .error ("Error processing book id: %d, format: %s" % (book_id , format_to_find ))
161
- common .log .error (e )
162
- self .gui .status_bar .show_message (f'{ action_text } "{ book_title } failed!": { str (e )} ' , 5000 )
163
- return error_dialog (
164
- self .gui ,
165
- f"Cannot { action_text } . Please verify that the epub is valid." ,
166
- "Error processing book id: %d, format: %s, error details: %s" % (book_id , format_to_find , e ),
167
- show = True ,
168
- )
169
-
170
- self .gui .status_bar .show_message (f'{ action_text } "{ book_title } success."' , 3000 )
171
- current_database .save_original_format (book_id , format_to_find )
172
- result = current_database .add_format (book_id , format_to_find , temp_file , replace = True , run_hooks = False )
173
-
174
- if not result :
175
- return error_dialog (
176
- self .gui ,
177
- f"Failed to { action_text } " ,
178
- f"Failed to { action_text } format %s to book id %d" % (format_to_find , book_id ),
179
- show = True ,
180
- )
219
+ if not self ._process_single_book (current_database , book_id , format_to_find , action_text , remove_metaguiding ):
220
+ return
181
221
182
222
# If we are here, it means that we have processed all the files
183
223
# check if we have processed any files
0 commit comments