@@ -32,7 +32,13 @@ class BigOcrPdfApp(Adw.Application):
3232
3333 def __init__ (self ):
3434 """Initialize the application"""
35- super ().__init__ (application_id = APP_ID )
35+ super ().__init__ (
36+ application_id = APP_ID ,
37+ flags = Gio .ApplicationFlags .HANDLES_OPEN
38+ )
39+
40+ # Store files to be opened
41+ self ._pending_files = []
3642
3743 # Add command line handling
3844 self .add_main_option (
@@ -42,6 +48,7 @@ def __init__(self):
4248
4349 # Setup signals
4450 self .connect ("activate" , self .on_activate )
51+ self .connect ("open" , self .on_open )
4552 self .connect ("handle-local-options" , self .on_handle_local_options )
4653
4754 # Set up application actions
@@ -111,6 +118,58 @@ def on_activate(self, app: Adw.Application) -> None:
111118 error_dialog .set_detail (str (e ))
112119 error_dialog .show ()
113120
121+ def on_open (self , app : Adw .Application , files : list , n_files : int , hint : str ) -> None :
122+ """Callback for opening files from command line or file manager
123+
124+ Args:
125+ app: The application instance
126+ files: List of GFile objects to open
127+ n_files: Number of files
128+ hint: Hint string (usually empty)
129+ """
130+ try :
131+ # Load custom CSS
132+ load_css ()
133+
134+ # Check if we already have a window open
135+ win = self .get_active_window ()
136+ if not win :
137+ # Create the main window
138+ win = BigOcrPdfWindow (app )
139+
140+ # Show the window
141+ win .present ()
142+
143+ # Extract file paths from GFile objects
144+ file_paths = []
145+ for gfile in files :
146+ path = gfile .get_path ()
147+ if path :
148+ file_paths .append (path )
149+
150+ # Add files to the application using idle_add to ensure UI is ready
151+ if file_paths :
152+ def add_files_when_ready ():
153+ try :
154+ if hasattr (win , 'settings' ):
155+ added = win .settings .add_files (file_paths )
156+ if added > 0 :
157+ logger .info (f"Added { added } file(s) from command line" )
158+ # Refresh the file list UI
159+ if hasattr (win , 'update_file_info' ):
160+ win .update_file_info ()
161+ except Exception as e :
162+ logger .error (f"Error adding files: { e } " )
163+ return False # Don't repeat
164+
165+ # Use a small delay to ensure the window is fully initialized
166+ GLib .timeout_add (100 , add_files_when_ready )
167+
168+ logger .info (_ (f"Opened { n_files } file(s)" ))
169+
170+ except Exception as e :
171+ logger .error (f"{ _ ('Error opening files' )} : { e } " )
172+
114173 def on_about_action (self , _action : Gio .SimpleAction , _param : Any ) -> None :
115174 """Show about dialog
116175
0 commit comments